Programming Retro Computers with a Little Help from My AI Friends
Here's a simple technique I use that has dramatically improved my productivity when using AI for retro programming.
The key is to give the AI a reliable way to verify its work. Correct results require automated tests.
I use an emulator as part of the development process. For C projects, I typically download a header-only emulator such as z80.hpp (or a similar library). The LLM can understand that code, and it also understands compiler-generated files like .map and .sym files.
Because of that, the emulator becomes much more than just something that runs your assembly. The AI can execute your Z80 code inside the emulator and inspect the complete machine state: variables, program counter, memory contents, registers, flags, and so on. That gives you one half of the agentic feedback loop.
The other half is using a trusted compiler as a reference implementation. For C code, I compile it with Clang targeting the PC and use the compiler's output as the expected result. The emulator then runs the handwritten assembly implementation, and the AI compares the two.
This approach isn't limited to simple algorithms. I recently implemented Bresenham's line drawing algorithm and Cohen–Sutherland clipping in C. I compiled that version with Clang to generate a simple 256×192 two-color PNG image. Then I extended my Z80 emulator with ZX Spectrum screen emulation, mapping the display memory starting at address 16384, and implemented the same algorithms in Z80 assembly.
The test renders a large number of lines (around 200 in my case) using both implementations and compares the resulting frame buffers pixel by pixel. If the screens match, the Z80 implementation is correct. At that point, you have fully automated tests for your assembly code.
Once you have this infrastructure, the final step is to let an AI agent run an iterative improvement loop: generate code, run the tests, inspect the emulator state, fix problems, optimize, and repeat until no further improvements can be made.
Then you go to sleep, and by morning the AI has done the tedious work for you.