1 #ifndef THREADSTATE_RUNNING
2
3 /*
4 Constants
5 */
6 typedef unsigned char UCHAR;
7 typedef unsigned int UINT;
8 typedef unsigned long ULONG;
9
10 #define THREADSTATE_VOID 0
11 #define THREADSTATE_RUNNING 1
12 #define THREADSTATE_BLOCKED 2
13 #define THREADSTATE_SUSPENDED 3
14
15 #define THREAD_COUNT 8
16 #define SEMAPHORE_COUNT 8
17 #define MESSAGE_COUNT 16
18 #define OS_DSTACK_SIZE 512
19 #define THREADSEM_COUNT THREAD_COUNT
20
21 #define ALLTHREAD_START 0x120
22 #define ALLSEM_START ALLTHREAD_START+THREAD_COUNT*sizeof(os_thread_struct)
23 #define ALLTHREADSEM_START ALLSEM_START+SEMAPHORE_COUNT*sizeof(os_semaphore_struct)
24 #define ALLMESSAGE_START ALLTHREADSEM_START+THREADSEM_COUNT*sizeof(os_threadsem_struct)
25 #define STACK_START ALLMESSAGE_START+MESSAGE_COUNT*sizeof(os_message)
26
27 #define TOTAL_STACK_SIZE 2048-STACK_START-OS_DSTACK_SIZE
28 /*
29 Message Types
30 */
31
32 typedef struct os_message_struct
33 {
34 void *message;
35 struct os_message_struct *next;
36 } os_message;
37
38
39 /*
40 Thread Types
41 */
42 typedef struct os_thread_struct
43 {
44 void *hwstack;
45 void *swstack;
46 UCHAR priority;
47 UINT sleep_duration;
48 UCHAR threadID;
49 UCHAR state;
50 struct os_message *message;
51 struct os_thread_struct *next;
52 struct os_thread_struct *prev;
53 } os_thread;
54
55 /*
56 Sempahore Types
57 */
58
59 typedef struct os_threadsem_struct
60 {
61 os_thread *thread;
62 struct os_threadsem_struct *next;
63 } os_threadsem;
64
65 typedef struct os_semaphore_struct
66 {
67 UCHAR remaining;
68 os_threadsem *queue;
69 struct os_semaphore_struct *next;
70 struct os_semaphore_struct *prev;
71 } os_semaphore;
72
73
74 /*
75 OS Variables
76 */
77 #asm(".EQU SpmcrAddr=0x57")
78
79 register UINT g_instaddr @6;
80 register UINT g_instdata @2;
81 register char g_spmcrval @10;
82 register UINT g_pageaddr @4;
83
84 UCHAR g_threadcount @0x100;
85 os_thread *g_prevthread @0x102;
86 os_thread *g_currthread @0x104;
87 os_thread *g_freethreads @0x106;
88 os_thread *g_usedthreads @0x108;
89
90 os_semaphore *g_freesemaphores @0x10A;
91 os_semaphore *g_usedsemaphores @0x10C;
92
93 os_semaphore *g_freethreadsems @0x10E;
94 os_semaphore *g_usedthreadsems @0x110;
95
96 os_semaphore *g_freemessages @0x112;
97 os_semaphore *g_usedmessages @0x114;
98
99 os_semaphore *g_uart_t_semaphore @0x116;
100 os_semaphore *g_uart_r_semaphore @0x118;
101
102 UCHAR *g_stkStart @0x11A;
103 UCHAR *g_stkCurrent @0x11C;
104 UCHAR *g_stkEnd @0x11E;
105
106 os_thread g_allthreads[THREAD_COUNT] @ALLTHREAD_START;
107 os_semaphore g_allsemaphores[SEMAPHORE_COUNT] @ALLSEM_START;
108 os_threadsem g_allthreadsems[THREADSEM_COUNT] @ALLTHREADSEM_START;
109 os_threadsem g_allmessages[MESSAGE_COUNT] @ALLMESSAGE_START;
110 UCHAR g_stkBlock[TOTAL_STACK_SIZE] @STACK_START;
111
112 os_thread* (*CreateThreadPtr)(UINT *entryPoint, UCHAR priVal, UINT swStackSize, UINT hwStackSize) @0x60;
113 void (*SleepThreadPtr)(os_thread* thread, UINT sleepLength) @0x62;
114 void (*SuspendThreadPtr)(os_thread* thread) @0x64;
115 void (*TerminateThreadPtr)(os_thread* thread) @0x66;
116 void (*ResumeThreadPtr)(os_thread* thread) @0x68;
117 os_thread* (*FindThreadPtr)(UCHAR threadID) @0x6A;
118 os_semaphore* (*CreateSemaphorePtr)(UCHAR semVal) @0x6C;
119 UCHAR (*WaitSemaphorePtr)(os_semaphore *semaphore) @0x6E;
120 void (*SignalSemaphorePtr)(os_semaphore *semaphore) @0x70;
121 void (*SendMessagePtr)(os_thread *thread, void *message) @0x72;
122 void* (*ReceiveMessagePtr)(os_thread *thread) @0x74;
123
124 /*
125 Loader Methods
126 */
127 void WriteSector(UINT sectorStart, UINT *instArr);
128 void WritePage(UINT pageStart, UINT *instArr, UCHAR instCount);
129 void ReadPage (UINT pageStart, UINT *instArr, UCHAR instCount);
130 UINT ReadInst(UINT instAddr);
131
132 /*
133 Thread Methods
134 */
135 os_thread* CreateThread(void (*entryPoint)(void), UCHAR priVal, UINT swStackSize, UINT hwStackSize);
136 void SleepThread(os_thread* thread, UINT sleepLength);
137 void SuspendThread(os_thread* thread);
138 void TerminateThread(os_thread* thread);
139 void ResumeThread(os_thread* thread);
140 os_thread* FindThread(UCHAR threadID);
141
142 /*
143 OS Methods
144 */
145 void os_schedule(char mode);
146 void os_contextswitch(void);
147 void *os_alloc_stack(UINT stkSize);
148
149 /*
150 Semaphore Methods
151 */
152 os_semaphore* CreateSemaphore(UCHAR semVal);
153 UCHAR WaitSemaphore(os_semaphore *semaphore);
154 void SignalSemaphore(os_semaphore *semaphore);
155
156 /*
157 Messaging Methods
158 */
159 void SendMessage(os_thread* thread, void* message);
160 void* ReceiveMessage(os_thread* thread);
161 #endif