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