1277 words
6 minutes
CS110 Project 2.2

Project 2: A RV32C Toy CPU#

IMPORTANT INFO - PLEASE READ#

The projects are part of your design project worth 2 credit points. As such they run in parallel to the actual course. So be aware that the due date for project and homework might be very close to each other! Start early and do not procrastinate.

Overview#

In this individual project, you will embark on a two-step journey to create a toy RVC CPU. Before diving into the task, please pay close attention to the following important points:

  • Any behavior that violates course rules is strictly prohibited. This includes viewing, copying or plagiarizing other individuals’ circuits and codes. Violators will face severe consequences

  • You are allowed to use any built-in blocks of Logisim, except for the System On a Chip group, throughout this project.

  • Save frequently and commit frequently! Try to save your code in Logisim every 5 minutes or so, and commit every time you produce a new feature, even if it is small.

  • Don’t move around the given inputs and outputs in your circuit; this could lead to issues with the autograder.

Introduction#

This excerpt from the RISC-V User-Level ISA Specification describes the current draft proposal for the RISC-V standard compressed instruction set extension, named “C”, which reduces static and dynamic code size by adding short 16-bit instruction encodings for common operations. The C extension can be added to any of the base ISAs (RV32, RV64, RV128), and we use the generic term “RVC” to cover any of these. Typically, 50%–60% of the RISC-V instructions in a program can be replaced with RVC instructions, resulting in a 25%–30% code-size reduction. In this project, we will build a CPU that solely supports some of the RVC instructions.

Project 2.1: Implement CI and CR type instructions (DDL: May 7th)#

In this part, your task involves implementing all CI and CR type instructions shown in The Instruction Set below. Feel free to refer to the advice provided in the How to Get Started section to kickstart your circuit adventure.

Project 2.2: Implement all instructions (DDL: May 26th)#

In this part, you need to implement all instructions shonw in The Instruction Set table below. Notice: A new template is provided for you to implement the instructions. You can copy and paste your circuit from the previous part to save time.

The Instruction Set#

The instructions you need to implement are shown in the table below:

FormatISAOP[1:0]Funct3Implementation
CRadd10100x[rd] = x[rd] + x[rs2]
CRmv10100x[rd] = x[rs2]
CIaddi01000x[rd] = x[rd] + sext(imm)
CIslli10000x[rd] = x[rd] << uimm
CIli01010x[rd] = sext(imm)
CIlui01011x[rd] = sext(imm[17:12] << 12)
CInop01000None
CSSswsp10110M[x[2] + uimm][31:0] = x[rs2]
CIWaddi4spn00000x[8+rd’] = x[2] + nzuimm
CLlw00010x[8+rd’] = sext(M[x[8+rs1’] + uimm][31:0])
CSsw00110M[x[8+rs1’] + uimm][31:0] = x[8+rs2’]
CBbeqz01110if (x[8+rs1’] == 0) pc += sext(offset)
CJj01101pc += sext(offset)
CRjr10100pc = x[rs1]

A detailed description of compressed instruction formats is provided in The RISC-V Compressed Instruction Set Manual, Version 1.9. Some important information is also provided below. Please note that there are different versions of RVC instructions, and referencing other documents or webpages may result in failing the test.

Instruction Formats#

2024-04-10-15-38-55-image.png

Table 1.1 shows the eight compressed instruction formats. CR, CI, and CSS can use any of the 32 RVI registers, but CIW, CL, CS, and CB are limited to use only 8 of them, which are registers x8 to x15 listed in Table 1.2. In project 2, you do not need to implement all 32 registers, please refer to the Restriction section for details.

Tips: you can try to decode the instruction to opcode, register address, immediate and so on firstly.

CR Format#

CRFUNCT4RD/RS1RS2OPCODE
Bits4552
C.ADD1001dest≠ 0src≠ 010
C.MV1000dest≠ 0src≠ 010
C.JR1000src≠ 0010

CI Format#

CIFUNCT3IMMRD/RS1IMMOPCODE
Bits31552
C.LI010imm[5]dest≠ 0imm[4:0]01
C.LUI011nzimm[17]dest≠{0,2}nzimm[16:12]01
C.ADDI000nzimm[5]dest≠ 0nzimm[4:0]01
C.SLLI000shamt[5]dest≠ 0shamt[4:0]10

CL Format#

CLFUNCT3IMMRS1’IMMRD’OPCODE
Bits333232
C.LW010offset[5:3]baseoffset[2|6]dest00

CS Format#

CSFUNCT3IMMRS1’IMMRS2’OPCODE
Bits333232
C.SW110offset[5:3]baseoffset[2|6]src00

CSS Format#

CSSFUNCT3IMMRS2’OPCODE
Bits3652
C.SWSP110offset[5:2|7:6]src10

CIW Format#

CIWFUNCT3IMMrd’OPCODE
Bits3832
C.ADDI4SPN000zimm[5:4|9:6|2|3]dest00

CJ Format#

CJFUNCT3JUMP TARGETOPCODE
Bits3112
C.J101offset[11|4|9:8|10|6|7|3:1|5]01

CB Format#

CBFUNCT3IMMRD’/RS1’IMMOPCODE
Bits33352
C.BEQZ110offset[8|4:3]srcoffset[7:6|2:1|5]01

TOP I/O#

The inputs and outputs of top level are fixed in TOP circuit. It’s not allowed to add extra pins in TOP circuit in your submission. Fail to comply with this may result in losing all your points!

Typesignalbit widthdescription
inputclk1clock
inputrst1reset
inputinst16RVC instruction
inputmem_dout32data from memory
inputcurrent_pc32current pc value
outputmem_wen1memory write enable
outputmem_din32data written to memory
outputmem_ren1memory read enable
outputmem_addr32address of memory
outputcontrol_en1enable writing value to pc
outputcontrol_pc32value written to pc
outputwb_en1regfile write enable
outputwb_addr5address written to regfile
outputwb_data32data written to regfile

Some of these pins are not involved in Project 2.1 but will be used in Project 2.2, so you can temporarily ignore them.

Fetch#

Throughout this project, you will work with the Fetch module provided by the TA. This module assumes that

  • The instruction memory is halfword-addressable, i.e., each address refers to a 16-bit memory space.
  • During each clock cycle, an instruction is fetched from the address stored in the program counter.
  • The address value in the program counter increments by one each clock cycle to fetch the next instruction without considering branch or jump.

Additionally, you have the flexibility to incorporate sub-circuits to explore the functionality of the Fetch module. Doing so will enhance your understanding of its operational principles.

How to get start#

Here is a simple template to get started. Please download and unzip it first. Opening proj_2_1.circ with logisim-evolution, you will find several subcircuits inside it.

For project 2.2, you can use this template to get started.

  • TOP is the top-level circuit you need to complete. It represents the implementation of the target toy CPU that includes the subcircuits you designed. Please note that do not modify the packaging of the TOP circuit or any parts marked as “Don’t touch”, as failing to comply with this may result in failing the test cases.
  • testbench is a completed circuit provided by TAs for testing purposes. You can write any binary format instruction to the ROM and observe whether your circuit operates as expected. This circuit will not be used in grading, so feel free to make any attempts.

Here are some suggestions for your information:

  • It’s recommended that try to implement instructions one by one and you can start with LI or LUI instructions! This method is debug-friendly.
  • List all the signals you need, and try to classify them into different groups. Classifying signals helps complete circuits in the simplest way. You will find that the workload is much smaller in this way.

Test#

Test for all CR and CI type instruction has been included with the template package. You can run it with the command./test.sh. The provided test case is carried out in the order (LI-LI-LUI-LUI-SLLI-ADD-MV-ADDI-ADDI), and errors in the previous instructions may result in incorrect results in the later ones even if you implement them correctly.

The 8000 instruction utilizes a reserved instruction space to generate the halt signal. This signal serves the same purpose as the halt signal in Lab 5, which is to terminate the execution of the auto-test script. While this signal is crucial for the auto-test script, it does not produce any additional effects. Please be aware that when the 8000 instruction is executed, your system should not generate any significant output. The Autograder will not evaluate the output of this instruction.

In submission, you need to correctly implement at least one of LI or LUI to ensure that the other instructions can be successfully tested.

Notice: The Autograder of project 2.2 will test all instruction shown in the Instruction Set.

Restriction#

In order to reduce your workload and help you pass the test smoothly, some restrictions are stipulated as follows. You should read this section carefully as it may not be consistent with the content in the Instruction Set Manual.

  • All data is represented in 2’s complement form.

  • The data memory (RAM) is word-addressable, i.e., each address refers to a 32-bit memory space.

  • The Autograder will reset your system before carrying out any instructions. In other words, during the first cycle, all pins should output 0.

  • To reduce your workload, all test scripts will only involve ten integer registers x0, x2, x8-x15.

  • All instructions in testcases are valid, and you don’t need to consider instruction checking (please note that NOP is also a valid instruction in RVC and our CPU).

  • Since logisim supports memory with limited size, only the lower 8 bits of an 32-bit address are used to access instruction memory (ROM) and lower 16 bits to access data memory (RAM).


In project 2.1 are,

Siting Liu <liust AT shanghaitech.edu.cn>

Yutong Wang <wangyt32023 AT shanghaitech.edu.cn>

and

Suting Chen <chenst AT shanghaitech.edu.cn>

In project 2.2 are,

Siting Liu <liust AT shanghaitech.edu.cn>

and

Yutong Wang <wangyt32023 AT shanghaitech.edu.cn>

Last modified: 2024-05-12

CS110 Project 2.2
https://zivmax.top/posts/cs110/cs110-project-2-2/
Author
Zivmax
Published at
2024-05-10