Questions about P2 Instructions
Dave Hein
Posts: 6,347
I thought I would start a thread to ask about undocumented P2 instructions. There are several instructions in Prop2_Docs.txt where the opcode is shown, but there is no other information.
So my first question is what does SCL do? I assume it's a scaled multiply, but how exactly does it work?
So my first question is what does SCL do? I assume it's a scaled multiply, but how exactly does it work?
Comments
There was this:
Looks to me, like SCL does a fixed point multiply with D & S in 2.18 bit format. You get the result:
SCL a,b ' a = (a * b) ~> 18
a and b are 20bit numbers and the result is the 40bit product shifted right by 18 = 22 bits signed. (as potatohead says)
Andy
Clusso99 mentions it here:
http://forums.parallax.com/archive/index.php/t-146797.html
Edit: Current link is http://forums.parallax.com/showthread.php/146797-P2-Tips-amp-Tricks
C.W.
Now I'm looking at ESWAP8 and ESWAP4. So ESWAP8 does an endian swap on bytes, correct? Does ESWAP4 swap nybbles in a similar manner? Seems odd to devote an instruction for that. Would anybody ever use it?
What do INCPAT and DECPAT do? The documentation I found says that it sets D to the next/previous value with the same number of ones and zeros. Is this a sequential pattern where the sequence starting with 111 would be {111, 1011, 1101, 1110, 10011, ...}?
What does SETBYTS do?
SETX, INCPAT and DECPAT are correct (don't ask me for what INCPAT and DECPAT is good)
Here is waht I have found with some Tests on the FPGA: I don't know for what ESWAP4 is good. A nibble swap in every byte in D would perhaps be more useful
MOVBYTS D,#%%0123 does the same as ESWAP8 so the latter may be removed
Andy
I know. I'm about to start the doc work, so I'll address those. I'm just testing the T3SAVE/T3LOAD instructions now.
It also works well with ROLNIB to form a RORNIB substitute.
On the subject of instructions usage.
The INCMOD D,#n will increment D. If D=#n on increment D is set to 0 (z=1) and C=1(Rollover). If D > #n no rollover occurs.
Same applies to DECMOD, the D value must initially be in the range of 0 to #n for correct operation.
Re: INCMOD/DECMOD That sounds like a bug to me. In any case it's not intuitive at all. the value of D should be MODed regardless of it's starting value. Otherwise it's not a true MOD and the instruction should have a different name.
Sort of similar to the grey encoding where only 1 bit changes at a time.
That's what INCPAT/DECPAT do.
@roy: Thanks.
I'm assuming the definition for INCMOD and DECMOD is quite simple at the moment.
INCMOD : if d=#n then d=0 else add d,#1
DECMOD : if d=0 then d=#n else sub d,#1
This did trip me up for a while. I accidently had a out of range value once which upset things a bit.
Making them allow for out of range input values would add a lot more gates/silicon?
Andy,
Do you have a nice little test rig you use to run single instructions or blocks of code and print out results? That looks pretty handy!!
Results for SETBYTS show flags are not written. Also Pnut doesn't allow effects in source for SETBYTS.
It would add a magnitude comparator, which is bigger and slower than the current equality comparator. Regardless, I don't see why you would ever introduce an out-of-range initial value that would need to be limited on the first iteration. These instruction will set C if the limit was reached and the value was reset. For example 'INCMOD reg,#2 WC' will set the carry on every 3rd iteration (of course, you'd need to start with a value 0..2, with 0 being the likely candidate).
It's not really a MOD operation. It probably shouldn't be using MOD in the name. Maybe, INCWRAP/DECWRAP ?
INCMOD and DECMOD create modulus counter functions, so I don't think the names are too far off-base. From a software-background standpoint, I can see the issue. In software, macro things happen, while in hardware, micro things happen.
I'd agree with this summary. MOD has explicit and expected numeric outcomes.
In software, yes, I agree. But this is hardware-level and it seems to me that instructions to increment and decrement modulus counters could be named aptly. I mean, how would INC and MOD even fit together in the software paradigm? They are two mutually exclusive concepts, aren't they?
It has nothing to do with it being hardware or software. It's just what the expected behavior is for a fundamental math operation (modulus).
Mark my words, people will have bugs in their P2 code because of this, and they'll be confused and angry when they find out a MOD instruction doesn't actually do a MOD operation.
Since it's a lot more expensive to do the proper MOD, then I think the best action to take is to rename the instructions.
I'm sorry to be pressing this issue, but I think it's important. The P2 has become a beast of a MCU with so many instructions, and so many "special considerations" for working with those instructions, that I think it's really important to make sure as much as possible is clear and makes sense. Having to go back and read minutia details about every instruction while coding to make sure you got it right is anything but fun.
Chip?! These are instructions for PASM2 that we will use in our SOFTWARE. Just because it's combining what would normally be two things into an atomic operation, doesn't mean it's not software anymore!
Take the case of a power of two. Say we want to increment something with a three bit wrap around.
That's something basic like:
add count, #1 (or some other value really)
and count, #%111
That's a modulo increment, but it's limited to a power of two.
In PASM, and assembly language in general, we do that kind of thing all the time. Generalizing that to work with any modulo value is handy.
Requiring a bounds check isn't a big deal. Once that is done, the concept of modulo works just fine otherwise. Modulo or wrap around is a basic assembly language concept. It happens in pages, it happens in data sizes, nibbles, bytes, words, etc... too.
From a PASM standpoint the instruction name makes perfect sense. Put the bounds checking into the instruction documentation, and leave it to the programmer as to whether or not they feel it necessary to perform clipping around the instruction to handle unplanned values and such; otherwise, the instruction performs that basic assembly language modulo / wrap around on values other than a power of two, and that is something I think is well worth having in the instruction set.
Besides, don't we have a whole pile of basic things which employ that basic idea? Small ring buffers, some DSP related things... Seems to me we do, and I can't count the number of times I wish I could have used AND to get that done outside of a power of two, but couldn't. The work around was to go ahead and factor things into a power of two, or fall back on CMP and get it done as a more instruction heavy bounds check routine.
Most assembly programmers are going to grok the instruction, and MOD works in that context because of how so many things work anyway. Once we are above PASM, then who really cares? Other representations will make better sense as they should.
I say leave it. The instruction is just fine as is, name included.
You example with the ADD + AND works fine with starting values outside of the range. These instructions do not. To me that is a significant difference.
Also, the AND operation there is not a modulo operation, it just behaves like one for positive integer values.
I am seriously baffled at this whole discussion. To me it's plainly obvious that this is a bad thing to do, and can't figure out why anyone is defending it. Misleading names should be avoided!
It's like having an ADD operation that only works when the values are between 100 and 500, otherwise it doesn't add. It might have some use, but it's not a good idea to just call it ADD.