TMC #0007: Von Neumann Architecture: Stored-Program Computer Simulator
Python simulator of the stored-program architecture described in von Neumann's "First Draft of a Report on the EDVAC" (1945). A 256-word memory machine with nine opcodes demonstrates the fetch-decode-execute cycle, conditional branching, and the bottleneck that still haunts modern CPUs.
von-neumannstored-programfetch-executearchitecturecomputer-historypythonsimulator
Python simulator of the stored-program computer architecture from John von Neumann's 1945 EDVAC report: the blueprint that every computer built since has followed.
What's in the code
von_neumann_sim.py: single self-contained file, no dependencies:
VonNeumannMachine: 256 × 16-bit word memory; registers PC, ACC, MAR, MDR, IR; nine-opcode instruction set (NOP, LDA, STA, ADD, SUB, JMP, JZ, JN, OUT, HLT). Thestep()method executes exactly one fetch-decode-execute cycle and returns the internal state for inspection.instr(opcode, addr): assembler helper: packs a 4-bit opcode and 8-bit address into a 16-bit instruction word. Programs are written as lists of these values and loaded into memory withload(), which writes them as plain integers: indistinguishable from data. This is the stored-program principle made literal.demo_stored_program_principle(): loads a two-instruction program, prints the raw memory dump (showing the program as ordinary numbers), then runs it. Makes tangible what "instructions are data" means.demo_sum_1_to_10(): sums the integers 1–10 using a conditional loop; prints 55. Uses LDA, ADD, SUB, STA, JZ, JMP. Pass--traceto see every fetch-decode-execute cycle annotated.demo_fibonacci(): computes the first 10 Fibonacci numbers using the same machine, different numbers in memory. The hardware is unchanged; only the contents of the memory array differ. Outputs0 1 1 2 3 5 8 13 21 34.- Von Neumann Bottleneck note: printed at the end: explains why the shared instruction/data bus caps throughput at and why every modern CPU (caches, pipelines, prefetchers) is fighting this constraint.
Running it
python3 von_neumann_sim.py # all three demos
python3 von_neumann_sim.py --trace # with per-cycle instruction trace
No dependencies beyond the Python standard library. Tested on Python 3.10+.
Source code