summaryrefslogtreecommitdiffstats
path: root/apps/plugins/rockboy/rockboy.c
blob: 12feabb8e18d1a3461d1dffe841f6126d36839a3 (plain)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Gameboy emulator based on gnuboy
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/helper.h"
#include "loader.h"
#include "rockmacros.h"
#include "input.h"
#include "emu.h"
#include "hw.h"
#include "pcm.h"

int shut,cleanshut;
char *errormsg;

#define optionname "options"

void die(char *message, ...)
{
    shut=1;
    errormsg=message;
}

struct options options;

void *audio_bufferbase;
void *audio_bufferpointer;
size_t audio_buffer_free;

void *my_malloc(size_t size)
{
    void *alloc;

    if (size + 4 > audio_buffer_free)
        return 0;
    alloc = audio_bufferpointer;
    audio_bufferpointer += size + 4;
    audio_buffer_free -= size + 4;
    return alloc;
}

static void setoptions (void)
{
    int fd;
    DIR* dir;
    char optionsave[sizeof(savedir)+sizeof(optionname)];

    dir=rb->opendir(savedir);
    if(!dir)
        rb->mkdir(savedir);
    else
        rb->closedir(dir);

    snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);

    fd = open(optionsave, O_RDONLY);

    int optionssize = sizeof(options);
    int filesize = 0;
    if(fd >= 0)
        filesize = rb->filesize(fd);

    /* don't read the option file if the size
     * is not as expected to avoid crash */
    if(fd < 0 || filesize!=optionssize)
    {
    /* no options to read, set defaults */
#ifdef HAVE_TOUCHSCREEN
        options.LEFT    = BUTTON_MIDLEFT;
        options.RIGHT   = BUTTON_MIDRIGHT;
#else
        options.LEFT    = BUTTON_LEFT;
        options.RIGHT   = BUTTON_RIGHT;
#endif

#if CONFIG_KEYPAD == IRIVER_H100_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_ON;
        options.B       = BUTTON_OFF;
        options.START   = BUTTON_REC;
        options.SELECT  = BUTTON_SELECT;
        options.MENU    = BUTTON_MODE;

#elif CONFIG_KEYPAD == IRIVER_H300_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_REC;
        options.B       = BUTTON_MODE;
        options.START   = BUTTON_ON;
        options.SELECT  = BUTTON_SELECT;
        options.MENU    = BUTTON_OFF;

#elif CONFIG_KEYPAD == RECORDER_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_F1;
        options.B       = BUTTON_F2;
        options.START   = BUTTON_F3;
        options.SELECT  = BUTTON_PLAY;
        options.MENU    = BUTTON_OFF;

#elif CONFIG_KEYPAD == IPOD_4G_PAD
        options.UP      = BUTTON_MENU;
        options.DOWN    = BUTTON_PLAY;

        options.A       = BUTTON_NONE;
        options.B       = BUTTON_NONE;
        options.START   = BUTTON_SELECT;
        options.SELECT  = BUTTON_NONE;
        options.MENU    = (BUTTON_SELECT | BUTTON_REPEAT);

#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_PLAY;
        options.B       = BUTTON_EQ;
        options.START   = BUTTON_MODE;
        options.SELECT  = (BUTTON_SELECT | BUTTON_REL);
        options.MENU    = (BUTTON_SELECT | BUTTON_REPEAT);

#elif CONFIG_KEYPAD == GIGABEAT_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.START   = BUTTON_A;
        options.SELECT  = BUTTON_SELECT;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == SANSA_E200_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_SELECT;
        options.B       = BUTTON_REC;
        options.START   = BUTTON_SCROLL_BACK;
        options.SELECT  = BUTTON_SCROLL_FWD;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == SANSA_FUZE_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_SELECT;
        options.B       = BUTTON_HOME;
        options.START   = BUTTON_SCROLL_BACK;
        options.SELECT  = BUTTON_SCROLL_FWD;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == SANSA_C200_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_SELECT;
        options.B       = BUTTON_REC;
        options.START   = BUTTON_VOL_DOWN;
        options.SELECT  = BUTTON_VOL_UP;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == SANSA_CLIP_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_SELECT;
        options.B       = BUTTON_HOME;
        options.START   = BUTTON_VOL_DOWN;
        options.SELECT  = BUTTON_VOL_UP;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_PLAY;
        options.B       = BUTTON_REC;
        options.START   = BUTTON_SELECT;
        options.SELECT  = BUTTON_NONE;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == IRIVER_H10_PAD
        options.UP      = BUTTON_SCROLL_UP;
        options.DOWN    = BUTTON_SCROLL_DOWN;

        options.A       = BUTTON_PLAY;
        options.B       = BUTTON_FF;
        options.START   = BUTTON_REW;
        options.SELECT  = BUTTON_NONE;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == MROBE500_PAD
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == COWON_D2_PAD
        options.A       = BUTTON_PLUS;
        options.B       = BUTTON_MINUS;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.START   = BUTTON_PLAY;
        options.SELECT  = BUTTON_SELECT;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_CUSTOM;
        options.B       = BUTTON_PLAY;
        options.START   = BUTTON_BACK;
        options.SELECT  = BUTTON_SELECT;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == CREATIVE_ZENXFI3_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.START   = BUTTON_PLAY;
        options.SELECT  = BUTTON_NONE;
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.START   = BUTTON_VIEW;
        options.SELECT  = BUTTON_SELECT;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;
        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.START   = BUTTON_NEXT;
        options.SELECT  = BUTTON_PLAY;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.START   = BUTTON_RIGHT;
        options.SELECT  = BUTTON_LEFT;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == ONDAVX747_PAD
        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.MENU    = BUTTON_MENU;

#elif CONFIG_KEYPAD == ONDAVX777_PAD
        options.MENU    = BUTTON_POWER;

#elif CONFIG_KEYPAD == IRIVER_H10_PAD
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_PLAY;
        options.B       = BUTTON_FFWD;
        options.START   = BUTTON_REW;
        options.SELECT  = BUTTON_RIGHT;
        options.MENU    = BUTTON_LEFT;

#elif (CONFIG_KEYPAD == SAMSUNG_YH820_PAD) || \
      (CONFIG_KEYPAD == SAMSUNG_YH920_PAD)
        options.UP      = BUTTON_UP;
        options.DOWN    = BUTTON_DOWN;

        options.A       = BUTTON_PLAY;
        options.B       = BUTTON_REW;
        options.START   = BUTTON_REC;
        options.SELECT  = BUTTON_FFWD;
        options.MENU    = BUTTON_NONE;

#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
        options.UP     = BUTTON_OK;
        options.DOWN   = BUTTON_CANCEL;
        options.LEFT   = BUTTON_PREV;
        options.RIGHT  = BUTTON_NEXT;

        options.A      = BUTTON_POWER;
        options.B      = BUTTON_REC;
        options.START  = BUTTON_PLAY;
        options.SELECT = BUTTON_UP;
        options.MENU   = BUTTON_MENU;

#elif CONFIG_KEYPAD == MPIO_HD300_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_REW;
        options.RIGHT  = BUTTON_FF;

        options.A      = BUTTON_PLAY;
        options.B      = BUTTON_REC;
        options.START  = (BUTTON_PLAY | BUTTON_REPEAT);
        options.SELECT = BUTTON_ENTER;
        options.MENU   = BUTTON_MENU;

#elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_LEFT;
        options.RIGHT  = BUTTON_RIGHT;

        options.A      = BUTTON_VOL_UP;
        options.B      = BUTTON_VOL_DOWN;
        options.START  = BUTTON_SELECT;
        options.SELECT = BUTTON_PLAYPAUSE;
        options.MENU   = BUTTON_BACK;

#elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_LEFT;
        options.RIGHT  = BUTTON_RIGHT;

        options.A      = BUTTON_VOL_UP;
        options.B      = BUTTON_VOL_DOWN;
        options.START  = BUTTON_PREV;
        options.SELECT = BUTTON_NEXT;
        options.MENU   = BUTTON_SELECT;

#elif CONFIG_KEYPAD == SAMSUNG_YPR0_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_LEFT;
        options.RIGHT  = BUTTON_RIGHT;

        options.A      = BUTTON_SELECT;
        options.B      = BUTTON_BACK;
        options.START  = BUTTON_POWER;
        options.SELECT = BUTTON_USER;
        options.MENU   = BUTTON_MENU;

#elif CONFIG_KEYPAD == HM801_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_LEFT;
        options.RIGHT  = BUTTON_RIGHT;

        options.A      = BUTTON_PREV;
        options.B      = BUTTON_NEXT;
        options.START  = BUTTON_PLAY;
        options.SELECT = BUTTON_SELECT;
        options.MENU   = BUTTON_POWER;

#elif CONFIG_KEYPAD == SONY_NWZ_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_LEFT;
        options.RIGHT  = BUTTON_RIGHT;

        options.START  = BUTTON_PLAY;
        options.MENU   = BUTTON_BACK;
        options.SELECT = (BUTTON_POWER|BUTTON_PLAY);
        options.A      = (BUTTON_POWER|BUTTON_LEFT);
        options.B      = (BUTTON_POWER|BUTTON_RIGHT);

#elif CONFIG_KEYPAD == CREATIVE_ZEN_PAD
        options.UP     = BUTTON_UP;
        options.DOWN   = BUTTON_DOWN;
        options.LEFT   = BUTTON_LEFT;
        options.RIGHT  = BUTTON_RIGHT;

        options.START  = BUTTON_BACK;
        options.MENU   = BUTTON_MENU;
        options.SELECT = BUTTON_SELECT;
        options.A      = BUTTON_SHORTCUT;
        options.B      = BUTTON_PLAYPAUSE;

#elif CONFIG_KEYPAD == DX50_PAD
        options.A       = BUTTON_VOL_UP;
        options.B       = BUTTON_VOL_DOWN;
        options.MENU    = BUTTON_POWER;
        options.START   = BUTTON_LEFT;
        options.SELECT  = BUTTON_RIGHT;

#else
#error No Keymap Defined!
#endif

#ifdef HAVE_TOUCHSCREEN
        options.UP      = BUTTON_TOPMIDDLE;
        options.DOWN    = BUTTON_BOTTOMMIDDLE;
        options.START   = BUTTON_TOPRIGHT;
        options.SELECT  = BUTTON_CENTER;
#if CONFIG_KEYPAD == MROBE500_PAD
        options.A       = BUTTON_BOTTOMLEFT;
        options.B       = BUTTON_BOTTOMRIGHT;
#elif (CONFIG_KEYPAD != COWON_D2_PAD ) || (CONFIG_KEYPAD != DX50_PAD )
        options.A       = BUTTON_BOTTOMLEFT;
        options.B       = BUTTON_BOTTOMRIGHT;
        options.MENU    = BUTTON_TOPLEFT;
#endif
#endif

        options.maxskip=4;
        options.fps=0;
        options.showstats=0;
#if (LCD_WIDTH>=160) && (LCD_HEIGHT>=144)
        options.scaling=0;
#else
        options.scaling=1;
#endif
        options.sound=1;
        options.pal=0;
    }
    else
        read(fd,&options, sizeof(options));

    close(fd);
}

static void savesettings(void)
{
    int fd;
    char optionsave[sizeof(savedir)+sizeof(optionname)];

    if(options.dirty)
    {
        options.dirty=0;
        snprintf(optionsave, sizeof(optionsave), "%s/%s", savedir, optionname);
        fd = open(optionsave, O_WRONLY|O_CREAT|O_TRUNC, 0666);
        write(fd,&options, sizeof(options));
        close(fd);
    }
}

void doevents(void)
{
    event_t ev;
    int st;

    ev_poll();
    while (ev_getevent(&ev))
    {
        if (ev.type != EV_PRESS && ev.type != EV_RELEASE)
            continue;
        st = (ev.type != EV_RELEASE);
        pad_set(ev.code, st);
    }
}

static int gnuboy_main(const char *rom)
{
    rb->lcd_puts(0,0,"Init video");
    vid_init();
    rb->lcd_puts(0,1,"Init sound");
    rockboy_pcm_init();
    rb->lcd_puts(0,2,"Loading rom");
    loader_init(rom);
    if(shut)
        return PLUGIN_ERROR;
    rb->lcd_puts(0,3,"Emu reset");
    emu_reset();
    rb->lcd_puts(0,4,"Emu run");
    rb->lcd_clear_display();
    rb->lcd_update();
    if(options.autosave) sn_load();
    emu_run();

    /* never reached */
    return PLUGIN_OK;
}

/* this is the plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
    rb->lcd_setfont(FONT_SYSFIXED);

    rb->lcd_clear_display();

    if (!parameter)
    {
        rb->splash(HZ*3, "Play gameboy ROM file! (.gb/.gbc)");
        return PLUGIN_OK;
    }
    if(rb->audio_status())
    {
        audio_bufferbase = audio_bufferpointer
            = rb->plugin_get_buffer(&audio_buffer_free);
        plugbuf=true;
    }
    else
    {
        audio_bufferbase = audio_bufferpointer
            = rb->plugin_get_audio_buffer(&audio_buffer_free);
        plugbuf=false;
    }
#if MEMORYSIZE <= 8 && (CONFIG_PLATFORM & PLATFORM_NATIVE)
    /* loaded as an overlay plugin, protect from overwriting ourselves */
    if ((unsigned)(plugin_start_addr - (unsigned char *)audio_bufferbase)
        < audio_buffer_free)
        audio_buffer_free = plugin_start_addr - (unsigned char *)audio_bufferbase;
#endif
    setoptions();

    shut=0;
    cleanshut=0;

#ifdef HAVE_WHEEL_POSITION
    rb->wheel_send_events(false);
#endif

#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
    rb->lcd_set_mode(LCD_MODE_PAL256);
#endif

    /* ignore backlight time out */
    backlight_ignore_timeout();

    gnuboy_main(parameter);

#ifdef HAVE_WHEEL_POSITION
    rb->wheel_send_events(true);
#endif

#if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256)
    rb->lcd_set_mode(LCD_MODE_RGB565);
#endif

    backlight_use_settings();

    if(!rb->audio_status())
        rockboy_pcm_close();

    if(shut&&!cleanshut)
    {
        rb->splash(HZ/2, errormsg);
        return PLUGIN_ERROR;
    }

    rb->splash(HZ/2, "Closing Rockboy");

    savesettings();

    cleanup();

    return PLUGIN_OK;
}