-
Notifications
You must be signed in to change notification settings - Fork 1
/
term.cpp
308 lines (269 loc) · 8.15 KB
/
term.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "hpp/term.hpp"
#include <stdarg.h>
#include "hpp/typ.hpp"
namespace term {
static int posX;
static int posY;
static volatile char* vmem_woffset;
void clear() {
vmem_woffset = vmem_base;
for (int x = 0; x < width; ++x)
for (int y = 0; y < height; ++y) {
*vmem_woffset++ = ' ';
*vmem_woffset++ = 7;
}
posX = 0;
posY = 0;
vmem_woffset = vmem_base;
}
void clearLine() {
moveAbs(0, posY);
for (int x = 0; x < width; ++x) {
*vmem_woffset++ = ' ';
*vmem_woffset++ = 7;
}
moveAbs(0, posY);
}
void init() {
posX = 0;
posY = 0;
vmem_woffset = vmem_base;
}
int getX() {
return posX;
}
int getY() {
return posY;
}
void moveAbs(int x, int y) {
posX = x%width;
posY = y%height + x/width;
vmem_woffset = vmem_base + (posY*width + posX)*2; // (char, style)
}
void moveRel(int dx, int dy) {
moveAbs(posX+dx, posY+dy);
}
void nl() {
moveAbs(0, posY+1);
}
void putch(const char c) {
if (c == '\n') {
nl();
return;
}
else if (c == '\r') {
moveAbs(0, posY);
return;
}
*vmem_woffset++ = c;
*vmem_woffset++ = 7;
moveRel(1, 0);
}
void puts(const char* str) {
while (*str != '\0')
putch(*str++);
}
constexpr char lcase_alphabet[] = "0123456789abcdef";
constexpr char ucase_alphabet[] = "0123456789ABCDEF";
template<class T, const char* alphabet = lcase_alphabet>
typ::enable_if< typ::unsigned_::pred<T> > putnum(T x, const T base = 10) {
// if (base == 16)
// puts("0x");
// else if (base == 2)
// puts("0b");
if (x == 0)
putch('0');
else {
// if (base == 8)
// putch('0');
volatile char* start = vmem_woffset;
for (;x > 0; x /= base)
putch(alphabet[x%base]);
// reverse the number
volatile char* cur = vmem_woffset-2; /*take last char*/
while (cur > start) {
char tmp = *cur;
*cur = *start;
*start = tmp;
cur -= 2; /*(char, style)*/
start += 2;
}
}
}
template<class T, const char* alphabet = lcase_alphabet>
typ::enable_if< typ::signed_::pred<T> > putnum(const T x, const T base = 10) {
using unsignedT = typ::unsigned_::make<T>;
if (x >= 0) {
putnum<unsignedT, alphabet>(static_cast<unsignedT>(x), static_cast<unsignedT>(base));
return;
}
putch('-');
putnum<unsignedT, alphabet>(static_cast<unsignedT>(-x), static_cast<unsignedT>(base));
}
template void putnum<signed char>(signed char x, const signed char base);
template void putnum<short>(short x, const short base);
template void putnum<int>(int x, const int base);
template void putnum<long>(long x, const long base);
// template void putnum<long long>(long long x, const long long base);
template void putnum<unsigned char>(unsigned char x, const unsigned char base);
template void putnum<unsigned short>(unsigned short x, const unsigned short base);
template void putnum<unsigned int>(unsigned int x, const unsigned int base);
template void putnum<unsigned long>(unsigned long x, const unsigned long base);
// template void putnum<unsigned long long>(unsigned long long x, const unsigned long long base);
void putsln(const char* str) {
puts(str);
putch('\n');
}
void printf(const char* fmt...) {
va_list args;
va_start(args, fmt);
for (;*fmt != '\0'; ++fmt) {
if (*fmt == '%') {
++fmt;
bool ljustify = false;
bool force_sign = false;
bool alt = false;
bool padw0 = false;
while (true) {
if (*fmt == '-')
ljustify = true;
else if (*fmt == '+')
force_sign = true;
else if (*fmt == '#')
alt = true;
else if (*fmt == '0')
padw0 = true;
else break;
++fmt;
}
int minw = -1;
if (*fmt == '*') {
minw = va_arg(args, int);
++fmt;
}
else if ('0' <= *fmt && *fmt <= '9') {
minw = 0;
while ('0' <= *fmt && *fmt <= '9') {
minw *= 10;
minw += *fmt - '0';
++fmt;
}
}
int prec = -1;
if (*fmt == '.') {
prec = 0;
++fmt;
if (*fmt == '*') {
prec = va_arg(args, int);
++fmt;
}
else
while ('0' <= *fmt && *fmt <= '9') {
prec *= 10;
prec += *fmt - '0';
++fmt;
}
}
bool hh = false;
bool h = false;
bool l = false;
bool ll = false;
if (*fmt == 'h') {
h = true;
++fmt;
if (*fmt == 'h') {
hh = true;
++fmt;
}
}
else if (*fmt == 'l') {
l = true;
++fmt;
if (*fmt == 'l') {
ll = true;
++fmt;
}
}
//
// note: va_args: types < int as targets are undefbeh
//
if (*fmt == '%')
putch('%');
else if (*fmt == 'c')
putch( static_cast<const char>(va_arg(args, int)) );
else if (*fmt == 's')
puts(va_arg(args, const char*));
else if (*fmt == 'd' || *fmt == 'i') {
if (hh)
putnum<signed char>( static_cast<signed char>(va_arg(args, int)) );
else if (h)
putnum<short>( static_cast<short>(va_arg(args, int)) );
// else if (ll)
// putnum<long long>(va_arg(args, long long));
else if (l)
putnum<long>(va_arg(args, long));
else
putnum<int>(va_arg(args, int));
}
else if (*fmt == 'o') {
if (hh)
putnum<unsigned char>( static_cast<unsigned char>(va_arg(args, unsigned int)), 8);
else if (h)
putnum<unsigned short>( static_cast<unsigned short>(va_arg(args, unsigned int)), 8);
// else if (ll)
// putnum<unsigned long long>(va_arg(args, unsigned long long), 8);
else if (l)
putnum<unsigned long>(va_arg(args, unsigned long), 8);
else
putnum<unsigned int>(va_arg(args, unsigned int), 8);
}
else if (*fmt == 'x' || *fmt == 'p') {
if (hh)
putnum<unsigned char>( static_cast<unsigned char>(va_arg(args, unsigned int)), 16);
else if (h)
putnum<unsigned short>( static_cast<unsigned short>(va_arg(args, unsigned int)), 16);
// else if (ll)
// putnum<unsigned long long>(va_arg(args, unsigned long long), 16);
else if (l)
putnum<unsigned long>(va_arg(args, unsigned long), 16);
else
putnum<unsigned int>(va_arg(args, unsigned int), 16);
}
else if (*fmt == 'X') {
if (hh)
putnum<unsigned char, ucase_alphabet>( static_cast<unsigned char>(va_arg(args, unsigned int)), 16);
else if (h)
putnum<unsigned short, ucase_alphabet>( static_cast<unsigned short>(va_arg(args, unsigned int)), 16);
// else if (ll)
// putnum<unsigned long long, ucase_alphabet>(va_arg(args, unsigned long long), 16);
else if (l)
putnum<unsigned long, ucase_alphabet>(va_arg(args, unsigned long), 16);
else
putnum<unsigned int, ucase_alphabet>(va_arg(args, unsigned int), 16);
}
else if (*fmt == 'u') {
if (hh)
putnum<unsigned char>( static_cast<unsigned char>(va_arg(args, unsigned int)) );
else if (h)
putnum<unsigned short>( static_cast<unsigned short>(va_arg(args, unsigned int)) );
// else if (ll)
// putnum<unsigned long long>(va_arg(args, unsigned long long));
else if (l)
putnum<unsigned long>(va_arg(args, unsigned long));
else
putnum<unsigned int>(va_arg(args, unsigned int));
}
// else // invalid format character
}
else
putch(*fmt);
}
va_end(args);
}
void panicln(const char* str) {
asm volatile("cli" : : );
puts("PANIC: ");
putsln(str);
asm volatile("hlt" : : );
}
}