-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvga.c
More file actions
113 lines (95 loc) · 3.01 KB
/
vga.c
File metadata and controls
113 lines (95 loc) · 3.01 KB
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
109
110
111
112
113
#include "kstd.h"
#include "vga.h"
/* VGA memory address */
#define VGA_MEMORY 0xB8000
void* mmemset(void* dst, int v, size_t n) {
unsigned char* p = dst;
while (n--) *p++ = (unsigned char)v;
return dst;
}
/* Terminal state */
size_t terminal_row;
size_t terminal_column;
uint8_t terminal_color;
uint16_t* terminal_buffer = (uint16_t*)VGA_MEMORY;
void vga_move_cursor(uint16_t row, uint16_t col) {
uint16_t pos = row * VGA_WIDTH + col;
outb(0x3D4, 0x0F);
outb(0x3D5, (uint8_t)(pos & 0xFF));
outb(0x3D4, 0x0E);
outb(0x3D5, (uint8_t)((pos >> 8) & 0xFF));
}
void terminal_initialize(void) {
terminal_row = 0;
terminal_column = 0;
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
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 terminal_setcolor(uint8_t color) {
terminal_color = color;
}
void terminal_scroll(void) {
for (size_t y = 0; y < VGA_HEIGHT - 1; y++) {
for (size_t x = 0; x < VGA_WIDTH; x++) {
terminal_buffer[y * VGA_WIDTH + x] = terminal_buffer[(y + 1) * VGA_WIDTH + x];
}
}
const size_t last = (VGA_HEIGHT - 1) * VGA_WIDTH;
for (size_t x = 0; x < VGA_WIDTH; x++) {
terminal_buffer[last + x] = vga_entry(' ', terminal_color);
}
}
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y) {
if (y >= VGA_HEIGHT || x >= VGA_WIDTH) return;
const size_t index = y * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(c, color);
}
void terminal_putchar(char c) {
if (c == '\n') {
terminal_row++;
terminal_column = 0;
if (terminal_row == VGA_HEIGHT) {
terminal_scroll();
terminal_row = VGA_HEIGHT - 1;
}
} else {
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
if (++terminal_column == VGA_WIDTH) {
terminal_column = 0;
if (++terminal_row == VGA_HEIGHT) {
terminal_scroll();
terminal_row = VGA_HEIGHT - 1;
}
}
}
vga_move_cursor(terminal_row,terminal_column);
}
void terminal_removechar(void){
if (terminal_column == 0) {
if (terminal_row == 0) return; // top-left, nothing to remove
terminal_row--;
terminal_column = VGA_WIDTH - 1; // 79
} else {
terminal_column--;
}
vga_move_cursor(terminal_row,terminal_column);
// overwrite the previous character with space
terminal_putentryat(' ', terminal_color, terminal_column, terminal_row);
}
void terminal_write(const char* data, size_t size) {
for (size_t i = 0; i < size; i++) {
terminal_putchar(data[i]);
}
}
void terminal_writestring(const char* data) {
terminal_write(data, strlen(data));
}
void femboysay(const char* data){
terminal_writestring("Femboy adham says: ");
terminal_writestring(data);
}