-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.c
186 lines (153 loc) · 3.69 KB
/
screen.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
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
#include <screen.h>
#include <memory.h>
/* how many spaces a full tab should equal */
#define TAB_WIDTH 4
static uint16_t *screen = (uint16_t*)(SCREEN_ADDR << 4);
/* current row */
static uint8_t row = 0;
/* current col */
static uint8_t col = 0;
static inline uint8_t get_color_attribute(uint8_t fg, uint8_t bg)
{
return (bg << 4) | (fg & 0x0F);
}
static inline uint16_t get_text_attribute(char c, uint8_t fg, uint8_t bg)
{
return (get_color_attribute(fg, bg) << 8) | c;
}
/* clears whole screen */
void clear_screen()
{
uint16_t blank = get_text_attribute(' ', WHITE, BLACK);
memsetw(screen, blank, SCREEN_ROWS * SCREEN_COLS);
/*int i;
for (i = 0; i < SCREEN_ROWS * SCREEN_COLS; ++i) {
screen[i] = blank;
}*/
}
/* moves all rows one up */
void scroll() {
/* move everything one row back */
memcpyw(screen, screen + SCREEN_COLS, SCREEN_ROWS * SCREEN_COLS - SCREEN_COLS);
/* set last row to empty character */
uint16_t blank = get_text_attribute(' ', WHITE, BLACK);
memsetw(screen + SCREEN_ROWS * SCREEN_COLS - SCREEN_COLS, blank, SCREEN_COLS);
// uint8_t i;
// uint16_t prev = 0;
// for (i = 1; i < SCREEN_ROWS; i++) {
// /* copy current row to prevous one */
// memcpyw(screen + prev, screen + prev + SCREEN_COLS, SCREEN_COLS);
// prev += SCREEN_COLS;
// }
// /* zero out last row */
// uint16_t c = get_text_attribute(' ', WHITE, BLACK);
// memsetw(screen + prev, c, SCREEN_COLS);
}
/* prints a character at the given position */
void screen_print(char c, uint8_t row, uint8_t col)
{
uint16_t data = get_text_attribute(c, WHITE, BLACK);
screen[row * SCREEN_COLS + col] = data;
}
/* print a character at the current position */
void printc(char s) {
if (row == SCREEN_ROWS) {
scroll();
row = row - 1;
}
if (s == '\n') {
col = 0;
row++;
} else if (s == '\t') {
uint8_t spaces = TAB_WIDTH - col % TAB_WIDTH;
uint8_t end = col + spaces;
/* make sure we don't cross any rows */
if (end > SCREEN_COLS) {
end = SCREEN_COLS;
}
uint8_t i;
for (i = col; i < end; i++) {
screen_print(' ', row, col++);
}
} else if (s >= 0x20 && s <= 0x7E ) {
/* print everything that you can type on a keyboard ... */
screen_print(s, row, col++);
}
if (col == SCREEN_COLS) {
col = 0;
row++;
}
}
/* print a null-byte terminated string at the current position */
void printstr(char* s)
{
while (*s) {
printc(*s++);
}
}
/* print a "len" characters at the current position */
void printstrl(char* s, uint8_t len) {
uint8_t k;
for (k = 0; k < len; k++) {
printc(s[k]);
}
}
void printint(uint8_t k) {
char c = '0' + k;
printc(c);
}
uint32_t number_of_digits(uint32_t k) {
uint32_t p = 10, q = 1;
while (k > p) {
q++;
p = p * 10;
}
return q;
}
void printk(uint32_t k) {
uint32_t p, q;
/* there's max. 10 digits in a 32 bit int */
char buf[10];
memset((uint8_t*)buf, 0, 10);
q = 0;
do {
p = k % 10;
k = k / 10;
buf[10 - (q++) - 1] = '0' + p;
} while (k > 0);
for (p = 0; p < 10; p++) {
if (buf[p] == 0) {
continue;
}
printc(buf[p]);
}
}
void printhl(uint32_t dword) {
/* print string representation of "word" as hex */
char* alphabet = "0123456789ABCDEF";
/* max. 8 nibbles in a 32 bit int */
char buf[8];
uint8_t p, q = 0;
do {
buf[8 - (q++) - 1] = alphabet[dword % 16];
dword = dword / 16;
} while (q < 8);
for (p = 0; p < q; p++) {
printc(buf[p]);
}
}
void printhw(uint16_t word) {
printhl((uint32_t)word);
}
void printhb(uint8_t byte) {
printhl((uint32_t)byte);
}
void printl(uint32_t l) {
printk(l);
}
void printw(uint16_t word) {
printk((uint32_t)word);
}
void printb(uint8_t byte) {
printk((uint32_t)byte);
}