Shop OBEX P1 Docs P2 Docs Learn Events
wait pne/peq and the Z flag — Parallax Forums

wait pne/peq and the Z flag

BradCBradC Posts: 2,601
edited 2007-10-03 18:36 in Propeller 1
The propeller quick reference gives the Z flag as result = 0.

What is the result?

For example, I'm waiting on 2 pins to be in a certain state.. let's say..

pinstate = 0100
pinmask = 0110

If I do waitpeq pinstate, pinmask wz
What happens to the Z flag? I would expect it to be 0 as pinstate & pinmask <> 0

If I'm using waitpne pinstate, pinmask wz
If the pins are all clear, I'd expect the Z flag to be set, as pinstate & pinmask == 0.

I'm not seeing this behaviour however. I have to do an additional "test pinmask, ina wz" to properly set
the Z flag. Am I missing something obvious?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-09-30 15:02
    As I understand the instruction, the pinstate and ina values are each masked with the pinmask value, then the results of this are compared (subtracted) and the Z flag is the result of the comparison (not the masking). For waitpeq, it will always be true. For waitpne, it will always be false.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 15:06
    (1) There is no general rule for the C flag, there is more systematic with the Z flag, but you should ALWAYS consult the manual, though it is sometimes a little bit curt with the flags (see SUBS e.g.)

    (2) Your guess "pinstate & pinmask" can not really be a possibility... Why should that be computed? I have the (shrude) suspicion that you think this is what WAITPEQ does. But is does not!

    WAITPEQ state, mask

    waits until state == (mask&ina)

    (3) So mask&ina is computed and Z is set when THIS AND is zero, i.e. none of the masked pins is on.

    (4) This means WAITPEQ will clear Z, except your requested state is really 0.
    The flag could be slightly more useful with WAITPNE. However I have never needed it.

    Post Edited (deSilva) : 9/30/2007 3:13:28 PM GMT
  • BradCBradC Posts: 2,601
    edited 2007-09-30 15:14
    deSilva said...


    WAITPEQ state, mask

    waits until state == (mask&ina)

    (3) So mask&ina is computed and Z is set when THIS and is zero, i.e. none of the masked pins is on.

    (4) This means WAITPEQ will clear Z, except your requested state is really 0.
    The flag could be slightly more useful with WAITPNE. However I have never needed it.

    I did miscommunicate what I was doing.. I actually meant ina & pinmask.
    Let's do some code shall we ?
    
    J        long        1
    K       long        2
    Mask     long       3
    
    :loop
          waitpne J, Mask  wz
      IF_Z wrlong zero, debug
      IF_NZ wrlong allones, debug
         waitpeq J, Mask
         jmp    #:loop
    
    I *never* see zero written to the debug register.. 
    If I add
        test   Mask, ina wz
    after the waitpne, then of course I get what I'm after.. 
    
    



    If the pins 0 & 1 are both zero, then waitpne J, Mask wz (which is 0001 / 0011) should set the Z flag,
    as ina & Mask == 0 .. this is not happening.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 15:55
    So maybe Mike is right?
    But where does he Know it from??

    Post Edited (deSilva) : 9/30/2007 4:02:03 PM GMT
  • CJCJ Posts: 470
    edited 2007-09-30 16:06
    page 412 of the propeller manual states

    "if the WZ effect is specified, the Z flag will be set (1) if the result of INx ANDed with Mask is zero."

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Parallax Forums - If you're ready to learn, we're ready to help.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 16:06
    But on the other hand your code is too tricky
      IF_Z wrlong zero, debug
      IF_NZ wrlong allones, debug
      waitpeq J, Mask
      jmp...
    



    A %00 will stop the loop just before the jump and you might have read "old" values.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 16:07
    @ CJ: Thank you! I know I quoted from that page smile.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2007-09-30 16:10
    deSilva (and others),
    I have no magic sources of information other than many years of experience with a very wide range of computing hardware and software.
    I try to read manuals carefully and I seem to have a good memory for detail. I also make mistakes, can be forgetful, and sometimes have strong opinions. CJ nicely found the answer in the manual and I was mistaken (again). Thank you.

    Post Edited (Mike Green) : 9/30/2007 4:15:40 PM GMT
  • BradCBradC Posts: 2,601
    edited 2007-09-30 16:29
    CJ said...
    page 412 of the propeller manual states

    "if the WZ effect is specified, the Z flag will be set (1) if the result of INx ANDed with Mask is zero."

    I have pin 0 pulled high and pin 1 pulled low with resistors..

    If I short pin 0 to ground, the loop progresses.. (condition 0001 Mask 0011 ina 0000) but the Z flag is not set

    According to the manual, it should be..

    I've tested, re-tested and then tested again.. there must be something I'm missing.

    I'm not reading old values because inserting

    test mask, ina wz

    *after* the waitpne that is *supposed* to clear the Z flag makes the reading correct..

    If pin 1 is tied to ground (and it is), then the only way to make the loop run is to pull pin 0 low.. thus making both 1 & 0 low..
    "and" that against the mask of %0011 and the result should be zero.. unless I'm very much mistaken.. %0000 & %0011 should be zero
    I've double checked all the variables. J = %0001 K = %0010 Mask = %0011

    My meter tells me at the time the loop progresses through that both pins 0 & 1 are 0v

    the waitpeq J, Mask at the end of the loop ensures I don't repeat the test/wrlong until ina = %0001

    Parallax ?? anyone?
  • AribaAriba Posts: 2,685
    edited 2007-09-30 18:56
    I have made some tests with PASD (Propeller Assembly Source Debugger), and have to affirm BradCs statement.
    The Z-Flag is always cleared, no matter if the Pin States AND ina results to Zero, or if you use WAITPNE or WAITPEQ.

    Andy
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 19:14
    The flag was never of any value as it were....
  • BradCBradC Posts: 2,601
    edited 2007-09-30 19:18
    Oh really? I wonder if it were of no value why I'd spend so much time trying to get it to work?


    Thanks Andy [noparse]:)[/noparse]
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 19:36
    Indeed, what did you want it for in the first place??
  • BradCBradC Posts: 2,601
    edited 2007-09-30 19:43
    To save 4 cycles and a long...

    I'm pausing on one state (01), and with that flag I can tell if I've got both pins as zero (00), or the pins have both inverted (10).
    When you have less than 26 cycles to play with in a loop.. 4 is a lot..

    The extra instruction to detect the (00) state is somewhat unwelcome.

    Doc says it should do it.. It don't do what it says on the tin..

    Useless indeed...
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-30 20:51
    I see... but it is a rare application...
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-10-01 17:52
    So. Is this a bug in the prop or a bug in the docs?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-10-01 19:14
    Fred Hawkins said...
    So. Is this a bug in the prop or a bug in the docs?
    It's a bug in whichever is easier to fix. (I think that implies the manual. smile.gif )

    -Phil
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-10-01 23:21
    LOL. Neither. It's another for the Tricks and Traps.
  • Jeff MartinJeff Martin Posts: 756
    edited 2007-10-02 01:04
    Hi,

    Wow, lot's of energetic discussion here. ;-)

    The Mask is ANDed with INx first,·that result is·subtracted from State and if that resulting value·is 0, the condition is true, otherwise false.· For this reason, it is vital that State not have any 1s that don't correspond to 1s in Mask, else a WAITPEQ will never be true.

    As for the Z flag:

    I'll get an official answer as soon as I can, but it appears to me that Z is actually only set in the case where each of Mask and State are 0.· Of course, you can only see the result with a WAITPEQ because that's the only one that will move on in that case (regardless of the state of INx).

    I still feel like I'm not seeing the entire picture here though... got to go to the source.· We'll clarify it here soon.· Sorry for the delay.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Jeff Martin

    · Sr. Software Engineer
    · Parallax, Inc.
  • Jeff MartinJeff Martin Posts: 756
    edited 2007-10-02 20:36
    Okay all,

    Thanks for your patience.

    Here is how the Z flag is affected by WAITPEQ and WAITPNE (with a WZ effect) according to lots of testing I've done.· We haven't confirmed this via the schematics yet, but we're pretty certain enough to tell you.

    WAITPEQ: Z = 1 if State + Mask = 0.
    WAITPNE: Z = 1 if State + Mask + 1 = 0.

    Now, I don't think we really·intended for Z to be of any particular use with these two instructions, but rather let it be whatever it naturally ended up; just like with C.

    That being said, I will be revising the docs to indicate that Z is undefined, just like C is, for those two commands.

    Sorry for the confusion.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Jeff Martin

    · Sr. Software Engineer
    · Parallax, Inc.
  • BradCBradC Posts: 2,601
    edited 2007-10-03 12:32
    Thanks for the reply Jeff, at least now I know why my code was not working [noparse]:)[/noparse]

    A documentation update might be a good idea.

    It *could* have been a useful flag..
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2007-10-03 18:26
    Jeff Martin (Parallax) said...



    WAITPEQ: Z = 1 if State + Mask = 0.
    WAITPNE: Z = 1 if State + Mask + 1 = 0.

    ...·Z is undefined, just like C is, for those two commands.
    Jeff,
    Are there any cases where State + Mask = 0 (for Waitpeq wz) or State + Mask + 1 = 0 (for Waitpne wz) where z = 0? Or conversely,·does z always equal zero when neither of these cases are fulfilled?

    It seems if both of these things are true, then z is not undefined but merely complicated.

    Fred
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-10-03 18:36
    It's not that it's undefined, it just produces a result which has no discernable use for a programmer. So rather go into a complex explanation of what result it produces when it is of no use anyways, it's easier to simply state it's undefined.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.