A very simple 8 bit cpu made from scratch in verilog, that runs on a de10 lite FPGA board
- SystemVerilog 68.7%
- Nix 21.7%
- Python 8.4%
- Assembly 0.8%
- Tcl 0.4%
| .qsys_edit | ||
| tools | ||
| .gitignore | ||
| alu.sv | ||
| alu_test.sv | ||
| assembler.py | ||
| bcd.sv | ||
| compile_alu | ||
| compile_cpu | ||
| compile_mux | ||
| compile_pc | ||
| compile_ram | ||
| compile_rom | ||
| cpu.pdf | ||
| cpu.qpf | ||
| cpu.qsf | ||
| cpu.qws | ||
| cpu.sdc | ||
| cpu.sv | ||
| cpu_test.sv | ||
| decoder.sv | ||
| modelsim.ini | ||
| mux.sv | ||
| mux_test.sv | ||
| output.bin | ||
| pc.sv | ||
| pc_test.sv | ||
| ram.sv | ||
| ram_test.sv | ||
| README.md | ||
| reg.sv | ||
| reg.sv.bak | ||
| register_file.sv | ||
| rom.sv | ||
| rom_gen.py | ||
| rom_test.sv | ||
| test.asm | ||
| test.bin | ||
Instruction Set
| Binary | Hex | Mnemonic | Format | Notes |
|---|---|---|---|---|
0000 |
0 | NOP | — | all-zero word |
0001 |
1 | MOVI | IMM | new |
0010 |
2 | ADDI | IMM | new |
0011 |
3 | MOV | REG | |
0100 |
4 | ADD | REG | |
0101 |
5 | SUB | REG | |
0110 |
6 | OR | REG | |
0111 |
7 | XOR | REG | |
1000 |
8 | AND | REG | |
1001 |
9 | NOT | R1 | |
1010 |
A | CMP | CMP | |
1011 |
B | BR | BR | JMP/JE/JNE/CALL |
1100 |
C | PUSH | STK | |
1101 |
D | POP | — | always → ACC |
1110 |
E | RET | — | |
1111 |
F | HLT | — |
The four branch mnemonics JMP/JE/JNE/CALL share the 0xB opcode and are told
apart by the cond/link bits.
Instruction Encoding (16-bit instruction word)
| 4 bits | 4 bits | 8 bits |
|---|---|---|
| op | dst/r1 | imm/... |
Registers (4-bit addresses)
| Address | Name | Description |
|---|---|---|
4'h0 |
R1 | First argument of a function |
4'h1 |
R2 | Second argument of a function |
4'h2 |
R3 | General purpose |
4'h3 |
R4 | General purpose |
4'h4 |
R5 | General purpose |
4'h5 |
R6 | General purpose |
4'h6 |
R7 | General purpose |
4'h7 |
R8 | General purpose |
4'h8 |
PC | Program counter (read-only for jumps) |
4'h9 |
SP | Stack pointer |
4'hA |
ACC | Accumulator (target of POP) |
Type IMM — MOVI, ADDI (immediate → dst)
| op (4) | dst (4) | imm (8) |
Type REG — MOV, ADD, SUB, OR, XOR, AND (2-op: src → dst)
| op (4) | dst (4) | src (4) | 0000 (4) |
Both src and dst are register addresses.
Type R1 — NOT (1-op: register)
| op (4) | reg (4) | 00000000 (8) |
Type CMP — CMP (2-op: both registers)
| op (4) | r1 (4) | r2 (4) | 0000 (4) |
Type BR — JMP, JE, JNE, CALL (branch address)
| op (4) | addr (4) | cond (2) | link (1) | 0 (1) |
cond: 00 always (JMP), 01 if Z (JE), 10 if NZ (JNE).
link: 1 = CALL (push PC first), 0 = plain jump.
Type STK — PUSH (1-op)
| op (4) | reg (4) | 00000000 (8) |
No-operand — NOP, RET, POP, HLT
| op (4) | 000000000000 (12) |
HLT may use the low byte as a cycle count: | op (4) | cycles (8) | when
desired.
Examples
movi #0xAB, R3 ; 0xAB → R3
> 0001 0010 10101011 (0x12AB)
addi #0x05, R2 ; R2 + 5 → R2
> 0010 0001 00000101 (0x2105)
mov R1, R2 ; R1 → R2
> 0011 0001 00000000 (0x3100)
add R1, R2 ; R1 + R2 → R2
> 0100 0001 00000000 (0x4100)
not R5
> 1001 0100 00000000 (0x9400)
push R1
> 1100 0000 00000000 (0xC000)
pop
> 1101 0000 00000000 (0xD000)
jmp 0x0C
> 1011 1100 00000000 (0xBC00)
nop
> 0000 0000 00000000 (0x0000)
Hardware Notes
- ALU: Supports ADD, SUB, XOR, OR, AND, NOT. Combinational; drives zero/carry/neg flag capture. Used by the cpu datapath.
- ROM: Generated by
rom_gen.pyfrom a binary file. Stores 16-bit words. - RAM: Used as the stack (and data storage). Synchronous read, 256 x 8.
- Stack: Grows downward. SP starts at top of RAM (reset 0xFF for 256 entries) and decrements on push, increments on pop.
- Register file (
register_file.sv): decodes a 4-bit register address into a one-hotloadselect and a read-portval(PC is read-only for reads). Two instances give a dual-read register file.
Implementation notes
- Immediates:
movi/addiuse the low byte as the immediate;addigoes through the ALU (ADD) so flags are set. - Register ops:
mov/add/sub/or/xor/and/notdecodedstandsrc(4-bit register addrs).sub/cmpfeed ALUA=src, B=dstsosrc-dst; othersA=dst, B=src.notisNOT_A. - Flags: ALU zero/neg/carry captured on ALU-writing ops and
cmp. Branch conditions use the captured Z. - Branches (
0xB, cond inimm[3:2], link inimm[1]): JMP/CALL = always, JE = Z, JNE = ~Z. CALL pushesPC+1and takes 2 cycles; JMP/JE/JNE take 1. - Stack ops:
pushwritesSP-1and decrements SP;popreads at SP, increments SP, and loads ACC on a second cycle;retloads PC fromram[SP]on a second cycle. - PC write-protect: register writes never target
dst == PC (0x8);jmp_addris{4'b0, dst[3:0]}so branches address up to 256 words. - Halt (
0xF, count in low byte): stalls PC forimmcycles.
DE10-Lite board mapping
| Board resource | Signal | Function |
|---|---|---|
CLOCK_50 (PIN_P11) |
clk |
50 MHz CPU clock |
KEY[1] (PIN_A7) |
key1 |
reset, active low |
KEY[0] (PIN_B8) |
key0 |
single-step button, active low |
SW[0] |
sw[0] |
0 = full speed, 1 = slow/step |
SW[1] |
sw[1] |
with SW[0]=1: 0 = 1 Hz auto-run, 1 = step on KEY[0] |
SW[9:2] |
sw[9:2] |
8-bit data input read by the program |
HEX5,HEX4 |
r2_hi,r2_lo |
R2 high/low nibble |
HEX3,HEX2 |
r1_hi,r1_lo |
R1 high/low nibble |
HEX1,HEX0 |
alu_hi,alu_lo |
latched ALU result high/low nibble |
LEDR[2:0] |
led[2:0] |
Z, N, C flags |
LEDR[3] |
led[3] |
halted |
LEDR[7:4] |
led[7:4] |
opcode of the last instruction |
LEDR[8] |
led[8] |
1 = out of reset |
LEDR[9] |
led[9] |
~1.5 Hz heartbeat |
Switch input port
ROM address 0x02 is a virtual I/O port. When the PC is there the CPU
forces the instruction to movi r2, SW[9:2], so a program can read the
slide switches by jumping to address 2. The demo program in test.asm
uses this to add the switch value to R1 once per loop.
Demo program
movi #0x00, r1
movi #0x01, r2
movi #0x00, r2 ; becomes "movi r2, SW[9:2]" at runtime
add r2, r1 ; R1 = R1 + R2
jmp 2
With SW[0]=1, SW[1]=0 the display counts up once per second by the
value on SW[9:2].