🔁 Where we left
In part 1 we built a blank NES project and one test, tests/hello.lua, that expects “HELLO” in the nametable at column 12, row 14. The suite was red, for the right reason. Today we go around the loop twice:
- 🟢 make
hello.luapass with the least code possible - 🔴 notice that green is not enough: the screen is still blank
- 🟢 add a font
- 🔴 write a test that the least code cannot pass
The code is on branch part-2 of nes-tdd-demo: git clone -b part-2 https://github.com/ilmanzo/nes-tdd-demo.
🟢 GREEN: the dumbest thing that works
The test reads the nametable, so the code only has to put five bytes there. The PPU can be written safely only while the screen is off (or during vblank), hence ppu_off() / ppu_on_all() around the write:
int main(void) {
pal_bg(palette);
ppu_off();
vram_adr(NTADR_A(12, 14));
vram_write("HELLO", 5);
ppu_on_all();
while (1) {
ppu_wait_nmi();
}
return 0;
}
Position and text are hard-coded, on purpose: that is the smallest change that satisfies the test.
$ make podman && make test
...
== tests/hello.lua
PASS: nametable row 14 col 12 expected "HELLO" got "HELLO"
1 checks, 0 failed
ALL PASSED
Green. And here is the emulator showing the same ROM:

The nametable contains tile numbers; what you see depends on what is drawn in those tiles, and our ROM has no graphics yet. The test is green and the screen is black: our suite has a hole.
🔴 RED: the pixels
Here is a new test written specifically to catch what our existing test missed. The pixels live in the pattern tables, 8 KB of CHR-ROM on our NROM cartridge. Each 8×8 tile uses a 2-bit-per-pixel (2BPP) planar format—8 bytes for bitplane 0 followed by 8 bytes for bitplane 1, giving 16 bytes per tile. With our convention (tile number = ASCII code) the tile for H starts at PPU address 72 * 16. A letter that is drawn has some non-zero bytes there:
T.run(function()
T.wait(2)
for ch in ("HELO"):gmatch(".") do
local lit = 0
for i = 0, 15 do
if emu.read(ch:byte() * 16 + i, T.PPU) ~= 0 then lit = lit + 1 end
end
T.check(lit > 0, string.format('FAIL: pattern tile "%s" (%d) expected lit pixels, got %d non-zero bytes', ch, ch:byte(), lit))
end
end, 30)
$ make test
...
== tests/font.lua
FAIL: pattern tile "H" (72) expected lit pixels, got 0 non-zero bytes
FAIL: pattern tile "E" (69) expected lit pixels, got 0 non-zero bytes
FAIL: pattern tile "L" (76) expected lit pixels, got 0 non-zero bytes
FAIL: pattern tile "O" (79) expected lit pixels, got 0 non-zero bytes
4 checks, 4 failed
4 FAILED
RED: tests/font.lua has failing checks
Red, and for the right reason: no graphics at all.
🔤 GREEN: a font in CHR-ROM
I do not want to draw glyphs by hand, and NES-ready fonts already exist as .chr files. I took Alpha.chr from the nesdoug tutorial examples by Doug Fraker, the author of the nesdoug library we link against. It is released under the MIT licence: I copied the file and the licence text unchanged into assets/, as the licence asks. It is an 8 KB CHR image, the exact size of our cartridge’s graphics ROM, and tile number = ASCII code, as our test assumes. No conversion needed.
The only job left is to get the 8192 bytes into the ROM, in the section that the llvm-mos linker script maps to the graphics ROM. An assembler file does it with one directive, src/chr.s:
.section .chr_rom,"a",@progbits
.incbin "assets/Alpha.chr"
The Makefile already compiles every src/*.c; it now takes src/*.s too:
SRCS = $(wildcard src/*.c src/*.s)
The linker report changes from chr_rom: 0 GB to:
chr_rom: 8 KB 8 KB 100.00%
== tests/font.lua
PASS: pattern tile "H" (72) expected lit pixels, got 14 non-zero bytes
...
4 checks, 0 failed
ALL PASSED

Note that hello.lua alone would have stayed green with an empty CHR-ROM: it was font.lua that noticed.
🔴 RED again
Now we make the test with hard-coded write fail: same greeting, another place. tests/hello_again.lua is a copy of hello.lua with COL, ROW = 4, 6, and it goes last in TESTS:
$ make test
== tests/hello.lua
PASS: nametable row 14 col 12 expected "HELLO" got "HELLO"
...
== tests/font.lua
PASS: pattern tile "H" (72) expected lit pixels, got 14 non-zero bytes
...
== tests/hello_again.lua
FAIL: nametable row 6 col 4 expected "HELLO" got "\x00\x00\x00\x00\x00"
1 checks, 1 failed
1 FAILED
RED: tests/hello_again.lua has failing checks
make: *** [Makefile:38: test] Error 1

Red, for the right reason: the other tests still pass, this one finds zeros where it expects letters. Three details:
- yes,
hello_again.luaduplicateshello.lua,printableincluded. We are red, and refactoring on red is wrong: the clean-up waits for the green bar in part 3. - the loop in the
Makefilestops at the first red, so only one thing fails at a time. That is the TDD order anyway. - to make sure the first green was earned, I reverted
src/main.cand rebuilt:hello.luafails again with its own message. A test that stays green without the code it tests proves nothing
📚 Credits
- nesdoug:
Alpha.chr, MIT licence, Copyright (c) 2018 Doug Fraker - the NESdev wiki on pattern tables and their tile format
⏭️ Next
In part 3 we’ll make hello_again.lua pass the right way: a function that draws a greeting at any position, a shared helper in the Lua library, and the tests tidied up under the green bar. Then a new red: we will add a sprite.

