1 | /* |
---|
2 | * conio.c : ARM GBA BSP |
---|
3 | * |
---|
4 | * This file contains the GBA conio I/O package. |
---|
5 | * |
---|
6 | * |
---|
7 | * Copyright (c) 2004 Markku Puro <markku.puro@kopteri.net> |
---|
8 | * |
---|
9 | * This source file is based on work by Rafael Vuijk (aka Dark Fader) |
---|
10 | * 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 | /*---------------------------------------------------------------------------* |
---|
21 | * Includes * |
---|
22 | *---------------------------------------------------------------------------*/ |
---|
23 | #include <rtems/score/types.h> |
---|
24 | #include <s3c2400.h> |
---|
25 | #include <conio.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdarg.h> |
---|
28 | |
---|
29 | #define gp_initButtons() {rPBCON=0x0;} |
---|
30 | #define gp_getButtons() ((((~rPEDAT >> 6) & 0x3) << 8) | \ |
---|
31 | ((~rPBDAT >> 8) & 0xFF)) |
---|
32 | |
---|
33 | /*---------------------------------------------------------------------------* |
---|
34 | * Defines * |
---|
35 | *---------------------------------------------------------------------------*/ |
---|
36 | #define LCD_WIDTH 240 |
---|
37 | #define LCD_HEIGHT 320 |
---|
38 | #define DEFAULT_FONT_WIDTH 4 |
---|
39 | #define DEFAULT_FONT_HEIGHT 6 |
---|
40 | #define W (LCD_WIDTH/DEFAULT_FONT_WIDTH) |
---|
41 | #define H (LCD_HEIGHT/DEFAULT_FONT_HEIGHT) |
---|
42 | |
---|
43 | typedef unsigned char Bitmap[LCD_HEIGHT][LCD_WIDTH]; |
---|
44 | #define GP32_VRAM 0x0c7b4000 |
---|
45 | #define bg_bitmap (*(Bitmap *)GP32_VRAM) |
---|
46 | |
---|
47 | /* color conversion */ |
---|
48 | #define RGB(r,g,b) ( (r)<<11 | (g)<<6 | (b)<<1 ) |
---|
49 | |
---|
50 | int _wherex; |
---|
51 | int _wherey; |
---|
52 | int _textattr; |
---|
53 | |
---|
54 | |
---|
55 | /*---------------------------------------------------------------------------* |
---|
56 | * Defaultfont * |
---|
57 | *---------------------------------------------------------------------------*/ |
---|
58 | #include "defaultfont.c" |
---|
59 | |
---|
60 | /*---------------------------------------------------------------------------* |
---|
61 | * gp32_gotoxy * |
---|
62 | *---------------------------------------------------------------------------*/ |
---|
63 | void gpconio_gotoxy(int _x, int _y) |
---|
64 | { |
---|
65 | _wherex = _x; |
---|
66 | _wherey = _y; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | /*---------------------------------------------------------------------------* |
---|
71 | * PutChar * |
---|
72 | *---------------------------------------------------------------------------*/ |
---|
73 | void PutChar(char c, int textattr, int x, int y) |
---|
74 | { |
---|
75 | int f = textattr & 0x0F; |
---|
76 | int b = textattr >> 4; |
---|
77 | uint32_t fmask = f | (f << 8) | (f << 16) | (f << 24); |
---|
78 | uint32_t bmask = b | (b << 8) | (b << 16) | (b << 24); |
---|
79 | uint32_t *dest = (uint32_t *)&bg_bitmap[((y << 1) + y) << 1][x << 2]; |
---|
80 | const uint32_t *src = (uint32_t *)&(font3x5[(int)c]); |
---|
81 | uint32_t s; |
---|
82 | |
---|
83 | s = *src++; |
---|
84 | *dest = (fmask&s) | (bmask&~s); |
---|
85 | dest += LCD_WIDTH / sizeof(uint32_t); |
---|
86 | |
---|
87 | s = *src++; |
---|
88 | *dest = (fmask&s) | (bmask&~s); |
---|
89 | dest += LCD_WIDTH / sizeof(uint32_t); |
---|
90 | |
---|
91 | s = *src++; |
---|
92 | *dest = (fmask&s) | (bmask&~s); |
---|
93 | dest += LCD_WIDTH / sizeof(uint32_t); |
---|
94 | |
---|
95 | s = *src++; |
---|
96 | *dest = (fmask&s) | (bmask&~s); |
---|
97 | dest += LCD_WIDTH / sizeof(uint32_t); |
---|
98 | |
---|
99 | s = *src++; |
---|
100 | *dest = (fmask&s) | (bmask&~s); |
---|
101 | dest += LCD_WIDTH / sizeof(uint32_t); |
---|
102 | |
---|
103 | s = *src++; |
---|
104 | *dest = (fmask&s) | (bmask&~s); |
---|
105 | dest += LCD_WIDTH / sizeof(uint32_t); |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | /*---------------------------------------------------------------------------* |
---|
110 | * gp32_textattr * |
---|
111 | *---------------------------------------------------------------------------*/ |
---|
112 | void gpconio_textattr(int _attr) |
---|
113 | { |
---|
114 | _textattr = _attr; |
---|
115 | } |
---|
116 | |
---|
117 | /*---------------------------------------------------------------------------* |
---|
118 | * gp32_textbackground * |
---|
119 | *---------------------------------------------------------------------------*/ |
---|
120 | void gpconio_textbackground(int _color) |
---|
121 | { |
---|
122 | _textattr = (_textattr & 0x0F) | (_color << 4); |
---|
123 | } |
---|
124 | |
---|
125 | /*---------------------------------------------------------------------------* |
---|
126 | * gp32_textcolor * |
---|
127 | *---------------------------------------------------------------------------*/ |
---|
128 | void gpconio_textcolor(int _color) |
---|
129 | { |
---|
130 | _textattr = (_textattr & 0xF0) | (_color); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /*---------------------------------------------------------------------------* |
---|
135 | * ClrLine * |
---|
136 | *---------------------------------------------------------------------------*/ |
---|
137 | void ClrLine(int y) |
---|
138 | { |
---|
139 | int x; |
---|
140 | |
---|
141 | for(x=0 ; x<=W ; x++) PutChar(0, _textattr, x, y); |
---|
142 | } |
---|
143 | |
---|
144 | /*---------------------------------------------------------------------------* |
---|
145 | * NextLine * |
---|
146 | *---------------------------------------------------------------------------*/ |
---|
147 | void NextLine() |
---|
148 | { |
---|
149 | _wherex = 0; |
---|
150 | |
---|
151 | if (++_wherey >= H) { |
---|
152 | _wherey = 0; |
---|
153 | } |
---|
154 | |
---|
155 | ClrLine(_wherey); |
---|
156 | } |
---|
157 | |
---|
158 | /*---------------------------------------------------------------------------* |
---|
159 | * clrscr * |
---|
160 | *---------------------------------------------------------------------------*/ |
---|
161 | void gpconio_clrscr() |
---|
162 | { |
---|
163 | int y; |
---|
164 | |
---|
165 | for(y=0 ; y<=H ; y++) { |
---|
166 | ClrLine(y); |
---|
167 | } |
---|
168 | |
---|
169 | gpconio_gotoxy(0,0); |
---|
170 | } |
---|
171 | |
---|
172 | /*---------------------------------------------------------------------------* |
---|
173 | * gp32_put * |
---|
174 | *---------------------------------------------------------------------------*/ |
---|
175 | void gpconio_put(char _c) |
---|
176 | { |
---|
177 | /* We have save some memory with fonts */ |
---|
178 | _c = _c & 0x7F; /* no extened chars */ |
---|
179 | _c = _c - 0x20; /* no cntr chars */ |
---|
180 | PutChar(_c, _textattr, _wherex, _wherey); |
---|
181 | } |
---|
182 | |
---|
183 | /*---------------------------------------------------------------------------* |
---|
184 | * gp32_putch * |
---|
185 | *---------------------------------------------------------------------------*/ |
---|
186 | void gpconio_putch(char _c) |
---|
187 | { |
---|
188 | switch (_c) { |
---|
189 | case ASCII_LF: |
---|
190 | NextLine(); |
---|
191 | break; |
---|
192 | case ASCII_CR: |
---|
193 | gpconio_gotoxy(0, _wherey); |
---|
194 | break; |
---|
195 | default: |
---|
196 | gpconio_put(_c); |
---|
197 | if (++_wherex >= W) { |
---|
198 | NextLine(); |
---|
199 | } |
---|
200 | break; |
---|
201 | } |
---|
202 | return; |
---|
203 | } |
---|
204 | |
---|
205 | /*---------------------------------------------------------------------------* |
---|
206 | * gp32_puts * |
---|
207 | *---------------------------------------------------------------------------*/ |
---|
208 | void gpconio_puts(const char *_str) |
---|
209 | { |
---|
210 | while (*_str) { |
---|
211 | gpconio_putch(*_str++); |
---|
212 | } |
---|
213 | return; |
---|
214 | } |
---|
215 | |
---|
216 | /*---------------------------------------------------------------------------* |
---|
217 | * gp32_printf * |
---|
218 | *---------------------------------------------------------------------------*/ |
---|
219 | int gpconio_printf(const char *_format, ...) |
---|
220 | { |
---|
221 | char s[256]; |
---|
222 | va_list marker; |
---|
223 | va_start(marker, _format); |
---|
224 | int r = vsprintf(s, _format, marker); |
---|
225 | |
---|
226 | va_end(marker); |
---|
227 | gpconio_puts(s); |
---|
228 | return r; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /*---------------------------------------------------------------------------* |
---|
233 | * InitConIO * |
---|
234 | *---------------------------------------------------------------------------*/ |
---|
235 | void InitConIO() |
---|
236 | { |
---|
237 | uint32_t GPHCLK = 66750000; |
---|
238 | unsigned short BPPMODE = 11; |
---|
239 | unsigned short CLKVAL = (GPHCLK/(83385*2*60))-1; |
---|
240 | uint32_t LCDBANK = GP32_VRAM >> 22; |
---|
241 | uint32_t LCDBASEU = (GP32_VRAM & 0x3FFFFF) >> 1; |
---|
242 | uint32_t LCDBASEL; |
---|
243 | unsigned short OFFSIZE = 0; |
---|
244 | unsigned short PAGEWIDTH; |
---|
245 | |
---|
246 | rLCDCON1 = (CLKVAL<<8) | (0<<7) | (3<<5) | (BPPMODE<<1) | (1<<0) ; |
---|
247 | rLCDCON2 = 0; |
---|
248 | rLCDCON2 = (1<<24) | (319<<14) | (2<<6) | (1<<0) ; |
---|
249 | rLCDCON3 = 0; |
---|
250 | rLCDCON3 = (6<<19) | (239<<8) | (2<<0) ; |
---|
251 | rLCDCON4 = 0; |
---|
252 | rLCDCON4 = (0<<24) | (0<<16) | (0<<8) | (4<<0) ; |
---|
253 | rLCDCON5 = 0; |
---|
254 | rLCDCON5 = ((1<<10) | (1<<9) | (1<<8) | (0<<7) | |
---|
255 | (0<<6) | (0<<4) | (0<<2) | (1<<1) | (0<<0)) ; |
---|
256 | |
---|
257 | LCDBASEL = LCDBASEU + 320*120; |
---|
258 | PAGEWIDTH = 120; |
---|
259 | |
---|
260 | rLCDSADDR1 = (LCDBANK<<21) | (LCDBASEU<<0) ; |
---|
261 | rLCDSADDR2 = (LCDBASEL<<0) ; |
---|
262 | rLCDSADDR3 = (OFFSIZE<<11) | (PAGEWIDTH<<0) ; |
---|
263 | |
---|
264 | gp_initButtons(); |
---|
265 | gpconio_textattr(0); |
---|
266 | gpconio_textcolor(DEF_TEXTCOLOR); |
---|
267 | gpconio_textbackground(DEF_TEXTBACKGROUND); |
---|
268 | gpconio_clrscr(); |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | static void delay_loop(unsigned int count) |
---|
273 | { |
---|
274 | int i; |
---|
275 | |
---|
276 | for(i = 0; i < count; i++) { |
---|
277 | i = i; |
---|
278 | } |
---|
279 | |
---|
280 | } |
---|
281 | |
---|
282 | /*---------------------------------------------------------------------------* |
---|
283 | * gp32_getch * |
---|
284 | *---------------------------------------------------------------------------*/ |
---|
285 | static unsigned char inputch = ASCII_CR; |
---|
286 | char gpconio_getch(void) |
---|
287 | { |
---|
288 | int keyx; |
---|
289 | int key = 0; |
---|
290 | |
---|
291 | while(1) { |
---|
292 | key = gp_getButtons(); |
---|
293 | |
---|
294 | do { |
---|
295 | keyx = gp_getButtons(); |
---|
296 | } while (keyx == key); |
---|
297 | |
---|
298 | switch (key) { |
---|
299 | case GP32_KEY_SELECT: |
---|
300 | gpconio_put(inputch); |
---|
301 | return inputch; |
---|
302 | break; |
---|
303 | |
---|
304 | case GP32_KEY_START: |
---|
305 | gpconio_put(' '); |
---|
306 | inputch = ASCII_CR; |
---|
307 | return inputch; |
---|
308 | break; |
---|
309 | |
---|
310 | case GP32_KEY_A: |
---|
311 | inputch = 'A'; |
---|
312 | break; |
---|
313 | |
---|
314 | case GP32_KEY_B: |
---|
315 | inputch = 'Z'; |
---|
316 | break; |
---|
317 | |
---|
318 | case GP32_KEY_UP: |
---|
319 | if ((inputch-1) >= 0x20) { |
---|
320 | inputch--; |
---|
321 | } |
---|
322 | break; |
---|
323 | |
---|
324 | case GP32_KEY_DOWN: |
---|
325 | if ((inputch+1) <= 0x7E) { |
---|
326 | inputch++; |
---|
327 | } |
---|
328 | break; |
---|
329 | |
---|
330 | case GP32_KEY_LEFT: |
---|
331 | if ((inputch - 0x20) >= 0x20) { |
---|
332 | inputch -= 0x20; |
---|
333 | } |
---|
334 | break; |
---|
335 | |
---|
336 | case GP32_KEY_RIGHT: |
---|
337 | if ((inputch + 0x20) <= 0x7E) { |
---|
338 | inputch += 0x20; |
---|
339 | } |
---|
340 | break; |
---|
341 | |
---|
342 | case GP32_KEY_R: |
---|
343 | inputch = '1'; |
---|
344 | break; |
---|
345 | |
---|
346 | case GP32_KEY_L: |
---|
347 | inputch = '9'; |
---|
348 | break; |
---|
349 | |
---|
350 | default: |
---|
351 | break; |
---|
352 | } |
---|
353 | |
---|
354 | gpconio_put(inputch); |
---|
355 | delay_loop(10000); |
---|
356 | } |
---|
357 | } |
---|