diff --git a/Makefile b/Makefile index d9ecf4c..0efda41 100644 --- a/Makefile +++ b/Makefile @@ -4,21 +4,33 @@ CC = $(CROSS_COMPILE)gcc LD = $(CROSS_COMPILE)ld 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 -kernel.elf: boot.o kernel.o +kernel.elf: ${OFILES} $(LD) -T boot.ld -o $@ $^ kernel.bin: kernel.elf $(OBJCOPY) -O binary $< $@ -boot.o: boot.s - $(AS) -mcpu=arm926ej-s $< -o $@ - -kernel.o: kernel.c - $(CC) $(CFLAGS) -c $< -o $@ - clean: - rm -f *.o *.elf *.bin \ No newline at end of file + rm -f *.o */*.o */*/*.o *.elf *.bin + +run: kernel.bin + qemu-system-arm -M versatilepb -m 128M -nographic -kernel kernel.bin \ No newline at end of file diff --git a/boot.s b/boot.s deleted file mode 100644 index 8180aa2..0000000 --- a/boot.s +++ /dev/null @@ -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 \ No newline at end of file diff --git a/uart.c b/devices/uart.c similarity index 100% rename from uart.c rename to devices/uart.c diff --git a/uart.h b/devices/uart.h similarity index 100% rename from uart.h rename to devices/uart.h diff --git a/irq.s b/irq.s deleted file mode 100644 index 2820c53..0000000 --- a/irq.s +++ /dev/null @@ -1,7 +0,0 @@ -.global irq_handler -.type irq_handler, %function - -irq_handler: - PUSH {r0-r12, lr} - BL task_schedule - POP {r0-r12, pc} \ No newline at end of file diff --git a/kernel.c b/kernel.c deleted file mode 100644 index 3703104..0000000 --- a/kernel.c +++ /dev/null @@ -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(); -} \ No newline at end of file diff --git a/shell.c b/shell.c deleted file mode 100644 index bc657e4..0000000 --- a/shell.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "uart.h" -#include "task.h" -#include "shell.h" -#include - -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; - } - } -} \ No newline at end of file diff --git a/shell.h b/shell.h deleted file mode 100644 index 318202c..0000000 --- a/shell.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef SHELL_H -#define SHELL_H - -void shell_run(void); - -#endif \ No newline at end of file diff --git a/startup.s b/startup.s deleted file mode 100644 index 1c75b92..0000000 --- a/startup.s +++ /dev/null @@ -1,9 +0,0 @@ -.global _start -.section .text -_start: - LDR sp, =stack_top - BL kernel_main - B . - -.org 0x18 - B irq_handler \ No newline at end of file diff --git a/switch.s b/switch.s deleted file mode 100644 index ee655bd..0000000 --- a/switch.s +++ /dev/null @@ -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 \ No newline at end of file diff --git a/task.c b/task.c deleted file mode 100644 index a69d4f7..0000000 --- a/task.c +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/task.h b/task.h deleted file mode 100644 index 032293e..0000000 --- a/task.h +++ /dev/null @@ -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 \ No newline at end of file