Timer Question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Timer Question

Post 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.
Only Human
Tim

Re:Timer Question

Post 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.
Curufir

Re:Timer Question

Post 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 :).
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Timer Question

Post 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?
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Timer Question

Post 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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Timer Question

Post 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?
Only Human
Tim

Re:Timer Question

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Timer Question

Post 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.
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Timer Question

Post 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.
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Timer Question

Post 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
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Timer Question

Post by Neo »

Yeah i saw this in another tutorial too. but what is the difference?
Only Human
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Timer Question

Post 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 ...
Post Reply