aboutsummaryrefslogtreecommitdiffstats
path: root/src/proc.asm
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/proc.asm53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/proc.asm b/src/proc.asm
index 058881f..114cc9c 100644
--- a/src/proc.asm
+++ b/src/proc.asm
@@ -368,3 +368,56 @@ sys_fork::
ld sp, hl
ld a, $FF
ret
+
+; =============================================================================
+; sys_exec(B = program id) - replace the current process image. Never returns.
+;
+; Looks up ProgramTable[B] = (ROM bank, entry). Points PROC_ROMB at the program
+; text bank, maps it live into $4000-$7FFF, resets the stack to the top of the
+; task's (already mapped) cart-RAM bank, and jumps to the entry. The task keeps
+; its RAM bank; a fuller exec would also copy .data from ROM and zero .bss, but
+; our demo programs keep no pre-initialized RAM.
+; =============================================================================
+sys_exec::
+ ; HL = &ProgramTable[B] = ProgramTable + B*4
+ ld hl, ProgramTable
+ ld a, b
+ add a ; *2
+ add a ; *4 (program id < 64)
+ add l
+ ld l, a
+ jr nc, .nc
+ inc h
+.nc
+ ld a, [hl+] ; bank lo
+ ld b, a
+ ld a, [hl+] ; bank hi
+ ld c, a
+ ld a, [hl+] ; entry lo
+ ld e, a
+ ld a, [hl] ; entry hi
+ ld d, a ; DE = entry, BC = bank(lo,hi)
+
+ ; PROC_ROMB = BC
+ ld a, [wCurProc]
+ add PROC_ROMB
+ ld l, a
+ ld a, [wCurProc+1]
+ adc 0
+ ld h, a
+ ld a, b
+ ld [hl+], a
+ ld a, c
+ ld [hl], a
+
+ ; map the program's ROM bank into $4000-$7FFF
+ ld a, b
+ ld [MBC5_ROMB_LO], a
+ ld a, c
+ ld [MBC5_ROMB_HI], a
+
+ ; fresh empty stack at the top of the task's cart-RAM bank, then enter
+ ld sp, TASK_RAM_TOP ; $C000; first push lands at $BFFF
+ ld h, d
+ ld l, e
+ jp hl ; jump to program entry ($4000)