Occasional ramblings on games, generally retro related

​When my parents brought home a ZX81 one day (complete with wobbly 16K RAM pack, of course) I discovered the joy of programming. But it wasn't until I got my hands on a ZX Spectrum that my obsession with games really began, which continued with the C64, Amiga, right through to this day. The 80s and early 90s were an amazing time for games, not just for the games themselves but for the fascinating people behind them - it was truly a time of pioneers and creativity.

I myself have spent the last (almost) 20 years working in the games industry on all manner of platforms, most recently iOS. Ziggurat Development Ltd is my company here in NZ that provides contract programming services.

Filtering by Tag: c64

Diary of a Game: Part 4 - Item Activation, Interpolation and... Bugs

Since my last update work has been a bit quieter post-ship and I enjoyed an unexpectedly low key Christmas break, so surely I've made a lot of progress, right? Well... life decided to intervene which resulted in a lot less time to work on the game than I had hoped. But I have managed to tick off a few things on the long to-do list...

Item Activation

The main - and, I must admit, only - playing facing addition is items that need to be activated before they can be matched. Activation occurs when a neighbour - regardless of colour - is matched and removed. I've reserved the 4th bit to indicate whether an item requires activation, which means I have 7 possible item types. So far I'm only using 4, so I think this will be enough. However, I should be able to take the 5th bit to give 15 item types (each with an unactivated variant) if need be.

The item bits at present are:

.const ITEM_TYPE_BITS = %00001111
.const ITEM_MOVING = %00100000
.const ITEM_DONT_DRAW = %01000000

.enum {






Up until a couple of months ago, the code to handle the sprite movement when the player fires an item was specific to that use case. Rather than having items jump immediately to their new positions when neighbours are removed, I want to utilise this sprite iterpolation to make it nice and smooth. So the first step in the process was to refactor the code into something more generic.

The resulting routines are super simplistic - the interpolation speeds are specified as pixels per update (whole pixels only), and the X and Y coordinates are interpolated at the same speed, which doesn't look great under some circumstances. I may revisit this in the future.

The following clip shows this code in action:

    :test_push_lerp(0, 0, 100, 200, 1)
    :test_push_lerp(200, 0, 50, 200, 2)
    :test_push_lerp(20, 0, 70, 200, 3)
    :test_push_lerp(60, 0, 40, 200, 4)
    :test_push_lerp(90, 0, 120, 200, 5)
    :test_push_lerp(0, 0, 100, 200, 1)
    :test_push_lerp(200, 0, 50, 200, 2)
    :test_push_lerp(20, 0, 70, 200, 3)
    :test_push_lerp(60, 0, 40, 200, 4)
    :test_push_lerp(90, 0, 120, 200, 5)

    :start_frame_update(1, !profiling)
    jsr item_lerper.tick

where the test_push_lerp parameters are the start X & Y, target X & Y and colour.


While writing the activation and interpolation code, I ran into two separate bugs that had me scatching my head for an embarassingly long time.

The first occurred when matched items were being removed - occasionally the bottom left item would disappear when it shouldn't. I suspected this had something to do with the new code which finds any neighbours of an item being removed and activates them. The 64 Debugger was a huge help in tracking the culprit down, especially since it recently added the ability to read KickAssembler debug symbols. This is brilliant as it gives you your full source code along side the instructions:

Sure enough the bug was in the routine to find occupied neighbours - if there were no neighbours, I was neglecting to set the count of neighbours found to zero. This meant it would try to activate whatever items happened to be lying around in a buffer.

The second bug was considerably harder to find...

The Perils of Macros

KickAssembler has a rich scripting language to assist with creating code and data. For my first pass on the interpolation routines, I relied heavily on the scripting - particularly macros and pseudocommands. This made things like operating over a buffer of data structures very straightforward, for example:

.for (var i = 0; i < lerping_items_count; i++) {
    lda lerping_items[i].active
    beq !skip+

    :lerp(lerping_items[i].current_x, lerping_items[i].target_x, ITEM_DROP_SPEED, lerping_items[i].active)
    :lerp(lerping_items[i].current_y, lerping_items[i].target_y, ITEM_DROP_SPEED, lerping_items[i].active)


But then I ran into a bug where some interpolating items would just disappear when a certain number had been queued. What made this particularly difficult to debug was the reliance on macros and pseudocommands. Because I was calling several macros, which in turn called other macros and pseudocommands, and all of this was wrapped in a for loop, the amount of actual code output was huge. I found stepping through all this code tedious and confusing - particularly since the issue only appeared towards the end of the interpolating items buffer.

The cause turned out to be a classic mixup between an address and the size of a buffer. In the routine to find a free interpolating item to use, I loop over all the items in the buffer to see if there is one that isn't active:

    ldx #0
    lda lerping_items,X
    beq !found_free+

    adc #lerping_item_size()
    cmp #lerping_items_end  // <---- d'oh
    bcs !none_free+

    jmp !loop-
    ldx #255

lerping_items_end happens to be the memory address immediately after the lerping items buffer. What I wanted, of course, was to compare the current offset to the size of the buffer.

KickAssembler 5

As well as the ability to output full symbols/source for use in the C64 Debugger, there are a number of other nice improvements in KickAssembler 5. However, the changes to escape characters in strings broke the useful unit test framework 64spec. I've submitted a pull request that fixes these errors.

Diary of a Game: Part 2 - Matching and Testing

Once again a depressing amount of time has passed since my last update. Given what's on my plate workwise over the next few months I suspect the sporadic nature of my posts will continue. Thankfully I have continued to find time to chip away at the game, and some progress has been made.

After I managed to get a super rough first pass of the (very) initial base of the game done (with the balls/bubbles/whatever falling down the board and stacking up correctly), the next thing to tackle was the colour matching. This ended up being a rather humbling endeavour as my initial confidence met the realities of my inexperience with 6502 coding.

The first step was get the connecting neighbours for each any given cell on the board, which was straightforward enough.

Finding the neighbours of each cell.

Finding the neighbours of each cell.

With a simple routine to return a list of neighbours for a given cell, I could move on to figuring out all of the matching connected items. This is where things got a little complicated. The board on which the items appear can be viewed as a graph/network of connected nodes. Most nodes have up to 6 neighbours, whereas those on the edges & corners of the board have up to 2, 3 or 4 depending on their position. Chains of connected items can potentially use every cell on the board. So I decided to use a basic graph/network search, which looks something like ...
For a given item:

  1. Push the item onto the "open" stack (items that need to be visited)
  2. While the open stack has items on it:
  3. Pop an item
  4. Mark it as matched & visited
  5. Get a list of matching neighbours
  6. Iterate through each neighbour, and if they haven't been visited then push them onto the open stack.
  7. Go to step 2

While this is the kind of thing I've done many times in more fully featured languages, I really struggled to get my head around doing it with 6502. The fact that I didn't have decent chunks of time to sit down and focus on it didn't help. I eventually got there, though, and the moment where it finally worked was one of the most satisfying achievements for me in recent times, which is somewhat amusing.

Testing the matching chains of items.

Testing the matching chains of items.

What this all highlighted for me was that I really needed to spend some time improving my testing and debugging process. The C64Debugger was useful, though I just can not seem to get it to load symbols - I keep meaning to download the source and have a poke around. I had never really explored debugging in VICE, but I found this blog post a great introduction. Once I realised I could load symbols, VICE was a huge help (I had previously been manually printing out addresses at compile time in KickAssembler).

But as I went back to refactor and optimise routines I found myself getting pretty frustrated at subtle bugs creeping in which became difficult to track down. Thankfully there is a nice unit test solution for the C64. Yes, unit testing on the C64. Michael Taszycki of the excellent 64Bites video series has created the 64spec framework, which allows you to setup unit tests and run them on the 64 (or in an emulator). This now allows me to make changes and have some confidence that if I break anything I'll be able to catch it early.

One of unit tests. I have multiple files for each major component.

One of unit tests. I have multiple files for each major component.

Oh dear, something went wrong somewhere. Time to debug.

Oh dear, something went wrong somewhere. Time to debug.

And so now things are at the point where balls/bubbles/whatever fall down, stack up and get removed when chains of 3+ matching items are found. Next on my plate is some more refactoring and optimisation, then I need to tackle using sprites for the moving items so they look a little less crap.