# udhayan27-dev/lc-3 architecture

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

- Interactive diagram: https://gitdiagram.com/udhayan27-dev/lc-3
- Repository: https://github.com/udhayan27-dev/lc-3

## Overview

#### Purpose and Entry Point

This is a Rust LC-3 virtual machine that loads compiled big-endian `.obj` bytecode and executes it interactively in a terminal. The process entry point is `src/main.rs`, which accepts one program path, loads it into VM memory, configures terminal behavior, and starts execution.

Examples are precompiled guest programs, including interactive games: `examples/hello-world.obj`, `examples/2048.obj`, and `examples/rogue.obj`.

#### Process Bootstrap and Program Loading

`src/main.rs` owns host-facing startup:
- validates the single object-file argument;
- reads the object format: first `u16` is the origin/load address, remaining words are sequential memory contents;
- constructs/initializes VM state and transfers control to the execution loop;
- manages raw versus cooked terminal mode, so cleanup must occur when the guest halts or the process exits.

The object loader is a boundary between host filesystem bytes and LC-3 word-addressed memory.

#### VM State and Memory Boundary

`src/hardware/vm/mod.rs` represents machine-level state: 64 KiB addressable `u16` memory plus behavior for memory access and keyboard polling. It is the architectural boundary where normal memory operations gain device semantics.

Reads of LC-3 keyboard-mapped addresses (`KBSR`/`KBDR`) interact with host stdin rather than behaving like ordinary RAM. This lets both instructions and guest programs use LC-3 memory-mapped I/O without knowing host terminal details.

#### Register File and CPU Status

`src/hardware/register/mod.rs` defines the LC-3 register model: eight general-purpose registers, program counter, and condition-code state. It also establishes the initial PC convention (`0x3000`).

Instruction execution mutates this module’s state, especially condition flags after value-producing operations. Branch instructions consume those flags, making register/condition updates a critical CPU invariant.

#### Fetch–Decode–Execute Control Loop

`src/hardware/mod.rs` provides the machine execution loop. Its core control flow repeatedly:
1. fetches the word addressed by PC from VM memory;
2. advances/uses PC according to LC-3 semantics;
3. dispatches the word to instruction decoding and execution;
4. continues until a halt trap ends execution.

This module coordinates the VM memory, register file, and instruction executor rather than implementing host CLI or bytecode parsing concerns.

#### Instruction Decoder and Execution Engine

`src/hardware/instruction/mod.rs` decodes LC-3 opcode fields and implements the supported instruction set: arithmetic/logical operations, conditional control flow, subroutine and jump operations, direct/indirect/base-offset loads and stores, address calculation, and traps.

It is the CPU-semantics layer. It receives machine state through the hardware/VM/register boundary, performs sign extension and operand addressing, reads/writes memory, updates registers and condition codes, and redirects PC for branches and calls.

#### Traps, Terminal I/O, and Halt

TRAP execution in `src/hardware/instruction/mod.rs` bridges guest LC-3 services to host terminal I/O. Standard trap vectors implement character input/output, null-terminated and packed string output, prompted input, and HALT.

Host dependencies are Rust’s standard runtime and terminal control through `termios`, invoked from the startup/terminal path in `src/main.rs`. The program requires a real TTY for raw keyboard input; object programs remain untrusted guest bytecode interpreted entirely within the in-process VM.

Main entry points: [`src/hardware/mod.rs`](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/mod.rs) (Execution loop), [`src/main.rs`](https://github.com/udhayan27-dev/lc-3/blob/main/src/main.rs) (Process bootstrap), [`src/hardware/instruction/mod.rs`](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/instruction/mod.rs) (Instruction engine), [`src/hardware/vm/mod.rs`](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/vm/mod.rs) (VM memory and devices)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["Host Runtime"]
  node_main["Process bootstrap<br/>Rust entry point<br/>[main.rs]"]
  node_terminal["TTY / termios<br/>terminal control<br/>[main.rs]"]
end

subgraph group_machine["LC-3 Machine"]
  node_hardware["Execution loop<br/>machine coordinator<br/>[mod.rs]"]
  node_instruction["Instruction engine<br/>opcode decoder<br/>[mod.rs]"]
  node_registers["Register file<br/>CPU state<br/>[mod.rs]"]
  node_vm[("VM memory and devices<br/>memory boundary<br/>[mod.rs]")]
  node_keyboard_io["Keyboard-mapped I/O<br/>LC-3 device<br/>[mod.rs]"]
  node_trap_io["TRAP services<br/>guest-host bridge<br/>[mod.rs]"]
end

subgraph group_guest["Guest Programs"]
  node_object_file["LC-3 .obj file<br/>guest bytecode"]
  node_examples["Example programs<br/>compiled guests<br/>[hello-world.obj]"]
end

node_object_file -->|"program argument"| node_main
node_examples -.->|"examples of"| node_object_file
node_main -->|"loads origin and words"| node_vm
node_main -->|"sets raw/cooked mode"| node_terminal
node_main -->|"starts execution"| node_hardware
node_hardware -->|"fetches instruction"| node_vm
node_hardware -->|"uses PC"| node_registers
node_hardware -->|"dispatches word"| node_instruction
node_instruction -->|"loads and stores"| node_vm
node_instruction -->|"updates registers, PC, flags"| node_registers
node_vm -->|"mapped reads"| node_keyboard_io
node_keyboard_io -->|"polls stdin"| node_terminal
node_instruction -->|"TRAP opcode"| node_trap_io
node_trap_io -->|"host input/output"| node_terminal
node_trap_io -->|"HALT ends loop"| node_hardware
node_hardware -.->|"returns for cleanup"| node_main

click node_main "https://github.com/udhayan27-dev/lc-3/blob/main/src/main.rs"
click node_terminal "https://github.com/udhayan27-dev/lc-3/blob/main/src/main.rs"
click node_examples "https://github.com/udhayan27-dev/lc-3/blob/main/examples/hello-world.obj"
click node_hardware "https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/mod.rs"
click node_instruction "https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/instruction/mod.rs"
click node_registers "https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/register/mod.rs"
click node_vm "https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/vm/mod.rs"
click node_keyboard_io "https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/vm/mod.rs"
click node_trap_io "https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/instruction/mod.rs"

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_main,node_terminal toneBlue
class node_hardware,node_instruction,node_registers,node_vm,node_keyboard_io,node_trap_io toneAmber
class node_object_file,node_examples toneMint
```

## Components

### Host Runtime

- [Process bootstrap](https://github.com/udhayan27-dev/lc-3/blob/main/src/main.rs): Rust entry point · Validates input, loads bytecode, configures terminal, and starts the VM. · `src/main.rs`
- [TTY / termios](https://github.com/udhayan27-dev/lc-3/blob/main/src/main.rs): terminal control · Raw mode enables interactive keyboard input and must be restored on exit. · `src/main.rs`

### LC-3 Machine

- [Execution loop](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/mod.rs): machine coordinator · Fetches at PC, dispatches execution, and runs until HALT. · `src/hardware/mod.rs`
- [Instruction engine](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/instruction/mod.rs): opcode decoder · Implements LC-3 CPU semantics, addressing, control flow, and traps. · `src/hardware/instruction/mod.rs`
- [Register file](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/register/mod.rs): CPU state · Holds general registers, PC, and condition codes; initial PC is 0x3000. · `src/hardware/register/mod.rs`
- [VM memory and devices](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/vm/mod.rs): memory boundary · Provides 64 KiB word-addressed memory and keyboard-mapped device behavior. · `src/hardware/vm/mod.rs`
- [Keyboard-mapped I/O](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/vm/mod.rs): LC-3 device · KBSR/KBDR reads poll host stdin instead of ordinary RAM. · `src/hardware/vm/mod.rs`
- [TRAP services](https://github.com/udhayan27-dev/lc-3/blob/main/src/hardware/instruction/mod.rs): guest-host bridge · Provides character and string I/O, prompted input, and HALT. · `src/hardware/instruction/mod.rs`

### Guest Programs

- **LC-3 .obj file**: guest bytecode · Big-endian words begin with an origin address.
- [Example programs](https://github.com/udhayan27-dev/lc-3/blob/main/examples/hello-world.obj): compiled guests · Includes hello-world, 2048, and rogue object programs. · `examples/hello-world.obj`

## Connections

- LC-3 .obj file → Process bootstrap: program argument
- Example programs → LC-3 .obj file: examples of
- Process bootstrap → VM memory and devices: loads origin and words
- Process bootstrap → TTY / termios: sets raw/cooked mode
- Process bootstrap → Execution loop: starts execution
- Execution loop → VM memory and devices: fetches instruction
- Execution loop → Register file: uses PC
- Execution loop → Instruction engine: dispatches word
- Instruction engine → VM memory and devices: loads and stores
- Instruction engine → Register file: updates registers, PC, flags
- VM memory and devices → Keyboard-mapped I/O: mapped reads
- Keyboard-mapped I/O → TTY / termios: polls stdin
- Instruction engine → TRAP services: TRAP opcode
- TRAP services → TTY / termios: host input/output
- TRAP services → Execution loop: HALT ends loop
- Execution loop → Process bootstrap: returns for cleanup
