geniuseric
    • 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
    # Lab3: SoftCPU contributed by < [geniuseric](https://github.com/geniuseric) > ## Requirement [Assignment3: SoftCPU](https://hackmd.io/@sysprog/2021-arch-homework3) ## Setup Environment 1. Prepare GNU Toolchain for RISC-V. See [The xPack GNU RISC-V Embedded GCC](https://xpack.github.io/riscv-none-embed-gcc/). ```shell $ cd /tmp $ wget https://github.com/xpack-dev-tools/riscv-none-embed-gcc-xpack/releases/download/v10.2.0-1.2/xpack-riscv-none-embed-gcc-10.2.0-1.2-linux-x64.tar.gz $ tar zxvf xpack-riscv-none-embed-gcc-10.2.0-1.2-linux-x64.tar.gz $ cp -af xpack-riscv-none-embed-gcc-10.2.0-1.2 $HOME/riscv-none-embed-gcc ``` 2. Configure `$PATH`. ```shell $ cd $HOME/riscv-none-embed-gcc $ echo "export PATH=`pwd`/bin:$PATH" > setenv ``` Once step (1) and (2) are complete, you can simply update `$PATH` environment variable via: ```shell $ cd $HOME $ source riscv-none-embed-gcc/setenv ``` Check `$PATH` at the first time: ```shell $ riscv-none-embed-gcc -v ``` 3. Fetch [SRV32](https://github.com/sysprog21/srv32). ```shell $ cd $HOME $ git clone https://github.com/sysprog21/srv32 ``` 4. Run compliance tests (https://github.com/sysprog21/srv32). ```shell $ cd $HOME/srv32/tools/ $ make $ cd $HOME/srv32/sim/ $ make $ cd $HOME/srv32/tests/ $ make tests # Run test v1 for RTL $ make tests-sw # Run test v1 for ISS simulator ``` ## Simple 3-stage pipeline RISC-V processor - The detailed description is written in [here](https://hackmd.io/@sysprog/S1Udn1Xtt). - [SRV32](https://github.com/sysprog21/srv32) is a three-stage pipeline processor with IF/ID,EX,WB stages. - It passes RV32IM compliance test. - It supports **full forwarding**, which means data hazard can be resolved WITHOUT stalling the processor. - The **branch penalty** is 2. - The pipeline architecture is shown below. ![](https://i.imgur.com/9lbFKBM.jpg) ## Search Insert Position - The original C code is written in [searchInsert.c](https://github.com/geniuseric/Computer_Architecture/blob/master/HW1/searchInsert.c). - In order to run on SRV32, I modify int to **volatile** int. - Also, I change ```mid = (down + up) / 2``` to ```mid = (down + up) >> 1``` for optimization. - I put [searchInsert](https://github.com/geniuseric/Computer_Architecture/tree/master/HW3/searchInsert) folder which contains the modified C code and Makefile into ```$HOME/srv32/sw``` folder. - Run code for RTL and ISS simulators. ```shell $ cd $HOME/srv32 $ make searchInsert ``` - The result is shown below. ```c= make[1]: Entering directory '/home/airobots/srv32/sw' make -C common make[2]: Entering directory '/home/airobots/srv32/sw/common' make[2]: Nothing to be done for 'all'. make[2]: Leaving directory '/home/airobots/srv32/sw/common' make[2]: Entering directory '/home/airobots/srv32/sw/searchInsert' riscv-none-embed-gcc -O3 -Wall -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -L../common -o searchInsert.elf searchInsert.c -lc -lm -lgcc -lsys -T ../common/default.ld riscv-none-embed-objcopy -j .text -O binary searchInsert.elf imem.bin riscv-none-embed-objcopy -j .data -O binary searchInsert.elf dmem.bin riscv-none-embed-objcopy -O binary searchInsert.elf memory.bin riscv-none-embed-objdump -d searchInsert.elf > searchInsert.dis riscv-none-embed-readelf -a searchInsert.elf > searchInsert.symbol make[2]: Leaving directory '/home/airobots/srv32/sw/searchInsert' make[1]: Leaving directory '/home/airobots/srv32/sw' make[1]: Entering directory '/home/airobots/srv32/sim' The insert position is 4 Excuting 1705 instructions, 2217 cycles, 1.300 CPI Program terminate - ../rtl/../testbench/testbench.v:418: Verilog $finish Simulation statistics ===================== Simulation time : 0.018 s Simulation cycles: 2228 Simulation speed : 0.123778 MHz make[1]: Leaving directory '/home/airobots/srv32/sim' make[1]: Entering directory '/home/airobots/srv32/tools' ./rvsim --memsize 128 -l trace.log ../sw/searchInsert/searchInsert.elf The insert position is 4 Excuting 1705 instructions, 2217 cycles, 1.300 CPI Program terminate Simulation statistics ===================== Simulation time : 0.001 s Simulation cycles: 2217 Simulation speed : 4.024 MHz make[1]: Leaving directory '/home/airobots/srv32/tools' Compare the trace between RTL and ISS simulator === Simulation passed === ``` ## View Waveform - Install [GTKWave](http://gtkwave.sourceforge.net/) and open it. ```shell $ sudo apt install gtkwave $ gtkwave ``` - Load the file ```$HOME/srv32/sim/wave.fst```. - Show the signals/events of the branch instruction ```beq a4,a5,ec``` at instruction address ```0x90```. ![](https://i.imgur.com/1lqB7KA.png) - Explain the instrction: 1. The previous instruction of 0x90 is ```0x8c```, which is ```lw a5,8(sp)```. The value of register ```a5``` is forwarded from 0x8c in WB stage to 0x90 in EX stage. 2. The time when 0x90 is in EX stage, the values of register ```a4``` and ```a5``` are equal. Therefore, the branch is taken and the next instruction of 0x90 should be ```0xec```. 3. Control signals ```ex_flush``` and ```wb_flush``` show when instructions ```0x94``` and ```0x98``` are flushed. Two instructions match the number of **branch penalty**, which is 2. ## Propose Software Optimization - For the further optimization, I eliminate **while** loop and use **goto** syntax instead. - The optimized code is written in [opt_searchInsert.c](https://github.com/geniuseric/Computer_Architecture/blob/master/HW3/opt_searchInsert/opt_searchInsert.c). - The optimized result is shown below. It saves ```2217-2214=3``` cycles. ```c= make[1]: Entering directory '/home/airobots/srv32/sw' make -C common make[2]: Entering directory '/home/airobots/srv32/sw/common' make[2]: Nothing to be done for 'all'. make[2]: Leaving directory '/home/airobots/srv32/sw/common' make[2]: Entering directory '/home/airobots/srv32/sw/opt_searchInsert' riscv-none-embed-gcc -O3 -Wall -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -L../common -o opt_searchInsert.elf opt_searchInsert.c -lc -lm -lgcc -lsys -T ../common/default.ld riscv-none-embed-objcopy -j .text -O binary opt_searchInsert.elf imem.bin riscv-none-embed-objcopy -j .data -O binary opt_searchInsert.elf dmem.bin riscv-none-embed-objcopy -O binary opt_searchInsert.elf memory.bin riscv-none-embed-objdump -d opt_searchInsert.elf > opt_searchInsert.dis riscv-none-embed-readelf -a opt_searchInsert.elf > opt_searchInsert.symbol make[2]: Leaving directory '/home/airobots/srv32/sw/opt_searchInsert' make[1]: Leaving directory '/home/airobots/srv32/sw' make[1]: Entering directory '/home/airobots/srv32/sim' The insert position is 4 Excuting 1702 instructions, 2214 cycles, 1.300 CPI Program terminate - ../rtl/../testbench/testbench.v:418: Verilog $finish Simulation statistics ===================== Simulation time : 0.019 s Simulation cycles: 2225 Simulation speed : 0.117105 MHz make[1]: Leaving directory '/home/airobots/srv32/sim' make[1]: Entering directory '/home/airobots/srv32/tools' ./rvsim --memsize 128 -l trace.log ../sw/opt_searchInsert/opt_searchInsert.elf The insert position is 4 Excuting 1702 instructions, 2214 cycles, 1.301 CPI Program terminate Simulation statistics ===================== Simulation time : 0.001 s Simulation cycles: 2214 Simulation speed : 3.919 MHz make[1]: Leaving directory '/home/airobots/srv32/tools' Compare the trace between RTL and ISS simulator === Simulation passed === ```

    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