Sign in to follow this  
Followers 0
Conor

Masked Move

5 posts in this topic

I have the following code. I have two things that I need explained/clarifed. This is in a PLC5/80 SOR BST ADD N110:123 72 N110:126 NXB MVM B25:26 01FH B45:[N110:126] BND EOR Now the ADD is on the first branch. This is taking the word N110:123 and adding 72 to N110:126. I am looking at the offline version of the code now and I see; Source A = 23 Source B = 72 Dest = 28 The maked move in the branch below is using the value from N110:126. Now, the Masked Move. Maybe I am wrong, but I will try and explain how I read this. Source B25:26 Mask 01FH Dest B45:[N110:126] For example do I read this as follows; B25:26 0010011101110110 Mask 0000000000011111 B45 0000000000010110 I am converting the mask into binary and then adding the Source and the Mask in Binary, is this right? A follow on from this is that I have B25:26/5 being latched in a different routine. When this bit is 1 I do not see it coming through in my MVM. I probably am as far out as a lighthouse here, so any help with this would be great. Edited by Conor

Share this post


Link to post
Share on other sites
The Masked Move (MVM) "moves" data from the source to the destination. ONLY the bits in the source which have an associated bit set in the mask are moved to the destination. All other bits in the destination are not affected, they remain as they were before the MVM. The logical equation would be: DEST = (DEST AND (NOT MASK)) OR (SOURCE AND MASK) Edited by b_carlton

Share this post


Link to post
Share on other sites
So where I have 01F, which is 11111, this does a binary Add with the source and therefore only looks at the first 5 bits.

Share this post


Link to post
Share on other sites
It is not an ADD. We're working with OR, NOT, and AND here. Remember these bit logic rules: 1. NOT reverses things. So a 1 becomes a 0 and vice versa. 2. AND requires that BOTH bits are a 1. Otherwise the result is a zero. So if we think in terms of masking, then if the mask has a 1, it retains the value of the bit we are masking (1 AND 1 = 1, 1 AND 0 = 0). Otherwise, it becomes a 0 (0 AND anything = 0). 3. OR results in a 1 if EITHER bit is a 1. If both bits are 0, the result is 0. Breaking it down in steps: 1. AND the source with the mask. This puts zeroes everywhere except where the mask has a 1. The bits inside the mask retain their original (source word) values. Everything else becomes zero. Hold onto this temporary value. 2. AND the destination with the inverse of the mask (using NOT). This puts zeroes in every location where there is a 1 in the mask but leaves ALL OTHER bits intact. So the masked bits are zero'd out. Everything else keeps the original value (either 1 or 0). 3. OR the resulting two values. The masked source contains 1's only where there were ones in the source that were masked. The inverse masked destination contains 1's only where there were 1's OUTSIDE the masked area, and zeroes where the mask is. The Logical OR puts 1's where there are 1's in either of the two temporary values, and because we used masks that are inverses of each other, you will have 1's only in one of the two temporary values. The result is that you are copying the value of the source into the destination (a MOV), BUT only where you have a 1 in the mask. Everything else retains the original value of the destination. This is not an ADD, not even close. It's a move. We are not invoking XOR (which is the bitwise version of ADD if we treat the word as a ring or field). In fact mostly we're using AND (which is multiplication in the bitwise world). The most common use for it is when you have a whole series of bits that represent some state and you want to clear or set a bunch of them in one fell swoop. An example is when you have lots of pilot lights and you add a "test" push button. You can create a mask for your output table where each 1 bit is the location of a pilot light. Then when you need to turn on (or off) all the pilot lights at once, you can use a masked move (actually you could use an OR as well) to turn on all the pilot lights at once and override the rest of your code. You may see this with some state machine implementations, changing just a few bits under program control in IO configuration tables, or manipulating sequencer (SQO/SQI) tables. On a PLC 5, I've seen some very heavy abuse of the MCP's using this. First let me say that I do NOT recommend this approach because having multiple MCP's means executing the PLC's housekeeping routines several times per scan. That being said, you can selectively turn different parts of your program on/off using masked move commands. It is much more efficient (even with the JSR penalty on a PLC 5/80) to use JSR's, but I have seen the MCP trick used more than once.

Share this post


Link to post
Share on other sites
An easy way to remember how the mask works: 0011 0011 0101-source 0000 0001 1111-mask 0000 0001 0101-destination The bits from the source fall through the mask, but the fat zeros block them. The skinny 1s, allow the data to fall through to the destination. It's important to remember that if the destination already had a bit set where the mask has a zero, then it won't be changed by the source and will retain the value it had prior to the MVM. Paul

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0