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 | { |
| 82 | memset(fb, 0, 80 * 25 * 2); |
| 83 | cursor_x = 0; |
| 84 | cursor_y = 0; |
| 85 | move_cursor(); |
| 86 | } |
| 87 | |
| 88 | void vga_write(char *c) |
| 89 | { |
| 90 | for (int i = 0; c[i]; i++) |
| 91 | vga_put(c[i]); |
| 92 | } |
| 93 | |
| 94 | void vga_putd(uint d) |
| 95 | { |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 96 | char str[48]; |
| 97 | memset(str, 0, 48); |
| 98 | uint i = 0; |
| 99 | |
swissChili | defeb0d | 2021-02-18 15:28:36 -0800 | [diff] [blame] | 100 | do |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 101 | { |
| 102 | str[i++] = (d % 10) + '0'; |
| 103 | d /= 10; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 104 | } while (d > 0 && i < 48); // should never be more than 48 digits anyway |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 105 | |
| 106 | for (uint j = i; j; j--) |
| 107 | { |
| 108 | vga_put(str[j - 1]); |
| 109 | } |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 110 | } |
| 111 | |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 112 | static bool vga_put_nibble(uchar n, bool first) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 113 | { |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame^] | 114 | // if (first && n == 0) |
| 115 | // return true; |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 116 | |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 117 | if (n <= 9) |
| 118 | vga_put('0' + n); |
| 119 | else |
| 120 | vga_put('A' + n - 10); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 121 | |
| 122 | return false; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void vga_putx(uint x) |
| 126 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 127 | bool first = false; |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 128 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 129 | for (int shift = 24; shift >= 0; shift -= 8) |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 130 | { |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 131 | uchar byte = x >> shift; |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 132 | |
swissChili | 825d46b | 2021-02-21 10:14:16 -0800 | [diff] [blame] | 133 | first = vga_put_nibble((byte & 0xf0) >> 4, first); |
swissChili | 0b35bf2 | 2021-02-18 12:49:40 -0800 | [diff] [blame] | 134 | first = vga_put_nibble(byte & 0x0f, first); |
swissChili | d813792 | 2021-02-17 15:34:07 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
swissChili | dc25b2b | 2021-02-23 17:07:13 -0800 | [diff] [blame^] | 137 | |
| 138 | void init_vga() |
| 139 | { |
| 140 | return; |
| 141 | |
| 142 | outb(0x3D4, 0x09); // set maximum scan line register to 15 |
| 143 | outb(0x3D5, 15); |
| 144 | |
| 145 | outb(0x3D4, 0x0B); // set the cursor end line to 15 |
| 146 | outb(0x3D5, 15); |
| 147 | |
| 148 | outb(0x3D4, 0x0A); // set the cursor start line to 14 and enable cursor |
| 149 | // visibility |
| 150 | outb(0x3D5, 10); |
| 151 | } |