Time to start getting familiar with using the zero page and use an interrupt for some simple effects. I decided to create a basic colour cycle effect that updates the colour map, shifting the character colours in a direction. Back in the day, I couldn’t get my head around how to actually achieve this via assembly language, but I’ve had a lot of experience in programming since then, so have a much better idea of how to achieve the effect.
First up, I’ll need to do some initialisation. Setting the screen and border colours to black should make the effect stand out better and this is done with a simple store to $d020 and $d021. I wanted to clear the screen also and make use of the clear screen kernal routine located at $e544.
Since the effect is going to modify the character colours in the screen character colour map, I want to get some text on screen. I first added a series of bytes that represent character screen codes to the tail of the program, and then added a short loop to read each character and store it across the top line of the screen. Starting at $0400 (1024), it simply stores each character into screen memory.
For this routine, I wanted to make use of interrupts. It’s possible to run it without, but it should be a nice simple routine to practice with. The next section of code points the interrupt vector at $0314 and $0315 (low/high) to my colour cycle routine. This way it will be called every 1/60 of a second. This will naturally be too fast for the effect to look nice, but I can slow it down easily enough.
Now it was time to get the actual routine together that will perform the cycle effect. To start with, the first thing the routine will do is execute a delay. So although this routine is called every 1/60 of a second, it will skip the cycling code until the counter hits 0. This way, the interrupt doesn’t hold up the rest of the system while waiting for the delay to expire and then continuing. It will only update the colour cycle every 1/20. Once the delay has expired, then we can get down to updating the colours on the top row of the screen.
The update begins modifying the colour map at the far right of the row (column 40 – or 39 since it’s an offset and zero based). It loads the starting colour for this update from “startcol” into the x register and then uses this to index the colours table for the next colour. Then each loop it increments the colour table index, looping back to 0 when it reaches 8. The use of 8 colours here was deliberate so we can easily loop once at the end of the colour table. Once all character colours in the colour map for that row have been updated, it updates the starting colour index for the next cycle and returns from the interrupt.
At the bottom of the routine are block of data for the display text and colour table. The use of zero page in this routine isn’t mandatory, but I thought since I had not yet used it in previous examples, I would do so here. Using the Zero page saves some cycles when accessing it, although there are only certain locations that are available. When I come to optimise things at a later date, I will make much greater use of the zero page.
Building and running the code gives me a nice, smooth colour scroll on the characters. Putting this effect together turned out to be much easier than I was expecting. It will be simple enough to modify it to scroll the colours in the opposite direction, or add some additional colour effects to (so I may revisit this at a later date to build on).
The code is available on github here.