This commit is contained in:
Andreas Fink 2025-04-10 17:18:34 +02:00
parent 549b86adef
commit 55fc65ba5c
12 changed files with 21 additions and 177 deletions

View file

@ -4,21 +4,33 @@ CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld LD = $(CROSS_COMPILE)ld
OBJCOPY = $(CROSS_COMPILE)objcopy OBJCOPY = $(CROSS_COMPILE)objcopy
CFLAGS = -Wall -mcpu=arm926ej-s -nostdlib -ffreestanding CFLAGS = -Wall -mcpu=arm926ej-s -nostdlib -ffreestanding -I.
.SUFFIXES: .s .o .c
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
%.o: %.s
$(AS) -mcpu=arm926ej-s $< -o $@
ASM_S_FILES = $(wildcard system/asm/*.s)
ASM_O_FILES = $(ASM_S_FILES:.s=.o)
CFILES = $(wildcard devices/*.c) $(wildcard devices/*/*.c) $(wildcard system/*.c) $(wildcard system/*/*.c)
COFILES = $(CFILES:.c=.o)
OFILES = $(ASM_O_FILES) $(COFILES)
all: kernel.bin all: kernel.bin
kernel.elf: boot.o kernel.o kernel.elf: ${OFILES}
$(LD) -T boot.ld -o $@ $^ $(LD) -T boot.ld -o $@ $^
kernel.bin: kernel.elf kernel.bin: kernel.elf
$(OBJCOPY) -O binary $< $@ $(OBJCOPY) -O binary $< $@
boot.o: boot.s
$(AS) -mcpu=arm926ej-s $< -o $@
kernel.o: kernel.c
$(CC) $(CFLAGS) -c $< -o $@
clean: clean:
rm -f *.o *.elf *.bin rm -f *.o */*.o */*/*.o *.elf *.bin
run: kernel.bin
qemu-system-arm -M versatilepb -m 128M -nographic -kernel kernel.bin

12
boot.s
View file

@ -1,12 +0,0 @@
.global _start
.section .text
_start:
// Set up stack pointer
LDR sp, =stack_top
// Jump to kernel main entry point
BL kernel_main
// Infinite loop if kernel_main returns
halt:
B halt

7
irq.s
View file

@ -1,7 +0,0 @@
.global irq_handler
.type irq_handler, %function
irq_handler:
PUSH {r0-r12, lr}
BL task_schedule
POP {r0-r12, pc}

View file

@ -1,32 +0,0 @@
#include "uart.h"
#include "task.h"
#include "shell.h"
#define TIMER_BASE 0x101E2000
#define TIMER_LOAD (*(volatile unsigned int *)(TIMER_BASE + 0x00))
#define TIMER_CTRL (*(volatile unsigned int *)(TIMER_BASE + 0x08))
void timer_init() {
TIMER_LOAD = 0x10000;
TIMER_CTRL = (1 << 7) | (1 << 6) | (1 << 5) | 1;
}
void enable_interrupts() {
__asm__ volatile (
"mrs r0, cpsr\n"
"bic r0, r0, #0x80\n"
"msr cpsr_c, r0\n"
);
}
void kernel_main(void) {
uart_init();
uart_write("MiniOS Kernel Booted\n");
timer_init();
enable_interrupts();
task_init();
shell_run();
}

36
shell.c
View file

@ -1,36 +0,0 @@
#include "uart.h"
#include "task.h"
#include "shell.h"
#include <string.h>
void shell_run() {
char input[64];
int pos = 0;
uart_write("MiniOS Shell> ");
while (1) {
char c = uart_getchar();
uart_putchar(c); // Echo
if (c == '\r' || c == '\n') {
input[pos] = '\0';
uart_write("\n");
if (strcmp(input, "echo") == 0) {
uart_write("Echo test\n");
} else if (strcmp(input, "ps") == 0) {
uart_write("Task list: [0] task1, [1] task2\n");
} else if (strcmp(input, "halt") == 0) {
uart_write("System halted.\n");
while (1);
} else {
uart_write("Unknown command\n");
}
pos = 0;
uart_write("MiniOS Shell> ");
} else {
input[pos++] = c;
}
}
}

View file

@ -1,6 +0,0 @@
#ifndef SHELL_H
#define SHELL_H
void shell_run(void);
#endif

View file

@ -1,9 +0,0 @@
.global _start
.section .text
_start:
LDR sp, =stack_top
BL kernel_main
B .
.org 0x18
B irq_handler

View file

@ -1,18 +0,0 @@
.global context_switch
.type context_switch, %function
context_switch:
// r0 = current task context
// r1 = next task context
// Save r4-r12
STMIA r0!, {r4-r12}
STR sp, [r0]
STR lr, [r0, #4]
// Restore r4-r12
LDMIA r1!, {r4-r12}
LDR sp, [r1]
LDR lr, [r1, #4]
BX lr

26
task.c
View file

@ -1,26 +0,0 @@
#include "task.h"
task_t tasks[MAX_TASKS];
int current_task = 0;
void task1() {
while (1);
}
void task2() {
while (1);
}
void task_init() {
tasks[0].context.sp = (unsigned int)&tasks[0].stack[255];
tasks[0].context.lr = (unsigned int)task1;
tasks[1].context.sp = (unsigned int)&tasks[1].stack[255];
tasks[1].context.lr = (unsigned int)task2;
current_task = 0;
}
void task_schedule() {
current_task = (current_task + 1) % MAX_TASKS;
}

22
task.h
View file

@ -1,22 +0,0 @@
#ifndef TASK_H
#define TASK_H
#define MAX_TASKS 2
typedef struct {
unsigned int regs[13]; // r0-r12
unsigned int sp;
unsigned int lr;
} context_t;
typedef struct {
context_t context;
unsigned int stack[256];
} task_t;
void task_init(void);
void task_schedule(void);
extern task_t tasks[MAX_TASKS];
extern int current_task;
#endif