Atomic Read and Set Bits in CSR
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.
csrrs xd, csr, xs1
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| csr | xs1 | 0 | 1 | 0 | xd | 111 | 00 | 11 | |||||||||||||||||||||||
| Variable | Bits | Width |
|---|---|---|
csr | [31:20] | 12 |
xs1 | [19:15] | 5 |
xd | [11:7] | 5 |
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;
Zicsr
| S | U | VS | VU |
|---|---|---|---|
| Always | Always | Always | Always |
| When | Use |
|---|---|
| xs1 == 0 && csr == 0x001 | frflags xd |
| xs1 == 0 && csr == 0x002 | frrm xd |
| xs1 == 0 && csr == 0x003 | frcsr xd |
| xs1 == 0 && csr == 0xC00 | rdcycle xd |
| xs1 == 0 && csr == 0xC01 | rdtime xd |
| xs1 == 0 && csr == 0xC02 | rdinstret xd |
| xs1 == 0 && csr == 0xC80 | rdcycleh xd |
| xs1 == 0 && csr == 0xC81 | rdtimeh xd |
| xs1 == 0 && csr == 0xC82 | rdinstreth xd |
| xs1 == 0 | csrr xd, csr |
| xd == 0 | csrs 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.