Webcentive RISC-V Reference

csrrsi

Atomic Read and Set Bits in CSR with Immediate

Extension
Zicsr
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
csrrsi xd, csr, imm
Length
32-bit

Encoding

3120191514121176210
csrzimm5110rd1110011

Match 0x00006073, mask 0x0000707f. Source: csrrsi rd csr zimm5 14..12=6 6..2=0x1C 1..0=3 in riscv-opcodes/extensions/rv_zicsr.

FieldBitsriscv-opcodes name
csr31:20csr
zimm519:15zimm5
rd11:7rd

Description

The CSRRSI variant is similar to CSRRS, except this updates the CSR using an XLEN-bit value obtained by zero-extending a 5-bit unsigned immediate (imm[4:0]) field encoded in the xs1 field instead of a value from an integer register. For CSRRSI, if the imm[4:0] field is zero, then this instruction will not write to the CSR, and shall not cause any of the side effects that might otherwise occur on a CSR write, nor raise illegal-instruction exceptions on accesses to read-only CSRs. The CSRRSI will always read the CSR and cause any read side effects regardless of xd and xs1 fields.

Description from riscv-unified-db, reformatted.

Operation

Boolean will_write = imm != 0;

Csr csr_handle = direct_csr_lookup(csr);

# permission checks
if (csr_handle.valid == false) {
  unimplemented_csr($encoding);
} else if (!compatible_mode?(csr_handle.mode, mode())) {
  raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
} else if (will_write && csr_handle.writable == false) {
  raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
}

XReg initial_csr_value = csr_sw_read(csr_handle);

if (will_write) {
  # set bits using the mask
  # performing any WARL transformations first
  XReg mask = imm;
  csr_sw_write(csr_handle, initial_csr_value | mask);
}

X[xd] = initial_csr_value;

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages