in General

Minimalist Z80 Computer

For years, I’ve had an abiding love of machine code, the fundamental binary instructions that the CPU of a computer understands, ever since I took my first embedded systems course at Santa Clara University. Below all the user interfaces and flashiness of modern computers, there is a hunk of silicon which is reading ones and zeroes from memory and performing the fundamentals of computer operation — store a byte in memory, retrieve a byte from memory, add one to the byte, subtract something from the bytes, etc. The fact that such a powerful force multiplier of humanity’s power all came down to a rock we trapped lightning in and forced to do math billions of times a second will forever be amazing to me.

One of the first machines that helped spark the computing revolution was the 1974 Altair 8800.

MITS Altair Simulator
Before I even knew its name, I saw this picture in my books about computers when I was young and was enamored of the concept: how could you talk to a computer without a keyboard and screen? The answer was to use the computer’s prima lingua — binary.

This basic machine was one of the first microcomputers, and in its initial state, you had to input binary code using the switches on the front to store commands in memory which the CPU would execute (here’s a long video but you can see him entering the binary code for a basic game at 1:41). Usually, you’d just input enough to bootstrap something more efficient like a tape drive or teletype unit but until then, you need to enter in your code one bit at a time: on-off-off-off-off-one initializes a counter, all off sets the counter to zero, and so forth. You can see a video of someone setting up a basic teletype with a BASIC interpreter and a prime number computing program here.

To me, this is the fundamental of computing — you go any higher level and there’s an abstraction layer between you and the CPU. You go any lower, and it just becomes a collection of transistors which is nearly impossible to get any work done with. But at the level of flipping bits to set memory which the CPU executes, you experience the platonic ideal of computing.

Another fun fact is the Altair helped give birth to Microsoft. When Paul Allen and Bill Gates were just getting started, Micro-Soft’s (as it was spelled at that time) first product they sold was an interpreter for the BASIC language which ran on the Altair and was sold to Traf-O-Data for use in counting cars that drove over car-counter systems (used to determine flow rates and impact of construction that would shut a street, for example).

Unfortunately, the Intel 8080 is the CPU at the heart of the Altair, and it is a wretched being. Its instructions (called “opcodes” for “operational codes”) are unintuitive and hard to understand, and it’s limited in a few key areas. However, the Zilog Z80 processor, also released in 1974, intentionally made massive improvements to the Intel 8080. The Z80, in fact, became the paradigm on which Intel modeled its 8086 architecture on (called “x86”), an architecture that reigns supreme today, nearly half a century later — if you’re reading this on a computer (and not a phone), you’re using the x86 architecture which was heavily inspired by the Z80.

Zilog Z80 - Wikipedia
The Z80 processor

It also happens that the Z80 is an incredibly close step-sibling to the Sharp LR35902 (catchy names, right?) which is the processor that runs the Gameboy Color. The Gameboy Color was the first piece of computing equipment that I owned myself (as opposed to the family computer) and was also the first platform which I did real world hacking on by writing machine code (I modified the Tetris DX game to give me a line piece whenever I hit the Select button 😉). Add on to this the fact that while the 8080 dominated the American market, the Z80 governed the British (and New Zealand!) markets in the form of the Sinclair ZX Spectrum, one of the first home computers, and you can understand my abiding fondness for the Z80.

ZX Spectrum - Wikipedia
The ZX Spectrum, in its computer-in-keyboard form factor

So, about two years ago, an idea was borne to build a sort of stripped-back Altair 8800 that had a Z80 at its heart instead of the 8080, and that was not meant as a stepping stone to teletype machines and monitors but instead an altar to those basic concepts of computing — memory in, memory out, CPU execute.

Unfortunately, I was put off by the complexity of actually directly driving a CPU — running a CPU outside of a computer and keeping it stable is very difficult. I wanted to do things like step-wise execution so I could watch the processor work in real time (as opposed to the 2.5 million cycles per second the Z80 originally ran at — slow by today’s numbers, but still far too fast for a human to follow), and slowing down a chip to 10-20 cycles per second is especially tricky without pretty advanced knowledge of how to keep it from falling off its metaphorical bicycle.

I couldn’t shake this idea, though, so I decided to see about emulating the CPU in a modern programming language, and pounded out a minimalist Altair clone in a week.

My basic emulator in Javascript bouncing a bit back and forth at memory address 0x0100.

Now there was blood in the water, and rather than sating my desire to build this, it strengthened it.

Fast forward to this week, and I dove head first into trying to find a Z80 emulator that was small enough to be run by an Arduino, a hobbyist electronics platform. Sadly, the few which existed were exceptionally poor, so I set about converting the brilliant (and incredibly thorough) work of Marat Fayzullin and his ultra-tiny and ultra-portable emulator written in ANSI C to work on the Arduino.

After a couple evenings’ hacking, I had it compiling and running. I submitted my library to the Arduino library collection, so hopefully I’ll save someone some trouble in the future (published open source on GitHub, as always)

My library listed in the Arduino collection

Alas, the fun part! Now I needed to put together the actual hardware to enter and display addresses and data. I took a half day for the long weekend and spent a few hours wiring it up and it worked beautifully — an ultra-minimal Altair 8800-style system with an emulated Z80 at the core. Here’s a quick video of me running the bouncing bit routine you saw in the emulator above, halting execution to load data from memory, etc.

(Pay no mind to the flayed furby in the background at the end.)

Enjoy the dulcet beeps and boops of Boards of Canada that I forgot to pause before recording this.

This was a super satisfying project, and as fiddly as programming the thing is, I feel a certain kinship with generations past who discovered the magic and power of computing this way.

At some point I’d like to package this up into a nice enclosure with slick fit and finish but there are so many wires which would require a lot of soldering which I hate. If anyone is passionate about PCB layout or would be interested in a joint effort, my inbox is wide open. We’ll see if this raw and messy version sates my desire… I have low hopes for getting this project off my mind though.

Such is love.

Write a Comment

Comment