Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to the forum
Board view  Mix view

modern timers in DOS (32bit) (Developers)

posted by Japheth Homepage, Germany (South), 06.05.2022, 08:06

Absolutely true! :-D

> If you only want to measure time in a very fine-grained way, use the TSC.
> Every Intel CPU since at least Pentium had a time stamp counter and most
> other brands introduced one not much later. The time stamps typically have
> a very high resolution, for example CPU clock cycles, and processors may
> even compensate the counts for you when changing clock speeds in power
> saving modes.

If you need a resolution of 0.1 to 1 ms, just use the normal, unmodified int 8 timer and read the values of the timer at the events you are interested in. Here's my standard code:


;--- this returns timer value in ms

GetTimerValue proc uses es bx

    mov es, [_0000H]
tryagain:
    mov edx,es:[46ch]
    mov al,0C2h     ;read timer 0 status + value low/high
    out 43h, al
    xchg edx, edx
    in al,40h
    mov cl,al       ;CL = status
    xchg edx, edx
    in al,40h
    mov ah, al      ;AH = value low
    xchg edx, edx
    in al,40h       ;AL = value high

    test cl,40h     ;was latch valid?
    jnz tryagain
    cmp edx,es:[46ch]   ;did an interrupt occur in the meantime?
    jnz tryagain        ;then do it again!

    xchg al,ah
;--- usually (counter mode 3) the timer is set to count down *twice*!
;--- however, sometimes counter mode 2 is set!
    mov ch,cl
    and ch,0110B    ;bit 1+2 relevant
    cmp ch,0110B    ;counter mode 3?
    jnz @F
;--- in mode 3, PIN status of OUT0 will become bit 15
    shr ax,1
    and cl,80h
    or ah, cl
@@:
;--- now the counter is in AX (counts from FFFF to 0000)
    neg ax
;--- now the count is from 0 to FFFF
    ret
GetTimerValue endp

;--- get timer value in ms in eax

gettimer proc

    call GetTimerValue

;--- the timer ticks are in EDX:AX, timer counts down
;--- a 16bit value with 1,193,180 Hz -> 1193180/65536 = 18.20648 Hz
;--- which are 54.83 ms
;--- to convert in ms:
;--- 1. subticks in ms: AX / 1193
;--- 2. ticks in ms: EDX * 55
;--- 3. total 1+2

    push edx
    movzx eax,ax    ;step 1
    cdq
    mov ecx, 1193
    div ecx
    mov ecx, eax
    pop eax         ;step 2
    mov edx, 55
    mul edx
    add eax, ecx    ;step 3
    ret
gettimer endp

---
MS-DOS forever!

 

Complete thread:

Back to the forum
Board view  Mix view
22049 Postings in 2034 Threads, 396 registered users, 181 users online (0 registered, 181 guests)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum