-
Notifications
You must be signed in to change notification settings - Fork 0
/
libblastem.c
416 lines (369 loc) · 10.2 KB
/
libblastem.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
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
#include <stdlib.h>
#include <string.h>
#include "libretro.h"
#include "system.h"
#include "util.h"
#include "vdp.h"
#include "render.h"
#include "io.h"
static retro_environment_t retro_environment;
RETRO_API void retro_set_environment(retro_environment_t re)
{
retro_environment = re;
}
static retro_video_refresh_t retro_video_refresh;
RETRO_API void retro_set_video_refresh(retro_video_refresh_t rvf)
{
retro_video_refresh = rvf;
}
static retro_audio_sample_t retro_audio_sample;
RETRO_API void retro_set_audio_sample(retro_audio_sample_t ras)
{
retro_audio_sample = ras;
}
RETRO_API void retro_set_audio_sample_batch(retro_audio_sample_batch_t rasb)
{
}
static retro_input_poll_t retro_input_poll;
RETRO_API void retro_set_input_poll(retro_input_poll_t rip)
{
retro_input_poll = rip;
}
static retro_input_state_t retro_input_state;
RETRO_API void retro_set_input_state(retro_input_state_t ris)
{
retro_input_state = ris;
}
int headless = 0;
int exit_after = 0;
int z80_enabled = 1;
char *save_filename;
tern_node *config;
uint8_t use_native_states = 1;
system_header *current_system;
system_media media;
RETRO_API void retro_init(void)
{
}
RETRO_API void retro_deinit(void)
{
if (current_system) {
retro_unload_game();
}
}
RETRO_API unsigned retro_api_version(void)
{
return RETRO_API_VERSION;
}
RETRO_API void retro_get_system_info(struct retro_system_info *info)
{
info->library_name = "BlastEm";
info->library_version = "0.6.3-pre"; //TODO: share this with blastem.c
info->valid_extensions = "md|gen|sms|bin|rom";
info->need_fullpath = 0;
info->block_extract = 0;
}
static vid_std video_standard;
static uint32_t last_width, last_height;
RETRO_API void retro_get_system_av_info(struct retro_system_av_info *info)
{
last_width = LINEBUF_SIZE;
info->geometry.base_width = info->geometry.max_width = LINEBUF_SIZE;
info->geometry.base_height = video_standard == VID_NTSC ? 240 : 288;
last_height = info->geometry.base_height;
info->geometry.max_height = info->geometry.base_height * 2;
info->geometry.aspect_ratio = 0;
info->timing.fps = video_standard == VID_NTSC ? 59.922740 : 49.701459;
info->timing.sample_rate = video_standard == VID_NTSC ? 53267 : 52781; //approximate sample rate of YM2612
}
RETRO_API void retro_set_controller_port_device(unsigned port, unsigned device)
{
}
/* Resets the current game. */
RETRO_API void retro_reset(void)
{
current_system->soft_reset(current_system);
}
/* Runs the game for one video frame.
* During retro_run(), input_poll callback must be called at least once.
*
* If a frame is not rendered for reasons where a game "dropped" a frame,
* this still counts as a frame, and retro_run() should explicitly dupe
* a frame if GET_CAN_DUPE returns true.
* In this case, the video callback can take a NULL argument for data.
*/
static uint8_t started;
RETRO_API void retro_run(void)
{
if (started) {
current_system->resume_context(current_system);
} else {
current_system->start_context(current_system, NULL);
started = 1;
}
}
/* Returns the amount of data the implementation requires to serialize
* internal state (save states).
* Between calls to retro_load_game() and retro_unload_game(), the
* returned size is never allowed to be larger than a previous returned
* value, to ensure that the frontend can allocate a save state buffer once.
*/
static size_t serialize_size_cache;
RETRO_API size_t retro_serialize_size(void)
{
if (!serialize_size_cache) {
uint8_t *tmp = current_system->serialize(current_system, &serialize_size_cache);
free(tmp);
}
return serialize_size_cache;
}
/* Serializes internal state. If failed, or size is lower than
* retro_serialize_size(), it should return false, true otherwise. */
RETRO_API bool retro_serialize(void *data, size_t size)
{
size_t actual_size;
uint8_t *tmp = current_system->serialize(current_system, &actual_size);
if (actual_size > size) {
free(tmp);
return 0;
}
memcpy(data, tmp, actual_size);
free(tmp);
return 1;
}
RETRO_API bool retro_unserialize(const void *data, size_t size)
{
current_system->deserialize(current_system, (uint8_t *)data, size);
return 0;
}
RETRO_API void retro_cheat_reset(void)
{
}
RETRO_API void retro_cheat_set(unsigned index, bool enabled, const char *code)
{
}
/* Loads a game. */
RETRO_API bool retro_load_game(const struct retro_game_info *game)
{
serialize_size_cache = 0;
if (game->path) {
media.dir = path_dirname(game->path);
media.name = basename_no_extension(game->path);
media.extension = path_extension(game->path);
}
media.buffer = malloc(nearest_pow2(game->size));
memcpy(media.buffer, game->data, game->size);
media.size = game->size;
current_system = alloc_config_system(detect_system_type(&media), &media, 0, 0);
unsigned format = RETRO_PIXEL_FORMAT_XRGB8888;
retro_environment(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &format);
return current_system != NULL;
}
/* Loads a "special" kind of game. Should not be used,
* except in extreme cases. */
RETRO_API bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
{
return retro_load_game(info);
}
/* Unloads a currently loaded game. */
RETRO_API void retro_unload_game(void)
{
free(media.dir);
free(media.name);
free(media.extension);
media.dir = media.name = media.extension = NULL;
//buffer is freed by the context
media.buffer = NULL;
current_system->free_context(current_system);
current_system = NULL;
}
/* Gets region of game. */
RETRO_API unsigned retro_get_region(void)
{
return video_standard == VID_NTSC ? RETRO_REGION_NTSC : RETRO_REGION_PAL;
}
/* Gets region of memory. */
RETRO_API void *retro_get_memory_data(unsigned id)
{
return NULL;
}
RETRO_API size_t retro_get_memory_size(unsigned id)
{
return 0;
}
//blastem render backend API implementation
uint32_t render_map_color(uint8_t r, uint8_t g, uint8_t b)
{
return r << 16 | g << 8 | b;
}
uint8_t render_create_window(char *caption, uint32_t width, uint32_t height, window_close_handler close_handler)
{
//not supported in lib build
return 0;
}
void render_destroy_window(uint8_t which)
{
//not supported in lib build
}
static uint32_t fb[LINEBUF_SIZE * 288 * 2];
static uint8_t last_fb;
uint32_t *render_get_framebuffer(uint8_t which, int *pitch)
{
*pitch = LINEBUF_SIZE * sizeof(uint32_t);
if (which != last_fb) {
*pitch = *pitch * 2;
}
if (which) {
return fb + LINEBUF_SIZE;
} else {
return fb;
}
}
void render_framebuffer_updated(uint8_t which, int width)
{
unsigned height = video_standard == VID_NTSC ? 240 : 288;
unsigned base_height = height;
if (which != last_fb) {
height *= 2;
last_fb = which;
}
if (width != last_width || height != last_height) {
struct retro_game_geometry geometry = {
.base_width = width,
.base_height = height,
.aspect_ratio = (float)LINEBUF_SIZE / base_height
};
retro_environment(RETRO_ENVIRONMENT_SET_GEOMETRY, &geometry);
last_width = width;
last_height = height;
}
retro_video_refresh(fb, width, height, LINEBUF_SIZE * sizeof(uint32_t));
current_system->request_exit(current_system);
}
uint8_t render_get_active_framebuffer(void)
{
return 0;
}
void render_set_video_standard(vid_std std)
{
video_standard = std;
}
int render_fullscreen(void)
{
return 1;
}
void process_events()
{
static int16_t prev_state[2][RETRO_DEVICE_ID_JOYPAD_L2];
static const uint8_t map[] = {
BUTTON_A, BUTTON_X, BUTTON_MODE, BUTTON_START, DPAD_UP, DPAD_DOWN,
DPAD_LEFT, DPAD_RIGHT, BUTTON_B, BUTTON_Y, BUTTON_Z, BUTTON_C
};
//TODO: handle other input device types
//TODO: handle more than 2 ports when appropriate
retro_input_poll();
for (int port = 0; port < 2; port++)
{
for (int id = RETRO_DEVICE_ID_JOYPAD_B; id < RETRO_DEVICE_ID_JOYPAD_L2; id++)
{
int16_t new_state = retro_input_state(port, RETRO_DEVICE_JOYPAD, 0, id);
if (new_state != prev_state[port][id]) {
if (new_state) {
current_system->gamepad_down(current_system, port + 1, map[id]);
} else {
current_system->gamepad_up(current_system, port + 1, map[id]);
}
prev_state[port][id] = new_state;
}
}
}
}
void render_errorbox(char *title, char *message)
{
}
void render_warnbox(char *title, char *message)
{
}
void render_infobox(char *title, char *message)
{
}
struct audio_source {
int32_t freq;
int32_t left_accum;
int32_t right_accum;
int32_t num_samples;
};
static audio_source *audio_sources[8];
static uint8_t num_audio_sources;
audio_source *render_audio_source(uint64_t master_clock, uint64_t sample_divider, uint8_t channels)
{
audio_sources[num_audio_sources] = calloc(1, sizeof(audio_source));
audio_sources[num_audio_sources]->freq = master_clock / sample_divider;
return audio_sources[num_audio_sources++];
}
void render_audio_adjust_clock(audio_source *src, uint64_t master_clock, uint64_t sample_divider)
{
}
void render_audio_source_gaindb(audio_source *src, float gain)
{
//TODO: Implement this once I hook up a core option for individual FM/PSG gain
}
static void check_put_sample(void)
{
for (int i = 0; i < num_audio_sources; i++)
{
int32_t min_samples = audio_sources[i]->freq / 53267;
if (audio_sources[i]->num_samples < min_samples) {
return;
}
}
int16_t left = 0, right = 0;
for (int i = 0; i < num_audio_sources; i++)
{
left += audio_sources[i]->left_accum / audio_sources[i]->num_samples;
right += audio_sources[i]->right_accum / audio_sources[i]->num_samples;
audio_sources[i]->left_accum = audio_sources[i]->right_accum = audio_sources[i]->num_samples = 0;
}
retro_audio_sample(left, right);
}
void render_put_mono_sample(audio_source *src, int16_t value)
{
src->left_accum += value;
src->right_accum += value;
src->num_samples++;
check_put_sample();
}
void render_put_stereo_sample(audio_source *src, int16_t left, int16_t right)
{
src->left_accum += left;
src->right_accum += right;
src->num_samples++;
check_put_sample();
}
void render_free_source(audio_source *src)
{
int index;
for (index = 0; index < num_audio_sources; index++)
{
if (audio_sources[index] == src) {
break;
}
}
num_audio_sources--;
audio_sources[index] = audio_sources[num_audio_sources];
free(src);
}
void bindings_set_mouse_mode(uint8_t mode)
{
}
extern const char rom_db_data[];
char *read_bundled_file(char *name, uint32_t *sizeret)
{
if (!strcmp(name, "rom.db")) {
*sizeret = strlen(rom_db_data);
char *ret = malloc(*sizeret+1);
memcpy(ret, rom_db_data, *sizeret + 1);
return ret;
}
return NULL;
}