Wednesday, 14 July 2010

Interrupted Interrupts

Having got the character translation routine finished, I have now written a short routine that executes when the game starts and which processes all the embedded strings, converting them to Screen Codes in-place. It bears more than a passing resemblance to CHARPLOT itself, since it processes the actual structures that are defined and given to the drawing routine - so it has to be able to interpret the Command Byte to isolate the Repeat/Length bit and figure-out where the characters start and how many there are to convert. Instead of drawing the characters, it just calls the translation routine and saves the result back to the structure - so it's a very simple cut-down variant of CHARPLOT itself, with the drawing and colour logic chopped out.

Having returned from these little diversions, I can now continue with the main task of converting logic from the  games' BASIC sourcecode into Assembly. But before progressing to the next stage of the process, I just needed a quick dive into the Kernal to see where the IRQ routine goes to read the keyboard - the title screen and main game itself both need to be aware of keypresses, but the program runs with interrupts disabled (for maximum speed) and therefore the keyboard-handler routine isn't automatically called. The IRQ routine at $EABF calls a few different routines needed to keep the machine ticking-over, and one of them is SCNKEY at $EB1E - this processes signals from VIA1 which is continually scanning the keyboard, and converts the results into characters in the keyboard buffer. 

The IRQ handler calls SCNKEY (and all the other housekeeping routines) 60 times a second, but I don't need all those other bits of code running - so Meanies executes an SEI as its' very first instruction to disable the IRQ flag in the CPU and stop interrupts occurring. Consequently, it becomes the games' responsibility to call SCNKEY at appropriate times and intervals to have keypresses processed and made available. So, for example, the title screen waits for a keypress using the following simple loop:

         ; Wait for keypress to start game
.keywait jsr SCNKEY       ; Scan keyboard
         lda KEYCOUNT     ; Get count of keypresses
         beq .keywait     ; Loop until any key is pressed

Of course, this is a spinlock - nothing else (except for NMIs) can happen whilst this loop is executing, so it's not the technique I'll be using in the main gameplay section. But it demonstrates the principle.

I now also have a short bit of code which initialises a set of gameplay variables like Energy, Score, and so on. This includes a few CHARPLOT structures for things like the Repair Bot and Meanies - you'll recall that I have CHARPLOT running at around 50 frames per second, so it's plenty fast enough to draw all the 'mobile' entities in the game.

The next thing to do is finalise the drawing of the main playfield elements, and then we'll be into the main loop of the game where Stuff Happens. :)

0 comments:

Post a Comment