Re: Please help me figure out a delay loop

From: Bob Masta (NoSpam_at_daqarta.com)
Date: 05/02/04


Date: Sun, 2 May 2004 12:26:12 +0000 (UTC)

On Sun, 2 May 2004 06:25:52 +0000 (UTC), "Matt Taylor"
<para@tampabay.rr.com> wrote:

>"Dan" <badtaste2k@hotmail.com> wrote in message
>news:rIYkc.45862$Qy.19235@fed1read04...
>> I'm having problems with creating a delay loop for a snake game I created
>> for class. The game is actually completed and works fine with the
>exception
>> of two problems. I already turned my code in a got full credit, so what I
>> ask is for my own personal knowledge, not for school. Anyways, I used a
>> hardware delay loop, basically cycling through a loop that counted down
>> 64000 times X amount of times.
><snip>
>
>That is normally how you delay, but the difference is that a proper delay
>loop is calibrated. As you suggested, the real-time clock is used. You use
>that to compute the value of the counter required to delay some small length
>of time, say 1 microsecond. Then you can figure out how many iterations are
>required to delay for any length of time: iters = time * iter/unit_time. It
>is even better to compute the value like this: iters = iter0 * delay /
>delay0 (where iter0 and delay0 are values from your calibration).
>

I suspect software delay loops are generally a bad idea, even if you
calibrate them when the program starts up. On today's CPUs things
just aren't all that predictable any more. Worse, there is no way to
use a software loop in a multitasking situation, unless maybe you are
writing kernel code that you know won't be interrupted.

>The real-time clock is a bit imprecise and will give you trouble; I would
>rely on the rdtsc instruction. Your CPU keeps a counter that increments with
>every clock cycle. The rdtsc instruction loads that value into edx:eax. The
>caveat is that you have to use the real-time clock to figure out how fast
>the CPU's clock is, then you can use rdtsc to time your calibration loop.
>

Instead of using RDTSC and the system timer to calibrate a software
timing loop, why not just use RDTSC in the loop? You would need
similar calibration to determine how many time-stamp ticks correspond
to the desired delay, then your delay routine would just read the
counter on entry and add the computed delay count to that to get
a target count. Then read the counter in a loop until you reach the
target. Note, however, that this is still subject to interruption in
protected mode multi-taskers like Windows, so the actual time
may be many milliseconds longer than you wanted.

I am wondering if the OP's problems with the BIOS functions
are because Windows doesn't really emulate them properly,
if at all. I normally use the system timer for timing under DOS,
by directly reading the time-of-day counter from hardware.
I've noticed that the times are incorrect when running in
a "DOS box". I haven't tested the BIOS functions, however.

Bob Masta
dqatechATdaqartaDOTcom
 
            D A Q A R T A
Data AcQuisition And Real-Time Analysis
           www.daqarta.com



Relevant Pages

  • Re: Write Signal on Disk II Question
    ... checked for sampling jitter using an oscilloscope? ... But I noticed another shocking thing: The timing loop I used to find ... the timing resolution is quite high, since the delay loop on the main ...
    (comp.sys.apple2)
  • Re: Write Signal on Disk II Question
    ... checked for sampling jitter using an oscilloscope? ... But I noticed another shocking thing: The timing loop I used to find ... the timing resolution is quite high, since the delay loop on the main ...
    (comp.sys.apple2)
  • Re: Write Signal on Disk II Question
    ... checked for sampling jitter using an oscilloscope? ... But I noticed another shocking thing: The timing loop I used to find ... the timing resolution is quite high, since the delay loop on the main ...
    (comp.sys.apple2)
  • Re: 6502 Delay Loops using Applesoft
    ... using delay padding instructions where necessary. ... I generally use index register loops (5 cycles per inner loop). ... you re-initialize ZP2 for each iteration ...
    (comp.sys.apple2)
  • Re: Write Signal on Disk II Question
    ... checked for sampling jitter using an oscilloscope? ... But I noticed another shocking thing: The timing loop I used to find ... the timing resolution is quite high, since the delay loop on the main ...
    (comp.sys.apple2)