#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "usf/usf.h"
#include "usf/usf_internal.h"
#define M64P_CORE_PROTOTYPES 1
#include "api/m64p_types.h"
#include "api/callbacks.h"
#include "main.h"
#include "rom.h"
#include "savestates.h"
#include "util.h"
#include "ai/ai_controller.h"
#include "memory/memory.h"
#include "pi/pi_controller.h"
#include "r4300/r4300.h"
#include "r4300/r4300_core.h"
#include "r4300/interupt.h"
#include "r4300/reset.h"
#include "rdp/rdp_core.h"
#include "ri/ri_controller.h"
#include "rsp/rsp_core.h"
#include "si/si_controller.h"
#include "vi/vi_controller.h"
#define CONFIG_PARAM_VERSION 1.01
void main_message(usf_state_t * state, m64p_msg_level level, unsigned int corner, const char *format, ...)
{
va_list ap;
char buffer[2049];
va_start(ap, format);
vsnprintf(buffer, 2047, format, ap);
buffer[2048]='\0';
va_end(ap);
DebugMessage(state, level, "%s", buffer);
}
m64p_error main_reset(usf_state_t * state, int do_hard_reset)
{
if (do_hard_reset)
state->reset_hard_job |= 1;
else
reset_soft(state);
return M64ERR_SUCCESS;
}
static void connect_all(
usf_state_t* state,
struct r4300_core* r4300,
struct rdp_core* dp,
struct rsp_core* sp,
struct ai_controller* ai,
struct pi_controller* pi,
struct ri_controller* ri,
struct si_controller* si,
struct vi_controller* vi,
uint32_t* dram,
size_t dram_size,
uint8_t* rom,
size_t rom_size)
{
connect_r4300(r4300, state);
connect_rdp(dp, r4300, sp, ri);
connect_rsp(sp, r4300, dp, ri);
connect_ai(ai, r4300, ri, vi);
connect_pi(pi, r4300, ri, rom, rom_size);
connect_ri(ri, dram, dram_size);
connect_si(si, r4300, ri);
connect_vi(vi, r4300);
}
m64p_error main_start(usf_state_t * state)
{
unsigned int RDRAMSize;
unsigned int disable_extra_mem;
memcpy(&RDRAMSize, state->save_state + 4, 4);
to_little_endian_buffer(&RDRAMSize, 4, 1);
#ifdef DEBUG_INFO
state->r4300emu = 0;
#else
state->r4300emu = state->enable_trimming_mode ? 0 : 2;
#endif
state->no_compiled_jump = 0;
state->g_delay_sp = 1;
state->g_disable_tlb_write_exception = 1;
disable_extra_mem = RDRAMSize == 0x400000;
state->count_per_op = COUNT_PER_OP_DEFAULT;
if (state->count_per_op <= 0)
state->count_per_op = state->ROM_PARAMS.countperop;
connect_all(state, &state->g_r4300, &state->g_dp, &state->g_sp,
&state->g_ai, &state->g_pi, &state->g_ri, &state->g_si, &state->g_vi,
state->g_rdram, (disable_extra_mem == 0) ? 0x800000 : 0x400000,
state->g_rom, state->g_rom_size);
init_memory(state, (disable_extra_mem == 0) ? 0x800000 : 0x400000);
state->g_ai.user_data = state;
state->g_ai.set_audio_format = usf_set_audio_format;
state->g_ai.push_audio_samples = usf_push_audio_samples;
r4300_reset_hard(state);
r4300_reset_soft(state);
r4300_begin(state);
if (!savestates_load(state, state->save_state, state->save_state_size, 0))
return M64ERR_INVALID_STATE;
if (state->enableFIFOfull)
{
state->g_delay_ai = 1;
ai_fifo_queue_int(&state->g_ai);
state->g_ai.regs[AI_STATUS_REG] |= 0x40000000;
}
if (state->enable_trimming_mode)
{
state->g_delay_si = 1;
state->g_delay_ai = 1;
state->g_delay_pi = 1;
state->g_delay_dp = 1;
state->enable_hle_audio = 0;
}
return M64ERR_SUCCESS;
}
void main_run(usf_state_t * state)
{
r4300_execute(state);
}