diff options
| author | user <user@clank> | 2026-07-16 00:12:43 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-07-16 00:12:43 +0200 |
| commit | 19a2b63bab71b796ee456b3ae8bec1a2ab297158 (patch) | |
| tree | 49b1effd335d47e859002903caea337fea27e1b9 /src/proc.asm | |
| parent | kernel: implement exec() (diff) | |
| download | gbos-19a2b63bab71b796ee456b3ae8bec1a2ab297158.tar.gz gbos-19a2b63bab71b796ee456b3ae8bec1a2ab297158.tar.xz gbos-19a2b63bab71b796ee456b3ae8bec1a2ab297158.zip | |
kernel: complete the process lifecycle (exit/wait/reap + bank free list)
- exit(): zombie + status, reparent orphans to init, wake blocked parent,
schedule away forever (resources reclaimed by the reaper)
- wait(): scan for a zombie child; reap (free cart-RAM bank + PCB slot) and
return pid/code; block PS_BLOCKED + yield if children still live; $FF if none
- cart-RAM banks are now a free list (wBankUsed bitmap): AllocRamBank/FreeRamBank
- scheduler: FindNextReady returns carry when nothing runnable; yield idles
instead of blindly picking slot 0
- helpers: FindPcbByPid, ReparentToInit
- demo: init forks 3 workers -> exec -> exit(pid) -> wait/reap => wwwR2R3R4!
verified bank recycling with 6 workers over 3 banks (w2w3w4w5w6w7)
- README: document the lifecycle + syscall status
Diffstat (limited to 'src/proc.asm')
| -rw-r--r-- | src/proc.asm | 110 |
1 files changed, 100 insertions, 10 deletions
diff --git a/src/proc.asm b/src/proc.asm index 114cc9c..a2b1368 100644 --- a/src/proc.asm +++ b/src/proc.asm @@ -11,26 +11,116 @@ SECTION "proc", ROM0 ProcInit:: ld a, 1 ld [wNextPid], a - ld a, 1 ; bank 0 is the init task's; allocate from 1 up - ld [wNextRamBank], a + ; bank 0 belongs to the init task; the rest start free + ld a, 1 + ld [wBankUsed + 0], a + ld hl, wBankUsed + 1 + ld b, NUM_RAM_BANKS - 1 + xor a +.bclr + ld [hl+], a + dec b + jr nz, .bclr ret ; ----------------------------------------------------------------------------- -; AllocRamBank - bump-allocate a cart-RAM bank. out: A=bank, CF set if none. +; AllocRamBank - claim a free cart-RAM bank. out: A=bank, CF set if none. ; ----------------------------------------------------------------------------- AllocRamBank:: - ld a, [wNextRamBank] + ld hl, wBankUsed + ld c, 0 +.scan + ld a, [hl] + or a + jr z, .free + inc hl + inc c + ld a, c cp NUM_RAM_BANKS - jr nc, .none - ld b, a - inc a - ld [wNextRamBank], a - ld a, b + jr c, .scan + scf + ret +.free + ld a, 1 + ld [hl], a ; mark used + ld a, c and a ; clear carry ret -.none + +; ----------------------------------------------------------------------------- +; FreeRamBank - release a cart-RAM bank. in: A=bank. +; ----------------------------------------------------------------------------- +FreeRamBank:: + ld hl, wBankUsed + add l + ld l, a + ld a, h + adc 0 + ld h, a + xor a + ld [hl], a + ret + +; ----------------------------------------------------------------------------- +; FindPcbByPid - locate a live PCB by pid. +; in: A=pid out: DE=&PCB, CF set if not found. +; ----------------------------------------------------------------------------- +FindPcbByPid:: + ld b, a ; target pid + ld c, 0 +.l + ld a, c + call PcbPtr ; DE=&PCB[c] (preserves BC) + ld a, [de] + cp PS_FREE + jr z, .n + ld h, d + ld l, e + inc hl ; ->PROC_PID + ld a, [hl] + cp b + jr z, .found +.n + inc c + ld a, c + cp MAX_PROCS + jr c, .l scf ret +.found + and a ; clear carry (pids are >= 1) + ret + +; ----------------------------------------------------------------------------- +; ReparentToInit - give every live child of wExitMyPid to pid INIT_PID. +; ----------------------------------------------------------------------------- +ReparentToInit:: + ld c, 0 +.l + ld a, c + call PcbPtr ; DE=&PCB (preserves BC) + ld a, [de] + cp PS_FREE + jr z, .n + ld h, d + ld l, e + ld a, l + add PROC_PARENT + ld l, a + ld a, h + adc 0 + ld h, a ; HL=&PROC_PARENT + ld a, [wExitMyPid] + cp [hl] + jr nz, .n + ld a, INIT_PID + ld [hl], a +.n + inc c + ld a, c + cp MAX_PROCS + jr c, .l + ret ; ----------------------------------------------------------------------------- ; FindFreeSlot - scan the proc table for a PS_FREE slot. |
