Having got a working version of the levels displaying, i needed to a) speed the drawing up and b) sort out the code to a more usable format.
Sorting the code was mainly due to the fact i had thrown things together to test if it could all work. Everything was put into a single source file and there was no nice formatting to it all. Having code look and read nice maybe a little bit down to OCD but over the years i have learnt that well presented code is not a bad thing.
Before i reformatted the code I hit the facebook group for the Sam to see if anyone had some ideas on speeding up the drawing. I had some ideas but wanted to see if I was missing anything. Simon Owen gave me a nice idea about using a lookup table for the mask data. The problem with having no mask is that i had to check each nibble to decided if it was using colour index 0 and from that decide whether to use the screen data or the graphic data. Using a lookup table greatly reduced the complexity for this. Aligning the table on a 256 byte boundary meant i could keep the high byte of a register pair on the table and just read the graphic data for the pixels into the low byte. A quick read and i could mask the screen data and or the graphic data before writing it all back to the screen. Presto, simple, compact and quicker.
I finished off the optimisations by changing the data step at the end of each line to restore the starting position and just adding 128 to the screen and graphics width to the graphic data. This simplified various areas of the clipping and allowed me to change the screen line step from an ‘inc de’ to and ‘inc e’. Its only a few t-states but it all helps. Lastly i unrolled the draw routine, repeating it 128 times and jumping to the correct version to draw the size i needed. This is a little wasteful of memory as it needed 1k of ram to store the 128 versions of the draw pixels code, but it does save a load of time checking and looping for each pixel.
I was finally happy enough for now with the drawing code, it felt alot faster, although some of the screen transitions were still a little slow, i may have to address that later.
Time to update the code base…
Before i went much further i wanted to set the code base out ready for the full game. Rather than having code hacked around i wanted things to be setup properly. I split the code up into separate files, put in some simple double buffering etc. The main change was to put my game code into a page which was placed at memory location A – address 0. I have always loved being able to do this on the Sam coupe. It allows you to use various features, such as using the RST commands to call frequently used functions, it also allows you to use the IM1 interrupt address (&0038) as your main interrupt handler. I moved my code and updated all the paging to work from this address and was finally ready to start with the rest of the game…