csrrs
Atomic Read and Set Bits in CSR
- Extension
- Zicsr
- XLEN
- RV32 and RV64
- Assembly (riscv-unified-db)
csrrs xd, csr, xs1- Length
- 32-bit
Encoding
| 3120 | 1915 | 1412 | 117 | 62 | 10 | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| csr | rs1 | 010 | rd | 11100 | 11 | ||||||||||||||||||||||||||
Match 0x00002073, mask 0x0000707f. Source: csrrs rd rs1 csr 14..12=2 6..2=0x1C 1..0=3 in riscv-opcodes/extensions/rv_zicsr.
| Field | Bits | riscv-opcodes name |
|---|---|---|
| csr | 31:20 | csr |
| rs1 | 19:15 | rs1 |
| rd | 11:7 | rd |
Description
Atomically read and set bits in a CSR.
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 set in the CSR. Any bit that is high in xs1 will cause the
corresponding bit to be set in the CSR, if that CSR bit is writable.
Other bits in the CSR are not explicitly written.
Description from riscv-unified-db, reformatted.
Operation
Boolean will_write = xs1 != 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 = X[xs1];
csr_sw_write(csr_handle, initial_csr_value | mask);
}
X[xd] = initial_csr_value;Pseudocode in riscv-unified-db’s IDL, quoted unchanged.