-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibretro_core.c
More file actions
356 lines (303 loc) · 13.3 KB
/
libretro_core.c
File metadata and controls
356 lines (303 loc) · 13.3 KB
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
/*
* libretro_core.c - Libretro Core Loading and Management Implementation
*
* Copyright (c) 2024 mikedx
* GitHub: https://github.com/mikedx/libretro_raylib
*
* This file is part of libretro_raylib.
*
* libretro_raylib is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*/
#include "libretro_core.h"
#include "libretro_frontend.h"
#include "libretro_environment.h"
#include "libretro_video.h"
#include "libretro_audio.h"
#include "libretro_input.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
/**
* Set up callbacks after loading a game
*/
static void setup_callbacks_after_load(libretro_frontend_t* frontend) {
if (!frontend->has_set_video_refresh) {
typedef void (*retro_set_video_refresh_t)(retro_video_refresh_t);
typedef void (*retro_set_audio_sample_t)(retro_audio_sample_t);
typedef void (*retro_set_audio_sample_batch_t)(retro_audio_sample_batch_t);
typedef void (*retro_set_input_poll_t)(retro_input_poll_t);
typedef void (*retro_set_input_state_t)(retro_input_state_t);
retro_set_video_refresh_t set_video = (retro_set_video_refresh_t)dlsym(frontend->core_handle, SYM_RETRO_SET_VIDEO_REFRESH);
retro_set_audio_sample_t set_audio = (retro_set_audio_sample_t)dlsym(frontend->core_handle, SYM_RETRO_SET_AUDIO_SAMPLE);
retro_set_audio_sample_batch_t set_audio_batch = (retro_set_audio_sample_batch_t)dlsym(frontend->core_handle, SYM_RETRO_SET_AUDIO_SAMPLE_BATCH);
retro_set_input_poll_t set_input_poll = (retro_set_input_poll_t)dlsym(frontend->core_handle, SYM_RETRO_SET_INPUT_POLL);
retro_set_input_state_t set_input_state = (retro_set_input_state_t)dlsym(frontend->core_handle, SYM_RETRO_SET_INPUT_STATE);
if (set_video && set_audio && set_audio_batch && set_input_poll && set_input_state) {
set_video(retro_video_refresh_callback);
set_audio(retro_audio_sample_callback);
set_audio_batch(retro_audio_sample_batch_callback);
set_input_poll(retro_input_poll_callback);
set_input_state(retro_input_state_callback);
frontend->has_set_video_refresh = true;
frontend->has_set_audio_sample = true;
frontend->has_set_audio_sample_batch = true;
frontend->has_set_input_poll = true;
frontend->has_set_input_state = true;
} else {
fprintf(stderr, "Warning: Failed to set up video/audio/input callbacks\n");
}
}
}
/**
* Load a libretro core from a dynamic library
*/
bool libretro_core_load(libretro_frontend_t* frontend, const char* core_path) {
if (!frontend || !core_path) return false;
void* handle = dlopen(core_path, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "Failed to load core: %s\n", dlerror());
return false;
}
frontend->core_handle = handle;
frontend->core = (struct retro_core_t*)calloc(1, sizeof(struct retro_core_t));
if (!frontend->core) {
dlclose(handle);
frontend->core_handle = NULL;
return false;
}
// Load symbols for callback setters
typedef void (*retro_set_environment_t)(retro_environment_t);
retro_set_environment_t set_env = (retro_set_environment_t)dlsym(handle, SYM_RETRO_SET_ENVIRONMENT);
if (!set_env) {
fprintf(stderr, "Failed to load required symbols from core\n");
free(frontend->core);
frontend->core = NULL;
dlclose(handle);
frontend->core_handle = NULL;
return false;
}
// Set environment callback early
libretro_environment_set_frontend(frontend);
fprintf(stderr, "Setting environment callback (matching RetroArch sequence)...\n");
set_env(retro_environment_callback);
frontend->has_set_environment = true;
// Load core functions
frontend->core->retro_init = (void (*)(void))dlsym(handle, SYM_RETRO_INIT);
frontend->core->retro_deinit = (void (*)(void))dlsym(handle, SYM_RETRO_DEINIT);
frontend->core->retro_api_version = (unsigned (*)(void))dlsym(handle, SYM_RETRO_API_VERSION);
frontend->core->retro_get_system_info = (void (*)(struct retro_system_info*))dlsym(handle, SYM_RETRO_GET_SYSTEM_INFO);
frontend->core->retro_get_system_av_info = (void (*)(struct retro_system_av_info*))dlsym(handle, SYM_RETRO_GET_SYSTEM_AV_INFO);
frontend->core->retro_set_controller_port_device = (void (*)(unsigned, unsigned))dlsym(handle, SYM_RETRO_SET_CONTROLLER_PORT_DEVICE);
frontend->core->retro_reset = (void (*)(void))dlsym(handle, SYM_RETRO_RESET);
frontend->core->retro_run = (void (*)(void))dlsym(handle, SYM_RETRO_RUN);
frontend->core->retro_serialize_size = (size_t (*)(void))dlsym(handle, SYM_RETRO_SERIALIZE_SIZE);
frontend->core->retro_serialize = (bool (*)(void*, size_t))dlsym(handle, SYM_RETRO_SERIALIZE);
frontend->core->retro_unserialize = (bool (*)(const void*, size_t))dlsym(handle, SYM_RETRO_UNSERIALIZE);
frontend->core->retro_cheat_reset = (void (*)(void))dlsym(handle, SYM_RETRO_CHEAT_RESET);
frontend->core->retro_cheat_set = (void (*)(unsigned, bool, const char*))dlsym(handle, SYM_RETRO_CHEAT_SET);
frontend->core->retro_load_game = (bool (*)(const struct retro_game_info*))dlsym(handle, SYM_RETRO_LOAD_GAME);
frontend->core->retro_unload_game = (void (*)(void))dlsym(handle, SYM_RETRO_UNLOAD_GAME);
frontend->core->retro_get_region = (unsigned (*)(void))dlsym(handle, SYM_RETRO_GET_REGION);
frontend->core->retro_get_memory_data = (void* (*)(unsigned))dlsym(handle, SYM_RETRO_GET_MEMORY_DATA);
frontend->core->retro_get_memory_size = (size_t (*)(unsigned))dlsym(handle, SYM_RETRO_GET_MEMORY_SIZE);
if (!frontend->core->retro_init || !frontend->core->retro_run) {
fprintf(stderr, "Failed to load core functions\n");
free(frontend->core);
frontend->core = NULL;
dlclose(handle);
frontend->core_handle = NULL;
return false;
}
return true;
}
/**
* Initialize the loaded libretro core
*/
bool libretro_core_init(libretro_frontend_t* frontend) {
if (!frontend || !frontend->core) return false;
struct retro_system_info info;
if (frontend->core->retro_get_system_info) {
frontend->core->retro_get_system_info(&info);
frontend->need_fullpath = info.need_fullpath;
fprintf(stderr, "Core: %s %s\n", info.library_name, info.library_version);
}
if (frontend->core->retro_init) {
frontend->core->retro_init();
}
if (frontend->core->retro_set_controller_port_device) {
frontend->core->retro_set_controller_port_device(0, RETRO_DEVICE_JOYPAD);
}
frontend->width = 240;
frontend->height = 160;
frontend->aspect_ratio = 3.0f / 2.0f;
frontend->initialized = true;
return true;
}
/**
* Update audio/video information from the core
*/
void libretro_core_update_av_info(libretro_frontend_t* frontend) {
if (!frontend || !frontend->core) return;
struct retro_system_av_info av_info;
if (frontend->core->retro_get_system_av_info) {
frontend->core->retro_get_system_av_info(&av_info);
frontend->width = av_info.geometry.base_width;
frontend->height = av_info.geometry.base_height;
frontend->aspect_ratio = av_info.geometry.aspect_ratio;
unsigned new_sample_rate = (unsigned)av_info.timing.sample_rate;
if (new_sample_rate == 0) {
fprintf(stderr, "Warning: Core reported 0 Hz sample rate, using default 44100 Hz\n");
new_sample_rate = 44100;
}
fprintf(stderr, "Video: %ux%u (aspect: %.2f, fps: %.2f)\n",
frontend->width, frontend->height, frontend->aspect_ratio, frontend->fps);
fprintf(stderr, "Audio: %u Hz\n", new_sample_rate);
if (new_sample_rate != frontend->audio_sample_rate) {
if (frontend->audio_ring_buffer) {
free(frontend->audio_ring_buffer);
}
frontend->audio_sample_rate = new_sample_rate;
frontend->audio_ring_buffer_size = frontend->audio_sample_rate / 4;
if (frontend->audio_ring_buffer_size == 0) {
frontend->audio_ring_buffer_size = 11025;
}
frontend->audio_ring_buffer = (float*)calloc(frontend->audio_ring_buffer_size * 2, sizeof(float));
frontend->audio_ring_read_pos = 0;
frontend->audio_ring_write_pos = 0;
frontend->audio_ring_available = 0;
}
frontend->fps = av_info.timing.fps;
// Don't allocate framebuffer here - let video callback handle it
// Framebuffer allocation should happen in video callback based on actual frame dimensions
}
}
/**
* Load a ROM file into the core
*/
bool libretro_core_load_rom(libretro_frontend_t* frontend, const char* rom_path) {
if (!frontend || !frontend->core) return false;
// Handle no-game mode
if (!rom_path) {
bool success = frontend->core->retro_load_game(NULL);
if (success) {
setup_callbacks_after_load(frontend);
libretro_core_update_av_info(frontend);
frontend->rom_path = NULL;
frontend->rom_data = NULL;
frontend->rom_data_size = 0;
}
return success;
}
if (!frontend->core->retro_load_game) {
fprintf(stderr, "Core does not support loading games\n");
return false;
}
char* abs_path = realpath(rom_path, NULL);
if (!abs_path) {
fprintf(stderr, "Failed to get absolute path for ROM: %s\n", rom_path);
return false;
}
struct retro_game_info game_info = {0};
void* rom_data = NULL;
if (frontend->need_fullpath) {
game_info.path = abs_path;
game_info.data = NULL;
game_info.size = 0;
game_info.meta = NULL;
} else {
FILE* file = fopen(abs_path, "rb");
if (!file) {
fprintf(stderr, "Failed to open ROM file: %s\n", abs_path);
free(abs_path);
return false;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
if (file_size <= 0) {
fprintf(stderr, "Invalid ROM file size\n");
fclose(file);
free(abs_path);
return false;
}
rom_data = malloc(file_size);
if (!rom_data) {
fprintf(stderr, "Failed to allocate memory for ROM\n");
fclose(file);
free(abs_path);
return false;
}
size_t bytes_read = fread(rom_data, 1, file_size, file);
fclose(file);
if (bytes_read != (size_t)file_size) {
fprintf(stderr, "Failed to read ROM file completely\n");
free(rom_data);
free(abs_path);
return false;
}
game_info.path = abs_path;
game_info.data = rom_data;
game_info.size = (size_t)file_size;
game_info.meta = NULL;
}
bool success = frontend->core->retro_load_game(&game_info);
if (!success) {
fprintf(stderr, "Failed to load ROM - core returned false\n");
if (rom_data) {
free(rom_data);
}
free(abs_path);
return false;
}
frontend->rom_data = rom_data;
frontend->rom_data_size = frontend->need_fullpath ? 0 : game_info.size;
frontend->rom_path = abs_path;
// Match RetroArch's exact sequence:
// 1. Set callbacks AFTER loading game (matching RetroArch line 4661)
setup_callbacks_after_load(frontend);
// 2. Get AV info (matching RetroArch line 4663)
// RetroArch calls retro_get_system_av_info right after setting callbacks
// This is critical - VICE may need this to happen before the first retro_run
libretro_core_update_av_info(frontend);
// 3. Call SET_SYSTEM_AV_INFO to notify VICE (matching what VICE expects)
// VICE's update_geometry only calls SET_SYSTEM_AV_INFO when runstate > RUNSTATE_FIRST_START
// But VICE needs SET_SYSTEM_AV_INFO to initialize video properly, so we call it here
// This matches RetroArch's behavior where the core calls SET_SYSTEM_AV_INFO via environment callback
if (frontend->core && frontend->core->retro_get_system_av_info) {
struct retro_system_av_info av_info;
frontend->core->retro_get_system_av_info(&av_info);
retro_environment_callback(RETRO_ENVIRONMENT_SET_SYSTEM_AV_INFO, &av_info);
}
return true;
}
/**
* Unload the core and cleanup resources
*/
void libretro_core_unload(libretro_frontend_t* frontend) {
if (!frontend) return;
if (frontend->core && frontend->core->retro_unload_game && frontend->rom_path) {
frontend->core->retro_unload_game();
}
if (frontend->rom_data) {
free(frontend->rom_data);
frontend->rom_data = NULL;
}
if (frontend->rom_path) {
free(frontend->rom_path);
frontend->rom_path = NULL;
}
if (frontend->core && frontend->core->retro_deinit && frontend->initialized) {
frontend->core->retro_deinit();
}
if (frontend->core_handle) {
dlclose(frontend->core_handle);
frontend->core_handle = NULL;
}
if (frontend->core) {
free(frontend->core);
frontend->core = NULL;
}
}