Does AMOSWAP.W write the new value or old value to rd?
rd receives the old 32-bit value from memory before the swap, sign-extended to XLEN. The low 32 bits of rs2 are the new value written to memory.
Atomically swap a 32-bit word: read the old memory value into rd and write the low 32 bits of rs2 back to the same address
AMOSWAP.W performs an atomic read-modify-write on the 32-bit word addressed by rs1: it reads the old memory value, sign-extends it into rd, and writes the low 32 bits of rs2 back to the same address. It does not use an LR/SC reservation; the aq/rl bits only affect ordering of this atomic memory operation relative to other memory operations in the same address domain.
Shows A-extension word atomic field decode, aq/rl ordering bits, memory read, and RMW state update.
A-extension word atomic instructions use a 32-bit encoding; the animation starts by splitting the fields.
This animation shows ISA-visible atomic read-modify-write, compare-and-swap, LR/SC reservation state, and ordering bits, not any specific CPU cache-coherence implementation, pipeline, or timing.
AMOSWAP.W writes the pre-swap old memory value to rd, while memory receives the low 32 bits of rs2. It is a single AMO read-modify-write operation, not an LR/SC loop.
Understand this scenario with real code like «amoswap.w.aq t0, t1, (a0) # atomic swap *a0 with t1, old value in t0».
rd receives the old 32-bit value from memory before the swap, sign-extended to XLEN. The low 32 bits of rs2 are the new value written to memory.
No. AMOSWAP.W is an independent AMO read-modify-write instruction and does not depend on an LR/SC reservation.