2018-06-06

Chuckie Egg GBA

Here’s a video of something old-but-new I’ve been working on:

It’s Chuckie Egg, but for the GBA.

How did I manage to squeeze the bitmap-mode UI library built for a dual-CPU 66/16MHz DS into a single-CPU 16MHz GBA? With full-screen scrolling?

I didn’t even try.

Extracting A Library

I mentioned before that the games I’ve been writing with SZLib adhere reasonably strictly to the MVC architecture. The game logic forms the “model” portion of that architecture. If done correctly, the game logic could live in an entirely separate library from the code that handles graphics, sound and user input. That’s what I’ve done to get this working: I moved the model to a new “SZChuckie” library and created a new front-end project that uses the GBA’s tile-and-sprite graphics modes.

Splitting out the game library was mostly trivial, but somehow took far longer than I expected. References to bitmaps, sounds and user input handling had all found their way into the game logic. For example, each of the sprite-esque things in the game (eggs, chicken feed, hens, ducks, Harry) stored an array of bitmaps that represented their animations. The view controller could ask for the current bitmap in order to display the correct image in a view. I had to replace the bitmap reference with a more abstract “animation frame” object, which ditched the bitmap data and stored just a size and a frame number. The view controller now stores the bitmaps and uses SZLib’s type introspection to figure out which bitmap to display for any incoming didChangeFrame events emitted by the game object. The class that represents a level had a function for generating a bitmap from its level data which the view controller used as the game’s background; I moved that into a separate file.

Pulling the sounds into a higher layer meant adding more callbacks to the game object’s delegate. The delegate - in this case, the view controller - can respond appropriately and play the correct sounds.

Input was similarly straightforward. I added a new d-pad/button class specific to Chuckie Egg (so there’s a “jump” button instead of “A” and “B” buttons) and pass its state to the game whenever I call its update() function. As a side-effect, this fixed a few places where the game wasn’t responding correctly to some button presses or the analog stick.

The most long-winded changes were purely administrative: renaming all of the files/classes/etc; creating makefiles and build scripts; and testing that nothing was broken.

With all that done I could move onto the GBA port, which of course started with more administrative work. I needed to get three libraries from SZLib building for the GBA: the core library, which includes reference-counted objects, boxed primitives and collections; the geometry library, which includes points, sizes and rectangles; and the hardware abstraction library, which includes abstractions for initializing the hardware, sound, vwaits and hardware division. Those all needed makefiles. I used the makefile from libgba as a template. With the makefiles in place I could add GBA support to the hardware abstraction library, which involved digging through libgba’s headers to find the appropriate function calls (where available) or disabling the functionality (where not). The core and geometry libraries just worked.

That all put the project in a very weird place. Typically when I start a game I get enough of the logic working to start pushing pixels around on the screen, and from there the logic and graphics code evolve in parallel. Somewhere near the end of the project I’ll add sound. However, in this project I just had to link the game library and hook up a few callbacks to get a fully-working and audible, but invisible, version of Chuckie Egg.

The GBA

Programming the GBA isn’t particularly difficult. Well, it shouldn’t be difficult because it’s a very simple platform. It’s a little harder than it needs to be because libgba has no documentation and the canonical tonc guide references constants that don’t match libgba, so I find myself trying to interpret memory addresses rather than looking for the names of those addresses. The command line graphics conversion utility that ships with devKitARM (grit) has a tendency to either get things wrong (it refuses to generate a correct palette for me) or be unhelpfully helpful (some combination of settings causes it to insert a blank tile at the start of a tile map, causing me to spend far too long trying to figure out why I couldn’t fill the screen with the tile I wanted).

Once I really get the hang of the GBA I’ll hide all of its features behind an abstraction library that I can make cross-platform, which will let me debug my code on my development machine. With neither a debugger nor the ability to printf() on the GBA I’m wasting far too much time running C in my head.

The Port

The GBA’s screen is 240x160 pixels, which is about 20% smaller than the Dragon 32’s resolution of 256x192 (this is a remake of the Dragon 32 version). The DS port was very fortunate in that the DS’ resolution is also 256x192, so there was no need to scroll or scale the game to get it to fit. For the GBA port I decided to opt for scrolling as it’s so simple to achieve (just change a couple of registers). It refused to work for a while until I dug up a comment in one of the examples that noted that the registers are write-only. Duh.

I think that the scrolling works really well. The levels aren’t much larger than the GBA’s screen so it’s impossible to get disoriented or lost. It is possible to get into a “leap of faith” situation where a player might jump from the top of the level hoping that there’s something at the bottom to catch them, but it doesn’t make the game less enjoyable (at least, not for me, but then I know these levels backwards so I’m not a great test subject).

The unmodifiable parts of the levels - the floors and ladders - are implemented in a tiled background. Everything else - the hens, duck, eggs, chicken feed and Harry himself - are all sprites. That gives me around 30 sprites per level, which the GBA throws around without breaking a sweat. I’m considering changing the eggs and feed to become tiles within the background if performance becomes a problem.

Sound is powered by MaxMod. The API is slightly different to the DS’ version, but they’re similar enough that making sound work was simple.

I wasn’t particularly worried about performance. From the considerable amount of perf work I’ve done on the DS version I know that the overwhelming amount of work the game does is in the UI libraries that I’m not using.

Disaster Strikes

Everything was progressing well until I hit a massive problem. The game worked perfectly in mGBA and VBA-M, but it refused to do anything but display a green screen on a real GBA. Every time I’ve encountered something like this on the DS it was caused by a memory problem. The DS emulators I use don’t accurately model the behavior of memory on a real device, so problems like allocating the wrong amount of memory, forgetting to initialize variables, and using memory after it’s freed (etc) can crash real hardware but cause no problems under emulation. I set about investigating.

First step: Code inspection. Was I allocating the wrong amount of memory for my objects? That’s a mistake I’ve made repeatedly in SZLib: allocating enough memory for a pointer to a struct rather than the struct itself. Nope. Forgetting to initialize variables? Nope. Leaking memory? Nope.

Next: Run the SDL version through a static analyzer. No problems.

Next: Run with the address sanitizer enabled. No problems.

Maybe the problem is in the GBA front-end code. I #ifdef-ed out enough of the GBA-specific code to get it compiling on my dev machine and ran that through all of the available code/memory checking tools. No problems.

Let’s dig back through the Git history to find a version that did work on the GBA. Rolling back a few days gave me a version that worked. It seemed that I’d subsequently made a change where I stopped writing to an intermediate buffer representing the OAM (object attribute memory; for sprites) and wrote directly to it. Undoing that change didn’t fix the bug.

I started to remove arbitrary pieces of code that caused memory allocation to occur. Sometimes a change would allow the game to run; sometimes a different bug would manifest. It made no sense.

I enabled libgba’s console output and wrapped malloc() so that it would print the address of the allocated memory. No clues there: all of the addresses were safely contained within the correct region of RAM. Then I started printing out the addresses of stack variables. It seemed that I was getting close to the end of the stack, which perhaps made sense. Stack variables are stored in a 32K block of memory (IWRAM) which is immediately adjacent to a 1K block of memory that controls I/O. Overflowing the stack would cause weird behavior. Immediately following the 1K I/O block is a 1K palette block. One of the changes I made to the game caused the palette to start cycling. Aha, that seems likely. But how am I overflowing the stack? And why doesn’t that bug show up in the emulators?

I started up mGBA and took a look at its memory viewer. According to that the game uses a tiny fraction of the 256K and 32K RAM blocks. There’s no way it should be overflowing the stack or encountering problems related to memory shortages.

Perhaps there’s something wrong with the GBA itself. I recently got hold of another backlit GBA which is the device I’ve been testing on. I haven’t used it for playing games; maybe it’s defective? Nope, same problem on another device.

At this point I’d wasted three evenings on this ridiculous problem and, although I’d learned a significant amount about how the GBA’s memory is laid out, I’d made no progress in figuring out the cause of the issue.

I put everything away and thought about the problem some more.

There’s nothing wrong with the majority of the code. It’s running on 4 other platforms without issue. I’ve tested it with every analysis tool at my disposal and can’t find any problems. The code that’s specific to the GBA amounts to less than 1,000 simple lines, and I analyzed most of that too. The fact that the game runs perfectly under emulation suggests it isn’t a library or compiler problem. It isn’t a problem with the GBA hardware because it exhibits the same behavior on two different devices. It isn’t a memory exhaustion problem. So what’s the difference between running the game in an emulator and running it on my GBA?

The flash cart.

Could it be the flash cart?

I’ve got an EZ-Flash IV (one of the older ones that uses mini-SD cards). It runs its own code and patches ROM files in order for them to work correctly. Could the flash cart be breaking the ROM? Fortunately I’ve got a couple of older flash carts lying around. Copy the ROM to my Supercard SD and…

It works. Perfectly.

The EZ-Flash is doing something bad to the ROM, causing it to write to bad memory addresses, causing bad things to happen. There’s nothing wrong with my code. And there was no way I was ever going to debug that problem.

To Do

There’s plenty of work left to do before the game is finished:

  • Add a “Simian Zombie” screen.
  • Add a title/high score screen.
  • Add level/life/score/time/bonus indicators.
  • Show the “Game over” screen when the player dies.
  • Show the score increasing when the player completes a level.
  • Show the “Get ready” screen before each level.

I also want to add an attract mode showing little demos of gameplay that appear after a few seconds idling on the title screen. I’m thinking of using that system to show how to perform various techniques in the game (like grabbing ladders easily, bouncing between ladders, etc). Oh, and then there’s the level editor that I didn’t finish, too, but that makes less sense on the GBA as it doesn’t have a writable file system.