Jump to content
IGNORED

Safe timer wait - reprise


Andrew Davie

Recommended Posts

This is some now-fixed code (I hope!) I have been using to wait for timers to expire.

The key is that I want to know exactly how much time was left on the timer -- which is returned in the x register.

The trick has been to catch the already-expired case when entering this routine, and the expired-during-the check-for-expired, and also the expired-after-the-check-for-expired. I think this code does all that -- if at any time during the various checks, the timer does actually expire, and TIMINT gets set and INTIM starts counting down in 1-steps... I think this will catch all of those and return a "correct" value in x giving how much time is.

 

safeTimerWait

    ; do not use for TIM64T values > 0x7F
    ; Now we wait for expiry of the timer.
    ; The code records the available time for display. This is the INTIM value at the start
    ; of the wait. This INTIM read clears the TIMINT flag, so we go through some convolutions
    ; to make it all work.


                    ldx #0
                    bit TIMINT
                    bmi .zeroTime                   ; already overtime!
                    lda INTIM
                    beq .zeroTime                   ; also time expired
                    bmi .zeroTime                   ; must have been just overtime and now counting down
                    tax
.xOSwait            bit TIMINT                      ; wait for the timer
                    bpl .xOSwait

.zeroTime           stx TimeLeftOS                  ; x holds the "how much time left"
                    rts

I use this sort of thing to reliably call an idle loop in C/ARM code.  For example...

 

                    ldx #OS_TIM64T
                    stx TIM64T              ; set timer for OS

                    ldy #_FN_GAME_OS
processArm          jsr CallArmCode
                    ldy #_FN_GAME_IDLE
                    jsr safeTimerWait
                    cpx #10
                    bcs processArm
                    jsr safeTimerWait

The above calls the C/ARM Overscan code, and then multiple idle calls until the available time is smallish, and finally waits for the timer to fully expire.

It's been working reliably now for some time.

 

  • Like 5
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...