blob: 1b108334ec9470f8ff41ef62672527e60cff901c [file] [log] [blame]
swissChilid8137922021-02-17 15:34:07 -08001#include "vga.h"
2#include "mem.h"
3
4static uint cursor_x = 0;
5static uint cursor_y = 0;
6
7static ushort color = WHITE;
8
9static ushort *fb = (ushort *)0xB8000;
10
11static void move_cursor()
12{
13 ushort loc = cursor_y * 80 + cursor_x;
14 outb(0x3d4, 14); // Setting high cursor byte
15 outb(0x3d4, loc >> 8);
16
17 outb(0x3d4, 15); // low byte
18 outb(0x3d4, loc & 0xff);
19}
20
21static void scroll()
22{
23 ushort blank = ' ' | color << 8;
24
25 while (cursor_y >= 25) // end of line
26 {
27 // scroll everything up
28 memcpy(fb, &fb[80], 24 * 80 * 2);
29
30 for (int i = 24 * 80; i < 25 * 80; i++)
31 {
32 fb[i] = blank;
33 }
34
35 cursor_y--;
36 }
37}
38
39void vga_set_color(enum vga_colors fg, enum vga_colors bg)
40{
41 color = (bg << 4) | fg & 0xf;
42}
43
44void vga_put(char c)
45{
46 switch (c)
47 {
48 case '\b':
49 if (cursor_x > 0)
50 cursor_x--;
51 break;
52 case '\t':
53 cursor_x = (cursor_x + 8) & ~ 7;
54 break;
55 case '\n':
56 cursor_y++;
57 case '\r':
58 cursor_x = 0;
59 break;
60 default:
61 cursor_x++;
62 fb[cursor_y * 80 + cursor_x] = c | (color << 8);
63 }
64
65 if (cursor_x >= 80) // off screen
66 {
67 cursor_x = 0;
68 cursor_y++;
69 }
70
71 scroll();
72 move_cursor();
73}
74
75void vga_clear()
76{
77 memset(fb, 0, 80 * 25 * 2);
78 cursor_x = 0;
79 cursor_y = 0;
80 move_cursor();
81}
82
83void vga_write(char *c)
84{
85 for (int i = 0; c[i]; i++)
86 vga_put(c[i]);
87}
88
89void vga_putd(uint d)
90{
91
92}
93
94static void vga_put_nibble(uchar n)
95{
96 if (n <= 9)
97 vga_put('0' + n);
98 else
99 vga_put('A' + n - 10);
100}
101
102void vga_putx(uint x)
103{
104 for (uint mask = 0xFF000000, shift = 24; mask; mask >>= 8, shift -= 8)
105 {
106 uchar byte = (x & mask) >> shift;
107
108 vga_put_nibble((byte & 0xf0) >> 8);
109 vga_put_nibble(byte & 0x0f);
110 }
111}