User Tools

Site Tools


base:launching_long_tasks_from_irq_handler

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
base:launching_long_tasks_from_irq_handler [2016-03-15 12:27] bitbreakerbase:launching_long_tasks_from_irq_handler [2016-11-23 10:54] (current) bitbreaker
Line 1: Line 1:
 ====== Launching long tasks from inside a IRQ handler ====== ====== Launching long tasks from inside a IRQ handler ======
 +
 +by Bitbreaker/Oxyron/*
  
 When executing code within an IRQ handler you have to finish things before the next IRQ occurs. But sometimes tasks just take some more time, for that you can spin off those tasks from inside the handler, and allow then upcoming IRQs to happen. When executing code within an IRQ handler you have to finish things before the next IRQ occurs. But sometimes tasks just take some more time, for that you can spin off those tasks from inside the handler, and allow then upcoming IRQs to happen.
Line 15: Line 17:
  
        ;... your desired irq handler code goes here        ;... your desired irq handler code goes here
 +       lda some_condition
        beq skip_long        beq skip_long
                
Line 26: Line 29:
        pha        pha
                
-       ;copy over the registers +skip_long
-       lda reg_a +
-       sta reg_a_ +
-       lda reg_x +
-       sta reg_x_ +
-       lda reg_y +
-       sta reg_y_ +
-       +
        ;here, further code can happen that has nothing to do with our task        ;here, further code can happen that has nothing to do with our task
-skip_long+
        ;restore registers        ;restore registers
        lda reg_a        lda reg_a
Line 45: Line 41:
          
 task task
 +       ;store registers again
 +       sta reg_a_
 +       stx reg_x_
 +       sty reg_y_
 +
        ;...some long long code        ;...some long long code
          
-       ;restore the copied over registers+       ;restore registers
        lda reg_a_        lda reg_a_
        ldx reg_x_        ldx reg_x_
Line 75: Line 76:
         sta $d012         sta $d012
                  
-        ;now allow irq2 happen on top of this task and return to this task when done+        ;now allow irq2 to happen on top of this task and return to this task when done
         cli         cli
                  
Line 109: Line 110:
         rti         rti
 </code> </code>
 +
 +A even more convenient variant is to use the stack for register saving:
 +
 +<code>
 +irq1
 +        pha
 +        txa
 +        pha
 +        tya
 +        pha
 +        dec $d019
 +        
 +        dec counter
 +        bne +
 +        
 +        lda #$08
 +        sta counter
 +        cli
 +        jsr long_task
 ++
 +        pla
 +        tay
 +        pla
 +        tax
 +        pla
 +        rti
 +</code>
 +
 +Now you can run a long task that can finish somewhen between the next 8 interrupts.
base/launching_long_tasks_from_irq_handler.1458041264.txt.gz · Last modified: 2016-03-15 12:27 by bitbreaker