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.
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.
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.
The program: one pixel of the SAD search per lap
Registers
Data memory
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.
The search, step by step
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
| Instructions | 2 |
|---|---|
| Candidate blocks at once | 3 or 4 |
| Subtractors | 16 |
| Absolute-value units | 16 |
| Adders, in a tree | 15 |
| Pixel pairs per pass | 16one 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?



