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@7 | 41 | .alias main_stack $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@5 | 54 | .invoke store16 tasks, ARG1 |
paul@5 | 55 | .invoke store16 main_stack, USER |
paul@5 | 56 | .invoke store16 first_task, ARG0 |
paul@7 | 57 | .invoke call new_task, + |
paul@7 | 58 | * .invoke store16 second_task, ARG0 |
paul@7 | 59 | .invoke call new_task, + |
paul@7 | 60 | * .invoke store16 third_task, ARG0 |
paul@7 | 61 | .invoke call new_task, + |
paul@7 | 62 | * jsr install_handler |
paul@4 | 63 | wait: |
paul@4 | 64 | jmp wait ; wait for the switcher to take over |
paul@0 | 65 | |
paul@3 | 66 | |
paul@3 | 67 | |
paul@0 | 68 | ; install the interrupt handler address in IRQ1V |
paul@0 | 69 | ; |
paul@0 | 70 | ; affects: A |
paul@0 | 71 | |
paul@0 | 72 | install_handler: |
paul@0 | 73 | sei |
paul@3 | 74 | .invoke mov16 IRQ1V, old_handler |
paul@3 | 75 | .invoke store16 handler, IRQ1V |
paul@0 | 76 | cli |
paul@0 | 77 | rts |
paul@0 | 78 | |
paul@3 | 79 | |
paul@3 | 80 | |
paul@0 | 81 | ; handle interrupts |
paul@0 | 82 | ; |
paul@0 | 83 | ; affects: (temporary stack usage) |
paul@0 | 84 | |
paul@0 | 85 | handler: |
paul@0 | 86 | pha ; A -> stack |
paul@0 | 87 | txa |
paul@0 | 88 | pha ; X -> stack |
paul@2 | 89 | tsx ; capture the stack pointer now for later use |
paul@2 | 90 | ; (stack is <empty>, X, A, F, LSB, MSB) |
paul@0 | 91 | tya |
paul@0 | 92 | pha ; Y -> stack |
paul@2 | 93 | |
paul@2 | 94 | ; save zero-page locations used in the handler |
paul@2 | 95 | |
paul@2 | 96 | .invoke push16 SP |
paul@2 | 97 | .invoke push16 NEXT |
paul@2 | 98 | .invoke push16 CURRENT |
paul@2 | 99 | |
paul@2 | 100 | init_sp: |
paul@2 | 101 | |
paul@2 | 102 | ; initialise the stack frame pointer |
paul@2 | 103 | |
paul@2 | 104 | txa |
paul@2 | 105 | sta SP |
paul@2 | 106 | lda #$01 ; $01xx |
paul@2 | 107 | sta SPH |
paul@2 | 108 | |
paul@2 | 109 | test_pc: |
paul@0 | 110 | |
paul@0 | 111 | ; test PC for execution of ROM routines |
paul@0 | 112 | ; these are probably not re-entrant |
paul@0 | 113 | |
paul@0 | 114 | ; obtain the stack location of the stored PC MSB |
paul@0 | 115 | |
paul@2 | 116 | ldy #5 ; offset of MSB (Y, X, A, F, LSB, MSB) |
paul@2 | 117 | lda (SP), y |
paul@0 | 118 | |
paul@0 | 119 | ; reference the stack location and compute PC MSB & $80 |
paul@0 | 120 | and #$80 |
paul@0 | 121 | cmp #$80 |
paul@0 | 122 | |
paul@0 | 123 | ; exit if PC MSB & $80 != 0 |
paul@0 | 124 | beq exit_handler |
paul@0 | 125 | |
paul@2 | 126 | load_task: |
paul@2 | 127 | |
paul@2 | 128 | ; load next task |
paul@2 | 129 | |
paul@3 | 130 | ldx task_offset |
paul@2 | 131 | lda tasks, x |
paul@2 | 132 | sta NEXT |
paul@2 | 133 | inx |
paul@2 | 134 | lda tasks, x |
paul@2 | 135 | sta NEXTH |
paul@2 | 136 | inx |
paul@2 | 137 | |
paul@2 | 138 | ; reset the task index if necessary |
paul@2 | 139 | |
paul@3 | 140 | cpx #TASK_TABLE_LENGTH |
paul@2 | 141 | bne test_task |
paul@2 | 142 | ldx #0 |
paul@2 | 143 | |
paul@2 | 144 | test_task: |
paul@3 | 145 | stx task_offset |
paul@2 | 146 | |
paul@2 | 147 | ; exit if null task |
paul@2 | 148 | |
paul@2 | 149 | lda NEXTH |
paul@2 | 150 | cmp #0 |
paul@2 | 151 | beq exit_handler |
paul@2 | 152 | |
paul@2 | 153 | switch_task: |
paul@2 | 154 | |
paul@2 | 155 | ; store flags, PC in current task structure |
paul@2 | 156 | |
paul@2 | 157 | .invoke mov16 current_task, CURRENT |
paul@2 | 158 | |
paul@5 | 159 | ; store the user stack for the task |
paul@5 | 160 | |
paul@5 | 161 | .invoke mov16_to_ref USER, CURRENT |
paul@5 | 162 | |
paul@2 | 163 | ldy #5 ; offset of MSB (Y, X, A, F, LSB, MSB) |
paul@5 | 164 | .invoke mov8_refs SP, CURRENT |
paul@2 | 165 | dey |
paul@5 | 166 | .invoke mov8_refs SP, CURRENT |
paul@2 | 167 | dey |
paul@5 | 168 | .invoke mov8_refs SP, CURRENT |
paul@2 | 169 | |
paul@2 | 170 | ; load flags, PC from next task structure |
paul@2 | 171 | |
paul@5 | 172 | .invoke mov8_refs NEXT, SP |
paul@2 | 173 | iny |
paul@5 | 174 | .invoke mov8_refs NEXT, SP |
paul@2 | 175 | iny |
paul@5 | 176 | .invoke mov8_refs NEXT, SP |
paul@2 | 177 | iny |
paul@2 | 178 | |
paul@2 | 179 | ; make the next task the current one |
paul@2 | 180 | |
paul@2 | 181 | .invoke mov16 NEXT, current_task |
paul@0 | 182 | |
paul@5 | 183 | ; set the user stack for the task |
paul@5 | 184 | |
paul@5 | 185 | .invoke mov16_from_ref NEXT, USER |
paul@5 | 186 | |
paul@0 | 187 | exit_handler: |
paul@2 | 188 | |
paul@2 | 189 | ; restore zero-page locations used in the handler |
paul@2 | 190 | |
paul@2 | 191 | .invoke pull16 CURRENT |
paul@2 | 192 | .invoke pull16 NEXT |
paul@2 | 193 | .invoke pull16 SP |
paul@2 | 194 | |
paul@0 | 195 | pla |
paul@0 | 196 | tay ; stack -> Y |
paul@0 | 197 | pla |
paul@0 | 198 | tax ; stack -> X |
paul@0 | 199 | pla ; stack -> A |
paul@0 | 200 | jmp (old_handler) |
paul@0 | 201 | |
paul@3 | 202 | |
paul@3 | 203 | |
paul@2 | 204 | ; location of previous interrupt handler |
paul@2 | 205 | |
paul@0 | 206 | old_handler: .word 0 |
paul@0 | 207 | |
paul@2 | 208 | ; location of current task (duplicated from the table) |
paul@2 | 209 | |
paul@2 | 210 | current_task: .word 0 |
paul@2 | 211 | |
paul@3 | 212 | ; offset of current task in table (in multiples of 2) |
paul@2 | 213 | |
paul@3 | 214 | task_offset: .byte 0 |
paul@2 | 215 | |
paul@2 | 216 | ; task table containing locations of each task |
paul@2 | 217 | |
paul@2 | 218 | tasks: |
paul@3 | 219 | .word 0 |
paul@3 | 220 | .word 0 |
paul@2 | 221 | .word 0 |
paul@2 | 222 | .word 0 |
paul@2 | 223 | .word 0 |
paul@2 | 224 | |
paul@3 | 225 | |
paul@3 | 226 | |
paul@3 | 227 | ; add a new task to the table |
paul@3 | 228 | ; |
paul@5 | 229 | ; ARG0, ARG0H: location of task structure |
paul@3 | 230 | ; affects: A, Y |
paul@7 | 231 | ; returns: 0 if successful, 1 if unsuccessful |
paul@3 | 232 | |
paul@3 | 233 | new_task: |
paul@3 | 234 | |
paul@3 | 235 | ; check the MSB of each task table entry |
paul@3 | 236 | |
paul@3 | 237 | ldy #1 |
paul@4 | 238 | |
paul@4 | 239 | check_new_task: |
paul@3 | 240 | lda tasks, y |
paul@3 | 241 | cmp #0 |
paul@3 | 242 | beq add_new_task |
paul@3 | 243 | iny |
paul@3 | 244 | iny |
paul@3 | 245 | |
paul@3 | 246 | ; if no space is found by the end of the table, exit |
paul@3 | 247 | |
paul@3 | 248 | cpy #TASK_TABLE_LENGTH |
paul@3 | 249 | bpl no_new_task |
paul@4 | 250 | jmp check_new_task |
paul@3 | 251 | |
paul@3 | 252 | add_new_task: |
paul@3 | 253 | |
paul@3 | 254 | ; copy the task structure location to the table |
paul@3 | 255 | |
paul@4 | 256 | sei |
paul@5 | 257 | lda ARG0H |
paul@3 | 258 | sta tasks, y |
paul@3 | 259 | dey |
paul@5 | 260 | lda ARG0 |
paul@3 | 261 | sta tasks, y |
paul@4 | 262 | cli |
paul@3 | 263 | |
paul@7 | 264 | lda #0 |
paul@5 | 265 | .invoke return |
paul@3 | 266 | |
paul@3 | 267 | no_new_task: |
paul@7 | 268 | lda #1 |
paul@5 | 269 | .invoke return |
paul@3 | 270 | |
paul@3 | 271 | |
paul@3 | 272 | |
paul@4 | 273 | ; remove a task from the table |
paul@4 | 274 | ; |
paul@4 | 275 | ; A: task offset |
paul@4 | 276 | ; affects: A, Y |
paul@4 | 277 | |
paul@4 | 278 | remove_task: |
paul@4 | 279 | |
paul@4 | 280 | ; zero out the table entry |
paul@4 | 281 | |
paul@4 | 282 | tay |
paul@4 | 283 | sei |
paul@4 | 284 | lda #0 |
paul@4 | 285 | sta tasks, y |
paul@4 | 286 | iny |
paul@4 | 287 | sta tasks, y |
paul@4 | 288 | cli |
paul@5 | 289 | .invoke return |
paul@5 | 290 | |
paul@4 | 291 | |
paul@4 | 292 | |
paul@2 | 293 | ; example tasks |
paul@2 | 294 | |
paul@2 | 295 | first_task: |
paul@5 | 296 | .word 0 ; user stack pointer |
paul@2 | 297 | .byte 0 ; currently unused |
paul@2 | 298 | .byte 0 ; saved flags |
paul@2 | 299 | .word first_task_start ; saved PC |
paul@2 | 300 | first_task_start: |
paul@6 | 301 | lda #1 |
paul@6 | 302 | first_task_continue: |
paul@6 | 303 | sta $7000 |
paul@6 | 304 | adc #1 |
paul@6 | 305 | jmp first_task_continue |
paul@2 | 306 | |
paul@3 | 307 | |
paul@3 | 308 | |
paul@2 | 309 | second_task: |
paul@5 | 310 | .word 0 ; user stack pointer |
paul@2 | 311 | .byte 0 ; currently unused |
paul@2 | 312 | .byte 0 ; saved flags |
paul@2 | 313 | .word second_task_start ; saved PC |
paul@2 | 314 | second_task_start: |
paul@6 | 315 | lda #1 |
paul@6 | 316 | second_task_continue: |
paul@6 | 317 | sta $7008 |
paul@6 | 318 | adc #1 |
paul@6 | 319 | jmp second_task_continue |
paul@2 | 320 | |
paul@4 | 321 | |
paul@4 | 322 | |
paul@4 | 323 | third_task: |
paul@5 | 324 | .word third_task_stack_base ; user stack pointer |
paul@4 | 325 | .byte 0 ; currently unused |
paul@4 | 326 | .byte 0 ; saved flags |
paul@4 | 327 | .word third_task_start ; saved PC |
paul@4 | 328 | third_task_start: |
paul@4 | 329 | ldx #0 |
paul@7 | 330 | _begin: |
paul@7 | 331 | ldy #0 ; reset counter LSB |
paul@7 | 332 | lda #0 ; reset counter MSB |
paul@7 | 333 | sta $7011 |
paul@4 | 334 | _loop: |
paul@7 | 335 | sty $7010 |
paul@6 | 336 | cpy #$ff |
paul@4 | 337 | bne _continue |
paul@7 | 338 | clc |
paul@7 | 339 | lda $7011 ; next MSB |
paul@7 | 340 | adc #1 |
paul@7 | 341 | sta $7011 |
paul@4 | 342 | cmp #$ff |
paul@4 | 343 | bne _continue |
paul@7 | 344 | txa |
paul@6 | 345 | .invoke pushA |
paul@7 | 346 | .invoke call remove_task, + |
paul@7 | 347 | * .invoke pullA |
paul@7 | 348 | tax |
paul@7 | 349 | inx ; move to next offset |
paul@7 | 350 | inx ; which is 2 locations away |
paul@7 | 351 | jmp _begin |
paul@4 | 352 | _continue: |
paul@6 | 353 | iny |
paul@4 | 354 | jmp _loop |
paul@5 | 355 | third_task_stack: |
paul@5 | 356 | .word 0 |
paul@5 | 357 | .word 0 |
paul@5 | 358 | .word 0 |
paul@5 | 359 | .word 0 |
paul@5 | 360 | .word 0 |
paul@7 | 361 | third_task_stack_base: |
paul@5 | 362 | .word 0 |
paul@4 | 363 | |
paul@0 | 364 | ; vim: tabstop=4 expandtab shiftwidth=4 |