Shop OBEX P1 Docs P2 Docs Learn Events
Modifying cnt? — Parallax Forums

Modifying cnt?

DragonRaider5DragonRaider5 Posts: 13
edited 2016-01-24 11:53 in Propeller 1
Hey,
I'm currently reading the CMUcam4 firmware in order to understand how to read the internal camera and then process the image. But there is one part which made me wondering what exactly its purpose is/how it works:
                        mov     cnt,                   #7                     ' Align instructions. Can be 6, 7, or 8.
if_nz                   add     cnt,                   #8                     '
                        add     cnt,                   cnt                    '
                        waitcnt cnt,                   cnt
My problem is: The manual states, that you should not use cnt as a target register in asm. However here this is what is beeing done so what's this doing for the program? I know what cnt is but I dont know why you would change it :/

Thanks in advance,
DragonRaider5

Comments

  • ErNaErNa Posts: 1,752
    It looks like a shadow register. You set the shadow to 7, if not z you add 8, -> set to 15, then you add the actual count (read access to count gives the counter, and next you wait, until the actual counter meets the shadow registers value. Now guarantee
  • When used in the destination field of a PASM instruction, cnt is just a regular old cog register. However, when you use it in the source field, you get the current value of the system counter. As far as the assembler is concerned, cnt is an alias for $1F1.

    Knowing that, your code first stores the number 7 in register $1F1, then adds the number 8 to the value in register $1F1 if the z flag is not set, then adds the value of the system counter into register $1F1, and then waits until the system counter equals that value. Since the source field of the waitcnt instruction is cnt, the value of the system counter is added to register $1F1 after the waitcnt is finished. I can't imagine how this last step is useful, but it likely doesn't matter, since it's very possible that the value in register $1F1 isn't used again (i.e. that the next operation that affects it is a mov that completely overwrites it).

    The following code will compile to exactly the same machine code. However, I wouldn't recommend using the following code without renaming tmp to something like tmpd to indicate that you can only use it in the dest field (otherwise you'll get the value of the system counter).
                            mov     tmp,                   #7                     ' Align instructions. Can be 6, 7, or 8.
    if_nz                   add     tmp,                   #8                     '
                            add     tmp,                   cnt                    '
                            waitcnt tmp,                   cnt
    
                            org     $1F1
    tmp                     res     1
    

  • Cool, thanks a lot :D
Sign In or Register to comment.