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