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.
25 #include "ke/spinlock.h"
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)
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);
65 static void kePrintChar(char c
)
68 while ((inb(SERIAL
+ 5) & 0x20) == 0);
79 if (cursory
== 25) keScroll(1);
86 while (cursorx
& 0x7) kePrintChar(' ');
89 vidmem
[cursory
* 160 + cursorx
* 2] = c
;
90 vidmem
[cursory
* 160 + cursorx
* 2 + 1] = 0x07;
96 if (cursory
== 25) keScroll(1);
106 static void kePrintString(const char *s
)
115 static void kePrintInt(int32_t value
, uint32_t base
, int capital_letters
)
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
]);
128 kePrintChar(digits
[digit
]);
131 static void kePrintUInt(uint32_t value
, uint32_t base
, int capital_letters
)
134 kePrintInt(value
/ base
, base
, capital_letters
);
136 int digit
= value
% base
;
137 if (capital_letters
) {
138 kePrintChar(digits_cap
[digit
]);
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
);
163 kePrintChar(*((char*)(*args
)));
168 kePrintInt(*((int32_t*)(*args
)), 10, 0);
172 kePrintUInt(*((uint32_t*)(*args
)), 10, 0);
176 kePrintUInt(*((uint32_t*)(*args
)), 16, 0);
180 kePrintUInt(*((uint32_t*)(*args
)), 16, 1);
184 kePrintString(*((char**)(*args
)));
202 keUnlockSpinlock(&printlock
);
203 keSetExecutionLevel(oldlevel
);