Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / kernel / arch / i386 / timer.c
blob442b4a1f984c02af48812f36b128c647757ee005
1 /*
2 * ZeX/OS
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/>.
21 #include <system.h>
22 #include <arch/io.h>
23 #include <string.h>
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' */
40 timer_ticks ++;
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);
47 }*/
49 //if (timer_ticks > 200)
50 // schedule ();
53 /* This will continuously loop until the given time has
54 * been reached */
55 void timer_wait (int ticks)
57 int eticks;
59 eticks = timer_ticks + ticks;
61 while (timer_ticks < eticks)
62 schedule ();
65 char *time_tostring (unsigned long s)
67 static char buffer[512];
68 unsigned int weeks, days, hours, minutes, seconds, total;
70 seconds = s % 60;
71 minutes = s / 60;
72 hours = minutes / 60;
73 days = hours / 24;
74 weeks = days / 7;
75 minutes = minutes % 60;
76 hours = hours % 24;
77 days = days % 7;
79 total = 0;
81 if (weeks)
82 total += sprintf (buffer, "%u week%s, ", weeks, (weeks > 1) ? "s" : "");
83 if (days)
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);
89 return buffer;
92 void uptime (void)
94 printf ("Uptime: %s\n", time_tostring (timer_ticks/((3579545L / 3) / HZ)));
97 void calc_freqency ()
99 /* Kernel with 1000Hz timer - dont need it ? */
100 if (kernel_attr & KERNEL_18HZ) {
101 int eticks;
103 eticks = timer_ticks + 18;
105 while (timer_ticks < eticks) {
106 ctps ++;
107 kprintf("");;
110 kprintf ("Freqency: %lluHz\n", ctps);
114 void calc_swtimer ()
116 unsigned long timer = timer_ticks;
118 while ((timer_ticks - timer) >= 1)
119 timer_swticks ++;
122 void usleep (unsigned len)
124 if (kernel_attr & KERNEL_18HZ) {
125 unsigned long long t = ctps * len;
126 unsigned long long i = 0;
128 while (t > i) {
129 i += 1000;
130 kprintf("");;
132 } else
133 timer_wait (1);
136 void init_8253 ()
138 unsigned foo = 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;
143 return;
144 } else
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 */
157 init_8253 ();
159 /* Installs 'timer_handler' to IRQ0 */
160 irq_install_handler (0, timer_handler);
162 return 1;