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

4.9
Last change on this file since 04b38ae was 04b38ae, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/30/08 at 04:35:31

2008-09-30 Ralf Corsépius <ralf.corsepius@…>

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