ECE 369 · Fall 2024 · University of Arizona · Team of 3

MIPS Processor + VBSME

A five-stage pipelined MIPS CPU in Verilog, programmed in hand-optimized MIPS assembly to run Variable Block Size Motion Estimation, the block-matching search used in video compression.

My role: Lead engineer and architect of the processor and the accelerator

  • Verilog
  • MIPS Assembly
  • Pipelining
  • Computer Architecture
  • Hazard Detection

The project

A computer-architecture project in two halves; I was lead engineer and architect on a team of three. First we designed a five-stage pipelined MIPS processor in Verilog (fetch, decode, execute, memory, write-back) with its register file, ALU and ALU controller, control unit, sign extension, muxes and pipeline registers. Then we programmed it in hand-written assembly to run Variable Block Size Motion Estimation (VBSME): the search for where a block from one video frame best matches a region of another, which motion-compensated video compression is built on.

At a glance

Course
ECE 369, Fall 2024
Team
Three; I was lead engineer and architect
Processor
Five-stage pipelined MIPS in Verilog
Program
VBSME in hand-written assembly
Accelerator
Attempted, never demonstrated

I also attempted a hardware accelerator for the search. It was never demonstrated.

Watch the pipeline run

Our hazard-detection unit stalls instead of forwarding. Step through four instructions from the search's inner loop to see what that costs.

Hazard unit hold PC and IF/ID destination registers in EX, MEM and WB · PC + 4 from WB cycle

Each hold lasts three cycles, and a copy of the waiting instruction goes down the pipeline on each (dashed); the last copy has the right operands. By the model, not measured on hardware, these four instructions take fourteen cycles.

The processor

Our pipeline differs from the textbook one in four ways. Branches are compared and their targets computed in decode, where jumps and jr are also resolved, and jal writes $ra from decode. There is no forwarding: an instruction that reads a register still in flight is held in decode for three cycles, until its own PC + 4 comes back from write-back. And because ID/EX keeps loading while decode is held, the held instruction runs down the pipeline again on each of those cycles; the last copy, with the right operands, wins.

The hazard unit compares register numbers, and which ones depends on the instruction: rs for the immediate ALU instructions, rs and rt for anything that writes a register, and nothing at all for branches and stores. Those still stall, through a timing quirk: for an instant after a new instruction reaches decode, the control unit still shows the previous one's signals, and the stall latch keeps any match it sees. Our search program only gives the right answers with that quirk in the model.

The diagram runs a short program one clock cycle at a time, each instruction keeping its color as it moves through the stages.

IF/ID ID/EX EX/MEM MEM/WB FETCH DECODE EXECUTE MEMORY WRITE-BACK control + PC + 4 ALU op M · WB + PC + 4 PC + 4 PC + 4 Instruction memory rs rt imm rt, rd jump target jal → $ra branch address jump address PCSrc (branch taken) Jump (j, jal, jr) stall (hold) flush PC + 4 of the instruction in write-back: ends a stall destination registers in EX, MEM and WB Hazard unit stall latch · flush Control signals + ALU operation Add PC+4 + imm×4 Compare branch test Register file $v0, $v1 → display Extend sign or zero jr store data rd rt write reg ALUSrc ALU RegDst ALU result Data memory byte · half · word MemToReg write register write data

The program: one pixel of the SAD search per lap

    Registers

    Data memory

    Our datapath, redrawn from the Verilog, running a cut-down version of the search's inner loop. The cycles come from a model of the processor tested on our final machine code, not from measurements on hardware; dotted packets are re-run copies of a held instruction and dashed lines are control signals.

    The stage schematics below were generated by Vivado from our Verilog partway through the project, when the ALU controller and the branch-target adder still sat in execute; the final design, drawn above, moved both into decode.

    Instruction fetch stage
    IF: program counter, instruction memory and PC adder.
    Instruction decode stage
    ID: register file, control unit and sign extension.
    Execute stage
    EX: the ALU and its muxes, with this revision's ALU controller and branch-target adder.
    Memory stage
    MEM: memory access and the write-back path.

    The search, step by step

    frame (6 × 6) window (2 × 2) position (x, y) — sum of absolute differences — best so far —

    The assembly's algorithm on a small example: an inward spiral that skips positions where the window would hang off the edge and keeps the smallest sum of absolute differences.

    Making the registers go further

    The program never writes to memory. It reads the frame and the window with eight lw instructions and has no sw, sh or sb and no stack, so every intermediate value has to live in a small register file. To make that fit, each 32-bit register holds two 16-bit values, such as X and Y coordinates or row and column bounds, packed and unpacked with shifts and masks. That nearly doubles the usable register file without extra memory traffic.

    The candidate block sweeps the frame in an inward spiral (right, down, left, up), shrinking the bounds each lap until every position has been visited. At each one the code computes the sum of absolute differences (SAD) between the frame block and the window, keeps the running minimum, and returns the (x, y) of the best match in $v0/$v1, which are wired to the board's seven-segment display. Because our ALU implements mul, each row offset into the frame and the window is one instruction instead of an add loop.

    The 30 instructions our controller decodes, read from the Verilog. All 16 four-bit ALU control codes are in use, ten for the ALU and six for the branch comparator in decode; the last column counts each instruction's uses in the final vbsme.s.

    Instruction Opcode funct / rt ALU control Notes Uses in vbsme.s
    R-type arithmetic and logic · opcode 0x00, result to rd
    add rd, rs, rt 0x00 0x20 0000 11
    sub rd, rs, rt 0x00 0x22 0001 6
    and rd, rs, rt 0x00 0x24 0011 8
    or rd, rs, rt 0x00 0x25 0100 17
    xor rd, rs, rt 0x00 0x26 0110
    nor rd, rs, rt 0x00 0x27 0101
    slt rd, rs, rt 0x00 0x2A 1001 signed compare 6
    sll rd, rt, shamt 0x00 0x00 0111 shift amount from bits 10–6 18
    srl rd, rt, shamt 0x00 0x02 1000 19
    mul rd, rs, rt 0x1C — 0010 low 32 bits of rs × rt, one cycle 3
    Immediate · result to rt
    addi rt, rs, imm 0x08 — 0000 immediate sign-extended 44
    slti rt, rs, imm 0x0A — 1001 immediate sign-extended
    andi rt, rs, imm 0x0C — 0011 immediate zero-extended 25
    ori rt, rs, imm 0x0D — 0100 immediate zero-extended
    xori rt, rs, imm 0x0E — 0110 immediate zero-extended
    Loads and stores · address = rs + sign-extended offset
    lb rt, off(rs) 0x20 — 0000 byte 0 of the word, zero-extended
    lh rt, off(rs) 0x21 — 0000 low half-word, zero-extended
    lw rt, off(rs) 0x23 — 0000 8
    sb rt, off(rs) 0x28 — 0000 writes byte 0, keeps the rest
    sh rt, off(rs) 0x29 — 0000 writes the low half, keeps the rest
    sw rt, off(rs) 0x2B — 0000
    Branches · compared in decode; the code drives the branch comparator, not the ALU
    beq rs, rt, label 0x04 — 1010 taken if rs = rt 9
    bne rs, rt, label 0x05 — 1011 taken if rs ≠ rt 6
    blez rs, label 0x06 — 1100 taken if rs ≤ 0
    bgtz rs, label 0x07 — 1101 taken if rs > 0
    bltz rs, label 0x01 rt = 0 1111 taken if rs < 0
    bgez rs, label 0x01 rt = 1 1110 taken if rs ≥ 0
    Jumps · resolved in decode
    j target 0x02 — — 16
    jal target 0x03 — — writes PC + 4 to $ra in decode
    jr rs 0x00 0x08 — target read from rs 1

    An attempt at an accelerator

    The last part of the project was an attempt to move the search itself into hardware. I designed a VBSME unit in Verilog that drops the spiral: the processor feeds it the image, and the comparisons happen inside the unit. It takes two instructions, one that loads data from memory into the accelerator and one that says the whole image has been read and releases the answer to the display. Because the image size was known ahead of time, no software loop of compares and branches is needed.

    The accelerator

    Instructions2
    Candidate blocks at once3 or 4
    Subtractors16
    Absolute-value units16
    Adders, in a tree15
    Pixel pairs per pass16one combinational pass

    Incoming data fills the accelerator's registers. Once enough has arrived for three or four candidate blocks, their SADs are worked out side by side and the lowest is kept, and this repeats as more of the image loads. The SAD unit is sixteen subtractors, sixteen absolute-value units and a tree of fifteen adders, so sixteen pixels of the window are compared with sixteen pixels of the frame in a single combinational pass, where the assembly spends a loop iteration on each pixel.

    It was not demonstrated. We missed the deadline to load it onto the board and show it running, so there is no measured result for it. Everything else on this page is the assembly version running on the processor.

    What I took from it

    A shortcut in hardware becomes a cost in software. Leaving out forwarding kept the pipeline simpler, but every dependency on a value still in flight stalls for three cycles, even where one would do, so the order of instructions in the assembly decides how many cycles are wasted. Designing the processor and writing its code were one problem, not two.

    Constraints shape the algorithm. With no memory writes and a small register file, packing two values per register was what made the search fit. And the most ambitious part of the project, the accelerator, is the one with nothing to show, because a hardware design isn't finished until it runs on the board.

    Where it goes next

    I plan to come back to the accelerator: the same kernel as an FPGA accelerator is one of the projects I am building next. It fits a larger plan I call "one kernel, three ways": the same SAD search as this MIPS assembly, as a CUDA kernel and as an FPGA accelerator, benchmarked side by side. This time the accelerator would be finished and measured.

    On the processor itself, the natural next change is forwarding. The model behind the diagrams on this page already counts stalled cycles, so adding forwarding to it first would show how many cycles the change saves before any Verilog is touched.

    Want the details behind this one, or to talk about similar work?