Shop OBEX P1 Docs P2 Docs Learn Events
Assembly waitcnt - How *exactly* does this work???? — Parallax Forums

Assembly waitcnt - How *exactly* does this work????

bill190bill190 Posts: 769
edited 2010-05-07 17:33 in Propeller 1
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?
·

Comments

  • KyeKye Posts: 2,200
    edited 2010-05-06 23:37
    The best way to learn ASM code is to download one of the video drivers freely available and play with it. You lean timing and how stuff works really quick. Well, this was true at least for me.

    So,

                     mov     waitcntTimeVar, loopTimeInCycles 
                     add     waitcntTimeVar, cnt
     
    here             waitcnt waitcntTimeVar, loopTimeInCycles
                     ' ... Do stuff
                     xor     outa,           #1 ' Example
                     ' ... Do stuff
                     jmp     #here
     
     
    loopTimeInCycles long 20
    waitcntTimeVar   res  1
    

    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
  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-07 00:07
    You're right about Delay being used for the next time. There really are two separate operations. The first is the wait where the cog "stalls" in a low power mode until the Destination field is equal to the System Clock value. The second operation simply adds the Source field to the Destination field in preparation for the next WAITCNT.

    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
  • bill190bill190 Posts: 769
    edited 2010-05-07 00:21
    Mike Green said...
    You're right about Delay being used for the next time. There really are two separate operations. The first is the wait where the cog "stalls" in a low power mode until the Destination field is equal to the System Clock value. The second operation simply adds the Source field to the Destination field in preparation for the next WAITCNT...
    OK, I need to "plan ahead" then! burger.gif

    Thank you both for your replies, this one had me baffled...
    ·
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-05-07 17:33
    You don't have to use delta it just saves a line of code.

    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
Sign In or Register to comment.