News:

  • July 02, 2026, 03:17:35 AM

Login with username, password and session length

Author Topic: SET Instruction with V_Register and Index  (Read 11064 times)

MAEdwards

  • Sr. Member
  • ****
  • Posts: 56
SET Instruction with V_Register and Index
« on: June 04, 2015, 02:41:14 PM »
Greetings Everyone!

I am looking for a a little help with my scenario.

Often times I want to set an individual bit in a Word or Double Word.

When using a User Memory Block in the following configuration:
-Name: Whatever
-Data Type: Bit
-Range: 0-31
I can create: SET Whatever[Index].

When I am using a V_Register I can create: SET V0:0

However, I have not been to create:
SET V0:Index or SET V0[Index]

Is there a Casting convention that I am failing to see OR am I having an Instruction and Data Type incompatibility issue?
Any help would much much appreciated.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3833
    • Host Engineering
Re: SET Instruction with V_Register and Index
« Reply #1 on: June 04, 2015, 03:24:39 PM »
There is no "indirect" casting mechanism, e.g. to cast to a variable bit of a V WORD.

HOWEVER, if you ever need to do any "indirect" manipulation, make the DATA-TYPE of the user data-block be the SMALLEST SIZE of indirection you think you need.  If it's BITs, make your data-block a block of BITs, EVEN IF you want to use them as REALs (because you can always cast to LARGER):

MyBlock as bits, but I want to use them as 100 REALs (so it would be 3200 "bits" long, cuz each REAL is 32 bits)

Now I can easily reference bits individually:
MyBlock0
MyBlock1

or as REALs
MyBlock0:R  // the first REAL
MyBlock32:R  // the second REAL (yes, ID's need to bump by 32 at a time)
MyBlock64:R  // the third
MyBlock96:R  // the fourth

But I can indirectly reference bits (array index is BIT ID)
MyBlock[V0]
where V0 is the bit ID

And I can even look at REALs them indirectly as arrays
MyBlock[V0]:R
where V0 is still the bit ID, hence it should equal 0 then 32 then 64 then 96 then ... (not 0, 1, 2, remember the index is still a BIT ID).

Note: V0 actually COULD be anything, but even that is handled:
e.g. V0 equal to 0..31 resolves to ID of 0, V0 equal to 32..63 resolves to ID of 32, V0 equal to 64..95 resolves to 64, V0 of 96..127 resolves to ID of 96
Cast to REAL or Signed Word (:SW) or Unsigned Byte (:UB), but the BASE data-block is BITs.

Conclusion: You can use array indexing to casts of LARGER sizes.  Array index works on the ID portion, but you cannot use indexing on the CAST side (past the ":").  Hence, make your data-blocks data-type be the SMALLEST size needed for "indirect casting".

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6164
  • Yes Pinky, Do-more will control the world!
Re: SET Instruction with V_Register and Index
« Reply #2 on: June 04, 2015, 03:52:43 PM »
You can create essentially the same mechanism by using bits as the base type, then doing aggregation casts to get the larger element.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

MAEdwards

  • Sr. Member
  • ****
  • Posts: 56
Re: SET Instruction with V_Register and Index
« Reply #3 on: June 04, 2015, 05:01:21 PM »
Thanks Franji1 and BobO!

I was afraid that this was the case.  I had already had success utilizing your suggestions, but was failing to execute the indexed V WORD.

I will use the User Data Blocks for now.
Thanks again for your help.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3619
  • Darth Ladder
Re: SET Instruction with V_Register and Index
« Reply #4 on: June 04, 2015, 05:45:31 PM »
Or to set bit V10 in D1000, do

Code: [Select]
MATH D1000 D1000 | (1 << V10)
to clear

Code: [Select]
MATH D1000 D1000 & (1 << V10)
NOTE:  2**V10 is equivalent to 1 << V10 but is much slower.
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

b_carlton

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 606
    • thePLCguy
Re: SET Instruction with V_Register and Index
« Reply #5 on: June 04, 2015, 06:19:04 PM »
Quote
MATH D1000 D1000 & (1 << V10)

I think it would have to be an AND with the NOT of 1 << V10

Possibly :

D1000 & ~(1 << V10)
« Last Edit: June 04, 2015, 06:24:34 PM by b_carlton »
An output is a PLC's way of getting its inputs to change.

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3619
  • Darth Ladder
Re: SET Instruction with V_Register and Index
« Reply #6 on: June 04, 2015, 06:27:03 PM »
Wups -- typing faster than thinking fault!   ;D

Thanks for catching it!
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.