-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.asm
284 lines (253 loc) · 7.66 KB
/
tasks.asm
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
include "hram.asm"
include "task.asm"
include "macros.asm"
include "longcalc.asm"
include "constants.asm"
SECTION "Task List", WRAM0
TaskList::
ds MAX_TASKS * TASK_SIZE
SECTION "Task management", ROM0
; Initialize task management structures
TaskInit::
; Setting task_sp to $0000 marks it as unoccupied
xor A
ld B, MAX_TASKS
ld HL, TaskList + task_sp
jr .start
.loop
LongAddConst HL, TASK_SIZE + (-2) ; TASK_SIZE - 2 is a syntax error for some reason
.start
ld [HL+], A
ld [HL+], A
dec B
jr nz, .loop
ret
; Find the next free task id and return it in B, or 255 if none are free.
; Clobbers A, HL.
TaskFindNextFree:
ld HL, TaskList + task_sp
ld B, MAX_TASKS - 1
jr .start
.loop
dec B
; if B underflowed, we checked all MAX_TASKS without breaking
; and should return B = 255, which it is because it just underflowed
ret c
LongAddConst HL, TASK_SIZE + (-1) ; TASK_SIZE - n is a syntax error, assembler bug
.start
ld A, [HL+]
or [HL] ; set z only if both A and [HL] are 0
jr nz, .loop
; If we got here, HL = TaskList + first free task id + task_sp + 1
; so we want to find HL - (TaskList + task_sp + 1).
; Since we know the final result is < 256, we only need to do the lower half
; of the subtraction. We don't care about the carry.
ld A, L
sub (TaskList + task_sp + 1) & $ff
ld B, A
ret
; Create a new task with entry point in DE, initial stack pointer in HL and inital ROM bank in C.
; Returns the new task id in B, or 255 if no task could be allocated.
TaskNewWithStack::
; Pick a task id
push HL
call TaskFindNextFree
pop HL
ld A, B
cp $ff
ret z ; if B == 255, exit early with failure
; fall through to TaskNewWithID
; Create a new task with entry point in DE, initial stack pointer in HL,
; initial ROM bank in C and new task id in B.
TaskNewWithID:
; prepare the initial stack, which can be mostly garbage.
dec HL
ld A, D
ld [HL-], A
ld [HL], E ; push DE to stack at HL - this becomes the initial PC
LongSub HL, 10, DE ; push 10 bytes of garbage to the stack and save in DE - this becomes junk + initial regs
; fill in the task struct
ld A, B
LongAddToA TaskList+task_sp, HL ; HL = TaskList + B + task_sp = &(TaskList[B].task_sp)
ld A, D
ld [HL+], A
ld A, E
ld [HL+], A ; [HL] = DE, this sets the initial stack pointer
RepointStruct HL, task_sp+2, task_rombank
ld A, C
ld [HL+], A ; [HL] = C, this sets the initial ROM bank
RepointStruct HL, task_rombank+1, task_rambank
xor A
ld [HL+], A ; [HL] = 0, this sets the initial RAM bank
RepointStruct HL, task_rambank+1, task_waiter
dec A ; A = ff
ld [HL], A ; [HL] = ff, this says the task is initially not waiting for any waiter
jp SchedAddTask ; schedule new task to run and return
; Create a new task with entry point in DE, ROM bank C (or 0) and a stack allocated from dynamic memory.
; Returns the new task id in B, or 255 if no task could be allocated.
TaskNewDynStack::
call TaskFindNextFree ; B = new task id or 255
ld A, B
cp $ff
ret z ; if B = 255, exit early with failure
push DE
ld D, B ; this sets task ownership of allocated mem
ld E, C ; storing B and C in D and E is faster than push and pop
ld B, DYN_MEM_STACK_SIZE
ld HL, GeneralDynMem
call DynMemAlloc ; allocate stack in the name of task D, put in HL
ld B, D
ld C, E ; restore B and C
pop DE
ld A, H
or L ; H or L -> set Z if HL == $0000
jr nz, .nofail
; return failure since we couldn't allocate a stack
ld B, $ff
ret
.nofail
; HL points to the base of the new stack, but stacks grow down,
; we want to give the top of the stack
ld A, DYN_MEM_STACK_SIZE
LongAddToA HL, HL ; HL += stack size
jr TaskNewWithID ; tail call
; Task-callable versions of TaskNew family
T_TaskNewWithStack::
call T_DisableSwitch
call TaskNewWithStack
jp T_EnableSwitch
T_TaskNewDynStack::
call T_DisableSwitch
call TaskNewDynStack
jp T_EnableSwitch
; Save task state from current cpu state and transitions into the (blank) core stack.
; Expects the top of stack to look like: (top), Return address, PC of task, user stack
; (it is done this way so that 'call TaskSave' will save your caller and return to you,
; suitable for interrupt handlers, etc)
TaskSave::
; We must be careful here not to lose register values before saving them
push AF
push BC
push DE
push HL
; Before leaving this stack, we need to pull out the return address,
; which is now 8 bytes into the stack
ld HL, SP+8
ld A, [HL+]
ld E, A
ld D, [HL] ; DE = return address. Note it's backwards because stack grows down.
; Now we can save SP and switch stacks
ld HL, SP+0
ld B, H
ld C, L ; BC = SP
ld A, [CurrentTask]
LongAddToA TaskList+task_sp, HL ; HL = TaskList + CurrentTask + task_sp = &(TaskList[CurrentTask].task_sp)
; Save SP to task struct
ld A, B
ld [HL+], A
ld [HL], C
; Load core stack
ld SP, CoreStack
; Return to saved address
ld H, D
ld L, E ; HL = return address
jp HL
; Load task state and return into the task.
; Does not return to called function - in fact, it ignores the current stack completely.
; Takes task ID to load in A.
TaskLoad::
ld [CurrentTask], A
LongAddToA TaskList+task_sp, HL ; HL = TaskList + A + task_sp = &(TaskList[A].task_sp)
; BC = [HL] = stored stack pointer
ld A, [HL+]
ld B, A
ld A, [HL+]
ld C, A
; Set ROM and RAM banks, if any
RepointStruct HL, task_sp+2, task_rombank
ld A, [HL+]
and A
jr z, .noROM
ld [CurrentROMBank], A
SetROMBank
xor A
.noROM
RepointStruct HL, task_rombank+1, task_rambank
add [HL]
jr z, .noRAM
ld [CurrentRAMBank], A
SetRAMBank
.noRAM
; set stack pointer
ld H, B
ld L, C
ld SP, HL
; Set up time slice interval and enable switch.
; EnableSwitch clobbers A, so we have to do this now.
; It should be safe even if we switch out here, since we will cleanly return to here,
; then continue on to finish the first switch.
ld A, SWITCH_TIME_INTERVAL
ld [SwitchTimer], A
EnableSwitch
; restore regs
pop HL
pop DE
pop BC
pop AF
; The SaveTask process leaves some junk on the stack, skip it.
; Note we can't use ADD SP, 2 here as this would change flags
inc SP
inc SP
; at this point, the top of the user's stack should be the PC to return to
ret
; Voluntarily give up task execution. Allows other tasks to run and returns some time later.
; Clobbers A.
T_TaskYield::
DisableSwitch
; fallthrough
; For use by core code to suspend currently running task, but keep it runnable.
TaskYield::
call TaskSave ; switch onto core stack
ld A, [CurrentTask]
ld B, A
call SchedAddTask ; re-enqueue task to run again
jp SchedLoadNext ; does not return
; Temporarily disable time-share switching (calling T_TaskYield is still ok),
; allowing critical sections without disabling interrupts entirely.
; Clobbers A.
T_DisableSwitch::
DisableSwitch
ret
; Re-enable time-share switching after a call to T_DisableSwitch.
; If a switch attempt was missed, this will trigger an immediate switch.
; Clobbers A.
T_EnableSwitch::
ld A, [Switchable]
cp 2
jp z, TaskYield ; will return to our caller later
EnableSwitch
ret
; This is the only safe way for a task to switch ROM banks.
; Sets current ROM bank for this task to C.
; It saves state so that the task's bank can be set on each TaskLoad.
; Clobbers A, HL
T_SetROMBank::
ld A, [CurrentTask]
LongAddToA TaskList+task_rombank, HL ; HL = TaskList + CurrentTask + task_rombank = &(TaskList[CurrentTask].task_rombank)
ld [HL], C
ld A, C
ld [CurrentROMBank], A
SetROMBank
ret
; This is the only safe way for a task to switch RAM banks.
; Sets current RAM bank for this task to C.
; It saves state so that the task's bank can be set on each TaskLoad.
; Clobbers A, HL
T_SetRAMBank::
ld A, [CurrentTask]
LongAddToA TaskList+task_rambank, HL ; HL = TaskList + CurrentTask + task_rambank = &(TaskList[CurrentTask].task_rambank)
ld [HL], C
ld A, C
ld [CurrentRAMBank], A
SetRAMBank
ret