|
- ; =============================================================
- ; irq-handler.asm - raster IRQ skeleton for the C64 (ca65)
- ;
- ; Changes the border color at raster lines 100 and 200, on top of the normal
- ; Kernal IRQ (keyboard scan, jiffy clock), and returns to BASIC.
- ;
- ; RUN install (SYS 2061)
- ; SYS 2064 remove
- ;
- ; c64 run examples/irq-handler.asm
- ; c64 test examples/irq-handler.asm -AfterJiffies 300 (see build/test/screenshot.png)
- ;
- ; Timing: line numbers are valid on PAL (312 lines) and NTSC (263 lines).
- ; =============================================================
-
- VIC_CTRL1 = $D011 ; bit 7 = raster compare bit 8
- VIC_RASTER = $D012
- VIC_IRR = $D019 ; interrupt request register (write 1s to acknowledge)
- VIC_IMR = $D01A ; interrupt mask register
- VIC_BORDER = $D020
- CIA1_ICR = $DC0D
- CINV = $0314 ; Kernal IRQ vector (RAM)
- KERNAL_IRQ_EXIT = $EA81 ; pull Y, X, A and RTI (end of the Kernal handler)
-
- .segment "CODE"
- entry: jmp install ; SYS 2061
- jmp remove ; SYS 2064
-
- ; ---------------------------------------------------------------
- ; install: hook CINV and enable the raster interrupt
- ; ---------------------------------------------------------------
- .proc install
- sei ; no IRQs while the vector is half-written
-
- lda CINV ; save the current handler so we can chain to it
- sta oldirq
- lda CINV+1
- sta oldirq+1
-
- lda #<raster_irq
- sta CINV
- lda #>raster_irq
- sta CINV+1
-
- lda VIC_CTRL1
- and #$7F ; raster compare bit 8 = 0
- sta VIC_CTRL1
- lda #100
- sta VIC_RASTER
-
- lda #$01 ; enable the raster IRQ (CIA 1 timer IRQ stays on)
- sta VIC_IMR
- sta VIC_IRR ; clear any stale raster request
-
- cli
- rts
- .endproc
-
- ; ---------------------------------------------------------------
- ; remove: restore the saved vector and disable the raster IRQ
- ; ---------------------------------------------------------------
- .proc remove
- sei
- lda oldirq
- sta CINV
- lda oldirq+1
- sta CINV+1
- lda #$00
- sta VIC_IMR
- lda #$01
- sta VIC_IRR ; acknowledge a raster IRQ that may be pending
- cli
- rts
- .endproc
-
- ; ---------------------------------------------------------------
- ; raster_irq: runs for every IRQ (the Kernal pushed A, X, Y already)
- ; ---------------------------------------------------------------
- .proc raster_irq
- lda VIC_IRR
- and #$01 ; raster interrupt?
- beq not_raster ; no: it is the CIA timer, run the Kernal handler
- sta VIC_IRR ; acknowledge (write 1 to bit 0)
-
- inc coloridx ; next border color
- lda coloridx
- and #$0F
- sta VIC_BORDER
-
- lda VIC_RASTER ; alternate between lines 100 and 200
- cmp #200
- bcs top
- lda #200
- bne set ; always taken
- top: lda #100
- set: sta VIC_RASTER
-
- ; The raster IRQ must not run the Kernal handler: it would advance the
- ; jiffy clock and scan the keyboard an extra time per frame.
- jmp KERNAL_IRQ_EXIT
-
- not_raster:
- jmp (oldirq) ; usually $EA31: keyboard, clock, acknowledges CIA 1
- .endproc
-
- .segment "DATA"
- oldirq: .word $EA31
- coloridx: .byte 0
|