APIC Timer: Difference between revisions

m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(3 intermediate revisions by 2 users not shown)
Line 61:
=== Prerequisites ===
Before we start, let's define some constant and functions.
<sourcesyntaxhighlight lang="asm">
apic = the linear address where you have mapped the APIC registers
 
Line 99:
writegate: ...
ret
</syntaxhighlight>
</source>
I will also assume that you have a working [[IDT]], and you have a function to write a gate for a specific interrupt: writegate(intnumber,israddress).
Furthermore, to make things simple, I'll assume that you did not change the default interrupt mapping found in almost every tutorial:
Line 110:
=== Example code in ASM ===
Here's a possible way to initialize APIC timer in fasm syntax assembly:
<sourcesyntaxhighlight lang="asm">
;you should read MSR, get APIC base and map to "apic"
;you should have used lidt properly
Line 214:
;although I have found buggy hardware that required it
mov dword [apic+APIC_TMRDIV], 03h
</syntaxhighlight>
</source>
 
=== Example code in C ===
This code is an example of how to initialize the APIC timer so that it ticks every 10 milliseconds. This is done by letting the APIC timer run, waiting for 10ms using the PIT and then getting the number of ticks that were done from the APIC timer. It assumes that you have functions to "write"/"read" the APIC's registers and "pit_prepare_sleep"/"pit_perform_sleep" to perform an as accurate as possible measurement of the timer's frequency.
<sourcesyntaxhighlight lang="c">
void apic_start_timer() {
// Tell APIC timer to use divider 16
Line 243:
write(APIC_REGISTER_TIMER_INITCNT, ticksIn10ms);
}
</syntaxhighlight>
</source>
 
== See also ==
Line 256:
 
[[Category:Interrupts]]
[[Category:TimeTimers]]