呂紹樺
    • 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
    • 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 Versions and GitHub Sync Note Insights 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Assignment3 Reindeer - RISCV RV32I[M] Soft CPU ###### tags: `Computer Architecture` ### How RISC-V Compliance Tests works? According to the "RISC-V Compliance Tests" documentation, the goal of compliance tests is to check whether the processor under development meets the open RISC-V standards or not. The Reindeer soft CPU uses an OCD to load code/data. And for the verilator simulation, a C++ testbench will replace the OCD. Take `I-ADD-01.elf` for example, the testbench will invoke the toolchain (objdump, readelf) to extract code/data from sections of the .elf file. Then testbench will mimic the OCD bus to load the code/data into CPU's memory to compare it with the signatures and finish the compliance test. ![](https://i.imgur.com/JbxkKyo.png) The followings are some code segments of `tb_PulseRain_RV2T.cpp`, which plays an important role in the task of the compliance test. There are several steps for the testbench to accomplish the compliance test. 1.`tb_PulseRain_RV2T.cpp` process the .elf file to extract `_start`, `begin_signature` and `end_signature`. ![](https://i.imgur.com/LsWPJdY.png) 2.Parse elf file to extract information for each section. ![](https://i.imgur.com/eUuKVMk.png) 3.Create a UUT and Testbench instance ![](https://i.imgur.com/zxGTkyT.png) 4.Reset the input signal of the UUT ![](https://i.imgur.com/7in2YHG.png) 5.Start to assert the signal and load text/data sections into UUT's memory ![](https://i.imgur.com/KXxnTzp.png) 6.Matching signature while peeking the memory ![](https://i.imgur.com/2iQXdQf.png) After the compilance test is done, you can check if the signature is match or not. ![](https://i.imgur.com/82dAsf4.png) ### Why signature should be matched? To ensure the processor work properly when executing different types of instructions. ### How Reindeer works with Verilator? Nowadays, we use RTL languages such as Verilog to discribe hardware components to design an integrated circuit(IC). Since Reindeer is a soft CPU of Von Neumann architecture, you can say that it is a piece of RTL script. On the other hand, Verilator "Verilates" the specified Verilog or SystemVerilog code by reading it, performing lint checks, and optionally inserting assertion checks and coverage-analysis points. It outputs single- or multi-threaded .cpp and .h files, the "Verilated" code. This implies that we can use Verilator to "Verilates" Reindeer into C++ script. Different from verilog, which is hardware-oriented, C++ is more behavior-oriented. That is, the "Verilation" process lets us to check the Reindeer from a behavioral point of view. With Verilator, we can write the testbench in C++ format instead of verilog. This makes the development of RISC-V processor much more intuitive. ### Advantages of 2 x 2 Pipeline In the 2 x 2 layout, each stage is active every other clock cycle. For the even cycle, only IF and EXE stages are active, while for the odd cycle, only ID and MEM stages are active. This implies that only one instruction comes into pipeline for every two cycles. * The IF and MEM stage always happen on different clock cycles, thus to avoid the structural hazard caused by the single port memory. * Comparing to a 5-stage pipeline, Reindeer merges MEM and WB stage into one stage. So we don't need MEM/WB pipeline register anymore. On the other hand, since an instruction only come into pipeline every two cycles, there is no need to consider data hazard which only needs one-cycle stalled to be solved . So this could save us from implementing some forwarding units. These factors might potentially reduces the cost of implementing the processor. * Since there are only two stages active seperately in the total four stages pipline for every clock cycle, we can be less concern about the hold time and setup time of the pipeline register. Thus, cycle time can also be reduced. ### “Hold and Load” and Bootstrapping #### “Hold ” state The soft CPU and the OCD can share the same UART port, as illustrated below. The RX signal goes to both the soft CPU and OCD, while the TX signal has to go through a mux. And that mux is controlled by the OCD. ![](https://i.imgur.com/4at01nu.png) After reset, the soft CPU will be put into a hold state, and it will have access to the UART TX port by default. But a valid debug frame sending from the host PC can let OCD to reconfigure the mux and switch the UART TX to OCD side, for which the memory can be accessed, and the control frames can be exchanged. #### Loading image during Hold state A new software image can be loaded into the memory during the CPU hold state. After the memory is loaded with the new image, the OCD can setup the start-address of the soft CPU, and send start pulse to make the soft CPU active. At that point, the OCD can switch the UART TX back to the CPU side for CPU's output. #### Bootstrapping The testbench will mimic the OCD bus to load the code/data into CPU's memory. Afterwards, the start-address of the .elf file ("_start" or "__start" symbol) will be passed onto the CPU, and turn the CPU into active state. ## Modify the assembly programs as new test case for Reindeer Simulation with Verilator ### Steps to generate .elf, .elf.objdump and .signature files :::info Prequisites: Prepare GNU Toolchain for RISC-V and configure $PATH as [assignment 2](https://hackmd.io/@sysprog/rJAufgHYS) did (Step1 and Step2). ::: 1. Clone `imperas-riscv-tests` repository from github to your file by inputting `git clone https://github.com/riscv-ovpsim/imperas-riscv-tests.git` to linux terminal ![](https://i.imgur.com/QjcLaD1.png) 2. Modify the text of Makefile in the `imperas-riscv-tests` file as following: ![](https://i.imgur.com/BJZQMQp.png) 3. Because the `riscvOVPsim.exe` is in a different directory, you need to add that path to the environment variable. For example, my `imperas-riscv-test` folder is located at the `root`. My command to set path is: `PATH=$PATH:~/imperas-riscv-tests/riscv-ovpsim/bin/Linux64` ![](https://i.imgur.com/cBmcEGU.png) :::info You can input the command `echo $PATH` to the terminal to check if the environment variables is correct or not. ![](https://i.imgur.com/WerEL7W.png) ::: 4. Put your .S file into `imperas-riscv-tests/riscv-tests-suite/rv32i/src` folder ![](https://i.imgur.com/mBlh5TX.png) Put your .reference_output file into `imperas-riscv-tests/riscv-tests-suite/rv32i/references` folder ![](https://i.imgur.com/qE7whvQ.png) 5. Add your project name to the Makefrag file, which is located at the rv32i folder. ![](https://i.imgur.com/G2gvBmn.png) 6. Now go to the the `imperas-riscv-tests` folder and input `make simulate verify`. ![](https://i.imgur.com/ZcIozrd.png) Then your .S file will be converted into 3 files. You can find it in the `work` folder. ![](https://i.imgur.com/XPzBVFF.png) ### Specify the .S file for the compliance test To write our own .S file, let's take a look at an example of a test case. Here is a code segment of file `I-ADD-01.S`: :::info * .S (capital S) stands for assembly code that must still pass through a pre-processor. That means it can have #include and #define among other macros. It can also be seeing as extension .sx * .s (lower s) is pure assembly code that can be compiled into an object ::: ![](https://i.imgur.com/9JVNnql.png) You can find that most of the marco be written in the .S file is `TEST_RR_OP`. In this example, the type of instruction being tested is `add`. And the `TEST_RR_OP` marco can be found in `riscv_test_macros.h` header file. ![](https://i.imgur.com/a0PZTSQ.png) ![](https://i.imgur.com/SrGFFdK.png) Take the first test marco `TEST_RR_OP(add, x0, x31, x16, 0x0, -0x1, 0x0, x5, 0, x6)` for example, it will be parsed to: ``` TEST_CASE(x6, x0, 0x0, x5, 0,\ li x31, MASK_XLEN(-0x1);\ li x16, MASK_XLEN(0x0);\ add x0, x31, x16; ) ``` And can be further parsed to: ``` li x31, MASK_XLEN(-0x1); li x16, MASK_XLEN(0x0); add x0, x31, x16; sw x0, 0(x5); RVTEST_IO_ASSERT_GPR_EQ(x6, x0, 0x0) ``` To make it clearly, it means : ``` li reg1, MASK_XLEN(val1); li reg2, MASK_XLEN(val2); inst desreg, reg1, reg2; sw desreg, 0(swreg); RVTEST_IO_ASSERT_GPR_EQ(testreg, desreg, correctval) ``` According to the section `3.2. Process` in the [documentation](https://github.com/riscv/riscv-compliance/blob/master/doc/README.adoc#process). :::info * Use the `RVTEST` macros (defined in `compliance_io.h`) to make it easy to see the details of a Test’s execution. There are macros for assertions (`RVTEST_IO_ASSERT_GPR_EQ`) and tracing (`RVTEST_IO_WRITE_STR`) which are empty on targets that can not implement them. ::: So`RVTEST_IO_ASSERT_GPR_EQ` function is to assert the signal in the CPU. Therefore, I wrote my .S file as following: ```clike= #include "riscv_test_macros.h" #include "compliance_test.h" #include "compliance_io.h" RV_COMPLIANCE_RV32M RV_COMPLIANCE_CODE_BEGIN RVTEST_IO_INIT RVTEST_IO_ASSERT_GPR_EQ(x31, x0, 0x00000000) RVTEST_IO_WRITE_STR(x31, "# Test Begin\n") lw x18, num1 lw x19, num2 la x3, res_addr beqz x18, assign loop: mv x20, x19 mod: sub x5, x18, x19 mv x18, x5 bgez x5, mod add x5, x18, x19 mv x18, x20 bnez x19, loop assign: mv x18, x19 sw x18, 0(x3) RVTEST_IO_WRITE_STR(x31, "# Test End\n") RV_COMPLIANCE_HALT RV_COMPLIANCE_CODE_END # Input data section. .data num1: .word 0x00000078 #decimal=120 num2: .word 0x0000004E #decimal=78 # Output data section. RV_COMPLIANCE_DATA_BEGIN res_addr: .fill 4, 4, -1 RV_COMPLIANCE_DATA_END ``` ## Execute .elf file on the Reindeer Put the 3 files I mentioned before into the `Reindeer/sim/compliance` folder. ![](https://i.imgur.com/TBACh7B.png) Access the `Reindeer/sim/verilator` folder and input `make test GCD`. ![](https://i.imgur.com/d0XyP21.png) ![](https://i.imgur.com/DSjwJUn.png) The test bench load .text section of the .elf file into the memory. The memory address to store machine instructions is from 0x8000000 to 0x80000138. ## Using GTKwave to check the wave ### Signals/events inside Reindeer Reindeer SoftCPU takes two clock cycles to fetch one instruction. ![](https://i.imgur.com/EaJoyka.png) *** Register read and write only occurs in the even cycle. ![](https://i.imgur.com/c2C5vkU.png) *** Writing the .elf text section into the memory. In this case, machine instrction `0x34202f73` is write to the memory address `0x80000004`, `0x00800f93` is write to the `0x80000008`, and so on. ![](https://i.imgur.com/SRfjS4f.png)

    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