范紘維
    • 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
      • Invitee
    • 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
    • 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 Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync 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
Invitee
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
# Assignment3: single-cycle RISC-V CPU contributed by < [`fan1071221`](https://github.com/fan1071221/ca2023-lab3) > ## **Engage with the Chisel Tutorial** ### **Describe the operation of Hello World in Chisel** ```scala class Hello extends Module { val io = IO(new Bundle { val led = Output(UInt(1.W)) }) val CNT_MAX = (50000000 / 2 - 1).U; val cntReg = RegInit(0.U(32.W)) val blkReg = RegInit(0.U(1.W)) cntReg := cntReg + 1.U when(cntReg === CNT_MAX) { cntReg := 0.U blkReg := ~blkReg } io.led := blkReg } ``` 1. **Class Declaration**: The program defines a class Hello that extends the Module class, a fundamental construct in Chisel for defining a hardware module. 2. **IO Interface**: Inside the Hello class, an IO interface is declared. This interface includes a single output, led, which is a one-bit unsigned integer (UInt(1.W)). This output will be connected to an LED on the hardware. 3. **Constants and Registers**: * **CNT_MAX**: A constant is defined as 50000000 / 2 - 1, which is then converted to an unsigned integer. This constant is likely used to determine the blink rate of the LED. * **cntReg**: A 32-bit register (UInt(32.W)) initialized to 0. This register is used as a counter. * **blkReg**: A 1-bit register (UInt(1.W)) initialized to 0. This register controls the state of the LED. 4. **Counter Logic**: * The counter cntReg is incremented by 1 on each clock cycle: cntReg := cntReg + 1.U. * When cntReg equals CNT_MAX, two actions occur: 1. The counter is reset to 0: cntReg := 0.U. 2. The blkReg register is toggled: blkReg := ~blkReg. :::info The tilde (~) is a bitwise negation operator, so if blkReg was 0, it becomes 1, and vice versa. ::: 5. **LED Control**: * The state of blkReg is assigned to io.led. This means the LED's on or off state is controlled by blkReg. When blkReg is 1, the LED is on, and when it's 0, the LED is off. ### **Enhance it by incorporating logic circuit** ```c= class Hello extends Module { val io = IO(new Bundle { val led = Output(UInt(1.W)) }) val CNT_MAX = (50000000 / 2 - 1).U val cntReg = RegInit(0.U(32.W)) cntReg := Mux(cntReg === CNT_MAX, 0.U, cntReg + 1.U) io.led := Mux(cntReg === CNT_MAX, ~io.led, io.led) } ``` 1. **Registers and Constants**: * Retains the CNT_MAX constant and the cntReg counter.Removes the blkReg register, simplifying hardware resource usage. 2. **Combined Counter**: * Utilizes the Mux function to directly implement the counter reset logic within the assignment expression of cntReg. 3. **LED Control**: * The state of the LED is directly determined in the assignment to io.led based on the value of cntReg. ## **Construct a single-cycle RISC-V CPU with Chisel** ### **Running Unit Tests** Run the unit tests: ```shell $ sbt test ``` Output: ```shell [info] welcome to sbt 1.9.7 (Ubuntu Java 11.0.20.1) [info] loading settings for project ca2023-lab3-build from plugins.sbt ... [info] loading project definition from /home/hongwei/ca2023-lab3/project [info] loading settings for project root from build.sbt ... [info] set current project to mycpu (in build file:/home/hongwei/ca2023-lab3/) [info] InstructionFetchTest: [info] InstructionFetch of Single Cycle CPU [info] - should fetch instruction [info] InstructionDecoderTest: [info] InstructionDecoder of Single Cycle CPU [info] - should produce correct control signal [info] ExecuteTest: [info] Execution of Single Cycle CPU [info] - should execute correctly [info] ByteAccessTest: [info] Single Cycle CPU [info] - should store and load a single byte [info] QuicksortTest: [info] Single Cycle CPU [info] - should perform a quicksort on 10 numbers [info] FibonacciTest: [info] Single Cycle CPU [info] - should recursively calculate Fibonacci(10) [info] hw2Test: [info] Single Cycle CPU [info] - should Perform hw2 [info] RegisterFileTest: [info] Register File of Single Cycle CPU [info] - should read the written content [info] - should x0 always be zero [info] - should read the writing content [info] Run completed in 21 seconds, 748 milliseconds. [info] Total number of tests run: 10 [info] Suites: completed 8, aborted 0 [info] Tests: succeeded 10, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed. [success] Total time: 23 s, completed Nov 30, 2023, 5:25:58 PM ``` ### **Descriptions of each unit test**: #### **InstructionFetchTest** ##### **Purpose of testing the "Instruction Fetch" phase**: * The processor can correctly fetch instructions from memory. * The program counter is correctly updated, whether for sequential execution or due to a jump instruction changing the flow of execution. * Jump and branch instructions are properly handled, ensuring that the program flow executes as expected. ##### **InstructionFetchTest.scala** :::danger :warning: **Refrain from copying and pasting your solution directly into the HackMD note**. Instead, provide a concise summary of the various test cases, outlining the aspects of the CPU they evaluate, the techniques employed for loading test program instructions, and the outcomes of these test cases. ::: ##### **Test Implementation** * It sets up a scenario to test instruction fetching with and without jump conditions. * The entry variable is the initial address, and pre and cur are used to track the previous and current instruction addresses, respectively. * The loop (for (x <- 0 to 100)) simulates 100 instruction fetch cycles. * Within each cycle, it randomly decides whether to perform a jump or not (using Random.nextInt(2)). * If there's no jump (case 0), it increments the address by 4 (standard for RISC-V as each instruction is 32 bits or 4 bytes). * If a jump is simulated (case 1), it sets the instruction address back to the entry. * The test checks if the module's output (instruction_address) matches the expected address (either incremented or reset to entry). ##### Waveform ![Screenshot from 2023-12-01 02-58-19](https://hackmd.io/_uploads/HJhvsL8rT.png) * When io_jump_flag_id is equal to 0, the Program Counter (PC) increments sequentially by the default instruction size, which for 32-bit instructions is 4 bytes, exemplified by the increase from 0x1008 to 0x100C. However, when io_jump_flag_id equals 1, it indicates that a jump has occurred, causing the PC not to just increment simply, but to leap to an entirely new address. In the example provided, the PC jumps from 0x1014 to 0x1000. * * * #### InstructionDecodeTest ##### Purpose of testing the "Instruction Decode" phase: * Correct Signal Generation: The decoder generates appropriate control signals for different types of instructions (e.g., S-type, LUI, ADD). * Accurate Register Addressing: It correctly identifies source and destination register addresses. * Proper ALU Operation Setup: The ALU operation sources are correctly determined based on the instruction type. ##### **InstructionDecodeTest.scala** :::danger :warning: **Refrain from copying and pasting your solution directly into the HackMD note**. Instead, provide a concise summary of the various test cases, outlining the aspects of the CPU they evaluate, the techniques employed for loading test program instructions, and the outcomes of these test cases. ::: ##### **Test Implementation** * It sets up a scenario to test instruction fetching with and without jump conditions. * The entry variable is the initial address, and pre and cur are used to track the previous and current instruction addresses, respectively. * The loop (for (x <- 0 to 100)) simulates 100 instruction fetch cycles. * Within each cycle, it randomly decides whether to perform a jump or not (using Random.nextInt(2)). * If there's no jump (case 0), it increments the address by 4 (standard for RISC-V as each instruction is 32 bits or 4 bytes). * If a jump is simulated (case 1), it sets the instruction address back to the entry. * The test checks if the module's output (instruction_address) matches the expected address (either incremented or reset to entry). ##### **Waveform** ![Screenshot from 2023-12-01 12-43-51](https://hackmd.io/_uploads/B1ssEkPrp.png) At clock cycle 3, the following events are taking place: * The "lui" instruction with the hexadecimal value 0x22B7 is executed, which loads the immediate value 2 into the upper 20 bits of register x5. * It is a U-type instruction that does not require memory access, hence memory_read_enable and memory_write_enable remain inactive. * The reg_write_enable signal is active to allow the value to be written to register x5. * io_ex_aluop1_source is set to 0, indicating no use of the first ALU operand or it is tied to an internal value for the "lui" operation. * The io_reg_write_address is set to 0x5, targeting register x5 for the operation. * io_ex_aluop2_source is set to 1 to use the immediate value 2 as the second ALU operand. * * * ### **ExecuteTest** ##### **Purpose of testing the “Execute” phase:** * Ensure arithmetic and logic instructions are processed correctly by the ALU. * Verify that results are properly written back to registers. * Check immediate value instructions accurately manipulate and store results. * Confirm that branch decisions and program counter updates occur correctly. * Validate that all control signals are set and interpreted correctly for proper operation. ##### **ExecuteTest.scala** :::danger :warning: **Refrain from copying and pasting your solution directly into the HackMD note**. Instead, provide a concise summary of the various test cases, outlining the aspects of the CPU they evaluate, the techniques employed for loading test program instructions, and the outcomes of these test cases. ::: ##### **Test Implementation** * Test for Add Operation: * The test sets an ADD instruction (x3 = x2 + x1) using a poke into the instruction input of the Execute module. It then enters a loop to test this operation 100 times with random input values for register data (reg1_data and reg2_data).For each iteration, it checks the mem_alu_result output against the expected result of the addition and asserts that no jump should be flagged (if_jump_flag). * Test for Branch Equal (BEQ) Operation: * The code sets up a BEQ instruction (pc + 2 if x1 === x2) and steps the clock once to process it.The test then checks for the equality of the contents of two registers (reg1_data and reg2_data). If they are equal, it expects the if_jump_flag to be set (indicating a successful branch), and the if_jump_address to be 4 (as the immediate was set to 2, and the addition with the instruction address should result in 4).It also tests the case where the registers are not equal, in which case the if_jump_flag should not be set. ##### Waveform ![Screenshot from 2023-12-01 15-39-11](https://hackmd.io/_uploads/ryXLlGDHp.png) * An opcode of 33 is specified, which in this context corresponds to the instruction addu (add unsigned) in some instruction sets.The operation being performed is the addition of two operands, io_op1 with the value 0x0BFABBC4 and io_op2 with the value 0x153495EA.The result of this addition is 0x212F51AE, which is stored in io_result. ## **Modify the handwritten RISC-V assembly code on the single-cycle RISC-V CPU** - **Remove the RDCYCLE or RDCYCLEH instruction and store the result in memory** - **Modify the makefile in csrc directory to generate corresponding .asmbin file** ``` BINS = \ fibonacci.asmbin \ hello.asmbin \ mmio.asmbin \ quicksort.asmbin \ sb.asmbin \ hw2.asmbin ``` - **Add test in CPUTest.scala** ```scala= class hw2Test extends AnyFlatSpec with ChiselScalatestTester { behavior.of("Single Cycle CPU") it should "Perform hw2" in { test(new TestTopModule("hw2.asmbin")).withAnnotations(TestAnnotations.annos) { c => for (i <- 1 to 50) { c.clock.step(1000) c.io.mem_debug_read_address.poke((i * 4).U) // Avoid timeout } c.io.mem_debug_read_address.poke(4.U) c.clock.step() c.io.mem_debug_read_data.expect(16.U) c.io.mem_debug_read_address.poke(8.U) c.clock.step() c.io.mem_debug_read_data.expect(23.U) c.io.mem_debug_read_address.poke(12.U) c.clock.step() c.io.mem_debug_read_data.expect(46.U) } } } ``` - **Result** ```shell $ sbt "testOnly riscv.singlecycle.hw2Test"` [info] welcome to sbt 1.9.7 (Ubuntu Java 11.0.21) [info] loading settings for project ca2023-lab3-build from plugins.sbt ... [info] loading project definition from /home/hongwei/ca2023-lab3/project [info] loading settings for project root from build.sbt ... [info] set current project to mycpu (in build file:/home/hongwei/ca2023-lab3/) [info] hw2Test: [info] Single Cycle CPU [info] - should Perform hw2 [info] Run completed in 6 seconds, 415 milliseconds. [info] Total number of tests run: 1 [info] Suites: completed 1, aborted 0 [info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0 [info] All tests passed. [success] Total time: 7 s, completed Dec 1, 2023, 4:05:15 PM ``` ## **Using Verilator to Run the Assembly** Use the following command to generate the simulation executable file of the CPU: ``` $ make verilator ``` Use the following command to run `hw2_asm.asmbin` on the simulated CPU: ``` $ ./run-verilator.sh -instruction src/main/resources/hw2.asmbin -time 2000 -vcd hw2.vcd ``` Output: ``` -time 2000 -memory 1048576 -instruction src/main/resources/hw2.asmbin [-------------------->] 100% ``` ## **Analyze the waveform data by loading the file into GTKWave** ### **Instruction Fetch** ![Screenshot from 2023-12-01 21-42-57](https://hackmd.io/_uploads/HkTlmPDHT.png) When io_jump_flag_id equals 1, it indicates that a jump has occurred, causing the PC not to just increment simply, but to leap to an entirely new address. In the example provided, the PC jumps from 0x10EC to 0x1024. ### **Instruction Decode** ![Screenshot from 2023-12-01 22-25-54](https://hackmd.io/_uploads/BkTZTvwHp.png) The io_instruction value 0x00452483 represents a lw instruction in a RISC-V system, which loads a word from memory into register s1. The memory read is enabled (io_memory_read_enable = 1), while memory write is disabled (io_memory_write_enable = 0). The register a0 (address 0x0A) is the base address for the memory access, with an offset of 4 (io_ex_immediate = 0x4). The data from memory will be written into register s1 (rd = 0x9), with write operations enabled (io_reg_write_enable = 1). ### **Execute** ![Screenshot from 2023-12-01 22-41-14](https://hackmd.io/_uploads/HyC5xdwr6.png) The alu_ctrl_io_alu_funct signal with a value of 0x0001 specifies an addition operation in the arithmetic logic unit (ALU) of the system. When the instruction type is a load operation (indicated by InstructionTypes.L), the ALU function is set to perform an addition (ALUFunctions.add). This is seen in the ALU control code. In the ALU's Scala implementation, when io.func is set to ALUFunctions.add, the ALU performs an addition of io.op1 and io.op2. This operation is used to calculate the effective memory address for the load instruction. The effective memory address, or alu_io_result, is computed by adding the base register value rs (0x00001004) to the immediate offset (0x000000EC), resulting in a calculated memory address of 0x000010F0. This is the address from which data will be loaded into the register. ### **Memory Access** The assembly program counts the number of zeros in a number and stores the result in memory. The count of zeros is calculated in the counting_zero function and stored in the t0 register. After counting, the program calls the print_result function, which stores the result from t0 into memory at the address pointed to by the s7 register. The s7 register is set before each counting operation to point to memory locations immediately following num1, num2, or num3 data areas, depending on which number is being processed. - **Test case 1 (Answer is 16)** ```shell c.io.mem_debug_read_address.poke(4.U) c.clock.step() c.io.mem_debug_read_data.expect(16.U) ``` ![Screenshot from 2023-12-01 23-06-51](https://hackmd.io/_uploads/HkmJDuPrp.png) - **Test case 2 (Answer is 23)** ```shell c.io.mem_debug_read_address.poke(8.U) c.clock.step() c.io.mem_debug_read_data.expect(23.U) ``` ![Screenshot from 2023-12-01 23-14-08](https://hackmd.io/_uploads/SkNIu_vB6.png) - **Test case 3 (Answer is 46)** ```shell c.io.mem_debug_read_address.poke(12.U) c.clock.step() c.io.mem_debug_read_data.expect(46.U) ``` ![Screenshot from 2023-12-01 23-16-32](https://hackmd.io/_uploads/ryUkYdPBT.png)

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