1 #include <lwk/driver.h>
2 #include <lwk/console.h>
3 #include <lwk/string.h>
5 /** Base address of the VGA frame buffer. */
6 static volatile uint8_t * const vga_fb = (uint8_t *) 0xffffffff800b8000ul;
8 /** Current cursor row coordinate. */
11 /** Current cursor column coordinate. */
14 /** Number of rows on the screen. */
15 static int nrows = 25;
17 /** Number of columns on the screen. */
18 static int ncols = 80;
20 /** Set when vga console has been initialized. */
21 static int initialized = 0;
23 /** Calculates the offset in the vga_fb corresponding to (row, col). */
24 static inline int cursor(int row, int col)
26 return (row * ncols * 2) + col * 2;
30 * Scrolls everything on the screen up by one row.
32 static void vga_scroll(void)
36 // Move all existing lines up by one
39 (void *) (vga_fb + cursor(1, 0)),
40 (nrows - 1) * ncols * sizeof(uint16_t)
43 // Blank the new line at the bottom of the screen
44 for (i = 0; i < ncols; i++)
45 vga_fb[cursor(nrows-1, i)] = ' ';
49 * Moves cursor to the next line.
51 static void vga_newline(void)
63 * Sets the VGA font color.
65 static void vga_set_font_color(uint8_t color)
68 for (i = 0; i < nrows; i++)
69 for (j = 0; j < ncols; j++)
70 vga_fb[cursor(i, j) + 1] = color;
74 * Prints a single character to the screen.
76 static void vga_putc(unsigned char c)
78 // Print the character
79 vga_fb[cursor(row, col)] = c;
87 * Writes a string to the screen at the current cursor location.
89 static void vga_write(struct console *con, const char *str)
93 while ((c = *str++) != '\0') {
102 while ((col % 8) != 0)
113 * VGA console device.
115 static struct console vga_console = {
116 .name = "VGA Console",
121 * Initializes and registers the VGA console driver.
123 void vga_console_init(void)
126 printk(KERN_ERR "VGA console already initialized.\n");
130 vga_set_font_color(0x0F /* White */);
131 console_register(&vga_console);
135 driver_init(vga_console_init);
138 * Sets the row on the screen to start printing at.
139 * This is used to avoid overwriting BIOS/boot messages.
140 * At least on x86-64, this is set automatically as part
141 * of the bootstrap process.
143 driver_param(row, int);