📄 os.h
字号:
#ifndef THREADSTATE_RUNNING
/*
Constants
*///
typedef unsigned char UCHAR;
typedef unsigned int UINT;
typedef unsigned long ULONG;
#define THREADSTATE_VOID 0
#define THREADSTATE_RUNNING 1
#define THREADSTATE_BLOCKED 2
#define THREADSTATE_SUSPENDED 3
#define THREAD_COUNT 8
#define SEMAPHORE_COUNT 8
#define MESSAGE_COUNT 16
#define OS_DSTACK_SIZE 256
#define THREADSEM_COUNT THREAD_COUNT
#define ALLTHREAD_START 0x120
#define ALLSEM_START ALLTHREAD_START+THREAD_COUNT*sizeof(os_thread_struct)
#define ALLTHREADSEM_START ALLSEM_START+SEMAPHORE_COUNT*sizeof(os_semaphore_struct)
#define ALLMESSAGE_START ALLTHREADSEM_START+THREADSEM_COUNT*sizeof(os_threadsem_struct)
#define STACK_START ALLMESSAGE_START+MESSAGE_COUNT*sizeof(os_message)
#define TOTAL_STACK_SIZE 2048-STACK_START-OS_DSTACK_SIZE-192
/*
Message Types
*/
typedef struct os_message_struct
{
void *message;
struct os_message_struct *next;
} os_message;
/*
Thread Types
*/
typedef struct os_thread_struct
{
void *hwstack;
void *swstack;
UCHAR priority;
UINT sleep_duration;
UCHAR threadID;
UCHAR state;
struct os_message *message;
struct os_thread_struct *next;
struct os_thread_struct *prev;
} os_thread;
/*
Sempahore Types
*/
typedef struct os_threadsem_struct
{
os_thread *thread;
struct os_threadsem_struct *next;
} os_threadsem;
typedef struct os_semaphore_struct
{
UCHAR remaining;
os_threadsem *queue;
struct os_semaphore_struct *next;
struct os_semaphore_struct *prev;
} os_semaphore;
/*
OS Variables
*/
#asm(".EQU SpmcrAddr=0x57")
register UINT g_instaddr @6;
register UINT g_instdata @2;
register char g_spmcrval @10;
register UINT g_pageaddr @4;
UCHAR g_threadcount @0x100;
os_thread *g_prevthread @0x102;
os_thread *g_currthread @0x104;
os_thread *g_freethreads @0x106;
os_thread *g_usedthreads @0x108;
os_semaphore *g_freesemaphores @0x10A;
os_semaphore *g_usedsemaphores @0x10C;
os_semaphore *g_freethreadsems @0x10E;
os_semaphore *g_usedthreadsems @0x110;
os_semaphore *g_freemessages @0x112;
os_semaphore *g_usedmessages @0x114;
os_semaphore *g_uart_t_semaphore @0x116;
os_semaphore *g_uart_r_semaphore @0x118;
UCHAR *g_stkStart @0x11A;
UCHAR *g_stkCurrent @0x11C;
UCHAR *g_stkEnd @0x11E;
os_thread g_allthreads[THREAD_COUNT] @ALLTHREAD_START;
os_semaphore g_allsemaphores[SEMAPHORE_COUNT] @ALLSEM_START;
os_threadsem g_allthreadsems[THREADSEM_COUNT] @ALLTHREADSEM_START;
os_threadsem g_allmessages[MESSAGE_COUNT] @ALLMESSAGE_START;
UCHAR g_stkBlock[TOTAL_STACK_SIZE] @STACK_START;
os_thread* (*CreateThreadPtr)(UINT *entryPoint, UCHAR priVal, UINT swStackSize, UINT hwStackSize) @0x60;
void (*SleepThreadPtr)(os_thread* thread, UINT sleepLength) @0x62;
void (*SuspendThreadPtr)(os_thread* thread) @0x64;
void (*TerminateThreadPtr)(os_thread* thread) @0x66;
void (*ResumeThreadPtr)(os_thread* thread) @0x68;
os_thread* (*FindThreadPtr)(UCHAR threadID) @0x6A;
os_semaphore* (*CreateSemaphorePtr)(UCHAR semVal) @0x6C;
UCHAR (*WaitSemaphorePtr)(os_semaphore *semaphore) @0x6E;
void (*SignalSemaphorePtr)(os_semaphore *semaphore) @0x70;
void (*SendMessagePtr)(os_thread *thread, void *message) @0x72;
void* (*ReceiveMessagePtr)(os_thread *thread) @0x74;
/*
Loader Methods
*/
void WriteSector(UINT sectorStart, UINT *instArr);
void WritePage(UINT pageStart, UINT *instArr, UCHAR instCount);
void ReadPage (UINT pageStart, UINT *instArr, UCHAR instCount);
UINT ReadInst(UINT instAddr);
/*
Thread Methods
*/
os_thread* CreateThread(void (*entryPoint)(void), UCHAR priVal, UINT swStackSize, UINT hwStackSize);
void SleepThread(os_thread* thread, UINT sleepLength);
void SuspendThread(os_thread* thread);
void TerminateThread(os_thread* thread);
void ResumeThread(os_thread* thread);
os_thread* FindThread(UCHAR threadID);
/*
OS Methods
*/
void os_schedule(void);
void os_contextswitch(void);
void *os_alloc_stack(UINT stkSize);
/*
Semaphore Methods
*/
os_semaphore* CreateSemaphore(UCHAR semVal);
UCHAR WaitSemaphore(os_semaphore *semaphore);
void SignalSemaphore(os_semaphore *semaphore);
/*
Messaging Methods
*/
void SendMessage(os_thread* thread, void* message);
void* ReceiveMessage(os_thread* thread);
void sdthread();
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -