Convert many #ifdef SIMULATOR to #if (CONFIG_PLATFORM & PLATFORM_HOSTED) (and similar).
[kugel-rb.git] / apps / plugins / rockboy / rockboy.c
blob797971c734c799f986dde6a0473aa9d97e222be3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Gameboy emulator based on gnuboy
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "loader.h"
23 #include "rockmacros.h"
24 #include "input.h"
25 #include "emu.h"
26 #include "hw.h"
27 #include "pcm.h"
29 PLUGIN_HEADER
30 PLUGIN_IRAM_DECLARE
32 int shut,cleanshut;
33 char *errormsg;
35 #define optionname "options"
37 void die(char *message, ...)
39 shut=1;
40 errormsg=message;
43 struct options options;
45 void *audio_bufferbase;
46 void *audio_bufferpointer;
47 size_t audio_buffer_free;
49 void *my_malloc(size_t size)
51 void *alloc;
53 if (size + 4 > audio_buffer_free)
54 return 0;
55 alloc = audio_bufferpointer;
56 audio_bufferpointer += size + 4;
57 audio_buffer_free -= size + 4;
58 return alloc;
61 static void setoptions (void)
63 int fd;
64 DIR* dir;
65 char optionsave[sizeof(savedir)+sizeof(optionname)];
67 dir=rb->opendir(savedir);
68 if(!dir)
69 rb->mkdir(savedir);
70 else
71 rb->closedir(dir);
73 snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);
75 fd = open(optionsave, O_RDONLY);
76 if(fd < 0) /* no options to read, set defaults */
78 #ifdef HAVE_TOUCHSCREEN
79 options.LEFT=BUTTON_MIDLEFT;
80 options.RIGHT=BUTTON_MIDRIGHT;
81 #else
82 options.LEFT=BUTTON_LEFT;
83 options.RIGHT=BUTTON_RIGHT;
84 #endif
86 #if CONFIG_KEYPAD == IRIVER_H100_PAD
87 options.UP=BUTTON_UP;
88 options.DOWN=BUTTON_DOWN;
90 options.A=BUTTON_ON;
91 options.B=BUTTON_OFF;
92 options.START=BUTTON_REC;
93 options.SELECT=BUTTON_SELECT;
94 options.MENU=BUTTON_MODE;
96 #elif CONFIG_KEYPAD == IRIVER_H300_PAD
97 options.UP=BUTTON_UP;
98 options.DOWN=BUTTON_DOWN;
100 options.A=BUTTON_REC;
101 options.B=BUTTON_MODE;
102 options.START=BUTTON_ON;
103 options.SELECT=BUTTON_SELECT;
104 options.MENU=BUTTON_OFF;
106 #elif CONFIG_KEYPAD == RECORDER_PAD
107 options.UP=BUTTON_UP;
108 options.DOWN=BUTTON_DOWN;
110 options.A=BUTTON_F1;
111 options.B=BUTTON_F2;
112 options.START=BUTTON_F3;
113 options.SELECT=BUTTON_PLAY;
114 options.MENU=BUTTON_OFF;
116 #elif CONFIG_KEYPAD == IPOD_4G_PAD
117 options.UP=BUTTON_MENU;
118 options.DOWN=BUTTON_PLAY;
120 options.A=BUTTON_NONE;
121 options.B=BUTTON_NONE;
122 options.START=BUTTON_SELECT;
123 options.SELECT=BUTTON_NONE;
124 options.MENU=(BUTTON_SELECT | BUTTON_REPEAT);
126 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
127 options.UP=BUTTON_UP;
128 options.DOWN=BUTTON_DOWN;
130 options.A=BUTTON_PLAY;
131 options.B=BUTTON_EQ;
132 options.START=BUTTON_MODE;
133 options.SELECT=(BUTTON_SELECT | BUTTON_REL);
134 options.MENU=(BUTTON_SELECT | BUTTON_REPEAT);
136 #elif CONFIG_KEYPAD == GIGABEAT_PAD
137 options.UP=BUTTON_UP;
138 options.DOWN=BUTTON_DOWN;
140 options.A=BUTTON_VOL_UP;
141 options.B=BUTTON_VOL_DOWN;
142 options.START=BUTTON_A;
143 options.SELECT=BUTTON_SELECT;
144 options.MENU=BUTTON_MENU;
146 #elif CONFIG_KEYPAD == SANSA_E200_PAD
147 options.UP=BUTTON_UP;
148 options.DOWN=BUTTON_DOWN;
150 options.A=BUTTON_SELECT;
151 options.B=BUTTON_REC;
152 options.START=BUTTON_SCROLL_BACK;
153 options.SELECT=BUTTON_SCROLL_FWD;
154 options.MENU=BUTTON_POWER;
156 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
157 options.UP=BUTTON_UP;
158 options.DOWN=BUTTON_DOWN;
160 options.A=BUTTON_SELECT;
161 options.B=BUTTON_HOME;
162 options.START=BUTTON_SCROLL_BACK;
163 options.SELECT=BUTTON_SCROLL_FWD;
164 options.MENU=BUTTON_POWER;
166 #elif CONFIG_KEYPAD == SANSA_C200_PAD
167 options.UP=BUTTON_UP;
168 options.DOWN=BUTTON_DOWN;
170 options.A=BUTTON_SELECT;
171 options.B=BUTTON_REC;
172 options.START=BUTTON_VOL_DOWN;
173 options.SELECT=BUTTON_VOL_UP;
174 options.MENU=BUTTON_POWER;
176 #elif CONFIG_KEYPAD == SANSA_CLIP_PAD
177 options.UP=BUTTON_UP;
178 options.DOWN=BUTTON_DOWN;
180 options.A=BUTTON_SELECT;
181 options.B=BUTTON_HOME;
182 options.START=BUTTON_VOL_DOWN;
183 options.SELECT=BUTTON_VOL_UP;
184 options.MENU=BUTTON_POWER;
186 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
187 options.UP=BUTTON_UP;
188 options.DOWN=BUTTON_DOWN;
190 options.A=BUTTON_PLAY;
191 options.B=BUTTON_REC;
192 options.START=BUTTON_SELECT;
193 options.SELECT=BUTTON_NONE;
194 options.MENU=BUTTON_POWER;
196 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
197 options.UP=BUTTON_SCROLL_UP;
198 options.DOWN=BUTTON_SCROLL_DOWN;
200 options.A=BUTTON_PLAY;
201 options.B=BUTTON_FF;
202 options.START=BUTTON_REW;
203 options.SELECT=BUTTON_NONE;
204 options.MENU=BUTTON_POWER;
206 #elif CONFIG_KEYPAD == MROBE500_PAD
207 options.MENU=BUTTON_POWER;
209 #elif CONFIG_KEYPAD == COWON_D2_PAD
210 options.A=BUTTON_PLUS;
211 options.B=BUTTON_MINUS;
212 options.MENU=BUTTON_MENU;
214 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
215 options.UP=BUTTON_UP;
216 options.DOWN=BUTTON_DOWN;
218 options.A=BUTTON_VOL_UP;
219 options.B=BUTTON_VOL_DOWN;
220 options.START=BUTTON_PLAY;
221 options.SELECT=BUTTON_SELECT;
222 options.MENU=BUTTON_MENU;
223 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
224 options.UP=BUTTON_UP;
225 options.DOWN=BUTTON_DOWN;
227 options.A=BUTTON_CUSTOM;
228 options.B=BUTTON_PLAY;
229 options.START=BUTTON_BACK;
230 options.SELECT=BUTTON_SELECT;
231 options.MENU=BUTTON_MENU;
233 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
234 options.UP=BUTTON_UP;
235 options.DOWN=BUTTON_DOWN;
237 options.A=BUTTON_VOL_UP;
238 options.B=BUTTON_VOL_DOWN;
239 options.START=BUTTON_VIEW;
240 options.SELECT=BUTTON_SELECT;
241 options.MENU=BUTTON_MENU;
243 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
244 options.UP=BUTTON_UP;
245 options.DOWN=BUTTON_DOWN;
247 options.A=BUTTON_VOL_UP;
248 options.B=BUTTON_VOL_DOWN;
249 options.START=BUTTON_RIGHT;
250 options.SELECT=BUTTON_LEFT;
251 options.MENU=BUTTON_MENU;
253 #elif CONFIG_KEYPAD == ONDAVX747_PAD
254 options.A=BUTTON_VOL_UP;
255 options.B=BUTTON_VOL_DOWN;
256 options.MENU=BUTTON_MENU;
258 #elif CONFIG_KEYPAD == ONDAVX777_PAD
259 options.MENU=BUTTON_POWER;
261 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
262 options.UP=BUTTON_UP;
263 options.DOWN=BUTTON_DOWN;
265 options.A=BUTTON_PLAY;
266 options.B=BUTTON_FFWD;
267 options.START=BUTTON_REW;
268 options.SELECT=BUTTON_RIGHT;
269 options.MENU=BUTTON_LEFT;
271 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
272 options.UP=BUTTON_UP;
273 options.DOWN=BUTTON_DOWN;
275 options.A=BUTTON_PLAY;
276 options.B=BUTTON_REW;
277 options.START=BUTTON_REC;
278 options.SELECT=BUTTON_FFWD;
279 options.MENU=BUTTON_NONE;
281 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
282 options.UP= BUTTON_OK;
283 options.DOWN= BUTTON_CANCEL;
284 options.LEFT= BUTTON_PREV;
285 options.RIGHT= BUTTON_NEXT;
287 options.A= BUTTON_POWER;
288 options.B= BUTTON_REC;
289 options.START= BUTTON_PLAY;
290 options.SELECT= BUTTON_UP;
291 options.MENU= BUTTON_MENU;
293 #else
294 #error No Keymap Defined!
295 #endif
297 #ifdef HAVE_TOUCHSCREEN
298 options.UP=BUTTON_TOPMIDDLE;
299 options.DOWN=BUTTON_BOTTOMMIDDLE;
300 options.START=BUTTON_TOPRIGHT;
301 options.SELECT=BUTTON_CENTER;
302 #if CONFIG_KEYPAD == MROBE500_PAD
303 options.A=BUTTON_BOTTOMLEFT;
304 options.B=BUTTON_BOTTOMRIGHT;
305 #elif CONFIG_KEYPAD != COWON_D2_PAD
306 options.A=BUTTON_BOTTOMLEFT;
307 options.B=BUTTON_BOTTOMRIGHT;
308 options.MENU=BUTTON_TOPLEFT;
309 #endif
310 #endif
312 options.maxskip=4;
313 options.fps=0;
314 options.showstats=0;
315 #if (LCD_WIDTH>=160) && (LCD_HEIGHT>=144)
316 options.scaling=0;
317 #else
318 options.scaling=1;
319 #endif
320 options.sound=1;
321 options.pal=0;
323 else
324 read(fd,&options, sizeof(options));
326 close(fd);
329 static void savesettings(void)
331 int fd;
332 char optionsave[sizeof(savedir)+sizeof(optionname)];
334 if(options.dirty)
336 options.dirty=0;
337 snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);
338 fd = open(optionsave, O_WRONLY|O_CREAT|O_TRUNC, 0666);
339 write(fd,&options, sizeof(options));
340 close(fd);
344 void doevents(void)
346 event_t ev;
347 int st;
349 ev_poll();
350 while (ev_getevent(&ev))
352 if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
353 continue;
354 st = (ev.type != EV_RELEASE);
355 pad_set(ev.code, st);
359 static int gnuboy_main(const char *rom)
361 rb->lcd_puts(0,0,"Init video");
362 vid_init();
363 rb->lcd_puts(0,1,"Init sound");
364 rockboy_pcm_init();
365 rb->lcd_puts(0,2,"Loading rom");
366 loader_init(rom);
367 if(shut)
368 return PLUGIN_ERROR;
369 rb->lcd_puts(0,3,"Emu reset");
370 emu_reset();
371 rb->lcd_puts(0,4,"Emu run");
372 rb->lcd_clear_display();
373 rb->lcd_update();
374 emu_run();
376 /* never reached */
377 return PLUGIN_OK;
380 /* this is the plugin entry point */
381 enum plugin_status plugin_start(const void* parameter)
383 PLUGIN_IRAM_INIT(rb)
385 rb->lcd_setfont(0);
387 rb->lcd_clear_display();
389 if (!parameter)
391 rb->splash(HZ*3, "Play gameboy ROM file! (.gb/.gbc)");
392 return PLUGIN_OK;
394 if(rb->audio_status())
396 audio_bufferbase = audio_bufferpointer
397 = rb->plugin_get_buffer(&audio_buffer_free);
398 plugbuf=true;
400 else
402 audio_bufferbase = audio_bufferpointer
403 = rb->plugin_get_audio_buffer(&audio_buffer_free);
404 plugbuf=false;
406 #if MEM <= 8 && (CONFIG_PLATFORM & PLATFORM_NATIVE)
407 /* loaded as an overlay plugin, protect from overwriting ourselves */
408 if ((unsigned)(plugin_start_addr - (unsigned char *)audio_bufferbase)
409 < audio_buffer_free)
410 audio_buffer_free = plugin_start_addr - (unsigned char *)audio_bufferbase;
411 #endif
412 setoptions();
414 shut=0;
415 cleanshut=0;
417 #ifdef HAVE_WHEEL_POSITION
418 rb->wheel_send_events(false);
419 #endif
421 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
422 rb->lcd_set_mode(LCD_MODE_PAL256);
423 #endif
425 gnuboy_main(parameter);
427 #ifdef HAVE_WHEEL_POSITION
428 rb->wheel_send_events(true);
429 #endif
431 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
432 rb->lcd_set_mode(LCD_MODE_RGB565);
433 #endif
435 if(shut&&!cleanshut)
437 rb->splash(HZ/2, errormsg);
438 return PLUGIN_ERROR;
440 if(!rb->audio_status())
441 rockboy_pcm_close();
443 rb->splash(HZ/2, "Closing Rockboy");
445 savesettings();
447 cleanup();
449 return PLUGIN_OK;