1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include "vga.h"
#include "string.h"
#include "port.h"
#include <stdint.h>
#include <stddef.h>
uint16_t* terminal_buffer = (uint16_t*) 0xB8000;;
uint8_t vga_color(enum vga_color fg, enum vga_color bg) {
return fg | bg << 4;
}
uint16_t vga_entry(unsigned char uc, uint8_t color) {
return (uint16_t) uc | (uint16_t) color << 8;
}
void vclear(void) {
terminal_row = 0;
terminal_column = 0;
for (size_t y = 0; y < VGA_HEIGHT; y++)
{
for (size_t x = 0; x < VGA_WIDTH; x++)
{
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(' ', terminal_color);
}
}
}
void vsetcolor(uint8_t color) {
terminal_color = color;
}
void vsetchar(char c, uint8_t color, size_t x, size_t y) {
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void vscroll() {
for (unsigned int row=1; row < VGA_HEIGHT; row++)
{
size_t row_bytesize = VGA_WIDTH * 2; // A VGA entry is 2 bytes
// Copy each line one line up
memcpy(&terminal_buffer[(row - 1) * VGA_WIDTH],
&terminal_buffer[row * VGA_WIDTH], row_bytesize);
}
// Clear last line
for (unsigned int col=0; col < VGA_WIDTH; col++)
{
terminal_buffer[VGA_WIDTH * (VGA_HEIGHT - 1) + col] = vga_entry(' ', terminal_color);
}
terminal_row--;
}
void vputchar(char c) {
void newline() {
terminal_column = 0;
if (++terminal_row >= VGA_HEIGHT)
vscroll();
}
switch (c) {
default:
vsetchar(c, terminal_color, terminal_column, terminal_row);
terminal_column++;
break;
case '\n':
newline();
break;
case '\t':
// tab width of 4
terminal_column = (terminal_column / 4 + 1) * 4;
break;
case '\b':
if (terminal_column > 0) terminal_column--;
break;
case '\r':
terminal_column = 0;
break;
case '\f':
vclear();
break;
case '\a':
break;
}
if (terminal_column >= VGA_WIDTH)
newline();
}
void _putchar(char c) {
vputchar(c);
}
void vprint(const char* str) {
for (size_t i = 0; i < strlen(str); i++)
vputchar(str[i]);
}
void vputn(const char* str, size_t l) {
for (size_t i = 0; i < l; i++)
vputchar(str[i]);
}
void vlocate(size_t column, size_t row) {
terminal_column = column % VGA_WIDTH;
terminal_row = row % VGA_HEIGHT;
}
|