Pin-Cheng
    • 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
    # Asssigment3: SoftCPU ## Setting up the Environment ### Follow the github, install RISC-V toolchains. 1. get this `xpack-riscv-none-embed-gcc-10.2.0-1.2-linux-x64.tar.gz` and do `tar zxvf xpack-riscv-none-embed-gcc-10.2.0-1.2-linux-x64.tar.gz`. 2. `cp -af xpack-riscv-none-embed-gcc-10.2.0-1.2 $HOME/riscv-none-embed-gcc` 3. cd $HOME/riscv-none-embed-gcc ,do `export CROSS_COMPILE=riscv-none-embed-` 4. `sudo apt install build-essential ccache` 5. cd $HOME ```clike= sudo apt install autoconf automake autotools-dev curl gawk git \ build-essential bison flex texinfo gperf libtool patchutils bc git \ libmpc-dev libmpfr-dev libgmp-dev gawk zlib1g-dev libexpat1-dev git clone --recursive https://github.com/riscv/riscv-gnu-toolchain cd riscv-gnu-toolchain mkdir -p build && cd build ../configure --prefix=/opt/riscv --enable-multilib make -j$(nproc) ``` 6. ```clike= cd ~/ git clone https://github.com/sysprog21/srv32 ``` 7. ```clike= $ cd $HOME/riscv-none-embed-gcc $ echo "export PATH=`pwd`/bin:$PATH" > setenv ``` 8. ```clike= $ cd $HOME $ source riscv-none-embed-gcc/setenv ``` 9. ```clike= cd ~/srv32/tools/ make ``` 10. ```clike= cd ~/srv32/sim/ make ``` 11. ```clike= cd ../tests/ make tests-sw ``` Then, output successed!! ![](https://i.imgur.com/n9xGw3Y.png) ## Requirement 1 First, I tried the example code in srv32(hello.c). ```c= $ cd .. $ make hello ``` Then, I got the print output. ![](https://i.imgur.com/USneLZ5.png) Second, I tried to execute my assignment 1. There is my C code of my [assignment 1](https://hackmd.io/@Pin-Cheng/r1U3mW-IF): ```clike= # include <stdio.h> # include <stdlib.h> int isPowerOfFour(int n){ if (n==0) return 0; if (n==1 || n==4) return 1; if (n%4==0) return isPowerOfFour(n/4); else return 0; } int main(void){ int a=256; int ans=isPowerOfFour(a); if (ans==1){ printf("%d is power of four\n",a); } else if (ans==0){ printf("%d is not power of four\n",a); } return 0; } ``` I overwrite my code in sample hello.c -- `srv32/sw/hello/hello.c`. Except `.c` , I also need the `Makefile`. There is my `Makefile`, and I put it as `srv32/sw/hello/Makefile`. ```clike= include ../common/Makefile.common EXE = .elf SRC = hello.c CFLAGS += -L../common LDFLAGS += -T ../common/default.ld TARGET = hello OUTPUT = $(TARGET)$(EXE) .PHONY: all clean all: $(TARGET) $(TARGET): $(SRC) $(CC) $(CFLAGS) -o $(OUTPUT) $(SRC) $(LDFLAGS) $(OBJCOPY) -j .text -O binary $(OUTPUT) imem.bin $(OBJCOPY) -j .data -O binary $(OUTPUT) dmem.bin $(OBJCOPY) -O binary $(OUTPUT) memory.bin $(OBJDUMP) -d $(OUTPUT) > $(TARGET).dis $(READELF) -a $(OUTPUT) > $(TARGET).symbol clean: $(RM) *.o $(OUTPUT) $(TARGET).dis $(TARGET).symbol [id]mem.bin memory.bin ``` Finally, I can get my code started. ![](https://i.imgur.com/AX5FTyP.png) As you can see, Ouput answer `256 is power of four` is correct. ## Requirement 2 ### Download GTKWave ``` $ sudo apt-get update $ sudo apt-get install gtkwave ``` And gtkwave is in your ubuntu now. click the gtkwave.exe ![](https://i.imgur.com/z82YwFl.png) file->open `wave.fst`(../srv32/sim/). ![](https://i.imgur.com/qI74EOG.png) Next, pull some signals in. ![](https://i.imgur.com/KrJihHj.png) In this case, I found that the branch hazard penalty is two clock cycles. ![](https://i.imgur.com/rAF3UIx.png) ## Requirement 3 After the simulation of the RISCV-gcc toolchain generated assembly file, I found that there are some places of my origin code to get better. I found the assembly file generated by RISCV-gcc toolchain is in `/srv32/sw/hello/hello.dis`. So I look at it. ### Fewer instructions * As for some instruction, actually I don't need to do those everytime when the function called. Details In my origin code: This is a loop for my function, everytime when the function is called recursively, it will go through those instructions. The assignment in `line 5` and `line 6`, it will be assigned everytime as 1 and 4, this is not necessary. ```clike= isPowerOfFour: addi sp, sp, -4 sw ra, 0(sp) beq a0, zero , false addi t3, zero, 1 addi t6, zero, 4 beq a0, t3 , true beq a0, t6 , true loop: andi t4 , a0 , 3 bne t4,zero,false srli a0,a0,2 jal ra, isPowerOfFour add a1,t5,a1 j res2 ``` In RISCV-gcc toolchain assembly file: It just need to assign one time(`line 54`, `line 58`) , out of the function loop code that `line 64` to `line 84` is function loop. ```clike= 54: 00100713 li a4,1 58: 00400693 li a3,4 5c: 00078a63 beqz a5,70 <isPowerOfFour+0x34> 60: 0340006f j 94 <isPowerOfFour+0x58> 64: 02e50463 beq a0,a4,8c <isPowerOfFour+0x50> 68: 02d50263 beq a0,a3,8c <isPowerOfFour+0x50> 6c: 02079463 bnez a5,94 <isPowerOfFour+0x58> 70: 41f55793 srai a5,a0,0x1f 74: 0037f793 andi a5,a5,3 78: 00a78533 add a0,a5,a0 7c: 40255513 srai a0,a0,0x2 80: 00357793 andi a5,a0,3 84: fe0510e3 bnez a0,64 <isPowerOfFour+0x28> ``` ### Eliminate unnecessary stalls * Some instructions with RAW data hazard must encounter stall to let the pipeline can get the data correctly. Details In my origin code: In every loop, I have to do this which leads to a stall. ```clike= andi t4 , a0 , 3 bne t4,zero,false ``` In RISCV-gcc toolchain assembly file: But actually, we can deal with this problem. It executes the value of `andi t4 , a0 , 3` in the end of last loop, so it doesn't have to do this in the current loop. As you can see, the `andi` is executed at the end of the loop(line 80). And the `bne` is executed at the top of the loop(line 6c). So they did't encounter hazard. ```clike= 64: 02e50463 beq a0,a4,8c <isPowerOfFour+0x50> 68: 02d50263 beq a0,a3,8c <isPowerOfFour+0x50> 6c: 02079463 bnez a5,94 <isPowerOfFour+0x58> 70: 41f55793 srai a5,a0,0x1f 74: 0037f793 andi a5,a5,3 78: 00a78533 add a0,a5,a0 7c: 40255513 srai a0,a0,0x2 80: 00357793 andi a5,a0,3 84: fe0510e3 bnez a0,64 <isPowerOfFour+0x28> ``` ## Requirement 4 The progresses of how srv32 works are already recorded in the former of my note. In order to achieve this homework, I have learned many things, including srv32 pipeline and its waveform. ###### tags: `jserv Computer Architecture 2021`

    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