1 | /*--------------------------------------------------------------------------------- |
---|
2 | Copyright (C) 2005 |
---|
3 | Michael Noland (joat) |
---|
4 | Jason Rogers (dovoto) |
---|
5 | Dave Murphy (WinterMute) |
---|
6 | |
---|
7 | This software is provided 'as-is', without any express or implied |
---|
8 | warranty. In no event will the authors be held liable for any |
---|
9 | damages arising from the use of this software. |
---|
10 | |
---|
11 | Permission is granted to anyone to use this software for any |
---|
12 | purpose, including commercial applications, and to alter it and |
---|
13 | redistribute it freely, subject to the following restrictions: |
---|
14 | |
---|
15 | 1. The origin of this software must not be misrepresented; you |
---|
16 | must not claim that you wrote the original software. If you use |
---|
17 | this software in a product, an acknowledgment in the product |
---|
18 | documentation would be appreciated but is not required. |
---|
19 | 2. Altered source versions must be plainly marked as such, and |
---|
20 | must not be misrepresented as being the original software. |
---|
21 | 3. This notice may not be removed or altered from any source |
---|
22 | distribution. |
---|
23 | |
---|
24 | ---------------------------------------------------------------------------------*/ |
---|
25 | |
---|
26 | #include <nds/jtypes.h> |
---|
27 | #include <nds/memory.h> |
---|
28 | #include <nds/arm9/console.h> |
---|
29 | #include <nds/arm9/video.h> |
---|
30 | #include <nds/arm9/background.h> |
---|
31 | #include <default_font_bin.h> |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdarg.h> |
---|
35 | #include <sys/iosupport.h> |
---|
36 | |
---|
37 | // global console variables |
---|
38 | |
---|
39 | #define CONSOLE_WIDTH 32 |
---|
40 | #define CONSOLE_HEIGHT 24 |
---|
41 | #define TAB_SIZE 3 |
---|
42 | |
---|
43 | // map to print to |
---|
44 | static u16* fontMap; |
---|
45 | |
---|
46 | // location of cursor |
---|
47 | static int row, col; |
---|
48 | static int savedX, savedY; |
---|
49 | |
---|
50 | // font may not start on a character base boundry |
---|
51 | static u16 fontOffset; |
---|
52 | |
---|
53 | // the first character in the set (0 if you have a full set) |
---|
54 | static u16 fontStart; |
---|
55 | |
---|
56 | // the 16-color palette to use |
---|
57 | bool fontBold = false; |
---|
58 | static u16 fontPal; |
---|
59 | |
---|
60 | static int consoleInitialised = 0; |
---|
61 | |
---|
62 | void consolePrintChar(char c); |
---|
63 | |
---|
64 | |
---|
65 | //--------------------------------------------------------------------------------- |
---|
66 | static void consoleCls(char mode) { |
---|
67 | //--------------------------------------------------------------------------------- |
---|
68 | |
---|
69 | int i = 0; |
---|
70 | int colTemp,rowTemp; |
---|
71 | |
---|
72 | switch (mode) |
---|
73 | { |
---|
74 | case '0': |
---|
75 | { |
---|
76 | colTemp = col; |
---|
77 | rowTemp = row; |
---|
78 | |
---|
79 | while(i++ < ((CONSOLE_HEIGHT * CONSOLE_WIDTH) - (rowTemp * CONSOLE_WIDTH + colTemp))) consolePrintChar(' '); |
---|
80 | |
---|
81 | col = colTemp; |
---|
82 | row = rowTemp; |
---|
83 | break; |
---|
84 | } |
---|
85 | case '1': |
---|
86 | { |
---|
87 | colTemp = col; |
---|
88 | rowTemp = row; |
---|
89 | |
---|
90 | row = 0; |
---|
91 | col = 0; |
---|
92 | |
---|
93 | while (i++ < (rowTemp * CONSOLE_WIDTH + colTemp)) consolePrintChar(' '); |
---|
94 | |
---|
95 | col = colTemp; |
---|
96 | row = rowTemp; |
---|
97 | break; |
---|
98 | } |
---|
99 | case '2': |
---|
100 | { |
---|
101 | row = 0; |
---|
102 | col = 0; |
---|
103 | |
---|
104 | while(i++ < CONSOLE_HEIGHT * CONSOLE_WIDTH) consolePrintChar(' '); |
---|
105 | |
---|
106 | row = 0; |
---|
107 | col = 0; |
---|
108 | break; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | //--------------------------------------------------------------------------------- |
---|
113 | static void consoleClearLine(char mode) { |
---|
114 | //--------------------------------------------------------------------------------- |
---|
115 | |
---|
116 | int i = 0; |
---|
117 | int colTemp; |
---|
118 | |
---|
119 | switch (mode) |
---|
120 | { |
---|
121 | case '0': |
---|
122 | { |
---|
123 | colTemp = col; |
---|
124 | |
---|
125 | while(i++ < (CONSOLE_WIDTH - colTemp)) { |
---|
126 | consolePrintChar(' '); |
---|
127 | } |
---|
128 | |
---|
129 | col = colTemp; |
---|
130 | |
---|
131 | break; |
---|
132 | } |
---|
133 | case '1': |
---|
134 | { |
---|
135 | colTemp = col; |
---|
136 | |
---|
137 | col = 0; |
---|
138 | |
---|
139 | while(i++ < ((CONSOLE_WIDTH - colTemp)-2)) { |
---|
140 | consolePrintChar(' '); |
---|
141 | } |
---|
142 | |
---|
143 | col = colTemp; |
---|
144 | |
---|
145 | break; |
---|
146 | } |
---|
147 | case '2': |
---|
148 | { |
---|
149 | colTemp = col; |
---|
150 | |
---|
151 | col = 0; |
---|
152 | |
---|
153 | while(i++ < CONSOLE_WIDTH) { |
---|
154 | consolePrintChar(' '); |
---|
155 | } |
---|
156 | |
---|
157 | col = colTemp; |
---|
158 | |
---|
159 | break; |
---|
160 | } |
---|
161 | default: |
---|
162 | { |
---|
163 | colTemp = col; |
---|
164 | |
---|
165 | while(i++ < (CONSOLE_WIDTH - colTemp)) { |
---|
166 | consolePrintChar(' '); |
---|
167 | } |
---|
168 | |
---|
169 | col = colTemp; |
---|
170 | |
---|
171 | break; |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | //--------------------------------------------------------------------------------- |
---|
176 | int con_write(struct _reent *r,int fd,const char *ptr,int len) { |
---|
177 | //--------------------------------------------------------------------------------- |
---|
178 | |
---|
179 | if (!consoleInitialised) return -1; |
---|
180 | |
---|
181 | char chr; |
---|
182 | |
---|
183 | int i, count = 0; |
---|
184 | char *tmp = (char*)ptr; |
---|
185 | |
---|
186 | if(!tmp || len<=0) return -1; |
---|
187 | |
---|
188 | i = 0; |
---|
189 | while(*tmp!='\0' && i<len) { |
---|
190 | |
---|
191 | chr = *(tmp++); |
---|
192 | i++; count++; |
---|
193 | |
---|
194 | if ( chr == 0x1b && *tmp == '[' ) { |
---|
195 | bool escaping = true; |
---|
196 | char *escapeseq = tmp; |
---|
197 | int escapelen = 0; |
---|
198 | |
---|
199 | do { |
---|
200 | chr = *(tmp++); |
---|
201 | i++; count++; escapelen++; |
---|
202 | int parameter; |
---|
203 | |
---|
204 | switch (chr) { |
---|
205 | ///////////////////////////////////////// |
---|
206 | // Cursor directional movement |
---|
207 | ///////////////////////////////////////// |
---|
208 | case 'A': |
---|
209 | siscanf(escapeseq,"[%dA", ¶meter); |
---|
210 | row = (row - parameter) < 0 ? 0 : row - parameter; |
---|
211 | escaping = false; |
---|
212 | break; |
---|
213 | case 'B': |
---|
214 | siscanf(escapeseq,"[%dB", ¶meter); |
---|
215 | row = (row + parameter) > CONSOLE_HEIGHT - 1 ? CONSOLE_HEIGHT - 1 : row + parameter; |
---|
216 | escaping = false; |
---|
217 | break; |
---|
218 | case 'C': |
---|
219 | siscanf(escapeseq,"[%dC", ¶meter); |
---|
220 | col = (col + parameter) > CONSOLE_WIDTH - 1 ? CONSOLE_WIDTH - 1 : col + parameter; |
---|
221 | escaping = false; |
---|
222 | break; |
---|
223 | case 'D': |
---|
224 | siscanf(escapeseq,"[%dC", ¶meter); |
---|
225 | col = (col - parameter) < 0 ? 0 : col - parameter; |
---|
226 | escaping = false; |
---|
227 | break; |
---|
228 | ///////////////////////////////////////// |
---|
229 | // Cursor position movement |
---|
230 | ///////////////////////////////////////// |
---|
231 | case 'H': |
---|
232 | case 'f': |
---|
233 | siscanf(escapeseq,"[%d;%df", &row, &col); |
---|
234 | escaping = false; |
---|
235 | break; |
---|
236 | ///////////////////////////////////////// |
---|
237 | // Screen clear |
---|
238 | ///////////////////////////////////////// |
---|
239 | case 'J': |
---|
240 | consoleCls(escapeseq[escapelen-2]); |
---|
241 | escaping = false; |
---|
242 | break; |
---|
243 | ///////////////////////////////////////// |
---|
244 | // Line clear |
---|
245 | ///////////////////////////////////////// |
---|
246 | case 'K': |
---|
247 | consoleClearLine(escapeseq[escapelen-2]); |
---|
248 | escaping = false; |
---|
249 | break; |
---|
250 | ///////////////////////////////////////// |
---|
251 | // Save cursor position |
---|
252 | ///////////////////////////////////////// |
---|
253 | case 's': |
---|
254 | savedX = col; |
---|
255 | savedY = row; |
---|
256 | escaping = false; |
---|
257 | break; |
---|
258 | ///////////////////////////////////////// |
---|
259 | // Load cursor position |
---|
260 | ///////////////////////////////////////// |
---|
261 | case 'u': |
---|
262 | col = savedX; |
---|
263 | row = savedY; |
---|
264 | escaping = false; |
---|
265 | break; |
---|
266 | } |
---|
267 | } while (escaping); |
---|
268 | continue; |
---|
269 | } |
---|
270 | |
---|
271 | consolePrintChar(chr); |
---|
272 | } |
---|
273 | |
---|
274 | return count; |
---|
275 | } |
---|
276 | |
---|
277 | const devoptab_t dotab_stdout = { |
---|
278 | "con", |
---|
279 | NULL, |
---|
280 | NULL, |
---|
281 | con_write, |
---|
282 | NULL, |
---|
283 | NULL, |
---|
284 | NULL |
---|
285 | }; |
---|
286 | |
---|
287 | |
---|
288 | /*--------------------------------------------------------------------------------- |
---|
289 | consoleInit |
---|
290 | param: |
---|
291 | font: 16 color font |
---|
292 | charBase: the location the font data will be loaded to |
---|
293 | numCharacters: count of characters in the font |
---|
294 | charStart: The ascii number of the first character in the font set |
---|
295 | if you have a full set this will be zero |
---|
296 | map: pointer to the map you will be printing to. |
---|
297 | pal: specifies the 16 color palette to use, if > 15 it will change all non-zero |
---|
298 | entries in the font to use palette index 255 |
---|
299 | ---------------------------------------------------------------------------------*/ |
---|
300 | void consoleInit( u16* font, u16* charBase, |
---|
301 | u16 numCharacters, u8 charStart, |
---|
302 | u16* map, u8 pal, u8 bitDepth) { |
---|
303 | //--------------------------------------------------------------------------------- |
---|
304 | int i; |
---|
305 | |
---|
306 | row = col = 0; |
---|
307 | |
---|
308 | fontStart = charStart; |
---|
309 | |
---|
310 | fontOffset = 0; |
---|
311 | |
---|
312 | fontMap = map; |
---|
313 | |
---|
314 | if(bitDepth == 16) |
---|
315 | { |
---|
316 | if(pal < 16) |
---|
317 | { |
---|
318 | fontPal = pal << 12; |
---|
319 | |
---|
320 | for (i = 0; i < numCharacters * 16; i++) |
---|
321 | charBase[i] = font[i]; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | fontPal = 15 << 12; |
---|
326 | |
---|
327 | for (i = 0; i < numCharacters * 16; i++) |
---|
328 | { |
---|
329 | u16 temp = 0; |
---|
330 | |
---|
331 | if(font[i] & 0xF) |
---|
332 | temp |= 0xF; |
---|
333 | if(font[i] & 0xF0) |
---|
334 | temp |= 0xF0; |
---|
335 | if(font[i] & 0xF00) |
---|
336 | temp |= 0xF00; |
---|
337 | if(font[i] & 0xF000) |
---|
338 | temp |= 0xF000; |
---|
339 | |
---|
340 | charBase[i] = temp; |
---|
341 | } |
---|
342 | } |
---|
343 | }//end if bitdepth |
---|
344 | else |
---|
345 | { |
---|
346 | fontPal = 0; |
---|
347 | for(i = 0; i < numCharacters * 16; i++) |
---|
348 | { |
---|
349 | u32 temp = 0; |
---|
350 | |
---|
351 | if(font[i] & 0xF) |
---|
352 | temp = 255; |
---|
353 | if(font[i] & 0xF0) |
---|
354 | temp |= 255 << 8; |
---|
355 | if(font[i] & 0xF00) |
---|
356 | temp |= 255 << 16; |
---|
357 | if(font[i] & 0xF000) |
---|
358 | temp |= 255 << 24; |
---|
359 | |
---|
360 | ((u32*)charBase)[i] = temp; |
---|
361 | |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | devoptab_list[STD_OUT] = &dotab_stdout; |
---|
366 | devoptab_list[STD_ERR] = &dotab_stdout; |
---|
367 | setvbuf(stderr, NULL , _IONBF, 0); |
---|
368 | setvbuf(stdout, NULL , _IONBF, 0); |
---|
369 | consoleCls('2'); |
---|
370 | consoleInitialised = 1; |
---|
371 | |
---|
372 | } |
---|
373 | |
---|
374 | //--------------------------------------------------------------------------------- |
---|
375 | // Places the console in a default mode using bg0 of the sub display, and vram c for |
---|
376 | // font and map..this is provided for rapid prototyping and nothing more |
---|
377 | void consoleDemoInit(void) { |
---|
378 | //--------------------------------------------------------------------------------- |
---|
379 | videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text |
---|
380 | vramSetBankC(VRAM_C_SUB_BG); |
---|
381 | |
---|
382 | SUB_BG0_CR = BG_MAP_BASE(31); |
---|
383 | |
---|
384 | BG_PALETTE_SUB[255] = RGB15(31,31,31); //by default font will be rendered with color 255 |
---|
385 | |
---|
386 | //consoleInit() is a lot more flexible but this gets you up and running quick |
---|
387 | consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16); |
---|
388 | } |
---|
389 | |
---|
390 | //--------------------------------------------------------------------------------- |
---|
391 | void consoleInitDefault(u16* map, u16* charBase, u8 bitDepth) { |
---|
392 | //--------------------------------------------------------------------------------- |
---|
393 | consoleInit((u16*)default_font_bin, charBase, 256, 0, map, CONSOLE_USE_COLOR255, bitDepth); |
---|
394 | } |
---|
395 | |
---|
396 | //void consoleInitColorDefault(u16* map, u16* charBase) { |
---|
397 | // consoleInit((u16*)default_font_bin, charBase, 256, 0, map, CONSOLE_USE_COLOR255, bitDepth); |
---|
398 | //} |
---|
399 | |
---|
400 | //--------------------------------------------------------------------------------- |
---|
401 | static void newRow(void) { |
---|
402 | //--------------------------------------------------------------------------------- |
---|
403 | int i; |
---|
404 | row++; |
---|
405 | if(row >= CONSOLE_HEIGHT) { |
---|
406 | row--; |
---|
407 | |
---|
408 | for(i = CONSOLE_WIDTH; i < CONSOLE_HEIGHT * CONSOLE_WIDTH; i++) fontMap[i - CONSOLE_WIDTH] = fontMap[i]; |
---|
409 | |
---|
410 | for(i = 0; i < CONSOLE_WIDTH; i++) fontMap[i + (CONSOLE_HEIGHT-1)*CONSOLE_WIDTH] = fontPal | (u16)(' ' + fontOffset - fontStart); |
---|
411 | |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | //--------------------------------------------------------------------------------- |
---|
417 | void consolePrintChar(char c) { |
---|
418 | //--------------------------------------------------------------------------------- |
---|
419 | |
---|
420 | if(col >= CONSOLE_WIDTH) { |
---|
421 | col = 0; |
---|
422 | |
---|
423 | newRow(); |
---|
424 | } |
---|
425 | |
---|
426 | switch(c) { |
---|
427 | /* |
---|
428 | The only special characters we will handle are tab (\t), carriage return (\r) & line feed (\n). |
---|
429 | Carriage return & line feed will function the same: go to next line and put cursor at the beginning. |
---|
430 | For everything else, use VT sequences. |
---|
431 | |
---|
432 | Reason: VT sequences are more specific to the task of cursor placement. |
---|
433 | The special escape sequences \b \f & \v are archaic and non-portable. |
---|
434 | */ |
---|
435 | case 9: |
---|
436 | col += TAB_SIZE; |
---|
437 | break; |
---|
438 | case 10: |
---|
439 | case 13: |
---|
440 | newRow(); |
---|
441 | col = 0; |
---|
442 | break; |
---|
443 | default: |
---|
444 | fontMap[col + row * CONSOLE_WIDTH] = fontPal | (u16)(c + fontOffset - fontStart); |
---|
445 | ++col; |
---|
446 | break; |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|
450 | //--------------------------------------------------------------------------------- |
---|
451 | void consoleClear(void) { |
---|
452 | //--------------------------------------------------------------------------------- |
---|
453 | iprintf("\x1b[2J"); |
---|
454 | } |
---|
455 | |
---|
456 | |
---|