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
|
/*
Copyright 2019 Benji Dial
Permission to use, copy, modify, and/or distribute this
software for any purpose with or without fee is hereby
granted, provided that the above copyright notice and this
permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.
*/
#include "vga.h"
#include "serial.h"
#include <stdbool.h>
uint16_t cursor_pos = 0;
uint8_t color = 0x70;
#define cols 80
#define rows 25
void clear(void) {
uint32_t fill = 0x00200020 | (color << 24) | (color << 8);
for (uint32_t *i = (uint32_t *)VGA_BUFFER; i < (uint32_t *)(VGA_BUFFER + rows * cols * 2); ++i)
*i = fill;
cursor_pos = 0;
}
void scroll(void) {
cursor_pos -= cols * 2;
for (uint32_t *i = (uint32_t *)VGA_BUFFER; i < (uint32_t *)(VGA_BUFFER + (rows - 1) * cols * 2); ++i)
*i = *(i + cols / 2);
uint32_t fill = (color << 8) | (color << 24) | 0x00200020;
for (uint32_t *i = (uint32_t *)(VGA_BUFFER + (rows - 1) * cols * 2); i < (uint32_t *)(VGA_BUFFER + rows * cols * 2); ++i)
*i = fill;
}
void put_char(uint8_t ch) {
send_byte(ch, COM2);
switch (ch) {
case '\b':
if (cursor_pos)
VGA_BUFFER[cursor_pos -= 2] = (uint8_t)' ';
break;
case '\t':
while ((cursor_pos % (cols * 2)) % 20)
put_char(' ');
break;
case '\n':
if ((cursor_pos = (cursor_pos / (cols * 2) + 1) * (cols * 2)) >= cols * rows * 2)
scroll();
break;
default:
VGA_BUFFER[cursor_pos++] = ch;
VGA_BUFFER[cursor_pos++] = color;
if (cursor_pos == (cols * rows * 2))
scroll();
}
}
void put_sz(uint8_t *sz) {
while (*sz)
put_char(*(sz++));
}
void put_16(uint16_t n) {
bool s = false;
if (n / 10000) {
put_char((uint8_t)'0' + n / 10000);
s = true;
}
n %= 10000;
if (n / 1000 || s) {
put_char((uint8_t)'0' + n / 1000);
s = true;
}
n %= 1000;
if (n / 100 || s) {
put_char((uint8_t)'0' + n / 100);
s = true;
}
n %= 100;
if (n / 10 || s) {
put_char((uint8_t)'0' + n / 10);
s = true;
}
put_char((uint8_t)'0' + n % 10);
}
void put_32_hex(uint32_t n) {
for (uint8_t i = 0; i < 4; ++i) {
put_char("0123456789abcdef"[n >> 28]);
n <<= 4;
}
put_char('.');
for (uint8_t i = 0; i < 4; ++i) {
put_char("0123456789abcdef"[n >> 28]);
n <<= 4;
}
}
void move_cursor(uint8_t col, uint8_t row) {
cursor_pos = (col + row * cols) * 2;
}
void set_color(uint8_t c) {
color = c;
}
|