Page 1 of 1

How to read the 16-bit counter #2 of PIT8253?

Posted: Sat Oct 03, 2020 10:46 am
by hlide
If I program the counter #2 as a byte counter, it works fine when I read $E006:

Code: Select all

    LD      HL,$E006
    LD      C,(HL)
If I program the counter #2 as a 16-bit word counter, it doesn't work when I try to read the lower byte first then the upper byte.

Code: Select all

    LD      HL,$E006
    LD      C,(HL)
    LD      B,(HL)
A note mentions:
The 8253 provides a special READ operation (read on the fly) that transfers the contents of the counter into a temporary storage and lets the CPU to read the data from the register. The timer continues to count down. To latch the current counter value a special mode control word is issued to the timer. The control word for latch {...} uses only from MSB of the control word rest we don’t cares. Bits 7&6 (SC1&SC0) select the counter to be read and bits 3 and 4 (RL1&RL0) are set to 00 to indicate a latching instruction. After the count has been latched, either or both of the bytes can be read.
Indeed, the MONITOR code shows such assembly lines, so I change to:

Code: Select all

    LD      HL,$E007 // $E007: latch counter #2
    LD      (HL),$80
    DEC     HL       // $E006: read counter #2
    LD      C,(HL)
    LD      B,(HL)
But even so, it doesn't seem to be correct (while better than I got beforehand)

The main reason I want it so badly, is because I want to use counter #2 to count BLANK numbers as an equivalent way to determine how much CPU percent is used since we cannot change the border color as usually done on other computers.

Unless someone would be able to tell me the solution, I think I keep a 8-bit counter #2 in two-BLNK unit. Less precise but still enough to get a CPU usage percent.