Page 1 of 1

Timer Question

Posted: Mon Oct 27, 2003 11:51 am
by Neo
I have just reached the timer handler stage and wanted to know what the difference is between using the 8254/8253 timer or the APIC.
Which one is better? Also the timer expects all values as Hz i just wanted to know what the relation is between Hz and seconds? Also if anyone has any useful links to timer stuff please let me know.
Is the timer dependent on the CPU speed? any explanations?
Thanx in advance.

Re:Timer Question

Posted: Mon Oct 27, 2003 2:46 pm
by Tim
Neo wrote:I have just reached the timer handler stage and wanted to know what the difference is between using the 8254/8253 timer or the APIC.
Which one is better?
The 8254/8253 timer is supported everywhere. The APIC timer is much higher resolution, and a bit more capable, but is only available on multiprocessor-compatible CPUs; I think this includes Pentium and above. You can test for the APIC through the CPUID instruction.
Also the timer expects all values as Hz i just wanted to know what the relation is between Hz and seconds?
1 Hz = 1 cycle per second. Seconds is a unit of time, Hz is a unit of frequency (technically it's time^-1).
Is the timer dependent on the CPU speed?
No.
any explanations?
The timer is not dependent on the CPU speed.

Re:Timer Question

Posted: Mon Oct 27, 2003 4:54 pm
by Curufir
any explanations?
Bored so I'll try.

A crystal on the PC's motherboard generates timing pulses at around 315/22 Mhz (This number is a hangover from ye olden times because 315/88 corresponds to the frequency of an NTSC colour TV).

The PIT generates ticks at a third of the rate of the PC timing pulse, around 315/264 Mhz.

The PIT decrements an internal counter on every tick. So the counter generates interrupts at around 315/(264*Max counter value) Mhz.

If the PIT is set to maximum delay (Max counter value = 65535) then it generates interrupts at a rate of around 18.2 Hz (Ie 18.2 interrupts are generated every second, 1 interrupt every 54.95ms ).

As you can see the timing depends on what value the counter is decrementing from and the (Fixed and standard) pulse generated by the crystal.

Hope that helps :).

Re:Timer Question

Posted: Mon Oct 27, 2003 8:49 pm
by Neo
ok so how do i get the timer IRQ to fire every 100 or 1000 ms and anyway what interval would be good to get it to fire?

Re:Timer Question

Posted: Tue Oct 28, 2003 2:50 am
by Pype.Clicker
just change the "divisor count" so that it gives you the requested frequency :
The 8253 internally decrease a counter at every crystal oscilliation. 65336 oscilliations give you 18.2Hz, thus only 16384 oscilliations will give you 72.8 Hz, etc.

You cannot get a frequency of 10Hz by just programming the PIT, but you could program it to work at 100Hz (10ms) and only run the interrupt handler every 10th IRQ (using a software counter), or running it at 20Hz (50ms iirc) and run the handler on even times.

I personnally chose the IRQ timer to be fired at 1KHz (every ms), but this needs an auto-adaptative trick to detect if i'm on a virtual PC (that usually can't sustain that rate) or not.

Re:Timer Question

Posted: Tue Oct 28, 2003 11:42 am
by Neo
is there any optimum value for this firing? btw will the IRQ fire for each timer counter i program or for only one. i.e. if i program counter0 in mode2 and counter1 in mode3 then will they both fire the IRQ if so how do i determine which one did it?

Re:Timer Question

Posted: Tue Oct 28, 2003 12:02 pm
by Tim
It depends on lots of things: mainly, what you're doing in response to timer interrupts. I'd advise setting it to 100Hz for now and put in the ability to change it in the future if that's not enough.

If you have it too high, your task switching/interrupt handling code runs too often and your threads never get anything useful done. If you have it too low, you might not have enough resolution for measuring periods of time.

Re:Timer Question

Posted: Wed Oct 29, 2003 2:44 am
by Pype.Clicker
note that, once again, it's not mandatory that you switch tasks at *every* clock interrupt. The frequency of the clock will define the granularity you work with, but not necessarily the frequency itself. You could decide to decrement a 'time left' counter at every tick and switch tasks only when that counter comes to 0 or when a more prioritized task comes.

what's the difference with a single quota*granularity clock ? each thread may have a different quota (i.e. you can give longer timeslice to more important jobs), which allows you to weight the CPU usage on a per-thread (or per-process) basis.

Re:Timer Question

Posted: Thu Oct 30, 2003 11:15 am
by Neo
I got the 8254 to generate a IRQ every 100ms but the problem is that it does not work in BOCHS, only when i test it on a PC does it work. What could be the reason for this? My source for initing the Timer is included below.
At every 100th IRQ i just display the cnt value but BOCHS displays only the other messages not the time.

Code: Select all

_InitTimer:
   push   eax
   mov   al,0x3c
   out   0x43,al
   mov   al,0x98
   out   TIMER_CNTR_0,al
   mov   al,0x2E
   out   TIMER_CNTR_0,al
   pop   eax
   ret
or is it something to do with the order of enabling the IRQ my present order is
1) disable IRQ's
2) remap the PIC
3) Init Timer
4) setup IDT
5) enable IRQ's

Also when i don't use the InitTimer routine and just enable the IRO 0 i get the time displayed.

Re:Timer Question

Posted: Thu Oct 30, 2003 11:29 am
by Pype.Clicker
according to my own code, the '0x36' value should be sent to the control port, instead of 0x3C ... 0x36 is (according to http://www.nondot.org/sabre/os/files/MiscHW/PIT.txt), counter 0, LSB first, mode 3 (square wave), binary

Re:Timer Question

Posted: Thu Oct 30, 2003 11:33 am
by Neo
Yeah i saw this in another tutorial too. but what is the difference?

Re:Timer Question

Posted: Fri Oct 31, 2003 4:02 am
by Pype.Clicker
Neo wrote: Yeah i saw this in another tutorial too. but what is the difference?
0x3C asks for mode #6, 0x36 asks for mode #3. There's a mode 3 and nothing like mode #6. So if you program the PIT with 0x3C, nothing will occur at all.

If you can post a link to the tutorial that suggests using 0x3C, there are chance it is a typo in it ...