source: rtems/c/src/lib/libbsp/arm/gba/console/conio.c @ 5bdda7f

4.115
Last change on this file since 5bdda7f was 5bdda7f, checked in by Joel Sherrill <joel.sherrill@…>, on 10/20/14 at 14:12:46

arm/gba/console/conio.c: Fix warning

  • Property mode set to 100644
File size: 12.0 KB
Line 
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
47typedef unsigned short      Palette[256];                              /**< 256 colors */
48typedef unsigned short      Palettes[16][16];                          /**< 16 palettes with each 16 colors */
49typedef unsigned short      Bitmap3[GBA_LCD_HEIGHT][GBA_LCD_WIDTH];    /**< 16 bits, single buffered */
50typedef unsigned char       Bitmap4[GBA_LCD_HEIGHT][GBA_LCD_WIDTH];    /**< 8 bits, double buffered  */
51typedef 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 */
70typedef 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
79typedef 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
86int  _wherex;                /**< Screen X coordinate */
87int  _wherey;                /**< Screen Y coordinate */
88int  _textattr;              /**< Text attribute      */
89
90/*
91 * Forward reference
92 */
93static void gba_initconio(void);
94
95/*---------------------------------------------------------------------------*
96 *  Defaultfont                                                              *
97 *---------------------------------------------------------------------------*/
98#include "defaultfont.h"
99
100/**
101 *  @brief gba_gotoxy function set screeen xy-coordinates
102 *
103 *  @param  _x screen x coordinate
104 *  @param  _y screen y coordinate
105 *  @return None
106 */
107void gba_gotoxy(int _x, int _y)
108{
109    _wherex = _x;
110    _wherey = _y;
111}
112
113
114/**
115 *  @brief gba_putchar function writes char-data to screen memory.
116 *
117 *  Char code is index to font table.
118 *
119 *  Input parameters:   char, attribute and cordinates
120 *  @param  c character code
121 *  @param  textattr text attribute
122 *  @param  x screen x coordinate
123 *  @param  y screen y coordinate
124 *  @return None
125 */
126static void gba_putchar(char c, int textattr, int x, int y)
127{
128  int       f = textattr & 0x0F;
129  int       b = textattr >> 4;
130  uint32_t  fmask = f | f<<8 | f<<16 | f<<24;
131  uint32_t  bmask = b | b<<8 | b<<16 | b<<24;
132  uint32_t  *dest = (uint32_t *)&bg_bitmap4a[((y<<1) + y) << 1][x<<2];
133  const uint32_t  *src = (uint32_t *)&(font3x5[(int)c]);
134  uint32_t s;
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  s = *src++;
157  *dest = (fmask&s) | (bmask&~s);
158  dest += GBA_LCD_WIDTH/sizeof(uint32_t);
159}
160
161
162/**
163 *  @brief gba_textattr function set textattribute
164 *
165 *  @param  _attr text attribute
166 *  @return None
167 */
168void gba_textattr(int _attr)
169{
170  _textattr = _attr;
171}
172
173/**
174 *  @brief gba_textbackground function set text background color
175 *
176 *  @param  _color backround color
177 *  @return None
178 *
179 */
180void gba_textbackground(int _color)
181{
182  _textattr = (_textattr & 0x0F) | (_color << 4);
183}
184
185/**
186 *  @brief gba_textcolor function set text color
187 *
188 *  @param  _colour text color
189 *  @return None
190 */
191void gba_textcolor(int _color)
192{
193  _textattr = (_textattr & 0xF0) | (_color);
194}
195
196
197/**
198 *  @brief gba_clearline function clear line number y
199 *
200 *  Line is filled with spaces
201 *
202 *  @param  y line number
203 *  @return None
204 */
205static void gba_clearline(int y)
206{
207  int x;
208
209  for (x=0 ; x<=W ; x++) {
210     gba_putchar(0, _textattr, x, y);
211  }
212}
213
214/**
215 *  @brief gba_nextline function moves cursor to next line and clears it
216 *
217 *  @param  None
218 *  @return None
219 */
220static void gba_nextline(void)
221{
222    _wherex = 0;
223    if (++_wherey >= H) {
224       _wherey = 0;
225    }
226    gba_clearline(_wherey);
227}
228
229/**
230 *  @brief gba_clrscr function clear screen
231 *
232 *  @param  None
233 *  @return None
234 */
235void gba_clrscr(void)
236{
237    int y;
238
239    for (y=0 ; y<=H ; y++) {
240       gba_clearline(y);
241    }
242    gba_gotoxy(0,0);
243}
244
245/**
246 *  @brief gba_put function convert ascii char to font index and
247 *  write char to screen memory
248 *
249 *  @param  _c character code
250 *  @return None
251 */
252static void gba_put(char _c)
253{
254  /* We have save some memory with reduced fonts */
255  _c = _c & 0x7F;   /* no extened chars */
256  _c = _c - 0x20;   /* no cntr chars    */
257  gba_putchar(_c, _textattr, _wherex, _wherey);
258}
259
260
261/**
262 *  @brief gba_putch function write ascii chars to screen
263 *
264 *  @param  _c character code
265 *  @return None
266 */
267void gba_putch(char _c)
268{
269    switch (_c) {
270        case ASCII_LF:
271            gba_nextline();
272            break;
273        case ASCII_CR:
274            gba_gotoxy(0, _wherey);
275            break;
276        default:
277            gba_put(_c);
278            if (++_wherex >= W)
279            {
280               gba_nextline();
281            }
282            break;
283    }
284    return;
285}
286
287/**
288 *  @brief gba_puts function write ascii string to screen
289 *
290 *  @param  _str ASCII string
291 *  @return None
292 */
293void gba_puts(const char *_str)
294{
295    while (*_str) {
296       gba_putch(*_str++);
297    }
298    return;
299}
300
301/**
302 *  @brief gba_printf function do formated printf
303 *
304 *  @param  _format printf format string
305 *  @param  ... parameters specified in format string
306 *  @return None
307 */
308int gba_printf(const char *_format, ...)
309{
310    char s[256];
311    va_list marker;
312    va_start(marker, _format);
313    int r = vsprintf(s, _format, marker);
314    va_end(marker);
315    gba_puts(s);
316    return r;
317}
318
319/**
320 *  @brief gba_initconio function initialize console
321 *
322 *  @param  None
323 *  @return None
324 */
325static void gba_initconio(void)
326{
327    GBA_REG_DISPCNT = GBA_DISP_MODE_4 | GBA_DISP_BG2_ON;/*  256 color bitmapped mode */
328    const BgAffineDestData bgAffineReset = {256,0,0,256,0,-256*2};
329    rBg2Affine = bgAffineReset;
330    bg_palette[BLACK       ] =  RGB( 0, 0, 0);   /*  BLACK */
331    bg_palette[BLUE        ] =  RGB( 0, 0,16);   /*  BLUE */
332    bg_palette[GREEN       ] =  RGB( 0,16, 0);   /*  GREEN */
333    bg_palette[CYAN        ] =  RGB( 0,16,16);   /*  CYAN */
334    bg_palette[RED         ] =  RGB(16, 0, 0);   /*  RED */
335    bg_palette[MAGENTA     ] =  RGB(16, 0,16);   /*  MAGENTA */
336    bg_palette[BROWN       ] =  RGB(16,16, 0);   /*  BROWN */
337    bg_palette[LIGHTGRAY   ] =  RGB(24,24,24);   /*  LIGHTGRAY */
338    bg_palette[DARKGRAY    ] =  RGB(16,16,16);   /*  DARKGRAY */
339    bg_palette[LIGHTBLUE   ] =  RGB( 0, 0,31);   /*  LIGHTBLUE */
340    bg_palette[LIGHTGREEN  ] =  RGB( 0,31, 0);   /*  LIGHTGREEN */
341    bg_palette[LIGHTCYAN   ] =  RGB( 0,31,31);   /*  LIGHTCYAN */
342    bg_palette[LIGHTRED    ] =  RGB(31, 0, 0);   /*  LIGHTRED */
343    bg_palette[LIGHTMAGENTA] =  RGB(31, 0,31);   /*  LIGHTMAGENTA */
344    bg_palette[YELLOW      ] =  RGB(31,31, 0);   /*  YELLOW */
345    bg_palette[WHITE       ] =  RGB(31,31,31);   /*  WHITE */
346    gba_textattr(0);
347    gba_textcolor(DEF_TEXTCOLOR);
348    gba_textbackground(DEF_TEXTBACKGROUND);
349    gba_clrscr();
350}
351
352/**
353 *  @brief gba_textmode function set console mode
354 *
355 *  @param  _mode console mode code
356 *  @return None
357 */
358void gba_textmode(int _mode)
359{
360  switch (_mode) {
361    case CO60: {
362      gba_initconio();
363      break;
364    }
365  }
366}
367
368
369/**
370 *  @brief delay_loop function is simple delay loop
371 *
372 *  @param  count loop counter
373 *  @return None
374 */
375static void delay_loop(unsigned int count)
376{
377  int i;
378
379  for (i = 0; i<count; i++) i = i;
380}
381
382static unsigned char inputch = ASCII_CR;    /**< input character value */
383/**
384 *  @brief gba_getch function read char from game pad keys
385 *
386 *  Character input is done with GBA buttons,
387 *      up-down-left-right/A/B/R/L/Select/Start
388 *  - Select-key accept selected character
389 *  - Start-key read CR (Enter)
390 *  - A-key select 'A' character
391 *  - B-key select 'Z' character
392 *  - R-key select '1' character
393 *  - L-key select '9' character
394 *  - up-key increment character ('A'->'B')
395 *  - down-key decrement character ('B'-'A')
396 *  - left-key change set of character ('!'->'A'->'a')
397 *  - right-key change set of character ('a'->'A'->'!')
398 *
399 *  @param  None
400 *  @return Selected char code
401 */
402int gba_getch(void)
403{
404  int  keyx, key  = 0;
405
406  while(1) {
407    key = GBA_KEY();
408    while ( (keyx=GBA_KEY())==key );
409    switch (key) {
410      case GBA_KEY_SELECT:
411        gba_put(inputch);
412        return inputch;
413        break;
414      case GBA_KEY_START:
415        gba_put(' ');
416        inputch = ASCII_CR;
417        return inputch;
418        break;
419      case GBA_KEY_A:
420        inputch = 'A';
421        break;
422      case GBA_KEY_B:
423        inputch = 'Z';
424        break;
425      case GBA_KEY_UP:
426        if ((inputch-1) >= 0x20) inputch--;
427        break;
428      case GBA_KEY_DOWN:
429        if ((inputch+1) <=  0x7E) inputch++;
430        break;
431      case GBA_KEY_LEFT:
432        if ((inputch - 0x20) >= 0x20) inputch -= 0x20;
433        break;
434      case GBA_KEY_RIGHT:
435        if ((inputch + 0x20) <= 0x7E) inputch += 0x20;
436        break;
437      case GBA_KEY_R:
438        inputch = '1';
439        break;
440      case GBA_KEY_L:
441        inputch = '9';
442        break;
443      default:
444        break;
445    }
446
447    gba_put(inputch);
448    delay_loop(1000);
449  }
450}
Note: See TracBrowser for help on using the repository browser.