Board index » delphi » Timer in DOS, running in a DOS-Box of Win95

Timer in DOS, running in a DOS-Box of Win95

My problem:

For a game, I want to use the timer (normally running at 18.2 /sec), and
make it faster. In DOS, this is no problem, but when running in a
DOS-Box of Win95, every try to change the frequenzy of the timer has no
effect. Windows does not allow this.
Is there a way to make the timer run faster in a DOS-Box, too? Or can I
use a timer supported by Windows itself (in a DOS-programm)??

Thanx for you help!!

Arno Fehm

P.S. Sorry for my bad English...

 

Re:Timer in DOS, running in a DOS-Box of Win95


Hi Arno,

Windows (95/98) does allow you to change the tick-rate of the timer.
It seems that, under Windows, the timer is running at a 1000 Hz by
default. Windows uses this high rate to time-stamp events and
messages. Windows does NOT allow you to specify a timer rate higher
than a 1000 Hz... and I guess that all the lower rates are simply
locked onto this 1000 Hz rate. I would therefore advise everyone who
want to use an accurate timer in a DOS-box to set the timer to 1000
Hz, ALL other frequency are either not allowed or inaccurate.

A complete other matter is the way you want to use the timer. I've
seen too much code in which the Timer Interrupt was overloaded with
additional routines for updating databases, displaying graphics, and
so on. Clearly Interrupts are not intended for these types of tasks.
Using the timer interrupt in this way will either make your program
instable (on slower computers), or make your timing inaccurate. The
only task the timer interrupt should normally perform is "counting
time".

Best regards,

Peter de Jong
oa...@programmers.net
http://www.nici.kun.nl/oasis
http://www.nici.kun.nl/oasis/vesa (256 color vesa)

On Fri, 22 Jan 1999 18:44:17 +0100, Arno Fehm <af...@bigfoot.de>
wrote:

Quote
>My problem:

>For a game, I want to use the timer (normally running at 18.2 /sec), and
>make it faster. In DOS, this is no problem, but when running in a
>DOS-Box of Win95, every try to change the frequenzy of the timer has no
>effect. Windows does not allow this.
>Is there a way to make the timer run faster in a DOS-Box, too? Or can I
>use a timer supported by Windows itself (in a DOS-programm)??

>Thanx for you help!!

>Arno Fehm

>P.S. Sorry for my bad English...

Re:Timer in DOS, running in a DOS-Box of Win95


Quote
Arno Fehm <af...@bigfoot.de> wrote:
> My problem:
> For a game, I want to use the timer (normally running at 18.2 /sec), and
> make it faster. In DOS, this is no problem, but when running in a
> DOS-Box of Win95, every try to change the frequenzy of the timer has no
> effect. Windows does not allow this.
> Is there a way to make the timer run faster in a DOS-Box, too? Or can I
> use a timer supported by Windows itself (in a DOS-programm)??

In my case, I was able to change the frequency of the timer in a dos
box, but only for small frequencies (<1000Hz). As far as I know, you
can't use the windows timer (or any other windows function) in a
DOS program. I think the best thing you can do is to use the RDTSC
instruction for timing, and at the start of your program use the
standard timer to measure how many instructions/sec the computer
is able to do. The timing will look like this (sortof pseudocode):

framecounter = 0;

instructions_per_sec = read_computer_speed();
instructions_per_frame = instructions_per_sec / 40;  
                                          { 40 frames per sec }

while not gameover do

        rdtsc_old = read_rdtsc();

        update_game(framecounter);

        rdtsc_new = read_rdtsc() - rdtsc_old;
        if rdtsc_new < instructions_per_frame then
                while (rdtsc_new <= instructions_per_frame)  
                        rdtsc_new = read_rdtsc() - rdtsc_old;
                end while
                frame_counter = frame_counter + 1;
        else
                frame_counter = frame_counter + (rdtsc_new/instructions_per_frame);
        end if

end while

Quote
> Thanx for you help!!

I hope this helped a bit,
Jaap.
Quote
> Arno Fehm
> P.S. Sorry for my bad English...

Re:Timer in DOS, running in a DOS-Box of Win95


Hi Jaap!

Thank you for you hint with RDTSC; but I do have some questions about
it:

Quote
> In my case, I was able to change the frequency of the timer in a dos
> box, but only for small frequencies (<1000Hz). As far as I know, you
> can't use the windows timer (or any other windows function) in a
> DOS program. I think the best thing you can do is to use the RDTSC
> instruction for timing, and at the start of your program use the
> standard timer to measure how many instructions/sec the computer
> is able to do.

What is this RDTSC? Is this the assembler command, that is increased
every instruction?

Can I use it in BP without ASM Code?
Which CPU does support this code?
Which Assembler?

Bye
 Arno Fehm

Re:Timer in DOS, running in a DOS-Box of Win95


Hi Peter!

Quote
You wrote:
> Windows (95/98) does allow you to change the tick-rate of the timer.
> It seems that, under Windows, the timer is running at a 1000 Hz by
> default. Windows uses this high rate to time-stamp events and
> messages. Windows does NOT allow you to specify a timer rate higher
> than a 1000 Hz... and I guess that all the lower rates are simply
> locked onto this 1000 Hz rate. I would therefore advise everyone who
> want to use an accurate timer in a DOS-box to set the timer to 1000
> Hz, ALL other frequency are either not allowed or inaccurate.

I cannot see that!! How are YOU using the timer? I use the following
code:

|| asm cli end;                                                        
|| izaehler := 1193180 DIV Freq;                                      
|| Port[$43] := $34;                                                  
|| Port[$40] := Lo(IZaehler);                                          
|| Port[$40] := Hi(IZaehler);                                          
|| asm sti end;                                                        

My Program reads the timer like that:
 time := MEML[ seg0040, $006C ];

When I set Freq := 1000, in DOS it works, but in a DOS-Box, it just
shows NO effect.

What code must I use?

Quote
> A complete other matter is the way you want to use the timer.

I just want use it for counting one counter!
I thought, I could use the one at 0040, 006C. Or must I use the IRQ
directly?

Thank you so far; I hope, you can help me to finally solve this problem!

Arno Fehm

Re:Timer in DOS, running in a DOS-Box of Win95


Jaap van Hengstum <s757...@kub.nl> wrote:

Quote
> Arno Fehm <af...@bigfoot.de> wrote:
>> My problem:
>> For a game, I want to use the timer (normally running at 18.2 /sec), and
>> make it faster. In DOS, this is no problem, but when running in a
>> DOS-Box of Win95, every try to change the frequenzy of the timer has no
>> effect. Windows does not allow this.
>> Is there a way to make the timer run faster in a DOS-Box, too? Or can I
>> use a timer supported by Windows itself (in a DOS-programm)??

Oops, I have been affording myself the luxuries of protected mode
programming too long and didn't realize that the RDTSC instruction
only works in pmode (which you are probably not using).

Mea culpa.

(if you do use pmode, let me know)

Quote
>> Arno Fehm
>> P.S. Sorry for my bad English...

Re:Timer in DOS, running in a DOS-Box of Win95


Quote
>You wrote:
>> Windows (95/98) does allow you to change the tick-rate of the timer.
>> It seems that, under Windows, the timer is running at a 1000 Hz by
>> default. Windows uses this high rate to time-stamp events and
>> messages. Windows does NOT allow you to specify a timer rate higher
>> than a 1000 Hz... and I guess that all the lower rates are simply
>> locked onto this 1000 Hz rate. I would therefore advise everyone who
>> want to use an accurate timer in a DOS-box to set the timer to 1000
>> Hz, ALL other frequency are either not allowed or inaccurate.
>I cannot see that!! How are YOU using the timer? I use the following
>code:
>|| asm cli end;
>|| izaehler := 1193180 DIV Freq;
>|| Port[$43] := $34;
>|| Port[$40] := Lo(IZaehler);
>|| Port[$40] := Hi(IZaehler);
>|| asm sti end;
>My Program reads the timer like that:
> time := MEML[ seg0040, $006C ];
>When I set Freq := 1000, in DOS it works, but in a DOS-Box, it just
>shows NO effect.
>What code must I use?

The fact is, that Windows allows to change the frequency, but this
change doesn't affect ALL interrupts etc. You may have noticed:
the clock works at the same speed. No so in Dos mode. Anyhow,
changing the timer frequency is not a very fine way to do things.
Most of the programmers forget e.g. to restore the Dos-clock!
Try this routine:
function getclock : longint;assembler;
{ this routine returns a clock with occassional spikes where time
  will look like its running backwards 1/18.2th of a second. The
  resolution of the clock is 1/(18.2*256) = 1/4659 second. 66 ticks
  of this clock are supposed to be equal to a monitor 1/70 second tick. }
asm
  mov   es,[seg0040]
  mov   dx,es:[6Ch]
  mov   cx,es:[6Eh]
  mov   ah,dl
  mov   dl,dh
  mov   dh,cl
  mov   al,0             { read lo Byte straight from timer chip  }
  out   $0043,al         { latch count                            }
  mov   al,1
  out   $0043,al         { set up to read count                   }
  in    al,$0040         { read in lo Byte (and discard)          }
  in    al,$0040         { hi Byte into al                        }
  neg   al               { make it so counting up instead of down }
end;

Bye,
Stefan
---
please remove the P in my email-adress to answer me
take a look @ my homepage: http://sourcenet.home.pages.de/

Re:Timer in DOS, running in a DOS-Box of Win95


Hi Jaap!

Quote
> Oops, I have been affording myself the luxuries of protected mode
> programming too long and didn't realize that the RDTSC instruction
> only works in pmode (which you are probably not using).
> (if you do use pmode, let me know)

Oh! I AM using Protected Mode. You cannot develope any bigger games with
600KB RAM!
But anyway, even if not, maybe other readers of this newsgroup might
want to know about RDTSC.

Please let us know about how to use it!

Arno Fehm

Re:Timer in DOS, running in a DOS-Box of Win95


Hi Stefan!

Quote
Stefan Goehler wrote:
> Try this routine:
> function getclock : longint;assembler;
> { this routine returns a clock with occassional spikes where time
>   will look like its running backwards 1/18.2th of a second. The
>   resolution of the clock is 1/(18.2*256) = 1/4659 second. 66 ticks
>   of this clock are supposed to be equal to a monitor 1/70 second tick. }
> asm
>   mov   es,[seg0040]
>   mov   dx,es:[6Ch]
>   mov   cx,es:[6Eh]
>   mov   ah,dl
>   mov   dl,dh
>   mov   dh,cl
>   mov   al,0             { read lo Byte straight from timer chip  }
>   out   $0043,al         { latch count                            }
>   mov   al,1
>   out   $0043,al         { set up to read count                   }
>   in    al,$0040         { read in lo Byte (and discard)          }
>   in    al,$0040         { hi Byte into al                        }
>   neg   al               { make it so counting up instead of down }
> end;

Hmm... really works!!! Thank you!
But is there a way to have another frequecy or do I have to divide this
1/4659 1/sec to get other resolutions?
But thanks anyway!

Arno Fehm

Re:Timer in DOS, running in a DOS-Box of Win95


Arno Fehm schrieb:

Quote
> Oh! I AM using Protected Mode. You cannot develope any bigger games with
> 600KB RAM!

600 kb? Wow, I wish I had so much. :-)

Quote
> But anyway, even if not, maybe other readers of this newsgroup might
> want to know about RDTSC.

I tried the following program, it worked (and of course, you own a
Pentium?):

{$IFNDEF DPMI}
  Protected Mode required
{$ENDIF}

var
  tsc : comp;

procedure rdtsc(var c: comp); assembler;
asm
  dw    310Fh
  les   di, c
  db    66h
  stosw
  db    66h
  mov   ax, dx
  db    66h
  stosw
end;

begin
  rdtsc(tsc);
  writeln('TSC = ', tsc:1:0)
end.

Re:Timer in DOS, running in a DOS-Box of Win95


Quote
Frdric wrote:
> > But anyway, even if not, maybe other readers of this newsgroup might
> > want to know about RDTSC.

> I tried the following program, it worked (and of course, you own a
> Pentium?):

Didn't you claim the RDTSC instruction to crash your computer last november?
;-)

=====================================================

I would like to use the RDTSC instruction (opcode 0Fh, 31h) in a Pascal
program to measure how fast a certain program part is. Unfortunately,
the program hangs when executing it.

=====================================================

        Tschau,
            Ingo

Re:Timer in DOS, running in a DOS-Box of Win95


In article <36AB019A.9F0F4...@rz-online.de>,
  "Frdric" <f...@rz-online.de> wrote:

Quote
> Arno Fehm schrieb:

> > Oh! I AM using Protected Mode. You cannot develope any bigger games with
> > 600KB RAM!

> 600 kb? Wow, I wish I had so much. :-)

> > But anyway, even if not, maybe other readers of this newsgroup might
> > want to know about RDTSC.

> I tried the following program, it worked (and of course, you own a
> Pentium?):

> {$IFNDEF DPMI}
>   Protected Mode required
> {$ENDIF}

> var
>   tsc : comp;

> procedure rdtsc(var c: comp); assembler;
> asm
>   dw    310Fh
>   les   di, c
>   db    66h
>   stosw
>   db    66h
>   mov   ax, dx
>   db    66h
>   stosw
> end;

> begin
>   rdtsc(tsc);
>   writeln('TSC = ', tsc:1:0)
> end.

Just a suggestion, if you try to single step the above in TD, it will hang, as
TD cannot handle the stand-alone db 66h's.

Either code it as

..

db 66h; stosw
db 66h; mov   ax, dx
db 66h; stosw

or define (as I do) some constants

const
  pushad       = $6066;
  popad        = $6166;
  segfs        = $64;
  seggs        = $65;
  fs386        = $6664;
  gs386        = $6665;
  cwde         = $9866;
  cdq          = $9966;
  movsd        = $a566;
  cmpsd        = $a766;
  stosd        = $ab66;
  lodsd        = $ad66;
  scasd        = $af66;
  ax_2_fs      = $e08e;
  ax_2_gs      = $e88e;
  rdtsc        = $310f

and code your 386 instructions as

dw    rdtsc
dw    stosd

which might be a little more readable.

Robert
--
Robert AH Prins
prin...@williscorroon.com

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    

Re:Timer in DOS, running in a DOS-Box of Win95


Ingo van Lil schrieb:

Quote
> Didn't you claim the RDTSC instruction to crash your computer last november?
> ;-)

Yes. Because I was not in protected mode. In TPW, it worked - even last
November. ;-)

Re:Timer in DOS, running in a DOS-Box of Win95


Robert AH Prins schrieb:

Quote
> Just a suggestion, if you try to single step the above in TD, it will hang, as
> TD cannot handle the stand-alone db 66h's.

Actually, TD already crashed when encountering RDTSC.

Re:Timer in DOS, running in a DOS-Box of Win95


Quote
> I tried the following program, it worked (and of course, you own a
> Pentium?):

Thank you a lot!
Just two questions remaining:
1. What CPUs do support rdtsc? What about AMD K6 or K6-2 or the WinChip?
2. I've got a 200MHz CPU, but in 1 sec, the tsc increases only about
195.000.000. What's the reason? Might this cause changes in the
frequenzy of my timer?

 Arno Fehm

Go to page: [1] [2]

Other Threads