# jnr-10/software-cpu architecture

> Architecture diagram of the GitHub repository jnr-10/software-cpu, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jnr-10/software-cpu
- Repository: https://github.com/jnr-10/software-cpu

## Overview

1. **What this repository is**
   - This is a C++ project that implements a small software CPU ecosystem: an **assembler**, a **CPU emulator**, sample **assembly programs**, and a **test/trace toolchain** around them.
   - The architecture is split into two major concerns reflected in the docs: **CPU design** and **program layout/execution**.

2. **Primary execution pipeline**
   - The main data flow is: **assembly source files** in `src/programs/` or `tests/assembly/` → **assembler** in `src/assembler/` → **machine code / loadable program** → **emulator CPU** in `src/emulator/` → optional **trace output** for visualization.
   - This is the key boundary to diagram: source-level program authoring is separated from execution by the assembler.

3. **Assembler subsystem**
   - `src/assembler/assembler.cpp|hpp` is the front end that translates the custom assembly language into the CPU’s instruction format.
   - It is exercised heavily by `tests/test_assembler.cpp` and by assembly-focused test fixtures under `tests/assembly/`.
   - The assembler is a distinct module, not embedded in the emulator, which makes the compile/execute boundary explicit.

4. **Emulator / CPU subsystem**
   - `src/emulator/cpu.cpp|hpp` is the control unit that fetches, decodes, and executes instructions.
   - The CPU depends on lower-level execution components:
     - `alu.cpp|hpp` for arithmetic/logic operations
     - `registers.cpp|hpp` for register state
     - `memory.cpp|hpp` for addressable storage
   - `src/main.cpp` is the top-level entry point that likely wires these pieces together for running a program.

5. **State model and hardware-like boundaries**
   - The emulator is organized like a simplified hardware stack: **register file**, **ALU**, **memory**, and **CPU orchestration** are separate modules.
   - This separation is important for architecture diagrams because it mirrors a real processor boundary: control logic in CPU, computation in ALU, persistent state in registers/memory.

6. **Tracing and observability**
   - `src/emulator/trace_recorder.cpp|hpp` captures execution traces from the emulator.
   - Traces are consumed by the `trace_viewer/` browser UI (`index.html`, `viewer.js`, `style.css`), which is an external visualization boundary rather than part of the CPU runtime.
   - `scripts/generate_trace.sh` and `scripts/run_general_with_trace.sh` suggest trace generation is a first-class workflow.

7. **Program assets and examples**
   - `src/programs/` contains example inputs that demonstrate the ISA and runtime behavior: `hello_world.asm`, `factorial.asm`, `fibonacci.asm`, `math.asm`, `timer_example.asm`, and others.
   - `factorial.c` and `factorial_logic.txt` appear to document or compare high-level logic against the assembly implementation.
   - These examples serve as both demos and reference workloads for the emulator.

8. **Testing strategy**
   - There are two complementary test layers:
     - **C++ unit tests** in `tests/test_alu.cpp`, `tests/test_memory.cpp`, `tests/test_cpu.cpp`, `tests/test_assembler.cpp`
     - **Assembly-level integration tests** in `tests/assembly/`
   - The assembly tests validate instruction semantics, branching/flags, stack behavior, string handling, multiplication, literals, and error cases, so they cover the full assembler-to-emulator path.

9. **Architecture documentation as design contract**
   - `docs/architecture/ISA.md`, `memory_map.md`, and `microarchitecture.md` define the machine’s contract: instruction set, memory layout, and execution model.
   - These docs are the authoritative boundary between “what the software CPU is supposed to do” and the C++ code that implements it.

10. **Build and run tooling**
   - `Makefile` is the main build orchestrator, with helper scripts in `scripts/` for building, running sample programs, and producing traces.
   - `demo.sh` likely provides a convenience demo workflow.
   - The tooling layer is important because it connects the assembler, emulator, and trace viewer into one reproducible workflow.

11. **Repository structure at a glance**
   - `src/` contains implementation.
   - `tests/` validates behavior.
   - `src/programs/` contains inputs/demos.
   - `docs/` contains the architecture spec and phase deliverables.
   - `trace_viewer/` contains the UI for execution traces.

12. **How to draw the architecture**
   - A good diagram would show:
     - authored assembly programs and test fixtures on the left,
     - the assembler in the middle as the compilation boundary,
     - the emulator core on the right with CPU/ALU/registers/memory as internal subsystems,
     - and a side channel from the emulator to trace recording and the browser-based viewer.
   - The main distinction is between **static program preparation**, **runtime execution**, and **post-run inspection**.

Main entry points: [`src/emulator/cpu.cpp`](https://github.com/jnr-10/software-cpu/blob/main/src/emulator/cpu.cpp) (CPU), [`src/assembler/assembler.cpp`](https://github.com/jnr-10/software-cpu/blob/main/src/assembler/assembler.cpp) (Assembler), [`tests/assembly`](https://github.com/jnr-10/software-cpu/tree/main/tests/assembly) (Assembly Suite), [`src/main.cpp`](https://github.com/jnr-10/software-cpu/blob/main/src/main.cpp) (Main)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_src["Source"]
  node_node_src_main["Main<br/>entrypoint<br/>[main.cpp]"]
  node_node_assembler["Assembler<br/>compiler<br/>[assembler.cpp]"]
  node_node_cpu["CPU<br/>executor<br/>[cpu.cpp]"]
  node_node_alu["ALU<br/>compute core<br/>[alu.cpp]"]
  node_node_registers["Registers<br/>state store<br/>[registers.cpp]"]
  node_node_memory["Memory<br/>state store<br/>[memory.cpp]"]
  node_node_trace_recorder["Trace Recorder<br/>observability<br/>[trace_recorder.cpp]"]
  node_node_programs["Sample Programs<br/>assembly workloads"]
end

subgraph group_group_tests["Tests"]
  node_node_test_assembler["Asm Tests<br/>unit tests<br/>[test_assembler.cpp]"]
  node_node_test_cpu["CPU Tests<br/>unit tests<br/>[test_cpu.cpp]"]
  node_node_test_alu_memory["Core Tests<br/>unit tests<br/>[test_alu.cpp]"]
  node_node_asm_integration["Assembly Suite<br/>integration tests"]
end

subgraph group_group_docs["Docs"]
  node_node_isa["ISA Spec<br/>architecture doc<br/>[ISA.md]"]
  node_node_memory_map["Memory Map<br/>architecture doc<br/>[memory_map.md]"]
  node_node_microarch["Microarchitecture<br/>architecture doc"]
end

subgraph group_group_tools["Tooling"]
  node_node_build["Makefile<br/>build system"]
  node_node_scripts["Scripts<br/>run tooling"]
  node_node_demo["Demo<br/>workflow<br/>[demo.sh]"]
end

subgraph group_group_viewer["Trace Viewer"]
  node_node_viewer["Trace UI<br/>web viewer"]
end

node_node_src_main -->|"invokes"| node_node_assembler
node_node_src_main -->|"runs on"| node_node_cpu
node_node_assembler -->|"implements"| node_node_isa
node_node_assembler -->|"compiles"| node_node_programs
node_node_cpu -->|"uses"| node_node_alu
node_node_cpu -->|"uses"| node_node_registers
node_node_cpu -->|"uses"| node_node_memory
node_node_cpu -->|"emits"| node_node_trace_recorder
node_node_cpu -->|"guided by"| node_node_microarch
node_node_memory -->|"constrained by"| node_node_memory_map
node_node_test_assembler -->|"verifies"| node_node_assembler
node_node_test_cpu -->|"verifies"| node_node_cpu
node_node_test_alu_memory -->|"verifies"| node_node_alu
node_node_test_alu_memory -->|"verifies"| node_node_memory
node_node_asm_integration -->|"runs"| node_node_programs
node_node_asm_integration -->|"exercises"| node_node_assembler
node_node_asm_integration -->|"exercises"| node_node_cpu
node_node_trace_recorder -->|"feeds"| node_node_viewer
node_node_build -->|"builds"| node_node_src_main
node_node_scripts -->|"orchestrates"| node_node_build
node_node_scripts -->|"generates"| node_node_trace_recorder
node_node_demo -->|"runs"| node_node_programs
node_node_demo -->|"demonstrates"| node_node_cpu

click node_node_src_main "https://github.com/jnr-10/software-cpu/blob/main/src/main.cpp"
click node_node_assembler "https://github.com/jnr-10/software-cpu/blob/main/src/assembler/assembler.cpp"
click node_node_cpu "https://github.com/jnr-10/software-cpu/blob/main/src/emulator/cpu.cpp"
click node_node_alu "https://github.com/jnr-10/software-cpu/blob/main/src/emulator/alu.cpp"
click node_node_registers "https://github.com/jnr-10/software-cpu/blob/main/src/emulator/registers.cpp"
click node_node_memory "https://github.com/jnr-10/software-cpu/blob/main/src/emulator/memory.cpp"
click node_node_trace_recorder "https://github.com/jnr-10/software-cpu/blob/main/src/emulator/trace_recorder.cpp"
click node_node_programs "https://github.com/jnr-10/software-cpu/tree/main/src/programs"
click node_node_test_assembler "https://github.com/jnr-10/software-cpu/blob/main/tests/test_assembler.cpp"
click node_node_test_cpu "https://github.com/jnr-10/software-cpu/blob/main/tests/test_cpu.cpp"
click node_node_test_alu_memory "https://github.com/jnr-10/software-cpu/blob/main/tests/test_alu.cpp"
click node_node_asm_integration "https://github.com/jnr-10/software-cpu/tree/main/tests/assembly"
click node_node_isa "https://github.com/jnr-10/software-cpu/blob/main/docs/architecture/ISA.md"
click node_node_memory_map "https://github.com/jnr-10/software-cpu/blob/main/docs/architecture/memory_map.md"
click node_node_microarch "https://github.com/jnr-10/software-cpu/blob/main/docs/architecture/microarchitecture.md"
click node_node_build "https://github.com/jnr-10/software-cpu/tree/main/Makefile"
click node_node_scripts "https://github.com/jnr-10/software-cpu/tree/main/scripts"
click node_node_demo "https://github.com/jnr-10/software-cpu/blob/main/demo.sh"
click node_node_viewer "https://github.com/jnr-10/software-cpu/tree/main/trace_viewer"

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_node_src_main,node_node_assembler,node_node_cpu,node_node_alu,node_node_registers,node_node_memory,node_node_trace_recorder,node_node_programs toneBlue
class node_node_test_assembler,node_node_test_cpu,node_node_test_alu_memory,node_node_asm_integration toneAmber
class node_node_isa,node_node_memory_map,node_node_microarch toneMint
class node_node_build,node_node_scripts,node_node_demo toneRose
class node_node_viewer toneIndigo
```

## Components

### Source

Core implementation of the assembler, emulator, and sample programs.

- [Main](https://github.com/jnr-10/software-cpu/blob/main/src/main.cpp): entrypoint · Top-level executable entry point that wires the assembler and emulator together. · `src/main.cpp`
- [Assembler](https://github.com/jnr-10/software-cpu/blob/main/src/assembler/assembler.cpp): compiler · Translates custom assembly source into machine-code/instruction form. · `src/assembler/assembler.cpp`
- [CPU](https://github.com/jnr-10/software-cpu/blob/main/src/emulator/cpu.cpp): executor · Fetch-decode-execute control unit for running instructions. · `src/emulator/cpu.cpp`
- [ALU](https://github.com/jnr-10/software-cpu/blob/main/src/emulator/alu.cpp): compute core · Performs arithmetic and logic operations for the CPU. · `src/emulator/alu.cpp`
- [Registers](https://github.com/jnr-10/software-cpu/blob/main/src/emulator/registers.cpp): state store · Holds CPU register state and flags. · `src/emulator/registers.cpp`
- [Memory](https://github.com/jnr-10/software-cpu/blob/main/src/emulator/memory.cpp): state store · Implements addressable memory for instructions and data. · `src/emulator/memory.cpp`
- [Trace Recorder](https://github.com/jnr-10/software-cpu/blob/main/src/emulator/trace_recorder.cpp): observability · Captures execution traces emitted by the emulator. · `src/emulator/trace_recorder.cpp`
- [Sample Programs](https://github.com/jnr-10/software-cpu/tree/main/src/programs): assembly workloads · Reference assembly inputs and example workloads for the CPU. · `src/programs`

### Tests

Unit and assembly-level validation of the CPU system.

- [Asm Tests](https://github.com/jnr-10/software-cpu/blob/main/tests/test_assembler.cpp): unit tests · C++ tests covering assembler behavior. · `tests/test_assembler.cpp`
- [CPU Tests](https://github.com/jnr-10/software-cpu/blob/main/tests/test_cpu.cpp): unit tests · C++ tests covering CPU execution behavior. · `tests/test_cpu.cpp`
- [Core Tests](https://github.com/jnr-10/software-cpu/blob/main/tests/test_alu.cpp): unit tests · C++ tests covering ALU and memory modules. · `tests/test_alu.cpp`
- [Assembly Suite](https://github.com/jnr-10/software-cpu/tree/main/tests/assembly): integration tests · Assembly-level tests spanning assembler and emulator semantics. · `tests/assembly`

### Docs

Architecture contract and design notes.

- [ISA Spec](https://github.com/jnr-10/software-cpu/blob/main/docs/architecture/ISA.md): architecture doc · Instruction set contract for the software CPU. · `docs/architecture/ISA.md`
- [Memory Map](https://github.com/jnr-10/software-cpu/blob/main/docs/architecture/memory_map.md): architecture doc · Defines the machine's memory layout and address boundaries. · `docs/architecture/memory_map.md`
- [Microarchitecture](https://github.com/jnr-10/software-cpu/blob/main/docs/architecture/microarchitecture.md): architecture doc · Describes the execution model and component responsibilities. · `docs/architecture/microarchitecture.md`

### Tooling

Build, run, trace generation, and demo entrypoints.

- [Makefile](https://github.com/jnr-10/software-cpu/tree/main/Makefile): build system · Primary build orchestration for the project. · `Makefile`
- [Scripts](https://github.com/jnr-10/software-cpu/tree/main/scripts): run tooling · Helper scripts for builds, execution, and trace generation. · `scripts`
- [Demo](https://github.com/jnr-10/software-cpu/blob/main/demo.sh): workflow · Convenience wrapper for running a showcase workflow. · `demo.sh`

### Trace Viewer

Browser UI for inspecting execution traces.

- [Trace UI](https://github.com/jnr-10/software-cpu/tree/main/trace_viewer): web viewer · Browser-based interface for viewing execution traces. · `trace_viewer`

## Connections

- Main → Assembler: invokes · Main uses the assembler to prepare assembly sources for execution.
- Main → CPU: runs on · Main starts the emulator CPU runtime.
- Assembler → ISA Spec: implements · Assembler follows the instruction-set contract.
- Assembler → Sample Programs: compiles · Example programs are input material for assembly.
- CPU → ALU: uses · CPU delegates arithmetic and logic operations to the ALU.
- CPU → Registers: uses · CPU reads and writes register state while executing instructions.
- CPU → Memory: uses · CPU fetches instructions and accesses program data through memory.
- CPU → Trace Recorder: emits · CPU execution can be recorded as traces.
- CPU → Microarchitecture: guided by · CPU behavior is defined by the microarchitecture contract.
- Memory → Memory Map: constrained by · Memory implementation follows the documented map.
- Asm Tests → Assembler: verifies · Unit tests validate assembler translation behavior.
- CPU Tests → CPU: verifies · Unit tests validate CPU execution behavior.
- Core Tests → ALU: verifies · ALU tests check arithmetic and logic operations.
- Core Tests → Memory: verifies · Memory tests check storage and addressing behavior.
- Assembly Suite → Sample Programs: runs · Assembly tests use source fixtures and workloads.
- Assembly Suite → Assembler: exercises · Assembly tests validate compilation of assembly inputs.
- Assembly Suite → CPU: exercises · Integration tests validate runtime semantics on the emulator.
- Trace Recorder → Trace UI: feeds · Recorded traces are consumed by the browser viewer.
- Makefile → Main: builds · Makefile builds the main executable and supporting components.
- Scripts → Makefile: orchestrates · Scripts wrap build and run workflows.
- Scripts → Trace Recorder: generates · Trace scripts produce execution trace artifacts.
- Demo → Sample Programs: runs · Demo workflows execute example programs.
- Demo → CPU: demonstrates · Demo workflows showcase emulator execution.
