summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHanjie Wu <hanjiew@andrew.cmu.edu>2023-04-24 04:13:40 -0400
committerHanjie Wu <hanjiew@andrew.cmu.edu>2023-04-24 04:13:40 -0400
commit09cce089128de5f265ee6d4e68e196f85537ccc7 (patch)
treec639113144ce8395205b6813d43c5cd19c8afcfb
parent6b13d5d9b1ff1cb38bb50e918e51a241a225f8da (diff)
timer
-rw-r--r--kern/timer.c22
-rw-r--r--kern/timer_asm.S4
2 files changed, 17 insertions, 9 deletions
diff --git a/kern/timer.c b/kern/timer.c
index 53fd576..0d623ae 100644
--- a/kern/timer.c
+++ b/kern/timer.c
@@ -33,14 +33,14 @@ static uint32_t lapic_dt;
/* 2ms */
#define TIMER_FREQ 500
-int timer_test_status;
+int timer_count;
void timer_test_handler();
void timer_init() {
/* use 10x slower frequency for APIC timer testing */
int counter = TIMER_RATE / (TIMER_FREQ / 10);
- outb(TIMER_MODE_IO_PORT, TIMER_SQUARE_WAVE);
+ outb(TIMER_MODE_IO_PORT, TIMER_ONE_SHOT);
outb(TIMER_PERIOD_IO_PORT, counter & 0xFF);
outb(TIMER_PERIOD_IO_PORT, (counter >> 8) & 0xFF);
if (heap_init(&timers) != 0) {
@@ -55,11 +55,20 @@ void timer_init() {
lapic_write(LAPIC_TIMER_DIV, LAPIC_X1);
lapic_write(LAPIC_TIMER_INIT, 0xffffffff);
- timer_test_status = 10;
- enable_interrupts();
- while (timer_test_status != 0) {
+ timer_count = 10;
+ while (1) {
+ int old_timer_count = timer_count;
+ outb(TIMER_MODE_IO_PORT, TIMER_ONE_SHOT);
+ outb(TIMER_PERIOD_IO_PORT, counter & 0xFF);
+ outb(TIMER_PERIOD_IO_PORT, (counter >> 8) & 0xFF);
+ enable_interrupts();
+ while (timer_count == old_timer_count) {
+ }
+ disable_interrupts();
+ if (timer_count == 0) {
+ break;
+ }
}
- disable_interrupts();
lapic_dt = (0xffffffff - lapic_read(LAPIC_TIMER_CUR)) / 100;
lapic_write(LAPIC_TIMER_INIT, 0);
@@ -70,7 +79,6 @@ void setup_lapic_timer() {
lapic_write(LAPIC_LVT_TIMER, (LAPIC_PERIODIC | TIMER_IDT_ENTRY));
lapic_write(LAPIC_TIMER_DIV, LAPIC_X1);
lapic_write(LAPIC_TIMER_INIT, lapic_dt);
- outb(TIMER_MODE_IO_PORT, TIMER_ONE_SHOT);
}
/**
diff --git a/kern/timer_asm.S b/kern/timer_asm.S
index cf4c76f..f70c606 100644
--- a/kern/timer_asm.S
+++ b/kern/timer_asm.S
@@ -8,9 +8,9 @@
.type timer_test_handler, %function
timer_test_handler:
push %eax
- mov timer_test_status, %eax
+ mov timer_count, %eax
dec %eax
- mov %eax, timer_test_status
+ mov %eax, timer_count
mov $INT_ACK_CURRENT, %al
mov $INT_CTL_PORT, %dx
outb %al, %dx