paul@0 | 1 | ; A task switcher for the Acorn Electron. |
paul@0 | 2 | |
paul@0 | 3 | ; Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 4 | |
paul@0 | 5 | ; This program is free software; you can redistribute it and/or modify it under |
paul@0 | 6 | ; the terms of the GNU General Public License as published by the Free Software |
paul@0 | 7 | ; Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 8 | ; version. |
paul@0 | 9 | |
paul@0 | 10 | ; This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 11 | ; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 12 | ; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 13 | ; details. |
paul@0 | 14 | |
paul@0 | 15 | ; You should have received a copy of the GNU General Public License along with |
paul@0 | 16 | ; this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 17 | |
paul@0 | 18 | .include "macros.oph" |
paul@0 | 19 | |
paul@3 | 20 | .alias IRQ1V $204 |
paul@3 | 21 | |
paul@2 | 22 | .alias SP $70 |
paul@2 | 23 | .alias SPH $71 |
paul@2 | 24 | .alias NEXT $72 |
paul@2 | 25 | .alias NEXTH $73 |
paul@2 | 26 | .alias CURRENT $74 |
paul@2 | 27 | .alias CURRENTH $75 |
paul@5 | 28 | .alias USER $76 |
paul@6 | 29 | .alias USERH $77 |
paul@6 | 30 | .alias TEMP $78 |
paul@5 | 31 | .alias ARG0 $80 |
paul@5 | 32 | .alias ARG0H $81 |
paul@5 | 33 | .alias ARG1 $82 |
paul@5 | 34 | .alias ARG1H $83 |
paul@2 | 35 | |
paul@3 | 36 | .alias TASK_TABLE_LENGTH 10 |
paul@3 | 37 | |
paul@5 | 38 | ; "user space" stack for the main program where the invocations might be |
paul@5 | 39 | ; interrupted and where the CPU stack might be disrupted |
paul@5 | 40 | |
paul@8 | 41 | .alias main_stack_base $1ffc |
paul@7 | 42 | .alias ABSTEMP $1ffe |
paul@5 | 43 | |
paul@5 | 44 | |
paul@5 | 45 | |
paul@0 | 46 | .org $2000 |
paul@0 | 47 | .text |
paul@5 | 48 | |
paul@5 | 49 | |
paul@5 | 50 | |
paul@5 | 51 | ; main program, installing the handler and adding example tasks |
paul@5 | 52 | |
paul@0 | 53 | main: |
paul@8 | 54 | .invoke store16 tasks, ARG1 ; for debugging |
paul@8 | 55 | |
paul@8 | 56 | ; initialise a user stack and add this program as a task |
paul@8 | 57 | |
paul@8 | 58 | .invoke store16 main_stack_base, USER |
paul@8 | 59 | .invoke store16 main_task, ARG0 |
paul@8 | 60 | .invoke call new_task, + |
paul@8 | 61 | |
paul@8 | 62 | ; install the interrupt handler |
paul@8 | 63 | |
paul@8 | 64 | * jsr install_handler |
paul@8 | 65 | |
paul@8 | 66 | ; perform task installation |
paul@8 | 67 | |
paul@5 | 68 | .invoke store16 first_task, ARG0 |
paul@7 | 69 | .invoke call new_task, + |
paul@7 | 70 | * .invoke store16 second_task, ARG0 |
paul@7 | 71 | .invoke call new_task, + |
paul@7 | 72 | * .invoke store16 third_task, ARG0 |
paul@10 | 73 | .invoke call new_task, main_wait |
paul@8 | 74 | |
paul@8 | 75 | ; loop while other tasks exist |
paul@8 | 76 | |
paul@10 | 77 | main_wait: |
paul@8 | 78 | ldx #7 |
paul@8 | 79 | sei |
paul@8 | 80 | lda tasks, x ; check for the third task |
paul@8 | 81 | cli |
paul@8 | 82 | cmp #0 |
paul@10 | 83 | bne main_wait |
paul@8 | 84 | |
paul@8 | 85 | ; where no other tasks exist, remove this task by updating its PC to the |
paul@8 | 86 | ; end label and removing its entry |
paul@8 | 87 | |
paul@10 | 88 | .invoke store16 main_end, main_task+6 |
paul@8 | 89 | lda #0 |
paul@10 | 90 | .invoke call remove_task, + |
paul@8 | 91 | |
paul@8 | 92 | ; wait for shutdown |
paul@8 | 93 | |
paul@10 | 94 | * jmp - |
paul@8 | 95 | |
paul@8 | 96 | ; return here with the initial main program stack |
paul@8 | 97 | |
paul@10 | 98 | main_end: |
paul@8 | 99 | rts |
paul@8 | 100 | |
paul@8 | 101 | |
paul@8 | 102 | |
paul@8 | 103 | ; main program task structure |
paul@8 | 104 | |
paul@8 | 105 | main_task: |
paul@8 | 106 | .word main_stack_base ; user stack pointer |
paul@8 | 107 | .byte 0 ; Y |
paul@8 | 108 | .byte 0 ; X |
paul@8 | 109 | .byte 0 ; A |
paul@8 | 110 | .byte 0 ; saved flags |
paul@8 | 111 | .word 0 ; saved PC |
paul@0 | 112 | |
paul@3 | 113 | |
paul@3 | 114 | |
paul@0 | 115 | ; install the interrupt handler address in IRQ1V |
paul@0 | 116 | ; |
paul@0 | 117 | ; affects: A |
paul@0 | 118 | |
paul@0 | 119 | install_handler: |
paul@0 | 120 | sei |
paul@3 | 121 | .invoke mov16 IRQ1V, old_handler |
paul@3 | 122 | .invoke store16 handler, IRQ1V |
paul@0 | 123 | cli |
paul@0 | 124 | rts |
paul@0 | 125 | |
paul@3 | 126 | |
paul@3 | 127 | |
paul@0 | 128 | ; handle interrupts |
paul@0 | 129 | ; |
paul@0 | 130 | ; affects: (temporary stack usage) |
paul@0 | 131 | |
paul@0 | 132 | handler: |
paul@0 | 133 | pha ; A -> stack |
paul@0 | 134 | txa |
paul@0 | 135 | pha ; X -> stack |
paul@0 | 136 | tya |
paul@0 | 137 | pha ; Y -> stack |
paul@2 | 138 | |
paul@8 | 139 | tsx ; capture the stack pointer now for later use |
paul@8 | 140 | ; (stack is <empty>, Y, X, A, F, LSB, MSB) |
paul@8 | 141 | |
paul@2 | 142 | ; save zero-page locations used in the handler |
paul@2 | 143 | |
paul@2 | 144 | .invoke push16 SP |
paul@2 | 145 | .invoke push16 NEXT |
paul@2 | 146 | .invoke push16 CURRENT |
paul@2 | 147 | |
paul@2 | 148 | ; initialise the stack frame pointer |
paul@2 | 149 | |
paul@8 | 150 | dex ; make the frame compatible with the task structure |
paul@2 | 151 | txa |
paul@2 | 152 | sta SP |
paul@2 | 153 | lda #$01 ; $01xx |
paul@2 | 154 | sta SPH |
paul@2 | 155 | |
paul@0 | 156 | ; test PC for execution of ROM routines |
paul@0 | 157 | ; these are probably not re-entrant |
paul@0 | 158 | |
paul@0 | 159 | ; obtain the stack location of the stored PC MSB |
paul@0 | 160 | |
paul@8 | 161 | ldy #7 ; offset of MSB (_, _, Y, X, A, F, LSB, MSB) |
paul@2 | 162 | lda (SP), y |
paul@0 | 163 | |
paul@0 | 164 | ; reference the stack location and compute PC MSB & $80 |
paul@0 | 165 | and #$80 |
paul@0 | 166 | cmp #$80 |
paul@0 | 167 | |
paul@0 | 168 | ; exit if PC MSB & $80 != 0 |
paul@10 | 169 | beq handler_exit |
paul@2 | 170 | |
paul@8 | 171 | ; obtain the current task using the task offset |
paul@2 | 172 | |
paul@3 | 173 | ldx task_offset |
paul@2 | 174 | lda tasks, x |
paul@8 | 175 | sta CURRENT |
paul@2 | 176 | inx |
paul@2 | 177 | lda tasks, x |
paul@8 | 178 | sta CURRENTH |
paul@8 | 179 | dex |
paul@8 | 180 | |
paul@8 | 181 | ; store the user stack for the task |
paul@8 | 182 | |
paul@8 | 183 | .invoke mov16_to_ref USER, CURRENT |
paul@8 | 184 | |
paul@8 | 185 | ; store registers, flags, PC in current task structure |
paul@8 | 186 | |
paul@8 | 187 | ldy #7 ; offset of MSB (_, _, Y, X, A, F, LSB, MSB) |
paul@8 | 188 | * .invoke mov8_refs SP, CURRENT |
paul@8 | 189 | dey |
paul@8 | 190 | cpy #2 |
paul@8 | 191 | bpl - |
paul@8 | 192 | |
paul@10 | 193 | handler_load_task: |
paul@8 | 194 | |
paul@8 | 195 | ; examine the next task |
paul@8 | 196 | |
paul@8 | 197 | inx |
paul@2 | 198 | inx |
paul@2 | 199 | |
paul@2 | 200 | ; reset the task index if necessary |
paul@2 | 201 | |
paul@3 | 202 | cpx #TASK_TABLE_LENGTH |
paul@10 | 203 | bne + |
paul@2 | 204 | ldx #0 |
paul@2 | 205 | |
paul@8 | 206 | ; obtain task location MSB |
paul@8 | 207 | |
paul@10 | 208 | * inx |
paul@8 | 209 | lda tasks, x |
paul@8 | 210 | sta NEXTH |
paul@8 | 211 | |
paul@8 | 212 | ; install if not a null task (tasks do not reside in zero page) |
paul@8 | 213 | |
paul@8 | 214 | cmp #0 |
paul@10 | 215 | bne handler_restore_task |
paul@8 | 216 | |
paul@8 | 217 | ; test against the start index |
paul@8 | 218 | ; where a complete circuit of the task table has not been performed, |
paul@8 | 219 | ; continue |
paul@8 | 220 | |
paul@8 | 221 | dex |
paul@8 | 222 | txa |
paul@8 | 223 | cmp task_offset |
paul@10 | 224 | bne handler_load_task |
paul@8 | 225 | |
paul@8 | 226 | ; where a complete circuit has been performed without finding any tasks |
paul@8 | 227 | ; uninstall the handler and install the details of the end handler |
paul@8 | 228 | |
paul@8 | 229 | sei |
paul@8 | 230 | .invoke mov16 old_handler, IRQ1V |
paul@8 | 231 | cli |
paul@8 | 232 | |
paul@8 | 233 | ; the end handler resides at the end of the task table |
paul@8 | 234 | |
paul@8 | 235 | ldx #TASK_TABLE_LENGTH |
paul@8 | 236 | inx |
paul@8 | 237 | lda tasks, x |
paul@8 | 238 | sta NEXTH |
paul@8 | 239 | |
paul@10 | 240 | handler_restore_task: |
paul@8 | 241 | |
paul@8 | 242 | dex |
paul@8 | 243 | lda tasks, x |
paul@8 | 244 | sta NEXT |
paul@3 | 245 | stx task_offset |
paul@2 | 246 | |
paul@8 | 247 | ; load registers, flags, PC from next task structure |
paul@5 | 248 | |
paul@8 | 249 | ldy #7 ; offset of MSB (_, _, Y, X, A, F, LSB, MSB) |
paul@8 | 250 | * .invoke mov8_refs NEXT, SP |
paul@2 | 251 | dey |
paul@8 | 252 | cpy #2 |
paul@8 | 253 | bpl - |
paul@0 | 254 | |
paul@5 | 255 | ; set the user stack for the task |
paul@5 | 256 | |
paul@5 | 257 | .invoke mov16_from_ref NEXT, USER |
paul@5 | 258 | |
paul@10 | 259 | handler_exit: |
paul@2 | 260 | |
paul@2 | 261 | ; restore zero-page locations used in the handler |
paul@2 | 262 | |
paul@2 | 263 | .invoke pull16 CURRENT |
paul@2 | 264 | .invoke pull16 NEXT |
paul@2 | 265 | .invoke pull16 SP |
paul@2 | 266 | |
paul@0 | 267 | pla |
paul@0 | 268 | tay ; stack -> Y |
paul@0 | 269 | pla |
paul@0 | 270 | tax ; stack -> X |
paul@0 | 271 | pla ; stack -> A |
paul@0 | 272 | jmp (old_handler) |
paul@0 | 273 | |
paul@3 | 274 | |
paul@3 | 275 | |
paul@2 | 276 | ; location of previous interrupt handler |
paul@2 | 277 | |
paul@0 | 278 | old_handler: .word 0 |
paul@0 | 279 | |
paul@3 | 280 | ; offset of current task in table (in multiples of 2) |
paul@2 | 281 | |
paul@3 | 282 | task_offset: .byte 0 |
paul@2 | 283 | |
paul@2 | 284 | ; task table containing locations of each task |
paul@2 | 285 | |
paul@2 | 286 | tasks: |
paul@3 | 287 | .word 0 |
paul@3 | 288 | .word 0 |
paul@2 | 289 | .word 0 |
paul@2 | 290 | .word 0 |
paul@8 | 291 | .word 0 ; last entry |
paul@8 | 292 | end_handler: |
paul@8 | 293 | .word main_task ; end handler entry |
paul@2 | 294 | |
paul@3 | 295 | |
paul@3 | 296 | |
paul@3 | 297 | ; add a new task to the table |
paul@3 | 298 | ; |
paul@5 | 299 | ; ARG0, ARG0H: location of task structure |
paul@3 | 300 | ; affects: A, Y |
paul@7 | 301 | ; returns: 0 if successful, 1 if unsuccessful |
paul@3 | 302 | |
paul@3 | 303 | new_task: |
paul@3 | 304 | |
paul@3 | 305 | ; check the MSB of each task table entry |
paul@3 | 306 | |
paul@3 | 307 | ldy #1 |
paul@4 | 308 | |
paul@10 | 309 | new_task_check: |
paul@3 | 310 | lda tasks, y |
paul@3 | 311 | cmp #0 |
paul@10 | 312 | beq new_task_add |
paul@3 | 313 | iny |
paul@3 | 314 | iny |
paul@3 | 315 | |
paul@3 | 316 | ; if no space is found by the end of the table, exit |
paul@3 | 317 | |
paul@3 | 318 | cpy #TASK_TABLE_LENGTH |
paul@10 | 319 | bpl new_task_failed |
paul@10 | 320 | jmp new_task_check |
paul@3 | 321 | |
paul@10 | 322 | new_task_add: |
paul@3 | 323 | |
paul@3 | 324 | ; copy the task structure location to the table |
paul@3 | 325 | |
paul@4 | 326 | sei |
paul@5 | 327 | lda ARG0H |
paul@3 | 328 | sta tasks, y |
paul@3 | 329 | dey |
paul@5 | 330 | lda ARG0 |
paul@3 | 331 | sta tasks, y |
paul@4 | 332 | cli |
paul@3 | 333 | |
paul@7 | 334 | lda #0 |
paul@5 | 335 | .invoke return |
paul@3 | 336 | |
paul@10 | 337 | new_task_failed: |
paul@7 | 338 | lda #1 |
paul@5 | 339 | .invoke return |
paul@3 | 340 | |
paul@3 | 341 | |
paul@3 | 342 | |
paul@4 | 343 | ; remove a task from the table |
paul@4 | 344 | ; |
paul@4 | 345 | ; A: task offset |
paul@4 | 346 | ; affects: A, Y |
paul@4 | 347 | |
paul@4 | 348 | remove_task: |
paul@4 | 349 | |
paul@4 | 350 | ; zero out the table entry |
paul@4 | 351 | |
paul@4 | 352 | tay |
paul@4 | 353 | sei |
paul@4 | 354 | lda #0 |
paul@4 | 355 | sta tasks, y |
paul@4 | 356 | iny |
paul@4 | 357 | sta tasks, y |
paul@4 | 358 | cli |
paul@5 | 359 | .invoke return |
paul@5 | 360 | |
paul@4 | 361 | |
paul@4 | 362 | |
paul@2 | 363 | ; example tasks |
paul@2 | 364 | |
paul@2 | 365 | first_task: |
paul@5 | 366 | .word 0 ; user stack pointer |
paul@8 | 367 | .byte 0 ; Y |
paul@8 | 368 | .byte 0 ; X |
paul@8 | 369 | .byte 0 ; A |
paul@2 | 370 | .byte 0 ; saved flags |
paul@2 | 371 | .word first_task_start ; saved PC |
paul@2 | 372 | first_task_start: |
paul@6 | 373 | lda #1 |
paul@8 | 374 | sta $7000 |
paul@6 | 375 | first_task_continue: |
paul@8 | 376 | inc $7000 |
paul@6 | 377 | jmp first_task_continue |
paul@2 | 378 | |
paul@3 | 379 | |
paul@3 | 380 | |
paul@2 | 381 | second_task: |
paul@5 | 382 | .word 0 ; user stack pointer |
paul@8 | 383 | .byte 0 ; Y |
paul@8 | 384 | .byte 0 ; X |
paul@8 | 385 | .byte 0 ; A |
paul@2 | 386 | .byte 0 ; saved flags |
paul@2 | 387 | .word second_task_start ; saved PC |
paul@2 | 388 | second_task_start: |
paul@6 | 389 | lda #1 |
paul@8 | 390 | sta $7010 |
paul@6 | 391 | second_task_continue: |
paul@8 | 392 | inc $7010 |
paul@6 | 393 | jmp second_task_continue |
paul@2 | 394 | |
paul@4 | 395 | |
paul@4 | 396 | |
paul@4 | 397 | third_task: |
paul@5 | 398 | .word third_task_stack_base ; user stack pointer |
paul@8 | 399 | .byte 0 ; Y |
paul@8 | 400 | .byte 0 ; X |
paul@8 | 401 | .byte 0 ; A |
paul@4 | 402 | .byte 0 ; saved flags |
paul@4 | 403 | .word third_task_start ; saved PC |
paul@4 | 404 | third_task_start: |
paul@8 | 405 | ldx #2 |
paul@10 | 406 | third_task_begin: |
paul@7 | 407 | ldy #0 ; reset counter LSB |
paul@7 | 408 | lda #0 ; reset counter MSB |
paul@8 | 409 | sta $7021 |
paul@10 | 410 | third_task_loop: |
paul@8 | 411 | stx $7028 |
paul@8 | 412 | sty $7020 |
paul@6 | 413 | cpy #$ff |
paul@10 | 414 | bne third_task_continue |
paul@7 | 415 | clc |
paul@8 | 416 | lda $7021 ; next MSB |
paul@7 | 417 | adc #1 |
paul@8 | 418 | sta $7021 |
paul@4 | 419 | cmp #$ff |
paul@10 | 420 | bne third_task_continue |
paul@7 | 421 | txa |
paul@6 | 422 | .invoke pushA |
paul@7 | 423 | .invoke call remove_task, + |
paul@7 | 424 | * .invoke pullA |
paul@7 | 425 | tax |
paul@7 | 426 | inx ; move to next offset |
paul@7 | 427 | inx ; which is 2 locations away |
paul@8 | 428 | cpx #TASK_TABLE_LENGTH |
paul@10 | 429 | beq + |
paul@10 | 430 | jmp third_task_begin |
paul@10 | 431 | * ldx #0 |
paul@10 | 432 | jmp third_task_begin |
paul@10 | 433 | third_task_continue: |
paul@6 | 434 | iny |
paul@10 | 435 | jmp third_task_loop |
paul@5 | 436 | third_task_stack: |
paul@5 | 437 | .word 0 |
paul@5 | 438 | .word 0 |
paul@5 | 439 | .word 0 |
paul@5 | 440 | .word 0 |
paul@5 | 441 | .word 0 |
paul@7 | 442 | third_task_stack_base: |
paul@5 | 443 | .word 0 |
paul@4 | 444 | |
paul@0 | 445 | ; vim: tabstop=4 expandtab shiftwidth=4 |