summaryrefslogtreecommitdiff
path: root/libretro.c
blob: d834ede09cd98338da5ed7ab9142e8c774955336 (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
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "common.h"
#include "libco.h"
#include "libretro.h"

#ifndef MAX_PATH
#define MAX_PATH (512)
#endif

static retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_input_poll_t input_poll_cb;
static retro_environment_t environ_cb;

struct retro_perf_callback perf_cb;

static cothread_t main_thread;
static cothread_t cpu_thread;

void switch_to_main_thread(void)
{
   co_switch(main_thread);
}

static inline void switch_to_cpu_thread(void)
{
   co_switch(cpu_thread);
}

static void cpu_thread_entry(void)
{
   execute_arm_translate(execute_cycles);
   execute_arm(execute_cycles);
}

static inline void init_context_switch(void)
{
   main_thread = co_active();
   cpu_thread = co_create(0x20000, cpu_thread_entry);
}

static inline void deinit_context_switch(void)
{
   co_delete(cpu_thread);
}

#ifdef PERF_TEST

extern struct retro_perf_callback perf_cb;

#define RETRO_PERFORMANCE_INIT(X) \
   static struct retro_perf_counter X = {#X}; \
   do { \
      if (!(X).registered) \
         perf_cb.perf_register(&(X)); \
   } while(0)

#define RETRO_PERFORMANCE_START(X) perf_cb.perf_start(&(X))
#define RETRO_PERFORMANCE_STOP(X) perf_cb.perf_stop(&(X))
#else
#define RETRO_PERFORMANCE_INIT(X)
#define RETRO_PERFORMANCE_START(X)
#define RETRO_PERFORMANCE_STOP(X)

#endif

void retro_get_system_info(struct retro_system_info* info)
{
   info->library_name = "gpSP";
   info->library_version = "v0.91";
   info->need_fullpath = true;
   info->block_extract = false;
   info->valid_extensions = "gba|bin|agb|gbz" ;
}


void retro_get_system_av_info(struct retro_system_av_info* info)
{
   info->geometry.base_width = GBA_SCREEN_WIDTH;
   info->geometry.base_height = GBA_SCREEN_HEIGHT;
   info->geometry.max_width = GBA_SCREEN_WIDTH;
   info->geometry.max_height = GBA_SCREEN_HEIGHT;
   info->geometry.aspect_ratio = 0;
   // 59.72750057 hz
   info->timing.fps = ((float) GBC_BASE_RATE) / (308 * 228 * 4);
   info->timing.sample_rate = GBA_SOUND_FREQUENCY;
}

#include <sys/mman.h>

void retro_init()
{
   init_gamepak_buffer();
   init_sound(1);

#ifdef HAVE_MMAP
   rom_translation_cache = mmap(NULL, ROM_TRANSLATION_CACHE_SIZE,
                                PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
   ram_translation_cache = mmap(NULL, RAM_TRANSLATION_CACHE_SIZE,
                                PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);
   bios_translation_cache = mmap(NULL, BIOS_TRANSLATION_CACHE_SIZE,
                                 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0);

   rom_translation_ptr = rom_translation_cache;
   ram_translation_ptr = ram_translation_cache;
   bios_translation_ptr = bios_translation_cache;
#endif
}

void retro_deinit()
{
   perf_cb.perf_log();
   memory_term();

#ifdef HAVE_MMAP
   munmap(rom_translation_cache, ROM_TRANSLATION_CACHE_SIZE);
   munmap(ram_translation_cache, RAM_TRANSLATION_CACHE_SIZE);
   munmap(bios_translation_cache, BIOS_TRANSLATION_CACHE_SIZE);
#endif
}

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_set_video_refresh(retro_video_refresh_t cb)
{
   video_cb = cb;
}
void retro_set_input_poll(retro_input_poll_t cb)
{
   input_poll_cb = cb;
}

void retro_set_controller_port_device(unsigned port, unsigned device) {}

void retro_reset()
{
   deinit_context_switch();

   update_backup();
   reset_gba();

   init_context_switch();
}


size_t retro_serialize_size()
{
   return GBA_STATE_MEM_SIZE;
}

bool retro_serialize(void* data, size_t size)
{
   if (size != GBA_STATE_MEM_SIZE)
      return false;

   memset (data,0, GBA_STATE_MEM_SIZE);
   gba_save_state(data);

   return true;
}

bool retro_unserialize(const void* data, size_t size)
{
   if (size != GBA_STATE_MEM_SIZE)
      return false;

   gba_load_state(data);

   return true;
}

void retro_cheat_reset() {}
void retro_cheat_set(unsigned index, bool enabled, const char* code) {}

void error_msg(const char* text)
{
   if (log_cb)
      log_cb(RETRO_LOG_ERROR, "[gpSP]: %s\n", text);
}

void info_msg(const char* text)
{
   if (log_cb)
      log_cb(RETRO_LOG_INFO, "[gpSP]: %s\n", text);
}

static void extract_directory(char* buf, const char* path, size_t size)
{
   strncpy(buf, path, size - 1);
   buf[size - 1] = '\0';

   char* base = strrchr(buf, '/');

   if (base)
      *base = '\0';
   else
      strncpy(buf, ".", size);
}

bool retro_load_game(const struct retro_game_info* info)
{
   char filename_bios[MAX_PATH];
   const char* dir = NULL;

   enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
   if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
      info_msg("RGB565 is not supported.");

   extract_directory(main_path, info->path, sizeof(main_path));

   if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
      strncpy(filename_bios, dir, sizeof(filename_bios));
   else
      strncpy(filename_bios, main_path, sizeof(filename_bios));

   strncat(filename_bios, "/gba_bios.bin", sizeof(filename_bios));


   if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
      strncpy(save_path, dir, sizeof(save_path));
   else
      strncpy(save_path, main_path, sizeof(save_path));

   if (load_bios(filename_bios) != 0)
   {
      error_msg("Could not load BIOS image file.");
      return false;
   }

   if (bios_rom[0] != 0x18)
   {
      info_msg("You have an incorrect BIOS image.");
      info_msg("While many games will work fine, some will not.");
      info_msg("It is strongly recommended that you obtain the correct BIOS file.");
   }

   gamepak_filename[0] = 0;

   if (load_gamepak(info->path) != 0)
   {
      error_msg("Could not load the game file.");
      return false;
   }

   reset_gba();

   init_context_switch();

   return true;
}


bool retro_load_game_special(unsigned game_type,
                             const struct retro_game_info* info, size_t num_info)
{
   return false;
}

void retro_unload_game()
{
   deinit_context_switch();
   update_backup();
}

unsigned retro_get_region()
{
   return RETRO_REGION_NTSC;
}

void* retro_get_memory_data(unsigned id)
{
   //   switch (id)
   //   {
   //   case RETRO_MEMORY_SAVE_RAM:
   //      return gamepak_backup;
   //   }

   return 0;
}

size_t retro_get_memory_size(unsigned id)
{
   //   switch (id)
   //   {
   //   case RETRO_MEMORY_SAVE_RAM:
   //      switch(backup_type)
   //      {
   //      case BACKUP_SRAM:
   //         return sram_size;

   //      case BACKUP_FLASH:
   //         return flash_size;

   //      case BACKUP_EEPROM:
   //         return eeprom_size;

   //      case BACKUP_NONE:
   //         return 0x0;

   //      default:
   //         return 0x8000;
   //      }
   //   }

   return 0;
}

static void check_variables(void)
{

}

void retro_run()
{
   bool updated = false;

   input_poll_cb();

   switch_to_cpu_thread();

   update_input();

   render_audio();

   video_cb(gba_screen_pixels, GBA_SCREEN_WIDTH, GBA_SCREEN_HEIGHT,
            GBA_SCREEN_PITCH * 2);

   if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
      check_variables();

}

unsigned retro_api_version()
{
   return RETRO_API_VERSION;
}