aboutsummaryrefslogtreecommitdiffstats
path: root/src/programs.asm
blob: b5e4d8ed2051e928b6dd4916aa5f1c2474af63df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
; =============================================================================
; programs.asm - userland program images (text-in-ROM model).
;
; Programs are linked to run from the ROMX window ($4000-$7FFF) and live in
; their own ROM banks. exec() maps the bank and jumps to the entry. Programs
; only talk to the kernel via `rst $30` (the trap + kernel live in ROM0, always
; mapped), so they never reference kernel labels.
; =============================================================================
INCLUDE "include/gbos.inc"

; -----------------------------------------------------------------------------
; PROG_WORKER: announce with 'w', then exit() with a status equal to our pid.
; Exercises exec (runs from a ROM bank) + the exit/wait lifecycle.
; -----------------------------------------------------------------------------
SECTION "prog_worker", ROMX[$4000], BANK[2]
ProgWorker:
    ld de, .msg
    ld b, 1
    ld c, SYS_WRITE
    rst $30
    ld c, SYS_GETPID
    rst $30
    ld b, a                 ; exit status = pid (so reaped codes are distinct)
    ld c, SYS_EXIT
    rst $30                 ; never returns
.msg
    db "w"

; -----------------------------------------------------------------------------
; Program table (ROM0). Indexed by exec()'s program id.
; Entry: bank (2 bytes, MBC5 16-bit) then entry address (2 bytes).
; -----------------------------------------------------------------------------
SECTION "progtab", ROM0
ProgramTable::
    db LOW(BANK(ProgWorker)), HIGH(BANK(ProgWorker))
    dw ProgWorker