How jnr-10/software-cpu works
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: cpu.cpp CPU · assembler.cpp Assembler · assembly Assembly Suite · main.cpp Main
Components 19
Source
Core implementation of the assembler, emulator, and sample programs.
- Main · Top-level executable entry point that wires the assembler and emulator together.
src/main.cpp - Assembler · Translates custom assembly source into machine-code/instruction form.
src/assembler/assembler.cpp - CPU · Fetch-decode-execute control unit for running instructions.
src/emulator/cpu.cpp - ALU · Performs arithmetic and logic operations for the CPU.
src/emulator/alu.cpp - Registers · Holds CPU register state and flags.
src/emulator/registers.cpp - Memory · Implements addressable memory for instructions and data.
src/emulator/memory.cpp - Trace Recorder · Captures execution traces emitted by the emulator.
src/emulator/trace_recorder.cpp - Sample Programs · Reference assembly inputs and example workloads for the CPU.
src/programs
Tests
Unit and assembly-level validation of the CPU system.
- Asm Tests · C++ tests covering assembler behavior.
tests/test_assembler.cpp - CPU Tests · C++ tests covering CPU execution behavior.
tests/test_cpu.cpp - Core Tests · C++ tests covering ALU and memory modules.
tests/test_alu.cpp - Assembly Suite · Assembly-level tests spanning assembler and emulator semantics.
tests/assembly
Docs
Architecture contract and design notes.
- ISA Spec · Instruction set contract for the software CPU.
docs/architecture/ISA.md - Memory Map · Defines the machine's memory layout and address boundaries.
docs/architecture/memory_map.md - Microarchitecture · Describes the execution model and component responsibilities.
docs/architecture/microarchitecture.md
Tooling
Build, run, trace generation, and demo entrypoints.
Trace Viewer
Browser UI for inspecting execution traces.
- Trace UI · Browser-based interface for viewing execution traces.
trace_viewer
Connections 23
- 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.