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@8 | 73 | .invoke call new_task, wait |
paul@8 | 74 | |
paul@8 | 75 | ; loop while other tasks exist |
paul@8 | 76 | |
paul@4 | 77 | 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@8 | 83 | bne 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@8 | 88 | .invoke store16 end, main_task+6 |
paul@8 | 89 | lda #0 |
paul@8 | 90 | .invoke call remove_task, shutdown |
paul@8 | 91 | |
paul@8 | 92 | ; wait for shutdown |
paul@8 | 93 | |
paul@8 | 94 | shutdown: |
paul@8 | 95 | jmp shutdown |
paul@8 | 96 | |
paul@8 | 97 | ; return here with the initial main program stack |
paul@8 | 98 | |
paul@8 | 99 | end: |
paul@8 | 100 | rts |
paul@8 | 101 | |
paul@8 | 102 | |
paul@8 | 103 | |
paul@8 | 104 | ; main program task structure |
paul@8 | 105 | |
paul@8 | 106 | main_task: |
paul@8 | 107 | .word main_stack_base ; user stack pointer |
paul@8 | 108 | .byte 0 ; Y |
paul@8 | 109 | .byte 0 ; X |
paul@8 | 110 | .byte 0 ; A |
paul@8 | 111 | .byte 0 ; saved flags |
paul@8 | 112 | .word 0 ; saved PC |
paul@0 | 113 | |
paul@3 | 114 | |
paul@3 | 115 | |
paul@0 | 116 | ; install the interrupt handler address in IRQ1V |
paul@0 | 117 | ; |
paul@0 | 118 | ; affects: A |
paul@0 | 119 | |
paul@0 | 120 | install_handler: |
paul@0 | 121 | sei |
paul@3 | 122 | .invoke mov16 IRQ1V, old_handler |
paul@3 | 123 | .invoke store16 handler, IRQ1V |
paul@0 | 124 | cli |
paul@0 | 125 | rts |
paul@0 | 126 | |
paul@3 | 127 | |
paul@3 | 128 | |
paul@0 | 129 | ; handle interrupts |
paul@0 | 130 | ; |
paul@0 | 131 | ; affects: (temporary stack usage) |
paul@0 | 132 | |
paul@0 | 133 | handler: |
paul@0 | 134 | pha ; A -> stack |
paul@0 | 135 | txa |
paul@0 | 136 | pha ; X -> stack |
paul@0 | 137 | tya |
paul@0 | 138 | pha ; Y -> stack |
paul@2 | 139 | |
paul@8 | 140 | tsx ; capture the stack pointer now for later use |
paul@8 | 141 | ; (stack is <empty>, Y, X, A, F, LSB, MSB) |
paul@8 | 142 | |
paul@2 | 143 | ; save zero-page locations used in the handler |
paul@2 | 144 | |
paul@2 | 145 | .invoke push16 SP |
paul@2 | 146 | .invoke push16 NEXT |
paul@2 | 147 | .invoke push16 CURRENT |
paul@8 | 148 | .invoke push16 TEMP |
paul@2 | 149 | |
paul@2 | 150 | init_sp: |
paul@2 | 151 | |
paul@2 | 152 | ; initialise the stack frame pointer |
paul@2 | 153 | |
paul@8 | 154 | dex ; make the frame compatible with the task structure |
paul@2 | 155 | txa |
paul@2 | 156 | sta SP |
paul@2 | 157 | lda #$01 ; $01xx |
paul@2 | 158 | sta SPH |
paul@2 | 159 | |
paul@2 | 160 | test_pc: |
paul@0 | 161 | |
paul@0 | 162 | ; test PC for execution of ROM routines |
paul@0 | 163 | ; these are probably not re-entrant |
paul@0 | 164 | |
paul@0 | 165 | ; obtain the stack location of the stored PC MSB |
paul@0 | 166 | |
paul@8 | 167 | ldy #7 ; offset of MSB (_, _, Y, X, A, F, LSB, MSB) |
paul@2 | 168 | lda (SP), y |
paul@0 | 169 | |
paul@0 | 170 | ; reference the stack location and compute PC MSB & $80 |
paul@0 | 171 | and #$80 |
paul@0 | 172 | cmp #$80 |
paul@0 | 173 | |
paul@0 | 174 | ; exit if PC MSB & $80 != 0 |
paul@0 | 175 | beq exit_handler |
paul@0 | 176 | |
paul@8 | 177 | evict_task: |
paul@2 | 178 | |
paul@8 | 179 | ; obtain the current task using the task offset |
paul@2 | 180 | |
paul@3 | 181 | ldx task_offset |
paul@2 | 182 | lda tasks, x |
paul@8 | 183 | sta CURRENT |
paul@2 | 184 | inx |
paul@2 | 185 | lda tasks, x |
paul@8 | 186 | sta CURRENTH |
paul@8 | 187 | dex |
paul@8 | 188 | |
paul@8 | 189 | ; store the user stack for the task |
paul@8 | 190 | |
paul@8 | 191 | .invoke mov16_to_ref USER, CURRENT |
paul@8 | 192 | |
paul@8 | 193 | ; store registers, flags, PC in current task structure |
paul@8 | 194 | |
paul@8 | 195 | ldy #7 ; offset of MSB (_, _, Y, X, A, F, LSB, MSB) |
paul@8 | 196 | * .invoke mov8_refs SP, CURRENT |
paul@8 | 197 | dey |
paul@8 | 198 | cpy #2 |
paul@8 | 199 | bpl - |
paul@8 | 200 | |
paul@8 | 201 | load_task: |
paul@8 | 202 | |
paul@8 | 203 | ; examine the next task |
paul@8 | 204 | |
paul@8 | 205 | inx |
paul@2 | 206 | inx |
paul@2 | 207 | |
paul@2 | 208 | ; reset the task index if necessary |
paul@2 | 209 | |
paul@3 | 210 | cpx #TASK_TABLE_LENGTH |
paul@2 | 211 | bne test_task |
paul@2 | 212 | ldx #0 |
paul@2 | 213 | |
paul@2 | 214 | test_task: |
paul@8 | 215 | |
paul@8 | 216 | ; obtain task location MSB |
paul@8 | 217 | |
paul@8 | 218 | inx |
paul@8 | 219 | lda tasks, x |
paul@8 | 220 | sta NEXTH |
paul@8 | 221 | |
paul@8 | 222 | ; install if not a null task (tasks do not reside in zero page) |
paul@8 | 223 | |
paul@8 | 224 | cmp #0 |
paul@8 | 225 | bne restore_task |
paul@8 | 226 | |
paul@8 | 227 | ; test against the start index |
paul@8 | 228 | ; where a complete circuit of the task table has not been performed, |
paul@8 | 229 | ; continue |
paul@8 | 230 | |
paul@8 | 231 | dex |
paul@8 | 232 | txa |
paul@8 | 233 | cmp task_offset |
paul@8 | 234 | bne load_task |
paul@8 | 235 | |
paul@8 | 236 | ; where a complete circuit has been performed without finding any tasks |
paul@8 | 237 | ; uninstall the handler and install the details of the end handler |
paul@8 | 238 | |
paul@8 | 239 | sei |
paul@8 | 240 | .invoke mov16 old_handler, IRQ1V |
paul@8 | 241 | cli |
paul@8 | 242 | |
paul@8 | 243 | ; the end handler resides at the end of the task table |
paul@8 | 244 | |
paul@8 | 245 | ldx #TASK_TABLE_LENGTH |
paul@8 | 246 | inx |
paul@8 | 247 | lda tasks, x |
paul@8 | 248 | sta NEXTH |
paul@8 | 249 | |
paul@8 | 250 | restore_task: |
paul@8 | 251 | |
paul@8 | 252 | dex |
paul@8 | 253 | lda tasks, x |
paul@8 | 254 | sta NEXT |
paul@3 | 255 | stx task_offset |
paul@2 | 256 | |
paul@8 | 257 | ; load registers, flags, PC from next task structure |
paul@5 | 258 | |
paul@8 | 259 | ldy #7 ; offset of MSB (_, _, Y, X, A, F, LSB, MSB) |
paul@8 | 260 | * .invoke mov8_refs NEXT, SP |
paul@2 | 261 | dey |
paul@8 | 262 | cpy #2 |
paul@8 | 263 | bpl - |
paul@0 | 264 | |
paul@5 | 265 | ; set the user stack for the task |
paul@5 | 266 | |
paul@5 | 267 | .invoke mov16_from_ref NEXT, USER |
paul@5 | 268 | |
paul@0 | 269 | exit_handler: |
paul@2 | 270 | |
paul@2 | 271 | ; restore zero-page locations used in the handler |
paul@2 | 272 | |
paul@8 | 273 | .invoke pull16 TEMP |
paul@2 | 274 | .invoke pull16 CURRENT |
paul@2 | 275 | .invoke pull16 NEXT |
paul@2 | 276 | .invoke pull16 SP |
paul@2 | 277 | |
paul@0 | 278 | pla |
paul@0 | 279 | tay ; stack -> Y |
paul@0 | 280 | pla |
paul@0 | 281 | tax ; stack -> X |
paul@0 | 282 | pla ; stack -> A |
paul@0 | 283 | jmp (old_handler) |
paul@0 | 284 | |
paul@3 | 285 | |
paul@3 | 286 | |
paul@2 | 287 | ; location of previous interrupt handler |
paul@2 | 288 | |
paul@0 | 289 | old_handler: .word 0 |
paul@0 | 290 | |
paul@3 | 291 | ; offset of current task in table (in multiples of 2) |
paul@2 | 292 | |
paul@3 | 293 | task_offset: .byte 0 |
paul@2 | 294 | |
paul@2 | 295 | ; task table containing locations of each task |
paul@2 | 296 | |
paul@2 | 297 | tasks: |
paul@3 | 298 | .word 0 |
paul@3 | 299 | .word 0 |
paul@2 | 300 | .word 0 |
paul@2 | 301 | .word 0 |
paul@8 | 302 | .word 0 ; last entry |
paul@8 | 303 | end_handler: |
paul@8 | 304 | .word main_task ; end handler entry |
paul@2 | 305 | |
paul@3 | 306 | |
paul@3 | 307 | |
paul@3 | 308 | ; add a new task to the table |
paul@3 | 309 | ; |
paul@5 | 310 | ; ARG0, ARG0H: location of task structure |
paul@3 | 311 | ; affects: A, Y |
paul@7 | 312 | ; returns: 0 if successful, 1 if unsuccessful |
paul@3 | 313 | |
paul@3 | 314 | new_task: |
paul@3 | 315 | |
paul@3 | 316 | ; check the MSB of each task table entry |
paul@3 | 317 | |
paul@3 | 318 | ldy #1 |
paul@4 | 319 | |
paul@4 | 320 | check_new_task: |
paul@3 | 321 | lda tasks, y |
paul@3 | 322 | cmp #0 |
paul@3 | 323 | beq add_new_task |
paul@3 | 324 | iny |
paul@3 | 325 | iny |
paul@3 | 326 | |
paul@3 | 327 | ; if no space is found by the end of the table, exit |
paul@3 | 328 | |
paul@3 | 329 | cpy #TASK_TABLE_LENGTH |
paul@3 | 330 | bpl no_new_task |
paul@4 | 331 | jmp check_new_task |
paul@3 | 332 | |
paul@3 | 333 | add_new_task: |
paul@3 | 334 | |
paul@3 | 335 | ; copy the task structure location to the table |
paul@3 | 336 | |
paul@4 | 337 | sei |
paul@5 | 338 | lda ARG0H |
paul@3 | 339 | sta tasks, y |
paul@3 | 340 | dey |
paul@5 | 341 | lda ARG0 |
paul@3 | 342 | sta tasks, y |
paul@4 | 343 | cli |
paul@3 | 344 | |
paul@7 | 345 | lda #0 |
paul@5 | 346 | .invoke return |
paul@3 | 347 | |
paul@3 | 348 | no_new_task: |
paul@7 | 349 | lda #1 |
paul@5 | 350 | .invoke return |
paul@3 | 351 | |
paul@3 | 352 | |
paul@3 | 353 | |
paul@4 | 354 | ; remove a task from the table |
paul@4 | 355 | ; |
paul@4 | 356 | ; A: task offset |
paul@4 | 357 | ; affects: A, Y |
paul@4 | 358 | |
paul@4 | 359 | remove_task: |
paul@4 | 360 | |
paul@4 | 361 | ; zero out the table entry |
paul@4 | 362 | |
paul@4 | 363 | tay |
paul@4 | 364 | sei |
paul@4 | 365 | lda #0 |
paul@4 | 366 | sta tasks, y |
paul@4 | 367 | iny |
paul@4 | 368 | sta tasks, y |
paul@4 | 369 | cli |
paul@5 | 370 | .invoke return |
paul@5 | 371 | |
paul@4 | 372 | |
paul@4 | 373 | |
paul@2 | 374 | ; example tasks |
paul@2 | 375 | |
paul@2 | 376 | first_task: |
paul@5 | 377 | .word 0 ; user stack pointer |
paul@8 | 378 | .byte 0 ; Y |
paul@8 | 379 | .byte 0 ; X |
paul@8 | 380 | .byte 0 ; A |
paul@2 | 381 | .byte 0 ; saved flags |
paul@2 | 382 | .word first_task_start ; saved PC |
paul@2 | 383 | first_task_start: |
paul@6 | 384 | lda #1 |
paul@8 | 385 | sta $7000 |
paul@6 | 386 | first_task_continue: |
paul@8 | 387 | inc $7000 |
paul@6 | 388 | jmp first_task_continue |
paul@2 | 389 | |
paul@3 | 390 | |
paul@3 | 391 | |
paul@2 | 392 | second_task: |
paul@5 | 393 | .word 0 ; user stack pointer |
paul@8 | 394 | .byte 0 ; Y |
paul@8 | 395 | .byte 0 ; X |
paul@8 | 396 | .byte 0 ; A |
paul@2 | 397 | .byte 0 ; saved flags |
paul@2 | 398 | .word second_task_start ; saved PC |
paul@2 | 399 | second_task_start: |
paul@6 | 400 | lda #1 |
paul@8 | 401 | sta $7010 |
paul@6 | 402 | second_task_continue: |
paul@8 | 403 | inc $7010 |
paul@6 | 404 | jmp second_task_continue |
paul@2 | 405 | |
paul@4 | 406 | |
paul@4 | 407 | |
paul@4 | 408 | third_task: |
paul@5 | 409 | .word third_task_stack_base ; user stack pointer |
paul@8 | 410 | .byte 0 ; Y |
paul@8 | 411 | .byte 0 ; X |
paul@8 | 412 | .byte 0 ; A |
paul@4 | 413 | .byte 0 ; saved flags |
paul@4 | 414 | .word third_task_start ; saved PC |
paul@4 | 415 | third_task_start: |
paul@8 | 416 | ldx #2 |
paul@7 | 417 | _begin: |
paul@7 | 418 | ldy #0 ; reset counter LSB |
paul@7 | 419 | lda #0 ; reset counter MSB |
paul@8 | 420 | sta $7021 |
paul@4 | 421 | _loop: |
paul@8 | 422 | stx $7028 |
paul@8 | 423 | sty $7020 |
paul@6 | 424 | cpy #$ff |
paul@4 | 425 | bne _continue |
paul@7 | 426 | clc |
paul@8 | 427 | lda $7021 ; next MSB |
paul@7 | 428 | adc #1 |
paul@8 | 429 | sta $7021 |
paul@4 | 430 | cmp #$ff |
paul@4 | 431 | bne _continue |
paul@7 | 432 | txa |
paul@6 | 433 | .invoke pushA |
paul@7 | 434 | .invoke call remove_task, + |
paul@7 | 435 | * .invoke pullA |
paul@7 | 436 | tax |
paul@7 | 437 | inx ; move to next offset |
paul@7 | 438 | inx ; which is 2 locations away |
paul@8 | 439 | cpx #TASK_TABLE_LENGTH |
paul@8 | 440 | beq _next |
paul@8 | 441 | jmp _begin |
paul@8 | 442 | _next: |
paul@8 | 443 | ldx #0 |
paul@7 | 444 | jmp _begin |
paul@4 | 445 | _continue: |
paul@6 | 446 | iny |
paul@4 | 447 | jmp _loop |
paul@5 | 448 | third_task_stack: |
paul@5 | 449 | .word 0 |
paul@5 | 450 | .word 0 |
paul@5 | 451 | .word 0 |
paul@5 | 452 | .word 0 |
paul@5 | 453 | .word 0 |
paul@7 | 454 | third_task_stack_base: |
paul@5 | 455 | .word 0 |
paul@4 | 456 | |
paul@0 | 457 | ; vim: tabstop=4 expandtab shiftwidth=4 |