0% found this document useful (0 votes)
158 views6 pages

RISC-V CPU Project Report

This project report describes a RISC-V CPU with a 5-stage pipeline implemented in Verilog HDL. The CPU features an RV32I instruction set, cache, and communication with a memory simulator using UART. Simulation was successful but testing on an FPGA board revealed issues with stall logic causing latches. The author learned about improving efficiency, automation, and debugging challenges when moving from simulation to hardware.

Uploaded by

Bilal Farooqui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views6 pages

RISC-V CPU Project Report

This project report describes a RISC-V CPU with a 5-stage pipeline implemented in Verilog HDL. The CPU features an RV32I instruction set, cache, and communication with a memory simulator using UART. Simulation was successful but testing on an FPGA board revealed issues with stall logic causing latches. The author learned about improving efficiency, automation, and debugging challenges when moving from simulation to hardware.

Uploaded by

Bilal Farooqui
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Project Report of RISC-V CPU

(Course Project of Computer Architecture)

Zhou Fan (范舟)


ACM Class, Shanghai Jiao Tong University

1 Introduction
GitHub repository of my CPU project: [Link]
This project is a RISC-V CPU with five-stage pipeline, implemented in Verilog HDL.

2 Design

2.1 Features
Main features of this RISC-V CPU are briefly introduced in the table below.

Feature RISC-V CPU

ISA RISC-V (RV32I subset)


Pipelining 5 stages
Data forwarding complete forwarding path
Cache N-way set associate I-cache and D-cache1
2
UART module passed simulation
3
Security perfect proof against Meltdown and Spectre attack

1. The cache is based on Zhekai Zhang’s (张哲恺) code [4] .

2. UART module has not passed test on FPGA yet for the limited time. I re-designed
part of CPU code to avoid hidden danger on FPGA, and it may need some more
debugging.

3. Just kidding ;-) That’s because the CPU is not with branch prediction or out-of-
order execution.

1
2.2 Specification
The CPU has a standard 5-stage pipeline with complete path data forwarding. Data
produced by EX stage or MEM stage is passed to ID stage, which avoids most of RAW
data hazard under ideal conditions (causes stall only if producer is a load instruction).
The picture below shows structure of the CPU design, each module is implemented
in a single verilog file. Red paths show the stall control flow, while orange ones show data
forwarding path.

Fig. 1: Overview of CPU Design

• Instruction fetching, Load/Store instruction or data dependency may cause stall of


pipeline, the logic of stall is managed by a stall controller module. It receives stall
requests from IF/ID/MEM stage, and emit stall signals to modules that should
pause.

• For program test on FPGA without capable memory, the CPU uses UART protocol
to communicate with PC, where runs a memory simulator written in C++.

• Latency of UART communication makes it significant to use a cache, the cache is


N-way associate using LRU replacement policy. It is based on code from Zhekai
Zhang’s MIPS CPU project.

The CPU with cache and UART module passed simulation in Xilinx Vivado using
multiple test programs. But something goes wrong when tested on FPGA (Basys 3),

2
which is explained in the next section.

Fig. 2: Communication with memory simulator using UART protocol

3 Thinkings
Efficiency Time efficiency is always a significant issue in CPU design. When I wrote
code for stall logic and Branch/Jump instruction processing part, I tried to minimize
number of unused clock cycle. But when UART latency is considered, all of those opti-
mization lose their meaning: UART communication is the only bottleneck of the pipeline,
and even the pipeline is not a pipeline any more, for an instruction would goes through
all stages before the next instruction is fetched. Though there is I-cache, it only works
for loops. Therefore, UART may not be a perfect solution to memory simulation.

Experience with FPGA After cache and UART module passed simulation, I tested
the CPU on Basys 3 FPGA, it read the first 5 instructions correctly but did not jump at
the 5th instruction, which is a branch instruction. That test program did not go wrong in
Vivado simulation. In the last two days before deadline I tried to find the bug but did not
make it. Zhanghao Wu gave me a tutorial about always @ blocks [5] , from which I learned
about latch generation. My original design for stall control with IF stages caused some
latch inferring, which could be ”a terrible place for bugs” [5] . I re-designed part of code to
avoid latches, and found that it may need some more debugging. From this experience,

3
I learned that some hidden trouble would not reveal itself in simulation. And I should
have learned more about Verilog HDL before starting to write code.

Automation To compile and assemble test programs RISC-V toolchain and some other
tools are used. Many commands are needed in this process. I wrote a Makefile and found
it really helpful. Automation improves efficiency and it feels good!

4 Acknowledgements
Special thanks would go to Zhanghao Wu (吴章昊) for his instructive discusstions
and useful suggestions on this project. I would like to express my gratitude to TA Zhekai
Zhang (张哲恺) as well, for his MIPS CPU project (especially the code of cache and
UART module) and much work for this assignment. I am also indebted to many other
classmates for their direct and indirect help to me.

5 Appendix

Fig. 3: Implementation on Basys 3 FPGA, using Xilinx Vivado

4
Fig. 4: Scematic Overview

Fig. 5: CPU Module Scematic

References
[1] 雷思磊. 自己动手写 CPU, 电子工业出版社, 2014.

[2] John L. Hennessy, David A. Patterson, et al. Computer Architecture: A Quantitative


Approach, Fifth Edition, 2012.

[3] David A. Patterson. PPT of CS252 Graduate Computer Architecture, 2001.

5
[4] Zhekai Zhang’s (张哲恺) MIPS CPU project. [Link]
mips-cpu

[5] Chris Fletcher. Verilog: always @ Blocks

Common questions

Powered by AI

Insights from prior projects, such as Zhekai Zhang's MIPS CPU project, significantly contributed to the RISC-V CPU design by providing a foundational codebase for the cache and UART modules. Adapting these components from Zhang's code allowed leveraging existing implementations that follow best practices and have been tested in similar contexts . This not only accelerated development by avoiding the need to create these modules from scratch but also provided a framework for understanding and troubleshooting potential issues within these modules .

The RISC-V CPU project addresses data hazards through complete data forwarding, where data produced in the EX or MEM stages is directly passed to the ID stage. This minimizes RAW data hazards by allowing subsequent instructions to access the necessary data without waiting for the completion of the entire instruction cycle . However, the design still requires stalling if the producer instruction is a load instruction, which remains a limitation in completely eliminating stalls in the pipeline .

Automation through Makefiles improved the efficiency of the CPU project by streamlining the compilation and assembly of test programs using the RISC-V toolchain. By writing a Makefile, complex sets of commands for compiling and linking could be executed automatically, reducing manual input and potential for human error . This approach significantly reduced the overhead of program setup, allowed for easy repetition of similar tasks, and provided the developer with more time to focus on debugging and optimizing the CPU code instead of repetitive configuration tasks .

The main advantage of a 5-stage pipeline, implemented in the RISC-V CPU project, is the efficiency it brings to instruction processing by allowing multiple instructions to be processed at different stages simultaneously, thereby increasing throughput. The stages include Instruction Fetch (IF), Instruction Decode (ID), Execute (EX), Memory Access (MEM), and Write Back (WB). This pipeline design also includes complete data forwarding, which reduces data hazards by forwarding data from the EX or MEM stages directly to the ID stage, leading to fewer stalls during execution . The pipelining structure is crucial for maintaining high performance and efficient execution flows in the CPU design.

The pipeline's efficiency in the RISC-V CPU project is achieved by minimizing unused clock cycles, ensuring that each instruction cycle is productive and the pipeline stages remain active with instruction processing . However, the external factor of UART communication inhibits this efficiency. Due to its inherent latency, the UART protocol can make the pipeline appear sequential rather than parallel, as it forces instructions to pass through all stages before the next one can be fetched, especially when data dependencies occur . This bottleneck limits the effective throughput that pipelining is supposed to enhance.

The UART communication protocol can introduce latency because it serially transmits data between the CPU and the external memory simulator, potentially slowing down the instruction pipeline as it must wait for data transactions to complete . The cache design attempts to mitigate this by using an N-way set associative I-cache and D-cache, which enables faster data retrieval during repeated access patterns, such as loops. However, since UART is the main bottleneck, these caches primarily help in specific scenarios and don't fully resolve the inefficiencies caused by UART latency .

The FPGA implementation encountered several limitations, such as bugs that did not appear during simulation and issues with executing branch instructions correctly. The project identified that the original stall control design in Verilog led to latch generation, which is problematic for reliable execution on FPGA hardware . To address these limitations, future developments are expected to involve refining the Verilog code to avoid hidden traps like unintentional latches, improving simulation tools for better real-world fidelity, and enhancing debugging techniques to catch errors earlier in the development process .

The stall controller module in this RISC-V CPU architecture manages pipeline stalls by receiving stall requests from the IF, ID, and MEM stages and emitting stall signals to the appropriate modules to pause execution. This is crucial for handling instruction fetching and data dependencies that cause pipeline stalls . If this module malfunctions, it could lead to incorrect stalling, causing either unnecessary delays or insufficient stalls that would result in data hazards, ultimately leading to incorrect program execution or reduced CPU performance .

The absence of branch prediction in this RISC-V CPU impacts performance by increasing the likelihood of pipeline stalls, especially in programs with frequent branch instructions, as the CPU must wait until the branch decision is made without speculative execution. This limits instruction throughput and can degrade performance in branch-heavy code . Potential solutions include implementing dynamic branch prediction algorithms, such as two-level adaptive predictors, which can reduce misprediction penalties and improve overall pipeline utilization, albeit at the cost of increased complexity in the CPU design .

Testing the RISC-V CPU on an FPGA platform like Basys 3 posed challenges, as certain bugs not previously identified in the Vivado simulation emerged, such as branch instructions not executing correctly. This highlighted issues like latch generation caused by the original stall control design in Verilog HDL . The re-design to avoid latches reflects a key learning point, emphasizing the complexity and importance of understanding hardware-descriptive languages thoroughly. The experience showcased that simulations don't always replicate real hardware execution, prompting the need for more robust testing and debugging strategies .

You might also like