aboutsummaryrefslogtreecommitdiffstats
path: root/src/proc.asm
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/proc.asm110
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.