2 * Copyright (c) 2008 Joshua Phillips. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 // get position of the hardware cursor
36 static void get_hw_cursor(struct console
*self
)
40 tmp
= inb(0x3D5) << 8;
43 self
->y
= tmp
/ self
->width
;
44 self
->x
= tmp
% self
->width
;
47 // set position of the hardware cursor
48 static void set_hw_cursor(struct console
*self
, int x
, int y
)
51 tmp
= y
* self
->width
+ x
;
53 outb(0x3D5, tmp
>> 8);
59 static void scroll(struct console
*self
, int n
)
64 self
->vram
+ n
* self
->width
,
65 self
->width
* (self
->height
- n
) * sizeof *self
->vram
);
67 for (p
= self
->width
* (self
->height
- n
)
68 ; p
< (self
->height
* self
->width
); p
++){
69 *(self
->vram
+ p
) = (self
->attrib
<< 8) | ' ';
75 static void newline(struct console
*self
)
79 if (self
->y
>= self
->height
){
80 scroll(self
, self
->y
+ 1 - self
->height
);
84 // process a backspace
85 static void backspace(struct console
*self
)
90 self
->vram
[self
->y
* self
->width
+ self
->x
] = (self
->attrib
<< 8) | ' ';
93 // handle special characters
94 static void special_chars(struct console
*self
, char ch
)
98 } else if (ch
== '\b'){
101 console_printf(self
, "\n*** TODO: Implement character %.8X\n", ch
);
105 void console_init(struct console
*self
)
107 self
->vram
= (unsigned short *) 0x000B8000;
111 self
->attrib
= 0x07; // white on black
114 void console_clear(struct console
*self
)
117 for (i
=0; i
<(self
->width
* self
->height
); i
++)
118 self
->vram
[i
] = (self
->attrib
<< 8) | ' ';
121 void console_colour_default(struct console
*self
)
126 void console_colour_fg(struct console
*self
, int fg
)
128 self
->attrib
= (self
->attrib
& ~0xF) | fg
;
131 void console_colour_fgbg(struct console
*self
, int fg
, int bg
)
133 self
->attrib
= (bg
<< 4) | fg
;
136 void console_putchar(struct console
*self
, char ch
)
138 if (ch
< ' ' || ch
== 0x7F){
139 special_chars(self
, ch
);
141 if (self
->y
>= self
->height
){
142 scroll(self
, self
->y
+ 1 - self
->height
);
144 self
->vram
[self
->y
* self
->width
+ self
->x
] = (self
->attrib
<< 8) | ch
;
146 if (self
->x
== self
->width
){
152 void console_puts(struct console
*self
, const char *str
)
155 console_putchar(self
, *str
);
158 set_hw_cursor(self
, self
->x
, self
->y
);
161 void console_printf(struct console
*self
, const char *fmt
, ...)
165 console_vprintf(self
, fmt
, ap
);
169 void console_vprintf(struct console
*self
, const char *fmt
, va_list ap
)
171 char buf
[256]; // should be enough for anyone :)
172 vsnprintf(buf
, 255, fmt
, ap
);
173 console_puts(self
, buf
);