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@2 | 20 | .alias SP $70 |
paul@2 | 21 | .alias SPH $71 |
paul@2 | 22 | .alias NEXT $72 |
paul@2 | 23 | .alias NEXTH $73 |
paul@2 | 24 | .alias CURRENT $74 |
paul@2 | 25 | .alias CURRENTH $75 |
paul@2 | 26 | |
paul@0 | 27 | .org $2000 |
paul@0 | 28 | .text |
paul@0 | 29 | main: |
paul@0 | 30 | jsr install_handler |
paul@2 | 31 | test: |
paul@2 | 32 | jmp test |
paul@0 | 33 | |
paul@0 | 34 | ; install the interrupt handler address in IRQ1V |
paul@0 | 35 | ; |
paul@0 | 36 | ; affects: A |
paul@0 | 37 | |
paul@0 | 38 | install_handler: |
paul@0 | 39 | sei |
paul@0 | 40 | .invoke mov16 $204, old_handler |
paul@0 | 41 | .invoke store16 handler, $204 |
paul@0 | 42 | cli |
paul@0 | 43 | rts |
paul@0 | 44 | |
paul@0 | 45 | ; handle interrupts |
paul@0 | 46 | ; |
paul@0 | 47 | ; affects: (temporary stack usage) |
paul@0 | 48 | |
paul@0 | 49 | handler: |
paul@0 | 50 | pha ; A -> stack |
paul@0 | 51 | txa |
paul@0 | 52 | pha ; X -> stack |
paul@2 | 53 | tsx ; capture the stack pointer now for later use |
paul@2 | 54 | ; (stack is <empty>, X, A, F, LSB, MSB) |
paul@0 | 55 | tya |
paul@0 | 56 | pha ; Y -> stack |
paul@2 | 57 | |
paul@2 | 58 | ; save zero-page locations used in the handler |
paul@2 | 59 | |
paul@2 | 60 | .invoke push16 SP |
paul@2 | 61 | .invoke push16 NEXT |
paul@2 | 62 | .invoke push16 CURRENT |
paul@2 | 63 | |
paul@2 | 64 | init_sp: |
paul@2 | 65 | |
paul@2 | 66 | ; initialise the stack frame pointer |
paul@2 | 67 | |
paul@2 | 68 | txa |
paul@2 | 69 | sta SP |
paul@2 | 70 | lda #$01 ; $01xx |
paul@2 | 71 | sta SPH |
paul@2 | 72 | |
paul@2 | 73 | test_pc: |
paul@0 | 74 | |
paul@0 | 75 | ; test PC for execution of ROM routines |
paul@0 | 76 | ; these are probably not re-entrant |
paul@0 | 77 | |
paul@0 | 78 | ; obtain the stack location of the stored PC MSB |
paul@0 | 79 | |
paul@2 | 80 | ldy #5 ; offset of MSB (Y, X, A, F, LSB, MSB) |
paul@2 | 81 | lda (SP), y |
paul@0 | 82 | |
paul@0 | 83 | ; reference the stack location and compute PC MSB & $80 |
paul@0 | 84 | and #$80 |
paul@0 | 85 | cmp #$80 |
paul@0 | 86 | |
paul@0 | 87 | ; exit if PC MSB & $80 != 0 |
paul@0 | 88 | beq exit_handler |
paul@0 | 89 | |
paul@2 | 90 | load_task: |
paul@2 | 91 | |
paul@2 | 92 | ; load next task |
paul@2 | 93 | |
paul@2 | 94 | ldx task_index |
paul@2 | 95 | lda tasks, x |
paul@2 | 96 | sta NEXT |
paul@2 | 97 | inx |
paul@2 | 98 | lda tasks, x |
paul@2 | 99 | sta NEXTH |
paul@2 | 100 | inx |
paul@2 | 101 | |
paul@2 | 102 | ; reset the task index if necessary |
paul@2 | 103 | |
paul@2 | 104 | cpx #10 ; length of tasks array |
paul@2 | 105 | bne test_task |
paul@2 | 106 | ldx #0 |
paul@2 | 107 | |
paul@2 | 108 | test_task: |
paul@2 | 109 | stx task_index |
paul@2 | 110 | |
paul@2 | 111 | ; exit if null task |
paul@2 | 112 | |
paul@2 | 113 | lda NEXTH |
paul@2 | 114 | cmp #0 |
paul@2 | 115 | beq exit_handler |
paul@2 | 116 | |
paul@2 | 117 | switch_task: |
paul@2 | 118 | |
paul@2 | 119 | ; store flags, PC in current task structure |
paul@2 | 120 | |
paul@2 | 121 | .invoke mov16 current_task, CURRENT |
paul@2 | 122 | |
paul@2 | 123 | ldy #5 ; offset of MSB (Y, X, A, F, LSB, MSB) |
paul@2 | 124 | lda (SP), y |
paul@2 | 125 | sta (CURRENT), y |
paul@2 | 126 | dey |
paul@2 | 127 | lda (SP), y |
paul@2 | 128 | sta (CURRENT), y |
paul@2 | 129 | dey |
paul@2 | 130 | lda (SP), y |
paul@2 | 131 | sta (CURRENT), y |
paul@2 | 132 | |
paul@2 | 133 | ; load flags, PC from next task structure |
paul@2 | 134 | |
paul@2 | 135 | lda (NEXT), y |
paul@2 | 136 | sta (SP), y |
paul@2 | 137 | iny |
paul@2 | 138 | lda (NEXT), y |
paul@2 | 139 | sta (SP), y |
paul@2 | 140 | iny |
paul@2 | 141 | lda (NEXT), y |
paul@2 | 142 | sta (SP), y |
paul@2 | 143 | iny |
paul@2 | 144 | |
paul@2 | 145 | ; make the next task the current one |
paul@2 | 146 | |
paul@2 | 147 | .invoke mov16 NEXT, current_task |
paul@0 | 148 | |
paul@0 | 149 | exit_handler: |
paul@2 | 150 | |
paul@2 | 151 | ; restore zero-page locations used in the handler |
paul@2 | 152 | |
paul@2 | 153 | .invoke pull16 CURRENT |
paul@2 | 154 | .invoke pull16 NEXT |
paul@2 | 155 | .invoke pull16 SP |
paul@2 | 156 | |
paul@0 | 157 | pla |
paul@0 | 158 | tay ; stack -> Y |
paul@0 | 159 | pla |
paul@0 | 160 | tax ; stack -> X |
paul@0 | 161 | pla ; stack -> A |
paul@0 | 162 | jmp (old_handler) |
paul@0 | 163 | |
paul@2 | 164 | ; location of previous interrupt handler |
paul@2 | 165 | |
paul@0 | 166 | old_handler: .word 0 |
paul@0 | 167 | |
paul@2 | 168 | ; location of current task (duplicated from the table) |
paul@2 | 169 | |
paul@2 | 170 | current_task: .word 0 |
paul@2 | 171 | |
paul@2 | 172 | ; index of current task |
paul@2 | 173 | |
paul@2 | 174 | task_index: .byte 0 |
paul@2 | 175 | |
paul@2 | 176 | ; task table containing locations of each task |
paul@2 | 177 | |
paul@2 | 178 | tasks: |
paul@2 | 179 | .word first_task |
paul@2 | 180 | .word second_task |
paul@2 | 181 | .word 0 |
paul@2 | 182 | .word 0 |
paul@2 | 183 | .word 0 |
paul@2 | 184 | |
paul@2 | 185 | ; example tasks |
paul@2 | 186 | |
paul@2 | 187 | first_task: |
paul@2 | 188 | .byte 0 ; currently unused |
paul@2 | 189 | .byte 0 ; currently unused |
paul@2 | 190 | .byte 0 ; currently unused |
paul@2 | 191 | .byte 0 ; saved flags |
paul@2 | 192 | .word first_task_start ; saved PC |
paul@2 | 193 | first_task_start: |
paul@2 | 194 | .invoke add16 1, $7000 |
paul@2 | 195 | jmp first_task_start |
paul@2 | 196 | |
paul@2 | 197 | second_task: |
paul@2 | 198 | .byte 0 ; currently unused |
paul@2 | 199 | .byte 0 ; currently unused |
paul@2 | 200 | .byte 0 ; currently unused |
paul@2 | 201 | .byte 0 ; saved flags |
paul@2 | 202 | .word second_task_start ; saved PC |
paul@2 | 203 | second_task_start: |
paul@2 | 204 | .invoke add16 1, $7008 |
paul@2 | 205 | jmp second_task_start |
paul@2 | 206 | |
paul@0 | 207 | ; vim: tabstop=4 expandtab shiftwidth=4 |