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.
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.
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.
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.
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.
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.
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.

