From 8df5a5628bfc470f5175ca180501954b2870cd27 Mon Sep 17 00:00:00 2001 From: Uiop Pilon Date: Tue, 17 Nov 2020 19:51:08 +0000 Subject: [PATCH 1/2] Added new Uptime Function --- include/pic/timer.h | 2 ++ src/pic/timer.c | 46 +++++++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/include/pic/timer.h b/include/pic/timer.h index be1cc54..7f27b6f 100644 --- a/include/pic/timer.h +++ b/include/pic/timer.h @@ -34,4 +34,6 @@ union { void init_timer(); +double get_uptime(); + #endif diff --git a/src/pic/timer.c b/src/pic/timer.c index d8e698c..c236736 100644 --- a/src/pic/timer.c +++ b/src/pic/timer.c @@ -1,30 +1,40 @@ - #include #include #include -int i = 0; -int j = 0; -void timer(struct interrupt_frame *frame) { - return; +unsigned long ticks; // The amount of ticks that have passed +int hz; // Self-Explanitory + +// Timer callback +void timer(struct interrupt_frame * frame) { + ticks++; + return; +} + +// Returns how many seconds the computer wa +double get_uptime() { + return ticks * (1 / hz); } -void set_timer_phase(int hz) { - PIT_8254 cmd; - cmd.BCD = BCD_16_BIT; - cmd.mode = MODE_SQR_WAVE; - cmd.RW = RW_READ | RW_WRITE; - cmd.CNTR = 0; +void set_timer_phase() { + PIT_8254 cmd; + cmd.BCD = BCD_16_BIT; + cmd.mode = MODE_SQR_WAVE; + cmd.RW = RW_READ | RW_WRITE; + cmd.CNTR = 0; - outb(PIT_CMD, cmd.cmd); - int divisor = 1193180 / hz; - outb(PIT_CH0, divisor & 0xff); - outb(PIT_CH0, divisor >> 8); + outb(PIT_CMD, cmd.cmd); + int divisor = 1193180 / hz; + outb(PIT_CH0, divisor & 0xff); + outb(PIT_CH0, divisor >> 8); } void init_timer() { - set_timer_phase(20); + hz = 20; + secs = 0; + ticks = 0; + set_timer_phase(); - set_interrupt_handler(0x20, &timer); - outb(PIC1_DATA, inb(PIC1_DATA)&~0x01); + set_interrupt_handler(0x20, & timer); + outb(PIC1_DATA, inb(PIC1_DATA) & ~0x01); } From 51f59085871d744e687c1180bfdb227ed9c08ca9 Mon Sep 17 00:00:00 2001 From: Uiop Pilon Date: Tue, 17 Nov 2020 19:54:29 +0000 Subject: [PATCH 2/2] Added Sleep Function --- include/pic/timer.h | 2 ++ src/pic/timer.c | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/pic/timer.h b/include/pic/timer.h index 7f27b6f..0091d5e 100644 --- a/include/pic/timer.h +++ b/include/pic/timer.h @@ -36,4 +36,6 @@ void init_timer(); double get_uptime(); +void sleep(double seconds); + #endif diff --git a/src/pic/timer.c b/src/pic/timer.c index c236736..98b5fba 100644 --- a/src/pic/timer.c +++ b/src/pic/timer.c @@ -11,7 +11,12 @@ void timer(struct interrupt_frame * frame) { return; } -// Returns how many seconds the computer wa +void sleep(double seconds) { + double to_wait = seconds + get_uptime(); + while(to_wait != get_uptime()); +} + +// Returns how many seconds the computer was awake double get_uptime() { return ticks * (1 / hz); }