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