-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibretro.c
398 lines (298 loc) · 8.4 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
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
#include "libretro.h"
#include "emumain.h"
volatile int Loop;
volatile int Sleep;
char launchDir[MAX_PATH];
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
static retro_log_printf_t log_cb;
static retro_video_refresh_t video_cb;
static retro_input_poll_t input_poll_cb;
static retro_input_state_t input_state_cb;
static retro_environment_t environ_cb;
#define NJEMU_DISPLAY_LIST_ID 0x1300
static PspGeContext main_context_buffer;
static PspGeContext njemu_context_buffer;
//static int main_context_state;
//static int njemu_context_state;
static bool game_is_loading = false;
void msg_printf(const char *text, ...)
{
static char message_buffer[1024];
va_list arg;
va_start(arg, text);
vsnprintf(message_buffer, sizeof(message_buffer), text, arg);
va_end(arg);
if (game_is_loading)
{
pspDebugScreenPrintf("%s",message_buffer);
}
printf("%s", message_buffer);
}
static int free_space_counter;
static void recurse_memory (int size)
{
if (size <= 0x100)
return;
void* tmp = malloc(size);
if (tmp)
{
free_space_counter += size;
recurse_memory(size);
free(tmp);
}
else
{
recurse_memory(size>>1);
}
}
int get_free_space(void)
{
free_space_counter = 0;
recurse_memory(0x4000000);
return free_space_counter;
}
void display_free_space(void)
{
msg_printf("free_space_size = %i\n", get_free_space());
}
int get_max_free_block_size(void)
{
int size = 0;
int chunk = 0x2000000;
while (chunk > 0x100)
{
void* tmp = malloc(size + chunk);
if (tmp)
{
free(tmp);
size += chunk;
}
chunk >>= 1;
}
return size;
}
void display_max_free_block_size(void)
{
msg_printf("max_free_block_size = %i\n", get_max_free_block_size());
}
void display_available_memory(void)
{
msg_printf("available memory: %i (total) %i (Max cont. block)\n", get_free_space(), get_max_free_block_size());
}
void fatalerror(const char *text, ...)
{
va_list arg;
va_start(arg, text);
vprintf(text, arg);
va_end(arg);
}
void retro_get_system_info(struct retro_system_info *info)
{
info->library_name = "NJEMU-" SYSTEM_NAME;
info->library_version = "v0.0.1";
info->need_fullpath = true;
info->block_extract = true;
info->valid_extensions = "zip";
}
void retro_get_system_av_info(struct retro_system_av_info *info)
{
info->geometry.base_width = FRAME_WIDTH;
info->geometry.base_height = FRAME_HEIGHT;
info->geometry.max_width = FRAME_WIDTH;
info->geometry.max_height = FRAME_HEIGHT;
info->geometry.aspect_ratio = 16.0/9.0;
info->timing.fps = FPS;
info->timing.sample_rate = sound->frequency;
}
//void msg_printf(const char *text, ...)
//{
// if(!log_cb)
// return;
// va_list arg;
// va_start(arg, text);
// log_cb(0,text, arg);
// va_end(arg);
//}
void retro_init()
{
game_is_loading = false;
getcwd(launchDir, MAX_PATH - 1);
strcat(launchDir, "/");
#if USE_CACHE
strcpy(cache_dir, launchDir);
strcat(cache_dir, "cache");
#endif
pad_init();
video_init();
sceGuSync(0,0);
// njemu_context_state = sceGuGetAllStatus();
sceGeSaveContext(&njemu_context_buffer);
struct retro_log_callback log;
if (environ_cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &log))
log_cb = log.log;
else
log_cb = NULL;
}
void retro_deinit()
{
}
void retro_set_environment(retro_environment_t cb)
{
environ_cb = cb;
}
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_input_poll(retro_input_poll_t cb) { input_poll_cb = cb; }
void retro_set_input_state(retro_input_state_t cb) { input_state_cb = cb; }
void retro_set_controller_port_device(unsigned port, unsigned device) {}
void retro_reset()
{
machine_reset();
}
size_t retro_serialize_size()
{
return 0;
}
bool retro_serialize(void *data, size_t size)
{
return true;
}
bool retro_unserialize(const void *data, size_t size)
{
return true;
}
void retro_cheat_reset() {}
void retro_cheat_set(unsigned index, bool enabled, const char *code) {}
bool retro_load_game(const struct retro_game_info *info)
{
char* temp_p;
bool ret;
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_RGB565;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{
if (log_cb)
log_cb(RETRO_LOG_INFO, "[NJEMU]: RGB565 is not supported.\n");
return false;
}
strcpy(game_dir, info->path);
temp_p = strrchr(game_dir, '/');
if (temp_p)
{
*temp_p = '\0';
strcpy(game_name, temp_p + 1);
}
else
{
strcpy(game_dir, ".");
strcpy(game_name, info->path );
}
temp_p = strchr(game_name, '.');
if (temp_p)
*temp_p = '\0';
sceGeSaveContext(&main_context_buffer);
sceGeRestoreContext(&njemu_context_buffer);
pspDebugScreenInitEx(NULL, PSP_DISPLAY_PIXEL_FORMAT_565, 1);
game_is_loading = true;
display_available_memory();
ret = machine_main();
display_available_memory();
game_is_loading = false;
// return 0;
// sceGuSync(0,0);
// sceGeSaveContext(&njemu_context_buffer);
// sceGeRestoreContext(&main_context_buffer);
return ret;
}
bool retro_load_game_special(unsigned game_type, const struct retro_game_info *info, size_t num_info)
{ return false; }
void retro_unload_game()
{
machine_exit();
}
unsigned retro_get_region() { return RETRO_REGION_NTSC; }
void *retro_get_memory_data(unsigned id)
{
return 0;
}
size_t retro_get_memory_size(unsigned id)
{
return 0;
}
static inline void list_finish_callback(int id)
{
if (id != NJEMU_DISPLAY_LIST_ID)
return;
sceGeSaveContext(&njemu_context_buffer);
sceGeRestoreContext(&main_context_buffer);
}
void retro_run()
{
static unsigned int __attribute__((aligned(64))) d_list[1024];
u16* texture_vram_p = (void*) ((u32)work_frame|0x44000000);
// u16* texture_vram_p = (void*) ((u32)draw_frame|0x44000000);
input_poll_cb();
sceGuSync(0,0);
// main_context_state = sceGuGetAllStatus();
sceGeSaveContext(&main_context_buffer);
// sceGuSetAllStatus(njemu_context_state);
// sceGeRestoreContext(&njemu_context_buffer);
sceGuSetCallback(GU_CALLBACK_FINISH, list_finish_callback);
sceGuStart(GU_DIRECT, gulist);
sceGuEnable(GU_SCISSOR_TEST);
sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT);
sceGuDisable(GU_ALPHA_TEST);
sceGuAlphaFunc(GU_LEQUAL, 0, 0x01);
sceGuDisable(GU_BLEND);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_ONE_MINUS_SRC_ALPHA, 0, 0);
sceGuDisable(GU_DEPTH_TEST);
sceGuDepthRange(65535, 0);
sceGuDepthFunc(GU_GEQUAL);
sceGuDepthMask(GU_TRUE);
sceGuClearDepth(0);
// sceGuEnable(GU_DEPTH_TEST);
// sceGuDepthMask(GU_FALSE);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexMode(GU_PSM_5551, 0, 0, GU_FALSE);
sceGuTexScale(1.0f / BUF_WIDTH, 1.0f / BUF_WIDTH);
sceGuTexOffset(0, 0);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
sceGuClutMode(GU_PSM_5551, 0, 0xff, 0);
sceGuFinish();
sceGuSync(0,0);
timer_update_cpu();
render_audio();
frames_displayed++;
update_inputport();
sceGuSync(0,0);
static unsigned int __attribute__((aligned(64))) restore_context_d_list[1024];
sceGuStart(GU_DIRECT, restore_context_d_list);
sceGuFinishId(NJEMU_DISPLAY_LIST_ID);
sceGuSync(0,0);
// might be necessary to check video_enable here and draw a black frame instead
// sceGuSync(0,0);
// njemu_context_state= sceGuGetAllStatus();
// sceGuSetAllStatus(main_context_state);
sceGuStart(GU_DIRECT, d_list);
sceGuEnable(GU_TEXTURE_2D);
sceGuTexMode(GU_PSM_5551, 0, 0, GU_FALSE);
sceGuTexImage(0, 512, 512, BUF_WIDTH, (texture_vram_p + (FRAME_OFFSET_X + FRAME_OFFSET_Y * BUF_WIDTH)) );
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
// sceGuDisable(GU_BLEND);
// sceGuEnable(GU_SCISSOR_TEST);
// sceGuScissor(0, 0, SCR_WIDTH, 272);
// sceGuDepthFunc(GU_GEQUAL);
// sceGuDepthMask(GU_TRUE);
// sceGuDisable(GU_DEPTH_TEST);
// sceGuTexScale(1.0f, 1.0f );
// sceGuTexOffset(0, 0);
// sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA);
// sceGuClutMode(GU_PSM_5551, 0, 0xff, 0);
// sceGuTexFilter(GU_LINEAR,GU_LINEAR);
sceGuFinish();
// video_cb(texture_vram_p, FRAME_WIDTH, FRAME_HEIGHT, BUF_WIDTH);
video_cb(RETRO_HW_FRAME_BUFFER_VALID, FRAME_WIDTH, FRAME_HEIGHT, BUF_WIDTH);
// sceDisplaySetFrameBuf(texture_vram_p, 512, PSP_DISPLAY_PIXEL_FORMAT_5551, PSP_DISPLAY_SETBUF_NEXTFRAME );
}
unsigned retro_api_version() { return RETRO_API_VERSION; }