# HG changeset patch # User Paul Boddie # Date 1435428100 -7200 # Node ID d7d91332b0aa73a7cffe57cfeeef04559042f13f # Parent 4ffa83fa5fe119443ed7db2c4b57771ed2f1c6f9 Added the new_task subroutine, renamed task_index to task_offset, added aliases. diff -r 4ffa83fa5fe1 -r d7d91332b0aa switcher.oph --- a/switcher.oph Sat Jun 27 19:21:03 2015 +0200 +++ b/switcher.oph Sat Jun 27 20:01:40 2015 +0200 @@ -17,6 +17,8 @@ .include "macros.oph" +.alias IRQ1V $204 + .alias SP $70 .alias SPH $71 .alias NEXT $72 @@ -24,24 +26,34 @@ .alias CURRENT $74 .alias CURRENTH $75 +.alias TASK_TABLE_LENGTH 10 + .org $2000 .text main: jsr install_handler + .invoke store16 first_task, $76 + jsr new_task + .invoke store16 second_task, $76 + jsr new_task test: jmp test + + ; install the interrupt handler address in IRQ1V ; ; affects: A install_handler: sei - .invoke mov16 $204, old_handler - .invoke store16 handler, $204 + .invoke mov16 IRQ1V, old_handler + .invoke store16 handler, IRQ1V cli rts + + ; handle interrupts ; ; affects: (temporary stack usage) @@ -91,7 +103,7 @@ ; load next task - ldx task_index + ldx task_offset lda tasks, x sta NEXT inx @@ -101,12 +113,12 @@ ; reset the task index if necessary - cpx #10 ; length of tasks array + cpx #TASK_TABLE_LENGTH bne test_task ldx #0 test_task: - stx task_index + stx task_offset ; exit if null task @@ -161,6 +173,8 @@ pla ; stack -> A jmp (old_handler) + + ; location of previous interrupt handler old_handler: .word 0 @@ -169,19 +183,62 @@ current_task: .word 0 -; index of current task +; offset of current task in table (in multiples of 2) -task_index: .byte 0 +task_offset: .byte 0 ; task table containing locations of each task tasks: - .word first_task - .word second_task + .word 0 + .word 0 .word 0 .word 0 .word 0 + + +; add a new task to the table +; +; $76, $77: location of task structure +; affects: A, Y +; returns: A = 0 (success); A = 1 (failure) + +new_task: + + ; check the MSB of each task table entry + + ldy #1 + lda tasks, y + cmp #0 + beq add_new_task + iny + iny + + ; if no space is found by the end of the table, exit + + cpy #TASK_TABLE_LENGTH + bpl no_new_task + +add_new_task: + + ; copy the task structure location to the table + + lda $77 + sta tasks, y + dey + lda $76 + sta tasks, y + + lda #0 + rts + +no_new_task: + lda #1 + rts + + + ; example tasks first_task: @@ -194,6 +251,8 @@ .invoke add16 1, $7000 jmp first_task_start + + second_task: .byte 0 ; currently unused .byte 0 ; currently unused