I've been doing a lot of testing with the PIT lately to get the most accuracy I can get off the chip. However what bothers me and confuses me is after reading PIT counter, that shouldn't be interrupted in any way and continously counting gives me overall being fast to its frequency. I use counter 2, which I believe nothing but the system speaker is hardwired to it, but it has nevered been enabled.
My code to read the counter looks like this:
Code: Select all
;==================================================================
CWR equ BYTE ; Control Word Register
;==================================================================
CWR_BCD equ 00000001b ; binary coded decimal counter
CWR_MODE0 equ 00000000b ; interrupt on terminal count
CWR_MODE1 equ 00000010b ; hardware retriggable one-shot
CWR_MODE2 equ 00000100b ; square wave
CWR_MODE3 equ 00000110b ; rate generator
CWR_MODE4 equ 00001000b ; software triggered strobe
CWR_MODE5 equ 00001010b ; hardware triggered strobe (retriggerable)
CWR_RWLOBYTE equ 00010000b ; read/write least signficant byte only
CWR_RWHIBYTE equ 00100000b ; read/write most signficant byte only
CWR_RWLOHIGH equ 00110000b ; read/write least signficant then most signficant
CWR_COUNTER0 equ 00000000b ; select counter 0
CWR_COUNTER1 equ 01000000b ; select counter 1
CWR_COUNTER2 equ 10000000b ; select counter 2
CWR_LATCH equ 00000000b ; latches counter
;-----------------------------------------------------
mov ebx, ds:[swSystemPeCounter]
mov al, CWR_COUNTER2 OR CWR_LATCH
out 43h, al
in al, 42h ; counter-2 read LSB
; sub bl, al
mov cl, al
in al, 42h ; counter-2 read MSB
; sbb bh, al
mov ch, al
xor edx, edx
mov ds:[swSystemPeCounter], ecx
sub bx, cx ; this algorithm subtracts a clock each wrap
sbb bx, dx ; around, which means OUT was high. This has no
; relevant strings with OUT, however it attempts to
; keep the correct clocks as it seems to be fast.
; ( note: I'm totally confused why clocks are fast
; and not slow)
add ds:[swSystemLoCounter], ebx ; update system counter
adc ds:[swSystemHiCounter], edx
But the only question is am I latching the counter register correctly? Which is the only thing I can come up with that is making this a bit fast. On the other hand this may be RTC, as its roughly 100ms ahead every 6 hrs comparing it with RTC without the wrap around decrement code (with the wrap around code it is still fast).
In the 8254 datasheet, it shows in the driagram that it shouldn't loose counts when OUT is high, but only when GATE is high, but GATE is never touched, and I'm not even looking for a reason for lost clocks, but why is clocks ahead.
Does anyone know why this is?, could this be RTC is not accurate, or could this be my latch is uncorrectly implemented? Or should I raise my white flag?