Jorge and Mochi

Putting a Ruby compiler inside a Game Boy cartridge

I wanted to write Ruby on the original Game Boy. So far I’ve tested my cartridge, GBRuby, entirely in emulators. Pick Tetris from the menu and the Game Boy’s 8-bit processor, the Sharp SM83, compiles the Ruby source. Then it has to keep the pieces falling.

I’d like to give this talk at RubyKaigi 2027, a Ruby conference in Japan. In a first for me, I’m sharing the slides while they’re still a draft. Read the draft slides (PDF, 34 slides).

To type Ruby on a Game Boy, I pick a letter on the on-screen keyboard with the D-pad and press A. Start runs the program. In the emulator I can also use my computer keyboard, which is how I’d recommend typing anything with parentheses. For the talk I picked a two-line program small enough to follow from keypress to LCD. The compiler turns the Ruby into bytecode, a set of instructions my interpreter (the virtual machine, or VM) knows how to run.

Three captures of the cartridge’s Ruby prompt. The smallest font fits 38 columns and 22 rows on the 160 by 144 pixel screen.
Slide 5. Select changes the keyboard page. At the smallest font size, I can fit 38 columns and 22 rows on that screen.

Finding room for Ruby

The Game Boy has 8 KiB of work RAM. A phone with 8 GB has roughly a million times as much. Into those 8,192 bytes I had to fit the VM, its instruction cache, a copy of the text screen and the CPU stack. The cartridge supplies 128 KiB of writable SRAM, but the CPU sees it through an 8 KiB window. When I switch banks, the same address refers to a different byte. Ruby objects live in three of those banks, so a heap reference has to identify both the bank and the object within it.

Memory map showing the VM, cache and stack in work RAM, the heap and bytecode in cartridge SRAM, and the compiler and dispatcher in ROM.
Slide 10. The VM keeps room for 16 active Ruby calls in work RAM. When calls nest deeper, it moves older frames into cartridge RAM.

Every Ruby value occupies four bytes for its type, flags and a 16-bit payload. A small integer fits in the payload. Bigger integers live on the heap, and the reference still fits in four bytes.

Nine bytes to print a three

x = 1 + 2
puts x

The lexer splits the source into tokens, keeping just the current one and the next. The parser gives x local slot zero and works out that 1 + 2 is 3. It writes bytecode as it goes, compact instructions for the Ruby VM. There’s no syntax tree to find room for.

The nine release bytecode bytes are 08 13 00 03 14 2D 00 01 00. The slide groups them into six instructions.
Slide 16. The first byte, 08, means “push the integer three.” The three is part of the instruction itself.

The verifier traces what each instruction puts on the stack or takes off it. These nine bytes need one slot and never read from an empty stack. Checking once saves the VM repeating that work. Its dispatcher reads each instruction and picks the routine the SM83 executes. Here it stores the three in slot zero, drops the spare copy, loads the value again and calls puts. A 3 appears on the screen.

There’s a six-byte version in the deck, too. A specialized instruction prints an integer local directly, and its handler passes the tests. The cartridge has 2 MiB of ROM. The dispatcher lives in one 16 KiB bank, and that bank was too full for the handler. I had space in the ROM and still couldn’t afford to save those three bytes.

When the program has to move

In Tetris, Ruby reads the buttons, picks the pieces, times the drops and keeps score. Scanning the playfield, the ten-by-eighteen grid where the pieces land, meant a slow loop over squares in Ruby. In C I pack each row into ten bits. One bit tells me whether a square is occupied. One comparison tells me whether to clear the row. Snake stores a two-bit arrow in each occupied square, so moving the tail means following one arrow. The boards for Life, Tetris and Snake together fit in 267 bytes.

Measured frames before and after adding native board operations: three Life generations fall from 6,924 to 320 frames, the first Tetris piece from 2,160 to 372, and one Snake step from 55 to 6.
Slide 24. The screen refreshes about 60 times a second. Compiling Life and running three generations went from 116 seconds to 5.4. The first Tetris piece appeared in 6.2 seconds instead of 36. One Snake step fell from 55 frames to six, or from nearly a second to a tenth. That’s a difference you can feel through the D-pad.

I also wrote SM83 assembly for the VM instructions that run most often. A thousand integer-arithmetic iterations took 458 frames in C (7.7 seconds) and 79 with the assembly (1.3 seconds). Speed is the easy bit to measure. The deck records 1,296 programs run both ways to check that the output and even the bytes left in the heap agreed.

GBRuby’s red title screen in Game Boy Color mode. A square Tetris piece above a stack on the emulated original Game Boy.
Same cartridge, two emulator modes. The Tetris demo ships as Ruby source in ROM.

The test that passed while the stack overflowed

The CPU stack gets 1,504 bytes. An early parser made several function calls for each opening parenthesis. Together those calls used about 208 bytes per level. Around six levels in, it started overwriting the prompt’s output buffer. The expression still printed the right answer.

Stack layout showing the CPU stack growing downward toward the Ruby prompt’s 512-byte output buffer.
Slide 29. The output buffer sits immediately below the stack. Moving it would break the browser, terminal client and tests that read it here.

In the PyBoy emulator I filled unused stack space with 0xA5. After the test, the lowest address where that marker had changed showed how far the stack had grown. I shortened the chain of calls for a parenthesis and added a check to stop before the stack ran out. Later I moved rarely used branches into their own functions. One parser function’s share of the stack fell from 64 bytes to 38. Shrinking several such frames took the parser from 14 nested pairs of parentheses to 23.

The next test needs a flash cartridge and an original handheld. I’d like to see that first piece fall on a real Game Boy.