-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsole.c
369 lines (299 loc) · 8.48 KB
/
console.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
BootMii - a Free Software replacement for the Nintendo/BroadOn bootloader.
Requires mini.
Copyright (C) 2009 bLAStY <blasty@bootmii.org>
Copyright (C) 2009 John Kelley <wiidev@kelley.ca>
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#include "powerpc.h"
#include "video_low.h"
#include "console.h"
#include "string.h"
#include "printf.h"
#include "malloc.h"
#include "config.h"
#include <stdarg.h>
#include "console_common.h"
int gfx_console_init = 0;
static u32 *xfb = NULL;
static int y_add = 0;
// current absolute cursor position
static int console_pos = 0;
rgb black = {.as_rgba = {0, 0, 0, 0xFF}};
u32 *font_yuv_normal[255],
*font_yuv_highlight[255],
*font_yuv_helptext[255],
*font_yuv_heading[255],
// used only to print log entries
*font_yuv_error[255];
u32 **selected_font_yuv = NULL;
u32 *get_xfb(void) {
return xfb;
}
static void memcpy32(u32 *dst, u32 *src, u32 count) {
while(count--) {
*dst = *src;
sync_after_write((const void *)dst, 4);
dst++;
src++;
}
}
static void memset32(u32 *dst, u32 setval, u32 count) {
while(count--) {
*dst = setval;
sync_after_write((const void *)dst, 4);
dst++;
}
}
u32 pal_idx(int i, u8 *pal, u8 *gfx) {
u32 pidx = gfx[i];
pidx *= 3;
return (pal[pidx+0] << 16) | (pal[pidx+1] << 8) | (pal[pidx+2]);
}
int make_yuv(rgb c1, rgb c2) {
int y1, cb1, cr1, y2, cb2, cr2, cb, cr;
y1 = (299 * c1.as_rgba.r + 587 * c1.as_rgba.g + 114 * c1.as_rgba.b) / 1000;
cb1 = (-16874 * c1.as_rgba.r - 33126 * c1.as_rgba.g + 50000 * c1.as_rgba.b + 12800000) / 100000;
cr1 = (50000 * c1.as_rgba.r - 41869 * c1.as_rgba.g - 8131 * c1.as_rgba.b + 12800000) / 100000;
y2 = (299 * c2.as_rgba.r + 587 * c2.as_rgba.g + 114 * c2.as_rgba.b) / 1000;
cb2 = (-16874 * c2.as_rgba.r - 33126 * c2.as_rgba.g + 50000 * c2.as_rgba.b + 12800000) / 100000;
cr2 = (50000 * c2.as_rgba.r - 41869 * c2.as_rgba.g - 8131 * c2.as_rgba.b + 12800000) / 100000;
cb = (cb1 + cb2) >> 1;
cr = (cr1 + cr2) >> 1;
return ((y1 << 24) | (cb << 16) | (y2 << 8) | cr);
}
void fill_rect(int x, int y, int w, int h, rgb rgbcol) {
u32 *fb = xfb;
u32 col = make_yuv(rgbcol, rgbcol);
fb += ((y + y_add) * (RESOLUTION_W >> 1));
fb += (x >> 1);
for(y = 0; y < h; y++) {
memset32(fb, col, w >> 1);
fb += (RESOLUTION_W >> 1);
}
}
void gfx_draw_char(int dx, int dy, unsigned char c) {
u32 y;
u32 *fb = xfb;
u32 *yuv_data = selected_font_yuv[ c ];
fb += ((dy + y_add) * (RESOLUTION_W >> 1));
fb += (dx >> 1);
for(y = 0; y < CONSOLE_CHAR_HEIGHT; y++) {
memcpy32(fb, yuv_data + ((CONSOLE_CHAR_WIDTH >> 1) * y), CONSOLE_CHAR_WIDTH >> 1);
fb += (RESOLUTION_W >> 1);
}
}
void scroll(void) {
unsigned int y;
u32 *fb = xfb;
fb += ((CONSOLE_Y_OFFSET+y_add+CONSOLE_ROW_HEIGHT) * (RESOLUTION_W >> 1));
fb += (CONSOLE_X_OFFSET >> 1);
for (y = 0; y < CONSOLE_LINES*CONSOLE_ROW_HEIGHT; y++) {
memcpy32(fb - (CONSOLE_ROW_HEIGHT * RESOLUTION_W/2), fb, CONSOLE_WIDTH >> 1);
fb += RESOLUTION_W/2;
}
fill_rect(CONSOLE_X_OFFSET, CONSOLE_Y_OFFSET+(CONSOLE_LINES-1)*CONSOLE_ROW_HEIGHT,
CONSOLE_WIDTH, CONSOLE_ROW_HEIGHT, black);
}
void gfx_clear(int x, int y, int w, int h, rgb c) {
fill_rect(CONSOLE_X_OFFSET + x*CONSOLE_CHAR_WIDTH, CONSOLE_Y_OFFSET+ y * CONSOLE_ROW_HEIGHT,
w*CONSOLE_CHAR_WIDTH, h * CONSOLE_ROW_HEIGHT, c);
}
void gfx_print(const char *str, size_t len) {
unsigned int i;
for (i = 0; i < len; i++) {
// special case: a newline forces to reposition on next line
if (str[i] == '\n') {
console_pos += (CONSOLE_COLUMNS - console_pos % CONSOLE_COLUMNS);
} else {
// increase absolute position by 1
console_pos++;
}
// calculate new coordinates
int x = console_pos % CONSOLE_COLUMNS;
int y = console_pos / CONSOLE_COLUMNS;
// did the line number increase?
if (x == 0) {
// is the new y coordinate overflowing height?
if (y == CONSOLE_LINES) {
scroll();
y = CONSOLE_LINES - 1;
// adjust to new scrolled position
console_pos = y * CONSOLE_COLUMNS + x;
}
}
// nothing to draw for newlines
if (str[i] == '\n') {
continue;
}
int dx = CONSOLE_X_OFFSET + x * CONSOLE_CHAR_WIDTH;
int dy = CONSOLE_Y_OFFSET + y * CONSOLE_ROW_HEIGHT;
gfx_draw_char(dx, dy, str[i]);
}
}
int gfx_printf(const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i = vsnprintf(pf_buffer, sizeof(pf_buffer)-1, fmt, args);
va_end(args);
if (i > 0) {
gfx_print(pf_buffer, i);
}
return i;
}
void internal_free_font(u32 *font_yuv[255]) {
int i;
for (i = 0; i < 255; i++) {
free(font_yuv[i]);
}
}
void font_to_yuv(u32 *font_yuv[255], rgb fg, rgb bg) {
int i, x, y;
for (i = 0; i < 255; i++) {
font_yuv[i] = (u32*)malloc(8*CONSOLE_CHAR_HEIGHT*2);
for (y = 0; y < CONSOLE_CHAR_HEIGHT; y++) {
for (x = 0; x < CONSOLE_CHAR_WIDTH; x+=2) {
rgb left, right;
if (((console_font_8x16[(i*CONSOLE_CHAR_HEIGHT)+y] >> (CONSOLE_CHAR_WIDTH-1-x)) & 0x01) == 1) {
left = fg;
} else {
left = bg;
}
if (((console_font_8x16[(i*CONSOLE_CHAR_HEIGHT)+y] >> (CONSOLE_CHAR_WIDTH-1-(x+1))) & 0x01) == 1) {
right = fg;
} else {
right = bg;
}
font_yuv[i][(y<<2)+(x>>1)] = make_yuv(left, right);
}
}
}
}
void free_font(int font) {
switch (font) {
case FONT_HEADING:
internal_free_font(font_yuv_heading);
break;
case FONT_NORMAL:
internal_free_font(font_yuv_normal);
break;
case FONT_HELPTEXT:
internal_free_font(font_yuv_helptext);
break;
case FONT_HIGHLIGHT:
internal_free_font(font_yuv_highlight);
break;
case FONT_ERROR:
internal_free_font(font_yuv_error);
break;
}
// PANIC!
return;
}
static void internal_init_font(rgb c[2], u32 *font_yuv[255]) {
// re-initialise color
font_to_yuv(font_yuv, c[0], c[1]);
}
void init_fb(int vmode) {
// default fonts setup before configuration is loaded
// and used to display configuration load errors / other log errors
selected_font_yuv = font_yuv_normal;
switch(vmode) {
case VIDEO_640X480_NTSCi_YUV16:
case VIDEO_640X480_PAL60_YUV16:
case VIDEO_640X480_NTSCp_YUV16:
y_add = 0;
break;
case VIDEO_640X480_PAL50_YUV16:
y_add = 48;
break;
}
xfb = memalign(32, RESOLUTION_W * (RESOLUTION_H + (y_add*2)) * 2);
}
// does not take splash screen in account
void clear_fb(rgb fill_rgb) {
int i;
u32 *fb;
u32 fill_yuv = make_yuv(fill_rgb, fill_rgb);
fb = xfb;
for (i = 0; i < (RESOLUTION_H + (y_add*2)) * 2 * (RESOLUTION_W >> 1); i++) {
*fb = fill_yuv;
sync_after_write(fb, 4);
fb++;
}
}
void select_font(int font) {
switch (font) {
case FONT_HEADING:
selected_font_yuv = font_yuv_heading;
break;
case FONT_NORMAL:
selected_font_yuv = font_yuv_normal;
break;
case FONT_HELPTEXT:
selected_font_yuv = font_yuv_helptext;
break;
case FONT_HIGHLIGHT:
selected_font_yuv = font_yuv_highlight;
break;
case FONT_ERROR:
selected_font_yuv = font_yuv_error;
break;
}
}
void init_font(int font) {
switch (font) {
case FONT_HEADING:
internal_init_font(config_color_heading, font_yuv_heading);
break;
case FONT_NORMAL:
internal_init_font(config_color_normal, font_yuv_normal);
break;
case FONT_HELPTEXT:
internal_init_font(config_color_helptext, font_yuv_helptext);
break;
case FONT_HIGHLIGHT:
internal_init_font(config_color_highlight, font_yuv_highlight);
break;
case FONT_ERROR:
internal_init_font(color_error, font_yuv_error);
break;
}
// PANIC!
return;
}
void console_move(int x, int y) {
console_pos = y * CONSOLE_COLUMNS + x;
}
// toggle a block sign on the top-right corner of the console screen
void console_set_blinker(int status) {
select_font(FONT_HEADING);
if (status)
gfx_printch_at(CONSOLE_COLUMNS-1, 0, 254);
else
gfx_printch_at(CONSOLE_COLUMNS-1, 0, ' ');
}
void console_blit(int dx, int dy, raster rst, rgb solid_bg, u32 *yuv_row) {
u32 x, y;
u32 *fb = xfb;
u32 *pixel_data = (u32 *)rst.pixels;
fb += ((dy + y_add) * (RESOLUTION_W >> 1));
fb += (dx >> 1);
// allocate a buffer that will be used for the RGB->YUV conversion
u32 row_len = rst.width >> 1;
for(y = 0; y < rst.height; y++) {
for (x = 0; x < rst.width; x+=2) {
// convert two pixels from source into one destination pixels
rgb left = {.as_u32 = pixel_data[x + y * rst.width]};
rgb right = {.as_u32 = pixel_data[x + 1 + y * rst.width]};
left = apply_alpha(left, solid_bg);
right = apply_alpha(right, solid_bg);
yuv_row[x >> 1] = make_yuv(left, right);
}
memcpy32(fb, yuv_row, row_len);
fb += (RESOLUTION_W >> 1);
}
}