-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibretro.c
363 lines (301 loc) · 8.72 KB
/
libretro.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include "libretro.h"
#include "retro_miscellaneous.h"
#include "gbemu.h"
retro_log_printf_t log_cb = NULL;
static retro_video_refresh_t video_cb = NULL;
static retro_input_poll_t poll_cb = NULL;
retro_input_state_t input_cb = NULL;
retro_audio_sample_batch_t audio_batch_cb = NULL;
retro_environment_t environ_cb = NULL;
struct retro_perf_callback perf_cb;
#ifdef PERF_TEST
#define RETRO_PERFORMANCE_INIT(name) \
retro_perf_tick_t current_ticks;\
static struct retro_perf_counter name = {#name};\
if (!name.registered) perf_cb.perf_register(&(name));\
current_ticks = name.total
#define RETRO_PERFORMANCE_START(name) perf_cb.perf_start(&(name))
#define RETRO_PERFORMANCE_STOP(name) \
perf_cb.perf_stop(&(name));\
current_ticks = name.total - current_ticks;
#else
#define RETRO_PERFORMANCE_INIT(name)
#define RETRO_PERFORMANCE_START(name)
#define RETRO_PERFORMANCE_STOP(name)
#endif
void gbemu_wait_for_input(void)
{
if(!poll_cb || !input_cb)
return;
printf("press START\n");
fflush(stdout);
do
{
retro_sleep(50);
poll_cb();
if(input_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT))
exit(0);
}
while(!input_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START));
}
void gbemu_check_exit_request(void)
{
if(!poll_cb || !input_cb)
return;
poll_cb();
if(input_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT))
// environ_cb(RETRO_ENVIRONMENT_SHUTDOWN, NULL);
exit(0);
}
void retro_set_environment(retro_environment_t cb)
{
struct retro_log_callback log;
environ_cb = cb;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
environ_cb(RETRO_ENVIRONMENT_GET_PERF_INTERFACE, &perf_cb);
}
void retro_init(void)
{
struct retro_log_callback log;
enum retro_pixel_format rgb565;
static const struct retro_variable vars[] =
{
{ "gbemu_dummy", "dummy; disabled|enabled" },
{ NULL, NULL },
};
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
rgb565 = RETRO_PIXEL_FORMAT_RGB565;
if (environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &rgb565) && log_cb)
log_cb(RETRO_LOG_INFO,
"Frontend supports RGB565 - will use that instead of XRGB1555.\n");
environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void*)vars);
}
void retro_deinit(void)
{
#ifdef PERF_TEST
perf_cb.perf_log();
#endif
}
static void check_variables(void)
{
struct retro_variable var;
var.key = "gbemu_dummy";
var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
(void)strcmp(var.value, "enabled");
}
void retro_get_system_info(struct retro_system_info* info)
{
info->need_fullpath = false;
info->valid_extensions = "gb";
info->library_version = "v0.01";
info->library_name = "GBEMU";
info->block_extract = false;
}
void retro_get_system_av_info(struct retro_system_av_info* info)
{
info->geometry.base_width = GBEMU_DRAWBUFFER_W;
info->geometry.base_height = GBEMU_DRAWBUFFER_H;
info->geometry.max_width = GBEMU_DRAWBUFFER_W;
info->geometry.max_height = GBEMU_DRAWBUFFER_H;
info->geometry.aspect_ratio = 0.0;
info->timing.fps = 60.0;
info->timing.sample_rate = (double) GB_FRAME_TICK_COUNT * 4 * 60.0 / (double) GBEMU_AUDIO_DECIMATION_RATE;
}
static void init_descriptors(void)
{
struct retro_input_descriptor desc[] =
{
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0 },
};
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
}
void retro_reset(void)
{
gbemu_reset();
}
void retro_run(void)
{
bool updated = false;
static int frames = 0;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
poll_cb();
gbemu_update_input();
RETRO_PERFORMANCE_INIT(gbemu_main_func);
RETRO_PERFORMANCE_START(gbemu_main_func);
gbemu_run();
RETRO_PERFORMANCE_STOP(gbemu_main_func);
// if(!GB.LCDC.LCD_enable)
// memset(gbemu_frame, 0xFF, sizeof(gbemu_frame));
#ifdef VIEW_TILEMAP
gbemu_draw_sprite_map();
gbemu_draw_bgmap();
gbemu_draw_tilemap();
gbemu_dump_memory();
#endif
uint16_t* read_pos = gbemu_sound_buffer;
while (read_pos < GB.APU.write_pos - 2048)
{
audio_batch_cb(read_pos, 1024);
read_pos += 2048;
}
audio_batch_cb(read_pos, (GB.APU.write_pos - read_pos) >> 1);
// gbemu_printf("samples played : %i\n", (GB.APU.write_pos - gbemu_sound_buffer) >> 1);
video_cb(gbemu_frame, GBEMU_DRAWBUFFER_W, GBEMU_DRAWBUFFER_H, GBEMU_DRAWBUFFER_W * 2);
// DEBUG_HOLD();
// gbemu_printf("frame : %i\n", frames);
frames++;
fflush(stdout);
// gbemu_check_exit_request();
}
bool retro_load_game(const struct retro_game_info* game)
{
init_descriptors();
check_variables();
uint8_t bios_data[256];
char bios_path[PATH_MAX_LENGTH];
char* system_dir;
if(environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &system_dir) && system_dir)
strncpy(bios_path, system_dir, sizeof(bios_path));
else if (game->path)
strncpy(bios_path, game->path, sizeof(bios_path));
else
strncpy(bios_path, ".", sizeof(bios_path));
#if defined(_WIN32)
char slash = '\\';
#else
char slash = '/';
#endif
char* tmp = strrchr(bios_path, slash);
if(!tmp)
tmp = strrchr(bios_path, '\0');
*tmp++ = slash;
strcpy(tmp, "gbbios.gb");
retro_sleep(10);
fflush(stdout);
gbemu_printf("romd info\n");
gbemu_printf("path : %s\n", game->path);
gbemu_printf("size : %u\n", game->size);
fflush(stdout);
gbemu_printf("loading bios from : %s\n", bios_path);
FILE* fp = fopen(bios_path, "rb");
if(fp)
{
fread(bios_data, 1, sizeof(bios_data), fp);
fclose(fp);
}
else
gbemu_printf("bios not found !\n");
gbemu_load_game(game->data, game->size, fp? bios_data: NULL);
return true;
}
unsigned retro_get_region(void)
{
return RETRO_REGION_NTSC;
}
void retro_set_video_refresh(retro_video_refresh_t cb)
{
video_cb = cb;
}
void retro_set_audio_sample(retro_audio_sample_t cb)
{}
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
{
audio_batch_cb = cb;
}
void retro_set_input_poll(retro_input_poll_t cb)
{
poll_cb = cb;
}
void retro_set_input_state(retro_input_state_t cb)
{
input_cb = cb;
}
void retro_set_controller_port_device(unsigned port, unsigned device) {}
unsigned retro_api_version()
{
return RETRO_API_VERSION;
}
bool retro_load_game_special(unsigned game_type,
const struct retro_game_info* info, size_t num_info)
{
return false;
}
void retro_unload_game(void)
{
}
void* retro_get_memory_data(unsigned id)
{
switch(id & RETRO_MEMORY_MASK)
{
case RETRO_MEMORY_SAVE_RAM:
return GB.MBC.SRAM;
case RETRO_MEMORY_SYSTEM_RAM:
return GB.HRAM;
case RETRO_MEMORY_VIDEO_RAM:
return GB.VRAM;
}
return NULL;
}
size_t retro_get_memory_size(unsigned id)
{
switch(id & RETRO_MEMORY_MASK)
{
case RETRO_MEMORY_SAVE_RAM:
return sizeof(GB.MBC.SRAM);
case RETRO_MEMORY_SYSTEM_RAM:
return sizeof(GB.HRAM);
case RETRO_MEMORY_VIDEO_RAM:
return sizeof(GB.VRAM);
}
return 0;
}
size_t retro_serialize_size(void)
{
return sizeof(GB);
}
bool retro_serialize(void* data, size_t size)
{
gbemu_state_t* saved_state = (gbemu_state_t*)data;
if (size != retro_serialize_size())
return false;
memcpy(data, &GB, sizeof(GB));
saved_state->MBC.active_ROM_bank = (uint8_t*)((uintptr_t)saved_state->MBC.active_ROM_bank - (uintptr_t)&GB);
saved_state->MBC.active_SRAM_bank = (uint8_t*)((uintptr_t)saved_state->MBC.active_SRAM_bank - (uintptr_t)&GB);
return true;
}
bool retro_unserialize(const void* data, size_t size)
{
if (size != sizeof(GB))
return false;
memcpy(&GB, data , sizeof(GB));
GB.MBC.active_ROM_bank = (uint8_t*)((uintptr_t)GB.MBC.active_ROM_bank + (uintptr_t)&GB);
GB.MBC.active_SRAM_bank = (uint8_t*)((uintptr_t)GB.MBC.active_SRAM_bank + (uintptr_t)&GB);
return true;
}
void retro_cheat_reset(void)
{
}
void retro_cheat_set(unsigned index, bool enabled, const char* code)
{
}