1.1 --- a/switcher.oph Sun Jun 28 15:45:29 2015 +0200
1.2 +++ b/switcher.oph Sun Jun 28 19:23:06 2015 +0200
1.3 @@ -38,7 +38,8 @@
1.4 ; "user space" stack for the main program where the invocations might be
1.5 ; interrupted and where the CPU stack might be disrupted
1.6
1.7 -.alias main_stack $1fff
1.8 +.alias main_stack $1ffc
1.9 +.alias ABSTEMP $1ffe
1.10
1.11
1.12
1.13 @@ -50,135 +51,20 @@
1.14 ; main program, installing the handler and adding example tasks
1.15
1.16 main:
1.17 - jsr install_handler
1.18 .invoke store16 tasks, ARG1
1.19 .invoke store16 main_stack, USER
1.20 .invoke store16 first_task, ARG0
1.21 - .invoke call new_task
1.22 - .invoke store16 second_task, ARG0
1.23 - .invoke call new_task
1.24 - .invoke store16 third_task, ARG0
1.25 - .invoke call new_task
1.26 + .invoke call new_task, +
1.27 +* .invoke store16 second_task, ARG0
1.28 + .invoke call new_task, +
1.29 +* .invoke store16 third_task, ARG0
1.30 + .invoke call new_task, +
1.31 +* jsr install_handler
1.32 wait:
1.33 jmp wait ; wait for the switcher to take over
1.34
1.35
1.36
1.37 -; move CPU stack data to the "user space" stack
1.38 -;
1.39 -; affects: USER (gains LSB, MSB)
1.40 -
1.41 -copy_to_user_stack:
1.42 - pha ; A -> stack
1.43 - txa
1.44 - pha ; X -> stack
1.45 - tsx ; capture the stack pointer now for later use
1.46 - ; (stack is Y, X, A, LSB, MSB)
1.47 - tya
1.48 - pha ; Y -> stack
1.49 -
1.50 - ; save zero-page locations used
1.51 -
1.52 - .invoke push16 SP
1.53 -
1.54 - ; initialise the stack frame pointer
1.55 -
1.56 - txa
1.57 - sta SP
1.58 - lda #$01 ; $01xx
1.59 - sta SPH
1.60 -
1.61 - ; allocate "user space" stack space to be compatible with the CPU stack
1.62 -
1.63 - .invoke sub16 5, USER
1.64 -
1.65 - ; copy the PC, updated by 4 (see the call macro)
1.66 -
1.67 - ldy #3
1.68 - clc
1.69 - lda (SP), y
1.70 - adc #4
1.71 - sta (USER), y
1.72 - iny
1.73 - lda (SP), y
1.74 - adc #0
1.75 - sta (USER), y
1.76 -
1.77 - ; correct "user space" stack
1.78 -
1.79 - .invoke add16 3, USER
1.80 -
1.81 - ; restore zero-page locations used
1.82 -
1.83 - .invoke pull16 SP
1.84 -
1.85 - ; return to the caller
1.86 -
1.87 - pla
1.88 - tay ; stack -> Y
1.89 - pla
1.90 - tax ; stack -> X
1.91 - pla ; stack -> A
1.92 - rts
1.93 -
1.94 -
1.95 -
1.96 -; move CPU stack data from the "user space" stack
1.97 -;
1.98 -; affects: USER (loses LSB, MSB)
1.99 -
1.100 -copy_from_user_stack:
1.101 - pha ; padding
1.102 - pha ; padding
1.103 - pha ; A -> stack
1.104 - txa
1.105 - pha ; X -> stack
1.106 - tsx ; capture the stack pointer now for later use
1.107 - ; (stack is Y, X, A, LSB, MSB)
1.108 - tya
1.109 - pha ; Y -> stack
1.110 -
1.111 - ; save zero-page locations used
1.112 -
1.113 - .invoke push16 SP
1.114 -
1.115 - ; initialise the stack frame pointer
1.116 -
1.117 - txa
1.118 - sta SP
1.119 - lda #$01 ; $01xx
1.120 - sta SPH
1.121 -
1.122 - ; adjust the "user space" stack pointer to be compatible with the PC stack
1.123 -
1.124 - .invoke sub16 3, USER
1.125 -
1.126 - ; copy the PC
1.127 -
1.128 - ldy #3
1.129 - .invoke mov8_refs USER, SP
1.130 - iny
1.131 - .invoke mov8_refs USER, SP
1.132 -
1.133 - ; update the "user space" stack pointer
1.134 -
1.135 - .invoke add16 5, USER
1.136 -
1.137 - ; restore zero-page locations used
1.138 -
1.139 - .invoke pull16 SP
1.140 -
1.141 - ; return to the stored PC
1.142 -
1.143 - pla
1.144 - tay ; stack -> Y
1.145 - pla
1.146 - tax ; stack -> X
1.147 - pla ; stack -> A
1.148 - rts
1.149 -
1.150 -
1.151 -
1.152 ; install the interrupt handler address in IRQ1V
1.153 ;
1.154 ; affects: A
1.155 @@ -342,6 +228,7 @@
1.156 ;
1.157 ; ARG0, ARG0H: location of task structure
1.158 ; affects: A, Y
1.159 +; returns: 0 if successful, 1 if unsuccessful
1.160
1.161 new_task:
1.162
1.163 @@ -374,9 +261,11 @@
1.164 sta tasks, y
1.165 cli
1.166
1.167 + lda #0
1.168 .invoke return
1.169
1.170 no_new_task:
1.171 + lda #1
1.172 .invoke return
1.173
1.174
1.175 @@ -438,24 +327,30 @@
1.176 .word third_task_start ; saved PC
1.177 third_task_start:
1.178 ldx #0
1.179 - ldy #0
1.180 +_begin:
1.181 + ldy #0 ; reset counter LSB
1.182 + lda #0 ; reset counter MSB
1.183 + sta $7011
1.184 _loop:
1.185 + sty $7010
1.186 cpy #$ff
1.187 bne _continue
1.188 - inc $7011
1.189 - lda $7011
1.190 + clc
1.191 + lda $7011 ; next MSB
1.192 + adc #1
1.193 + sta $7011
1.194 cmp #$ff
1.195 bne _continue
1.196 - tya
1.197 + txa
1.198 .invoke pushA
1.199 - txa
1.200 - .invoke call remove_task
1.201 - .invoke pullA
1.202 - tay
1.203 - inx
1.204 + .invoke call remove_task, +
1.205 +* .invoke pullA
1.206 + tax
1.207 + inx ; move to next offset
1.208 + inx ; which is 2 locations away
1.209 + jmp _begin
1.210 _continue:
1.211 iny
1.212 - sty $7010
1.213 jmp _loop
1.214 third_task_stack:
1.215 .word 0
1.216 @@ -463,7 +358,7 @@
1.217 .word 0
1.218 .word 0
1.219 .word 0
1.220 +third_task_stack_base:
1.221 .word 0
1.222 -third_task_stack_base:
1.223
1.224 ; vim: tabstop=4 expandtab shiftwidth=4