summaryrefslogblamecommitdiffstats
path: root/c/src/lib/libbsp/arm/gba/console/conio.c
blob: 3cb053a5f5035a375c6941894efcfc8fd6bfd400 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428











































































































































































































































































































































































































































                                                                                                              
/**
 *  @file conio.c
 *
 *  This file contains the GBA conio I/O package.
 */
/*
 *  RTEMS GBA BSP
 *
 *  Copyright (c) 2004  Markku Puro <markku.puro@kopteri.net>
 *  based on MyLib by Rafael Vuijk (aka Dark Fader)
 *
 *  The license and distribution terms for this file may be
 *  found in found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */

/*****************************************************************************
 * This source file is based on work by Rafael Vuijk (aka Dark Fader)
 *****************************************************************************
 *****************************************************************************
 * MyLib by Rafael Vuijk (aka Dark Fader)
 * :
 * This file is released into the public domain for commercial
 * or non-commercial usage with no restrictions placed upon it.
 *****************************************************************************/

/*---------------------------------------------------------------------------*
 *   Includes                                                                *
 *---------------------------------------------------------------------------*/
#include <stdint.h>
#include <rtems/score/types.h>
#include <gba.h>
#include <conio.h>
#include <stdio.h>
#include <stdarg.h>

/*---------------------------------------------------------------------------*
 * Defines                                                                   *
 *---------------------------------------------------------------------------*/
#define W                   60                        /**< Screen width      */
#define H                   26                        /**< Screen height     */
#define CLRSCR_SIZE         (W*4 * H*6)               /**< Screen size       */
#define DEFAULT_FONT_WIDTH  4                         /**< Font width        */
#define DEFAULT_FONT_HEIGHT 6                         /**< Font height       */

typedef unsigned short      Palette[256];                              /**< 256 colors */
typedef unsigned short      Palettes[16][16];                          /**< 16 palettes with each 16 colors */
typedef unsigned short      Bitmap3[GBA_LCD_HEIGHT][GBA_LCD_WIDTH];    /**< 16 bits, single buffered */
typedef unsigned char       Bitmap4[GBA_LCD_HEIGHT][GBA_LCD_WIDTH];    /**< 8 bits, double buffered  */
typedef unsigned short      Bitmap5[GBA_MODE5_HEIGHT][GBA_MODE5_WIDTH];/**< double buffered          */

#define VRAM                GBA_VRAM_ADDR             /**< VRAM address      */
#define VRAM_END            (VRAM +    0x18000)       /**< VRAM end          */
#define BG_BITMAP0_VRAM     (VRAM +        0x0)       /**< BG Bitmap 0 RAM   */
#define BG_BITMAP1_VRAM     (VRAM +     0xa000)       /**< BG Bitmap 1 RAM   */

#define bg_bitmap0          (*(Bitmap4 *)BG_BITMAP0_VRAM)
#define bg_bitmap1          (*(Bitmap4 *)BG_BITMAP1_VRAM)
#define bg_bitmap3          (*(Bitmap3 *)BG_BITMAP0_VRAM)
#define bg_bitmap4a         (*(Bitmap4 *)BG_BITMAP0_VRAM)
#define bg_bitmap4b         (*(Bitmap4 *)BG_BITMAP1_VRAM)
#define bg_bitmap5a         (*(Bitmap5 *)BG_BITMAP0_VRAM)
#define bg_bitmap5b         (*(Bitmap5 *)BG_BITMAP1_VRAM)

/** Color conversion macro  */
#define RGB(r,g,b)          ( (r)<<0 | (g)<<5 | (b)<<10 )

/** BG Affine Transformation Destination Data Structure */
typedef struct {
    int16_t H_DiffX;        /**< Line Direction X Coordinate Difference     */
    int16_t V_DiffX;        /**< Vertical Direction X Coordinate Difference */
    int16_t H_DiffY;        /**< Line Direction Y Coordinate Difference     */
    int16_t V_DiffY;        /**< Vertical Direction Y Coordinate Difference */
    int32_t StartX;         /**< Start X Coordinate                         */
    int32_t StartY;         /**< Start Y Coordinate                         */
} BgAffineDestData;

typedef volatile BgAffineDestData   vBgAffineDestData;
#define rBg2Affine          (*(vBgAffineDestData *)0x4000020)

/** 256 colors for background(s) */
#define bg_palette          (*(Palette *)(GBA_PAL_RAM_ADDR))


int  _wherex;                /**< Screen X coordinate */
int  _wherey;                /**< Screen Y coordinate */
int  _textattr;              /**< Text attribute      */


/*---------------------------------------------------------------------------*
 *  Defaultfont                                                              *
 *---------------------------------------------------------------------------*/
#include "defaultfont.c"

/**
 *  @brief gba_gotoxy function set screeen xy-coordinates
 *
 *  @param  _x screen x coordinate
 *  @param  _y screen y coordinate
 *  @return None
 */
void gba_gotoxy(int _x, int _y)
{
    _wherex = _x;
    _wherey = _y;
}


/**
 *  @brief gba_putchar function writes char-data to screen memory.
 *
 *  Char code is index to font table.
 *
 *  Input parameters:   char, attribute and cordinates
 *  @param  c character code
 *  @param  textattr text attribute
 *  @param  x screen x coordinate
 *  @param  y screen y coordinate
 *  @return None
 */
void gba_putchar(char c, int textattr, int x, int y)
{
    int       f = textattr & 0x0F;
    int       b = textattr >> 4;
    uint32_t  fmask = f | f<<8 | f<<16 | f<<24;
    uint32_t  bmask = b | b<<8 | b<<16 | b<<24;
    uint32_t  *dest = (uint32_t *)&bg_bitmap4a[((y<<1) + y) << 1][x<<2];
    const uint32_t  *src = (uint32_t *)&(font3x5[(int)c]);
    uint32_t s;
    s = *src++; *dest = (fmask&s) | (bmask&~s); dest += GBA_LCD_WIDTH/sizeof(uint32_t);
    s = *src++; *dest = (fmask&s) | (bmask&~s); dest += GBA_LCD_WIDTH/sizeof(uint32_t);
    s = *src++; *dest = (fmask&s) | (bmask&~s); dest += GBA_LCD_WIDTH/sizeof(uint32_t);
    s = *src++; *dest = (fmask&s) | (bmask&~s); dest += GBA_LCD_WIDTH/sizeof(uint32_t);
    s = *src++; *dest = (fmask&s) | (bmask&~s); dest += GBA_LCD_WIDTH/sizeof(uint32_t);
    s = *src++; *dest = (fmask&s) | (bmask&~s); dest += GBA_LCD_WIDTH/sizeof(uint32_t);
}


/**
 *  @brief gba_textattr function set textattribute
 *
 *  @param  _attr text attribute
 *  @return None
 */
void gba_textattr(int _attr)
{
    _textattr = _attr;
}

/**
 *  @brief gba_textbackground function set text background color
 *
 *  @param  _color backround color
 *  @return None

 */
void gba_textbackground(int _color)
{
    _textattr = (_textattr & 0x0F) | (_color << 4);
}

/**
 *  @brief gba_textcolor function set text color
 *
 *  @param  _colour text color
 *  @return None
 */
void gba_textcolor(int _color)
{
    _textattr = (_textattr & 0xF0) | (_color);
}


/**
 *  @brief gba_clearline function clear line nro y
 *
 *  Line is filled with spaces
 *
 *  @param  y line number
 *  @return None
 */
void gba_clearline(int y)
{
    int x;

    for (x=0 ; x<=W ; x++) {
       gba_putchar(0, _textattr, x, y);
    }
}

/**
 *  @brief gba_nextline function moves cursor to next line and clears it
 *
 *  @param  None
 *  @return None
 */
void gba_nextline(void)
{
    _wherex = 0;
    if (++_wherey >= H) {
       _wherey = 0;
    }
    gba_clearline(_wherey);
}

/**
 *  @brief gba_clrscr function clear screen
 *
 *  @param  None
 *  @return None
 */
void gba_clrscr(void)
{
    int y;

    for (y=0 ; y<=H ; y++) {
       gba_clearline(y);
    }
    gba_gotoxy(0,0);
}

/**
 *  @brief gba_put function convert ascii char to font index and
 *  write char to screen memory
 *
 *  @param  _c character code
 *  @return None
 */
void gba_put(char _c)
{
  /* We have save some memory with reduced fonts */
  _c = _c & 0x7F;   /* no extened chars */
  _c = _c - 0x20;   /* no cntr chars    */
  gba_putchar(_c, _textattr, _wherex, _wherey);
}


/**
 *  @brief gba_putch function write ascii chars to screen
 *
 *  @param  _c character code
 *  @return None
 */
void gba_putch(char _c)
{
    switch (_c) {
        case ASCII_LF:
            gba_nextline();
            break;
        case ASCII_CR:
            gba_gotoxy(0, _wherey);
            break;
        default:
            gba_put(_c);
            if (++_wherex >= W)
            {
               gba_nextline();
            }
            break;
    }
    return;
}

/**
 *  @brief gba_puts function write ascii string to screen
 *
 *  @param  _str ASCII string
 *  @return None
 */
void gba_puts(const char *_str)
{
    while (*_str) {
       gba_putch(*_str++);
    }
    return;
}

/**
 *  @brief gba_printf function do formated printf
 *
 *  @param  _format printf format string
 *  @param  ... parameters specified in format string
 *  @return None
 */
int gba_printf(const char *_format, ...)
{
    char s[256];
    va_list marker;
    va_start(marker, _format);
    int r = vsprintf(s, _format, marker);
    va_end(marker);
    gba_puts(s);
    return r;
}

/**
 *  @brief gba_initconio function initialize console
 *
 *  @param  None
 *  @return None
 */
void gba_initconio(void)
{
    GBA_REG_DISPCNT = GBA_DISP_MODE_4 | GBA_DISP_BG2_ON;/*  256 color bitmapped mode */
    const BgAffineDestData bgAffineReset = {256,0,0,256,0,-256*2};
    rBg2Affine = bgAffineReset;
    bg_palette[BLACK       ] =  RGB( 0, 0, 0);   /*  BLACK */
    bg_palette[BLUE        ] =  RGB( 0, 0,16);   /*  BLUE */
    bg_palette[GREEN       ] =  RGB( 0,16, 0);   /*  GREEN */
    bg_palette[CYAN        ] =  RGB( 0,16,16);   /*  CYAN */
    bg_palette[RED         ] =  RGB(16, 0, 0);   /*  RED */
    bg_palette[MAGENTA     ] =  RGB(16, 0,16);   /*  MAGENTA */
    bg_palette[BROWN       ] =  RGB(16,16, 0);   /*  BROWN */
    bg_palette[LIGHTGRAY   ] =  RGB(24,24,24);   /*  LIGHTGRAY */
    bg_palette[DARKGRAY    ] =  RGB(16,16,16);   /*  DARKGRAY */
    bg_palette[LIGHTBLUE   ] =  RGB( 0, 0,31);   /*  LIGHTBLUE */
    bg_palette[LIGHTGREEN  ] =  RGB( 0,31, 0);   /*  LIGHTGREEN */
    bg_palette[LIGHTCYAN   ] =  RGB( 0,31,31);   /*  LIGHTCYAN */
    bg_palette[LIGHTRED    ] =  RGB(31, 0, 0);   /*  LIGHTRED */
    bg_palette[LIGHTMAGENTA] =  RGB(31, 0,31);   /*  LIGHTMAGENTA */
    bg_palette[YELLOW      ] =  RGB(31,31, 0);   /*  YELLOW */
    bg_palette[WHITE       ] =  RGB(31,31,31);   /*  WHITE */
    gba_textattr(0);
    gba_textcolor(DEF_TEXTCOLOR);
    gba_textbackground(DEF_TEXTBACKGROUND);
    gba_clrscr();
}

/**
 *  @brief gba_textmode function set console mode
 *
 *  @param  _mode console mode code
 *  @return None
 */
void gba_textmode(int _mode)
{
    switch (_mode) {
        case CO60:
        {
            gba_initconio();
            break;
        }
    }
}


/**
 *  @brief delay_loop function is simple delay loop
 *
 *  @param  count loop counter
 *  @return None
 */
void delay_loop(unsigned int count)
{
    int i;
    for (i = 0; i<count; i++) i = i;
}

static unsigned char inputch = ASCII_CR;    /**< input character value */
/**
 *  @brief gba_getch function read char from game pad keys
 *
 *  Character input is done with GBA buttons, up-down-left-right/A/B/R/L/Select/Start
 *  - Select-key accept selected character
 *  - Start-key read CR (Enter)
 *  - A-key select 'A' character
 *  - B-key select 'Z' character
 *  - R-key select '1' character
 *  - L-key select '9' character
 *  - up-key increment character ('A'->'B')
 *  - down-key decrement character ('B'-'A')
 *  - left-key change set of character ('!'->'A'->'a')
 *  - right-key change set of character ('a'->'A'->'!')
 *
 *  @param  None
 *  @return Selected char code
 */
char gba_getch(void)
{
  int  keyx, key  = 0;

  while(1) {
      key = GBA_KEY();
      while ( (keyx=GBA_KEY())==key );
      switch (key)
      {
        case GBA_KEY_SELECT:
            gba_put(inputch);
            return inputch;
            break;
        case GBA_KEY_START:
            gba_put(' ');
            inputch = ASCII_CR;
            return inputch;
            break;
        case GBA_KEY_A:
            inputch = 'A';
            break;
        case GBA_KEY_B:
            inputch = 'Z';
            break;
        case GBA_KEY_UP:
            if ((inputch-1) >= 0x20) inputch--;
            break;
        case GBA_KEY_DOWN:
            if ((inputch+1) <=  0x7E) inputch++;
            break;
        case GBA_KEY_LEFT:
            if ((inputch - 0x20) >= 0x20) inputch -= 0x20;
            break;
        case GBA_KEY_RIGHT:
            if ((inputch + 0x20) <= 0x7E) inputch += 0x20;
            break;
        case GBA_KEY_R:
            inputch = '1';
            break;
        case GBA_KEY_L:
            inputch = '9';
            break;
        default:
            break;
      }
      gba_put(inputch);
      delay_loop(1000);
  }
}