Skip to content
Tactiverge

Write code for the machine you built

15A simple virtual processor

  1. Stage 1
  2. Stage 2
  3. Stage 3
  4. Stage 4
  5. Stage 5
  6. Stage 6
  7. Stage 7
  8. Stage 8
  9. Stage 9
  10. Stage 10
  11. Stage 11
  12. Stage 12
  13. Stage 13
  14. Stage 14
  15. Stage 15 (this one)

Explanation

Everything joins up: registers, an ALU with flags, a decoder, memory, and a control unit stepping through fetch, decode and execute. Bench-8 is a conceptual 8-bit accumulator machine with 16 instructions. Write assembly, assemble it into bytes, then run it cycle by cycle or all at once.

The programming challenges are graded right here in your browser: your program runs on several test inputs and its output must match exactly. At the Systems and Research levels, measure instructions, cycles and CPI, compare single-cycle, multi-cycle and pipelined designs, and see how a cache changes memory behaviour.

Program (Bench-8 assembly)

Assembles: 13 bytes. Loaded.

Instruction set (16 instructions)
opcodedoescycles
NOP00—. Do nothing for one instruction.5
LDI n10A ← n. Load the number n itself into A.6
LDA a20A ← M[a]. Load the byte stored at address a into A.7
STA a30M[a] ← A. Store A into memory at address a.7
ADD a40A ← A + M[a]. Add the byte at a to A (8-bit wraparound, C = carry out).8
SUB a50A ← A − M[a]. Subtract the byte at a from A (C = 1 means a borrow: A was smaller).8
AND a60A ← A AND M[a]. Bitwise AND with the byte at a.8
OR a70A ← A OR M[a]. Bitwise OR with the byte at a.8
XOR a80A ← A XOR M[a]. Bitwise XOR with the byte at a.8
NOT90A ← NOT A. Flip every bit of A.6
JMP aA0PC ← a. Jump: the next instruction comes from address a.6
JZ aB0if Z: PC ← a. Jump to a if the last result was zero.6
JC aC0if C: PC ← a. Jump to a if the last ADD carried (or the last SUB borrowed).6
IND0A ← input. Read the next number from the input tape (0 if the tape is empty).6
OUTE0output ← A. Write A to the output display.6
HALTF0stop. Stop the clock.6
bus idlePC0MAR0MemoryM[0] = 16IR00 NOPOperand0Control / decoderreadyA (accumulator)0B latch0ALUidleFlagszcnvINnext 3OUT–FETCHDECODEEXECUTEcycle 0 · instructions 0
  • no signals yet: press Step

Next: F1 MAR ← PC.

Machine state as text

Cycle 0, 0 instructions completed. Next step: F1 (fetch) MAR ← PC. PC 0, IR 0 (NOP), operand 0, MAR 0. A 0, B 0. Flags Z 0 C 0 N 0 V 0. Output: nothing yet.

Output

–

Instructions

0

Cycles

0

CPI

–

Listing and breakpoints

Assembled program; press a row's button to toggle a breakpoint
bpaddrbytessource
0010 05▶ LDI 5
02B0 0Aloop: JZ done ; stop when A is 0
04E0 00OUT
0650 0CSUB one
08A0 02JMP loop
0AF0 00done: HALT
0C01one: DB 1

Memory (256 bytes)

Memory contents in hex; PC and MAR cells are marked
+0123456789ABCDEF
0010 (PC)05B00AE000500CA002F00001000000
1000000000000000000000000000000000
2000000000000000000000000000000000
3000000000000000000000000000000000
4000000000000000000000000000000000
5000000000000000000000000000000000
6000000000000000000000000000000000
7000000000000000000000000000000000
8000000000000000000000000000000000
9000000000000000000000000000000000
A000000000000000000000000000000000
B000000000000000000000000000000000
C000000000000000000000000000000000
D000000000000000000000000000000000
E000000000000000000000000000000000
F000000000000000000000000000000000

blue = PC, lime = MAR. Program and data share this memory.

Cycle-by-cycle trace

cyclestepphasetransferbusAPC

Nothing yet. Step the clock.

Saved programs stay in this browser only (localStorage). Nothing is uploaded.

Programming challenges

Each challenge runs your program on several input tapes, right here in your browser. Every output must match exactly, and the program must HALT.

Add two numbers Level 1+

Read two numbers with IN, output their sum with OUT, then HALT. Sums wrap at 256, just like the register.

Hint

ADD only adds a value from memory, so store the first number somewhere (STA) before you read the second.

Performance (Level 4+)

Run a program (Run to end) to measure it.

Single-cycle vs multi-cycle vs pipeline

Run a program first: the comparison replays the exact instructions it executed.

Experiment: throughput vs hazard rate

The same model with a synthetic instruction mix, as a Simulation OS experiment: change one variable at a time and compare runs.

Simulation OS · siliconEducational model, not a digital twin

Pipeline throughput vs hazard rate

How data hazards and taken branches eat into the speed-up of a 5-stage pipeline, with and without forwarding.

Your level (shows more controls and deeper equations)
  1. 1. Ask
  2. 2. Hypothesise
  3. 3. Set up
  4. 4. Run
  5. 5. Record
  6. 6. Change one variable
  7. 7. Compare
  8. 8. Interpret
  9. 9. Limitations & save

If I change …, then … will …, because …

3 · Set up the parameters

10–1000

Longer runs hide the 4-clock fill time of the pipeline.

0–1

Fraction of instructions that need the result of the instruction just before them.

0–1

Forwarding passes a result straight from EX or MEM to the next instruction.

0–1

A value read from memory is ready one stage later, so even forwarding stalls once.

0–0.5

Fraction of instructions that are jumps.

0–1

Fraction of jumps that are taken (the pipeline guesses not-taken).

0–3 clocks

Instructions flushed when a taken branch is discovered in EX.

The seed fixes any random noise. Same parameters + same seed = exactly the same result, every time, on every computer.

6 · Change one variable

Your first run is the baseline. After it, change one thing at a time.

7 · Compare on a graph

Run the model to draw your first curve here.

5 · Run log

No runs yet.

Equations

stalls = (forwarding ? H_load : 2·H) + B_taken · penalty, with H = round(N·h), H_load = round(H·L), B_taken = round(N·b·t). Speed-up = 5N / cycles, since a single-cycle clock must fit all 5 stages.

Assumptions (always true in this model)

  • Every stage takes exactly one time unit; there is no pipeline-register overhead.
  • Every hazard is between neighbouring instructions (distance 1).
  • Branches are predicted not-taken and resolved in EX.
  • No structural hazards, cache misses or interrupts.
  • Counts are rounded to whole instructions, so results are whole clocks.
Text description of what the chart shows

A line chart of clocks per instruction (CPI) against hazard rate from 0 to 1. Without forwarding CPI climbs steeply, by about 2 for every hazard per instruction; with forwarding it stays close to 1, rising only for hazards that follow a load. Taken branches lift both lines by the branch penalty.

Did the result support your hypothesis? Use the numbers from the comparison table and link them to an equation.

Which assumptions could change your answer in the real world?

Export and save

Exports use the ticked runs (or every run if none are ticked). Your draft saves automatically in this browser only. The Journal is private to you.

Cache simulator (Level 5)

Hit rate 0.0% (0 hits, 64 misses of 64; 8 compulsory)

  • direct-mapped0.0%
  • 2-way75.0%
  • 4-way75.0%
First 64 accesses: address → block → set and tag
#addrblocksettagresultevicted
10000miss
21024256064miss0
31000miss256
41025256064miss0
52000miss256
61026256064miss0
73000miss256
81027256064miss0
94110miss
101028257164miss1
115110miss257
121029257164miss1
136110miss257
141030257164miss1
157110miss257
161031257164miss1
178220miss
181032258264miss2
199220miss258
201033258264miss2
2110220miss258
221034258264miss2
2311220miss258
241035258264miss2
2512330miss
261036259364miss3
2713330miss259
281037259364miss3
2914330miss259
301038259364miss3
3115330miss259
321039259364miss3
330000miss256
341024256064miss0
351000miss256
361025256064miss0
372000miss256
381026256064miss0
393000miss256
401027256064miss0
414110miss257
421028257164miss1
435110miss257
441029257164miss1
456110miss257
461030257164miss1
477110miss257
481031257164miss1
498220miss258
501032258264miss2
519220miss258
521033258264miss2
5310220miss258
541034258264miss2
5511220miss258
561035258264miss2
5712330miss259
581036259364miss3
5913330miss259
601037259364miss3
6114330miss259
621038259364miss3
6315330miss259
641039259364miss3

Reads only, LRU replacement, one level, no timing. Bench-8 itself has no cache: this panel is a separate model fed with its address trace.

Simulation OS · siliconEducational model, not a digital twin

Cache hit rate vs size and associativity

Run a repeating array scan through a small cache and compare direct-mapped with 2-way and 4-way set-associative caches of the same size.

Your level (shows more controls and deeper equations)
  1. 1. Ask
  2. 2. Hypothesise
  3. 3. Set up
  4. 4. Run
  5. 5. Record
  6. 6. Change one variable
  7. 7. Compare
  8. 8. Interpret
  9. 9. Limitations & save

If I change …, then … will …, because …

3 · Set up the parameters

0–6

The cache holds 2^value blocks: 0 → 1 line, 6 → 64 lines.

0–3

0 = direct-mapped, 1 = 2-way, 2 = 4-way, 3 = 8-way (never more ways than lines).

0–4

Bigger blocks bring neighbouring bytes in on each miss.

4–256 B

Bytes in the array being scanned.

1–8

Repeated passes reward a cache big enough to keep the array.

1–16 B

Step between addresses read.

0–1

Interleave reads of a second array placed 1024 bytes away, which maps onto the same sets.

The seed fixes any random noise. Same parameters + same seed = exactly the same result, every time, on every computer.

6 · Change one variable

Your first run is the baseline. After it, change one thing at a time.

7 · Compare on a graph

Run the model to draw your first curve here.

5 · Run log

No runs yet.

Equations

misses = compulsory (first touch of each block) + conflict and capacity misses. Associativity removes conflict misses between blocks that share a set; size removes capacity misses.

Assumptions (always true in this model)

  • Reads only: no writes, so no write policy.
  • One cache level, least-recently-used replacement, no prefetching.
  • The trace is a deterministic array scan; real programs mix many patterns.
  • Timing is not modelled: only hits and misses are counted.
Text description of what the chart shows

A line chart of cache hit rate against cache size, from 1 to 64 lines, for direct-mapped, 2-way and 4-way caches. Hit rate rises once the cache can hold the whole array. When a second array maps onto the same sets, the direct-mapped line stays at zero while the associative caches recover.

Did the result support your hypothesis? Use the numbers from the comparison table and link them to an equation.

Which assumptions could change your answer in the real world?

Export and save

Exports use the ticked runs (or every run if none are ticked). Your draft saves automatically in this browser only. The Journal is private to you.

Check yourself

Marked instantly in your browser. Use the interactive above to work it out.

  1. 1. A program runs 10 instructions in 65 cycles. What is its CPI?
  2. 2. Which Bench-8 instruction lets a loop stop when a counter reaches 0?