Home / Learn Chip Programming / Project 10: Design Your Own 8-Bit CPU

Project 10: Design Your Own 8-Bit CPU

Build a small CPU in 74HC chips (or Verilog): register, ALU, RAM, program counter and control logic.

In shortBuild a small CPU in 74HC chips (or Verilog): register, ALU, RAM, program counter and control logic.

Theory in plain words

One instructionPC to RAM addressRAM to instruction registerPC + 1decode opcodeexecute (ALU / load / store / out)
One instruction

What it is

A SAP-1 style CPU: 16 bytes of RAM, a 4 bit program counter, accumulator, ALU, output register. A control unit decodes instructions and sets the control lines for each step.

Why we use it

Building a CPU is the best way to understand fetch, decode and execute. Everything in the theory lessons becomes real.

How it works

Each instruction takes fetch (PC to memory address, memory to instruction register) then execute steps. The control logic is a ROM or gates mapping opcode and step to control signals.

Where it is used

  • Computer architecture understanding
  • Interview preparation
  • The base of every processor

Equipment

What you need: equipment list with costTry it Yourself

This build is large. Start with a Verilog or browser simulation. A logic analyzer helps check the bus on a slow clock.

Module plan

Module74HC chipsJob
Program counter74HC161counts addresses 0 to 15
Accumulator and B register74HC273 / 74HC377hold values
ALU adder74HC283 x2 + 74HC86 XORadd and subtract
RAM74HC189 or an AT28C16 / 62256holds program and data
Instruction register74HC377holds the current instruction
Bus buffers74HC245 / 74HC541put values on the shared bus
Control ROMAT28C16 EEPROMmicrocode: opcode + step to control lines
Clock555 or crystal + 74HC161manual step and slow auto clock

Live CPU simulator

Write a program and watch the CPU run itTry it Yourself

This is a real stored-program computer: change the program text and the same hardware does a different job.

Verilog version (same CPU)

sap.v
module sap (input clk, rst, output reg [7:0] out, output reg halt);
  reg [7:0] ram [0:15]; reg [3:0] pc; reg [7:0] a, ir; reg [1:0] st;
  initial $readmem("prog.hex", ram);
  always @(posedge clk) if (rst) begin pc <= 0; a <= 0; st <= 0; halt <= 0; end
  else if (!halt) case (st)
    0: begin ir <= ram[pc]; pc <= pc + 1; st <= 1; end                  // fetch
    1: begin st <= 0;                                                    // execute
         case (ir[7:4])
           4'h1: a <= ram[ir[3:0]];  4'h2: a <= a + ram[ir[3:0]];  4'h3: a <= a - ram[ir[3:0]];
           4'h4: ram[ir[3:0]] <= a;  4'h5: a <= {4'b0, ir[3:0]};   4'h6: pc <= ir[3:0];
           4'h7: if (a == 0) pc <= ir[3:0];  4'hE: out <= a;  4'hF: halt <= 1;
         endcase end
  endcase
endmodule
  1. Run the browser simulator and write a program that adds two numbers.
  2. Port it to the Verilog model and simulate it with a testbench.
  3. Build one module at a time on breadboards (program counter first) and test each with LEDs.
  4. Connect the modules to the shared bus with 74HC245 buffers and test with a slow clock.
  5. Program the control ROM (microcode) and run your first program.
Tip:Resist wiring everything at once. A CPU on breadboards has hundreds of wires: test every module separately and label the wires by colour.

Quick quiz

1. Fetch stage does...

2. JZ jumps when...