Skip to main content

Build Your Own Toy Computer

A toy computer project goes one level below an emulator. Instead of only emulating an existing instruction set, you design the tiny machine: binary representation, logic blocks, registers, memory, instruction format, control flow, I/O, and a small monitor program.


1. Overview & motivation​

Build a small computer model with fixed-width words, registers, flags, RAM, an instruction set, fetch/decode/execute control, an assembler or loader, example programs, and optional display, keyboard, timer, or storage.

Unlike Emulator, the goal is not compatibility with a real platform. The goal is to make every hardware concept explicit enough that you could emulate it, test it, and explain it.


2. Where this fits in the degree​

  • Phase: Systems
  • Semester: 4 (Systems Programming)
  • Modules deepened: Module 2 (machine representation), Module 3 (computer organization), Module 5 (interpretation)

Cross-phase relevance:

  • Prepares directly for Emulator.
  • Makes Compiler code generation concrete because the target machine is yours.
  • Helps before Operating System, where CPU privilege, interrupts, and memory maps matter.

3. Local source backbone​

Primary local source:

  • Programming a Toy Computer from Scratch (build-your-own/toy-computer)
Local chunksUse them forProject milestone
002-004Binary numbers, arithmetic, logical operations, hexBit and word representation
005-010Transistors, logic gates, arithmetic circuits, memory cells, busesDatapath and memory model
011-017Instructions, toy instruction set, control circuits, implementation, example programsCPU core and sample programs
018-032Embedded board overview, registers, instruction set, vector table, first programReal-hardware comparison and optional embedded path
033-040Bytecode instructions and interpreterFetch/decode/execute loop
041-063Clocks, display, keyboard, UART, interrupts, basic I/OTimers, input, output, and interrupt-style extensions
064-172Flash, UI, algorithms, implementation, compilation and testsMonitor, debugger, storage, and larger capstone extensions

4. Implementation milestones​

Milestone 1: Word, memory, and register model​

Choose word size, address size, register count, and memory layout.

Evidence: unit tests for overflow, sign/zero handling, and memory bounds.

Milestone 2: Instruction set​

Define opcodes for load/store, arithmetic, compare, jump, and halt.

Evidence: instruction reference table with binary encoding and examples.

Milestone 3: Fetch/decode/execute loop​

Implement the CPU state and run one instruction at a time.

Evidence: trace shows PC, opcode, operands, registers, flags, and changed memory after each instruction.

Milestone 4: Assembler or loader​

Write a minimal assembler or structured loader so examples are not hand-coded byte arrays.

Evidence: assemble and run sum 1..10, max, and a loop with conditional jump.

Milestone 5: I/O device​

Add memory-mapped output, keyboard input, or a timer.

Evidence: a program writes text or pixels through the device interface.

Milestone 6: Monitor/debugger​

Add single-step, breakpoints, memory inspection, and register inspection.

Evidence: debugging transcript of a broken program being diagnosed.


5. Tests & evidence​

TestEvidence
ALU correctnessarithmetic and flag tests
Instruction decodeevery opcode decodes from bytes correctly
Control flowloops, branches, halt behavior
Memory safetyinvalid addresses fail predictably
Program examplesat least three non-trivial programs
Debuggertrace or transcript proving step mode works

6. Architecture specification​

Before coding, write a machine specification. Keep it small enough to fit on one page.

DecisionExampleWhy it matters
Word size8-bit, 16-bit, or 32-bitDetermines overflow, instruction width, and memory addressing
Address size8-bit address = 256 cellsSets program/data limits
Register fileR0-R7, PC, SP, FLAGSDefines what instructions can touch directly
Flagszero, carry, negative, overflowDefines branch behavior
Memory mapcode, data, stack, I/O pagePrevents accidental overlap
Instruction formatfixed 16-bit or variable lengthControls decoder complexity
Halt/error modelHALT, invalid opcode trapMakes failures inspectable

Suggested minimal ISA​

CategoryInstructions
Data movementLOAD, STORE, MOV, PUSH, POP
ArithmeticADD, SUB, INC, DEC
LogicAND, OR, XOR, NOT, SHL, SHR
Compare/branchCMP, JMP, JZ, JNZ, JC, JNC
I/OIN, OUT or memory-mapped load/store
ControlCALL, RET, HALT, TRAP

Do not add every instruction at once. Add a category only when you have a program that needs it.

Execution trace contract​

Every instruction should be traceable:

cycle=0042 pc=0x0010 instr=ADD R1,R2 before={R1=3,R2=4,Z=0,C=0} after={R1=7,Z=0,C=0}

If a learner cannot explain a bug from the trace, the machine is still too implicit.


7. Programs to require​

ProgramConcepts proven
Add two numbersload, arithmetic, halt
Sum 1..Nloop, compare, conditional branch
Max of an arraymemory addressing, branch decision
Copy memory rangepointer-like iteration
Subroutine callstack, call frame, return address
Echo input to outputI/O register or memory-mapped device

Each program must include source assembly, assembled bytes, final output, and one execution trace.


8. Common failure modes​

  • No written instruction encoding. If encoding lives only in code, the assembler and CPU drift.
  • Ambiguous flags. Define whether SUB sets carry on borrow and how signed overflow is represented.
  • PC update bugs. Branches, calls, and variable-length instructions are frequent off-by-one sources.
  • Stack collision. If stack grows into program/data memory, the machine needs a trap.
  • I/O side effects hidden in helpers. Memory-mapped I/O should be visible in the trace.
  • Too-real too soon. A tiny correct ISA teaches more than an unfinished ARM clone.

9. Portfolio framing​

Publish this as a computer-architecture artifact: instruction set reference, architecture diagram, sample programs, execution traces, debugger transcript, and explicit limitations.

The strongest demo is a tiny program that a reviewer can read in assembly, run, and watch at the register/memory level.


10. Deep project spec​

Project contract​

Build a toy machine, not only an interpreter loop. The project must define word size, address width, register file, flags, memory map, instruction encoding, assembler syntax, trap behavior, and at least one I/O mechanism. The implementation may be an emulator, but the deliverable is the machine specification plus runnable programs.

Source-backed reading map​

Source IDUse forRequired output
build-your-own/toy-computerbinary arithmetic, gates, buses, instruction set, bytecode interpreter, I/O, monitorISA reference, trace format, debugger transcript

Milestone map​

MilestoneDeliverableTestsFailure case
Machine specone-page architecturespec review checklistundefined flag/overflow behavior
ALU/registersarithmetic and flagsoverflow/carry testssigned/unsigned confusion
Instruction setbinary encoding and disassemblerencode/decode round tripinvalid opcode trap
CPU loopfetch/decode/executeinstruction trace fixturesPC off-by-one regression
Assemblersource to bytesassembly golden fixtureslabel resolution error
Programsrequired sample suiteoutput and trace fixturesstack/data collision
Monitorstep/break/inspectdebugger transcriptinvalid breakpoint

Test matrix​

Test typeRequired examples
UnitALU, flags, memory bounds, encode/decode
Goldenassembly to bytes, bytes to disassembly
Integrationsum, max, memory copy, call/return, echo I/O
Propertyassembler/disassembler round trip for valid instructions
Traceevery sample program has at least one inspectable run

Design notes required​

  • isa.md: instruction table, binary encoding, examples.
  • memory-map.md: code, data, stack, I/O regions.
  • debugger.md: trace format and debugger command contract.

Portfolio evidence​

Publish the ISA reference, architecture diagram, sample programs, assembled bytes, execution traces, and one bug story where the debugger exposed a machine-state issue.


Source​

This project is based on the local toypc-contents chunks and complements the Emulator and Compiler projects.