Skip to content

Individual conclusions

The spec asks every member for an individual sheet with three parts:

  1. A contribution linked to a commit, review or test.
  2. An annotated trace they can reproduce (PC, bytes, fields, control signals, buffers, state before and after).
  3. A conclusion about a decision or a fix of their own.

The traces in Tests and traces show the format; each member annotates one of their own.

PR #125 (SDRAM bridge, instruction cache and top_doom) and PR #129 (data cache).

Doom needs more than 10 MB and the FPGA has ~182 KB of memory, so it runs from the board’s 64 MB SDRAM. My part was everything between the CPU and the SDRAM controller:

  • Bridge (rtl/sdram_bridge.v): turns a load or store into a request for Intel’s SDRAM controller and holds the CPU with mem_stall until it’s done (a load takes ~8 cycles instead of 1). It also swaps the bytes, because the controller is little-endian and our CPU big-endian.
  • Instruction cache (rtl/icache.v): 8 KB, direct-mapped, 16-byte lines. A hit gives one instruction per cycle.
  • Data cache (rtl/dcache.v): 16 KB, direct-mapped, write-through. It took Doom from 4.2 to 7.8 frames per second in simulation.

Checked with verif/doom_mem.do (Intel’s real controller and its memory model), Verilator with random memory delays, Doom in Verilator, and on the board on Nov 26.

From verif/traces/trace_I.txt. To reproduce it: python src/assembler/assembler.py src/programs/tc16_trace.asm, then do trace.do in ModelSim from verif/.

Before: $s1 = 10010000 $t3 = ffffffff mem[0x10010004] = cafe1234
Cycle 1 IF PC = 00000020 -> bytes 8e 2b 00 04 -> word 8e2b0004

The 4 bytes come from the 4 banks of the instruction memory, one byte per address, joined big-endian.

Cycle 2 ID fields I: opcode = 100011, rs = 17 ($s1), rt = 11 ($t3), immediate = 0004 -> extended 00000004
control: RegDst = 0, ALUSrc = 1, MemtoReg = 1, RegWrite = 1, MemRead = 1, MemWrite = 0
register file: A = R[rs] = 10010000 · hazard unit: stall = 0

Opcode 35 is lw. The destination is rt (RegDst = 0), the ALU takes the immediate (ALUSrc = 1), and the value written back comes from memory (MemtoReg = 1). Reg… signals are about the register file and Mem… about data memory: a lw reads memory and writes a register. No stall: no older instruction writes $s1.

Cycle 3 EX ALU: 10010000 + 00000004 = 10010004
Cycle 4 MEM memory bus: address = 10010004, read = 1 -> read data = cafe1234

The decoder sees 0x1001…, the on-chip data memory, which answers in the same cycle. In Doom the same lw to an address 0x2000… goes to the data cache: a hit also answers in the same cycle, and a miss holds the whole pipeline with mem_stall for ~8 cycles while the line comes from the SDRAM. That is my contribution seen from one instruction.

Cycle 5 WB $t3 <= cafe1234 (bypassed to ID during the same cycle)
After : $s1 = 10010000 $t3 = cafe1234 mem[0x10010004] = cafe1234

Only $t3 changed; memory is the same (MemWrite = 0).

When I designed the data cache I chose write-through: every store goes to the SDRAM, and also into the cache if the line is there. A write-back cache would be faster for stores, because it writes the SDRAM only when a line is replaced, but then the SDRAM can hold old data. Our SDRAM has two other readers that don’t go through the data cache: the instruction cache and the JTAG master we use to load Doom and the WAD. With write-through the SDRAM is always up to date, so none of them can read stale data, and there is nothing to write back when a line is replaced.

The cost is that every store waits ~3 cycles for the SDRAM. Even so, the data cache almost doubled Doom’s speed (4.2 → 7.8 frames per second), because most accesses are loads that hit. What I learned: a cache is not only about speed. Who else reads the same memory decides how it has to write, and choosing the simpler, always-correct option first let us get Doom running on the board on the first try.

I worked on the CPU core: the pipeline registers (PR #101), branches and jumps resolved in ID (PR #106), and the hazard detection unit (PR #107). Ariel and I also worked on the full MIPS I integer set (PR #109).

The pipeline separates instruction fetch, decode, execute, memory access and writeback with IF/ID, ID/EX, EX/MEM and MEM/WB registers. The hazard unit holds PC and IF/ID when an instruction needs a result still in EX or MEM, and inserts a bubble into ID/EX. Branches are decided in ID; the next instruction is the delay slot. These behaviors are documented in Pipeline and checked by tc4.do, tc5.do, cpu6.do and the R/I/J traces.

From verif/traces/trace_J.txt. To reproduce the trace, assemble src/programs/tc16_trace.asm with python src/assembler/assembler.py src/programs/tc16_trace.asm, then run do trace.do in ModelSim from verif/. The trace was captured from mips_pipeline; the notes below explain the control-flow decision I worked on.

Before: $t4 = 00000000
Cycle 1 IF PC = 00000024 -> bytes 08 00 00 0c -> word 0800000c
Cycle 2 ID IF/ID valid = 1; opcode = 000010 (j), target = 000000c
jump address = {(PC + 4)[31:28], target, 00} = 00000030
Jump = 1, RegWrite = 0, MemRead = 0, MemWrite = 0
hazard: stall = 0, redirect = 1
IF fetches 00000028 (delay slot); next fetch is 00000030
Cycle 3 EX ID/EX valid = 1; no register or memory write
Cycle 4 MEM EX/MEM valid = 1; MemRead = 0, MemWrite = 0
Cycle 5 WB MEM/WB valid = 1; RegWrite = 0, so no register changes
After : $t4 = 00000000; the addi on the skipped path did not run

The jump’s 26-bit target is shifted left by two bits and combined with the high four bits of PC + 4. Resolving it in ID redirects the fetch to 0x30, while the instruction already fetched at 0x28 still runs. The assembler places a nop there for assembly programs, so this delay slot does not change $t4.

I kept branch and jump decisions in ID and made the pipeline execute one delay-slot instruction. This allows the existing fetch to complete while the next PC is redirected, and it matches the behavior expected by MIPS code compiled for the CPU. The assembler inserts a nop in the slot for our hand-written programs. The trace taught me to check control flow across cycles: it is not enough to see the final jump target; the instruction at PC + 4 and its side effects must also be accounted for.

Contribution: the assembler and the reference simulator

Section titled “Contribution: the assembler and the reference simulator”

PR #111 added the phase 2 encodings, the pseudo-instructions li, la and move, and the nop after every jump and branch. PR #110 added ref_sim.py, the software reference that prints every register write. The assembler’s diagnostics (one example per error, with the line number) and the board images are part of the same work.

Checked with test_pseudo and test_phase2 against MARS’s Data Segment, t1_alu against MARS’s registers, and check_asm_errors.py, which runs the eight error examples.

From verif/traces/trace_R.txt. To reproduce it: python src/assembler/assembler.py src/programs/tc16_trace.asm, then do trace.do in ModelSim from verif/.

The instruction is at 0x0000001C, and the assembler encodes it as 0x01095021. Before it runs, $t0 = 5 and $t1 = 7.

Cycle 1 IF PC = 0000001c -> word 01095021
Cycle 2 ID fields R: opcode = 000000, rs = 8 ($t0), rt = 9 ($t1), rd = 10 ($t2), shamt = 0, funct = 100001 (0x21)
control: RegDst = 1, RegWrite = 1, ALUSrc = 0, ALUOp = 10, ALU control = 0010 (add)
register file: A = R[rs] = 00000005, B = R[rt] = 00000007, destination = 10 ($t2)
hazard unit: stall = 0, redirect = 0

RegDst = 1 selects rd as the destination, not rt, and ALUOp = 10 tells the ALU control to read funct to pick the operation. Here funct = 0x21 means add.

Cycle 3 EX ALU: 00000005 + 00000007 = 0000000c

The ALU does the addition. ALUSrc = 0 means the second operand is $t1 and not the immediate.

Cycle 4 MEM no memory access (MemRead = 0, MemWrite = 0); value to MEM/WB = 0000000c
Cycle 5 WB $t2 <= 0000000c at the end of the cycle
After : $t0 = 00000005 $t1 = 00000007 $t2 = 0000000c

Conclusion: where the files go and how the passes work

Section titled “Conclusion: where the files go and how the passes work”

The assembler writes its images to mem/ by default, because that is where Quartus (at compile time) and ModelSim ($readmemh) look for them. -o <folder> is for images that must not replace the ones the board loads. The error checker uses build/asm_errors, so it never touches the program on the board. The two quartus/program.qsf lines are printed only when the output is mem/. What I learned: the first pass goes line by line, counts the words each line takes (including the nop after a branch or jump) and records the address of every label. The second pass turns the instructions into 32-bit words, now that every label has an address, and writes the .hex files.

Contribution: Game of Life, test programs and the graded algorithm

Section titled “Contribution: Game of Life, test programs and the graded algorithm”

Each task links to its pull request.

Task What I did Link
LIFE-1, LIFE-2 Game of Life in MARS: 32x24 board with 4x4 cells, two buffers, the rules, wrap-around edges, keyboard controls PR #69
TC-4 6 self-checking test programs, one per instruction group (R, shifts, I, memory, branches, jumps) PR #70
TC-5 4 hazard test programs (data, load-use, branches, jumps) PR #98
LIFE-4 Live-cell bar on the LEDs (LEDR9-1) PR #100
TC-15 Graded algorithm “frequency of a value” with three inputs, one of them an edge case PR #103

From src/programs/count_value_in1.asm. To reproduce it in MARS: assemble with F3 and step with F7. Addresses are MARS’s (base 0x00400000); on the board the PC starts at 0. The control signals match the table on the Datapath page.

Before: $t0 = 8 $t6 = 0
PC = 0x00400018 -> word 0x0008702A = 000000 00000 01000 01110 00000 101010
fields R: opcode 0, rs = 0 ($zero), rt = 8 ($t0), rd = 14 ($t6), shamt = 0, funct = 0x2A
control: RegDst = 1, ALUSrc = 0, MemtoReg = 0, RegWrite = 1, MemRead = 0, MemWrite = 0, Branch = 0, Jump = 0, ALU = slt

The ALU compares 0 < 8 (signed) and gives 1.

After : $t6 = 1 next PC = 0x0040001C

I began by studying each part of the project, one at a time: how to write a program for this CPU, what a hazard is, how the board’s memory and I/O are laid out. I used Claude to learn how to write the code, and I did all the running and checking myself. I will be honest that understanding the whole system completely has been hard for me, because the time was very short. Most design decisions were made by the team lead, Ariel, who already knew what the project needed to work on the FPGA (for example the memory map in contracts.md). My work was to learn those decisions and build my programs on top of them. What taught me the most was the review feedback on my three larger pull requests. Ariel made all the necessary corrections after the reviews, and I studied each one to understand it:

  • Tests can have bugs too (#98). The fail: handler of my hazard tests used lui, ori and add back to back, which are hazards themselves. On a CPU with broken hazards it showed BA0000 instead of BAD0nn, which is exactly when you need to know which test failed. The fix is to put independent instructions between those three. Another test said “callee copies $ra” but only compared 1 with 1 and never checked $s2. Also, because the assembler always inserts a nop, my tests never check the delay slot itself. I learned that a test must not depend on the thing it is testing, and that a check that cannot fail proves nothing.
  • Choose a scale from the range of the data (#100). My LED bar used 16 live cells per LED, so it saturated at 144 live cells and with a random board all 9 LEDs stayed on. Dividing the range (768 cells / 9 LEDs, about 86) makes the bar actually move. I also learned that after changing a program you must regenerate the .hex files, or the board keeps running the old one.
  • Correct in the simulator is not correct on the hardware (#103). The algorithm gave 4, 4 and 0 in MARS, but data_memory.v had no initialization file, so on our CPU n and target read 0 and all three results were 0. The data has to be loaded into memory too. After Ariel fixed this, the board gave 4 for input 1 (see Algorithm).

I can explain my trace and my test programs, and I am still learning the parts of the CPU that I did not write.