Assembly waitcnt - How *exactly* does this work????
bill190
Posts: 769
The way I am thinking the assembly version of "waitcnt" works does not make sense to me? (Not the spin waitcnt version which I understand, rather the waitcnt used in assembly language...)
I am seeing stuff like this when searching for·waitcnt to learn more...
mov···· Time,·· cnt
add···· Time,·· #9
...
waitcnt Time,·· Delay
...
waitcnt Time,·· Delay
What I am not clear on is what exactly happens on the
"waitcnt Time,·· Delay" line.
I know that "Time" at that point equals cnt + 9...
And I assume that the "9" is the number of clock cycles the clock will increase by the time it gets to "waitcnt Time,·· Delay"? (The amount of clock cycles used up for add and the next waitcnt?)
What is confusing me is this from the Propeller manual:
"WAITCNT Target, (#) Delta"
"The WAITCNT instruction pauses the cog until the global System Counter equals the value in the Target register, then it adds Delta to Target and execution continues at the next instruction."
Note it does not say it waits for Delta as well!
So does that mean that it waits until the value is reached in Target which was previously stored in the lines above? Then·executes the next instruction (but prior to doing this it adds·the value of Delta to Target, but does not use that for a delay on THIS line?
Then uses the added Delta in the next waitcnt line?
So the Delta does not·have any effect on·the current waitcnt line, rather the next waitcnt line?
That is the way I am reading that explanation from the manual and that does not make sense????
·
Also the Propeller manual says the assembly waitcnt sets a bit (C) if there is a math addition which results in a overflow. [noparse][[/noparse]"If the WC effect is specified, the C flag will be set (1) if the sum of Target and Delta resulted in a 32-bit carry (overflow)."]
Is this a situation I would need to handle? As in check to see if there was an overflow and then handle that? Or do I not need to worry about that and it would wait in the same manner the spin waitcnt waits in an overflow situation?
·
I am seeing stuff like this when searching for·waitcnt to learn more...
mov···· Time,·· cnt
add···· Time,·· #9
...
waitcnt Time,·· Delay
...
waitcnt Time,·· Delay
What I am not clear on is what exactly happens on the
"waitcnt Time,·· Delay" line.
I know that "Time" at that point equals cnt + 9...
And I assume that the "9" is the number of clock cycles the clock will increase by the time it gets to "waitcnt Time,·· Delay"? (The amount of clock cycles used up for add and the next waitcnt?)
What is confusing me is this from the Propeller manual:
"WAITCNT Target, (#) Delta"
"The WAITCNT instruction pauses the cog until the global System Counter equals the value in the Target register, then it adds Delta to Target and execution continues at the next instruction."
Note it does not say it waits for Delta as well!
So does that mean that it waits until the value is reached in Target which was previously stored in the lines above? Then·executes the next instruction (but prior to doing this it adds·the value of Delta to Target, but does not use that for a delay on THIS line?
Then uses the added Delta in the next waitcnt line?
So the Delta does not·have any effect on·the current waitcnt line, rather the next waitcnt line?
That is the way I am reading that explanation from the manual and that does not make sense????
·
Also the Propeller manual says the assembly waitcnt sets a bit (C) if there is a math addition which results in a overflow. [noparse][[/noparse]"If the WC effect is specified, the C flag will be set (1) if the sum of Target and Delta resulted in a 32-bit carry (overflow)."]
Is this a situation I would need to handle? As in check to see if there was an overflow and then handle that? Or do I not need to worry about that and it would wait in the same manner the spin waitcnt waits in an overflow situation?
·
Comments
So,
And that's how you use it.
Note that you should NOT!!!!!!!!!!!!!!!!!!!!!!!!! Try to code for exact clock cycle timing. This will not work. You should at least try to give yourself the amount of time it takes the instruction to execute and a few clock cycles of overhead. Only when you start having a really good feel for how long it takes each instruction to execute and everything else should you go to the limit.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
Post Edited (Kye) : 5/6/2010 11:42:56 PM GMT
You don't have to worry about the overflow. The System Clock can also overflow and this is ignored. The addition operation in WAITCNT uses the same logic as the normal ADD instruction and this calculates an overflow bit which can be optionally written to the C flag.
Post Edited (Mike Green) : 5/7/2010 12:12:40 AM GMT
Thank you both for your replies, this one had me baffled...
·
A lot of people tend to try and use waitcnt just like a delay command and you can do this but it is more than that.
The cnt keeps incrementing all the time so you can for example take a note of cnt, do a load of processing and then waitcnt until a certain time has passed since the point you stored cnt, even if the processing takes a variable amount of time. This is very useful for fixed length loops (fixed in time) which have conditional statements and hence a varied number of commands.
Graham