1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Felix Arends
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 #include <SDL_thread.h>
27 #include "system-sdl.h"
28 #include "thread-sdl.h"
34 static SDL_TimerID tick_timer_id
;
37 /* Condition to signal that "interrupts" may proceed */
38 static SDL_cond
*sim_thread_cond
;
39 /* Mutex to serialize changing levels and exclude other threads while
41 static SDL_mutex
*sim_irq_mtx
;
42 static int interrupt_level
= HIGHEST_IRQ_LEVEL
;
43 static int handlers_pending
= 0;
44 static int status_reg
= 0;
47 * 1) All threads must pass unblocked
48 * 2) Current handler must always pass unblocked
49 * 3) Threads must be excluded when irq routine is running
50 * 4) No more than one handler routine should execute at a time
52 int set_irq_level(int level
)
54 SDL_LockMutex(sim_irq_mtx
);
56 int oldlevel
= interrupt_level
;
58 if (status_reg
== 0 && level
== 0 && oldlevel
!= 0)
60 /* Not in a handler and "interrupts" are being reenabled */
61 if (handlers_pending
> 0)
62 SDL_CondSignal(sim_thread_cond
);
65 interrupt_level
= level
; /* save new level */
67 SDL_UnlockMutex(sim_irq_mtx
);
71 void sim_enter_irq_handler(void)
73 SDL_LockMutex(sim_irq_mtx
);
76 if(interrupt_level
!= 0)
78 /* "Interrupts" are disabled. Wait for reenable */
79 SDL_CondWait(sim_thread_cond
, sim_irq_mtx
);
85 void sim_exit_irq_handler(void)
87 if (--handlers_pending
> 0)
88 SDL_CondSignal(sim_thread_cond
);
91 SDL_UnlockMutex(sim_irq_mtx
);
94 static bool sim_kernel_init(void)
96 sim_irq_mtx
= SDL_CreateMutex();
97 if (sim_irq_mtx
== NULL
)
99 panicf("Cannot create sim_handler_mtx\n");
103 sim_thread_cond
= SDL_CreateCond();
104 if (sim_thread_cond
== NULL
)
106 panicf("Cannot create sim_thread_cond\n");
113 void sim_kernel_shutdown(void)
115 SDL_RemoveTimer(tick_timer_id
);
116 SDL_DestroyMutex(sim_irq_mtx
);
117 SDL_DestroyCond(sim_thread_cond
);
120 Uint32
tick_timer(Uint32 interval
, void *param
)
127 new_tick
= (SDL_GetTicks() - start_tick
) / (1000/HZ
);
129 while(new_tick
!= current_tick
)
131 sim_enter_irq_handler();
133 /* Run through the list of tick tasks - increments tick
134 * on each iteration. */
137 sim_exit_irq_handler();
143 void tick_start(unsigned int interval_in_ms
)
145 if (!sim_kernel_init())
147 panicf("Could not initialize kernel!");
151 if (tick_timer_id
!= NULL
)
153 SDL_RemoveTimer(tick_timer_id
);
154 tick_timer_id
= NULL
;
158 start_tick
= SDL_GetTicks();
161 tick_timer_id
= SDL_AddTimer(interval_in_ms
, tick_timer
, NULL
);