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 | |
| 9 | static ushort color = WHITE; |
| 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 | |
| 13 | static void move_cursor() |
| 14 | { |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 15 | ushort pos = cursor_y * 80 + cursor_x; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 16 | |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 17 | outb(0x3d4, 0x0e); |
| 18 | outb(0x3d5, pos >> 8); |
| 19 | outb(0x3d4, 0x0f); |
| 20 | outb(0x3d5, pos & 0xff); |
| 21 | |
| 22 | io_wait(); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | static void scroll() |
| 26 | { |
| 27 | ushort blank = ' ' | color << 8; |
| 28 | |
| 29 | while (cursor_y >= 25) // end of line |
| 30 | { |
| 31 | // scroll everything up |
| 32 | memcpy(fb, &fb[80], 24 * 80 * 2); |
| 33 | |
| 34 | for (int i = 24 * 80; i < 25 * 80; i++) |
| 35 | { |
| 36 | fb[i] = blank; |
| 37 | } |
| 38 | |
| 39 | cursor_y--; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | void vga_set_color(enum vga_colors fg, enum vga_colors bg) |
| 44 | { |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 45 | color = (bg << 4) | (fg & 0xf); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void vga_put(char c) |
| 49 | { |
| 50 | switch (c) |
| 51 | { |
| 52 | case '\b': |
| 53 | if (cursor_x > 0) |
| 54 | cursor_x--; |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 55 | fb[cursor_y * 80 + cursor_x] = ' ' | (color << 8); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 56 | break; |
| 57 | case '\t': |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 58 | cursor_x = (cursor_x + 8) & ~7; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 59 | break; |
| 60 | case '\n': |
| 61 | cursor_y++; |
| 62 | case '\r': |
| 63 | cursor_x = 0; |
| 64 | break; |
| 65 | default: |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 66 | fb[cursor_y * 80 + cursor_x] = c | (color << 8); |
swissChili | 19ef418 | 2021-02-21 17:45:51 -0800 | [diff] [blame] | 67 | cursor_x++; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | if (cursor_x >= 80) // off screen |
| 71 | { |
| 72 | cursor_x = 0; |
| 73 | cursor_y++; |
| 74 | } |
| 75 | |
| 76 | scroll(); |
| 77 | move_cursor(); |
| 78 | } |
| 79 | |
| 80 | void vga_clear() |
| 81 | { |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 82 | for (int i = 0; i < 25 * 80; i++) |
| 83 | { |
| 84 | fb[i] = ' ' | (WHITE << 8); |
| 85 | } |
| 86 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 87 | cursor_x = 0; |
| 88 | cursor_y = 0; |
| 89 | move_cursor(); |
| 90 | } |
| 91 | |
| 92 | void vga_write(char *c) |
| 93 | { |
| 94 | for (int i = 0; c[i]; i++) |
| 95 | vga_put(c[i]); |
| 96 | } |
| 97 | |
| 98 | void vga_putd(uint d) |
| 99 | { |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 100 | char str[48]; |
| 101 | memset(str, 0, 48); |
| 102 | uint i = 0; |
| 103 | |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 104 | do |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 105 | { |
| 106 | str[i++] = (d % 10) + '0'; |
| 107 | d /= 10; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 108 | } while (d > 0 && i < 48); // should never be more than 48 digits anyway |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 109 | |
| 110 | for (uint j = i; j; j--) |
| 111 | { |
| 112 | vga_put(str[j - 1]); |
| 113 | } |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 116 | static bool vga_put_nibble(uchar n, bool first) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 117 | { |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 118 | // if (first && n == 0) |
| 119 | // return true; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 120 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 121 | if (n <= 9) |
| 122 | vga_put('0' + n); |
| 123 | else |
| 124 | vga_put('A' + n - 10); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 125 | |
| 126 | return false; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void vga_putx(uint x) |
| 130 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 131 | bool first = false; |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 132 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 133 | for (int shift = 24; shift >= 0; shift -= 8) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 134 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 135 | uchar byte = x >> shift; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 136 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 137 | first = vga_put_nibble((byte & 0xf0) >> 4, first); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 138 | first = vga_put_nibble(byte & 0x0f, first); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 139 | } |
| 140 | } |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 141 | |
| 142 | void init_vga() |
| 143 | { |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 144 | // Enable and set max scan line |
| 145 | outb(0x3D4, 0x09); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 146 | outb(0x3D5, 15); |
| 147 | |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 148 | // Cursor end line |
| 149 | outb(0x3D4, 0x0B); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 150 | outb(0x3D5, 15); |
| 151 | |
swissChili | 959aa8a | 2021-02-23 19:43:00 -0800 | [diff] [blame] | 152 | // Cursor start line |
| 153 | outb(0x3D4, 0x0A); |
| 154 | outb(0x3D5, 14); |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame] | 155 | } |