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%
Find a file
2026-11-03 22:24:53 +01:00
.qsys_edit Add Qsys editor settings 2026-09-07 19:54:55 +02:00
tools Add Quartus Nix build tooling 2026-11-02 20:35:05 +01:00
.gitignore Ignore Quartus build artifacts 2026-11-03 22:20:24 +01:00
alu.sv Add ALU 2026-09-07 19:33:48 +02:00
alu_test.sv Add ALU testbench 2026-09-07 19:47:49 +02:00
assembler.py Add assembler and example output 2026-09-07 21:30:43 +02:00
bcd.sv Add BCD to 7-segment decoder 2026-11-02 20:32:06 +01:00
compile_alu Add ALU compile script 2026-06-02 23:22:13 +02:00
compile_cpu Add full CPU compile script 2026-09-08 21:41:47 +02:00
compile_mux Add 2:1 multiplexer 2026-06-03 14:03:54 +02:00
compile_pc Add program counter testbench 2026-07-21 23:39:26 +02:00
compile_ram Add RAM compile script 2026-06-03 18:30:31 +02:00
compile_rom Add ROM compile script 2026-06-04 01:03:21 +02:00
cpu.pdf Add CPU block diagram 2026-09-07 19:57:55 +02:00
cpu.qpf Add Quartus project file 2026-07-22 04:29:02 +02:00
cpu.qsf Add Quartus pin assignments 2026-11-02 22:02:00 +01:00
cpu.qws Add Quartus workspace 2026-11-03 09:50:08 +01:00
cpu.sdc Add timing constraints 2026-11-02 21:22:04 +01:00
cpu.sv Add CPU top level 2026-11-02 21:22:04 +01:00
cpu_test.sv Add CPU testbench 2026-11-02 20:27:13 +01:00
decoder.sv Add instruction decoder 2026-09-07 16:07:20 +02:00
modelsim.ini Add ModelSim configuration 2026-11-02 20:32:06 +01:00
mux.sv Add 2:1 multiplexer 2026-06-03 14:03:54 +02:00
mux_test.sv Add multiplexer testbench 2026-06-03 18:30:02 +02:00
output.bin Add assembler and example output 2026-09-07 21:30:43 +02:00
pc.sv Add program counter 2026-11-02 21:22:04 +01:00
pc_test.sv Add program counter testbench 2026-07-21 23:39:26 +02:00
ram.sv Add RAM module 2026-06-04 21:45:59 +02:00
ram_test.sv Add RAM testbench 2026-06-04 21:46:19 +02:00
README.md Add README (description of the project) 2026-11-03 22:24:53 +01:00
reg.sv Add register module 2026-09-07 19:45:18 +02:00
reg.sv.bak Add register backup file 2026-07-22 04:33:12 +02:00
register_file.sv Add register file 2026-09-07 19:33:43 +02:00
rom.sv Add ROM generator and test program 2026-11-02 20:26:27 +01:00
rom_gen.py Add ROM generator and test program 2026-11-02 20:26:27 +01:00
rom_test.sv Add ROM generator and test program 2026-11-02 20:26:27 +01:00
test.asm Add ROM generator and test program 2026-11-02 20:26:27 +01:00
test.bin Add ROM generator and test program 2026-11-02 20:26:27 +01:00

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.py from 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-hot load select and a read-port val (PC is read-only for reads). Two instances give a dual-read register file.

Implementation notes

  • Immediates: movi/addi use the low byte as the immediate; addi goes through the ALU (ADD) so flags are set.
  • Register ops: mov/add/sub/or/xor/and/not decode dst and src (4-bit register addrs). sub/cmp feed ALU A=src, B=dst so src-dst; others A=dst, B=src. not is NOT_A.
  • Flags: ALU zero/neg/carry captured on ALU-writing ops and cmp. Branch conditions use the captured Z.
  • Branches (0xB, cond in imm[3:2], link in imm[1]): JMP/CALL = always, JE = Z, JNE = ~Z. CALL pushes PC+1 and takes 2 cycles; JMP/JE/JNE take 1.
  • Stack ops: push writes SP-1 and decrements SP; pop reads at SP, increments SP, and loads ACC on a second cycle; ret loads PC from ram[SP] on a second cycle.
  • PC write-protect: register writes never target dst == PC (0x8); jmp_addr is {4'b0, dst[3:0]} so branches address up to 256 words.
  • Halt (0xF, count in low byte): stalls PC for imm cycles.

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].