Yves Robert.

A 6502 computer, written gate by gate.

An old 6502 chip dissolving into light and pouring into a small FPGA board

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.

Watch the series Get the book Get the code

The finished computer

Two and a half minutes to see where the series goes: the machine running Microsoft BASIC, typed on its own keyboard, and the road from one logic gate to there.

  1. The finished computer: Microsoft BASIC on the screen, the keyboard, and the small FPGA board

    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.

    Watch on YouTube

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.

  1. Episode 1: Rebuilding the 6502

    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.

    Watch on YouTube

  2. Episode 2: Two gates can add

    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.

    Watch on YouTube

  3. Episode 3: 131,072 tests, zero errors

    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.

    Watch on YouTube

  4. Episode 4: How chips remember

    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.

    Watch on YouTube

  5. Episode 5: How screens are drawn

    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.

    Watch on YouTube

  6. Episode 6: The clock HDMI needs

    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.

    Watch on YouTube

  7. Episode 7: How HDMI really works

    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.

    Watch on YouTube

  8. Episode 8: Our 6502 video card

    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.

    Watch on YouTube

  9. Episode 9: Our first 6502 program

    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.

    Watch on YouTube

  10. Episode 10: Assembling our first 6502 program

    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.

    Watch on YouTube

  11. Episode 11: ROM, RAM and the bus

    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.

    Watch on YouTube

  12. Episode 12: Our own 6502 in Verilog

    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.

    Watch on YouTube

  13. Episode 13: The ALU of our own 6502

    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.

    Watch on YouTube

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.

  1. 01-box-of-gates

    An FPGA Is a Box of Gates

    An LED driven by two buttons through one gate, then an LED that blinks.

  2. 02-logic-that-computes

    Logic That Computes

    An eight-bit addition done by our gates, in simulation and then on the board.

  3. 03-logic-that-remembers

    Logic That Remembers

    A counter on the six LEDs, then a light that runs back and forth.

  4. 04-how-a-screen-is-drawn

    How a Screen Is Drawn

    A test pattern computed in the simulator and written to a picture file.

  5. 05-driving-an-hdmi-screen

    Driving an HDMI Screen

    The same test pattern on a real monitor.

  6. 06-320-by-200

    320 by 200

    A picture of 320 by 200 pixels in sixteen colors on that monitor.

  7. 07-what-a-processor-really-does

    What a Processor Really Does

    A first program of eleven bytes, assembled by hand and then by an assembler, which gives the same bytes.

  8. 08-memory-the-bus-and-the-first-fetch

    Memory, 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.

  9. 09-the-alu

    The ALU

    The part that computes: thirteen operations and four flags, checked in the simulator.

  10. 10-first-instructions

    First Instructions

    Our processor runs its first program: five instructions, and the six LEDs of the board count.

  11. 11-addressing-modes

    Addressing Modes

    A light two LEDs wide sweeps across the board, read from a table.

  12. 12-arithmetic-compares-and-decimal-mode

    Arithmetic, Compares and Decimal Mode

    A stopwatch that counts the seconds in decimal on the six LEDs.

  13. 13-branches-and-loops

    Branches and Loops

    A loop that computes the Collatz sequence on the six LEDs and stops by itself on 1.

  14. 14-the-stack-and-subroutines

    The Stack and Subroutines

    A subroutine that calls itself makes a bar of light grow and shrink on the six LEDs.

  15. 15-reset-and-interrupts

    Reset and Interrupts

    A light walks along the six LEDs; button S2 interrupts it, and it goes on from where it was.

  16. 16-is-it-really-a-6502

    Is 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. 17-the-processor-draws

    The Processor Draws

    The processor draws 128 straight lines on the screen, then changes the colors of the whole picture.

  18. 18-letters-made-of-pixels

    Letters Made of Pixels

    Text on the screen: a few lines, the 95 characters of our font and 48 lines of slanting stripes.

  19. 19-talking-to-your-computer

    Talking to Your Computer

    A program sent from your computer through the USB-C cable draws mazes on the screen.

  20. 20-our-own-monitor

    Our Own Monitor

    A monitor in the ROM: from your computer, you look at the memory, change it and run a program.

  21. 21-a-real-usb-keyboard

    A Real USB Keyboard

    The reports of a real USB keyboard, read through a CH9350 module, on the screen and on your computer.

  22. 22-from-key-codes-to-characters

    From Key Codes to Characters

    The monitor at the keyboard: characters, Shift and AltGr, keys that repeat, Backspace and a blinking cursor.

  23. 23-running-the-basic-bill-gates-wrote

    Running the BASIC Bill Gates Wrote

    Microsoft’s BASIC for the 6502 runs on our computer, started from the monitor with D000R.

  24. Volume 2: a BASIC of our own

    We write our own BASIC, then SAVE and LOAD on an SD card.

Cover of Our Own 6502, Gate by Gate. Volume 1: The Machine

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.

If you buy it, you get every later version of this volume at no extra cost.

Get the book

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 linker ld65 and the disassembler da65.
  • 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.

Open the repository on GitHub

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
Chapter 3, with the suite unpacked in your home folder.
Yves Robert

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.