1.1 --- a/switcher.oph Sat Jun 27 19:21:03 2015 +0200
1.2 +++ b/switcher.oph Sat Jun 27 20:01:40 2015 +0200
1.3 @@ -17,6 +17,8 @@
1.4
1.5 .include "macros.oph"
1.6
1.7 +.alias IRQ1V $204
1.8 +
1.9 .alias SP $70
1.10 .alias SPH $71
1.11 .alias NEXT $72
1.12 @@ -24,24 +26,34 @@
1.13 .alias CURRENT $74
1.14 .alias CURRENTH $75
1.15
1.16 +.alias TASK_TABLE_LENGTH 10
1.17 +
1.18 .org $2000
1.19 .text
1.20 main:
1.21 jsr install_handler
1.22 + .invoke store16 first_task, $76
1.23 + jsr new_task
1.24 + .invoke store16 second_task, $76
1.25 + jsr new_task
1.26 test:
1.27 jmp test
1.28
1.29 +
1.30 +
1.31 ; install the interrupt handler address in IRQ1V
1.32 ;
1.33 ; affects: A
1.34
1.35 install_handler:
1.36 sei
1.37 - .invoke mov16 $204, old_handler
1.38 - .invoke store16 handler, $204
1.39 + .invoke mov16 IRQ1V, old_handler
1.40 + .invoke store16 handler, IRQ1V
1.41 cli
1.42 rts
1.43
1.44 +
1.45 +
1.46 ; handle interrupts
1.47 ;
1.48 ; affects: (temporary stack usage)
1.49 @@ -91,7 +103,7 @@
1.50
1.51 ; load next task
1.52
1.53 - ldx task_index
1.54 + ldx task_offset
1.55 lda tasks, x
1.56 sta NEXT
1.57 inx
1.58 @@ -101,12 +113,12 @@
1.59
1.60 ; reset the task index if necessary
1.61
1.62 - cpx #10 ; length of tasks array
1.63 + cpx #TASK_TABLE_LENGTH
1.64 bne test_task
1.65 ldx #0
1.66
1.67 test_task:
1.68 - stx task_index
1.69 + stx task_offset
1.70
1.71 ; exit if null task
1.72
1.73 @@ -161,6 +173,8 @@
1.74 pla ; stack -> A
1.75 jmp (old_handler)
1.76
1.77 +
1.78 +
1.79 ; location of previous interrupt handler
1.80
1.81 old_handler: .word 0
1.82 @@ -169,19 +183,62 @@
1.83
1.84 current_task: .word 0
1.85
1.86 -; index of current task
1.87 +; offset of current task in table (in multiples of 2)
1.88
1.89 -task_index: .byte 0
1.90 +task_offset: .byte 0
1.91
1.92 ; task table containing locations of each task
1.93
1.94 tasks:
1.95 - .word first_task
1.96 - .word second_task
1.97 + .word 0
1.98 + .word 0
1.99 .word 0
1.100 .word 0
1.101 .word 0
1.102
1.103 +
1.104 +
1.105 +; add a new task to the table
1.106 +;
1.107 +; $76, $77: location of task structure
1.108 +; affects: A, Y
1.109 +; returns: A = 0 (success); A = 1 (failure)
1.110 +
1.111 +new_task:
1.112 +
1.113 + ; check the MSB of each task table entry
1.114 +
1.115 + ldy #1
1.116 + lda tasks, y
1.117 + cmp #0
1.118 + beq add_new_task
1.119 + iny
1.120 + iny
1.121 +
1.122 + ; if no space is found by the end of the table, exit
1.123 +
1.124 + cpy #TASK_TABLE_LENGTH
1.125 + bpl no_new_task
1.126 +
1.127 +add_new_task:
1.128 +
1.129 + ; copy the task structure location to the table
1.130 +
1.131 + lda $77
1.132 + sta tasks, y
1.133 + dey
1.134 + lda $76
1.135 + sta tasks, y
1.136 +
1.137 + lda #0
1.138 + rts
1.139 +
1.140 +no_new_task:
1.141 + lda #1
1.142 + rts
1.143 +
1.144 +
1.145 +
1.146 ; example tasks
1.147
1.148 first_task:
1.149 @@ -194,6 +251,8 @@
1.150 .invoke add16 1, $7000
1.151 jmp first_task_start
1.152
1.153 +
1.154 +
1.155 second_task:
1.156 .byte 0 ; currently unused
1.157 .byte 0 ; currently unused