MicrOS
vga.h
Go to the documentation of this file.
1 #ifndef VGA_H
2 #define VGA_H
3 
4 #include <stdint.h>
5 #include "assembly/io.h"
6 
7 // Legacy shit, please switch to 03h defines or proper to current graphic mode
8 // TODO: Remove this
9 #define VGA_COLOR_BLACK 0x00
10 #define VGA_COLOR_BLUE 0x01
11 #define VGA_COLOR_GREEN 0x02
12 #define VGA_COLOR_CYAN 0x03
13 #define VGA_COLOR_RED 0x04
14 #define VGA_COLOR_MAGENTA 0x05
15 #define VGA_COLOR_BROWN 0x06
16 #define VGA_COLOR_LIGHT_GRAY 0x07
17 #define VGA_COLOR_DARK_GRAY 0x08
18 #define VGA_COLOR_LIGHT_BLUE 0x09
19 #define VGA_COLOR_LIGHT_GREEN 0x0A
20 #define VGA_COLOR_LIGHT_CYAN 0x0B
21 #define VGA_COLOR_LIGHT_RED 0x0C
22 #define VGA_COLOR_LIGHT_MAGENTA 0x0D
23 #define VGA_COLOR_YELLOW 0x0E
24 #define VGA_COLOR_WHITE 0x0F
25 #define VGA_NOT_BLINK 0x00
26 #define VGA_BLINK 0x01
27 
30 {
31  VGA_MODE_00H = 0x00,
32  VGA_MODE_01H = 0x01,
33  VGA_MODE_02H = 0x02,
34  VGA_MODE_03H = 0x03,
35  VGA_MODE_07H = 0x07
36 };
37 
38 // Mode 00h
39 
41 #define VGA_MODE_00H_BASE_ADDR 0xC00B8000
42 #define VGA_MODE_00H_SCREEN_COLUMNS 40
44 #define VGA_MODE_00H_SCREEN_ROWS 25
46 
47 #define VGA_MODE_00H_COLOR_BLACK 0x00
48 #define VGA_MODE_00H_COLOR_BLUE 0x01
49 #define VGA_MODE_00H_COLOR_GREEN 0x02
50 #define VGA_MODE_00H_COLOR_CYAN 0x03
51 #define VGA_MODE_00H_COLOR_RED 0x04
52 #define VGA_MODE_00H_COLOR_MAGENTA 0x05
53 #define VGA_MODE_00H_COLOR_BROWN 0x06
54 #define VGA_MODE_00H_COLOR_LIGHT_GRAY 0x07
55 #define VGA_MODE_00H_COLOR_DARK_GRAY 0x08
56 #define VGA_MODE_00H_COLOR_LIGHT_BLUE 0x09
57 #define VGA_MODE_00H_COLOR_LIGHT_GREEN 0x0A
58 #define VGA_MODE_00H_COLOR_LIGHT_CYAN 0x0B
59 #define VGA_MODE_00H_COLOR_LIGHT_RED 0x0C
60 #define VGA_MODE_00H_COLOR_LIGHT_MAGENTA 0x0D
61 #define VGA_MODE_00H_COLOR_YELLOW 0x0E
62 #define VGA_MODE_00H_COLOR_WHITE 0x0F
63 #define VGA_MODE_00H_NOT_BLINK 0x00
64 #define VGA_MODE_00H_BLINK 0x01
65 
66 #define VGA_MODE_00H_MAX_SCREENS 8
67 #define VGA_MODE_00H_SCREEN_OFFSET 4000
68 
69 // Mode 01h
70 
72 #define VGA_MODE_01H_BASE_ADDR 0xC00B8000
73 #define VGA_MODE_01H_SCREEN_COLUMNS 40
75 #define VGA_MODE_01H_SCREEN_ROWS 25
77 
78 #define VGA_MODE_01H_COLOR_BLACK 0x00
79 #define VGA_MODE_01H_COLOR_BLUE 0x01
80 #define VGA_MODE_01H_COLOR_GREEN 0x02
81 #define VGA_MODE_01H_COLOR_CYAN 0x03
82 #define VGA_MODE_01H_COLOR_RED 0x04
83 #define VGA_MODE_01H_COLOR_MAGENTA 0x05
84 #define VGA_MODE_01H_COLOR_BROWN 0x06
85 #define VGA_MODE_01H_COLOR_LIGHT_GRAY 0x07
86 #define VGA_MODE_01H_COLOR_DARK_GRAY 0x08
87 #define VGA_MODE_01H_COLOR_LIGHT_BLUE 0x09
88 #define VGA_MODE_01H_COLOR_LIGHT_GREEN 0x0A
89 #define VGA_MODE_01H_COLOR_LIGHT_CYAN 0x0B
90 #define VGA_MODE_01H_COLOR_LIGHT_RED 0x0C
91 #define VGA_MODE_01H_COLOR_LIGHT_MAGENTA 0x0D
92 #define VGA_MODE_01H_COLOR_YELLOW 0x0E
93 #define VGA_MODE_01H_COLOR_WHITE 0x0F
94 #define VGA_MODE_01H_NOT_BLINK 0x00
95 #define VGA_MODE_01H_BLINK 0x01
96 
97 #define VGA_MODE_01H_MAX_SCREENS 8
98 #define VGA_MODE_01H_SCREEN_OFFSET 4000
99 
100 // Mode 02h
101 
103 #define VGA_MODE_02H_BASE_ADDR 0xC00B8000
104 #define VGA_MODE_02H_SCREEN_COLUMNS 80
106 #define VGA_MODE_02H_SCREEN_ROWS 25
108 
109 #define VGA_MODE_02H_COLOR_BLACK 0x00
110 #define VGA_MODE_02H_COLOR_BLUE 0x01
111 #define VGA_MODE_02H_COLOR_GREEN 0x02
112 #define VGA_MODE_02H_COLOR_CYAN 0x03
113 #define VGA_MODE_02H_COLOR_RED 0x04
114 #define VGA_MODE_02H_COLOR_MAGENTA 0x05
115 #define VGA_MODE_02H_COLOR_BROWN 0x06
116 #define VGA_MODE_02H_COLOR_LIGHT_GRAY 0x07
117 #define VGA_MODE_02H_COLOR_DARK_GRAY 0x08
118 #define VGA_MODE_02H_COLOR_LIGHT_BLUE 0x09
119 #define VGA_MODE_02H_COLOR_LIGHT_GREEN 0x0A
120 #define VGA_MODE_02H_COLOR_LIGHT_CYAN 0x0B
121 #define VGA_MODE_02H_COLOR_LIGHT_RED 0x0C
122 #define VGA_MODE_02H_COLOR_LIGHT_MAGENTA 0x0D
123 #define VGA_MODE_02H_COLOR_YELLOW 0x0E
124 #define VGA_MODE_02H_COLOR_WHITE 0x0F
125 #define VGA_MODE_02H_NOT_BLINK 0x00
126 #define VGA_MODE_02H_BLINK 0x01
127 
128 #define VGA_MODE_02H_MAX_SCREENS 8
129 #define VGA_MODE_02H_SCREEN_OFFSET 4000
130 
131 // Mode 03h
132 
134 #define VGA_MODE_03H_BASE_ADDR 0xC00B8000
135 #define VGA_MODE_03H_SCREEN_COLUMNS 80
137 #define VGA_MODE_03H_SCREEN_ROWS 25
139 
140 #define VGA_MODE_03H_COLOR_BLACK 0x00
141 #define VGA_MODE_03H_COLOR_BLUE 0x01
142 #define VGA_MODE_03H_COLOR_GREEN 0x02
143 #define VGA_MODE_03H_COLOR_CYAN 0x03
144 #define VGA_MODE_03H_COLOR_RED 0x04
145 #define VGA_MODE_03H_COLOR_MAGENTA 0x05
146 #define VGA_MODE_03H_COLOR_BROWN 0x06
147 #define VGA_MODE_03H_COLOR_LIGHT_GRAY 0x07
148 #define VGA_MODE_03H_COLOR_DARK_GRAY 0x08
149 #define VGA_MODE_03H_COLOR_LIGHT_BLUE 0x09
150 #define VGA_MODE_03H_COLOR_LIGHT_GREEN 0x0A
151 #define VGA_MODE_03H_COLOR_LIGHT_CYAN 0x0B
152 #define VGA_MODE_03H_COLOR_LIGHT_RED 0x0C
153 #define VGA_MODE_03H_COLOR_LIGHT_MAGENTA 0x0D
154 #define VGA_MODE_03H_COLOR_YELLOW 0x0E
155 #define VGA_MODE_03H_COLOR_WHITE 0x0F
156 #define VGA_MODE_03H_NOT_BLINK 0x00
157 #define VGA_MODE_03H_BLINK 0x01
158 
159 #define VGA_MODE_03H_MAX_SCREENS 8
160 #define VGA_MODE_03H_SCREEN_OFFSET 4000
161 
162 // Mode 07h
163 
165 #define VGA_MODE_07H_BASE_ADDR 0xC00B0000
166 #define VGA_MODE_07H_SCREEN_COLUMNS 80
168 #define VGA_MODE_07H_SCREEN_ROWS 25
170 
171 #define VGA_MODE_07H_COLOR_BLACK 0x00
172 #define VGA_MODE_07H_COLOR_BLUE 0x01
173 #define VGA_MODE_07H_COLOR_GREEN 0x02
174 #define VGA_MODE_07H_COLOR_CYAN 0x03
175 #define VGA_MODE_07H_COLOR_RED 0x04
176 #define VGA_MODE_07H_COLOR_MAGENTA 0x05
177 #define VGA_MODE_07H_COLOR_BROWN 0x06
178 #define VGA_MODE_07H_COLOR_LIGHT_GRAY 0x07
179 #define VGA_MODE_07H_COLOR_DARK_GRAY 0x08
180 #define VGA_MODE_07H_COLOR_LIGHT_BLUE 0x09
181 #define VGA_MODE_07H_COLOR_LIGHT_GREEN 0x0A
182 #define VGA_MODE_07H_COLOR_LIGHT_CYAN 0x0B
183 #define VGA_MODE_07H_COLOR_LIGHT_RED 0x0C
184 #define VGA_MODE_07H_COLOR_LIGHT_MAGENTA 0x0D
185 #define VGA_MODE_07H_COLOR_YELLOW 0x0E
186 #define VGA_MODE_07H_COLOR_WHITE 0x0F
187 #define VGA_MODE_07H_NOT_BLINK 0x00
188 #define VGA_MODE_07H_BLINK 0x01
189 
190 #define VGA_MODE_07H_MAX_SCREENS 1
191 #define VGA_MODE_07H_SCREEN_OFFSET 4000
192 
194 typedef struct vga_screen_pos
195 {
197  uint16_t x;
199  uint16_t y;
201 
203 
205 typedef struct vga_color_with_blink
206 {
208  unsigned char letter : 4;
210  unsigned char background : 3;
212  unsigned char blikning : 1;
214 
216 
219 {
221  unsigned char letter : 4;
223  unsigned char background : 4;
225 
227 
229 typedef union vga_color {
235  uint8_t value;
236 } vga_color;
237 
239 
240 typedef struct vga_character
241 {
243  unsigned char ascii_code;
246 } vga_character;
247 
249 
251 typedef union screen_char {
253  uint16_t value;
256 } screen_char;
257 
258 // Functions for users
259 
261 
266 uint8_t vga_init(uint8_t mode);
267 
269 
273 void vga_printchar(char character);
274 
276 
281 void vga_printchar_color(char character, vga_color *color);
282 
284 
288 void vga_printstring(const char *str);
289 
291 
296 void vga_printstring_color(const char *str, vga_color *color);
297 
299 
305 void vga_set_char(uint16_t x, uint16_t y, char character);
306 
308 
313 void vga_set_char_struct(vga_screen_pos spos, char character);
314 
316 
322 char vga_get_char(uint16_t x, uint16_t y);
323 
325 
331 
333 
339 void vga_set_color(uint16_t x, uint16_t y, vga_color col);
340 
342 
348 
350 
356 union vga_color vga_get_color(uint16_t x, uint16_t y);
357 
359 
365 
367 
373 void vga_set_character(uint16_t x, uint16_t y, vga_character character);
374 
376 
382 
384 
390 vga_character vga_get_character(uint16_t x, uint16_t y);
391 
393 
399 
401 
406 void vga_set_cursor_pos(uint16_t x, uint16_t y);
407 
409 
414 
416 
421 
423 
426 void vga_clear_screen();
427 
429 
433 void vga_clear_given_screen(uint8_t screen);
434 
436 
439 void vga_clear_all_screens();
440 
442 
446 void vga_change_printing_screen(uint8_t screen);
447 
449 
454 void vga_copy_screen(uint8_t from, uint8_t to);
455 
457 
460 void vga_cursor_on();
461 
463 
465 void vga_cursor_off();
466 
468 
470 void vga_newline();
471 
472 // Helpers
473 
475 
481 uint16_t __vga_calcualte_position_with_offset(uint16_t x, uint16_t y);
482 
484 
490 uint16_t __vga_calcualte_position_without_offset(uint16_t x, uint16_t y);
491 
493 
498 void __vga_enable_cursor(uint8_t cursor_start, uint8_t cursor_end);
499 
501 
503 void __vga_disable_cursor();
504 
506 
512 
513 #endif
Define one character on screen.
Definition: vga.h:251
VGA mode 02h.
Definition: vga.h:33
uint16_t __vga_calcualte_position_with_offset(uint16_t x, uint16_t y)
Calculate position with offset.
Definition: vga.c:394
vga_character vga_get_character(uint16_t x, uint16_t y)
Get character and character color color from given point on screen.
Definition: vga.c:230
void vga_printchar_color(char character, vga_color *color)
Print character on screen.
Definition: vga.c:129
void vga_set_color(uint16_t x, uint16_t y, vga_color col)
Set character color on given point on screen.
Definition: vga.c:197
vga_character character
Struct contains ASCII code and color data.
Definition: vga.h:255
void vga_set_char_struct(vga_screen_pos spos, char character)
Set character on given point on screen.
Definition: vga.c:181
void vga_printstring(const char *str)
Print string on screen.
Definition: vga.c:155
void vga_clear_screen()
Clear screen.
Definition: vga.c:270
void vga_newline()
Print new line.
Definition: vga.c:361
void vga_set_character(uint16_t x, uint16_t y, vga_character character)
Set character and character color on given point on screen.
Definition: vga.c:219
uint16_t x
Position x on screen.
Definition: vga.h:197
vga_screen_pos vga_get_cursor_pos()
Get cursor position.
Definition: vga.c:257
VGA mode 01h.
Definition: vga.h:32
void __vga_enable_cursor(uint8_t cursor_start, uint8_t cursor_end)
Turn on cursor.
Definition: vga.c:404
vga_color_with_blink color_with_blink
Uses to define color in blinking mode.
Definition: vga.h:231
void vga_set_cursor_pos_struct(vga_screen_pos spos)
Set cursor position.
Definition: vga.c:251
vga_color_without_blink color_without_blink
Uses to define color in non blinking mode.
Definition: vga.h:233
uint16_t __vga_calcualte_position_without_offset(uint16_t x, uint16_t y)
Calculate position without offset.
Definition: vga.c:399
Struct that defines position on screen.
Definition: vga.h:194
uint8_t vga_init(uint8_t mode)
Initialization of VGA.
Definition: vga.c:33
void vga_printchar(char character)
Print character on screen.
Definition: vga.c:103
void vga_set_color_struct(vga_screen_pos spos, vga_color col)
Set character color on given point on screen.
Definition: vga.c:203
VGA mode 00h.
Definition: vga.h:31
vga_color __vga_get_default_terminal_color(uint8_t vga_current_mode)
Get default terminal color.
Definition: vga.c:419
VGA mode 07h.
Definition: vga.h:35
void vga_clear_given_screen(uint8_t screen)
Clear given screen.
Definition: vga.c:291
vga_character vga_get_character_struct(vga_screen_pos spos)
Get character and character color color from given point on screen.
Definition: vga.c:236
uint8_t value
Uses to define color as value.
Definition: vga.h:235
uint8_t vga_current_mode
Current text mode.
Definition: vga.c:19
Struct that define one character.
Definition: vga.h:240
void vga_set_char(uint16_t x, uint16_t y, char character)
Set character on given point on screen.
Definition: vga.c:175
void vga_cursor_on()
Turn on cursor.
Definition: vga.c:351
void vga_copy_screen(uint8_t from, uint8_t to)
Copy printing screen.
Definition: vga.c:330
void vga_set_character_struct(vga_screen_pos spos, vga_character character)
Set character and character color on given point on screen.
Definition: vga.c:225
void vga_printstring_color(const char *str, vga_color *color)
Print string on screen.
Definition: vga.c:165
void __vga_disable_cursor()
Disable cursor.
Definition: vga.c:413
uint16_t value
16 bit value of field.
Definition: vga.h:253
void vga_set_cursor_pos(uint16_t x, uint16_t y)
Set cursor position.
Definition: vga.c:241
void vga_clear_all_screens()
Clear all screen.
Definition: vga.c:312
char vga_get_char_struct(vga_screen_pos spos)
Get character from given point on screen.
Definition: vga.c:192
vga_color color
Color of letter and background. In blinking mode also include blinking flag.
Definition: vga.h:245
Color of one character.
Definition: vga.h:229
union vga_color vga_get_color_struct(vga_screen_pos spos)
Get character color from given point on screen.
Definition: vga.c:214
VGA mode 03h.
Definition: vga.h:34
void vga_change_printing_screen(uint8_t screen)
Change printing screen.
Definition: vga.c:321
union vga_color vga_get_color(uint16_t x, uint16_t y)
Get character color from given point on screen.
Definition: vga.c:208
unsigned char ascii_code
ASCII code of letter.
Definition: vga.h:243
char vga_get_char(uint16_t x, uint16_t y)
Get character from given point on screen.
Definition: vga.c:186
VGA_TEXT_MODES
Enum represent available VGA text modes.
Definition: vga.h:29
static char mode
Definition: vga_gmode.c:20
void vga_cursor_off()
Turn off cursor.
Definition: vga.c:356
uint16_t y
Position y on screen.
Definition: vga.h:199