You can respond to changes in $d012
by using a raster IRQ handler. I'll throw in a couple of specifics from my game code because it can be hairy to get this to work if you use the wrong combination of incantations. This should also give you enough stuff to google.
In particular, you might want to look at installing your int handler in $0314
like the code mentions, in which case your IRQ handler will be chained with the commie's own default handlers, and you need to skip the pha ... pha
bit at the start of your handler. This can be useful if you need to use some of its I/O code.
;;; -----------------------------------------------------------------------------
;;; install raster interrupt handler
;;; -----------------------------------------------------------------------------
sei ; turn off interrupts
ldx #1 ; enable raster interrupts
stx $d01a
lda #<int_handler ; set raster interrupt vector
ldx #>int_handler
sta $fffe
stx $ffff
ldy #$f0 ; set scanline on which to trigger interrupt
sty $d012
lda $d011 ; scanline hi bit
and #%01111111
sta $d011
lda #$35 ; disable kernal and BASIC memory ($e000 - $ffff)
sta $01
asl $d019 ; acknowledge VIC interrupts
cli
loop_pro_semper
jmp loop_pro_semper
Then you handle these interrupts like this:
;;; -----------------------------------------------------------------------------
;;; raster IRQ handler
;;; -----------------------------------------------------------------------------
int_handler
pha ; needed if our raster int handler is set in fffe instead of 0314
txa
pha
tya
pha
; ... do your stuff here ...