Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uunet!zephyr.ens.tek.com!tektronix!sequent!norsk From: norsk@sequent.UUCP (Doug Thompson) Newsgroups: comp.sys.ibm.pc Subject: .84 usec Timer resolution on PC Message-ID: <25235@sequent.UUCP> Date: 21 Nov 89 16:53:44 GMT Reply-To: norsk@sequent.UUCP (Doug Thompson) Distribution: usa Organization: Sequent Computer Systems, Inc. Lines: 65 PC Timing Resolution of .84 microseconds are possible on a PC machine. In the April 1984 issue of the late PC TECH JOURNAL, there was an article titled "Life in the Fast Lane", by Bob Smith and Tom Puckett, that discribe the mechanism for obtaining very good timing resolution from the PC 8253-5 Programmable Interval Timer (PIT). The clock interrupt is generated by Timer 0 of the PIT. The input to the timer is 1.193182 Mhz with a period of 0.838095 microseconds or .84 us. By altering the programming mode of Timer 0 of the PIT from Mode 3 (which is a square wave output where the count down counter decrements by 2 during the first half of the cycle, then again during the last half) to Mode 2 (which has the output normally high, but when the count down reaches 0, the output goes low for 1 timer-clock cycle then goes high again.) In this mode, a query via a Timer command of the current count down register value is possible. The command causes the PIT to latch the current value and make it available for query. Then this value is the count of timer-clock cycles that have occurred since the last Timer 0 interrupt. Coupled with the interrupt count in the BIOS data area, one can measure very in fine timing the length of short events. To program Timer 0 to Mode 2 do the following: mov al, 34h ; Timer 0 - Mode 2 out 43h, al xor al, al ; Get ZERO to give full count of 65536 jmp $+2 out 40h, al ; load low order byte jmp $+2 out 40h, al ; hi order byte To get a current timer value dot he following: ... mov ax, 40h ; BIOS data segment mov es, ax mov al, 00h ; Latch Timer 0 counter cmd cli ; critical section hold off ints out 43h, al ; issue latch cmd mov bx, es:[6ch] ; get BIOS timer low word mov cx, es:[6eh] ; get BIOS timer hi word in al, 40h ; fetch latched counter mov ah, al jmp $+1 in al, 40h ; fetch hi order latched counter sti xchg ah, al ; get in proper byte order neg ax ; get up count from down count xchg ax, cx ; return in common register order AX has MSWord of timer interrupt count BX has LSWord of timer interrupt count CX has count of timer-clock cycles since last timer interrupt (.84us) -- Douglas Thompson UUCP: ..{tektronix,ogcvax,uunet}!sequent!norsk Sequent Computer Systems Phone: (503) 526-5727 15450 SW Koll Parkway !"The scientist builds to learn;the engineer learns in Beaverton OR 97006 !order to build." Fred Brooks