View Single Post
Old 19 September 2015, 21:29   #4

phx's Avatar
Join Date: Nov 2009
Location: Herford / Germany
Posts: 1,569
Untested, but try something like this:
CIAB            equ     $bfd000

CIATALO         equ     $400
CIATAHI         equ     $500
CIACRA          equ     $e00

        ; clear and start Timer A
        lea     CIAB,a0
        moveq   #0,d0
        move.b  d0,CIATALO(a0)
        move.b  d0,CIATAHI(a0)
        ; force load, one-shot mode, start timer
        move.b  #%00011001,CIACRA(a0)

        ; ... do something ...

        ; stop timer and read result

        lea     CIAB,a0
        move.b  #$00,CIACRA(a0)
        moveq   #0,d0
        sub.b   CIATAHI(a0),d0
        lsl.w   #8,d0
        sub.b   CIATALO(a0),d0

        ; d0: time in 02 clocks
I chose CIA-B Timer A as the CIA-B timers are not used by the operating system. But note that other programs may use them, like for example a protracker playing routine.

I have set the timer to one-shot mode, so it stops when it underflows. Then you have a chance to detect such a case. When the code to measure requires more time than 65536 02-clocks (ca. 92ms) you can add Timer B and let it count underflows of Timer A. You would set bit 6 of CIACRB for that.

The CIA timers are counting backwards, so the result is subtracted from zero. To get the time in milliseconds or microseconds you need to know that the timer runs with 709379Hz on a PAL and with 715909Hz on an NTSC system.

If you want to be very precise you have to subtract the time for
        lea     CIAB,a0
        move.b  #$00,CIACRA(a0)
from the result.
phx is offline  
Page generated in 0.04963 seconds with 11 queries