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.
28 static char digits
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
29 static char digits_cap
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
31 static char *vidmem
= (char*)0xC00B8000;
32 static int cursorx
= 0;
33 static int cursory
= 0;
35 void kePrintInit(void)
37 outb(SERIAL
+ 1, 0); // Disable interrupts
38 outb(SERIAL
+ 3, 0x80); // Set baud rate
39 outb(SERIAL
, 0x3); // High byte, 38400 baud
40 outb(SERIAL
+ 1, 0); // Low byte
41 outb(SERIAL
+ 3, 0x3); // 8 bits, no parity bit, one stop bit
42 outb(SERIAL
+ 2, 0xC7); // Enable FIFO
43 outb(SERIAL
+ 4, 0x0B); // Enable interrupts
46 static void keScroll(int lines
)
48 memmove(vidmem
, vidmem
+ lines
* 160, (25 - lines
) * 160);
49 memset(vidmem
+ (25 - lines
) * 160, 0, lines
* 160);
53 static void kePrintChar(char c
)
56 while ((inb(SERIAL
+ 5) & 0x20) == 0);
65 if (cursory
== 25) keScroll(1);
72 while (cursorx
& 0x7) kePrintChar(' ');
75 vidmem
[cursory
* 160 + cursorx
* 2] = c
;
76 vidmem
[cursory
* 160 + cursorx
* 2 + 1] = 0x07;
82 if (cursory
== 25) keScroll(1);
87 static void kePrintString(const char *s
)
96 static void kePrintInt(int32_t value
, uint32_t base
, int capital_letters
)
102 if (value
>= (int32_t)base
) {
103 kePrintInt(value
/ base
, base
, capital_letters
);
105 int digit
= value
% base
;
106 if (capital_letters
) {
107 kePrintChar(digits_cap
[digit
]);
109 kePrintChar(digits
[digit
]);
112 static void kePrintUInt(uint32_t value
, uint32_t base
, int capital_letters
)
115 kePrintInt(value
/ base
, base
, capital_letters
);
117 int digit
= value
% base
;
118 if (capital_letters
) {
119 kePrintChar(digits_cap
[digit
]);
121 kePrintChar(digits
[digit
]);
125 void kePrint(const char *fmt
, ...)
127 int *args
= ((int*)&fmt
) + 1;
128 kePrintList(fmt
, &args
);
130 void kePrintList(const char *fmt
, int **args
)
142 kePrintChar(*((char*)(*args
)));
147 kePrintInt(*((int32_t*)(*args
)), 10, 0);
151 kePrintUInt(*((uint32_t*)(*args
)), 10, 0);
155 kePrintUInt(*((uint32_t*)(*args
)), 16, 0);
159 kePrintUInt(*((uint32_t*)(*args
)), 16, 1);
163 kePrintString(*((char**)(*args
)));