Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

So far we have designed the datapath and control logic to support instructions in the RV32I base ISA to run any compiled C program. We are still missing a few components required for every computer.

1Control and Status Registers (CSRs)

Control and status registers (CSRs) are separate from the register file (x0-x31). CSRs are not in the base ISA, but they are pretty much mandatory for every implementation. Because the ISA is modular, CSRs are necessary for counters and timers, and communication with peripherals. In other words, CSRs monitor status and performance, such as counting the number of cycles executed and communicating with peripherals (like printers) or other units on the same chip (like floating-point co-processors).

The RISC-V ISA allows space for addressing up to 4096 CSRs. Communication is often done by placing a control word into the register for a peripheral to pick up; when done, the peripheral places its status (ready, waiting, or done) back in the register. Sometimes this communication is just a single bit, drawing parallels to the postal service mailbox where raising a flag indicates mail is ready to be picked up. This is why single-bit pieces of information in processors are called flags, which we “set” and “clear.”

Read more about CSRs in the RISC-V Privileged ISA Specification, Volume II Chapter 2.

1.1CSR Instruction Formats

CSR instructions are separate from the base ISA in their own standard extension module. The CSR instruction format are similar to I-Type, except the upper 12-bit field is reserved for the CSR address csr (instead of immediate imm).

Table 1 shows the two instruction format types for CSR instructions:

Table 1:CSR Instruction Format Types

3125
2420
1915
1412
117
60
csrrsfunct3rdopcode
csruimmfunct3rdopcode

All CSR instructions have opcode field 1110011 (SYSTEM).

1.2CSR Instructions

The instructions in Table 2 generally work by swapping values between CSRs and general-purpose registers.

Table 2:CSR Instructions: Register Operand

Instructionrd is x0rs1 is x0Reads CSRWrites CSR
CSRRWYes-NoYes
CSRRWNo-YesYes
CSRRS/CSRRC-YesYesNo
CSRRS/CSRRC-NoYesYes

The immediate operand instructions in Table 3 work similarly to their register operand counterparts. These instructions update the CSR based on a 5-bit unsigned immediate uimm field that is zero-extended to 32 bits.

Table 3:CSR Instructions: Immediate Operand

Instructionrd is x0uimm is 0Reads CSRWrites CSR
CSRRWIYes-NoYes
CSRRWINo-YesYes
CSRRSI/CSRRCI-YesYesNo
CSRRSI/CSRRCI-NoYesYes

There are pseudoinstructions that do not read the CSR, opting to set rd=x0:

1.3Implementation

Implementing CSR and CSR instructions is not “magic”; like the RegFile, CSRs are just a block of registers. However, clocks and write enable signals are still essential to avoid accidentally “scribbling” over any CSRs.

2System Instructions

There are a few more instructions in the base set that share the same SYSTEM opcode 1110011 that you will inevitably encounter:

Footnotes
  1. These pairs of concurrent operations are called atomic; we discuss this idea later.