1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus-rsp-hle - main.c *
3 * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4 * Copyright (C) 2012 Bobby Smiles *
5 * Copyright (C) 2009 Richard Goedeken *
6 * Copyright (C) 2002 Hacktarux *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28 #define M64P_PLUGIN_PROTOTYPES 1
29 #include "m64p_types.h"
30 #include "m64p_common.h"
31 #include "m64p_plugin.h"
37 #define min(a,b) (((a) < (b)) ? (a) : (b))
39 /* some rsp status flags */
40 #define RSP_STATUS_HALT 0x1
41 #define RSP_STATUS_BROKE 0x2
42 #define RSP_STATUS_INTR_ON_BREAK 0x40
43 #define RSP_STATUS_TASKDONE 0x200
45 /* some rdp status flags */
46 #define DP_STATUS_FREEZE 0x2
48 /* some mips interface interrupt flags */
49 #define MI_INTR_SP 0x1
52 /* helper functions prototypes */
53 static unsigned int sum_bytes(const unsigned char *bytes, unsigned int size);
54 static void dump_binary(const char * const filename, const unsigned char * const bytes,
56 static void dump_task(const char * const filename, const OSTask_t * const task);
58 static void handle_unknown_task(unsigned int sum);
59 static void handle_unknown_non_task(unsigned int sum);
61 /* global variables */
65 static const int FORWARD_AUDIO = 0, FORWARD_GFX = 1;
66 static void (*l_DebugCallback)(void *, int, const char *) = NULL;
67 static void *l_DebugCallContext = NULL;
68 static int l_PluginInit = 0;
74 * Try to figure if the RSP was launched using osSpTask* functions
75 * and not run directly (in which case DMEM[0xfc0-0xfff] is meaningless).
77 * Previously, the ucode_size field was used to determine this,
78 * but it is not robust enough (hi Pokemon Stadium !) because games could write anything
79 * in this field : most ucode_boot discard the value and just use 0xf7f anyway.
81 * Using ucode_boot_size should be more robust in this regard.
85 return (get_task()->ucode_boot_size <= 0x1000);
88 static void rsp_break(unsigned int setbits)
90 *rsp.SP_STATUS_REG |= setbits | RSP_STATUS_BROKE | RSP_STATUS_HALT;
92 if ((*rsp.SP_STATUS_REG & RSP_STATUS_INTR_ON_BREAK))
94 *rsp.MI_INTR_REG |= MI_INTR_SP;
95 rsp.CheckInterrupts();
99 static void forward_gfx_task()
101 if (rsp.ProcessDlistList != NULL)
103 rsp.ProcessDlistList();
104 *rsp.DPC_STATUS_REG &= ~DP_STATUS_FREEZE;
108 static void forward_audio_task()
110 if (rsp.ProcessAlistList != NULL)
112 rsp.ProcessAlistList();
116 static void show_cfb()
118 if (rsp.ShowCFB != NULL)
124 static int try_fast_audio_dispatching()
126 /* identify audio ucode by using the content of ucode_data */
127 const OSTask_t * const task = get_task();
128 const unsigned char * const udata_ptr = rsp.RDRAM + task->ucode_data;
130 if (*(unsigned int*)(udata_ptr + 0) == 0x00000001)
132 if (*(unsigned int*)(udata_ptr + 0x30) == 0xf0000f00)
135 * Many games including:
136 * Super Mario 64, Diddy Kong Racing, BlastCorp, GoldenEye, ... (most common)
138 alist_process_ABI1(); return 1;
143 * Mario Kart / Wave Race,
148 * Zelda Ocarina of Time,
149 * Zelda Majoras Mask / Pokemon Stadium 2,
152 * FIXME: in fact, all these games do not share the same ABI.
153 * That's the reason of the workaround in ucode2.cpp with isZeldaABI and isMKABI
155 alist_process_ABI2(); return 1;
160 if (*(unsigned int*)(udata_ptr + 0x10) == 0x00000001)
163 * Musyx ucode found in following games:
164 * RogueSquadron, ResidentEvil2, SnowCrossPolaris, TheWorldIsNotEnough,
165 * RugratsInParis, NBAShowTime, HydroThunder, Tarzan,
166 * GauntletLegend, Rush2049, IndianaJones, BattleForNaboo
167 * TODO: implement ucode
169 DebugMessage(M64MSG_WARNING, "MusyX ucode not implemented.");
175 * Many games including:
176 * Pokemon Stadium, Banjo Kazooie, Donkey Kong, Banjo Tooie, Jet Force Gemini,
177 * Mickey SpeedWay USA, Perfect Dark, Conker Bad Fur Day ...
179 alist_process_ABI3(); return 1;
186 static int try_fast_task_dispatching()
188 /* identify task ucode by its type */
189 const OSTask_t * const task = get_task();
193 case 1: if (FORWARD_GFX) { forward_gfx_task(); return 1; } break;
196 if (FORWARD_AUDIO) { forward_audio_task(); return 1; }
197 else if (try_fast_audio_dispatching()) { return 1; }
200 case 7: show_cfb(); return 1;
206 static void normal_task_dispatching()
208 const OSTask_t * const task = get_task();
209 const unsigned int sum =
210 sum_bytes(rsp.RDRAM + task->ucode, min(task->ucode_size, 0xf80) >> 1);
214 /* StoreVe12: found in Zelda Ocarina of Time [misleading task->type == 4] */
215 case 0x278: /* Nothing to emulate */ return;
217 /* GFX: Twintris [misleading task->type == 0] */
219 if (FORWARD_GFX) { forward_gfx_task(); return; }
222 /* JPEG: found in Pokemon Stadium J */
223 case 0x2c85a: jpeg_decode_PS0(); return;
225 /* JPEG: found in Zelda Ocarina of Time, Pokemon Stadium 1, Pokemon Stadium 2 */
226 case 0x2caa6: jpeg_decode_PS(); return;
228 /* JPEG: found in Ogre Battle, Bottom of the 9th */
231 jpeg_decode_OB(); return;
234 handle_unknown_task(sum);
237 static void non_task_dispatching()
239 const unsigned int sum = sum_bytes(rsp.IMEM, 0x1000 >> 1);
243 /* CIC x105 ucode (used during boot of CIC x105 games) */
244 case 0x9e2: /* CIC 6105 */
245 case 0x9f2: /* CIC 7105 */
246 cicx105_ucode(); return;
249 handle_unknown_non_task(sum);
252 static void handle_unknown_task(unsigned int sum)
255 const OSTask_t * const task = get_task();
257 DebugMessage(M64MSG_WARNING, "unknown OSTask: sum %x PC:%x", sum, *rsp.SP_PC_REG);
259 sprintf(&filename[0], "task_%x.log", sum);
260 dump_task(filename, task);
263 sprintf(&filename[0], "ucode_boot_%x.bin", sum);
264 dump_binary(filename, rsp.RDRAM + (task->ucode_boot & 0x7fffff), task->ucode_boot_size);
267 if (task->ucode != 0)
269 sprintf(&filename[0], "ucode_%x.bin", sum);
270 dump_binary(filename, rsp.RDRAM + (task->ucode & 0x7fffff), 0xf80);
274 if (task->ucode_data != 0)
276 sprintf(&filename[0], "ucode_data_%x.bin", sum);
277 dump_binary(filename, rsp.RDRAM + (task->ucode_data & 0x7fffff), task->ucode_data_size);
281 if (task->data_ptr != 0)
283 sprintf(&filename[0], "data_%x.bin", sum);
284 dump_binary(filename, rsp.RDRAM + (task->data_ptr & 0x7fffff), task->data_size);
288 static void handle_unknown_non_task(unsigned int sum)
292 DebugMessage(M64MSG_WARNING, "unknown RSP code: sum: %x PC:%x", sum, *rsp.SP_PC_REG);
294 // dump IMEM & DMEM for further analysis
295 sprintf(&filename[0], "imem_%x.bin", sum);
296 dump_binary(filename, rsp.IMEM, 0x1000);
298 sprintf(&filename[0], "dmem_%x.bin", sum);
299 dump_binary(filename, rsp.DMEM, 0x1000);
303 /* Global functions */
304 void DebugMessage(int level, const char *message, ...)
309 if (l_DebugCallback == NULL)
312 va_start(args, message);
313 vsprintf(msgbuf, message, args);
315 (*l_DebugCallback)(l_DebugCallContext, level, msgbuf);
320 /* DLL-exported functions */
321 EXPORT m64p_error CALL PluginStartup(m64p_dynlib_handle CoreLibHandle, void *Context,
322 void (*DebugCallback)(void *, int, const char *))
325 return M64ERR_ALREADY_INIT;
327 /* first thing is to set the callback function for debug info */
328 l_DebugCallback = DebugCallback;
329 l_DebugCallContext = Context;
331 /* this plugin doesn't use any Core library functions (ex for Configuration), so no need to keep the CoreLibHandle */
334 return M64ERR_SUCCESS;
337 EXPORT m64p_error CALL PluginShutdown(void)
340 return M64ERR_NOT_INIT;
342 /* reset some local variable */
343 l_DebugCallback = NULL;
344 l_DebugCallContext = NULL;
347 return M64ERR_SUCCESS;
350 EXPORT m64p_error CALL PluginGetVersion(m64p_plugin_type *PluginType, int *PluginVersion, int *APIVersion, const char **PluginNamePtr, int *Capabilities)
352 /* set version info */
353 if (PluginType != NULL)
354 *PluginType = M64PLUGIN_RSP;
356 if (PluginVersion != NULL)
357 *PluginVersion = RSP_HLE_VERSION;
359 if (APIVersion != NULL)
360 *APIVersion = RSP_PLUGIN_API_VERSION;
362 if (PluginNamePtr != NULL)
363 *PluginNamePtr = "Hacktarux/Azimer High-Level Emulation RSP Plugin";
365 if (Capabilities != NULL)
370 return M64ERR_SUCCESS;
373 EXPORT unsigned int CALL DoRspCycles(unsigned int Cycles)
377 if (!try_fast_task_dispatching()) { normal_task_dispatching(); }
378 rsp_break(RSP_STATUS_TASKDONE);
382 non_task_dispatching();
389 EXPORT void CALL InitiateRSP(RSP_INFO Rsp_Info, unsigned int *CycleCount)
394 EXPORT void CALL RomClosed(void)
396 memset(rsp.DMEM, 0, 0x1000);
397 memset(rsp.IMEM, 0, 0x1000);
403 /* local helper functions */
404 static unsigned int sum_bytes(const unsigned char *bytes, unsigned int size)
406 unsigned int sum = 0;
407 const unsigned char * const bytes_end = bytes + size;
409 while (bytes != bytes_end)
416 static void dump_binary(const char * const filename, const unsigned char * const bytes,
421 // if file already exists, do nothing
422 f = fopen(filename, "r");
425 // else we write bytes to the file
426 f= fopen(filename, "wb");
428 if (fwrite(bytes, 1, size, f) != size)
430 DebugMessage(M64MSG_ERROR, "Writing error on %s", filename);
436 DebugMessage(M64MSG_ERROR, "Couldn't open %s for writing !", filename);
445 static void dump_task(const char * const filename, const OSTask_t * const task)
449 f = fopen(filename, "r");
452 f = fopen(filename, "w");
456 "ucode_boot = %#08x size = %#x\n"
457 "ucode = %#08x size = %#x\n"
458 "ucode_data = %#08x size = %#x\n"
459 "dram_stack = %#08x size = %#x\n"
460 "output_buff = %#08x *size = %#x\n"
461 "data = %#08x size = %#x\n"
462 "yield_data = %#08x size = %#x\n",
463 task->type, task->flags,
464 task->ucode_boot, task->ucode_boot_size,
465 task->ucode, task->ucode_size,
466 task->ucode_data, task->ucode_data_size,
467 task->dram_stack, task->dram_stack_size,
468 task->output_buff, task->output_buff_size,
469 task->data_ptr, task->data_size,
470 task->yield_data_ptr, task->yield_data_size);