csrrci
Atomic Read and Clear Bits in CSR with Immediate
- Extension
- Zicsr
- XLEN
- RV32 and RV64
- Assembly (riscv-unified-db)
csrrci xd, csr, imm- Length
- 32-bit
Encoding
| 3120 | 1915 | 1412 | 117 | 62 | 10 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| csr | zimm5 | 111 | rd | 11100 | 11 | ||||||||||||||||||||||||||
Match 0x00007073, mask 0x0000707f. Source: csrrci rd csr zimm5 14..12=7 6..2=0x1C 1..0=3 in riscv-opcodes/extensions/rv_zicsr.
| Field | Bits | riscv-opcodes name |
|---|---|---|
| csr | 31:20 | csr |
| zimm5 | 19:15 | zimm5 |
| rd | 11:7 | rd |
Description
The CSRRCI variant is similar to CSRRC, 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 CSRRCI, 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 CSRRCI 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.