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