Shop OBEX P1 Docs P2 Docs Learn Events
Waiting for CNT to be equal OR LARGER than a value — Parallax Forums

Waiting for CNT to be equal OR LARGER than a value

jac_goudsmitjac_goudsmit Posts: 418
edited 2011-07-14 19:44 in Propeller 1
Hi everyone,

I'm working on a project that lets a Propeller bitbang the pins of a 6502 processor, including the clock input (see this page for more info).

The main loop of my Propeller code has to do its work within the 1 microsecond that it takes for the 6502 processor to execute a clock pulse, so timing is very tight. Obviously this part of the project is written in Assembly.

During normal operation, I can use a WAITCNT:
MainLoop
        ...                  ' Do stuff for the first part of the clock pulse
        waitcnt clock, count ' Wait until enough time passes
        or OUTA, mask_CLK0   ' Set the clock output

        ...                  ' Do stuff for the second part of the clock pulse
        waitcnt clock, count ' Wait until enough time passes
        andn OUTA, mask_CLK0 ' Reset the clock output
        jmp #MainLoop
As long as the Propeller can get its work done within the time limit, this works fine. But during certain special operations, I have to access the hub and perhaps later on even an SD card reader or serial port. So, I'm running into the possibility that my Propeller code is not done when it's already time to flip the clock output.

For the 6502 this is no big deal: it can run much slower than the usual 1MHz (as a matter of fact I'm running it at 10Hz for debugging), but if I use WAITCNT, I'll have to wait until the counter comes around to the same value again (50 seconds?). This is unacceptable. I also can't use a timer because that allows the 6502 to "run away" from the Propeller.

So, what I need is basically some assembly code that waits for an amount of time since the previous event, or continues right away if that minimum time has already passed. Is there an easy solution to do this, e.g. by using a WAITVID (obviously with the video pin mask set to 0)?

Unless there's no other way, the following is what I don't want to do:
MainLoop
        mov t1, CNT          ' Store current time

        ...                  ' Do stuff for the first part of the clock pulse

:loop1
        mov t2, CNT          ' Store current time
        sub t2, t1           ' Time difference in t2; Subtracting eliminates chance that t2 < t1
        cmp t2, count wc
  if_nc jmp #:loop1

        or OUTA, mask_CLK0   ' Set the clock output

        ...                  ' Do stuff for the second part of the clock pulse

:loop2
        mov t2, CNT          ' Store current time
        sub t2, t1           ' Time difference in t2; Subtracting eliminates chance that t2 < t1
        cmp t2, count_times_two wc
  if_nc jmp #:loop2

        andn OUTA, mask_CLK0 ' Reset the clock output
        jmp #MainLoop
Thanks in advance for any suggestions!

===Jac

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2011-07-14 18:57
    As a quick solution I can think of an NCO counter (mis)used as a monoflop. Downside is it consumes a pin. So you set phsx to e.g. -128 (output high) and wait for the output to go low. Once the 128 cycles have passed you have a 26sec (low) window which would make the waitpxx fall right through.
  • jac_goudsmitjac_goudsmit Posts: 418
    edited 2011-07-14 19:40
    Thanks Kuroneko! I didn't see an obvious way to use a timer in monoflop mode but I guess if I have 26 seconds to fake it, it's doable.

    ===Jac
  • kuronekokuroneko Posts: 3,623
    edited 2011-07-14 19:44
    Yes, not quite as mono as one wishes but good enough most of the time.
Sign In or Register to comment.