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 | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 3 | #include "log.h" |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 4 | |
| 5 | static uint cursor_x = 0; |
| 6 | static uint cursor_y = 0; |
| 7 | |
| 8 | static ushort color = WHITE; |
| 9 | |
swissChili | d3a652e | 2021-02-21 22:16:06 -0800 | [diff] [blame] | 10 | static ushort *fb = (ushort *)(0xB8000 + 0xC0000000); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 11 | |
| 12 | static void move_cursor() |
| 13 | { |
| 14 | ushort loc = cursor_y * 80 + cursor_x; |
| 15 | outb(0x3d4, 14); // Setting high cursor byte |
| 16 | outb(0x3d4, loc >> 8); |
| 17 | |
| 18 | outb(0x3d4, 15); // low byte |
| 19 | outb(0x3d4, loc & 0xff); |
| 20 | } |
| 21 | |
| 22 | static void scroll() |
| 23 | { |
| 24 | ushort blank = ' ' | color << 8; |
| 25 | |
| 26 | while (cursor_y >= 25) // end of line |
| 27 | { |
| 28 | // scroll everything up |
| 29 | memcpy(fb, &fb[80], 24 * 80 * 2); |
| 30 | |
| 31 | for (int i = 24 * 80; i < 25 * 80; i++) |
| 32 | { |
| 33 | fb[i] = blank; |
| 34 | } |
| 35 | |
| 36 | cursor_y--; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void vga_set_color(enum vga_colors fg, enum vga_colors bg) |
| 41 | { |
| 42 | color = (bg << 4) | fg & 0xf; |
| 43 | } |
| 44 | |
| 45 | void vga_put(char c) |
| 46 | { |
| 47 | switch (c) |
| 48 | { |
| 49 | case '\b': |
| 50 | if (cursor_x > 0) |
| 51 | cursor_x--; |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 52 | fb[cursor_y * 80 + cursor_x] = ' ' | (color << 8); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 53 | break; |
| 54 | case '\t': |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 55 | cursor_x = (cursor_x + 8) & ~7; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 56 | break; |
| 57 | case '\n': |
| 58 | cursor_y++; |
| 59 | case '\r': |
| 60 | cursor_x = 0; |
| 61 | break; |
| 62 | default: |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 63 | fb[cursor_y * 80 + cursor_x] = c | (color << 8); |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 64 | cursor_x++; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (cursor_x >= 80) // off screen |
| 68 | { |
| 69 | cursor_x = 0; |
| 70 | cursor_y++; |
| 71 | } |
| 72 | |
| 73 | scroll(); |
| 74 | move_cursor(); |
| 75 | } |
| 76 | |
| 77 | void vga_clear() |
| 78 | { |
| 79 | memset(fb, 0, 80 * 25 * 2); |
| 80 | cursor_x = 0; |
| 81 | cursor_y = 0; |
| 82 | move_cursor(); |
| 83 | } |
| 84 | |
| 85 | void vga_write(char *c) |
| 86 | { |
| 87 | for (int i = 0; c[i]; i++) |
| 88 | vga_put(c[i]); |
| 89 | } |
| 90 | |
| 91 | void vga_putd(uint d) |
| 92 | { |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 93 | char str[48]; |
| 94 | memset(str, 0, 48); |
| 95 | uint i = 0; |
| 96 | |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 97 | do |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 98 | { |
| 99 | str[i++] = (d % 10) + '0'; |
| 100 | d /= 10; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 101 | } while (d > 0 && i < 48); // should never be more than 48 digits anyway |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 102 | |
| 103 | for (uint j = i; j; j--) |
| 104 | { |
| 105 | vga_put(str[j - 1]); |
| 106 | } |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 107 | } |
| 108 | |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 109 | static bool vga_put_nibble(uchar n, bool first) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 110 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 111 | // if (first && n == 0) |
| 112 | // return true; |
| 113 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 114 | if (n <= 9) |
| 115 | vga_put('0' + n); |
| 116 | else |
| 117 | vga_put('A' + n - 10); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 118 | |
| 119 | return false; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void vga_putx(uint x) |
| 123 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 124 | bool first = false; |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 125 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 126 | for (int shift = 24; shift >= 0; shift -= 8) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 127 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 128 | uchar byte = x >> shift; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 129 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 130 | first = vga_put_nibble((byte & 0xf0) >> 4, first); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 131 | first = vga_put_nibble(byte & 0x0f, first); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 132 | } |
| 133 | } |