Welly0902
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CA 2021 Term Project - Analyze kleine-riscv and validate its pipeline design In this project, despite I have no experience on Verilog, I will try to use all the knowledge of computer architecture I learned in the class this semester to learn and understand how a processor works and implements. There will be two parts of sharing in this article. First, I will show my experience of setting up the kleine-riscv step-by-step, to help other beginners like me who want to try using kleine-riscv but can hardly find document on official Github or anywhere else on the internet. Second, I will try to read the Verilog code and combine the stuffs I've learned in the class about pipeline, control signals and hazard to learn how to design a processor. I will share what I've learned during this process. Without further ado, let's started! ## Implement kleine-riscv on Ubuntu [Kleine-riscv](https://github.com/rolandbernard/kleine-riscv) is a small RISC-V core written in synthesizable Verilog that supports the RV32I unprivileged ISA and parts of the privileged ISA, namely M-mode. `# M-mode is for simple embedded systems that run trusted application code with no memory protection (beyond trapping non-existent memory addresses); very low cost to implement.` To implement the kleine-riscv, first we prepare an operating system, in my case Ubuntu. And then download the code of kleine-riscv form Github. ``` git clone https://github.com/rolandbernard/kleine-riscv.git ``` After downloading, we change the directory and run make. ``` # change dir cd kleine-riscv # make file make ``` Ohh no, first error message... ![](https://i.imgur.com/lqDteZ9.png) Fix it with installing the `clang` library. ``` sudo apt-get install clang ``` Try again! Ohh no, not again... ![](https://i.imgur.com/dfPae8e.png) From the error message, it seems like the `ld` has no emulation mode it wants. This is because the `ld` in my Ubuntu is supported by the GNU Scientific Library(gsl). And seems like this ld is not working. So I reinstall the `ld` by intalling the LLVM Compiler Infrastructure Project. ``` $ git clone https://github.com/llvm/llvm-project llvm-project $ mkdir build $ cd build $ cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../llvm-project/llvm $ make install ``` And then remember to add the path to the system library. (For kleine-riscv, we need `ld.lld`) ``` sudo ln -s /home/harvey/llvm-project/build/bin/ld.lld /usr/bin/ld ``` Now we can finally run `make`, from the `makefile` we can see it will run `make sim` and `make test`. `make sim` will create the build directory and generate the `Vcore` that is the core program to run C program or riscv program; After Vcore was established, `make test` will scan for the .c(c code) and .S(assembly code) file in `tests` directory and build the ELF file in `/kleine-riscv/tests/build`, then use Vcore to run ELF file. ![](https://i.imgur.com/imTsvJo.png) These test code will use the header file in `/kleine-riscv/tests/include`. ![](https://i.imgur.com/GKi5Rew.png) You can design your own C/assembly programs, design the test case and see if the output is correct. For example, in `/kleine-riscv/tests/misc/prime.c` we alter the test case #5 ![](https://i.imgur.com/DFKeTq2.png) run `make test` and get error message because 4 is not a prime number. ![](https://i.imgur.com/OaxuZLh.png) To conclude the first part, we try to set up the kleine-riscv, fixed some obstacles and observe what it is capable of. There is one concern that the `Vcore` won't show any message when the instruction is successfully done. So it took me a lot of time to figure out what it can do. ## Analyse the design & architecture of klein-riscv processor In the second part, because kleine-riscv is a 5-staged pipeline processor, so based on the knowledge I've learned about pipeline, control signals and hazard in the course. I will try to compare with the course materials to see the difference and understand how the processor is designed, so maybe someday I can design my own processor. Let's look at the architecture of the 5-staged pipeline processor from the lecture materials first. ![](https://i.imgur.com/mHkYOpu.png) I assume the `kleine-riscv` will look very much like this, so now I will start look through the Verilog code and see if it is similar. First, we start from observing the structure of it. ``` └── kleine-riscv/ └── src/ ├──pipeline/ │ ├── decode.v │ ├── execute.v │ ├── fetch.v │ ├── memory.v │ ├── pipeline.v │ └── writeback.v ├── units/ │ ├── alu.v │ ├── busio.v │ ├── cmp.v │ ├── csr.v │ ├── hazard.v │ └── regfile.v ├── core.v └── params.vh ``` From `pipeline`, we can see there are 5 stages of pipeline `IF`,`ID`,`EX`,`MEM` and `WB`. From `units`, we can guess is the components in the graph above, such as `alu`, `bus io`, `branch compare unit`, `control signal register`,`hazard bypass wire` and `register` Now we'll dive into some Verilog code and see what is inside. Start from the components in `units`. * `alu.v` This component is for calculating stuffs. There are inputs for 2 operands a and b(32-bits). Also `funct3` input to select which operator will the alu execute; A `function_modifier` input as signal input. The following are the operators supported by alu. ![](https://i.imgur.com/TInj4s7.png) However, in the code, there are 2 outputs which are 1st cycle and 2nd cycle output that I still have no idea why. * `busio.v` This part is the communication signal between register and memory. * `cmp.v` This part compares input data a and b. Also input unsign signal to decide whether is signed number. Moreover, output the signal of branch equal and branch less than. Exactly like the graph above shows. * `csr.v` Control signal register. This part of code is hard to understand. * `hazard.v` This part is the unit to identify whether the pipeline has hazard. Its goal is to output two signals: stall and invalidate, to help all of the components in pipeline to know how to deal with the data. * `regfile.v` In this component, it is the register in `ID` stage. There are inputs of rs1, rs2 and rd register address(5-bits), rd data(32-bits); Output of rs1 and rs2 data. Also a signal input clk to update the rd register when the clock value is positive. So now, after we briefly went through the code. Let's see how does the processor works to deal with the hazards. Kleine-riscv uses the hazard unit to deal with the hazards. These are the hazards it deals with: **EX data hazard**, **MEM data hazard** and **Control hazard**. ![](https://i.imgur.com/GjbleuN.png) First, from `pipeline/execute.v` we can see that there's input to choose whether the data go into the ALU is the original data from the registers or the data sent from EX/MEM to ID. ![](https://i.imgur.com/Wxea5Z8.png) So we can learned that kleine-riscv has the mechanism of bypassing value from one stage to another. And then we check the `units/hazard.v` to see what and how it design to deal with these hazards. We know that the hazard unit will generate the `stall` and `invalidate` signal to establish the system to fix hazard. If `stall=True`, the components in pipeline will stall and do nothing. And if `stall=False`, it will also check `invalidate` and make sure it is `False` to proceed what they supposed to do. So let's see how these two signals generate. From the following chunk of code: ![](https://i.imgur.com/ps9cOJ6.png) We learned that if `reset` is true, all of the stage will get the `invalidate=True` signal; If `trap_inalidate` is True then the `branch_invalidate` is also True. That also will get `invalidate=True`. ![](https://i.imgur.com/bPTs1w8.png) So why it has to use two variables to get the same result? It is because of the scenario when **branch is taken**. The `invalidate` will be true when the branch is take, so the IF, ID and EX should flush (do nothing) what they are doing when the branch taken or not is decided during EX stage. About the `EX` and `MEM` data hazard, we can observe how they work from this chunk of code: ![](https://i.imgur.com/vcpSThm.png) We can see from first section, it will check if the register address of 1 and 2 in the `ID` stage instruction is the same as the destination register's address of `EX` stage instruction. This is the scenario of the destination register being used in the next 2 instructions. ![](https://i.imgur.com/M8lx87z.png) In the second section, it checks if the register address of 1 and 2 in the `ID` stage instruction is the same as the destination register's address of `MEM` stage instruction. This is the scenario of the destination register being used in the next instruction when now there is a `lw` instruction in the `EX` stage. ![](https://i.imgur.com/2O20qKq.png) These two scenario will cause the `IF` to stall. After observing the design of how the processor deal with hazards, however, I'm not sure if the design can check if the 2nd instruction behind in scenario 1 can be detected. ``` add t0,t1,t2 sub t4,t0,t3 and t5,t0,t6 # this instruction ``` Because this instruction will also cause data hazard. So maybe it should also check the `IF` stage instruction's register 1 and 2 address to make the process more complete. ```=verilog ###units/hazard.v### module hazard ( input reset, ##### add this // from fetch input valid_fetch, input [4:0] rs1_address_fetch, input [4:0] rs2_address_fetch, input uses_rs1_fetch, input uses_rs2_fetch, ##### // from decode input valid_decode, input [4:0] rs1_address_decode, #change variable name input [4:0] rs2_address_decode, #change varaible name input uses_rs1, input uses_rs2, input uses_csr, . . . ) wire data_hazard = valid_decode && ( (valid_execute && rd_address_execute != 0 && ( uses_rs1_decode && rs1_address_decode == rd_address_execute || uses_rs2_decode && rs2_address_decode == rd_address_execute )) #change varaible name ##### add here ||(valid_execute && rd_address_execute != 0 && ( uses_rs1_fetch && rs1_address_fetch == rd_address_execute || uses_rs2_fetch && rs2_address_fetch == rd_address_execute )) ##### || (valid_memory && rd_address_memory != 0 && !bypass_memory && ( uses_rs1 && rs1_address_decode == rd_address_memory || uses_rs2 && rs2_address_decode == rd_address_memory )) || uses_csr && ( csr_write_execute && valid_execute || csr_write_memory && valid_memory || csr_write_writeback && valid_writeback )); ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully