processor 6502 SPRITE0IDX = 0 SPRITE1IDX = 1 ;SPRITE0IDX = 4 ;SPRITE1IDX = 5 ; 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 numtests = 6 ;numtests = 39 cinv = $314 cnmi = $318 raster = 48 ; start of raster interrupt m = $fb ; zero page variable .org $801 basic: .word 0$ ; link to next line .word 1995 ; line number .byte $9E ; SYS token ; SYS digits .if (* + 8) / 10000 .byte $30 + (* + 8) / 10000 .endif .if (* + 7) / 1000 .byte $30 + (* + 7) % 10000 / 1000 .endif .if (* + 6) / 100 .byte $30 + (* + 6) % 1000 / 100 .endif .if (* + 5) / 10 .byte $30 + (* + 5) % 100 / 10 .endif .byte $30 + (* + 4) % 10 0$: .byte 0,0,0 ; end of BASIC program 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 jsr setupsprites ldx #1 stx $d01a ; enable raster interrupt 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 ; 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 #endif #else bit $24 ; 6569, 63 cycles/line #endif 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 lda $d01e ldx #$0f startloop: dex bne startloop lda $d01e inc $d021 dec $d021 result: sta $0428 lda result+1 cmp #$28+numtests beq endirq inc $d000+SPRITE0IDX * 2 inc $d000+SPRITE1IDX * 2 inc result+1 jmp endirq endirq: ; sec ; sbc #$28 tax lda refpal-$28,x sta $0450-$28,x lda #5 sta $d828-$28,x sta $d850-$28,x lda $0428-$28,x cmp $0450-$28,x beq nofail lda #10 sta $d828-$28,x sta $d850-$28,x inc fails nofail: cpx #$28+numtests bne notlast ldy #5 lda fails beq allok ldy #10 allok: sty $d020 lda $d020 and #$0f ldx #$ff ; failure cmp #5 ; green bne fail2 ldx #0 ; success fail2: stx $d7ff notlast: 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 setupsprites: lda #$34 sta $d001+SPRITE0IDX * 2 sta $d001+SPRITE1IDX * 2 lda #$56 ;+8 sta $d000+SPRITE0IDX * 2 sta $d000+SPRITE1IDX * 2 lda #(1 << SPRITE0IDX) + (1 << SPRITE1IDX) sta $d015 lda #$3f sta $07f8+SPRITE0IDX sta $07f8+SPRITE1IDX lda #$00 ldx #$3f loopspritepattern: sta $0fc0,x dex bne loopspritepattern lda #$80 sta $0fc0 lda #0 sta fails sta $d020 sta $d021 lda #1 sta $d027+SPRITE0IDX lda #3 sta $d027+SPRITE1IDX ; clear screen ldx #0 lda #$20 clp: sta $0400,x sta $0500,x sta $0600,x sta $06e8,x inx bne clp rts fails: .byte 0 refpal: .byte 3,3,0,0,0,0,0