🔁 Where we left
In part 2 we made hello.lua pass with minimal code, loaded a font into CHR-ROM so font.lua passed, and wrote hello_again.lua expecting the greeting at column 4, row 6. The test suite ended red. Today we complete a full TDD cycle across all three steps:
- 🟢 make
hello_again.luapass by generalising the greeting logic - 🔵 clean up duplicate Lua test code while the suite is green
- 🔴 write a failing test for the next feature: showing a sprite on screen
The code lives on branch part-3 of nes-tdd-demo: git clone -b part-3 https://github.com/ilmanzo/nes-tdd-demo. Each step has its own commit, so git log walks through the progress.
🟢 GREEN: generalise
The failing test asks for a greeting at a second position. We replace the hard-coded write in main with a helper that takes coordinates, and call it twice:
static void greet(unsigned char col, unsigned char row) {
vram_adr(NTADR_A(col, row));
vram_write("HELLO", 5);
}
int main(void) {
pal_bg(palette);
ppu_off();
greet(12, 14);
greet(4, 6);
ppu_on_all();
...
There is no parameter for text, no table of greetings, and no loop, because no test asks for them yet. The second test forced only the abstraction we needed.
$ make podman && make test
...
== tests/hello_again.lua
PASS: nametable row 6 col 4 expected "HELLO" got "HELLO"
1 checks, 0 failed
ALL PASSED
The whole suite is green. The linker report shows ram: 32 B used out of 1536.

🔵 BLUE: refactor with a safety net
Part 2 left duplicate code on purpose: hello.lua and hello_again.lua shared 16 lines, including the printable() helper, differing only in coordinates. I left them alone while the suite was red, because when a test is failing, a refactoring mistake looks identical to the real failure. With a green suite, cleaning up is safe.
The shared code moves into the harness at tests/lib.lua:
local function printable(s)
return (s:gsub("[^ -~]", function(c)
return string.format("\\x%02x", c:byte())
end))
end
function T.check_text(col, row, expected)
local got = T.nt_text(col, row, #expected)
T.check(got == expected, string.format('FAIL: nametable row %d col %d expected "%s" got "%s"', row, col, expected, printable(got)))
end
Each test file then shrinks to its specific assertions:
local T = dofile((os.getenv("ROOT") or ".") .. "/tests/lib.lua")
T.run(function()
T.wait(10)
T.check_text(4, 6, "HELLO")
T.shot("hello_again")
end, 60)
Each test file loses 9 lines net, while the harness gains 11. That barely changes total line count, but the next text check will take only one line, and the failure message is defined in one place. The output of make test matches the previous run character for character, confirming the behavior did not change.
To confirm the refactoring did not weaken the test, we can break the code intentionally, just as we did in part 2. Removing the greet(4, 6) call makes the suite fail again with the original message:
== 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
Restoring the call returns the suite to green. This is how test helpers grow in this series: T.check_text was introduced only when a second test required it.
🔴 RED: a sprite
Sprites provide another way to put graphics on screen. Instead of writing tiles to a background grid, the PPU draws up to 64 independent objects described in OAM (Object Attribute Memory), using 4 bytes per sprite:
| byte | meaning |
|---|---|
| 0 | Y position |
| 1 | tile number |
| 2 | attributes (palette, flip, priority) |
| 3 | X position |
Mesen2 exposes this memory to Lua as emu.memType.nesSpriteRam, mapped in our harness as T.OAM. The new test scans these 64 entries, looking for a sprite at the player’s starting position near the center of the screen:
local T = dofile((os.getenv("ROOT") or ".") .. "/tests/lib.lua")
local PLAYER_TILE, PLAYER_X, PLAYER_Y = 64, 120, 112 -- tile 64 is "@" in the font
-- OAM: 64 sprites x 4 bytes: y, tile, attributes, x
T.run(function()
T.wait(10)
local found = false
for i = 0, 63 do
local y, tile, x = emu.read(i * 4, T.OAM), emu.read(i * 4 + 1, T.OAM), emu.read(i * 4 + 3, T.OAM)
if tile == PLAYER_TILE and x == PLAYER_X and y == PLAYER_Y then found = true end
end
T.check(found, string.format("FAIL: sprite expected tile %d at x=%d y=%d, OAM entry 0 is y=%d tile=%d x=%d",
PLAYER_TILE, PLAYER_X, PLAYER_Y, emu.read(0, T.OAM), emu.read(1, T.OAM), emu.read(3, T.OAM)))
T.shot("sprite")
end, 60)
A few details about this test:
- Tile 64 is
@in our font. The font has no solid player block, and drawing one would distract from the goal. A sprite is just a tile index, so using an@like an old roguelike keeps things simple. - The test reads raw OAM bytes to compare against what the code writes. The PPU actually renders sprites one scanline below their stored Y byte, but we can ignore that hardware offset as long as code and tests use the same convention.
- The search loop is written inline because only one test inspects OAM right now. When a second test needs sprite checks, we can extract a
T.oam()helper just like we did withcheck_text. - The failure message prints OAM entry 0 so we can inspect whatever is currently in memory. Because OAM starts cleared to zero on boot, tile 0 will not match tile 64, preventing false passes.
We add tests/sprite.lua to TESTS:
$ make test
...
== tests/hello_again.lua
PASS: nametable row 6 col 4 expected "HELLO" got "HELLO"
1 checks, 0 failed
ALL PASSED
== tests/sprite.lua
FAIL: sprite expected tile 64 at x=120 y=112, OAM entry 0 is y=0 tile=0 x=0
1 checks, 1 failed
1 FAILED
RED: tests/sprite.lua has failing checks
make: *** [Makefile:33: test] Error 1
The test fails as expected. The screen output has not changed because nothing writes to OAM yet. To confirm the test could pass, I temporarily added oam_clear(); oam_spr(120, 112, 64, 0); to main, watched the test turn green, and reverted the change.
This test verifies sprite data in OAM without checking whether pixels appear on screen. Just like the font setup in part 2, confirming visual output requires inspecting rendered pixels, which we will address later.
⏭️ Next
In part 4 we make sprite.lua pass with minimal code, then write the next failing test: holding a d-pad button to move the sprite. That is where the controller port configuration from part 1 comes into play.
