How lightcode/8bit-computer works
Purpose and Entry Points
This is a simulation-oriented 8-bit Verilog computer: a CPU, 256-byte RAM, and assembly toolchain. Programs are assembled into memory.list, then loaded by the simulated machine. Primary entry points are asm/asm.py for assembly and Makefile for build/run orchestration; the top-level hardware integration is rtl/machine.v.
System Integration and Runtime
rtl/machine.v composes the CPU, RAM, clocks, reset behavior, and externally visible I/O bus into the runnable computer. The design runs in a Verilog simulator invoked through make run; GTKWave support is provided through generated signal-display filters under gtkwave/. There are no network services or deployment infrastructure dependencies.
Paths: rtl/machine.v, Makefile, gtkwave/gen-filters.py.
CPU Datapath
The CPU datapath centers on an accumulator/register file, ALU, program counter, stack pointer, instruction register, and shared internal bus. rtl/cpu.v integrates control and datapath. rtl/cpu_registers.v owns CPU state and register-selection/storage behavior, while rtl/alu.v performs arithmetic, logical operations, comparison, and flag production.
Paths: rtl/cpu.v, rtl/cpu_registers.v, rtl/alu.v.
Microcoded Control and Instruction Execution
rtl/cpu_control.v implements the instruction decoder and multi-cycle control state machine. Every instruction starts with PC and instruction fetch, then asserts state-specific control signals for ALU execution/store, immediate loads, memory moves, jumps, calls/returns, stack operations, halt, and I/O. This is the principal control boundary: decoded opcode and flags in, bus/register/memory-enable signals out.
Paths: rtl/cpu_control.v, rtl/cpu.v, rtl/parameters.v.
Memory, Bus, and I/O Boundary
The machine uses a 256-byte RAM implementation. CPU memory instructions use address/data/control signals to read or write this RAM; in and out reuse the bus but route access through a memory-versus-I/O control boundary. Tri-state buffering models shared-bus ownership, preventing all components from directly driving the bus simultaneously.
Paths: rtl/library/ram.v, rtl/library/tristate_buffer.v, rtl/machine.v.
Timing Model and Sequential Primitives
The design derives staged clocks for CPU-cycle, memory, and internal-register operations, allowing a micro-operation to place data on the bus and another to latch it in an ordered phase. Reusable RTL primitives implement clock generation, counters, and registers. Timing behavior is therefore architectural rather than purely combinational.
Paths: rtl/library/clock.v, rtl/library/counter.v, rtl/library/register.v.
Program Build and Verification Flow
Assembly source programs are translated by the Python assembler into the RAM initialization image consumed by simulation. Example programs exercise ALU behavior, moves, calls, multiplication, and I/O. Verilog testbenches validate machine-level and selected component behavior; the Bats suite likely drives end-to-end assembly/simulation checks.
Paths: asm/asm.py, tests/multiplication_test.asm, rtl/tb/machine_tb.v.
Main entry points: machine.v Machine · cpu.v CPU integration · cpu_control.v Microcoded control · ram.v 256-byte RAM
Components 17
Build & Programs
- Make orchestration · Builds images and runs the Verilog simulation.
Makefile - Assembler · Translates assembly programs into the RAM image.
asm/asm.py - Example assembly
tests/multiplication_test.asm - Machine testbench
rtl/tb/machine_tb.v - GTKWave filters
gtkwave/gen-filters.py
Machine Integration
- Machine · Runnable computer integrating CPU, RAM, clocks, reset, and external I/O.
rtl/machine.v - External I/O bus · Shares the machine bus under memory-versus-I/O control.
rtl/machine.v
CPU
- CPU integration · Connects control, state, ALU, and the shared internal bus.
rtl/cpu.v - Microcoded control · Decodes opcodes and flags into multi-cycle bus, register, memory, and I/O enables.
rtl/cpu_control.v - CPU registers · Holds accumulator, PC, stack pointer, instruction register, and selected CPU state.
rtl/cpu_registers.v - ALU
rtl/alu.v - Instruction parameters
rtl/parameters.v
Memory & Timing
- 256-byte RAM · Stores the assembled program image and CPU data.
rtl/library/ram.v - Tri-state bus buffer · Models controlled bus ownership.
rtl/library/tristate_buffer.v - Staged clock · Provides ordered phases for micro-operations.
rtl/library/clock.v - Counter primitive
rtl/library/counter.v - Register primitive
rtl/library/register.v
Connections 23
- Make orchestration → Assembler: builds image with
- Example assembly → Assembler: assembly input
- Assembler → 256-byte RAM: produces memory.list
- Make orchestration → Machine: runs simulation
- Machine testbench → Machine: verifies
- GTKWave filters → Machine: displays signals for
- Machine → CPU integration: hosts
- Machine → 256-byte RAM: connects
- Machine → Staged clock: uses timing
- Machine → External I/O bus: exposes
- CPU integration → Microcoded control: executes under
- CPU integration → CPU registers: reads and writes
- CPU integration → ALU: operates through
- Microcoded control → Instruction parameters: uses opcode definitions
- Microcoded control → CPU registers: selects and enables
- Microcoded control → ALU: selects operation
- Microcoded control → 256-byte RAM: drives memory access
- CPU integration → Tri-state bus buffer: uses shared bus
- Tri-state bus buffer → 256-byte RAM: controls bus ownership
- Tri-state bus buffer → External I/O bus: routes I/O access
- Staged clock → Counter primitive: clocks
- Staged clock → Register primitive: clocks
- Register primitive → CPU registers: implements state storage