I built a language model for a 1984 MSX

Type "Once upon a time" into a 40-column text screen, press Return, and wait. The first few words take a while. Then the story arrives one token at a time, with roughly four seconds between tokens. The Z80 in an MSX1 does the tokenisation, the neural network inference and the sampling. The model weighs in at roughly 920,000 parameters and lives in a cartridge ROM.
The Toshiba MSX was my first computer. It's where I really learnt to program, and it's the reason I wanted to see how far I could get on the hardware budget of a 1984 home computer.
MSX was an 8-bit home computer standard that Japan's ASCII Corporation developed with Microsoft in 1983. Toshiba, Sony and other manufacturers built their own machines to the same standard, so software could run across brands. Microsoft supplied MSX BASIC, which was built into ROM and let you start writing programs as soon as you switched the machine on. An MSX1 gives you a 3.58 MHz Z80 and a 16-bit address space. The CPU has no multiply instruction. That last detail shaped nearly every decision, from the model architecture to the order in which the exporter writes bytes into the ROM. The build runs in openMSX using its stock-speed C-BIOS_MSX1 profile with 64 KiB of RAM. The timing and test results below come from that emulator. A run on a physical MSX is still pending.
A model with 384 bytes of memory
I used a single layer gated recurrent neural network. It takes a 128-value embedding for the current token and combines it with a 384-value hidden state. Each hidden value is an eight-bit integer, so the model carries 384 bytes of recurrent state from one token to the next. A longer prompt takes more steps to read, but the network does the same amount of work for each new token it generates. A transformer would bring a different budget. Its attention mechanism needs products between activations, which would still require software multiplication even if the model weights were ternary. A key-value cache would also take up more memory as the sequence grew. The fixed size recurrent state gave me something I could budget for in bytes and cycles.
I also dropped the GRU's reset gate. The update gate and candidate state can then read the same input vector, made by joining the embedding and previous hidden state. That lets the two calculations reuse the same lookup tables. The bounded update keeps the next hidden state within the eight-bit range.
Four weights at a time
The model has 786,432 ternary weights. Each is one of three values, −1, 0 or +1. Multiplying an input by the weight reduces to subtracting it, skipping it or adding it. The straightforward Z80 implementation still spends cycles decoding and branching on every weight. Instead, the engine groups four inputs and precomputes the 81 possible weighted sums for that group. Four two bit weights fit in one byte, and the byte itself indexes the table. For example, if the inputs are [6, −3, 2, 5] and the weights are [+1, 0, −1, +1], the selected table entry contains 9. The next output row can use the same table with a different packed weight byte. Across hundreds of rows, that reuse pays for building the table.
The engine processes 64 inputs at a time, using 8 KiB of RAM for 16 groups of tables. It puts the low and high bytes of each sum in adjacent memory pages so the Z80 can use the same index for both. In the hot loop it even points the stack pointer into the cartridge window and uses POP to read two packed bytes at once, with interrupts disabled while the stack pointer is borrowed. The matrix kernel works out at about 15.4 Z80 clock cycles per weight, including the MSX's instruction fetch wait states. Theres still an integer multiply in the recurrent gate update. The lookup trick targets the work that dominates the runtime. In the emulator profile, the matrix kernel accounted for about 82% of generation time.
From PyTorch to cartridge ROM
I trained the model on a modern CPU using TinyStories, with a vocabulary of 1,024 tokens. That vocabulary size directly affects the output matrix. At 384 hidden values per token, scoring the vocabulary takes 393,216 weights, exactly half the model's ternary weights. The training code simulates the arithmetic the cartridge will use, including ternary weights, integer rounding, activation lookup tables and the fixed point state update. The exporter then packs the weights, embeddings, biases and lookup tables into a 512 KiB ASCII8 MegaROM image alongside the assembled engine.
Bank switching is how a Z80 with a 64 KiB address space can read a cartridge that large. The engine stays mapped at 0x4000–0x5FFF. When it needs a different slice of the model, it selects an 8 KiB bank to appear at 0x6000–0x7FFF. The weights stay in ROM, while RAM holds the lookup tables, accumulators and working state. Once you press Return, the assembly code tokenises the prompt using greedy longest match and feeds those tokens through the recurrent network. It then scores the vocabulary, samples from the eight highest-scoring tokens and prints the result. The token it just picked becomes the input for the next step.
Checking the bytes behind the story
A plausible looking sentence is a poor test of a port like this. An arithmetic mistake can change a logit without changing the sampled token in a short run. I used a Python integer implementation as the reference and had the test harness dump the emulators RAM. Across six golden cases, the prompt token IDs, generated token IDs, all 384 final hidden values and all 1,024 final logits matched bit for bit. The emulator measured 4.14 seconds per generated token, plus about 2.1 seconds for each prompt token. A full 48-token continuation takes roughly three minutes and 19 seconds once generation starts. The stories tend to open convincingly, then lose track of who did what. Watching the text appear at that pace, you can see both how much the little network learned and how quickly it runs out of context it can use.
What I enjoyed most was being able to follow a single token all the way through. A choice made in Python changes the bytes in the ROM. The order of those bytes changes what the assembly loop can do. And the cost of that loop determines how long you sit in front of the prompt waiting for the next word. I still have that Toshiba boxed up. At some point I'll get it out, try the ROM on the original hardware and see what story it comes up with.
The trainer, ROM exporter, Z80 engine, emulator screenshots and build instructions are in the msx-llm repository in my Github.
About the author
- Senior Software Engineer specialising in cloud architecture, distributed systems, and modern .NET development, with over two decades of experience designing and delivering enterprise platforms in financial, insurance, and high-scale commercial environments. My focus is on building systems that are reliable, scalable, and maintainable over the long term. I’ve led modernisation initiatives moving legacy platforms to cloud-native Azure architectures, designed high-throughput streaming solutions to eliminate performance bottlenecks, and implemented secure microservices environments using container-based deployment models and event-driven integration patterns. From an architecture perspective, I have strong practical experience applying approaches such as Vertical Slice Architecture, Domain-Driven Design, Clean Architecture, and Hexagonal Architecture. I’m particularly interested in modular system design that balances delivery speed with long-term sustainability, and I enjoy solving complex problems involving distributed workflows, performance optimisation, and system reliability. I enjoy mentoring engineers, contributing to architectural decisions, and helping teams simplify complex systems into clear, maintainable designs. I’m always open to connecting with other engineers, architects, and technology leaders working on modern cloud and distributed system challenges.
Comments
Join the discussionFitting a 920k-parameter language model into an MSX1 cartridge ROM on a 3.58 MHz Z80 without a hardware multiply instruction is a brilliant exercise in hardware-aware model design. Choosing a single-layer recurrent architecture with a fixed 384-byte 8-bit hidden state avoids both the quadratic attention products and the growing KV cache that would immediately blow past 64 KiB of RAM.
Dropping the GRU reset gate so the update gate and candidate state share lookup tables—paired with ternary weights—shows how much co-designing architecture and memory layout can squeeze out of severely constrained silicon.




