From 137ec0fd740814047a9200e2f4b2b87c2cdb4fb5 Mon Sep 17 00:00:00 2001 From: Matte23 Date: Sun, 3 Nov 2024 11:41:22 +0100 Subject: [PATCH] Add missing files --- os/process.c | 15 +++++++++++++++ os/process.h | 23 +++++++++++++++++++++++ os/scheduler.c | 31 +++++++++++++++++++++++++++++++ os/scheduler.h | 1 + startup.s | 15 +++++++++++++++ tasks/tasks.c | 11 +++++++++++ 6 files changed, 96 insertions(+) create mode 100644 os/process.c create mode 100644 os/process.h create mode 100644 os/scheduler.c create mode 100644 os/scheduler.h create mode 100644 startup.s create mode 100644 tasks/tasks.c diff --git a/os/process.c b/os/process.c new file mode 100644 index 0000000..3a429e9 --- /dev/null +++ b/os/process.c @@ -0,0 +1,15 @@ +#include "process.h" +#include "alloc.h" + +void create_process_table(ProcessTable **table) { + *table = malloc(sizeof(ProcessTable)); + (*table)->entries = 0; +} + +int create_process(ProcessTable *table, void *entrypoint) { + if (table->entries>=MAX_PROCESS) return 1; + + Process *pentry = &table->process_list[table->entries++]; + pentry->entrypoint = entrypoint; + return 0; +} diff --git a/os/process.h b/os/process.h new file mode 100644 index 0000000..33bb187 --- /dev/null +++ b/os/process.h @@ -0,0 +1,23 @@ +#ifndef PROCESS_H +#include + +#define PROCESS_H +#define MAX_PROCESS 16 + +#define STACK_SIZE 32 +#define STACK_START(stack) (&stack[STACK_SIZE-1]) + +typedef struct { + int (*entrypoint)(); + uint32_t stack[STACK_SIZE]; +} Process; + +typedef struct { + Process process_list[MAX_PROCESS]; + int entries; +} ProcessTable; + +void create_process_table(ProcessTable **table); + +int create_process(ProcessTable *table, void *entrypoint); +#endif diff --git a/os/scheduler.c b/os/scheduler.c new file mode 100644 index 0000000..8f7b4ac --- /dev/null +++ b/os/scheduler.c @@ -0,0 +1,31 @@ +#include "process.h" + +uint32_t **current_sp; + +extern void puts(const char *s); + +void start_task(Process *pentry) { + __asm volatile( + "mov %0, sp" + : "=r" (*current_sp) + ); + __asm volatile( + "mov sp, %0\n" + :: "r" (STACK_START(pentry->stack)) + ); + pentry->entrypoint(); + __asm volatile( + "mov sp, %0\n" + :: "r" (*current_sp) + ); +} + +void run(ProcessTable *ptable) { + while(1) { + for (int i = 0; ientries; i++) { + Process *pentry = &ptable->process_list[i]; + + start_task(pentry); + } + } +} diff --git a/os/scheduler.h b/os/scheduler.h new file mode 100644 index 0000000..8251ebe --- /dev/null +++ b/os/scheduler.h @@ -0,0 +1 @@ +void run(); diff --git a/startup.s b/startup.s new file mode 100644 index 0000000..67e9f10 --- /dev/null +++ b/startup.s @@ -0,0 +1,15 @@ +// The .word directive allocates a 32-bit value in the memory. In this case it allocates in memory +// the address of the stack_top (value in the linker.ld file) and _Reset. +.word stack_top // Address of the stack_top +.word _start // Address of the _start function + +// The thumb_func is used to make sure the function is in thumb mode, +// which is required for the Cortex-M0+. +.thumb_func +.syntax unified + +.global _start + +_start: + BL main + B . diff --git a/tasks/tasks.c b/tasks/tasks.c new file mode 100644 index 0000000..c3a0118 --- /dev/null +++ b/tasks/tasks.c @@ -0,0 +1,11 @@ +#include "library.h" + +int task2(void) { + puts("Hello World from task 1\n"); + return 0; +} + +int task1(void) { + puts("Hello World from task 2\n"); + return 0; +}