From ab3ebbe227c72b745a46f9233fbfbb27477586b8 Mon Sep 17 00:00:00 2001 From: user Date: Wed, 15 Jul 2026 23:54:13 +0200 Subject: kernel: implement fork() - sys_fork: switch to a kernel stack, bump-allocate a cart-RAM bank, copy the parent's 8 KiB bank via a WRAM bounce buffer, plant a switch-in frame so the child returns 0 to the post-fork PC, fill the child PCB (shared ROM text) - add AllocRamBank (bump allocator) + FindFreeSlot helpers - demo: init forks; parent prints P, child prints C (PCPC...); nested fork gives three distinct pids (123...) - README: document the fork mechanism, mark syscall status --- src/tasks.asm | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'src/tasks.asm') diff --git a/src/tasks.asm b/src/tasks.asm index 1f84142..459ccb6 100644 --- a/src/tasks.asm +++ b/src/tasks.asm @@ -1,33 +1,39 @@ ; ============================================================================= -; tasks.asm - two demo userland tasks (text-in-ROM model). -; Each loops: write its letter to the console, then yield. This exercises the -; whole pipeline: syscall trap, console, and the HRAM context switch swapping -; RAM banks / SVBK between tasks. +; tasks.asm - the init task demonstrates fork(). +; init forks once; the parent loops printing 'P', the child loops printing 'C'. +; Both run the same ROM text, diverging on fork()'s return value (0 in child). +; Exercises: syscall trap, fork (8 KiB bank copy + child frame), scheduler, +; and the HRAM context switch swapping cart-RAM banks per task. ; ============================================================================= INCLUDE "include/gbos.inc" SECTION "tasks", ROM0 -TaskA:: -.loop - ld de, .msg - ld b, 1 ; len +TaskInit:: + ld c, SYS_FORK + rst $30 ; A = child pid (parent) or 0 (child) + or a + jr z, .child + +.parent + ld de, .pmsg + ld b, 1 ld c, SYS_WRITE rst $30 ld c, SYS_YIELD rst $30 - jr .loop -.msg - db "A" + jr .parent -TaskB:: -.loop - ld de, .msg +.child + ld de, .cmsg ld b, 1 ld c, SYS_WRITE rst $30 ld c, SYS_YIELD rst $30 - jr .loop -.msg - db "B" + jr .child + +.pmsg + db "P" +.cmsg + db "C" -- cgit v1.3.1-sl0p