I finished writing code for version 2.1 of the demo game Shooting Stars. As one probably noticed, the main loop in version 2.0 looked quite ugly, as I had to query the keyboard buffer often to make the game responsive. In version 2.1 I removed all this code and replaced it with an elegant alternative: everytime the horizontal raster line is 10, the system creates a maskable interrupt (IRQ) and jumps to routine „rasterirq“.
To make this happen, we have to tell the processor where our interrupt service routine (ISR) is:
lda #<rasterirq
sta $0314 ; RAM IRQ vector
lda #>rasterirq
sta $0315 ; high byte
„rasterirq“ is the address where the ISR starts, in our case $3000:
*= $3000
rasterirq
...
Before we jump to this address, we have to tell the processor that its at line 10 we want to trigger the IRQ (label „raster“ equals to the current raster line address $d012):
lda #10 ; trigger if rast=10
sta raster
And we have to enable the raster IRQ by setting bit 0 in the interrupt mask register:
lda $d01a
ora #%00000001
sta $d01a
When we initialize the IRQ, we disable all interrupts using the command „sei“, afterwards we enable them again with „cli“.
Now, once every 60 seconds, the processor interrupts our game, jumps to the ISR at address $3000 and we can query the keyboard buffer (label „read_keyb“). But first we have to make sure that it was indeed a raster IRQ that triggered:
lda #%00000001
bit intflag
bmi read_keyb
If bit 0 of register $d019 (intflag) is set, when we have a raster IRQ. If we interrupted for a different reason, we jump to the standard IRQ routine of the kernel at $ea31 instead.
We end the ISR with command „rti“ and go right back to the game.
As always, the code, the binary and some explanations can be found at https://github.com/jklingel/ShootingStars/releases/tag/2.1.