contributed by < paulpeng-popo
>
In order to avoid affecting the original computer environment, a container is set up to provide the experimental environment for Assignment 3.
Here, I use Docker for building container.
Then, following the instructions provided in Lab3: Construct a single-cycle RISC-V CPU with Chisel to install necessary dependency packages and tools.
The module has two registers, cntReg
and blkReg
, both initialized with zero values. cntReg
is a 32-bit counter that increments by 1 in each clock cycle. When cntReg
reaches a certain value (CNT_MAX), it resets to zero, and blkReg
toggles its value.
I test Hello.scala
on chisel-template
For testing convenience, I have reduced the number from 50,000,000 to 10, making it easier to observe the differences in the output.
The updated CNT_MAX
value is now set to 4
Then create HelloSpec.scala
in scr/test/scala/example
It checks whether the module correctly simulates by stepping the simulation forward for 10 clock cycles and printing the values of the clk
and led
signals at each step.
The output would look like:
Using
when
blocks in Hardware Description Language (HDL) designs is not necessarily something that should be avoided in all cases. However, in some situations, particularly when dealing with simple state machines or conditional assignments, it might be more readable and synthesizable to use multiplexers (muxes) instead ofwhen
blocks.The primary reason for preferring
muxes
in some cases is that they directly map to hardware multiplexing structures, which synthesis tools can often recognize and implement more efficiently. This is especially true for simple conditions or state machines where amux
can directly represent the selection of one value from several inputs.–- From ChatGPT –-
Here, a multiplexer is employed to determine whether the counter should increment or reset to zero. Simultaneously, a logical XOR
operation is utilized to toggle the state of blkReg. The XOR
operation ensures that blkReg changes its state whenever the counter is reset, providing the desired functionality without using a when
block.
Instruction fetch stage does:
The PC register is initially set to the entry address of the program. Upon encountering a valid instruction, the CPU fetches the instruction located at the address specified by the PC. If a jump is necessary, the CPU checks the jump_flag_id
to determine whether a jump should be taken. If a jump is required, the PC is then updated with the address specified by jump_address_id
. Otherwise, the PC is incremented by 4 to move to the next sequential instruction.
PC initiates at address 0x1000
. In the first test case, where no jump occurs, the PC advances to fetch the next instruction by incrementing to PC + 4. Subsequently, in the second test case, a jump to address 0x1000
is executed, causing the PC to update its value to 0x1000
during the next clock cycle.
Decode stage does:
add
, read two registersaddi
, read one registerjal
, no reads are necessaryAt this stage, 8 signals need to be generated, and the remaining two outputs, namely memory_read_enable
and memory_write_enable
, have not been implemented yet.
These two signals appear to be associated with load and store instructions.
To finalize their implementation, we can easily configure memory_read_enable
to be true.B
when processing L type instructions, and set memory_write_enable
to true.B
for S type instructions; otherwise, the default value remains false.B
.
A warning occurs during compilation:
method apply in object MuxLookup is deprecated (since Chisel 3.6): Use MuxLookup(key, default)(mapping) instead
To address this warning, simply relocate the mapping sequence section to eliminate the deprecation message.
Three test cases:
According to our design specification, when the opcode is 0x3
, the signal memory_read_enable
should be set to true.B
, and when the opcode is 0x23
, the signal memory_write_enable
should be set to true.B
. The waveform chart above conveniently validates this behavior.
Execution stage does:
The control line for the ALU, denoted as alu.io.func
, is derived from the output of the ALU control module, specifically alu_ctrl.io.alu_funct
. Additionally, the two inputs of the ALU are determined by the control lines aluop1_source
and aluop2_source
. These control lines drive the corresponding inputs through two Muxes.
Initially, there are some test cases that involve the ADD instruction, aiming to evaluate the normal functioning of the ALU. The final two tests involve the BEQ instruction, assessing both jump and non-jump scenarios. In the case where the jump is taken, the program counter advances to PC + 2, equivalent to 0x4
.
With the completion of modules for each stage, the subsequent phase involves connecting the inputs and outputs of these stages. Once this integration is accomplished, the single-cycle RISC-V CPU will be considered complete.