A 6502 computer, written gate by gate.
We start with a single gate and a blinking light. We end with a machine that shows its picture on an HDMI screen, reads a USB keyboard and runs BASIC. The processor is our own, written line by line in Verilog for one small FPGA board: the Sipeed Tang Nano 20K.
Three short videos
The first shows where the series goes: the machine running Microsoft BASIC, typed on its own keyboard. The second shows where it comes from: the 6502 computer I built on breadboards, before writing one in Verilog. The third shows the same computer on a smaller board, the Tang Nano 9K, with a 7-inch screen and over HDMI.
-
From One Logic Gate to Microsoft BASIC
A processor written from scratch in Verilog, its own HDMI picture, a real USB keyboard, and Microsoft's BASIC from 1977, all in one small FPGA board. There is no 6502 chip inside.
-
What Comes After Ben Eater's 6502?
A 6502 computer on breadboards, in the style of Ben Eater's, with a GAL as its address decoder and a FabGL board for its VGA screen. Everything on it is moving into the Tang Nano.
-
Our Own 6502 Computer Fits on a Tang Nano 9K
The same computer on the smaller Tang Nano 9K: 24 KB of RAM, Microsoft BASIC on a 7-inch screen with a small USB keyboard module, and the same picture over HDMI.
The episodes
The videos show every line being written, and explain it. They take their time. If you can program, in any language, you can follow: no Verilog and no assembly language needed at the start.
-
I'm Rebuilding the 6502 from Scratch, One Gate at a Time
What an FPGA really is, the free tools, and two circuits on the board: a single logic gate, then a light that blinks with no program running.
-
How a Computer Adds: I Built an Adder from Just Two Logic Gates
The six basic gates, counting in binary, and the half adder: two gates that add two bits. On the board: one plus one, shown on two LEDs.
-
I Built an 8-Bit Adder and Tested Every Possible Addition
Eight full adders in a chain, and a test bench that tries every addition there is: all 131,072 of them. On the board: 23 + 19 = 42.
-
How a Chip Remembers: I Built Memory from Scratch, One Bit at a Time
The flip-flop: one bit of memory, and a clock. Then a register, a counter, a timer and a first state machine, a light that runs back and forth.
-
How a Screen Is Really Drawn: One Pixel at a Time, with Two Counters
What a monitor expects to receive, and the circuit that produces it: two counters and a test pattern. A test bench writes the whole 640 by 480 picture into a file, with no screen plugged in.
-
The Missing Clock: How a PLL Turns 27 MHz into 25.2 MHz
The board has a clock of 27 MHz, and the picture needs 25.2 MHz. No counter can make one from the other: a PLL can. The proof is on the board, two timers and two LEDs that blink together.
-
How HDMI Really Works: I Sent a Picture from an FPGA to a Real Monitor
How the picture gets from the chip to a monitor: a TMDS encoder written in Verilog, three serializers and a clock five times faster pack its 26 wires into the four pairs of an HDMI cable. The test pattern appears on a real monitor.
-
Our 6502 Computer Gets a Video Card: 320x200, 16 Colors
The picture moves into a memory: 320 by 200 pixels in 16 colors, with two colors for each cell of 8 by 8, the way the Commodore 64 did it. The memory and the display are written in Verilog, and a title picture appears on a real monitor.
-
Our First 6502 Program: Machine Code, Assembled by Hand
Where the processor begins: what it really does, fetch a byte, decode it, execute it, and start again. Then our first program, five instructions turned into eleven bytes by hand, one opcode at a time from the table.
-
Assembling Our First 6502 Program with ca65 and ld65
The same program, assembled a second time with the tools made for the job: the assembler ca65 and the linker ld65. They give the same eleven bytes as the ones assembled by hand, and the program runs with a pencil, the way our processor will have to.
-
ROM, RAM and the Bus for Our 6502 Computer, in Verilog
The memory of our computer, in Verilog: a ROM that holds the program, a RAM that can be written, and the bus that ties them to the processor. Both are tested in the simulator, and we work out why reading one byte takes two ticks of the clock.
-
Our Own 6502 in Verilog: Writing the First Lines of the Processor
The first lines of our processor in Verilog: a program counter and a state machine with two states, enough for it to fetch. In the simulator, it reads the eleven bytes of our first program one by one, and then we break it on purpose, twice.
-
Writing the ALU of Our Own 6502 in Verilog
The part of our processor that computes, written in Verilog: an ALU with thirteen operations. Five of them are done by one adder, because on a byte a subtraction is an addition. Everything runs in the simulator.
-
The Four Flags of Our 6502's ALU, in Verilog
Our ALU gets its four flags: carry, zero, negative and overflow. Then we test it on every input it can ever be given, 1,703,936 operations checked in the simulator, and break it twice on purpose to see the test catch it.
-
Teaching Our 6502 Its First Five Instructions, in Verilog
Our processor starts learning its first five instructions: LDA, STA, CLC, ADC and JMP. We read what a real 6502 does with them, cycle by cycle, then the Verilog that follows the same table, a decoder and a sequencer with five states.
-
Our Own 6502 Runs Its First Program, in Verilog
Our processor runs its first program: five instructions, and a loop that counts. We follow LDA and STA tick by tick, then a test bench checks every cycle of the bus, 6,611 of them, in the simulator.
-
Our Own 6502 Runs on a Real FPGA Board
Our processor leaves the simulator. First, tests written by other people judge its five instructions: 45,038 of them, without one difference. Then, on the Tang Nano 20K, six LEDs count in binary, and no counter in the chip drives them.
-
Our Own 6502 Learns to Read Its RAM, in Verilog
Our processor gets its two index registers, X and Y, and reads a byte back from its RAM for the first time: stored at cycle nine, read back at cycle twelve. On the way, the zero page, where an address fits in one byte, and an instruction the assembler accepts without a word while our processor skips it.
-
Our Own 6502 Reads a Table from ROM, in Verilog
Our processor learns to read a table with an index register and goes back onto the real board: two lights sweep across the six LEDs, and each step comes from a table of 256 bytes in its ROM. On the way, the extra cycle a 6502 takes when the index crosses a page, measured on the board, where the eye can't see it.
-
Our Own 6502 Has the Famous JMP Bug, in Verilog
Our processor learns indirect addressing and gets the most famous flaw of the 6502, the JMP bug: JMP ($03FF) takes the high byte of its target at $0300, not at $0400, as the original chip does. Nothing was added to give it this flaw: it comes from one line of Verilog. On the way, the two modes that keep an address in the zero page, and INC and DEC, which write the same byte twice, one cycle apart.
-
Our Own 6502 Learns to Subtract and Compare, in Verilog
Our processor learns to subtract and to compare: 76 new opcodes, and not one new state, because every new instruction takes a path that was already there. In the whole sequencer, a single line changes. On the way, numbers wider than a byte, added one byte at a time while the carry waits in a flag, and the reason a subtraction starts with SEC.
-
Our Own 6502 Learns Decimal Mode, in Verilog
Our processor learns to count in decimal and runs a stopwatch on the real board: after nine, the four LEDs on the right never show ten in binary, they start again from zero and the fifth one lights up on its own. Nothing in the program reads a clock: each lap of its loop takes 25 cycles, half a second, measured on the chip. On the way, binary-coded decimal, and the strange flags of the NMOS 6502 in decimal mode.
-
Our Own 6502 Passes 1.23 Million Tests, in Verilog
Our processor faces 1,230,000 tests written by other people, decimal mode included, and passes every one. Before that, we break it on purpose, fifteen times: one fault is a single line of 64doc, a famous document on the 6502, and with it the stopwatch goes from nine seconds back to zero. Our own test benches catch fourteen of the fifteen faults; only the tests of others see the last one.
-
Our Own 6502 Learns to Branch, in Verilog
Our processor learns to branch: to read a flag and decide where the program goes next. A branch doesn't hold an address but a distance, one signed byte counted from the instruction after it, so FD means three bytes back. On the way, the smallest loop of collatz.s and how a 6502 waits 51 cycles, the transfers between registers, BIT and CLV. Everything in this episode runs in the simulator.
-
Our Own 6502 Learns to Decide, in Verilog
Our processor learns to decide: each of its eight branches reads one flag, and a taken branch needs two more states. Then a loop that computes runs on the real board, from seven: an even number is halved, an odd one becomes 3n + 1, and the six LEDs show every step down to one, where the loop stops by itself. Measured in the chip, every odd number costs twelve cycles more.
-
Our Own 6502 Branches Across a Page, in Verilog
Our processor takes each of its eight branches both ways, and from one page to the next and back: crossing a page costs one more cycle and a read for nothing. The flags N, Z and V finally get a flip-flop each, because the branches now read them. Then I break it on purpose thirteen times: the program of the board sees five faults, a test bench written for the branches sees all thirteen. And 1,390,000 tests written by other people pass without one difference. Everything in this episode runs in the simulator.
-
Our Own 6502 Gets a Stack, in Verilog
Our processor learns to call a subroutine with JSR and to come back with RTS. The surprise: JSR doesn't push the address it will come back to, but one less, the address of its own last byte, and RTS adds the one back. In the simulator ours does it cycle for cycle, $C007 on the stack and back at $C008. Six opcodes more, PHA, PLA, JSR, RTS, TSX and TXS, and it knows 145 out of 151. Everything in this episode runs in the simulator.
-
One Missing PLA, and Our Own 6502 Gets Lost
Our processor runs a subroutine that calls itself, six calls deep, on the real board. Take out a single PLA and, in the simulator, the program jumps into the RAM and slides through bytes nobody has written; it only finds its first instruction again at cycle 85,343, about 28 minutes on the board. Before that, the sequencer learns the six instructions of the stack cycle by cycle, JSR in six cycles and RTS that adds the one back. Then a test bench checks every cycle of the bus, and the chip itself is measured: 35 intervals out of 35.
-
I Broke Our Own 6502, and the LEDs Can't Tell
Our processor takes its stack all the way round, from 00 to FF and back, and comes home from a JSR that ends on the last byte of a page. Then I break it on purpose: I swap the two bytes of every return address, and the bar of LEDs stays the same, at the same cycles, while the board's test bench says pass. Only a test bench that watches the bus catches it, at the very first push. Fourteen faults go in front of ten test benches, and 1,450,000 tests written by other people still pass. The faults run in the simulator only.
Where the machine stands
The code grows one chapter at a time, and every chapter ends on something you can see. This is what runs today, and what comes next.
-
01-box-of-gatesAn FPGA Is a Box of Gates
An LED driven by two buttons through one gate, then an LED that blinks.
-
02-logic-that-computesLogic That Computes
An eight-bit addition done by our gates, in simulation and then on the board.
-
03-logic-that-remembersLogic That Remembers
A counter on the six LEDs, then a light that runs back and forth.
-
04-how-a-screen-is-drawnHow a Screen Is Drawn
A test pattern computed in the simulator and written to a picture file.
-
05-driving-an-hdmi-screenDriving an HDMI Screen
The same test pattern on a real monitor.
-
06-320-by-200320 by 200
A picture of 320 by 200 pixels in sixteen colors on that monitor.
-
07-what-a-processor-really-doesWhat a Processor Really Does
A first program of eleven bytes, assembled by hand and then by an assembler, which gives the same bytes.
-
08-memory-the-bus-and-the-first-fetchMemory, the Bus and the First Fetch
A ROM, a RAM, a bus and the first piece of the processor: it fetches the eleven bytes one by one, in the simulator.
-
09-the-aluThe ALU
The part that computes: thirteen operations and four flags, checked in the simulator.
-
10-first-instructionsFirst Instructions
Our processor runs its first program: five instructions, and the six LEDs of the board count.
-
11-addressing-modesAddressing Modes
A light two LEDs wide sweeps across the board, read from a table.
-
12-arithmetic-compares-and-decimal-modeArithmetic, Compares and Decimal Mode
A stopwatch that counts the seconds in decimal on the six LEDs.
-
13-branches-and-loopsBranches and Loops
A loop that computes the Collatz sequence on the six LEDs and stops by itself on 1.
-
14-the-stack-and-subroutinesThe Stack and Subroutines
A subroutine that calls itself makes a bar of light grow and shrink on the six LEDs.
-
15-reset-and-interruptsReset and Interrupts
A light walks along the six LEDs; button S2 interrupts it, and it goes on from where it was.
-
16-is-it-really-a-6502Is It Really a 6502?
The functional test of Klaus Dormann runs again and again on the board, at full speed, and the six LEDs count the runs that went well.
-
17-the-processor-drawsThe Processor Draws
The processor draws 128 straight lines on the screen, then changes the colors of the whole picture.
-
18-letters-made-of-pixelsLetters Made of Pixels
Text on the screen: a few lines, the 95 characters of our font and 48 lines of slanting stripes.
-
19-talking-to-your-computerTalking to Your Computer
A program sent from your computer through the USB-C cable draws mazes on the screen.
-
20-our-own-monitorOur Own Monitor
A monitor in the ROM: from your computer, you look at the memory, change it and run a program.
-
21-a-real-usb-keyboardA Real USB Keyboard
The reports of a real USB keyboard, read through a CH9350 module, on the screen and on your computer.
-
22-from-key-codes-to-charactersFrom Key Codes to Characters
The monitor at the keyboard: characters, Shift and AltGr, keys that repeat, Backspace and a blinking cursor.
-
23-running-the-basic-bill-gates-wroteRunning the BASIC Bill Gates Wrote
Microsoft’s BASIC for the 6502 runs on our computer, started from the monitor with
D000R. -
Volume 2: a BASIC of our own
We write our own BASIC, then SAVE and LOAD on an SD card.
First edition
The book
Our Own 6502, Gate by Gate goes through the same chapters, for those who would rather read than pause a video.
The first volume is complete: twenty-three chapters, 592 pages.
The PDF is on Payhip: if you buy it there, you get every later version of this volume at no extra cost. The paperback is on Amazon.
The code
All of it is free, under the MIT license. Each folder holds the code as it stands at the end of its chapter, with its test benches and its Makefile.
What you need
- A Sipeed Tang Nano 20K and a USB-C cable. From chapter 5 on, an HDMI monitor and its cable.
- The OSS CAD Suite. It is free and it holds every tool used here: Yosys, nextpnr,
gowin_pack, openFPGALoader and Icarus Verilog. It exists for macOS, Linux and Windows. - From chapter 7 on, cc65. It is free too, and it holds the assembler
ca65, the linkerld65and the disassemblerda65. make, and Python 3 from chapter 6 on.- From chapter 21 on, a USB keyboard and the KAmod USB HID Extender from Kamami, a small module with a CH9350 chip, with six header pins, three Dupont wires, a soldering iron and a multimeter.
source ~/oss-cad-suite/environment # once in each terminal
make test # run the test benches in the simulator
make count # build the counter
make flash-count # send it to the board
make keep-count # or write it into the board's permanent memory
Who is writing this
I'm Yves Robert. I teach mathematics and computing at Universidade Aberta, in Portugal, where I took my PhD in computational algebra. I also write novels.