or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Assignment3: Single-cycle RISC-V CPU
contributed by <
jeremy90307
>Environment setup
OS:ubuntu 22.04
sbt versopn:1.9.4
JDK version:1.8.0
Follow the instructions in Lab3: Construct a single-cycle RISC-V CPU with Chisel to set up the environment.
GTKWave Installation
Install
gtkwave-3.3.117.tar.gz
.README
file instructions, if the installation fails, you need to install some packages.Hello World in Chisel
led
is an output terminal with an unsigned type and a bit width of 1.cntReg
is a counter with an initial value set to 0 and a bit width of 32 bitsCNT_MAX
is the maximum value of the counter.blkReg
represents the current state, with an initial value of 0 and a bit width of 1.when(...)
: When cntReg is equal to CNT_MAX, reset cntReg, and change the state of blkReg.blkReg
to the output signal.Lab 3 : Single Cycle RISC-V CPU
Install the dependent packages
Run all test
If the execution is successful, you will see the following message.
Run single Scala file for unit test
Output
.vcd
file and analyze using GTKWave.Resolve MyCPU
Pending resolution
My repository, where you can see the code for my completed MyCPU, is available here.

Instruction Fetch
Test
io.instruction_valid
is true, then jump to that location.(0x1000)io.instruction_valid
is false, then pc + 4GTKWave
From the diagram, it can be observed that when
io.instruction_valid = 1
, the PC position returns to 0x1000 on the next rising edge of the clock.Instruction Decoder
Test
After inputting 'sw,' 'lui,' and 'add,' the correct control signals are obtained.
io.memory_read_enable
is set to true.io.memory_write_enable
is set to true.GTKWave
input=0x00a02223L.U –>
sw x10, 4(x0)
When the instruction is of the store type, io_memory_write_enable is set to 1.
Execution
Explanation of Scala syntax
muxLookup
descriptionfunct3
: The value to be matched.false.B
: The default value when no corresponding match is found.InstructionsTypeB.beq -> (io.reg1_data === io.reg2_data),
:If funct3 is equal to InstructionsTypeB.beq, then it evaluates whether io.reg1_data = io.reg2_data is true, and the result serves as the output of MuxLookup. If funct3 is not equal to InstructionsTypeB.beq, the default value for MuxLookup is set to false.B.
Test
add
, and obtain the expected output.beq
, and determine the output address by comparing if they are equal.GTKWave
add
alu_io_op1
=io_reg1_data
= 016A05E2alu_io_op2
=io_reg2_data
= 0FBD8F12alu_io_result
is the sum ofalu_io_op1
andalu_io_op2
.alu_io_function
is set to 1, it corresponds to ALUFunctions.add.add
operation matches the expectations.beq
alu_io_op1
=io_instruction_address
= 0x00000002alu_io_op2
=io_immediate
= 0x00000002alu_io_result
is the sum ofalu_io_op1
andalu_io_op2
.alu_io_func=1
-> ALUFunctions.addreg1
andreg2
are the same,io_if_jump_flag
is set to 1.RegisterFileTest
Test
x0
always results in 0.GTKWave
io_write_enable
is set to 1, and data is written to registers_2, and it is successfully read.CPU
Complete
CPU.scala
, this part is missing the necessary data and signals for connecting to module EXE.Test
HW2 runs on MyCPU
Adapt HW2 for MyCPU
c code
Process
hw2.S
) into theca2023-lab/csrc
directory.ecall
and add_start:
Makefile
,and addhw2.asmbin
under BINS.$ make update
in the directory to generatehw2.asmbin
.CPUTest.scala
, add a Test class forhw2.asmbin
.Test the scale value for the first set of data in hw2.
Output
Verilator
generate Verilog files
-memory
Example:
-memory 4096
-instruction
Example:
-instruction src/main/resources/hello.asmbin
-signature
Example:
-signature 0x100 0x200 mem.txt
-halt
0xBABECAFE
to this memory address stops the simulation.Example:
-halt 0x8000
-vcd
Example:
-vcd dump.vcd
-time
Example:
-time 1000
Load the
hw2.asmbin
file, simulate for 2000 cycles, and save the simulation waveform to thedump01.vcd
file.Output
Run GTKwave
dump01.vcd
to check its waveform.I-type :
addi x2, x2, -4
Hexadecimal = 0xffc10113
Binary = 1111111 11100 00010 000 00010 0010011
IF
io_instruction=0xFFC10113 -> addi x2, x2, -4
io_instruction_read_data=io_instruction
io_jump_flag_id=0
, the next pc is pc+4.ID
io_ex_aluop1_source=0
reads the value ofio_reg1_data
.io_ex_aluop2_source=1
,reads the value ofio_ex_immediate
.(io_ex_immediate=0xFFFFFFFC = -4
)io_memory_read_enable=io_memory_write_enable=0
, there is no modification to the memoryL-type :
io_memory_read_enable = 1
S-type :
io_memory_write_enable = 1
io_regs_reg1_read_address=02
(x2=sp)EXE
alu_ctrl_io_alu_func=1
, it indicates theaddi
function.io_aluop1_source=0
,alu_io_op1
is equal toio_reg1_data
, which is 0.io_aluop2_source=1
,alu_io_op2
is equal toio_immediate
, which is 0xFFFFFFFCMEM
io_alu_result
is equal toio_memory_bundle_address
, both being 0xFFFFFFFC.WB
io_regs_write_data=0xFFFFFFFC
to registerx2
.J-type :
jal x0, 68
Hexdicimal:0x0440006f
Binary:00000100010000000000 00000 1101111
IF
io_instruction[31:0]=0440006F -> jal x0, 68
io_instruction[31:0]=io_instruction_read_data[31:0]
io_jump_flag_id=1
is set to 1, the program counter (pc) consequently receives the instructionio_jump_address_id[31:0]=00001098
, resulting in the pc becomingpc=1098
.ID
io_ex_aluop1_source=1
=io.instruction_address
io_ex_aluop2_source=1
reads the value ofio_ex_immediate[31:0]=00000044
io_memory_read_enable=0
andio_memory_write_enable=0
, no read or write operations are performed on the memory.io_regs_reg1_read_address[4:0]=00
(x0) is equivalent tord[4:0]=00
(x0).EXE
io_aluop1_source=1
,alu_io_op1
is equal toio_instruction_address=00001054
, where pc=1054.io_aluop2_source=1
,alu_io_op2 is equal
toio_immediate
, which is 0x00000044io_if_jump_flag=1
, the program counter (pc) jumps toio_if_jump_address=00001098
.(The next pc is set to 1098.)io_if_jump_address
is defined as the sum ofio_immediate
andio_instruction_address
.MEM
io_alu_result
is equal toio_memory_bundle_address
.WB
S-type :
sw x15, 0(x12)
Hexadecimal=0x00f62023
Binary=00000000 11110 11000 100 00000 100011
IF
io_instruction=0xFFC10113 -> sw x15, 0(x12)
io_instruction_read_data=io_instruction
io_jump_flag_id=0
, the next pc is pc+4.ID
io_ex_aluop1_source=0
is the base memory address, which is the value of x12.(io_regs_reg1_read_address=0x0C
)io_ex_aluop2_source=1
is the offset of the memory address, which is 0.(io_ex_immediate=0x00000000
)io_memory_write_enable=1
, data is written into memory.EXE
alu_io_result=00001334
, is equal to the sum of ALU operands, wherealu_io_op1=00001334
andalu_io_op2=00000000
.MEM
io_memory_bundle_address=0x00001334
, is equivalent to the ALU output,io_alu_result=00001334
.io_memory_bundle_write_data
is 0x00000000, which is equal to the value ofio_reg2_data
.WB
Reference