Our Own 6502, Gate by Gate. Volume 1: The Machine
Build a complete computer in an FPGA: write the processor, drive a screen, read a keyboard, and give it its own BASIC.
This is the book of the Gate by Gate video series. It starts with a single gate and a blinking light on a small FPGA board, the Sipeed Tang Nano 20K. This first volume goes on until that board is a 6502 computer that shows its picture on an HDMI monitor, reads a USB keyboard and runs the BASIC that Microsoft wrote for the 6502. The processor is our own, written line by line in Verilog. Every circuit is explained, tested in the simulator, and then run on the real board.
What is in this volume
I wrote this book while the machine was being built. A chapter went in only when what it builds had run on my board. This first edition holds the whole volume, twenty-three chapters, 592 pages:
- An FPGA Is a Box of Gates: the board, the free tools, a first gate, a blinking light.
- Logic That Computes: gates that add, test benches, and an eight-bit adder checked on all 131,072 of its additions.
- Logic That Remembers: the flip-flop, registers, counters, and a first state machine.
- How a Screen Is Drawn: the raster of 640 by 480, a test pattern, the pixel clock.
- Driving an HDMI Screen: the encoder, the serializer, and the test pattern on a real monitor.
- 320 by 200: a picture in memory, sixteen colors, the display our computer will have.
- What a Processor Really Does: a first program of eleven bytes, assembled by hand and then by an assembler.
- Memory, the Bus and the First Fetch: a ROM, a RAM, a bus, and the first piece of the processor, which fetches those bytes one by one.
- The ALU: the part that computes, thirteen operations and four flags.
- First Instructions: our processor runs its first program, five instructions, and the six LEDs of the board count.
- Addressing Modes: the index registers X and Y, the zero page, indexed and indirect addresses, forty-seven opcodes carried out cycle by cycle.
- Arithmetic, Compares and Decimal Mode: the rest of the arithmetic, the comparisons, the shifts, and the decimal mode.
- Branches and Loops: the eight branches, and the loops they make possible.
- The Stack and Subroutines: the stack, JSR and RTS, and a subroutine that calls itself.
- Reset and Interrupts: the reset vector, IRQ, NMI and BRK; a light walks along the six LEDs, and a button interrupts it.
- Is It Really a 6502?: our processor passes the functional test of Klaus Dormann, on the board, at 13.5 million cycles a second.
- The Processor Draws: the picture moves into the processor's memory, and a program draws 128 straight lines.
- Letters Made of Pixels: a font of 95 characters, and a console of 40 columns and 25 rows.
- Talking to Your Computer: a serial link through the USB-C cable, and a program sent from your computer.
- Our Own Monitor: a program in the ROM that shows the memory, changes it and runs a program, typed from your computer.
- A Real USB Keyboard: a USB keyboard, read through a small module with a CH9350 chip.
- From Key Codes to Characters: keyboard layouts, Shift and AltGr, keys that repeat, a real Backspace and a blinking cursor.
- Running the BASIC Bill Gates Wrote: the BASIC that Microsoft wrote for the 6502 runs on our machine.
Volume 2, where we write a BASIC of our own, comes next.
If you buy it, you get every later version of this volume at no extra cost.
What you need
- A Sipeed Tang Nano 20K board and a USB-C cable. I paid thirty-four euros for my board. From chapter 5 on, an HDMI monitor.
- From chapter 21 on, a USB keyboard and the KAmod USB HID Extender from Kamami, a small module with a CH9350 chip, with a few header pins, wires and a soldering iron.
- A computer. The tools are free and exist for macOS, Linux and Windows. I work on a Mac, and I have not yet run the chapters on Linux or on Windows myself. The book says so where it matters.
- You can program, in any language. You do not need to know Verilog or assembly language.
The code
All the code of the book is free, under the MIT license: https://github.com/yvesrob/6502-gate-by-gate
Format: PDF, 7 by 10 inches.