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

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

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