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