DEBUG=0 !if DEBUG=1 { DBGCOLOR0=$d020 DBGCOLOR1=$d021 } else { DBGCOLOR0=$dbff DBGCOLOR1=$dbff } ; Select the video timing (processor clock cycles per raster line) ;CYCLES = 65 ; 6567R8 and above, NTSC-M ;CYCLES = 64 ; 6567R5 6A, NTSC-M ;CYCLES = 63 ; 6569 (all revisions), PAL-B cinv = $314 cnmi = $318 raster = 46 ; start of raster interrupt m = $fb ; zero page variable * = $0801 !byte $0b,$08,$01,$00,$9e !convtab pet !tx "2061" !byte $00,$00,$00 jmp start start: jmp install jmp deinstall install: ; install the raster routine jsr restore ; Disable the Restore key (disable NMI interrupts) checkirq: lda cinv ; check the original IRQ vector ldx cinv+1 ; (to avoid multiple installation) cmp #irq1 beq skipinit irqinit: sei sta oldirq ; store the old IRQ vector stx oldirq+1 lda #irq1 sta cinv ; set the new interrupt vector stx cinv+1 skipinit: lda #$1b sta $d011 ; set the raster interrupt location lda #raster sta $d012 lda #$7f sta $dc0d ; disable timer interrupts sta $dd0d ldx #1 stx $d01a ; enable raster interrupt ldx #0 lp1a: lda #1 sta $d800,x sta $d900,x sta $da00,x sta $db00,x lda #$20 sta $0400,x sta $0500,x sta $0600,x sta $0700,x inx bne lp1a ldx #$27 lp1: lda #$01 sta $0400,x lda #$02 sta $0428,x lda #$03 sta $0450,x dex bpl lp1 lda $d011 bpl *-3 lda $dc0d ; acknowledge CIA interrupts lsr $d019 ; and video interrupts cli jmp * deinstall: sei ; disable interrupts lda #$1b sta $d011 ; restore text screen mode lda #$81 sta $dc0d ; enable Timer A interrupts on CIA 1 lda #0 sta $d01a ; disable video interrupts lda oldirq sta cinv ; restore old IRQ vector lda oldirq+1 sta cinv+1 bit $dd0d ; re-enable NMI interrupts cli rts !align 255,0,0 ; Auxiliary raster interrupt (for syncronization) irq1: ; irq (event) ; > 7 + at least 2 cycles of last instruction (9 to 16 total) ; pha ; 3 ; txa ; 2 ; pha ; 3 ; tya ; 2 ; pha ; 3 ; tsx ; 2 ; lda $0104,x ; 4 ; and #xx ; 2 ; beq ; 3 ; jmp ($314) ; 5 ; --- ; 38 to 45 cycles delay at this stage lda #irq2 sta cinv+1 nop ; waste at least 12 cycles nop ; (up to 64 cycles delay allowed here) nop nop nop nop inc $d012 ; At this stage, $d012 has already been incremented by one. lda #1 sta $d019 ; acknowledge the first raster interrupt cli ; enable interrupts (the second interrupt can now occur) ldy #9 dey bne *-1 ; delay nop ; The second interrupt will occur while executing these nop ; two-cycle instructions. nop nop nop oldirq = * + 1 ; Placeholder for self-modifying code jmp * ; Return to the original interrupt ; Main raster interrupt irq2: ; irq (event) ; 7 + 2 or 3 cycles of last instruction (9 or 10 total) ; pha ; 3 ; txa ; 2 ; pha ; 3 ; tya ; 2 ; pha ; 3 ; tsx ; 2 ; lda $0104,x ; 4 ; and #xx ; 2 ; beq ; 3 ; jmp (cinv) ; 5 ; --- ; 38 or 39 cycles delay at this stage lda #irq1 sta cinv+1 ldx $d012 nop !if CYCLES - 63 { !if CYCLES - 64 { nop ; 6567R8, 65 cycles/line bit $24 } else { nop ; 6567R56A, 64 cycles/line nop } } else { bit $24 ; 6569, 63 cycles/line } cpx $d012 ; The comparison cycle is executed CYCLES or CYCLES+1 cycles ; after the interrupt has occurred. beq *+2 ; Delay by one cycle if $d012 hadn't changed. ; Now exactly CYCLES+3 cycles have passed since the interrupt. dex dex stx $d012 ; restore original raster interrupt position ldx #1 stx $d019 ; acknowledge the raster interrupt ; start action here !if CYCLES=63 { ldx #83-3 } !if CYCLES=65 { ldx #83 } preloop: dex bne preloop bit $21 !if CYCLES=63 { bit $21 } !if CYCLES=65 { nop } nop nop inc DBGCOLOR1 ;6 dec DBGCOLOR1 ;6 lda $d011 tay and #$f8 sta $d011 sty $d011 !if CYCLES=63 { ldx #80-3 } !if CYCLES=65 { ldx #80 } loop2: dex bne loop2 nop nop nop bit $21 inc DBGCOLOR0 ;6 dec DBGCOLOR0 ;6 tax stx $d011 sty $d011 lda #80 postloop: cmp $d012 bne postloop jmp endirq endirq: lda $d011 bpl *-3 lda #0 ; success sta $d7ff jmp $ea81 ; return to the auxiliary raster interrupt restore: ; disable the Restore key lda cnmi ldy cnmi+1 pha lda #nmi sta cnmi+1 ldx #$81 stx $dd0d ; Enable CIA 2 Timer A interrupt ldx #0 stx $dd05 inx stx $dd04 ; Prepare Timer A to count from 1 to 0. ldx #$dd stx $dd0e ; Cause an interrupt. nmi = * + 1 lda #$40 ; RTI placeholder pla sta cnmi sty cnmi+1 ; restore original NMI vector (although it won't be used) rts