- Fixed keGetUnixTime().
[planlOS.git] / system / kernel / ke / debug.c
blobd64872a1bdf00eafb0a2ed5b4f4cbb0661c504d8
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "ke/debug.h"
23 #include "ke/ports.h"
24 #include "ke/level.h"
25 #include "ke/spinlock.h"
26 #include <string.h>
28 #define SERIAL 0x3f8
30 static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
31 static char digits_cap[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 static char *vidmem = (char*)0xC00B8000;
34 static int cursorx = 0;
35 static int cursory = 0;
37 static KeSpinlock printlock;
39 static int use_vga = 1;
41 void keTerminalInitialized(void)
43 //use_vga = 0;
46 void kePrintInit(void)
48 outb(SERIAL + 1, 0); // Disable interrupts
49 outb(SERIAL + 3, 0x80); // Set baud rate
50 outb(SERIAL, 0x3); // High byte, 38400 baud
51 outb(SERIAL + 1, 0); // Low byte
52 outb(SERIAL + 3, 0x3); // 8 bits, no parity bit, one stop bit
53 outb(SERIAL + 2, 0xC7); // Enable FIFO
54 outb(SERIAL + 4, 0x0B); // Enable interrupts
55 keInitSpinlock(&printlock);
58 static void keScroll(int lines)
60 memmove(vidmem, vidmem + lines * 160, (25 - lines) * 160);
61 memset(vidmem + (25 - lines) * 160, 0, lines * 160);
62 cursory -= lines;
65 static void kePrintChar(char c)
67 // Serial output
68 while ((inb(SERIAL + 5) & 0x20) == 0);
69 outb(SERIAL, c);
71 if (use_vga)
73 // VGA output
74 switch (c)
76 case '\n':
77 cursorx = 0;
78 cursory++;
79 if (cursory == 25) keScroll(1);
80 break;
81 case '\r':
82 cursorx = 0;
83 break;
84 case '\t':
85 kePrintChar(' ');
86 while (cursorx & 0x7) kePrintChar(' ');
87 break;
88 default:
89 vidmem[cursory * 160 + cursorx * 2] = c;
90 vidmem[cursory * 160 + cursorx * 2 + 1] = 0x07;
91 cursorx++;
92 if (cursorx == 80)
94 cursorx = 0;
95 cursory++;
96 if (cursory == 25) keScroll(1);
98 break;
101 else
103 // TODO
106 static void kePrintString(const char *s)
108 while (*s)
110 kePrintChar(*s);
111 s++;
115 static void kePrintInt(int32_t value, uint32_t base, int capital_letters)
117 if (value < 0) {
118 kePrintChar('-');
119 value = -value;
121 if (value >= (int32_t)base) {
122 kePrintInt(value / base, base, capital_letters);
124 int digit = value % base;
125 if (capital_letters) {
126 kePrintChar(digits_cap[digit]);
127 } else {
128 kePrintChar(digits[digit]);
131 static void kePrintUInt(uint32_t value, uint32_t base, int capital_letters)
133 if (value >= base) {
134 kePrintInt(value / base, base, capital_letters);
136 int digit = value % base;
137 if (capital_letters) {
138 kePrintChar(digits_cap[digit]);
139 } else {
140 kePrintChar(digits[digit]);
144 void kePrint(const char *fmt, ...)
146 int *args = ((int*)&fmt) + 1;
147 kePrintList(fmt, &args);
149 void kePrintList(const char *fmt, int **args)
151 KeExecLevel oldlevel = keSetExecutionLevel(KE_LEVEL_HIGH);
152 keLockSpinlock(&printlock);
153 while (*fmt) {
154 switch (*fmt)
156 case '%':
157 switch(*(fmt+1))
159 case '%':
160 kePrintChar('%');
161 break;
162 case 'c':
163 kePrintChar(*((char*)(*args)));
164 (*args)++;
165 break;
166 case 'd':
167 case 'i':
168 kePrintInt(*((int32_t*)(*args)), 10, 0);
169 (*args)++;
170 break;
171 case 'u':
172 kePrintUInt(*((uint32_t*)(*args)), 10, 0);
173 (*args)++;
174 break;
175 case 'x':
176 kePrintUInt(*((uint32_t*)(*args)), 16, 0);
177 (*args)++;
178 break;
179 case 'X':
180 kePrintUInt(*((uint32_t*)(*args)), 16, 1);
181 (*args)++;
182 break;
183 case 's':
184 kePrintString(*((char**)(*args)));
185 (*args)++;
186 break;
188 fmt++;
189 break;
190 case '\n':
191 kePrintChar('\n');
192 break;
193 case '\t':
194 kePrintChar('\t');
195 break;
196 default:
197 kePrintChar(*fmt);
198 break;
200 fmt++;
202 keUnlockSpinlock(&printlock);
203 keSetExecutionLevel(oldlevel);