Page 1 of 1

FAQ: Read-modify-write (RMW). What it is and how to avoid it

PostPosted: Tue May 27, 2014 2:56 am
by ric
(note, this is still a work in progress. Initially it is aimed at chips which have LAT registers. It will be extended to cover parts which don't, such as mid-range PIC16Fxxx parts.)

Background
When you set or clear an individual bit in a port, the compiler produces either a BSF or BCF assembly instruction.
These instructions read the entire 8 bit port, change the indicated bit, and rewrite the entire 8 bit port.
That is why this is called a "read-modify-write" instruction.

If you address the PORT register, it reads the current state of the pins in the first step
If you address the LAT register, it reads the internal latch holding the current output states.
The write operation is identical for PORT and LAT.

Now think about what happens if you change two pins on the same PORT in consecutive instructions.
The second instruction is executed while the pin is still changing voltage, so the read phase of the second instruction will see the state of the pin BEFORE it started changing, and that is what will get written back to the port.
The effect is you lose the change made by the first instruction.

The speed at which the pin voltage changes is dependant upon load and capacitance on the pin.
The faster your PIC runs, the less time the pin has to change before this will affect you.

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Tue Jun 10, 2014 10:49 am
by _Banx
Exactly.

One way to avoid RMW problems is to keep a copy of the port in RAM. To change a bit (or bits) read the RAM copy, modify the data then write it to the port and back to RAM.

Steve.

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Jul 31, 2014 12:50 pm
by tunelabguy
I wonder why Microchip chose to implement BSF and BCF instructions as a byte-wide read-modify-write on all 8 bits? It seems they could have implemented these instructions as operations on a single bit in the output latch, leaving the other bits untouched. This could have been done even without the user-addressable LATx registers. Perhaps it takes more silicon to do it that way, given that pathways already exist for byte-wide operations?

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Jul 31, 2014 1:37 pm
by Tom Maier
It's easier to design the architecture for RWM than to make a true bit adressable architecture.

If I recall correctly, the 8051 had this same issue for the ports pins. I used to use 8051s a lot for the low end micros before microchip came along. Haven't used them for 20 years now. The 8051 was a more advanced architecture than the pics, but that extra architecture made the processor more expensive and microchip was competing in the market on the price and size, so the pics were smaller and cheaper, but they also had the weird architecture you had to contend with. 8051 and the motorola 68hcXX series were easier to program on the assembly language level and were more amiable to compiler writers, so the old rule of thumb was that if the job was small then do it in pic with assembly, and if the job was slightly larger and more complex then use the 8051 or the 68hc16 in C. If the job was much larger, then use an intel 80186. Pic was the low end of the food chain. With microchips current product line you have room to move up without leaving the company.

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Jul 31, 2014 1:42 pm
by vloki
Subsumtion fom Stefan Uhlemayr-> RMW and solutions for it

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Jul 31, 2014 2:04 pm
by Ian.M
Also see John Temples' explanation: http://www.xargs.com/pic/c-faq.html#rmw

@Tunelabguy:
If Microchip wanted to implement true bit writes, they'd need an individual write line to each bit in a byte bussed to every RAM and SFR location. That would be a heck of a lot of extra interconnects and still wouldn't help with instructions that modify more than one bit in a register. The simple solution was to connect the LAT registers to the databus at their own address, and that is what they have done for all newer device families.

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Jul 31, 2014 2:23 pm
by user2009
Here somebody reuses the standard headers. I bookmarked it but never used PIC16:
http://www.microchip.com/forums/m775818.aspx

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Jul 31, 2014 3:11 pm
by Ian.M
Yep. I contributed heavily to that. Its also linked from the end of Stefan's thread above.

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Wed Oct 10, 2018 9:00 pm
by JeremyNash
ric wrote:(note, this is still a work in progress. Initially it is aimed at chips which have LAT registers. It will be extended to cover parts which don't, such as mid-range PIC16Fxxx parts.)

Background
When you set or clear an individual bit in a port, the compiler produces either a BSF or BCF assembly instruction.
These instructions read the entire 8 bit port, change the indicated bit, and rewrite the entire 8 bit port.
That is why this is called a "read-modify-write" instruction.

If you address the PORT register, it reads the current state of the pins in the first step
If you address the LAT register, it reads the internal latch holding the current output states.
The write operation is identical for PORT and LAT.

Now think about what happens if you change two pins on the same PORT in consecutive instructions.
The second instruction is executed while the pin is still changing voltage, so the read phase of the second instruction will see the state of the pin BEFORE it started changing, and that is what will get written back to the port.
The effect is you lose the change made by the first instruction.

The speed at which the pin voltage changes is dependant upon load and capacitance on the pin.
The faster your PIC runs, the less time the pin has to change before this will affect you.


Hey
To change a bit (or bits) read the RAM duplicate, adjust the information at that point compose it to the port and back to RAM.

Re: FAQ: Read-modify-write (RMW). What it is and how to avoi

PostPosted: Thu Oct 11, 2018 2:41 am
by AussieSusan
Jeremy - you are correct when someone who understands the RMW issue actually creates the shadow register in RAM.
However there are a number of beginners who don't know about RMW, who fall into the trap and come asking (often before looking for existing answers or don't know the keywords to search for).
The thread was an attempt to help such people by providing something to point to without having to write it out each time.
Susan