1     #ifndef THREADSTATE_RUNNING
2
3
4
5     /*
6
7       Constants
8
9     *///
10
11    typedef unsigned char UCHAR;
12
13    typedef unsigned int  UINT;
14
15    typedef unsigned long ULONG;
16
17
18
19    #define THREADSTATE_VOID        0
20
21    #define THREADSTATE_RUNNING     1
22
23    #define THREADSTATE_BLOCKED     2
24
25    #define THREADSTATE_SUSPENDED   3
26
27
28
29    #define THREAD_COUNT            8
30
31    #define SEMAPHORE_COUNT         8
32
33    #define MESSAGE_COUNT           16
34
35    #define OS_DSTACK_SIZE          256
36
37    #define THREADSEM_COUNT         THREAD_COUNT
38
39
40
41    #define ALLTHREAD_START         0x120
42
43    #define ALLSEM_START            ALLTHREAD_START+THREAD_COUNT*sizeof(os_thread_struct)
44
45    #define ALLTHREADSEM_START      ALLSEM_START+SEMAPHORE_COUNT*sizeof(os_semaphore_struct)
46
47    #define ALLMESSAGE_START        ALLTHREADSEM_START+THREADSEM_COUNT*sizeof(os_threadsem_struct)
48
49    #define STACK_START             ALLMESSAGE_START+MESSAGE_COUNT*sizeof(os_message)
50
51
52
53    #define TOTAL_STACK_SIZE        2048-STACK_START-OS_DSTACK_SIZE-192
54
55    /*
56
57      Message Types
58
59    */
60
61
62
63    typedef struct os_message_struct
64
65    {
66
67        void                        *message;
68
69        struct os_message_struct    *next;
70
71    } os_message;
72
73
74
75
76
77    /*
78
79      Thread Types
80
81    */
82
83    typedef struct os_thread_struct
84
85    {
86
87            void                    *hwstack;
88
89            void                    *swstack;
90
91            UCHAR                   priority;
92
93            UINT                    sleep_duration;
94
95            UCHAR                   threadID;
96
97            UCHAR                   state;
98
99            struct os_message       *message;
100
101           struct os_thread_struct *next;
102
103           struct os_thread_struct *prev;
104
105   } os_thread;
106
107
108
109   /*
110
111     Sempahore Types
112
113   */
114
115
116
117   typedef struct os_threadsem_struct
118
119   {
120
121       os_thread                   *thread;
122
123       struct os_threadsem_struct  *next;
124
125   } os_threadsem;
126
127
128
129   typedef struct os_semaphore_struct
130
131   {
132
133       UCHAR                       remaining;
134
135       os_threadsem                *queue;
136
137       struct os_semaphore_struct  *next;
138
139       struct os_semaphore_struct  *prev;
140
141   } os_semaphore;
142
143
144
145
146
147   /*
148
149     OS Variables
150
151   */
152
153   #asm(".EQU SpmcrAddr=0x57")
154
155
156
157   register UINT g_instaddr @6;
158
159   register UINT g_instdata @2;
160
161   register char g_spmcrval @10;
162
163   register UINT g_pageaddr @4;
164
165
166
167   UCHAR g_threadcount                 @0x100;
168
169   os_thread *g_prevthread             @0x102;
170
171   os_thread *g_currthread             @0x104;
172
173   os_thread *g_freethreads            @0x106;
174
175   os_thread *g_usedthreads            @0x108;
176
177
178
179   os_semaphore *g_freesemaphores      @0x10A;
180
181   os_semaphore *g_usedsemaphores      @0x10C;
182
183
184
185   os_semaphore *g_freethreadsems      @0x10E;
186
187   os_semaphore *g_usedthreadsems      @0x110;
188
189
190
191   os_semaphore *g_freemessages        @0x112;
192
193   os_semaphore *g_usedmessages        @0x114;
194
195
196
197   os_semaphore *g_uart_t_semaphore    @0x116;
198
199   os_semaphore *g_uart_r_semaphore    @0x118;
200
201
202
203   UCHAR *g_stkStart                   @0x11A;
204
205   UCHAR *g_stkCurrent                 @0x11C;
206
207   UCHAR *g_stkEnd                     @0x11E;
208
209
210
211   os_thread g_allthreads[THREAD_COUNT]            @ALLTHREAD_START;
212
213   os_semaphore g_allsemaphores[SEMAPHORE_COUNT]   @ALLSEM_START;
214
215   os_threadsem g_allthreadsems[THREADSEM_COUNT]   @ALLTHREADSEM_START;
216
217   os_threadsem g_allmessages[MESSAGE_COUNT]       @ALLMESSAGE_START;
218
219   UCHAR g_stkBlock[TOTAL_STACK_SIZE]              @STACK_START;
220
221
222
223   os_thread* (*CreateThreadPtr)(UINT *entryPoint, UCHAR priVal, UINT swStackSize, UINT hwStackSize)       @0x60;
224
225   void (*SleepThreadPtr)(os_thread* thread, UINT sleepLength)                                             @0x62;
226
227   void (*SuspendThreadPtr)(os_thread* thread)                                                             @0x64;
228
229   void (*TerminateThreadPtr)(os_thread* thread)                                                           @0x66;
230
231   void (*ResumeThreadPtr)(os_thread* thread)                                                              @0x68;
232
233   os_thread* (*FindThreadPtr)(UCHAR threadID)                                                             @0x6A;
234
235   os_semaphore* (*CreateSemaphorePtr)(UCHAR semVal)                                                       @0x6C;
236
237   UCHAR (*WaitSemaphorePtr)(os_semaphore *semaphore)                                                      @0x6E;
238
239   void (*SignalSemaphorePtr)(os_semaphore *semaphore)                                                     @0x70;
240
241   void (*SendMessagePtr)(os_thread *thread, void *message)                                                @0x72;
242
243   void* (*ReceiveMessagePtr)(os_thread *thread)                                                           @0x74;
244
245
246
247   /*
248
249     Loader Methods
250
251   */
252
253   void WriteSector(UINT sectorStart, UINT *instArr);
254
255   void WritePage(UINT pageStart, UINT *instArr, UCHAR instCount);
256
257   void ReadPage (UINT pageStart, UINT *instArr, UCHAR instCount);
258
259   UINT ReadInst(UINT instAddr);
260
261
262
263   /*
264
265     Thread Methods
266
267   */
268
269   os_thread* CreateThread(void (*entryPoint)(void), UCHAR priVal, UINT swStackSize, UINT hwStackSize);
270
271   void SleepThread(os_thread* thread, UINT sleepLength);
272
273   void SuspendThread(os_thread* thread);
274
275   void TerminateThread(os_thread* thread);
276
277   void ResumeThread(os_thread* thread);
278
279   os_thread* FindThread(UCHAR threadID);
280
281
282
283   /*
284
285     OS Methods
286
287   */
288
289   void os_schedule(void);
290
291   void os_contextswitch(void);
292
293   void *os_alloc_stack(UINT stkSize);
294
295
296
297   /*
298
299     Semaphore Methods
300
301   */
302
303   os_semaphore* CreateSemaphore(UCHAR semVal);
304
305   UCHAR WaitSemaphore(os_semaphore *semaphore);
306
307   void SignalSemaphore(os_semaphore *semaphore);
308
309
310
311   /*
312
313     Messaging Methods
314
315   */
316
317   void SendMessage(os_thread* thread, void* message);
318
319   void* ReceiveMessage(os_thread* thread);
320   void sdthread();
321   #endif