csrrw

Atomic Read/Write CSR

This instruction is defined by:

Encoding

svg

Synopsis

Atomically swap values in the CSRs and integer registers.

Read the old value of the CSR, zero-extends the value to XLEN bits, and then write it to integer register rd. The initial value in rs1 is written to the CSR. If rd=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.

Access

M

HS

U

VS

VU

Always

Always

Always

Always

Always

Decode Variables

Bits<12> csr = $encoding[31:20];
Bits<5> rs1 = $encoding[19:15];
Bits<5> rd = $encoding[11:7];

Execution

  • Pruned, XLEN == 64

  • Original

if (rd != 0) {
  X[rd] = CSR[csr].sw_read();
}
CSR[csr].sw_write(X[rs1]);
if (rd != 0) {
  X[rd] = CSR[csr].sw_read();
}
CSR[csr].sw_write(X[rs1]);