Karim Mohamed

Index

  1. 01Homestart here
  2. 02Researchrun the testbenches
  3. 03Workshopthings to poke
  4. 04Venturescompanies
  5. 05Experiencethe files
  6. 06About meand what’s on now
  7. 07Off the clockplay a game

More

Seattle · ·

GitHubLinkedInContact
03 Computer architecture
Simulation

RISC8, a processor from the gates up

Computer architecture · digital logic · verification

Run my processor

hit a tour, or step the clock yourself with S

what you’re looking atMy processor running real programs: tap a tour, or press Step clock to move one clock tick at a time.

The reference program from the repository: F(0)…F(13) into memory 0–13. 103 instructions, 412 cycles.

New here? Try a 30-second tour
cycle 0retired 0READY
  1. IFFetch
  2. IDDecode
  3. EXExecute
  4. WBWriteback

PC0IMEM256 × 16IR0x0000CONTROLNOPREG FILE8 × 8A0B0ALU0Z0 N0 C0DMEM256 × 8MDR0WBBR
Highlighted: the parts of the datapath used in the last clock cycle (—).

Registers

R00 hardwired00000000
R100000000
R200000000
R300000000
R400000000
R500000000
R600000000
R700000000

Registers are editable between clock cycles. Changes made after decode reach the next instruction, because operands are latched at ID.

Instruction register

0000000000000000

NOP

op
NOP (0)
rd
R0
rs1
R0
rs2 / shamt
0
imm8
0

Control signals

  • aluOp—
  • regWritefalse
  • flagsWritefalse
  • memReadfalse
  • memWritefalse
  • branch—
  • haltfalse

Data memory · 256 bytes (hex)

00
10
20
30
40
50
60
70
80
90
A0
B0
C0
D0
E0
F0
M[0] = 0 · 00000000

Instruction memory

    Cycle trace · same format as the original simulator

     cycle st PC       IR               R0       R1       R2       R3       R4       R5       R6       R7       ZNC | changes
    
    

    01 · the question

    What does a processor actually do on every clock edge, and can I build one honest enough to prove it?

    02 · where it stands

    tests passing, in the original and in the browser port
    26/26
    clock cycles for Fibonacci, matching the golden trace byte for byte
    412
    clock states per instruction: readable, but no pipeline yet
    4

    03 · what broke → what’s next

    • Multi-cycle is slow: every instruction costs four clocks, even a NOP.
    • An 8-bit datapath wraps at 255, so the Fibonacci demo stops at F(13) = 233.
    • A five-stage pipeline with hazard detection and forwarding, checked against the same golden traces.
    • Describe it in an HDL so it could run on an FPGA. None of that exists yet.
    Read the full write-up8 sections · Why a processor, Instruction set, Registers & memory…

    Why build a processor?

    Every program I’d written ran on a machine I’d never really looked inside. I wanted to see, one clock edge at a time, what a processor actually does between fetching a word and writing a result: which latch changes, which flag flips, which bit falls out of a shift.

    So I designed a small one and wrote a simulator honest enough that its output reads like a logic-analyser capture. The original is Python; what runs on this page is a line-for-line TypeScript port that passes the same tests.

    to be precise: 16-bit instruction words, 8-bit datapath.

    The instruction set

    Sixteen opcodes, each one 16-bit word: a 4-bit opcode, then up to three 3-bit register fields or an 8-bit immediate. The decoder slices every field at once, the way hardware would; a control ROM decides which fields an opcode actually uses.

     15  12 11  9 8   6 5   3 2   0
    +------+-----+-----+-----+-----+
    |  op  | rd  | rs1 | rs2 | 000 |   R-type  ADD SUB AND OR XOR
    |  op  | rd  | rs1 |shamt| 000 |   S-type  SHL SHR
    |  op  | rd  | rs1 |   000000  |   M-type  LD ST
    |  op  | rd  |0|      imm8     |   I-type  LDI ADDI JMP BZ BNZ
    |  op  |       000000000000    |   N-type  NOP HLT
    InstructionEffectFlags
    NOPnothing
    ADD rd, rs1, rs2rd = rs1 + rs2ZNC
    SUB rd, rs1, rs2rd = rs1 − rs2ZNC
    AND / OR / XORbitwiseZN, C=0
    SHL / SHR rd, rs1, nlogical shift by 0–7ZNC
    LDI rd, imm8rd = imm8
    ADDI rd, imm8rd = rd + imm8ZNC
    LD rd, [rs1]rd = mem[rs1]
    ST rd, [rs1]mem[rs1] = rd
    JMP imm8PC = imm8
    BZ / BNZ rd, imm8branch if rd = 0 / ≠ 0
    HLTstop the clock

    Registers and memory

    Eight registers, R0 = 0

    R0–R7, 8 bits each, two read ports and one write port. R0 is hardwired to zero, so MOV rd, rs is just ADD rd, rs, R0, and needs no opcode of its own.

    Two memories

    256 × 16-bit instruction memory (read-only; unused words hold HLT, so a runaway program counter stops) and 256 bytes of data memory. Flags: Z, N and C.

    The ALU, by hand

    ADD and SUB go through eight full adders written as gate equations: sum = a ⊕ b ⊕ c, carry = ab + c(a ⊕ b). Shifts go through a three-stage barrel shifter made of 2:1 multiplexers. This panel runs that exact code.

    A
    0x96
    B
    0x35
    ADD result11001011= 203 · 0xCBZ=0N=1C=0
    FA71c8=0FA61c7=0FA50c6=1FA40c5=1FA31c4=0FA20c3=1FA11c2=0FA01c1=0

    Eight full adders in a row. Click bits to watch the carries change.

    FA0a0b0s0FA1a1b1s1FA2a2b2s2FA3a3b3s3the carry ripples ←c4
    Four of the eight full adders. The carry has to ripple through all of them before the sum is valid.

    Execution stages

    Each instruction takes exactly four clock states, one after another. They do not overlap: this is a multi-cycle machine, not a pipeline. That made every intermediate value visible in the trace.

    1. IF IR ← IMEM[PC], PC ← PC + 1
    2. ID slice fields, look up the control ROM, read registers into latches A and B
    3. EX run the ALU, read or write data memory, decide the branch
    4. WB write the register and flags, redirect the PC if a branch was taken, stop on HLT
    RISC8 datapath schematic: program counter and instruction memory feed the instruction register; the decoder and control ROM drive register reads into latches A and B; the ALU and data memory feed writeback; branch logic redirects the PC.
    The datapath, from the original repository (generated by its own script).

    Verification

    • Exhaustive ALU tests: every one of the 65,536 input pairs for ADD, SUB and the logic operations is checked against native arithmetic; shifts for every value and amount.
    • Two engines: a functional fast core (about 13× faster) must end in exactly the same state as the cycle-accurate one.
    • A golden trace: the Fibonacci program (F(0)…F(13), 103 instructions, 412 cycles) is compared byte for byte against a stored trace.
    • 26 tests in the original; the port on this page passes the same 26.

    Source on GitHub · Architecture specification · The golden trace

    Limitations

    • It’s a simulator: no hardware description, no timing analysis, no synthesis.
    • No call/return or stack, no interrupts, no signed-overflow flag.
    • Four states per instruction, every time: simple and readable, but slow.

    FPGA and next steps

    Next is a five-stage pipelined version with hazard detection and forwarding, compared against this core on the same programs. After that, describing it in an HDL and checking the hardware model against the same golden traces, which would make an FPGA implementation possible. None of that exists yet; it hasn’t been on an FPGA or in silicon.

    Research log

    Dated entries from the research notebook.

    1. Nov 10, 2026MilestoneArchitecture schematic and pipeline timeline
    2. Nov 9, 2026DecisionDecision: multi-cycle, not pipelined (yet)
    3. Nov 9, 2026MilestoneRISC8 simulator: first version
    Esc
    • Homestart here
    • Researchrun the testbenches
    • Workshopthings to poke
    • Venturescompanies
    • Experiencethe files
    • About meand what’s on now
    • Off the clockplay a game
    • My deskclick the objects
    • Field notesdated entries
    • Nowthis season
    • Changeloghow it grew
    • Photographycamera roll
    • Failure-aware manipulation
    • MalaBoost
    • RISC8 processor
    • Writing
    • Contact
    • Terminal
    • Colophon
    • Failure-aware robotic manipulationRobotics · In development
    • MalaBoostDiagnostics · Proposed design, Simulation
    • RISC8, a processor from the gates upComputer architecture · Simulation
    • Catalytic enhancement for faint pLDH linesResearch design · not peer reviewed
    • RISC8 architecture specificationTechnical specification
    • RushdFounder & CEO
    • Preventa HomeCo-founder
    • DaythreadBuilt the product on a hackathon team
    • Run the RISC8 processorguided tours
    • Make the robot arm sliptestbench
    • Rescue a faint malaria test lineMalaBoost model
    • Logic gates, adders & a puzzle
    • Watch the worst-case carry
    • Take a penaltythe keeper learns
    • ? Some pages have a key just under Esc.not yet
    • ? Two keys, one of them is K.not yet
    • ? The terminal can run a diagnostic.not yet
    • ? Referees are allowed a second look. So is the terminal.not yet
    • ? Every card is hiding a constraint.not yet
    • ? The footer is saying something.not yet
    • ? Seven cards on About. All of them.not yet

    40 results

    terminal · ` or Esc to close