swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 1 | #include "vga.h" |
swissChili | 9b3584b | 2021-02-18 13:57:27 -0800 | [diff] [blame] | 2 | #include "io.h" |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 3 | #include "paging.h" |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 4 | #include "log.h" |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 5 | |
| 6 | static uint cursor_x = 0; |
| 7 | static uint cursor_y = 0; |
| 8 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 9 | static uchar color = WHITE; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 10 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 11 | static ushort *fb = (ushort *)PHYS_TO_VIRT(0xB8000); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 12 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 13 | #define BUFFER_SIZE 16 |
| 14 | static char buffer[BUFFER_SIZE]; |
| 15 | static uint buffer_index = 0; |
| 16 | static bool in_escape = false; |
| 17 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 18 | static void move_cursor() |
| 19 | { |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 20 | ushort pos = cursor_y * 80 + cursor_x; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 21 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 22 | outb(0x3d4, 0x0e); |
| 23 | outb(0x3d5, pos >> 8); |
| 24 | outb(0x3d4, 0x0f); |
| 25 | outb(0x3d5, pos & 0xff); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static void scroll() |
| 29 | { |
| 30 | ushort blank = ' ' | color << 8; |
| 31 | |
| 32 | while (cursor_y >= 25) // end of line |
| 33 | { |
| 34 | // scroll everything up |
| 35 | memcpy(fb, &fb[80], 24 * 80 * 2); |
| 36 | |
| 37 | for (int i = 24 * 80; i < 25 * 80; i++) |
| 38 | { |
| 39 | fb[i] = blank; |
| 40 | } |
| 41 | |
| 42 | cursor_y--; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void vga_set_color(enum vga_colors fg, enum vga_colors bg) |
| 47 | { |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 48 | color = (bg << 4) | (fg & 0xf); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 49 | } |
| 50 | |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 51 | void got_escape() |
| 52 | { |
| 53 | if (buffer[0] != '[') |
| 54 | return; |
| 55 | |
| 56 | int c = parse_int(buffer+1); |
| 57 | |
| 58 | static const char ansi_to_vga_colors[] = |
| 59 | { |
| 60 | [30] = BLACK, |
| 61 | RED, |
| 62 | GREEN, |
| 63 | LIGHT_BROWN, |
| 64 | BLUE, |
| 65 | MAGENTA, |
| 66 | CYAN, |
| 67 | WHITE, |
| 68 | [90] = LIGHT_GREY, |
| 69 | LIGHT_RED, |
| 70 | LIGHT_GREEN, |
| 71 | LIGHT_BROWN, |
| 72 | LIGHT_BLUE, |
| 73 | LIGHT_MAGENTA, |
| 74 | LIGHT_CYAN, |
| 75 | WHITE, |
| 76 | }; |
| 77 | |
| 78 | if (c == 0) |
| 79 | { |
| 80 | color = WHITE; |
| 81 | } |
| 82 | else if ((c >= 30 && c <= 37) || (c >= 90 && c <= 97)) |
| 83 | { |
| 84 | color &= 0xf0; |
| 85 | color |= ansi_to_vga_colors[c]; |
| 86 | } |
| 87 | else if ((c >= 40 && c <= 47) || (c >= 100 && c <= 107)) |
| 88 | { |
| 89 | color &= 0x0f; |
| 90 | color |= ansi_to_vga_colors[c - 10] << 4; |
| 91 | } |
| 92 | } |
| 93 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 94 | void vga_put(char c) |
| 95 | { |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 96 | if (in_escape) |
| 97 | { |
| 98 | if (buffer_index >= BUFFER_SIZE || c == 'm') |
| 99 | { |
| 100 | // For now we are only supporting color escape sequences. |
| 101 | if (c == 'm') |
| 102 | got_escape(); |
| 103 | |
| 104 | // Escape sequence is too long, sorry. Failing silently. |
| 105 | in_escape = false; |
| 106 | buffer_index = 0; |
| 107 | memset(buffer, 0, sizeof(buffer)); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | buffer[buffer_index++] = c; |
| 112 | } |
| 113 | |
| 114 | return; |
| 115 | } |
| 116 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 117 | switch (c) |
| 118 | { |
| 119 | case '\b': |
| 120 | if (cursor_x > 0) |
| 121 | cursor_x--; |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 122 | fb[cursor_y * 80 + cursor_x] = ' ' | (color << 8); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 123 | break; |
| 124 | case '\t': |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 125 | cursor_x = (cursor_x + 8) & ~7; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 126 | break; |
| 127 | case '\n': |
| 128 | cursor_y++; |
| 129 | case '\r': |
| 130 | cursor_x = 0; |
| 131 | break; |
swissChili | 9bd74de | 2021-06-15 20:30:48 -0700 | [diff] [blame] | 132 | case 033: |
| 133 | in_escape = true; |
| 134 | break; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 135 | default: |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 136 | fb[cursor_y * 80 + cursor_x] = c | (color << 8); |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 137 | cursor_x++; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if (cursor_x >= 80) // off screen |
| 141 | { |
| 142 | cursor_x = 0; |
| 143 | cursor_y++; |
| 144 | } |
| 145 | |
| 146 | scroll(); |
| 147 | move_cursor(); |
| 148 | } |
| 149 | |
| 150 | void vga_clear() |
| 151 | { |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 152 | for (int i = 0; i < 25 * 80; i++) |
| 153 | { |
| 154 | fb[i] = ' ' | (WHITE << 8); |
| 155 | } |
| 156 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 157 | cursor_x = 0; |
| 158 | cursor_y = 0; |
| 159 | move_cursor(); |
| 160 | } |
| 161 | |
| 162 | void vga_write(char *c) |
| 163 | { |
| 164 | for (int i = 0; c[i]; i++) |
| 165 | vga_put(c[i]); |
| 166 | } |
| 167 | |
| 168 | void vga_putd(uint d) |
| 169 | { |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 170 | char str[48]; |
| 171 | memset(str, 0, 48); |
| 172 | uint i = 0; |
| 173 | |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 174 | do |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 175 | { |
| 176 | str[i++] = (d % 10) + '0'; |
| 177 | d /= 10; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 178 | } while (d > 0 && i < 48); // should never be more than 48 digits anyway |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 179 | |
| 180 | for (uint j = i; j; j--) |
| 181 | { |
| 182 | vga_put(str[j - 1]); |
| 183 | } |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 184 | } |
| 185 | |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 186 | static bool vga_put_nibble(uchar n, bool first) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 187 | { |
swissChili | 36ed5d7 | 2021-07-23 14:56:36 -0700 | [diff] [blame] | 188 | if (first && n == 0) |
| 189 | return true; |
| 190 | else if (n <= 9) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 191 | vga_put('0' + n); |
| 192 | else |
| 193 | vga_put('A' + n - 10); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 194 | |
| 195 | return false; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void vga_putx(uint x) |
| 199 | { |
swissChili | 36ed5d7 | 2021-07-23 14:56:36 -0700 | [diff] [blame] | 200 | bool first = true; |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 201 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 202 | for (int shift = 24; shift >= 0; shift -= 8) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 203 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 204 | uchar byte = x >> shift; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 205 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 206 | first = vga_put_nibble((byte & 0xf0) >> 4, first); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 207 | first = vga_put_nibble(byte & 0x0f, first); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 210 | |
| 211 | void init_vga() |
| 212 | { |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 213 | // Enable and set max scan line |
| 214 | outb(0x3D4, 0x09); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 215 | outb(0x3D5, 15); |
| 216 | |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 217 | // Cursor end line |
| 218 | outb(0x3D4, 0x0B); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 219 | outb(0x3D5, 15); |
| 220 | |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 221 | // Cursor start line |
| 222 | outb(0x3D4, 0x0A); |
| 223 | outb(0x3D5, 14); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 224 | } |