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

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

Postby ric » Tue May 27, 2014 2:56 am

(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.
My new leslie-special website (for fans of The Great Race) :)
User avatar
ric
Verified identity
 
Posts: 341
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

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

Postby _Banx » Tue Jun 10, 2014 10:49 am

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.
User avatar
_Banx
 
Posts: 1
Joined: Wed Jun 04, 2014 9:55 pm
Location: UK, up north
PIC experience: Professional 5+ years with MCHP products

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

Postby tunelabguy » Thu Jul 31, 2014 12:50 pm

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?
User avatar
tunelabguy
Verified identity
 
Posts: 29
Joined: Sun Jul 20, 2014 9:41 pm
PIC experience: Professional 5+ years with MCHP products

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

Postby Tom Maier » Thu Jul 31, 2014 1:37 pm

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.
User avatar
Tom Maier
Verified identity
 
Posts: 179
Joined: Mon May 26, 2014 2:37 pm
PIC experience: Professional 5+ years with MCHP products

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

Postby vloki » Thu Jul 31, 2014 1:42 pm

Subsumtion fom Stefan Uhlemayr-> RMW and solutions for it
vloki
Verified identity
 
Posts: 162
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

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

Postby Ian.M » Thu Jul 31, 2014 2:04 pm

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.
Ian.M
Verified identity
 
Posts: 94
Joined: Wed May 28, 2014 12:47 am
PIC experience: Professional 1+ years with MCHP products

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

Postby user2009 » Thu Jul 31, 2014 2:23 pm

Here somebody reuses the standard headers. I bookmarked it but never used PIC16:
http://www.microchip.com/forums/m775818.aspx
user2009
 
Posts: 13
Joined: Tue Jul 01, 2014 11:48 am
PIC experience: Professional 2-5 years with MCHP products

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

Postby Ian.M » Thu Jul 31, 2014 3:11 pm

Yep. I contributed heavily to that. Its also linked from the end of Stefan's thread above.
Ian.M
Verified identity
 
Posts: 94
Joined: Wed May 28, 2014 12:47 am
PIC experience: Professional 1+ years with MCHP products


Return to Digital I/O Ports

Who is online

Users browsing this forum: No registered users and 1 guest

cron