summaryrefslogtreecommitdiffstats
path: root/apps/plugins/rockboy/rockboy.c
blob: 84dcc33ef09ba04a24d0fcd997f36710d2bb8223 (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
/***************************************************************************
 *             __________               __   ___.
 *   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 "loader.h"
#include "rockmacros.h"
#include "input.h"
#include "emu.h"
#include "hw.h"
#include "pcm.h"

PLUGIN_HEADER
PLUGIN_IRAM_DECLARE

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;
}

/* Using #define isn't enough with GCC 4.0.1 */

void* memcpy(void* dst, const void* src, size_t size)
{
    return rb->memcpy(dst, src, size);
}

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);
    if(fd < 0) /* 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 == 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.UP=BUTTON_RC_PLAY;
        options.DOWN=BUTTON_RC_DOWN;
        options.LEFT=BUTTON_RC_REW;
        options.RIGHT=BUTTON_RC_FF;

        options.A=BUTTON_RC_VOL_DOWN;
        options.B=BUTTON_RC_VOL_UP;
        options.START=BUTTON_RC_HEART;
        options.SELECT=BUTTON_RC_MODE;
        options.MENU=BUTTON_POWER;

#elif CONFIG_KEYPAD == COWOND2_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 == 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;
#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 != COWOND2_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);
        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");
    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();
    emu_run();

    /* never reached */
    return PLUGIN_OK;
}

/* this is the plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
    PLUGIN_IRAM_INIT(rb)

    rb->lcd_setfont(0);

    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 MEM <= 8 && !defined(SIMULATOR)
    /* 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

    gnuboy_main(parameter);

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

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

    savesettings();

    cleanup();

    return PLUGIN_OK;
}