delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/01/28/15:38:56

From: cnc AT netcom8 DOT netcom DOT com (Christopher Christensen)
Date: Fri, 28 Jan 1994 12:14:05 PST
To: Thomas Sams <SAMS AT nbivax DOT nbi DOT dk>
Subject: Re: How to measure time better than 1/10000 sec?
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu

>Is it possible to measure time to better than, say, 1/10000 sec ?
>Does one have to turn off the 18.2 Hz interrupts from the machine 
>to do so?
>How?
>
>Thanks in advance
>    Thomas Sams.

You can read the timer register directly.  The following code should work
assuming that the timer hasn't been reprogrammed to interrupt at more than
18.2 Hz.  The timer runs at 1193180 Hz (65536 times faster than the 18.2
Hz timer tick).  If you have problems with this code, let me know and I will
try to fix it.

------------------------------------------------------------
/* short timer16() -- return 16-bit timer value */
/* timer resolution is 0.8381 microseconds (1193180 Hz clock) */
/* rolls over 18.2 times per second */
	.align 2
.globl  _timer16
_timer16:
	cli
	movb  $0xc2,%al       # read back cmd (status+count)
	outb  %al,$0x43
	.byte 0xeb,0          # jmp delay
	inb   $0x40,%al       # get status (msb=out pin)
	movb  %al,%cl
	.byte 0xeb,0          # jmp delay
	inb   $0x40,%al       # get LSB
	movb  %al,%ah
	.byte 0xeb,0          # jmp delay
	inb   $0x40,%al       # get MSB
	sti
	xchgb %ah,%al
	orw   %ax,%ax
	jz     _timer16       # zero is problematic
	shlb  $1,%cl          # carry=out status
	rcrw  $1,%ax
	negw  %ax
	ret


/* unsigned int timer32() -- return 32-bit timer value */
/* returns a timer with 0.8381 microsecond resolution that rolls over
   about once an hour */
	.align 2
.globl  _timer32
_timer32:
	call  __go32_conventional_mem_selector
	movw  %ax,%gs
	xorl  %eax,%eax
	movl  %gs:0x046c,%edx   # read timer tick count in dos memory
	call  _timer16
	cmpl  %gs:0x046c,%edx   # has timer advanced?
	je    Ldone4

	testb $0x80,%ah   # if msbit==0 inc %edx
	jnz   Ldone4
	incl  %edx
Ldone4:
	shll  $16,%edx
	orl   %edx,%eax
	ret
------------------------------------------------------------


-- 
----------------------------------------------------------------------
:  Christopher  :  Huntington Beach California, USA                  :
:  Christensen  :  email:  cnc AT netcom DOT com                            :
----------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019