N26104345莊崴
    • 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
    # Follow [How I built a RISC-V CPU Core in a span of 5 days](https://github.com/iamrk-vlsi/RISC-V-MYTH-Workshop) and rework for 32-bit RISC-V ###### tags: `computer-arch-HW` ## Content ### Main plan for this term project ! * Because we want to reconstruct the RISC-V CPU core with 32-bit ISA,then we need to plan the outline about each stage of this term project, which would be showed like below! - [ ] 1. Build the RISC-V CPU with default 64-bit ISA! (Author written) * main motivation about this stage is that helping us to familly with the whole architecture ! - [x] 1-1. Using `RISC-V toolchain` to compile the c code we modify.(`riscv32-unknown-elf-gcc`,`riscv64-unknown-elf-gcc`) - [x] 1-2. Using `spike` and `pk` to simulate the code compiled by `RISC-V toolchain` compiler. - [ ] 1-3. Built `riscv64-core` by using `Makerchip`. - [ ] 1-4. Using `iverilog` to establish the `riscv64-core` (Made by author) and execute the object file privious compiled. - [x] 2. Make clearly knowing about where is ISA in this architecture. * Observe the builiting processing, and then try to make clear note about the ISA position of CPU core. - [ ] 3. Replace expected ISA to our built RISC-V CPU core! * Main task in this term project! - [ ] 3-1. Built our `riscv32-core` by using `Makerchip`. - [x] 3-2. Translate the built`5-stage-rsicv64-pipline` to verilog format - [x] 3-3. Using `iverilog` to establish the `riscv32-core` (`picorv32.v`) and execute the object file privious compiled. ## Build the RISC-V CPU with default 64-bit ISA! ### Day 1 Instruction Set Architecture & GNU Toolchain. #### Lab 1 : C program of Sum 1 to n numbers. * The basic concept to compile the C code by gcc compiler is like that we could using compiler by fellowing command to compile the code we writed, then the `sum1to9` (custom name) execution file would be built, we could directly exeucute this file. ```shell= gcc -o sum1to9 sum1to9.c ./sum1to9 ``` ![](https://i.imgur.com/Pjlef6u.png) #### Lab 2 : C program of Sum 1 to n numbers, RISC-V toolchain. * Because we need to use the RISC-V toolchain, then we install [riscv-gun-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain) first. * Last time we use `riscv32`, then this time we need to re-make `riscv-gun-toolchain` with 64-bits version. ```shell= cd /home/wei/riscv-gnu-toolchain ./configure --prefix=/opt/riscv64 --with-arch=rv64imc --with-abi=lp64 sudo make ``` * After install [riscv-gun-toolchain](https://github.com/riscv-collab/riscv-gnu-toolchain), then we could add bin folder path in our `~/.bashrc file`. Help us initialize the enviroment when we create new terminal. ```shell= vim ~/.bashrc # add new path in .bashrc file! cat ~/.bashrc ``` ![](https://i.imgur.com/QpGbW3K.png) * Start compile the code by using the riscv-gun-toolchain. (for 64-bits) ```shell= riscv64-unknown-elf-gcc -O1 -mabi=lp64 -march=rv64i -o sum1to9_64.o sum1to9.c # using O1 optimization level and with rv64i architecture, abi for l->long, p->pointer data type using 64-bits. ``` * The consequence like this, we get the `sum1to9_64.o` from `riscv64-unknown-elf-gcc` compiler. ![](https://i.imgur.com/QmWQrH5.png) * some discription of `-mabi` from document! -mabi=ABI-string Specify integer and floating-point calling convention. ABI-string contains two parts: the size of integer types and the registers used for floating-point types. **For example -march=rv64ifd -mabi=lp64d means that long and pointers are 64-bit** (implicitly defining int to be 32-bit), and that floating-point values up to 64 bits wide are passed in F registers. Contrast this with -march=rv64ifd -mabi=lp64f, which still allows the compiler to generate code that uses the F and D extensions but only allows floating-point values up to 32 bits long to be passed in registers; or -march=rv64ifd -mabi=lp64, in which no floating-point arguments will be passed in registers. The default for this argument is system dependent, users who want a specific calling convention should specify one explicitly. The valid calling conventions are: ilp32, ilp32f, ilp32d, lp64, lp64f, and lp64d. Some calling conventions are impossible to implement on some ISAs: for example, -march=rv32if -mabi=ilp32d is invalid because the ABI requires 64-bit values be passed in F registers, but F registers are only 32 bits wide. * Using `riscv64-unknown-elf-objdump` to read elf assemble code. ```shell= riscv64-unknown-elf-objdump -d sum1to9_64.o ``` ![](https://i.imgur.com/UfOJ452.png) * Because the `spike` and `pk` simulator we don't have, then we need to install the above two tools. * Start to install [spike](https://github.com/riscv-software-src/riscv-isa-sim) ```shell= git clone https://github.com/riscv-software-src/riscv-isa-sim.git cd riscv-isa-smi sudo apt-get update sudo apt-get install device-tree-compiler mkdir build cd build ../configure --prefix=$RISCV # $RISCV where your RISCV install make sudo make install ``` * Start to install [pk](https://github.com/riscv-software-src/riscv-pk) ```shell= git clone https://github.com/riscv-software-src/riscv-pk.git cd riscv-pk mkdir build cd build ../configure --prefix=$RISCV --host=riscv64-unknown-elf # $RISCV where your RISCV install make sudo make install ``` * using the `spike` and `pk` to simulate the riscv64 assemble code. ```shell= spike pk sum1to9_64.o ``` * We could get the output directly by this simulator on terminal ![](https://i.imgur.com/fXCB7F9.png) * Which could be run in steps by adding `-d` with command. ```shell= spike -d pk sum1to9_64.o ``` * When we key in the "enter", we could execute the assembly code steps by steps. ![](https://i.imgur.com/SLat95J.png) #### Lab 3 : Max and Min number representations. * Now, we use `riscv64-unknown-elf-gcc` to compile the `unsignedHighest.c` and using `spike` and `pk` simulator to test this code. ```shell= riscv64-unknown-elf-gcc -01 -mabi=lp64 -march=rv64i -o unsignedHighest_64.o unsignedHighest.c spike pk unsignedHighest_64.o ``` ![](https://i.imgur.com/PCNsvUz.png) ### Day 2 Application Binary Interface and basic verification flow. #### Lab 1 : ASM & ABI function Calls * We still use `riscv64-unknown-elf-gcc` compiler with optimization level `Ofast` to compile it. Then we use the `riscv64-unknown-elf-objdump` to view the object file in readable format. Finally we use `spike` and `pk` simulator to test it. ```shell= riscv64-unknown-elf-gcc -Ofast -mabi=lp64 -march=rv64i -o 1to9_custom_64.o 1to9_custom.c load.S riscv64-unknown-elf-objdump -d 1to9_custom_64.o|less spike pk 1to9_custom_64.o ``` * Because there are some mistake in the original code, then we need to modify the code to complete the expectance consequence. * `1to9_custom.c` ```c= #include <stdio.h> extern int load(int x,int y); // remove the '.' in front of load() int main() { int result=0; int count=9; result=load(0x0,count+1); printf("sum of numbers from 1 to %d is %d \n",count,result); } ``` * `load.S` ```assembly= .section .text .global load .type load,@function load: add a4,zero,zero # initialize the a4 in zero add a2,a1,zero # assign a1 (count+1) to a2 add a3,a0,zero # assign a0 (0x0) to a3 loop: add a4,a3,a4 # add each element to the result addi a3,a3,1 # update the current element by add 1 to a3 blt a3,a2,loop # check the current element if reach or not to final element add a0,a4,zero # assign result to a0 and return it ret # jal back to the original pc ``` * Finally we could get the terminal like below. ![](https://i.imgur.com/lCsYbBb.png) ![](https://i.imgur.com/cNeZPgD.png) ![](https://i.imgur.com/k9Wtn2C.png) ![](https://i.imgur.com/Ujroznl.png) #### Lab 2 : To run and verify on a RISC-V Core * Because we don't have the `riscv_workshop_collaterals`, then we decided to clone one from the github. And install the dependance. ![](https://i.imgur.com/RYTHOrl.png) ```shell= git clone https://github.com/kunalg123/riscv_workshop_collaterals.git cd riscv_workshop_collaterals cat run.sh ``` * Because we had install the `riscv64-unknown-elf-gcc`, `spike` and `pk`, then we only need to install `iverilog` in my enviroment. Comment the unnecessary part in `run.sh` ![](https://i.imgur.com/TBUNZyR.png) * install iverilog ```shell= cd riscv_workshop_collaterals sh run.sh cd ~/iverilog ./configure make sudo make install ``` * Back to the `riscv_workshop_collaterals`, there are some strange about the `rv32im.sh`, because we want to exectute the `riscv64-unknown-elf-gcc`, but the `-mabi` and the `-march` are the parameter about the `riscv32`. ![](https://i.imgur.com/LBV4G57.png) then I decided to re-write the `rv64im.sh` to execute, however the file `picorv32.v` is not easy to replace, because this is the core of `riscv32`, that mean I need to find out another `riscv64-core` or write new one. Of course, the `hex8tohex32.py` need to be change to `hex8tohex64.py`. ![](https://i.imgur.com/5XNKOhd.png) * Finally I decided to exchange the compiler to `riscv32-unknown-elf-gcc`, then using iverilog to compile `picorv32.v` and `testbench.v` this two verilog file.(means that using `riscv32-core` to execute the frimeware we previous prepare. ) ![](https://i.imgur.com/pljnjK9.png) * The finally output on terminal. (with the `riscv32-core` ) ![](https://i.imgur.com/ThNQpnc.png) ### Day 3 Introduction to TL Verilog and [Makerchip](https://www.makerchip.com/sandbox/). Makerchip is the web platform to design the hardware. We could use easier HDL to describe our design, then translate to verilog. And there are lots useful function equip on the Makerchip. #### Lab: 1 Cycle Sequential Calculator with Validity * Copy the code in the Day3, and past on `MakerChip` . ![](https://i.imgur.com/GMabJFp.png) * We could view the signal directly. ![](https://i.imgur.com/ok9I4t6.png) ### Day 4 RISCV CPU Core Implementation. #### Something strange I found ! * From the `riscv64-core` which author provided. ![](https://i.imgur.com/8PkVXOw.png) * We could find out the instructure length is't 64 bits obviously ! That means this core is `riscv32-core` not `riscv64-core` ! ### Day 5 Pipelining the RISCV Core. #### Same situation happened on 5-stage-rsicv64-pipline ![](https://i.imgur.com/rRRLqH0.png) * `5-stage-rsicv64-pipline` ![](https://i.imgur.com/WWGIade.png) * Translate the built `5-stage-rsicv64-pipline` to verilog format. ![](https://i.imgur.com/MIK8sYf.png) ![](https://i.imgur.com/WiGf0k4.png) ## Conclusion * 1. The author had done the `riscv32-core` by using Makerchip not `riscv64-core`. * 2. We could using `picorv32.v` to replace the core made by author. * 3. If today we really want to use the `riscv64-core` to execute our code, then we could use [riscv-cores-list](https://github.com/riscvarchive/riscv-cores-list) to find out the core we needs.(written by verilog) But as sametime, the `hex8tohex64.py` file needs to be done first. * 4. I had not built the own `riscv32-core`, then I use `picorv32.v` core directly.

    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