# lightcode/8bit-computer architecture

> Architecture diagram of the GitHub repository lightcode/8bit-computer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lightcode/8bit-computer
- Repository: https://github.com/lightcode/8bit-computer

## Overview

#### 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: [`rtl/machine.v`](https://github.com/lightcode/8bit-computer/blob/master/rtl/machine.v) (Machine), [`rtl/cpu.v`](https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu.v) (CPU integration), [`rtl/cpu_control.v`](https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu_control.v) (Microcoded control), [`rtl/library/ram.v`](https://github.com/lightcode/8bit-computer/blob/master/rtl/library/ram.v) (256-byte RAM)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_toolchain["Build &amp; Programs"]
  node_make["Make orchestration<br/>build entry point"]
  node_assembler["Assembler<br/>[asm.py]"]
  node_program["Example assembly<br/>assembly program"]
  node_testbench["Machine testbench<br/>Verilog testbench<br/>[machine_tb.v]"]
  node_gtkwave["GTKWave filters<br/>waveform tooling<br/>[gen-filters.py]"]
end

subgraph group_machine["Machine Integration"]
  node_machine_top["Machine<br/>top-level Verilog<br/>[machine.v]"]
  node_io_bus{{"External I/O bus<br/>I/O boundary<br/>[machine.v]"}}
end

subgraph group_cpu["CPU"]
  node_cpu_top["CPU integration<br/>CPU top level<br/>[cpu.v]"]
  node_control["Microcoded control<br/>decoder and state machine<br/>[cpu_control.v]"]
  node_registers["CPU registers<br/>register file and state<br/>[cpu_registers.v]"]
  node_alu["ALU<br/>arithmetic and flags<br/>[alu.v]"]
  node_parameters["Instruction parameters<br/>RTL definitions<br/>[parameters.v]"]
end

subgraph group_primitives["Memory &amp; Timing"]
  node_ram[("256-byte RAM<br/>memory implementation<br/>[ram.v]")]
  node_tristate["Tri-state bus buffer<br/>shared-bus primitive<br/>[tristate_buffer.v]"]
  node_clock["Staged clock<br/>clock primitive<br/>[clock.v]"]
  node_counter["Counter primitive<br/>sequential primitive<br/>[counter.v]"]
  node_register["Register primitive<br/>sequential primitive<br/>[register.v]"]
end

node_make -->|"builds image with"| node_assembler
node_program -->|"assembly input"| node_assembler
node_assembler -->|"produces memory.list"| node_ram
node_make -->|"runs simulation"| node_machine_top
node_testbench -->|"verifies"| node_machine_top
node_gtkwave -.->|"displays signals for"| node_machine_top
node_machine_top -->|"hosts"| node_cpu_top
node_machine_top -->|"connects"| node_ram
node_machine_top -->|"uses timing"| node_clock
node_machine_top -->|"exposes"| node_io_bus
node_cpu_top -->|"executes under"| node_control
node_cpu_top -->|"reads and writes"| node_registers
node_cpu_top -->|"operates through"| node_alu
node_control -->|"uses opcode definitions"| node_parameters
node_control -->|"selects and enables"| node_registers
node_control -->|"selects operation"| node_alu
node_control -->|"drives memory access"| node_ram
node_cpu_top -->|"uses shared bus"| node_tristate
node_tristate -->|"controls bus ownership"| node_ram
node_tristate -->|"routes I/O access"| node_io_bus
node_clock -->|"clocks"| node_counter
node_clock -->|"clocks"| node_register
node_register -.->|"implements state storage"| node_registers

click node_make "https://github.com/lightcode/8bit-computer/tree/master/Makefile"
click node_assembler "https://github.com/lightcode/8bit-computer/blob/master/asm/asm.py"
click node_program "https://github.com/lightcode/8bit-computer/blob/master/tests/multiplication_test.asm"
click node_testbench "https://github.com/lightcode/8bit-computer/blob/master/rtl/tb/machine_tb.v"
click node_gtkwave "https://github.com/lightcode/8bit-computer/blob/master/gtkwave/gen-filters.py"
click node_machine_top "https://github.com/lightcode/8bit-computer/blob/master/rtl/machine.v"
click node_io_bus "https://github.com/lightcode/8bit-computer/blob/master/rtl/machine.v"
click node_cpu_top "https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu.v"
click node_control "https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu_control.v"
click node_registers "https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu_registers.v"
click node_alu "https://github.com/lightcode/8bit-computer/blob/master/rtl/alu.v"
click node_parameters "https://github.com/lightcode/8bit-computer/blob/master/rtl/parameters.v"
click node_ram "https://github.com/lightcode/8bit-computer/blob/master/rtl/library/ram.v"
click node_tristate "https://github.com/lightcode/8bit-computer/blob/master/rtl/library/tristate_buffer.v"
click node_clock "https://github.com/lightcode/8bit-computer/blob/master/rtl/library/clock.v"
click node_counter "https://github.com/lightcode/8bit-computer/blob/master/rtl/library/counter.v"
click node_register "https://github.com/lightcode/8bit-computer/blob/master/rtl/library/register.v"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_make,node_assembler,node_program,node_testbench,node_gtkwave toneBlue
class node_machine_top,node_io_bus toneAmber
class node_cpu_top,node_control,node_registers,node_alu,node_parameters toneMint
class node_ram,node_tristate,node_clock,node_counter,node_register toneRose
```

## Components

### Build & Programs

- [Make orchestration](https://github.com/lightcode/8bit-computer/tree/master/Makefile): build entry point · Builds images and runs the Verilog simulation. · `Makefile`
- [Assembler](https://github.com/lightcode/8bit-computer/blob/master/asm/asm.py): Python assembler · Translates assembly programs into the RAM image. · `asm/asm.py`
- [Example assembly](https://github.com/lightcode/8bit-computer/blob/master/tests/multiplication_test.asm): assembly program · `tests/multiplication_test.asm`
- [Machine testbench](https://github.com/lightcode/8bit-computer/blob/master/rtl/tb/machine_tb.v): Verilog testbench · `rtl/tb/machine_tb.v`
- [GTKWave filters](https://github.com/lightcode/8bit-computer/blob/master/gtkwave/gen-filters.py): waveform tooling · `gtkwave/gen-filters.py`

### Machine Integration

- [Machine](https://github.com/lightcode/8bit-computer/blob/master/rtl/machine.v): top-level Verilog · Runnable computer integrating CPU, RAM, clocks, reset, and external I/O. · `rtl/machine.v`
- [External I/O bus](https://github.com/lightcode/8bit-computer/blob/master/rtl/machine.v): I/O boundary · Shares the machine bus under memory-versus-I/O control. · `rtl/machine.v`

### CPU

- [CPU integration](https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu.v): CPU top level · Connects control, state, ALU, and the shared internal bus. · `rtl/cpu.v`
- [Microcoded control](https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu_control.v): decoder and state machine · Decodes opcodes and flags into multi-cycle bus, register, memory, and I/O enables. · `rtl/cpu_control.v`
- [CPU registers](https://github.com/lightcode/8bit-computer/blob/master/rtl/cpu_registers.v): register file and state · Holds accumulator, PC, stack pointer, instruction register, and selected CPU state. · `rtl/cpu_registers.v`
- [ALU](https://github.com/lightcode/8bit-computer/blob/master/rtl/alu.v): arithmetic and flags · `rtl/alu.v`
- [Instruction parameters](https://github.com/lightcode/8bit-computer/blob/master/rtl/parameters.v): RTL definitions · `rtl/parameters.v`

### Memory & Timing

- [256-byte RAM](https://github.com/lightcode/8bit-computer/blob/master/rtl/library/ram.v): memory implementation · Stores the assembled program image and CPU data. · `rtl/library/ram.v`
- [Tri-state bus buffer](https://github.com/lightcode/8bit-computer/blob/master/rtl/library/tristate_buffer.v): shared-bus primitive · Models controlled bus ownership. · `rtl/library/tristate_buffer.v`
- [Staged clock](https://github.com/lightcode/8bit-computer/blob/master/rtl/library/clock.v): clock primitive · Provides ordered phases for micro-operations. · `rtl/library/clock.v`
- [Counter primitive](https://github.com/lightcode/8bit-computer/blob/master/rtl/library/counter.v): sequential primitive · `rtl/library/counter.v`
- [Register primitive](https://github.com/lightcode/8bit-computer/blob/master/rtl/library/register.v): sequential primitive · `rtl/library/register.v`

## Connections

- 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
