diff options
Diffstat (limited to 'uisimulator/sdl/kernel.c')
-rw-r--r-- | uisimulator/sdl/kernel.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/uisimulator/sdl/kernel.c b/uisimulator/sdl/kernel.c new file mode 100644 index 0000000000..25f2df220c --- /dev/null +++ b/uisimulator/sdl/kernel.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 by Jens Arnold | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include <stddef.h> | ||
21 | #include "kernel.h" | ||
22 | #include "thread.h" | ||
23 | #include "debug.h" | ||
24 | |||
25 | static void (*tick_funcs[MAX_NUM_TICK_TASKS])(void); | ||
26 | |||
27 | int set_irq_level (int level) | ||
28 | { | ||
29 | static int _lv = 0; | ||
30 | return (_lv = level); | ||
31 | } | ||
32 | |||
33 | void queue_init(struct event_queue *q) | ||
34 | { | ||
35 | q->read = 0; | ||
36 | q->write = 0; | ||
37 | } | ||
38 | |||
39 | void queue_wait(struct event_queue *q, struct event *ev) | ||
40 | { | ||
41 | while(q->read == q->write) | ||
42 | { | ||
43 | switch_thread(); | ||
44 | } | ||
45 | |||
46 | *ev = q->events[(q->read++) & QUEUE_LENGTH_MASK]; | ||
47 | } | ||
48 | |||
49 | void queue_wait_w_tmo(struct event_queue *q, struct event *ev, int ticks) | ||
50 | { | ||
51 | unsigned int timeout = current_tick + ticks; | ||
52 | |||
53 | while(q->read == q->write && TIME_BEFORE( current_tick, timeout )) | ||
54 | { | ||
55 | sleep(1); | ||
56 | } | ||
57 | |||
58 | if(q->read != q->write) | ||
59 | { | ||
60 | *ev = q->events[(q->read++) & QUEUE_LENGTH_MASK]; | ||
61 | } | ||
62 | else | ||
63 | { | ||
64 | ev->id = SYS_TIMEOUT; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | void queue_post(struct event_queue *q, long id, void *data) | ||
69 | { | ||
70 | int wr; | ||
71 | int oldlevel; | ||
72 | |||
73 | oldlevel = set_irq_level(15<<4); | ||
74 | wr = (q->write++) & QUEUE_LENGTH_MASK; | ||
75 | |||
76 | q->events[wr].id = id; | ||
77 | q->events[wr].data = data; | ||
78 | set_irq_level(oldlevel); | ||
79 | } | ||
80 | |||
81 | bool queue_empty(const struct event_queue* q) | ||
82 | { | ||
83 | return ( q->read == q->write ); | ||
84 | } | ||
85 | |||
86 | void queue_clear(struct event_queue* q) | ||
87 | { | ||
88 | /* fixme: This is potentially unsafe in case we do interrupt-like processing */ | ||
89 | q->read = 0; | ||
90 | q->write = 0; | ||
91 | } | ||
92 | |||
93 | void switch_thread (void) | ||
94 | { | ||
95 | yield (); | ||
96 | } | ||
97 | |||
98 | void sim_tick_tasks(void) | ||
99 | { | ||
100 | int i; | ||
101 | |||
102 | /* Run through the list of tick tasks */ | ||
103 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) | ||
104 | { | ||
105 | if(tick_funcs[i]) | ||
106 | { | ||
107 | tick_funcs[i](); | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | int tick_add_task(void (*f)(void)) | ||
113 | { | ||
114 | int i; | ||
115 | |||
116 | /* Add a task if there is room */ | ||
117 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) | ||
118 | { | ||
119 | if(tick_funcs[i] == NULL) | ||
120 | { | ||
121 | tick_funcs[i] = f; | ||
122 | return 0; | ||
123 | } | ||
124 | } | ||
125 | DEBUGF("Error! tick_add_task(): out of tasks"); | ||
126 | return -1; | ||
127 | } | ||
128 | |||
129 | int tick_remove_task(void (*f)(void)) | ||
130 | { | ||
131 | int i; | ||
132 | |||
133 | /* Remove a task if it is there */ | ||
134 | for(i = 0;i < MAX_NUM_TICK_TASKS;i++) | ||
135 | { | ||
136 | if(tick_funcs[i] == f) | ||
137 | { | ||
138 | tick_funcs[i] = NULL; | ||
139 | return 0; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | return -1; | ||
144 | } | ||
145 | |||
146 | void mutex_init(struct mutex *m) | ||
147 | { | ||
148 | (void)m; | ||
149 | } | ||
150 | |||
151 | void mutex_lock(struct mutex *m) | ||
152 | { | ||
153 | (void)m; | ||
154 | } | ||
155 | |||
156 | void mutex_unlock(struct mutex *m) | ||
157 | { | ||
158 | (void)m; | ||
159 | } | ||
160 | |||