csrrwi
Atomic Read/Write CSR Immediate
- Extension
- Zicsr
- XLEN
- RV32 and RV64
- Assembly (riscv-unified-db)
csrrwi xd, csr, imm- Length
- 32-bit
Encoding
| 3120 | 1915 | 1412 | 117 | 62 | 10 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| csr | zimm5 | 101 | rd | 11100 | 11 | ||||||||||||||||||||||||||
Match 0x00005073, mask 0x0000707f. Source: csrrwi rd csr zimm5 14..12=5 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
Atomically write CSR using a 5-bit immediate, and load the previous value into 'xd'.
Read the old value of the CSR, zero-extends the value to XLEN bits,
and then write it to integer register xd.
The 5-bit uimm field is zero-extended and written to the CSR.
If xd=x0, then the instruction shall not read the CSR and shall not
cause any of the side effects that might occur on a CSR read.
Description from riscv-unified-db, reformatted.
Operation
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 (csr_handle.writable == false) {
raise (ExceptionCode::IllegalInstruction, mode(), $encoding);
}
if (xd != 0) {
X[xd] = csr_sw_read(csr_handle);
}
# writes the zero-extended immediate to the CSR,
# performing any WARL transformations first
csr_sw_write(csr_handle, {{MXLEN-5{1'b0}}, imm});Pseudocode in riscv-unified-db’s IDL, quoted unchanged.