Lessons and the learning game

Build a home computer

A 1977-style home computer on an Uno: a PS/2 keyboard, a text screen, a hex monitor and a BASIC that saves your programs.

The finished machine

The course builds everything a home computer of 1976 or 1977 had, one piece at a time:

  • The CPU board: an Arduino Uno. Its ATmega328P is an 8-bit CPU at 16 MHz with 2 KB of RAM, the same class of machine as the MOS 6502 in the Apple I, which ran at 1 MHz with 4 KB.
  • The keyboard: a PS/2 keyboard on two wires, its clock on pin 3 and its data on pin 2. Click it while the circuit runs and it takes your real typing.
  • The screen: the terminal, 20 characters by 6 lines. It takes one byte at a time on D0 to D7 with a write enable and a clock edge, from pins 13 down to 4.
  • The CPU you can see inside: the first lesson runs the Kestrel, the CPU of about four thousand NAND gates from From gates to a computer, with its program ROM and the same terminal.

How it works

The keyboard makes the clock. Every byte is a frame of eleven bits: a start bit, eight data bits lowest first, an odd parity bit and a stop bit. The Uno reads DATA on each falling edge of CLK in an interrupt, and checks the start, stop and parity bits before trusting the byte. Keys send events, not letters: a make code going down, F0 and the same code coming up, and E0 in front for the keys added after the original 84-key layout. Shift is a key the computer remembers is down.

The screen is written a byte at a time. Data on the eight pins first, then WE high, then a rising edge on CLK. The terminal follows teletype rules: byte 10 is a new line, 8 rubs out a character, 12 clears the screen.

The monitor works like the Woz Monitor of 1976: 00.09 shows memory, 05: 21 stores a byte, 00R runs. What it runs is Kestrel machine code on an emulated Kestrel, so the alphabet loop you assemble by hand would run on the NAND gates too.

Mokxi BASIC keeps your program as numbered lines in 768 bytes of RAM, in line number order, and runs it by reading each statement straight from its text. Expressions are read by recursive descent, which is what makes PRINT 2+3*4 print 14. It has PRINT, LET, IF ... THEN, GOTO, GOSUB, RETURN, FOR ... NEXT, INPUT, REM, END and CLS, 26 variables, RND and ABS, and SAVE and LOAD to the ATmega's 1 KB of EEPROM, at 3.3 ms a byte.

The screen shows everything, your typing included. The serial monitor logs what the computer says, like a printer beside it, and you can type into it as well as on the keyboard.

The course

Build a home computer takes six lessons:

  1. The Kestrel CPU: run a CPU of NAND gates, step it, write it a loop in assembly, and find what it lacks: an IN instruction.
  2. A keyboard: decode PS/2 by hand, from eleven-bit frames to make and break codes, Shift and the E0 keys.
  3. A text terminal: drive the screen with eight data lines, a write enable and a clock, and give Enter, Backspace and Escape their jobs.
  4. A monitor program: examine and patch memory in hex and run machine code assembled by hand.
  5. BASIC-lite: a calculator, variables, numbered lines, loops, decisions and errors that name their line.
  6. Project: type and run a program: type in a guess-the-number game, play it with a binary search, change it, save it to EEPROM and load it back.

Every step is checked against the running machine.