Wen Cao

Back to projects

Oct – Dec 2024 EECS 151 FPGA lab

3-stage RISC-V superscalar CPU with branch predictor

A 3-stage pipelined RISC-V CPU written in Verilog for the Xilinx PYNQ-Z1 FPGA, extended into a dual-issue superscalar core. It reached a CPI of 0.975, making it the first and only fully functional superscalar CPU in the lab’s history, and it won the Apple New Silicon Initiative design contest.

Clock
100 MHz
Superscalar CPI
0.975
Base CPI
1.245
Branch prediction
Up to 99.8% accurate
Recognition
Apple New Silicon Initiative winner, presented at Apple Park

The base pipeline

I started with a three-stage pipeline that runs real RISC-V programs over UART and can synthesize audio. A few design choices kept it fast without many stalls.

Sketch of the three-stage datapath
The base three-stage datapath.

Jumps need no stall, because the immediate is computed directly and passed to the first stage’s PC input; only a load followed by JALR costs one cycle. The register file is double-pumped so simultaneous writes and reads work, and nearly all forwarding goes through the write-back value. The control logic takes in all three stages and computes only the signals each one needs.

To close timing at 100 MHz, I moved UART handling from the second stage to the third and computed the ALU operand and operation selects one stage early. The finished core ran the challenge benchmark at a CPI of 1.245, about 0.16 seconds per run.

A configurable branch predictor

The first creative feature was a fully functional N-way set-associative cache for the branch predictor, parameterized by address width, data width, number of lines, and number of ways, with LRU replacement. Testing different sizes showed that four ways gave the best trade-off.

BenchmarkWithout predictorWith predictorExtra correct predictions
Matrix multiply50.3%79.9%52,615
Bubble sort50.2%99.8%124,746

To keep the predictor small, I merged the LRU bits into the cache itself and synthesized with Vivado’s area-focused optimization.

ResourceBeforeAfter
Slice LUTs6,2734,470
Flip-flops and latches6,9644,323
LUT64,7843,007
MUXF71,663782
FPGA device view before optimization
FPGA utilization before area optimization.
FPGA device view after optimization
And after, with a noticeably smaller footprint.

Going superscalar

The second creative feature was a superscalar core that issues two instructions per cycle. A prefetcher reads three instructions per cycle so a dispatch unit can check dependencies before sending instructions down two parallel paths.

Superscalar datapath diagram
The dual-issue datapath, with two ALUs, two store and load units, and the instruction prefetcher feeding two CPU paths.
Diagram of instruction prefetch and dispatch
The prefetcher fills an instruction queue, and the dispatch unit checks hazards before issuing to two paths.

R-, I-, U-, and S-type instructions all issue two at a time when there are no hazards. Jumps resolve in the first stage using precomputed immediates, and branches resolve there too through the four-way predictor. The register file, immediate generator, partial load and store logic, and ALU are all duplicated for two instructions, and I wrote my own testbench to check it.

Terminal output showing cycle count, instruction count, and branch prediction counts
Output from the fully functional superscalar core on the FPGA: 12,579,680 cycles for 12,894,957 instructions gives a CPI of 0.9756, and 2,940,681 of 3,678,272 branches were predicted correctly, 79.9% accuracy. Counts are shown in hexadecimal.

Back to projects