-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_timer.C
executable file
·112 lines (83 loc) · 3.74 KB
/
simple_timer.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
File: simple_timer.C
Author: R. Bettati
Department of Computer Science
Texas A&M University
Date : 09/03/19
Simple control of the
*/
/*--------------------------------------------------------------------------*/
/* DEFINES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* INCLUDES */
/*--------------------------------------------------------------------------*/
#include "assert.H"
#include "utils.H"
#include "console.H"
#include "interrupts.H"
#include "simple_timer.H"
/*--------------------------------------------------------------------------*/
/* VARIABLES */
/*--------------------------------------------------------------------------*/
unsigned long SimpleTimer::seconds;
int SimpleTimer::ticks; /* ticks since last "seconds" update. */
/* At what frequency do we update the ticks counter? */
int SimpleTimer::hz; /* Actually, by defaults it is 18.22Hz.
In this way, a 16-bit counter wraps
around every hour. */
/*--------------------------------------------------------------------------*/
/* INITIALIZATION OF STATIC VARIABLES */
/*--------------------------------------------------------------------------*/
void SimpleTimer::init(int _hz) {
/* How long has the system been running? */
seconds = 0;
ticks = 0; /* ticks since last "seconds" update. */
/* At what frequency do we update the ticks counter? */
/* hz = 18; */
/* Actually, by defaults it is 18.22Hz.
In this way, a 16-bit counter wraps
around every hour. */
set_frequency(_hz);
}
/*--------------------------------------------------------------------------*/
/* METHODS FOR CLASS S i m p l e T i m e r */
/*--------------------------------------------------------------------------*/
void SimpleTimer::handler(REGS *_r) {
/* What to do when timer interrupt occurs? In this case, we update "ticks",
and maybe update "seconds".
This must be installed as the interrupt handler for the timer in the
when the system gets initialized. (e.g. in "kernel.C") */
/* Increment our "ticks" count */
ticks++;
/* Whenever a second is over, we update counter accordingly. */
if (ticks >= hz )
{
seconds++;
ticks = 0;
Console::puts("One second has passed\n");
}
}
void SimpleTimer::set_frequency(int _hz) {
/* Set the interrupt frequency for the simple timer.
Preferably set this before installing the timer handler! */
hz = _hz; /* Remember the frequency. */
int divisor = 1193180 / _hz; /* The input clock runs at 1.19MHz */
outportb(0x43, 0x34); /* Set command byte to be 0x36. */
outportb(0x40, divisor & 0xFF); /* Set low byte of divisor. */
outportb(0x40, divisor >> 8); /* Set high byte of divisor. */
}
void SimpleTimer::current(unsigned long * _seconds, int * _ticks) {
/* Return the current "time" since the system started. */
*_seconds = seconds;
*_ticks = ticks;
}
void SimpleTimer::wait(unsigned long _seconds) {
/* Wait for a particular time to be passed. This is based on busy looping! */
unsigned long now_seconds;
int now_ticks;
current(&now_seconds, &now_ticks);
unsigned long then_seconds = now_seconds + _seconds;
while((seconds <= then_seconds) && (ticks < now_ticks));
}