IRQ0
        LDA #RASTER+4   ;                                                               2
        STA $D012       ;                                                               4
        JSR PRIMEREU    ;                                                             130
        LDA #$35        ;                                                               2
        STA $01         ;                                                               3
        LDA #<IRQ1      ;                                                               2
        STA $FFFE       ;                                                               4
        LDA #>IRQ1      ;                                                               2
        STA $FFFF       ;                                                               4
        DEC $D019       ;                                                               6 (-2 jitter)
        TSX             ;                                                               2
        LDA #$33        ;  safe to open the border now                                  2
        STA $D011
        CLI             ;                                                               2
        !fill 64, $EA   ;  lots of NOPs
-       JMP -           ;  limit jitter to 2 cycles.


IRQ1                    ; We are at the second last line before the lower border
        TXS             ; restore the stack pointer                                     2
        LDA #14         ;                                                               2
        STA $D020       ;                                                               4
        LDX #4          ;                                                      2
-       DEX             ;                                                      2
        BNE -           ;                             
LINECNT LDX #48         ;                                                               2
        LDY #RCMDrecv   ;                                                               2
        LDA #$13        ; Turn off graphics mode as we enter the right border           2
        INC DUMMY       ;                                                               6
        STA DUMMY       ;                                                               4
        STA $D011       ; last cycle may overlap with BA going low, which takes         4
                        ; care of the 1 cycle jitter left over from the NOPs we
                        ; were executing while the interupt occured
                        ; This is the last possible moment for switching to character
                        ; mode before entering the DMA loop, and the first possible
                        ; moment where we can switch mode without side effects in the
                        ; visible area of the display.

;------------------------------------------------------------------------------------------------------------------
;
;                       R A S T E R B E A M  L O C K E D  C O D E
;
;------------------------------------------------------------------------------------------------------------------

                        ; We are now locked to the raster beam. Our timing is 12 cycles
                        ; before the start of the display area.

        sta DUMMY       ;                                                               4


                        ; Code below must have a fixed execution time, 63 cycles per display line. 
                        ; 23 cycles of code in a loop, 40 cycles of dma
REULOOP
        lda #40         ;                                                               2
        sta RECbase+7   ;                                                               4
        STY RECbase+1   ;                                                               4
                        ; 40 bytes DMA from REU memory to VICNDREAD                    40
.reucol LDA COLTAB3, x  ;                                                               4
        STA $D021       ;                                                               4
        dex             ;                                                               2
        bne REULOOP     ;                                                               3/2
                        ;                                                            ----+
                        ;                                                              63/62 (exit)

                        ; Timing is 13 cycles before start of the display area

;------------------------------------------------------------------------------------------------------------------
;
;                       E N D  O F  R A S T E R B E A M  L O C K E D  C O D E
;
;------------------------------------------------------------------------------------------------------------------
; Non time critical remainder.

        LDA BGCOLOR
        STA $D021
        LDA #$00
        STA VICNDREAD   ; Clear the graphics
        LDA BDRCOLOR
        STA $D020
        LDA #$7F
        STA $D011       ; Normal screen (be ready to open the border again)
        LDA #$01
        STA $D019

        LDA #<IRQ2
        STA $FFFE
        LDA #>IRQ2
        STA $FFFF
        LDA #$02
        STA $D012
        TSX
        CLI
        !fill 256, $EA

