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
309
310
311
312
313
314
315
316
317
318
|
/**
* @file fault_drawer.c
*
* Implements routines for drawing text with a fixed font directly to a framebuffer, used in displaying
* the crash screen implemented by fault.c
*/
#include "fault.h"
#include "macros.h"
#include "libc64/aprintf.h"
#include "libc/stdarg.h"
#include "libc/stdbool.h"
#include "terminal.h"
typedef struct {
/* 0x00 */ u16* fb;
/* 0x04 */ u16 w;
/* 0x06 */ u16 h;
/* 0x08 */ u16 yStart;
/* 0x0A */ u16 yEnd;
/* 0x0C */ u16 xStart;
/* 0x0E */ u16 xEnd;
/* 0x10 */ u16 foreColor;
/* 0x12 */ u16 backColor;
/* 0x14 */ u16 cursorX;
/* 0x16 */ u16 cursorY;
/* 0x18 */ const u32* fontData;
/* 0x1C */ u8 charW;
/* 0x1D */ u8 charH;
/* 0x1E */ s8 charWPad;
/* 0x1F */ s8 charHPad;
/* 0x20 */ u16 printColors[10];
/* 0x34 */ u8 escCode; // bool
/* 0x35 */ u8 osSyncPrintfEnabled;
/* 0x38 */ void (*inputCallback)(void);
} FaultDrawer; // size = 0x3C
// TODO: Extract sFaultDrawerFont
extern const u32 sFaultDrawerFont[];
#pragma GLOBAL_ASM("asm/jp/nonmatchings/boot/fault_drawer/sFaultDrawerFont.s")
#define FAULT_DRAWER_CURSOR_X 22
#define FAULT_DRAWER_CURSOR_Y 16
FaultDrawer sFaultDrawerDefault = {
(u16*)(PHYS_TO_K0(0x400000) - sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH])),
SCREEN_WIDTH,
SCREEN_HEIGHT,
FAULT_DRAWER_CURSOR_Y,
SCREEN_HEIGHT - FAULT_DRAWER_CURSOR_Y - 1,
FAULT_DRAWER_CURSOR_X,
SCREEN_WIDTH - FAULT_DRAWER_CURSOR_X - 1,
GPACK_RGBA5551(255, 255, 255, 1),
GPACK_RGBA5551(0, 0, 0, 0),
FAULT_DRAWER_CURSOR_X,
FAULT_DRAWER_CURSOR_Y,
sFaultDrawerFont,
8,
8,
0,
0,
{
GPACK_RGBA5551(0, 0, 0, 1),
GPACK_RGBA5551(255, 0, 0, 1),
GPACK_RGBA5551(0, 255, 0, 1),
GPACK_RGBA5551(255, 255, 0, 1),
GPACK_RGBA5551(0, 0, 255, 1),
GPACK_RGBA5551(255, 0, 255, 1),
GPACK_RGBA5551(0, 255, 255, 1),
GPACK_RGBA5551(255, 255, 255, 1),
GPACK_RGBA5551(120, 120, 120, 1),
GPACK_RGBA5551(176, 176, 176, 1),
},
false,
false,
NULL,
};
FaultDrawer sFaultDrawer;
void FaultDrawer_SetOsSyncPrintfEnabled(u32 enabled) {
sFaultDrawer.osSyncPrintfEnabled = enabled;
}
void FaultDrawer_DrawRecImpl(s32 xStart, s32 yStart, s32 xEnd, s32 yEnd, u16 color) {
u16* fb;
s32 x;
s32 y;
s32 xDiff = sFaultDrawer.w - xStart;
s32 yDiff = sFaultDrawer.h - yStart;
s32 xSize = xEnd - xStart + 1;
s32 ySize = yEnd - yStart + 1;
if ((xDiff > 0) && (yDiff > 0)) {
if (xDiff < xSize) {
xSize = xDiff;
}
if (yDiff < ySize) {
ySize = yDiff;
}
fb = sFaultDrawer.fb + sFaultDrawer.w * yStart + xStart;
for (y = 0; y < ySize; y++) {
for (x = 0; x < xSize; x++) {
*fb++ = color;
}
fb += sFaultDrawer.w - xSize;
}
osWritebackDCacheAll();
}
}
void FaultDrawer_DrawChar(char c) {
FaultDrawer* faultDrawer = &sFaultDrawer;
s32 x;
s32 y;
u32 data;
s32 cursorX = sFaultDrawer.cursorX;
s32 cursorY = sFaultDrawer.cursorY;
s32 shift = c % 4;
const u32* dataPtr = &faultDrawer->fontData[((c / 8) * 16) + ((c & 4) >> 2)];
u16* fb = sFaultDrawer.fb + (sFaultDrawer.w * cursorY) + cursorX;
if ((sFaultDrawer.xStart <= cursorX) && ((sFaultDrawer.charW + cursorX - 1) <= sFaultDrawer.xEnd) &&
(sFaultDrawer.yStart <= cursorY) && ((sFaultDrawer.charH + cursorY - 1) <= sFaultDrawer.yEnd)) {
for (y = 0; y < sFaultDrawer.charH; y++) {
u32 mask = 0x10000000 << shift;
data = *dataPtr;
for (x = 0; x < sFaultDrawer.charW; x++) {
if (mask & data) {
fb[x] = sFaultDrawer.foreColor;
} else if (sFaultDrawer.backColor & 1) {
fb[x] = sFaultDrawer.backColor;
}
mask >>= 4;
}
fb += sFaultDrawer.w;
dataPtr += 2;
}
}
}
s32 FaultDrawer_ColorToPrintColor(u16 color) {
s32 i;
for (i = 0; i < ARRAY_COUNT(sFaultDrawer.printColors); i++) {
if (color == sFaultDrawer.printColors[i]) {
return i;
}
}
return -1;
}
void FaultDrawer_UpdatePrintColor(void) {
s32 idx;
if (sFaultDrawer.osSyncPrintfEnabled) {
osSyncPrintf(VT_RST);
idx = FaultDrawer_ColorToPrintColor(sFaultDrawer.foreColor);
if (idx >= 0 && idx < ARRAY_COUNT(sFaultDrawer.printColors) - 2) {
osSyncPrintf(VT_SGR("3%d"), idx);
}
idx = FaultDrawer_ColorToPrintColor(sFaultDrawer.backColor);
if (idx >= 0 && idx < ARRAY_COUNT(sFaultDrawer.printColors) - 2) {
osSyncPrintf(VT_SGR("4%d"), idx);
}
}
}
void FaultDrawer_SetForeColor(u16 color) {
sFaultDrawer.foreColor = color;
FaultDrawer_UpdatePrintColor();
}
void FaultDrawer_SetBackColor(u16 color) {
sFaultDrawer.backColor = color;
FaultDrawer_UpdatePrintColor();
}
void FaultDrawer_SetFontColor(u16 color) {
FaultDrawer_SetForeColor(color | 1); // force alpha to be set
}
void FaultDrawer_SetCharPad(s8 padW, s8 padH) {
sFaultDrawer.charWPad = padW;
sFaultDrawer.charHPad = padH;
}
void FaultDrawer_SetCursor(s32 x, s32 y) {
if (sFaultDrawer.osSyncPrintfEnabled) {
osSyncPrintf(VT_CUP("%d", "%d"), (y - sFaultDrawer.yStart) / (sFaultDrawer.charH + sFaultDrawer.charHPad),
(x - sFaultDrawer.xStart) / (sFaultDrawer.charW + sFaultDrawer.charWPad));
}
sFaultDrawer.cursorX = x;
sFaultDrawer.cursorY = y;
}
void FaultDrawer_FillScreen(void) {
if (sFaultDrawer.osSyncPrintfEnabled) {
osSyncPrintf(VT_CLS);
}
FaultDrawer_DrawRecImpl(sFaultDrawer.xStart, sFaultDrawer.yStart, sFaultDrawer.xEnd, sFaultDrawer.yEnd,
sFaultDrawer.backColor | 1);
FaultDrawer_SetCursor(sFaultDrawer.xStart, sFaultDrawer.yStart);
}
char* FaultDrawer_PrintCallback(char* arg, const char* str, size_t count) {
for (; count != 0; count--, str++) {
s32 curXStart;
s32 curXEnd;
if (sFaultDrawer.escCode) {
sFaultDrawer.escCode = false;
if (*str > '0' && *str <= '9') {
FaultDrawer_SetForeColor(sFaultDrawer.printColors[*str - '0']);
}
curXStart = sFaultDrawer.cursorX;
curXEnd = sFaultDrawer.xEnd - sFaultDrawer.charW;
} else {
switch (*str) {
case '\n':
if (sFaultDrawer.osSyncPrintfEnabled) {
osSyncPrintf("\n");
}
sFaultDrawer.cursorX = sFaultDrawer.w;
curXStart = sFaultDrawer.cursorX;
curXEnd = sFaultDrawer.xEnd - sFaultDrawer.charW;
break;
case FAULT_ESC:
sFaultDrawer.escCode = true;
curXStart = sFaultDrawer.cursorX;
curXEnd = sFaultDrawer.xEnd - sFaultDrawer.charW;
break;
default:
if (sFaultDrawer.osSyncPrintfEnabled) {
osSyncPrintf("%c", *str);
}
FaultDrawer_DrawChar(*str);
sFaultDrawer.cursorX += sFaultDrawer.charW + sFaultDrawer.charWPad;
curXStart = sFaultDrawer.cursorX;
curXEnd = sFaultDrawer.xEnd - sFaultDrawer.charW;
break;
}
}
if (curXEnd <= curXStart) {
sFaultDrawer.cursorX = sFaultDrawer.xStart;
sFaultDrawer.cursorY += sFaultDrawer.charH + sFaultDrawer.charHPad;
if (sFaultDrawer.yEnd - sFaultDrawer.charH <= sFaultDrawer.cursorY) {
if (sFaultDrawer.inputCallback) {
sFaultDrawer.inputCallback();
FaultDrawer_FillScreen();
}
sFaultDrawer.cursorY = sFaultDrawer.yStart;
}
}
}
osWritebackDCacheAll();
return arg;
}
const char D_8003E280[] = "(null)";
s32 FaultDrawer_VPrintf(const char* fmt, va_list args) {
return _Printf(FaultDrawer_PrintCallback, (void*)&sFaultDrawer, fmt, args);
}
s32 FaultDrawer_Printf(const char* fmt, ...) {
s32 ret;
va_list args;
va_start(args, fmt);
ret = FaultDrawer_VPrintf(fmt, args);
va_end(args);
return ret;
}
void FaultDrawer_DrawText(s32 x, s32 y, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
FaultDrawer_SetCursor(x, y);
FaultDrawer_VPrintf(fmt, args);
va_end(args);
}
void FaultDrawer_SetDrawerFB(void* fb, u16 w, u16 h) {
sFaultDrawer.fb = fb;
sFaultDrawer.w = w;
sFaultDrawer.h = h;
}
void FaultDrawer_SetInputCallback(void (*callback)(void)) {
sFaultDrawer.inputCallback = callback;
}
void FaultDrawer_WritebackFBDCache(void) {
osWritebackDCache(sFaultDrawer.fb, sFaultDrawer.w * sFaultDrawer.h * sizeof(u16));
}
void FaultDrawer_Init(void) {
bcopy(&sFaultDrawerDefault, &sFaultDrawer, sizeof(FaultDrawer));
sFaultDrawer.fb = (u16*)(PHYS_TO_K0(osMemSize) - sizeof(u16[SCREEN_HEIGHT][SCREEN_WIDTH]));
}
|