-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvga.c
144 lines (133 loc) · 3.3 KB
/
vga.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "io.h"
#include "vga.h"
#include "mem.h"
#include "graphics.h"
extern int fontFile; // this is an int because its pointer becomes an int*; i.e. an array.
extern int textMode;
extern Framebuffer image;
int cursorX;
int cursorY;
void setChar(char character, char color, int x, int y) {
if (textMode) {
char* cell = (char*) 0x000b8000 + ((y * 80 + x) * 2);
*cell = character;
*(cell + 1) = color;
}
else {
drawBuffer(x*9, y*16, 9, 16, &fontFile + character*16*9);
}
}
void setVgaRegister3c0(char index, char data) {
in(0x3da);
out(0x3c0, index);
out(0x3c0, data);
}
void setVgaRegister(unsigned short port, char index, char data) {
out(port, index);
out(port + 1, data);
}
char getVgaRegister3c0(char index) {
in(0x3da);
out(0x3c0, index);
return in(0x3c1);
}
char getVgaRegister(unsigned short port, char index) {
out(port, index);
return in(port + 1);
}
int cmpChar(char* position, char character) {
for (int i = 0; i < 16; i++) {
if (!memcmp(position + i*image.width*4, (char*)(&fontFile + character*16*9 + i*9), 9*4)) {
return 0;
}
}
return 1;
}
void setCursor(int x, int y) {
if (textMode) {
short pos = y * 80 + x;
setVgaRegister(0x3d4, 0x0f, (char) (pos & 0xFF));
setVgaRegister(0x3d4, 0x0e, (char) ((pos >> 8) & 0xFF));
}
else {
/*for (int i = 0; i < 25; i++) {
for (int j = 0; i < 80; j++) {
if (cmpChar((char*)(image.data + (i*image.width*16) + (j*9)), ' ')) {
setChar('#', 0x07, j, i);
}
}
}*/
if (cmpChar((char*)(image.data + (cursorY*image.width*16) + (cursorX*9)), '_')) { // if the old cursor position is still shown as a cursor
setChar(' ', 0x07, cursorX, cursorY); // overwrite it with a space
}
cursorX = x;
cursorY = y;
if (cmpChar((char*)(image.data + (cursorY*image.width*16) + (cursorX*9)), ' ')) {
setChar('_', 0x07, cursorX, cursorY);
}
}
}
void enableCursor(char cursor_start, char cursor_end) {
setVgaRegister(0x3d4, 0x0a, (getVgaRegister(0x3d4, 0x0a) & 0xc0) | cursor_start);
setVgaRegister(0x3d4, 0x0b, (getVgaRegister(0x3d4, 0x0b) & 0xe0) | cursor_end);
}
void disableCursor() {
setVgaRegister(0x3d4, 0x0a, 0x20);
}
int currentX = 0;
int currentY = 0;
void clearText(char character, char color) {
for (int i = 0; i < 25; i++){
for (int j = 0; j < 80; j++){
setChar(character, color, j, i);
}
}
currentX = 0;
currentY = 0;
setCursor(0, 0);
}
void writeChar(char character) {
if (character == '\r') {
currentX = 0;
}
else if (character == '\n') {
currentY++;
}
else if (character == '\b') {
currentX--;
}
else {
setChar(character, 0x07, currentX, currentY);
currentX++;
}
if (currentX > 79) {
currentX = 0;
currentY++;
}
if (currentY > 24) {
if (textMode) {
for (int row = 0; row < 24; row++) {
char* thisRow = (char*) 0x000b8000 + 160*row;
char* nextRow = (char*) 0x000b8000 + 160*(row+1);
memcpy(nextRow, thisRow, 160);
}
}
else {
for (int row = 0; row < 24; row++) {
int* thisRow = (int*)image.data + image.width*16*row;
int* nextRow = (int*)image.data + image.width*16*(row+1);
memcpyDwords(nextRow, thisRow, image.width*16);
}
}
for (int x = 0; x < 80; x++) {
setChar(' ', 0x07, x, 24);
}
currentY = 24;
}
setCursor(currentX, currentY);
}
void vgaPrint(char* string) {
for (int i = 0; string[i] != 0; i++) {
writeChar(string[i]);
}
}