How udhayan27-dev/lc-3 works
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: hardware/mod.rs Execution loop · main.rs Process bootstrap · instruction/mod.rs Instruction engine · vm/mod.rs VM memory and devices
Components 10
Host Runtime
- Process bootstrap · Validates input, loads bytecode, configures terminal, and starts the VM.
src/main.rs - TTY / termios · Raw mode enables interactive keyboard input and must be restored on exit.
src/main.rs
LC-3 Machine
- Execution loop · Fetches at PC, dispatches execution, and runs until HALT.
src/hardware/mod.rs - Instruction engine · Implements LC-3 CPU semantics, addressing, control flow, and traps.
src/hardware/instruction/mod.rs - Register file · Holds general registers, PC, and condition codes; initial PC is 0x3000.
src/hardware/register/mod.rs - VM memory and devices · Provides 64 KiB word-addressed memory and keyboard-mapped device behavior.
src/hardware/vm/mod.rs - Keyboard-mapped I/O · KBSR/KBDR reads poll host stdin instead of ordinary RAM.
src/hardware/vm/mod.rs - TRAP services · Provides character and string I/O, prompted input, and HALT.
src/hardware/instruction/mod.rs
Guest Programs
- LC-3 .obj file · Big-endian words begin with an origin address.
- Example programs · Includes hello-world, 2048, and rogue object programs.
examples/hello-world.obj
Connections 16
- 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