arthur chang arthur
    • 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 ## Generate the files I pick the c code in Assignment 1. and transform into test.c . test.c is the target c file. I modify the Makefile to generate corresbonding .efl file to simulate. The Makefile and test.c are put in a folder called test. ### Makefile ``` include ../common/Makefile.common EXE = .elf SRC = test.c CFLAGS += -L../common LDFLAGS += -T ../common/default.ld TARGET = test 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 ``` we just execute the following command in srv32-devel: ``` make test ``` The .elf file and other output will be generated ## Output with printf() ``` 5 4 3 2 4 Excuting 3746 instructions, 4774 cycles, 1.274 CPI Program terminate - ../rtl/../testbench/testbench.v:418: Verilog $finish Simulation statistics ===================== Simulation time : 0.079 s Simulation cycles: 4785 Simulation speed : 0.0605696 MHz make[1]: Leaving directory '/home/korin/srv32-devel/sim' make[1]: Entering directory '/home/korin/srv32-devel/tools' ./rvsim --memsize 128 -l trace.log ../sw/test/test.elf 5 4 3 2 4 Excuting 3746 instructions, 4774 cycles, 1.274 CPI Program terminate Simulation statistics ===================== Simulation time : 0.006 s Simulation cycles: 4774 Simulation speed : 0.764 MHz make[1]: Leaving directory '/home/korin/srv32-devel/tools' Compare the trace between RTL and ISS simulator === Simulation passed === ``` Without printf() ``` Excuting 73 instructions, 89 cycles, 1.219 CPI Program terminate - ../rtl/../testbench/testbench.v:418: Verilog $finish Simulation statistics ===================== Simulation time : 0.019 s Simulation cycles: 100 Simulation speed : 0.00526316 MHz make[1]: Leaving directory '/home/korin/srv32-devel/sim' make[1]: Entering directory '/home/korin/srv32-devel/tools' ./rvsim --memsize 128 -l trace.log ../sw/test/test.elf Excuting 73 instructions, 89 cycles, 1.219 CPI Program terminate Simulation statistics ===================== Simulation time : 0.000 s Simulation cycles: 89 Simulation speed : 1.047 MHz make[1]: Leaving directory '/home/korin/srv32-devel/tools' Compare the trace between RTL and ISS simulator === Simulation passed === ``` As we can compare the output between the code with printf() and without printf(), the gap of the number of executed instructions is large. ### A part of Assembly code Execute the following command and get the assembly code. ``` riscv-none-embed-objdump -d test.elf ``` ``` test.elf: file format elf32-littleriscv Disassembly of section .text: 00000000 <_start>: 0: 00000297 auipc t0,0x0 4: 08028293 addi t0,t0,128 # 80 <trap_handler> 8: 30529073 csrw mtvec,t0 c: 3050e073 csrsi mtvec,1 10: 00020297 auipc t0,0x20 14: 00028293 mv t0,t0 18: 00020317 auipc t1,0x20 1c: ffc30313 addi t1,t1,-4 # 20014 <_bss_end> 00000020 <_bss_clear>: 20: 0002a023 sw zero,0(t0) # 20010 <heap_ptr> 24: 00428293 addi t0,t0,4 28: fe62ece3 bltu t0,t1,20 <_bss_clear> 2c: 00040117 auipc sp,0x40 30: fd410113 addi sp,sp,-44 # 40000 <_stack> 34: 008000ef jal ra,3c <main> 38: 3580006f j 390 <exit> 0000003c <main>: 3c: 00500613 li a2,5 40: 00200793 li a5,2 44: 00300713 li a4,3 48: 00400593 li a1,4 4c: 00f5a6b3 slt a3,a1,a5 50: fff68693 addi a3,a3,-1 54: 00d7f7b3 and a5,a5,a3 58: fff60613 addi a2,a2,-1 5c: 00060e63 beqz a2,78 <main+0x3c> 60: 00178793 addi a5,a5,1 64: 00170713 addi a4,a4,1 68: 00e5a6b3 slt a3,a1,a4 6c: fff68693 addi a3,a3,-1 70: 00d77733 and a4,a4,a3 74: fd9ff06f j 4c <main+0x10> 78: 00000513 li a0,0 7c: 00008067 ret ``` ``` 00000390 <exit>: 390: 0ff57793 andi a5,a0,255 394: 90000737 lui a4,0x90000 398: 02f70623 sb a5,44(a4) # 9000002c <_stack+0x8ffc002c> 39c: 0000006f j 39c <exit+0xc> ``` ### Branch instruction In address ```0x5c```, there is an bracnh instruction ```beqz a2,78```. When the condition is true, the next instrution address is `0x78`, which is ```li a0,0```. ![](https://i.imgur.com/MfdrkZX.png) ![](https://i.imgur.com/MNIqFDy.png) * ```ex_inst``` is the instruction executed in EX stage. * ```ex_pc``` is the address of the instruction in EX stage. * ```fetch_pc```is the address of next instruction As the EX stage excute ```beqz```, which address is ```0x5c``` and decoded as ```00060E63```, the address of the address is update into ```00000078``` in next cycle, and executed later. Since srv32 is a 3-stage RISC-V processor, the program will jump to the destination instruction after 3 cycle. The instrutions at address ```0x60``` ```0x64``` will be stalled. |Instruction|1|2|3|4|5|6|7| |:-:|-|-|:-:|:-:|:-:|:-:|-| |beqz a2,78|IF/ID|EX|WB|||| |addi a5,a5,1||IF/ID|NOP|NOP|| |addi a4,a4,1|||IF/ID|NOP|NOP| |li a0,0||||IF/ID|EX|WB| ## Execution By comparing the instruction dumped by compiler and the instructions in GTKwave. Most of the instructions are not executed. By running the folowing command: ``` riscv-none-embed-size test.elf ``` Get the total size of the code. ``` text data bss dec hex filename 1036 16 4 1056 420 test.elf ``` As we can see that the number is over 1000. However, in the simulator, we only execute 73 instructions in 89 cycles. In fact, the assembly code I put here are the parts which are executed in waveform. The program begin in start and execute through ```<_bss_clear>``` until the instruction```jal ra,3c ```, which jump to the ```<main>```. Since there is a branch instruction ```beqz``` and jump instruction ```j```in the main, this part will be executed five times, which is the length of the array in c code, and branch to the end of ```<main>``` by ```beqz```. Then, return back to instruction ```j 390 <exit>```. Finally, the processor execute ```<exit>``` in address ```390``` and terminated.

    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