forked from iPodLinux/ipodloader2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
console.c
205 lines (172 loc) · 4.78 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
#include "bootloader.h"
#include "fb.h"
#include "console.h"
#include "minilibc.h"
#include "ipodhw.h"
#include "fontlarge.h"
#include "fontmedium.h"
// not used currently: #include "fontsmall.h"
int font_lines;
int font_height, font_width;
int console_printcount = 0;
static const uint8 *fontdata;
static struct {
struct {
uint16 x,y;
} cursor;
struct {
uint16 w,h;
} dimensions;
uint16 *fb;
uint16 fgcolor,bgcolor;
uint8 transparent;
char cls_pending;
char scroll_pending;
char scrollMode;
ipod_t *ipod;
} console;
void console_setcolor(uint16 fg, uint16 bg, uint8 transparent) {
console.fgcolor = fg;
console.bgcolor = bg;
console.transparent = transparent;
}
void console_getcolor(uint16 *fg, uint16 *bg, uint8 *transparent) {
*fg = console.fgcolor;
*bg = console.bgcolor;
*transparent = console.transparent;
}
static int console_suppress_fbupdate_cnt = 0;
int console_suppress_fbupdate (int modify) {
// pass 1 to suppress calls to fb_update with linefeeds, pass -1 to undo it again
// pass 0 to inquire the current value
// once the counter goes back to 0, a fb_update is performed
console_suppress_fbupdate_cnt += modify;
if (!console_suppress_fbupdate_cnt) {
fb_update (console.fb);
}
return console_suppress_fbupdate_cnt;
}
void console_home()
{
console.cursor.x = 0;
console.cursor.y = 0;
console.scroll_pending = 0;
}
void console_clear()
{
console_home();
console.cls_pending = 1;
}
void console_setfont (const uint8 *font) {
font_width = font[0];
font_height = font[1];
fontdata = font + 2;
font_lines = console.dimensions.h / font_height;
}
const uint8* console_currentfont (void)
{
return fontdata - 2;
}
static void console_blitchar(int x, int y, char ch) {
int r,c;
if ((y >= 0) && ((y + font_height) <= console.dimensions.h)) {
int ofs = y * console.dimensions.w + x;
for(r=0;r<font_height;r++) {
for(c=0;c<font_width;c++) {
if( (uint8)fontdata[(uint8)ch * font_height + r] & (1<<(8-c)) ) { /* Pixel set */
console.fb[ofs+c] = console.fgcolor;
} else { /* Pixel clear */
if( !console.transparent )
console.fb[ofs+c] = console.bgcolor;
}
}
ofs += console.dimensions.w;
}
}
console_printcount += 1;
}
static void console_linefeed () {
console.cursor.x = 0;
console.cursor.y++;
/* Check if we need to scroll the display up */
if(console.cursor.y >= font_lines ) {
if (console.scrollMode) {
console.scroll_pending = 1; // delay scroll until we actually write text to a new line
} else {
// reset cursor to top of screen
console.cursor.y = 0;
console.cls_pending = 1; // we must delay fb_cls or we'd never see the just printed line!
}
}
if (!console_suppress_fbupdate_cnt) {
fb_update(console.fb);
#ifdef MSGDELAY // actually, such a delay can now be achieved using the debug option in the config file
unsigned int start = inl (0x60005010);
while (inl (0x60005010) < start + MSGDELAY) {}
#endif
}
}
void console_putchar(char ch) {
again:
if (console.cls_pending) {
// clear screen
fb_cls (console.fb, console.bgcolor);
console.cls_pending = 0;
} else if (console.scroll_pending) {
// scroll
console.scroll_pending = 0;
int i;
mlc_memcpy(console.fb,
console.fb+(console.dimensions.w*font_height),
(console.dimensions.w*console.dimensions.h*2) -
(console.dimensions.w*font_height*2) );
for (i = console.dimensions.w*(console.dimensions.h-font_height); i < console.dimensions.w*console.dimensions.h; i++) {
console.fb[i] = console.bgcolor;
}
console.cursor.y--;
}
if( console.cursor.x >= (console.dimensions.w / font_width) ) {
console_linefeed ();
goto again;
}
if(ch == '\n') {
console_linefeed ();
} else if(ch == '\r') {
console.cursor.x = 0;
} else {
uint16 x,y;
x = console.cursor.x * font_width;
y = console.cursor.y * font_height;
console_blitchar(x,y,ch);
console.cursor.x += 1;
}
}
void console_puts(volatile char *str) {
while(*str != 0) {
console_putchar(*str);
str++;
}
}
void console_putsXY(int x,int y,volatile char *str) {
while(*str != 0) {
console_blitchar(x,y,*str);
x += font_width;
str++;
}
}
void console_init(uint16 *fb)
{
console.ipod = ipod_get_hwinfo();
console.cursor.x = 0;
console.cursor.y = 0;
console.dimensions.w = console.ipod->lcd_width;
console.dimensions.h = console.ipod->lcd_height;
console_setfont ( (console.dimensions.w < 300) ? font_medium : font_large );
console.fgcolor = WHITE;
console.bgcolor = BLACK;
console.transparent = 0;
console.scrollMode = 1;
console.cls_pending = 1;
console.scroll_pending = 0;
console.fb = fb;
}