-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvga.c
More file actions
190 lines (166 loc) · 4.39 KB
/
vga.c
File metadata and controls
190 lines (166 loc) · 4.39 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "vga.h"
/*
See Intel® OpenSource HD Graphics PRM pdf file
for following defined data for each vga register
and its explaination
*/
uint8* vga_buffer;
static uint8 seq_data[5] = {0x03, 0x01, 0x0F, 0x00, 0x0E};
static uint8 crtc_data[25] = {0x5F, 0x4F, 0x50, 0x82,
0x54, 0x80, 0xBF, 0x1F,
0x00, 0x41, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x9C, 0x0E, 0x8F, 0x28,
0x40, 0x96, 0xB9, 0xA3,
0xFF};
static uint8 gc_data[9] = {0x00, 0x00, 0x00, 0x00,
0x00, 0x40, 0x05, 0x0F,
0xFF};
static uint8 ac_data[21] = {0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F,
0x41, 0x00, 0x0F, 0x00,
0x00};
uint8 inb(uint16 port)
{
uint8 data;
asm volatile("inb %1, %0" : "=a"(data) : "Nd"(port));
return data;
}
void outb(uint16 port, uint8 data)
{
asm volatile("outb %0, %1" : : "a"(data), "Nd"(port));
}
void set_misc()
{
outb(VGA_MISC_WRITE, 0x63);
}
void set_seq()
{
// write sequence data to index of 0-4
for(uint8 index = 0; index < 5; index++){
// first set index
outb(VGA_SEQ_INDEX, index);
// write data at that index
outb(VGA_SEQ_DATA, seq_data[index]);
}
}
void set_crtc()
{
// write crtc data to index of 0-24
for(uint8 index = 0; index < 25; index++){
outb(VGA_CRTC_INDEX, index);
outb(VGA_CRTC_DATA, crtc_data[index]);
}
}
void set_gc()
{
// write gc data to index of 0-8
for(uint8 index = 0; index < 9; index++){
outb(VGA_GC_INDEX, index);
outb(VGA_GC_DATA, gc_data[index]);
}
}
void set_ac()
{
uint8 d;
// write ac data to index of 0-20
for(uint8 index = 0; index < 21; index++){
outb(VGA_AC_INDEX, index);
outb(VGA_AC_WRITE, ac_data[index]);
}
d = inb(VGA_INSTAT_READ);
outb(VGA_AC_INDEX, d | 0x20);
}
void clear_screen()
{
for(uint32 index = 0; index < VGA_MAX; index++)
vga_buffer[index] = 0;
}
void clear_color(uint8 color)
{
for(uint32 index = 0; index < VGA_MAX; index++)
vga_buffer[index] = color;
}
void init_vga()
{
set_misc();
set_seq();
set_crtc();
set_gc();
set_ac();
vga_buffer = (uint8*)VGA_ADDRESS;
clear_screen();
}
void putpixel(uint16 x, uint16 y, uint8 color)
{
uint32 index = 0;
index = 320 * y + x;
if(index < VGA_MAX)
vga_buffer[index] = color;
}
void draw_line(uint16 x1, uint16 y1, uint16 x2, uint16 y2, uint8 color)
{
if(y1 == y2){
for(uint16 i = x1; i <= x2; i++)
putpixel(i, y1, color);
return;
}
if(x1 == x2){
for(uint16 i = y1; i <= y2; i++){
putpixel(x1, i, color);
}
return;
}
}
void draw_rect(uint16 x, uint16 y, uint16 width, uint16 height, uint8 color)
{
draw_line(x, y, x, y + height, color);
draw_line(x, y, x + width, y, color);
draw_line(x + width, y, x + width, y + height, color);
draw_line(x, y + height, x + width, y + height, color);
}
void draw_bresenham_circle(int xc, int yc, int x, int y, uint8 color)
{
putpixel(xc+x, yc+y, color);
putpixel(xc-x, yc+y, color);
putpixel(xc+x, yc-y, color);
putpixel(xc-x, yc-y, color);
putpixel(xc+y, yc+x, color);
putpixel(xc-y, yc+x, color);
putpixel(xc+y, yc-x, color);
putpixel(xc-y, yc-x, color);
}
void draw_circle(uint16 x, uint16 y, uint16 radius, uint8 color)
{
int x2 = 0, y2 = radius;
int d = 3 - 2 * radius;
draw_bresenham_circle(x, y, x2, y2, color);
while(y2 >= x2){
x2++;
if(d > 0){
y2--;
d = d + 4 * (x2 - y2) + 10;
}else{
d = d + 4 * x2 + 6;
}
draw_bresenham_circle(x, y, x2, y2, color);
}
}
void draw_diamond(uint16 x, uint16 y, uint16 radius, uint8 color)
{
uint16 x2 = 0, y2 = radius;
uint16 d = 3 - 2 * radius;
draw_bresenham_circle(x, y, x2, y2, color);
while(y2 >= x2){
x2++;
if(d > 0){
y2--;
d = d + 4 * (x2 - y2) + 10;
}else{
d = d + 4 * x2 + 6;
}
draw_bresenham_circle(x, y, x2, y2, color);
}
}