aboutsummaryrefslogtreecommitdiffstats
path: root/src/tasks.asm
blob: 459ccb6140d3d028a12a415efcca7bf3b31d1a0c (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
37
38
39
; =============================================================================
; 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

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 .parent

.child
    ld de, .cmsg
    ld b, 1
    ld c, SYS_WRITE
    rst $30
    ld c, SYS_YIELD
    rst $30
    jr .child

.pmsg
    db "P"
.cmsg
    db "C"