3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /* This will keep track of how many ticks that the system
26 * has been running for */
27 unsigned long timer_ticks
= 0;
29 /* Software timer - ticks count to 1ms */
30 unsigned long timer_swticks
= 0;
32 /* Handles the timer. In this case, it's very simple: We
33 * increment the 'timer_ticks' variable every time the
34 * timer fires. By default, the timer fires 18.222 times
35 * per second. Why 18.222Hz? Some engineer at IBM must've
36 * been smoking something funky */
37 void timer_handler (struct regs
*r
)
39 /* Increment our 'tick count' */
42 /* Every 18 clocks (approximately 1 second), we will
43 * display a message on the screen */
44 /* if (timer_ticks % (HZ/18) == 0)
46 kprintf (": %d\n", timer_ticks);
49 //if (timer_ticks > 200)
53 /* This will continuously loop until the given time has
55 void timer_wait (int ticks
)
59 eticks
= timer_ticks
+ ticks
;
61 while (timer_ticks
< eticks
)
65 char *time_tostring (unsigned long s
)
67 static char buffer
[512];
68 unsigned int weeks
, days
, hours
, minutes
, seconds
, total
;
75 minutes
= minutes
% 60;
82 total
+= sprintf (buffer
, "%u week%s, ", weeks
, (weeks
> 1) ? "s" : "");
84 total
+= sprintf (buffer
+ total
, "%u day%s, ", days
, (days
> 1) ? "s" : "");
86 if ((hours
|| minutes
|| seconds
) || (!(weeks
|| days
)))
87 total
+= sprintf (buffer
+ total
, "%2uh %2umin %2usec", hours
, minutes
, seconds
);
94 printf ("Uptime: %s\n", time_tostring (timer_ticks
/((3579545L / 3) / HZ
)));
99 /* Kernel with 1000Hz timer - dont need it ? */
100 if (kernel_attr
& KERNEL_18HZ
) {
103 eticks
= timer_ticks
+ 18;
105 while (timer_ticks
< eticks
) {
110 kprintf ("Freqency: %lluHz\n", ctps
);
116 unsigned long timer
= timer_ticks
;
118 while ((timer_ticks
- timer
) >= 1)
122 void usleep (unsigned len
)
124 if (kernel_attr
& KERNEL_18HZ
) {
125 unsigned long long t
= ctps
* len
;
126 unsigned long long i
= 0;
140 if (kernel_attr
& KERNEL_18HZ
) { // Dont set new timer frequency, when is setted KERNEL_18HZ
141 kprintf ("Warning -> kernel is set to 18HZ timer frequency !\n");
142 foo
= (3579545L / 3) / 18;
145 foo
= (3579545L / 3) / HZ
;
147 /* reprogram the 8253 timer chip to run at 'HZ', instead of 18 Hz */
148 outb (0x43, 0x36); /* channel 0, LSB/MSB, mode 3, binary */
149 outb (0x40, (unsigned short) foo
& 0xFF); /* LSB */
150 outb (0x40, (unsigned short) foo
>> 8); /* MSB */
153 /* Sets up the system clock by installing the timer handler into IRQ0 */
154 unsigned int timer_install ()
156 /* set hw timer (irq0) to HZ frequency */
159 /* Installs 'timer_handler' to IRQ0 */
160 irq_install_handler (0, timer_handler
);