2018-10-16 09:01:38 +00:00
|
|
|
#include "i8253.h"
|
|
|
|
#include "i386.h"
|
|
|
|
#include "IO.h"
|
|
|
|
#include "PIC.h"
|
2018-11-07 21:15:02 +00:00
|
|
|
#include "Scheduler.h"
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
#define IRQ_TIMER 0
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
extern "C" void timer_interrupt_entry();
|
|
|
|
extern "C" void timer_interrupt_handler(RegisterDump&);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
asm(
|
2019-01-31 16:31:23 +00:00
|
|
|
".globl timer_interrupt_entry \n"
|
|
|
|
"timer_interrupt_entry: \n"
|
2018-10-16 09:01:38 +00:00
|
|
|
" pusha\n"
|
|
|
|
" pushw %ds\n"
|
|
|
|
" pushw %es\n"
|
|
|
|
" pushw %fs\n"
|
|
|
|
" pushw %gs\n"
|
|
|
|
" pushw %ss\n"
|
|
|
|
" pushw %ss\n"
|
|
|
|
" pushw %ss\n"
|
|
|
|
" pushw %ss\n"
|
2018-10-23 08:12:50 +00:00
|
|
|
" pushw %ss\n"
|
2018-10-16 09:01:38 +00:00
|
|
|
" popw %ds\n"
|
|
|
|
" popw %es\n"
|
|
|
|
" popw %fs\n"
|
|
|
|
" popw %gs\n"
|
2018-11-07 23:24:59 +00:00
|
|
|
" mov %esp, %eax\n"
|
2019-01-31 16:31:23 +00:00
|
|
|
" call timer_interrupt_handler\n"
|
2018-10-16 09:01:38 +00:00
|
|
|
" popw %gs\n"
|
2018-10-23 08:12:50 +00:00
|
|
|
" popw %gs\n"
|
2018-10-16 09:01:38 +00:00
|
|
|
" popw %fs\n"
|
|
|
|
" popw %es\n"
|
|
|
|
" popw %ds\n"
|
|
|
|
" popa\n"
|
|
|
|
" iret\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Timer related ports */
|
|
|
|
#define TIMER0_CTL 0x40
|
|
|
|
#define TIMER1_CTL 0x41
|
|
|
|
#define TIMER2_CTL 0x42
|
|
|
|
#define PIT_CTL 0x43
|
|
|
|
|
|
|
|
/* Building blocks for PIT_CTL */
|
|
|
|
#define TIMER0_SELECT 0x00
|
|
|
|
#define TIMER1_SELECT 0x40
|
|
|
|
#define TIMER2_SELECT 0x80
|
|
|
|
|
|
|
|
#define MODE_COUNTDOWN 0x00
|
|
|
|
#define MODE_ONESHOT 0x02
|
|
|
|
#define MODE_RATE 0x04
|
|
|
|
#define MODE_SQUARE_WAVE 0x06
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
#define WRITE_WORD 0x30
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
#define BASE_FREQUENCY 1193182
|
|
|
|
|
2019-03-25 01:06:57 +00:00
|
|
|
static dword s_ticks_this_second;
|
|
|
|
static dword s_seconds_since_boot;
|
2019-02-01 02:50:06 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
void timer_interrupt_handler(RegisterDump& regs)
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
|
|
|
IRQHandlerScope scope(IRQ_TIMER);
|
2019-03-25 01:06:57 +00:00
|
|
|
if (++s_ticks_this_second >= TICKS_PER_SECOND) {
|
|
|
|
// FIXME: Synchronize with the RTC somehow to prevent drifting apart.
|
|
|
|
++s_seconds_since_boot;
|
|
|
|
s_ticks_this_second = 0;
|
|
|
|
}
|
2018-11-07 23:24:59 +00:00
|
|
|
Scheduler::timer_tick(regs);
|
2018-10-16 09:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace PIT {
|
|
|
|
|
2019-03-25 01:06:57 +00:00
|
|
|
dword ticks_this_second()
|
2019-02-01 02:50:06 +00:00
|
|
|
{
|
2019-03-25 01:06:57 +00:00
|
|
|
return s_ticks_this_second;
|
2019-02-01 02:50:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 01:06:57 +00:00
|
|
|
dword seconds_since_boot()
|
2018-10-16 09:01:38 +00:00
|
|
|
{
|
2019-03-25 01:06:57 +00:00
|
|
|
return s_seconds_since_boot;
|
|
|
|
}
|
2019-02-01 02:50:06 +00:00
|
|
|
|
2019-03-25 01:06:57 +00:00
|
|
|
void initialize()
|
|
|
|
{
|
2018-11-16 23:11:08 +00:00
|
|
|
word timer_reload;
|
2018-10-16 09:01:38 +00:00
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_SQUARE_WAVE);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
timer_reload = (BASE_FREQUENCY / TICKS_PER_SECOND);
|
|
|
|
|
2019-02-06 10:32:23 +00:00
|
|
|
kprintf("PIT: %u Hz, square wave (%x)\n", TICKS_PER_SECOND, timer_reload);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
IO::out8(TIMER0_CTL, LSB(timer_reload));
|
|
|
|
IO::out8(TIMER0_CTL, MSB(timer_reload));
|
|
|
|
|
2019-01-31 16:31:23 +00:00
|
|
|
register_interrupt_handler(IRQ_VECTOR_BASE + IRQ_TIMER, timer_interrupt_entry);
|
2018-10-16 09:01:38 +00:00
|
|
|
|
|
|
|
PIC::enable(IRQ_TIMER);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|