peishan
    • 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
    # Assignment3: SoftCPU ###### tags: `computer architure 2021` ## Introduction ### How Does RISC-V Compliance Tests Works The RISC-V Architectural Test suite is decribed like a minimal filter in [README](https://github.com/riscv-non-isa/riscv-arch-test/blob/master/doc/README.adoc), and we can find more information here. > The result that the architecture tests provide to the user is an assurance that the specification has been interpreted correctly and the implementation under test (DUT) can be declared as RISC-V Architecture Test compliant. The result that the architecture tests provide to the user is an assurance that the specification has been interpreted correctly and the implementation under test (DUT) can be declared as RISC-V Architecture Test compliant. > According to the RISC-V website, we can find several steps of complliance. ![](https://i.imgur.com/7v3Xte1.png) #### Why signature should be matched? To ensure the processor work properly when executing different types of instructions. #### How srv32 works with Verilator The simulator generated by Verilator is called sim. This is a RTL-level generator that is capable of simulating the execution of RISC-V binary at RTL level. RTL (Register-Transistor-Level) simulation is done on either Verilator (default) or Icarus Verilog.RTL simulator is located in sim/ directory.Result make all command build the core and run RTL sim, all simulation passed. ## Assignment ### Part 1 -- Makefile in sw Directory #### Rewrite C-Code Observation: * Need to add `volatile` before `int` and `char`. * In the function `printf`, we have to add `\n` at the end of sentence. ```c= #include <stdio.h> volatile int lengthOfLastWord(volatile char * s) { while(*s != 0) { s++; } volatile int position = 0; s--; while(*s == 32) { s--; } while(*s != 32) { position++; s--; } printf("\nLength of the last word is %d\n", position); } int main() { volatile char *string = "Hello World"; lengthOfLastWord(string); return 0; } ``` #### Process of Compiling in RISC-V I spent lots of time reading the information of [srv32](https://github.com/sysprog21/srv32) and [kuopinghsu](https://github.com/kuopinghsu/srv32/tree/86bd7f9b7278b17310d2fd4fca44e03b110bc45a). After reading the makefile in srv32 and makefile in dhrystone, we can find that the function of makefile in dhrystone is similar to the function we want for compiling our file. In this way, we can try to modify the makefile and our code to compile. ##### step 1. Create a folder and put your code, makefile(copy from dhrystone), and syscall.c(copy from dhrystone). ![](https://i.imgur.com/tA8ytqE.png) ##### step 2. Modity the makefile. Change line 4 `SRC = dhry_1.c dhry_2.c syscall.c` to `SRC = filename syscall.c` ![](https://i.imgur.com/Wn0rSnF.png) ##### step 3. Back to srv32 and do `make LOLW` ``` peishan@peishan-VirtualBox:~/srv32$ make LOLW ``` And it would come out the message and your program result as follow. ``` make[1]: Entering directory '/home/peishan/srv32/sw' make -C common make[2]: Entering directory '/home/peishan/srv32/sw/common' make[2]: Nothing to be done for 'all'. make[2]: Leaving directory '/home/peishan/srv32/sw/common' make[2]: Entering directory '/home/peishan/srv32/sw/LOLW' riscv-none-embed-gcc -O3 -Wall -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -DTIME -DRISCV -DHZ=100000000 -L../common -Wno-return-type -Wno-implicit-function-declaration -Wno-implicit-int -o LOLW.elf Length_of_Last_Word.c syscall.c -lc -lm -lgcc -lsys -T ../common/default.ld riscv-none-embed-objcopy -j .text -O binary LOLW.elf imem.bin riscv-none-embed-objcopy -j .data -O binary LOLW.elf dmem.bin riscv-none-embed-objcopy -O binary LOLW.elf memory.bin riscv-none-embed-objdump -d LOLW.elf > LOLW.dis riscv-none-embed-readelf -a LOLW.elf > LOLW.symbol make[2]: Leaving directory '/home/peishan/srv32/sw/LOLW' make[1]: Leaving directory '/home/peishan/srv32/sw' make[1]: Entering directory '/home/peishan/srv32/sim' Length of the last word is 5 Excuting 2076 instructions, 2740 cycles, 1.319 CPI Program terminate - ../rtl/../testbench/testbench.v:418: Verilog $finish Simulation statistics ===================== Simulation time : 0.184 s Simulation cycles: 2751 Simulation speed : 0.0149511 MHz make[1]: Leaving directory '/home/peishan/srv32/sim' make[1]: Entering directory '/home/peishan/srv32/tools' ./rvsim --memsize 128 -l trace.log ../sw/LOLW/LOLW.elf Length of the last word is 5 Excuting 2076 instructions, 2740 cycles, 1.320 CPI Program terminate Simulation statistics ===================== Simulation time : 0.026 s Simulation cycles: 2740 Simulation speed : 0.107 MHz make[1]: Leaving directory '/home/peishan/srv32/tools' Compare the trace between RTL and ISS simulator === Simulation passed === ``` ### Part 2 - Waveform Open the `wave.fst` in gtkwave. And we can choose the component we want to see its waveform. Here I insert imem, dmem, clock, pc, branch and some internal instructions. ![](https://i.imgur.com/6sAbXFX.png) ![](https://i.imgur.com/KDkoy7x.png) Look at srv32 architature. We can find the reg and logic signal step by step. ![](https://i.imgur.com/dpioXld.png) ![](https://i.imgur.com/c3im6P3.png) Take the screenshots below for example. #### clock! ![](https://i.imgur.com/Kq7R4Px.png) * Clk's period is 4ps. * After a cycle of clk, pc will increase 4. * Every 7 cycles of clk, branch turn to 1 for 1 clk. #### Execution Process! ![](https://i.imgur.com/I6AqDba.png) We can look at the assembly code file in `sw`. ![](https://i.imgur.com/sWahE2J.png) * At the beginning, we can find that the start address is `00000000`, so as imem_addr. * First line is `imem_addr`, and the number inside means PC. * `imem_rdata` take the instruction, like the fisrt instruction `00014297 auipc t0 0x14`. * `0x00014000` is `t0` and it will go down from imm to ex_imm. (IF/ID stage to EX stage) * And then we can find `0x00014000` went to `wb_result`. * `0x00014520` is the result from `addi t0, t0, 1440`. And we can see the `ex_result` from EX stage went to WB stage. ### Part 3 - Optimizations I try another way to complete this problem in less instructions. I reduce two while loop and make there is only on for loop leaved. #### Code Rewrite ``` c #include <stdio.h> #include <string.h> volatile int lengthOfLastWord(volatile char * s) { volatile int len = strlen(s); volatile int count = 0; for(volatile int i = len -1; i >=0; i--) { if(*(s+i) != ' ') { count++; } else break; } printf("\nLength of the last word is %d\n", count); } int main() { volatile char *string = "Hello World"; lengthOfLastWord(string); return 0; } ``` After compiling we can see the message as follow, ``` Length of the last word is 5 Excuting 2095 instructions, 2749 cycles, 1.312 CPI Program terminate - ../rtl/../testbench/testbench.v:418: Verilog $finish Simulation statistics ===================== Simulation time : 0.087 s Simulation cycles: 2760 Simulation speed : 0.0317241 MHz make[1]: Leaving directory '/home/peishan/srv32/sim' make[1]: Entering directory '/home/peishan/srv32/tools' ./rvsim --memsize 128 -l trace.log ../sw/LOLW/LOLW.elf ``` But the result is not as good as I think. I consider that the problem is `if` and `else`. Howerver, I did try other code. #### Code Rewrite - 2 ```c= #include <stdio.h> #include <string.h> volatile int lengthOfLastWord(volatile char * s) { volatile int len = strlen(s) - 1; volatile int count = 0; while(len >= 0 && s[len] == ' ') --len; while(len >= 0 && s[len] != ' '){ --len; ++count; } printf("\nLength of the last word is %d\n", count); } int main() { volatile char *string = "Hello World"; lengthOfLastWord(string); return 0; } ``` And the result os as follow, ``` Length of the last word is 5 Excuting 2100 instructions, 2756 cycles, 1.312 CPI Program terminate Simulation statistics ===================== Simulation time : 0.010 s Simulation cycles: 2756 Simulation speed : 0.278 MHz ``` It also didn't do better than original code. I have been tried in several way to modify the C code but there came out nothing better. If I can out better thought, I will keep modify the assighment. ### Reference 1. [ReadME](https://github.com/riscv-non-isa/riscv-arch-test/blob/master/doc/README.adoc) 2. [Length Of Last Word](https://leetcode.com/problems/length-of-last-word/) 3. [RISCV - International](https://riscv.org/)

    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