1 | /** |
---|
2 | * @file conio.c |
---|
3 | * |
---|
4 | * This file contains the GBA conio I/O package. |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * RTEMS GBA BSP |
---|
9 | * |
---|
10 | * Copyright (c) 2004 Markku Puro <markku.puro@kopteri.net> |
---|
11 | * based on MyLib by Rafael Vuijk (aka Dark Fader) |
---|
12 | * |
---|
13 | * The license and distribution terms for this file may be |
---|
14 | * found in the file LICENSE in this distribution or at |
---|
15 | * http://www.rtems.org/license/LICENSE. |
---|
16 | */ |
---|
17 | |
---|
18 | /***************************************************************************** |
---|
19 | * This source file is based on work by Rafael Vuijk (aka Dark Fader) |
---|
20 | ***************************************************************************** |
---|
21 | ***************************************************************************** |
---|
22 | * MyLib by Rafael Vuijk (aka Dark Fader) |
---|
23 | * : |
---|
24 | * This file is released into the public domain for commercial |
---|
25 | * or non-commercial usage with no restrictions placed upon it. |
---|
26 | *****************************************************************************/ |
---|
27 | |
---|
28 | /*---------------------------------------------------------------------------* |
---|
29 | * Includes * |
---|
30 | *---------------------------------------------------------------------------*/ |
---|
31 | #include <stdint.h> |
---|
32 | #include <rtems/score/types.h> |
---|
33 | #include <gba.h> |
---|
34 | #include <conio.h> |
---|
35 | #include <stdio.h> |
---|
36 | #include <stdarg.h> |
---|
37 | |
---|
38 | /*---------------------------------------------------------------------------* |
---|
39 | * Defines * |
---|
40 | *---------------------------------------------------------------------------*/ |
---|
41 | #define W 60 /**< Screen width */ |
---|
42 | #define H 26 /**< Screen height */ |
---|
43 | #define CLRSCR_SIZE (W*4 * H*6) /**< Screen size */ |
---|
44 | #define DEFAULT_FONT_WIDTH 4 /**< Font width */ |
---|
45 | #define DEFAULT_FONT_HEIGHT 6 /**< Font height */ |
---|
46 | |
---|
47 | typedef unsigned short Palette[256]; /**< 256 colors */ |
---|
48 | typedef unsigned short Palettes[16][16]; /**< 16 palettes with each 16 colors */ |
---|
49 | typedef unsigned short Bitmap3[GBA_LCD_HEIGHT][GBA_LCD_WIDTH]; /**< 16 bits, single buffered */ |
---|
50 | typedef unsigned char Bitmap4[GBA_LCD_HEIGHT][GBA_LCD_WIDTH]; /**< 8 bits, double buffered */ |
---|
51 | typedef unsigned short Bitmap5[GBA_MODE5_HEIGHT][GBA_MODE5_WIDTH];/**< double buffered */ |
---|
52 | |
---|
53 | #define VRAM GBA_VRAM_ADDR /**< VRAM address */ |
---|
54 | #define VRAM_END (VRAM + 0x18000) /**< VRAM end */ |
---|
55 | #define BG_BITMAP0_VRAM (VRAM + 0x0) /**< BG Bitmap 0 RAM */ |
---|
56 | #define BG_BITMAP1_VRAM (VRAM + 0xa000) /**< BG Bitmap 1 RAM */ |
---|
57 | |
---|
58 | #define bg_bitmap0 (*(Bitmap4 *)BG_BITMAP0_VRAM) |
---|
59 | #define bg_bitmap1 (*(Bitmap4 *)BG_BITMAP1_VRAM) |
---|
60 | #define bg_bitmap3 (*(Bitmap3 *)BG_BITMAP0_VRAM) |
---|
61 | #define bg_bitmap4a (*(Bitmap4 *)BG_BITMAP0_VRAM) |
---|
62 | #define bg_bitmap4b (*(Bitmap4 *)BG_BITMAP1_VRAM) |
---|
63 | #define bg_bitmap5a (*(Bitmap5 *)BG_BITMAP0_VRAM) |
---|
64 | #define bg_bitmap5b (*(Bitmap5 *)BG_BITMAP1_VRAM) |
---|
65 | |
---|
66 | /** Color conversion macro */ |
---|
67 | #define RGB(r,g,b) ( (r)<<0 | (g)<<5 | (b)<<10 ) |
---|
68 | |
---|
69 | /** BG Affine Transformation Destination Data Structure */ |
---|
70 | typedef struct { |
---|
71 | int16_t H_DiffX; /**< Line Direction X Coordinate Difference */ |
---|
72 | int16_t V_DiffX; /**< Vertical Direction X Coordinate Difference */ |
---|
73 | int16_t H_DiffY; /**< Line Direction Y Coordinate Difference */ |
---|
74 | int16_t V_DiffY; /**< Vertical Direction Y Coordinate Difference */ |
---|
75 | int32_t StartX; /**< Start X Coordinate */ |
---|
76 | int32_t StartY; /**< Start Y Coordinate */ |
---|
77 | } BgAffineDestData; |
---|
78 | |
---|
79 | typedef volatile BgAffineDestData vBgAffineDestData; |
---|
80 | #define rBg2Affine (*(vBgAffineDestData *)0x4000020) |
---|
81 | |
---|
82 | /** 256 colors for background(s) */ |
---|
83 | #define bg_palette (*(Palette *)(GBA_PAL_RAM_ADDR)) |
---|
84 | |
---|
85 | |
---|
86 | int _wherex; /**< Screen X coordinate */ |
---|
87 | int _wherey; /**< Screen Y coordinate */ |
---|
88 | int _textattr; /**< Text attribute */ |
---|
89 | |
---|
90 | |
---|
91 | /*---------------------------------------------------------------------------* |
---|
92 | * Defaultfont * |
---|
93 | *---------------------------------------------------------------------------*/ |
---|
94 | #include "defaultfont.h" |
---|
95 | |
---|
96 | /** |
---|
97 | * @brief gba_gotoxy function set screeen xy-coordinates |
---|
98 | * |
---|
99 | * @param _x screen x coordinate |
---|
100 | * @param _y screen y coordinate |
---|
101 | * @return None |
---|
102 | */ |
---|
103 | void gba_gotoxy(int _x, int _y) |
---|
104 | { |
---|
105 | _wherex = _x; |
---|
106 | _wherey = _y; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** |
---|
111 | * @brief gba_putchar function writes char-data to screen memory. |
---|
112 | * |
---|
113 | * Char code is index to font table. |
---|
114 | * |
---|
115 | * Input parameters: char, attribute and cordinates |
---|
116 | * @param c character code |
---|
117 | * @param textattr text attribute |
---|
118 | * @param x screen x coordinate |
---|
119 | * @param y screen y coordinate |
---|
120 | * @return None |
---|
121 | */ |
---|
122 | static void gba_putchar(char c, int textattr, int x, int y) |
---|
123 | { |
---|
124 | int f = textattr & 0x0F; |
---|
125 | int b = textattr >> 4; |
---|
126 | uint32_t fmask = f | f<<8 | f<<16 | f<<24; |
---|
127 | uint32_t bmask = b | b<<8 | b<<16 | b<<24; |
---|
128 | uint32_t *dest = (uint32_t *)&bg_bitmap4a[((y<<1) + y) << 1][x<<2]; |
---|
129 | const uint32_t *src = (uint32_t *)&(font3x5[(int)c]); |
---|
130 | uint32_t s; |
---|
131 | |
---|
132 | s = *src++; |
---|
133 | *dest = (fmask&s) | (bmask&~s); |
---|
134 | dest += GBA_LCD_WIDTH/sizeof(uint32_t); |
---|
135 | |
---|
136 | s = *src++; |
---|
137 | *dest = (fmask&s) | (bmask&~s); |
---|
138 | dest += GBA_LCD_WIDTH/sizeof(uint32_t); |
---|
139 | |
---|
140 | s = *src++; |
---|
141 | *dest = (fmask&s) | (bmask&~s); |
---|
142 | dest += GBA_LCD_WIDTH/sizeof(uint32_t); |
---|
143 | |
---|
144 | s = *src++; |
---|
145 | *dest = (fmask&s) | (bmask&~s); |
---|
146 | dest += GBA_LCD_WIDTH/sizeof(uint32_t); |
---|
147 | |
---|
148 | s = *src++; |
---|
149 | *dest = (fmask&s) | (bmask&~s); |
---|
150 | dest += GBA_LCD_WIDTH/sizeof(uint32_t); |
---|
151 | |
---|
152 | s = *src++; |
---|
153 | *dest = (fmask&s) | (bmask&~s); |
---|
154 | dest += GBA_LCD_WIDTH/sizeof(uint32_t); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** |
---|
159 | * @brief gba_textattr function set textattribute |
---|
160 | * |
---|
161 | * @param _attr text attribute |
---|
162 | * @return None |
---|
163 | */ |
---|
164 | void gba_textattr(int _attr) |
---|
165 | { |
---|
166 | _textattr = _attr; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * @brief gba_textbackground function set text background color |
---|
171 | * |
---|
172 | * @param _color backround color |
---|
173 | * @return None |
---|
174 | * |
---|
175 | */ |
---|
176 | void gba_textbackground(int _color) |
---|
177 | { |
---|
178 | _textattr = (_textattr & 0x0F) | (_color << 4); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * @brief gba_textcolor function set text color |
---|
183 | * |
---|
184 | * @param _colour text color |
---|
185 | * @return None |
---|
186 | */ |
---|
187 | void gba_textcolor(int _color) |
---|
188 | { |
---|
189 | _textattr = (_textattr & 0xF0) | (_color); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | /** |
---|
194 | * @brief gba_clearline function clear line number y |
---|
195 | * |
---|
196 | * Line is filled with spaces |
---|
197 | * |
---|
198 | * @param y line number |
---|
199 | * @return None |
---|
200 | */ |
---|
201 | static void gba_clearline(int y) |
---|
202 | { |
---|
203 | int x; |
---|
204 | |
---|
205 | for (x=0 ; x<=W ; x++) { |
---|
206 | gba_putchar(0, _textattr, x, y); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * @brief gba_nextline function moves cursor to next line and clears it |
---|
212 | * |
---|
213 | * @param None |
---|
214 | * @return None |
---|
215 | */ |
---|
216 | static void gba_nextline(void) |
---|
217 | { |
---|
218 | _wherex = 0; |
---|
219 | if (++_wherey >= H) { |
---|
220 | _wherey = 0; |
---|
221 | } |
---|
222 | gba_clearline(_wherey); |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * @brief gba_clrscr function clear screen |
---|
227 | * |
---|
228 | * @param None |
---|
229 | * @return None |
---|
230 | */ |
---|
231 | void gba_clrscr(void) |
---|
232 | { |
---|
233 | int y; |
---|
234 | |
---|
235 | for (y=0 ; y<=H ; y++) { |
---|
236 | gba_clearline(y); |
---|
237 | } |
---|
238 | gba_gotoxy(0,0); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * @brief gba_put function convert ascii char to font index and |
---|
243 | * write char to screen memory |
---|
244 | * |
---|
245 | * @param _c character code |
---|
246 | * @return None |
---|
247 | */ |
---|
248 | static void gba_put(char _c) |
---|
249 | { |
---|
250 | /* We have save some memory with reduced fonts */ |
---|
251 | _c = _c & 0x7F; /* no extened chars */ |
---|
252 | _c = _c - 0x20; /* no cntr chars */ |
---|
253 | gba_putchar(_c, _textattr, _wherex, _wherey); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | /** |
---|
258 | * @brief gba_putch function write ascii chars to screen |
---|
259 | * |
---|
260 | * @param _c character code |
---|
261 | * @return None |
---|
262 | */ |
---|
263 | void gba_putch(char _c) |
---|
264 | { |
---|
265 | switch (_c) { |
---|
266 | case ASCII_LF: |
---|
267 | gba_nextline(); |
---|
268 | break; |
---|
269 | case ASCII_CR: |
---|
270 | gba_gotoxy(0, _wherey); |
---|
271 | break; |
---|
272 | default: |
---|
273 | gba_put(_c); |
---|
274 | if (++_wherex >= W) |
---|
275 | { |
---|
276 | gba_nextline(); |
---|
277 | } |
---|
278 | break; |
---|
279 | } |
---|
280 | return; |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * @brief gba_puts function write ascii string to screen |
---|
285 | * |
---|
286 | * @param _str ASCII string |
---|
287 | * @return None |
---|
288 | */ |
---|
289 | void gba_puts(const char *_str) |
---|
290 | { |
---|
291 | while (*_str) { |
---|
292 | gba_putch(*_str++); |
---|
293 | } |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * @brief gba_printf function do formated printf |
---|
299 | * |
---|
300 | * @param _format printf format string |
---|
301 | * @param ... parameters specified in format string |
---|
302 | * @return None |
---|
303 | */ |
---|
304 | int gba_printf(const char *_format, ...) |
---|
305 | { |
---|
306 | char s[256]; |
---|
307 | va_list marker; |
---|
308 | va_start(marker, _format); |
---|
309 | int r = vsprintf(s, _format, marker); |
---|
310 | va_end(marker); |
---|
311 | gba_puts(s); |
---|
312 | return r; |
---|
313 | } |
---|
314 | |
---|
315 | /** |
---|
316 | * @brief gba_initconio function initialize console |
---|
317 | * |
---|
318 | * @param None |
---|
319 | * @return None |
---|
320 | */ |
---|
321 | void gba_initconio(void) |
---|
322 | { |
---|
323 | GBA_REG_DISPCNT = GBA_DISP_MODE_4 | GBA_DISP_BG2_ON;/* 256 color bitmapped mode */ |
---|
324 | const BgAffineDestData bgAffineReset = {256,0,0,256,0,-256*2}; |
---|
325 | rBg2Affine = bgAffineReset; |
---|
326 | bg_palette[BLACK ] = RGB( 0, 0, 0); /* BLACK */ |
---|
327 | bg_palette[BLUE ] = RGB( 0, 0,16); /* BLUE */ |
---|
328 | bg_palette[GREEN ] = RGB( 0,16, 0); /* GREEN */ |
---|
329 | bg_palette[CYAN ] = RGB( 0,16,16); /* CYAN */ |
---|
330 | bg_palette[RED ] = RGB(16, 0, 0); /* RED */ |
---|
331 | bg_palette[MAGENTA ] = RGB(16, 0,16); /* MAGENTA */ |
---|
332 | bg_palette[BROWN ] = RGB(16,16, 0); /* BROWN */ |
---|
333 | bg_palette[LIGHTGRAY ] = RGB(24,24,24); /* LIGHTGRAY */ |
---|
334 | bg_palette[DARKGRAY ] = RGB(16,16,16); /* DARKGRAY */ |
---|
335 | bg_palette[LIGHTBLUE ] = RGB( 0, 0,31); /* LIGHTBLUE */ |
---|
336 | bg_palette[LIGHTGREEN ] = RGB( 0,31, 0); /* LIGHTGREEN */ |
---|
337 | bg_palette[LIGHTCYAN ] = RGB( 0,31,31); /* LIGHTCYAN */ |
---|
338 | bg_palette[LIGHTRED ] = RGB(31, 0, 0); /* LIGHTRED */ |
---|
339 | bg_palette[LIGHTMAGENTA] = RGB(31, 0,31); /* LIGHTMAGENTA */ |
---|
340 | bg_palette[YELLOW ] = RGB(31,31, 0); /* YELLOW */ |
---|
341 | bg_palette[WHITE ] = RGB(31,31,31); /* WHITE */ |
---|
342 | gba_textattr(0); |
---|
343 | gba_textcolor(DEF_TEXTCOLOR); |
---|
344 | gba_textbackground(DEF_TEXTBACKGROUND); |
---|
345 | gba_clrscr(); |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * @brief gba_textmode function set console mode |
---|
350 | * |
---|
351 | * @param _mode console mode code |
---|
352 | * @return None |
---|
353 | */ |
---|
354 | void gba_textmode(int _mode) |
---|
355 | { |
---|
356 | switch (_mode) { |
---|
357 | case CO60: { |
---|
358 | gba_initconio(); |
---|
359 | break; |
---|
360 | } |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | |
---|
365 | /** |
---|
366 | * @brief delay_loop function is simple delay loop |
---|
367 | * |
---|
368 | * @param count loop counter |
---|
369 | * @return None |
---|
370 | */ |
---|
371 | static void delay_loop(unsigned int count) |
---|
372 | { |
---|
373 | int i; |
---|
374 | |
---|
375 | for (i = 0; i<count; i++) i = i; |
---|
376 | } |
---|
377 | |
---|
378 | static unsigned char inputch = ASCII_CR; /**< input character value */ |
---|
379 | /** |
---|
380 | * @brief gba_getch function read char from game pad keys |
---|
381 | * |
---|
382 | * Character input is done with GBA buttons, |
---|
383 | * up-down-left-right/A/B/R/L/Select/Start |
---|
384 | * - Select-key accept selected character |
---|
385 | * - Start-key read CR (Enter) |
---|
386 | * - A-key select 'A' character |
---|
387 | * - B-key select 'Z' character |
---|
388 | * - R-key select '1' character |
---|
389 | * - L-key select '9' character |
---|
390 | * - up-key increment character ('A'->'B') |
---|
391 | * - down-key decrement character ('B'-'A') |
---|
392 | * - left-key change set of character ('!'->'A'->'a') |
---|
393 | * - right-key change set of character ('a'->'A'->'!') |
---|
394 | * |
---|
395 | * @param None |
---|
396 | * @return Selected char code |
---|
397 | */ |
---|
398 | int gba_getch(void) |
---|
399 | { |
---|
400 | int keyx, key = 0; |
---|
401 | |
---|
402 | while(1) { |
---|
403 | key = GBA_KEY(); |
---|
404 | while ( (keyx=GBA_KEY())==key ); |
---|
405 | switch (key) { |
---|
406 | case GBA_KEY_SELECT: |
---|
407 | gba_put(inputch); |
---|
408 | return inputch; |
---|
409 | break; |
---|
410 | case GBA_KEY_START: |
---|
411 | gba_put(' '); |
---|
412 | inputch = ASCII_CR; |
---|
413 | return inputch; |
---|
414 | break; |
---|
415 | case GBA_KEY_A: |
---|
416 | inputch = 'A'; |
---|
417 | break; |
---|
418 | case GBA_KEY_B: |
---|
419 | inputch = 'Z'; |
---|
420 | break; |
---|
421 | case GBA_KEY_UP: |
---|
422 | if ((inputch-1) >= 0x20) inputch--; |
---|
423 | break; |
---|
424 | case GBA_KEY_DOWN: |
---|
425 | if ((inputch+1) <= 0x7E) inputch++; |
---|
426 | break; |
---|
427 | case GBA_KEY_LEFT: |
---|
428 | if ((inputch - 0x20) >= 0x20) inputch -= 0x20; |
---|
429 | break; |
---|
430 | case GBA_KEY_RIGHT: |
---|
431 | if ((inputch + 0x20) <= 0x7E) inputch += 0x20; |
---|
432 | break; |
---|
433 | case GBA_KEY_R: |
---|
434 | inputch = '1'; |
---|
435 | break; |
---|
436 | case GBA_KEY_L: |
---|
437 | inputch = '9'; |
---|
438 | break; |
---|
439 | default: |
---|
440 | break; |
---|
441 | } |
---|
442 | |
---|
443 | gba_put(inputch); |
---|
444 | delay_loop(1000); |
---|
445 | } |
---|
446 | } |
---|