Shop OBEX P1 Docs P2 Docs Learn Events
What's the best way to wait for a lock with a timout in assembly? — Parallax Forums

What's the best way to wait for a lock with a timout in assembly?

LawsonLawson Posts: 870
edited 2008-07-25 16:59 in Propeller 1
I just started hacking together a few programs in assembly. (already liking Propeller assembly FAR better than SX assembly) I'm adding an assembly program to an existing application that uses a shared serial port protected by a lock. I've passed my assembly all the information it needs to work. As stated in the subject, I'm wondering what the best way to wait for a lock to be free or a timeout to occur is in assembly?

My current code is below, I'm concerned that It won't deal with wraparounds of CNT properly.

:loop          'lots of code that we don't care about
                mov     timeout,        cnt                     'mark the current CNT
                add     timeout,        wait_time               'move my mark far into the future        
:ck_lock        cmp     timeout,        CNT             wc      'is CNT greater than "timeout"?
        if_c    jmp     #:end_loop                              'out of time, go to the end of the loop        
                lockset xtx_lock                        wc      'check if the TX lock is free and set the lock if it is free
        if_c    jmp     #:ck_lock                               'keep waiting till the lock is free
                .
                .
                .
:end_loop    jmp    #:loop

'variables etc...
xtx_lock        long    0-0
wait_time       long    11000
timeout         res     1



Thanks,
Marty

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?

Post Edited (Lawson) : 7/24/2008 10:00:00 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-24 22:05
    Just like in Spin, it's better to remove CNT from timeout calculations to avoid wraparound issues.
    :bgn  mov   startTime,cnt            ' Save start time
    :chk  mov   temp, cnt                 ' Get current time
             sub    temp,startTime        ' Now we have a time difference
             cmp   temp,timeout     wc  ' Is difference less than timeout
     if_nc jmp   #:endTime               ' Timeout occurred
             lockset xtx_lock           wc  ' Check for lock available
     if_c   jmp   #:chk
                                                      ' Lock now available
             lockclr xtx_lock                  ' Free the lock
             jmp    #:bgn                      ' Start all over again
    


    Note that this requires that the timeout period be less than 2^32
    clock cycles (about 50 seconds at 80MHz).
  • evanhevanh Posts: 15,545
    edited 2008-07-24 22:57
    :chk  mov   temp, cnt                 ' Get current time
             sub    temp,startTime        ' Now we have a time difference
             cmp   temp,wait_time   wc  ' Is difference less than timeout
    


    Yes, a very important basic coding principle. It should be at the start of every beginner's programming book. It applies to a whole world of threshold logic.

    This is a good example of the difference between the far too simplistic educational examples and that of a really useful tool.


    Evan
  • LawsonLawson Posts: 870
    edited 2008-07-25 16:59
    Huh, already knew about that trick in Spin... looking at ASM must be truning my brain to blackberry jelly. The CMP op-code was confusing me. I thought because it was based on subtraction I could drop the extra instruction.

    Marty

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lunch cures all problems! have you had lunch?
Sign In or Register to comment.