← Home

csrrc

Atomic Read and Clear Bits in CSR

The CSRRC (Atomic Read and Clear Bits in CSR) instruction reads the value of the CSR, zero-extends the value to XLEN bits, and writes it to integer register `xd`. The initial value in integer register `xs1` is treated as a bit mask that specifies bit positions to be cleared in the CSR. Any bit that is high in `xs1` will cause the corresponding bit to be cleared in the CSR, if that CSR bit is writable.

For CSRRC, if `xs1=x0`, then the instruction will not write to the CSR at all, and so 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. CSRRC always reads the addressed CSR and cause any read side effects regardless of `xs1` and `xd` fields. Note that if `xs1` specifies a register other than `x0`, and that register holds a zero value, the instruction will not action any attendant per-field side effects, but will action any side effects caused by writing to the entire CSR.

Assembly format

csrrc xd, csr, xs1

Encoding

313029282726252423222120191817161514131211109876543210
csrxs1011xd1110011

Decode variables

VariableBitsWidth
csr[31:20]12
xs1[19:15]5
xd[11:7]5

Execution

Csr csr_handle = direct_csr_lookup(csr);

Boolean will_write = xs1 != 0;

# 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 (xs1 != 0) {
  # clear bits using the mask
  # performing any WARL transformations first
  XReg mask = X[xs1];
  csr_sw_write(csr_handle, initial_csr_value & ~mask);
}

X[xd] = initial_csr_value;

Defined by

Zicsr

Access

SUVSVU
AlwaysAlwaysAlwaysAlways

Pseudoinstructions

WhenUse
xd == 0csrc csr, xs1

Version af3eaf9+cheriot.f635218

Data from the RISC-V Unified Database, copyright RISC-V International and contributors.

CHERIoT extensions from the CHERIoT specification and Sail model, copyright Microsoft and the CHERIoT Platform contributors.

This site is provided as a community resource and is NOT affiliated with nor endorsed by RISC-V International.