Merge Icache emulation from PCSX Redux + Senquack changes from PCSX4ALL
[pcsx_rearmed.git] / libpcsxcore / lightrec / plugin.c
index 3cd672a..3e68a9c 100644 (file)
@@ -42,6 +42,7 @@ static struct lightrec_state *lightrec_state;
 static char *name = "retroarch.exe";
 
 static bool use_lightrec_interpreter;
+static bool use_pcsx_interpreter;
 static bool lightrec_debug;
 static bool lightrec_very_debug;
 static u32 lightrec_begin_cycles;
@@ -111,7 +112,7 @@ static void (*cp2_ops[])(struct psxCP2Regs *) = {
 
 static char cache_buf[64 * 1024];
 
-static u32 cop0_mfc(struct lightrec_state *state, u8 reg)
+static u32 cop0_mfc(struct lightrec_state *state, u32 op, u8 reg)
 {
        return psxRegs.CP0.r[reg];
 }
@@ -124,12 +125,12 @@ static u32 cop2_mfc_cfc(struct lightrec_state *state, u8 reg, bool cfc)
                return MFC2(reg);
 }
 
-static u32 cop2_mfc(struct lightrec_state *state, u8 reg)
+static u32 cop2_mfc(struct lightrec_state *state, u32 op, u8 reg)
 {
        return cop2_mfc_cfc(state, reg, false);
 }
 
-static u32 cop2_cfc(struct lightrec_state *state, u8 reg)
+static u32 cop2_cfc(struct lightrec_state *state, u32 op, u8 reg)
 {
        return cop2_mfc_cfc(state, reg, true);
 }
@@ -176,22 +177,22 @@ static void cop2_mtc_ctc(struct lightrec_state *state,
                MTC2(value, reg);
 }
 
-static void cop0_mtc(struct lightrec_state *state, u8 reg, u32 value)
+static void cop0_mtc(struct lightrec_state *state, u32 op, u8 reg, u32 value)
 {
        cop0_mtc_ctc(state, reg, value, false);
 }
 
-static void cop0_ctc(struct lightrec_state *state, u8 reg, u32 value)
+static void cop0_ctc(struct lightrec_state *state, u32 op, u8 reg, u32 value)
 {
        cop0_mtc_ctc(state, reg, value, true);
 }
 
-static void cop2_mtc(struct lightrec_state *state, u8 reg, u32 value)
+static void cop2_mtc(struct lightrec_state *state, u32 op, u8 reg, u32 value)
 {
        cop2_mtc_ctc(state, reg, value, false);
 }
 
-static void cop2_ctc(struct lightrec_state *state, u8 reg, u32 value)
+static void cop2_ctc(struct lightrec_state *state, u32 op, u8 reg, u32 value)
 {
        cop2_mtc_ctc(state, reg, value, true);
 }
@@ -211,7 +212,8 @@ static void cop2_op(struct lightrec_state *state, u32 func)
                cp2_ops[func & 0x3f](&psxRegs.CP2);
 }
 
-static void hw_write_byte(struct lightrec_state *state, u32 mem, u8 val)
+static void hw_write_byte(struct lightrec_state *state,
+                         u32 op, void *host, u32 mem, u8 val)
 {
        psxRegs.cycle = lightrec_current_cycle_count(state);
 
@@ -221,7 +223,8 @@ static void hw_write_byte(struct lightrec_state *state, u32 mem, u8 val)
        lightrec_reset_cycle_count(state, psxRegs.cycle);
 }
 
-static void hw_write_half(struct lightrec_state *state, u32 mem, u16 val)
+static void hw_write_half(struct lightrec_state *state,
+                         u32 op, void *host, u32 mem, u16 val)
 {
        psxRegs.cycle = lightrec_current_cycle_count(state);
 
@@ -231,7 +234,8 @@ static void hw_write_half(struct lightrec_state *state, u32 mem, u16 val)
        lightrec_reset_cycle_count(state, psxRegs.cycle);
 }
 
-static void hw_write_word(struct lightrec_state *state, u32 mem, u32 val)
+static void hw_write_word(struct lightrec_state *state,
+                         u32 op, void *host, u32 mem, u32 val)
 {
        psxRegs.cycle = lightrec_current_cycle_count(state);
 
@@ -241,7 +245,7 @@ static void hw_write_word(struct lightrec_state *state, u32 mem, u32 val)
        lightrec_reset_cycle_count(state, psxRegs.cycle);
 }
 
-static u8 hw_read_byte(struct lightrec_state *state, u32 mem)
+static u8 hw_read_byte(struct lightrec_state *state, u32 op, void *host, u32 mem)
 {
        u8 val;
 
@@ -254,7 +258,8 @@ static u8 hw_read_byte(struct lightrec_state *state, u32 mem)
        return val;
 }
 
-static u16 hw_read_half(struct lightrec_state *state, u32 mem)
+static u16 hw_read_half(struct lightrec_state *state,
+                       u32 op, void *host, u32 mem)
 {
        u16 val;
 
@@ -267,7 +272,8 @@ static u16 hw_read_half(struct lightrec_state *state, u32 mem)
        return val;
 }
 
-static u32 hw_read_word(struct lightrec_state *state, u32 mem)
+static u32 hw_read_word(struct lightrec_state *state,
+                       u32 op, void *host, u32 mem)
 {
        u32 val;
 
@@ -291,12 +297,14 @@ static struct lightrec_mem_map_ops hw_regs_ops = {
 
 static u32 cache_ctrl;
 
-static void cache_ctrl_write_word(struct lightrec_state *state, u32 mem, u32 val)
+static void cache_ctrl_write_word(struct lightrec_state *state,
+                                 u32 op, void *host, u32 mem, u32 val)
 {
        cache_ctrl = val;
 }
 
-static u32 cache_ctrl_read_word(struct lightrec_state *state, u32 mem)
+static u32 cache_ctrl_read_word(struct lightrec_state *state,
+                               u32 op, void *host, u32 mem)
 {
        return cache_ctrl;
 }
@@ -393,11 +401,11 @@ static int lightrec_plugin_init(void)
                        lightrec_map, ARRAY_SIZE(lightrec_map),
                        &lightrec_ops);
 
-       fprintf(stderr, "M=0x%lx, P=0x%lx, R=0x%lx, H=0x%lx\n",
-                       (uintptr_t) psxM,
-                       (uintptr_t) psxP,
-                       (uintptr_t) psxR,
-                       (uintptr_t) psxH);
+       // fprintf(stderr, "M=0x%lx, P=0x%lx, R=0x%lx, H=0x%lx\n",
+       //              (uintptr_t) psxM,
+       //              (uintptr_t) psxP,
+       //              (uintptr_t) psxR,
+       //              (uintptr_t) psxH);
 
 #ifndef _WIN32
        signal(SIGPIPE, exit);
@@ -489,6 +497,9 @@ static void print_for_big_ass_debugger(void)
        printf("\n");
 }
 
+
+extern void intExecuteBlock();
+
 static u32 old_cycle_counter;
 
 static void lightrec_plugin_execute_block(void)
@@ -496,27 +507,33 @@ static void lightrec_plugin_execute_block(void)
        u32 old_pc = psxRegs.pc;
        u32 flags;
 
-       lightrec_reset_cycle_count(lightrec_state, psxRegs.cycle);
-       lightrec_restore_registers(lightrec_state, psxRegs.GPR.r);
+       if (use_pcsx_interpreter) {
+               intExecuteBlock();
+       } else {
+               lightrec_reset_cycle_count(lightrec_state, psxRegs.cycle);
+               lightrec_restore_registers(lightrec_state, psxRegs.GPR.r);
 
-       if (use_lightrec_interpreter)
-               psxRegs.pc = lightrec_run_interpreter(lightrec_state, psxRegs.pc);
-       else
-               psxRegs.pc = lightrec_execute_one(lightrec_state, psxRegs.pc);
+               if (use_lightrec_interpreter)
+                       psxRegs.pc = lightrec_run_interpreter(lightrec_state,
+                                                             psxRegs.pc);
+               else
+                       psxRegs.pc = lightrec_execute_one(lightrec_state,
+                                                         psxRegs.pc);
 
-       psxRegs.cycle = lightrec_current_cycle_count(lightrec_state);
+               psxRegs.cycle = lightrec_current_cycle_count(lightrec_state);
 
-       lightrec_dump_registers(lightrec_state, psxRegs.GPR.r);
-       flags = lightrec_exit_flags(lightrec_state);
+               lightrec_dump_registers(lightrec_state, psxRegs.GPR.r);
+               flags = lightrec_exit_flags(lightrec_state);
 
-       if (flags & LIGHTREC_EXIT_SEGFAULT) {
-               fprintf(stderr, "Exiting at cycle 0x%08x\n",
-                       psxRegs.cycle);
-               exit(1);
-       }
+               if (flags & LIGHTREC_EXIT_SEGFAULT) {
+                       fprintf(stderr, "Exiting at cycle 0x%08x\n",
+                               psxRegs.cycle);
+                       exit(1);
+               }
 
-       if (flags & LIGHTREC_EXIT_SYSCALL)
-               psxException(0x20, 0);
+               if (flags & LIGHTREC_EXIT_SYSCALL)
+                       psxException(0x20, 0);
+       }
 
        psxBranchTest();
 
@@ -532,8 +549,9 @@ static void lightrec_plugin_execute_block(void)
        }
 
        if ((psxRegs.cycle & ~0xfffffff) != old_cycle_counter) {
-               printf("RAM usage: IR %u KiB, CODE %u KiB, "
+               SysDLog("RAM usage: Lightrec %u KiB, IR %u KiB, CODE %u KiB, "
                       "MIPS %u KiB, TOTAL %u KiB, avg. IPI %f\n",
+                      lightrec_get_mem_usage(MEM_FOR_LIGHTREC) / 1024,
                       lightrec_get_mem_usage(MEM_FOR_IR) / 1024,
                       lightrec_get_mem_usage(MEM_FOR_CODE) / 1024,
                       lightrec_get_mem_usage(MEM_FOR_MIPS_CODE) / 1024,
@@ -553,9 +571,31 @@ static void lightrec_plugin_execute(void)
 
 static void lightrec_plugin_clear(u32 addr, u32 size)
 {
-       /* size * 4: PCSX uses DMA units */
-       lightrec_invalidate(lightrec_state, addr, size * 4);
+       if (addr == 0 && size == UINT32_MAX)
+               lightrec_invalidate_all(lightrec_state);
+       else
+               /* size * 4: PCSX uses DMA units */
+               lightrec_invalidate(lightrec_state, addr, size * 4);
+}
+
+#ifdef ICACHE_EMULATION
+static void lightrec_plugin_notify(int note, void *data)
+{
+       /*
+       To change once proper icache emulation is emulated
+       switch (note)
+       {
+               case R3000ACPU_NOTIFY_CACHE_UNISOLATED:
+                       lightrec_plugin_clear(0, 0x200000/4);
+                       break;
+               case R3000ACPU_NOTIFY_CACHE_ISOLATED:
+               // Sent from psxDma3().
+               case R3000ACPU_NOTIFY_DMA3_EXE_LOAD:
+               default:
+                       break;
+       }*/
 }
+#endif
 
 static void lightrec_plugin_shutdown(void)
 {
@@ -564,10 +604,8 @@ static void lightrec_plugin_shutdown(void)
 
 static void lightrec_plugin_reset(void)
 {
-// Called in every frame (or every frame where input changes) when using runahead
-//     lightrec_plugin_shutdown();
-//     lightrec_plugin_init();
-       lightrec_invalidate_all(lightrec_state);
+       lightrec_plugin_shutdown();
+       lightrec_plugin_init();
 }
 
 R3000Acpu psxRec =
@@ -577,5 +615,8 @@ R3000Acpu psxRec =
        lightrec_plugin_execute,
        lightrec_plugin_execute_block,
        lightrec_plugin_clear,
+#ifdef ICACHE_EMULATION
+       lightrec_plugin_notify,
+#endif
        lightrec_plugin_shutdown,
 };