Have you ever wondered how the CPU in your computer or smartphone actually does the things it does? I have done a lot of programming over the years, including some assembly language programming that directly generates machine code. But I always regarded the CPU itself as a magic “black box” that performed its memory reads, arithmetic and logic operations, etc. in wondrous but mysterious ways.
But recently, I came across an interesting website, CPUville.com, where Donn Stewart offers a kit to build a simple 8 bit CPU out of standard TTL integrated circuits. He explains what everything in those circuits does, and how it all helps to make the CPU work.
I am not going to try to explain here what you can read on the website. But I can report that I built the kits, got the CPU working, learned how to program it, and wrote some simple programs. In the process I learned a lot about the inner workings of CPUs.
This project is all about learning! It will cost a little over $300 dollars for all of the kits, and what you end up with is a simple computer that any Arduino can run circles around. But if you want to learn more about CPUs than you ever thought you could learn, this might be the perfect project for you. If you are new to assembly language, you will also learn how to program in assembly language, i.e. create machine code for this CPU.
HardwareThe CPU itself is built on three 4½ x 6½ inch boards – each board containing about 30 ICs. There is the Main board which contains all the registers, the ALU (Arithmetic Logic Unit) board which performs all the math and logic operations, and the Control board that sequences each operation and makes the various connections to move data around between memory and registers, etc.
But those three boards just form the CPU itself. I bought 2 other kits (also each a 4½ x 6½ inch board) that form the complete project. To see what the CPU is doing, there is a register display board that shows in LEDs the status of the program counter, op code, operand, accumulator, and state machine.
The last board is the actual computer that this CPU works with. It has 64 K bytes of RAM memory, a serial interface (that can be interfaced to a PC through a TTY terminal program), and some other support hardware. This board is actually a complete Z80 single board computer complete with a disk drive interface. But for our purpose here, we replace the Z80 with our 3 board 8 bit CPU. We also need to install a ROM which contains a simple monitor program for this specific CPU (it comes with the CPU kit).
All five of these boards come with excellent instructions and building them from kits was very straight forward. Directions were always crystal clear!
Together, they form a stack of 5 boards that form a complete computer with a front panel display. Everything on these 5 boards has been carefully thought out and they connect together perfectly to form a finished computer.
There are several clock options for our CPU. It normally runs at about 2 MHz, but a slow clock option allows you to watch the action at about 10 clock cycles per second on the register display. There is also a single step option where you can manually step through each state of an individual instruction. (The slow clock option is what you are viewing in the title page video for this project!)
SoftwareThis CPU has its own unique instruction set. The hardware is very simple as CPUs go, and as a result, the instruction set is fairly limited. But it is a full-fledged computer, capable of doing anything. Programming it, however, can be a little harder than programming a more complex CPU with a richer instruction set.
There are no high level languages here like Python or C++. Everything is assembly language or machine code. But programming can be done on a PC. The assembler that can program this CPU is called TASM and, unfortunately, it runs in DOS. However, there are several DOS emulators for modern PCs. I used vDos myself. You place it in a folder and that folder becomes a DOS based computer. Drop TASM in that folder, along with your source files (.asm) and a CPU specific op-code file, and you can start writing code for this new CPU. TASM processes your source code and produces a detailed program listing, along with a binary code file that can be loaded directly into program memory.
As we said earlier, the CPU kit comes with a Monitor program on ROM. This monitor program allows us to examine memory, load a program through the serial interface, execute code at a specified address, etc. It also provides basic I/O operations through the serial interface.
I am not going to duplicate any of Donn’s work here. But I will leave you with two sample programs that I wrote: the first is of the “Hello World” variety. It asks for your Name, and then responds with “Hello Name” – a simple demo of how to use the I/O routines on Donn’s ROM monitor program.
My second program is TicTacToe. This is a version where I deliberately left out some mid-game logic – the stuff that makes it impossible to beat the computer. TicTacToe is, after all, a kid’s game, and what kid wants to play a game they can’t possibly win. So this program plays a good game of TicTacToe, but it can be beat.
The TicTacToe game was an interesting project in itself. It made me think about the limitations of this CPUs instruction set, and how to take advantage of the tools that I did have. I generally try to avoid self-modifying code, for example, but with the single 8 bit accumulator being the programmer’s only available register, you find yourself resorting to self-modifying code whether you like it or not!
ConclusionDonn Stewart has written excruciatingly detailed instructions on how to build all this – the CPU itself, how to connect it up, test it, connect it to a PC, set up TASM, etc. He also includes a sample program which calculates the value of pi using floating point math, a real accomplishment on this simple CPU.
As I said earlier, this project is about learning how a CPU works, and what make them tick! Building the kits in itself does not automatically accomplish that goal. You have to read and study the documentation. I personally found I understood about 50% on first read, and 75% on the second read. I’m still not at 100%, but I now have a pretty good idea how everything works.
Overall, this was a fairly expensive and time-consuming project, but one I found to be well worth the investment. I learned a whole lot.
Comments