spu: irq adjustments according to MiSTer
[pcsx_rearmed.git] / deps / lightrec / lightrec.c
index fa08a80..b9e82fb 100644 (file)
@@ -28,9 +28,6 @@
 #include <stddef.h>
 #include <string.h>
 
-#define GENMASK(h, l) \
-       (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
-
 static struct block * lightrec_precompile_block(struct lightrec_state *state,
                                                u32 pc);
 static bool lightrec_block_is_fully_tagged(const struct block *block);
@@ -107,7 +104,7 @@ static void lightrec_swl(struct lightrec_state *state,
                         u32 opcode, void *host, u32 addr, u32 data)
 {
        unsigned int shift = addr & 0x3;
-       unsigned int mask = GENMASK(31, (shift + 1) * 8);
+       unsigned int mask = shift < 3 ? GENMASK(31, (shift + 1) * 8) : 0;
        u32 old_data;
 
        /* Align to 32 bits */
@@ -171,7 +168,7 @@ static u32 lightrec_lwr(struct lightrec_state *state,
                        u32 opcode, void *host, u32 addr, u32 data)
 {
        unsigned int shift = addr & 0x3;
-       unsigned int mask = GENMASK(31, 32 - shift * 8);
+       unsigned int mask = shift ? GENMASK(31, 32 - shift * 8) : 0;
        u32 old_data;
 
        /* Align to 32 bits */
@@ -201,7 +198,7 @@ static void lightrec_invalidate_map(struct lightrec_state *state,
        }
 }
 
-enum psx_map
+static enum psx_map
 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr)
 {
        const struct lightrec_mem_map *map;
@@ -431,23 +428,43 @@ u32 lightrec_mfc(struct lightrec_state *state, union code op)
 
        if (op.i.op == OP_CP0)
                return state->regs.cp0[op.r.rd];
-       else if (op.r.rs == OP_CP2_BASIC_MFC2)
-               return lightrec_mfc2(state, op.r.rd);
-
-       val = state->regs.cp2c[op.r.rd];
 
-       switch (op.r.rd) {
-       case 4:
-       case 12:
-       case 20:
-       case 26:
-       case 27:
-       case 29:
-       case 30:
-               return (u32)(s16)val;
-       default:
-               return val;
+       if (op.i.op == OP_SWC2) {
+               val = lightrec_mfc2(state, op.i.rt);
+       } else if (op.r.rs == OP_CP2_BASIC_MFC2)
+               val = lightrec_mfc2(state, op.r.rd);
+       else {
+               val = state->regs.cp2c[op.r.rd];
+
+               switch (op.r.rd) {
+               case 4:
+               case 12:
+               case 20:
+               case 26:
+               case 27:
+               case 29:
+               case 30:
+                       val = (u32)(s16)val;
+                       fallthrough;
+               default:
+                       break;
+               }
        }
+
+       if (state->ops.cop2_notify)
+               (*state->ops.cop2_notify)(state, op.opcode, val);
+
+       return val;
+}
+
+static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
+{
+       u32 rt = lightrec_mfc(state, op);
+
+       if (op.i.op == OP_SWC2)
+               state->cp2_temp_reg = rt;
+       else if (op.r.rt)
+               state->regs.gpr[op.r.rt] = rt;
 }
 
 static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
@@ -564,21 +581,36 @@ static void lightrec_ctc2(struct lightrec_state *state, u8 reg, u32 data)
        }
 }
 
-void lightrec_mtc(struct lightrec_state *state, union code op, u32 data)
+void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data)
 {
-       if (op.i.op == OP_CP0)
-               lightrec_mtc0(state, op.r.rd, data);
-       else if (op.r.rs == OP_CP2_BASIC_CTC2)
-               lightrec_ctc2(state, op.r.rd, data);
-       else
-               lightrec_mtc2(state, op.r.rd, data);
+       if (op.i.op == OP_CP0) {
+               lightrec_mtc0(state, reg, data);
+       } else {
+               if (op.i.op == OP_LWC2 || op.r.rs != OP_CP2_BASIC_CTC2)
+                       lightrec_mtc2(state, reg, data);
+               else
+                       lightrec_ctc2(state, reg, data);
+
+               if (state->ops.cop2_notify)
+                       (*state->ops.cop2_notify)(state, op.opcode, data);
+       }
 }
 
 static void lightrec_mtc_cb(struct lightrec_state *state, u32 arg)
 {
        union code op = (union code) arg;
+       u32 data;
+       u8 reg;
+
+       if (op.i.op == OP_LWC2) {
+               data = state->cp2_temp_reg;
+               reg = op.i.rt;
+       } else {
+               data = state->regs.gpr[op.r.rt];
+               reg = op.r.rd;
+       }
 
-       lightrec_mtc(state, op, state->regs.gpr[op.r.rt]);
+       lightrec_mtc(state, op, reg, data);
 }
 
 void lightrec_rfe(struct lightrec_state *state)
@@ -654,7 +686,7 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
        void *func;
        int err;
 
-       for (;;) {
+       do {
                func = lut_read(state, lut_offset(pc));
                if (func && func != state->get_next_block)
                        break;
@@ -723,11 +755,8 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
                } else {
                        lightrec_recompiler_add(state->rec, block);
                }
-
-               if (state->exit_flags != LIGHTREC_EXIT_NORMAL ||
-                   state->current_cycle >= state->target_cycle)
-                       break;
-       }
+       } while (state->exit_flags == LIGHTREC_EXIT_NORMAL
+                && state->current_cycle < state->target_cycle);
 
        state->next_pc = pc;
        return func;
@@ -830,6 +859,9 @@ static void * lightrec_emit_code(struct lightrec_state *state,
 
        *size = (unsigned int) new_code_size;
 
+       if (state->ops.code_inv)
+               state->ops.code_inv(code, new_code_size);
+
        return code;
 }
 
@@ -992,6 +1024,8 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
        jit_prolog();
        jit_frame(256);
 
+       jit_getarg(LIGHTREC_REG_STATE, jit_arg());
+       jit_getarg(JIT_V0, jit_arg());
        jit_getarg(JIT_V1, jit_arg());
        jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
 
@@ -999,10 +1033,6 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
        for (i = 0; i < NUM_REGS; i++)
                jit_movr(JIT_V(i + FIRST_REG), JIT_V(i + FIRST_REG));
 
-       /* Pass lightrec_state structure to blocks, using the last callee-saved
-        * register that Lightning provides */
-       jit_movi(LIGHTREC_REG_STATE, (intptr_t) state);
-
        loop = jit_label();
 
        /* Call the block's code */
@@ -1098,6 +1128,10 @@ static struct block * generate_dispatcher(struct lightrec_state *state)
                jit_movr(LIGHTREC_REG_CYCLE, JIT_V0);
        }
 
+       /* Reset JIT_V0 to the next PC */
+       jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
+                   offsetof(struct lightrec_state, next_pc));
+
        /* If we get non-NULL, loop */
        jit_patch_at(jit_bnei(JIT_V1, 0), loop);
 
@@ -1382,6 +1416,8 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
        block->_jit = _jit;
 
        lightrec_regcache_reset(cstate->reg_cache);
+       lightrec_preload_pc(cstate->reg_cache);
+
        cstate->cycles = 0;
        cstate->nb_local_branches = 0;
        cstate->nb_targets = 0;
@@ -1403,7 +1439,7 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                        pr_debug("Branch at offset 0x%x will be emulated\n",
                                 i << 2);
 
-                       lightrec_emit_eob(cstate, block, i, false);
+                       lightrec_emit_eob(cstate, block, i);
                        skip_next = !op_flag_no_ds(elm->flags);
                } else {
                        lightrec_rec_opcode(cstate, block, i);
@@ -1586,7 +1622,7 @@ static void lightrec_print_info(struct lightrec_state *state)
 
 u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
 {
-       s32 (*func)(void *, s32) = (void *)state->dispatcher->function;
+       s32 (*func)(struct lightrec_state *, u32, void *, s32) = (void *)state->dispatcher->function;
        void *block_trace;
        s32 cycles_delta;
 
@@ -1603,7 +1639,8 @@ u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
        if (block_trace) {
                cycles_delta = state->target_cycle - state->current_cycle;
 
-               cycles_delta = (*func)(block_trace, cycles_delta);
+               cycles_delta = (*func)(state, state->next_pc,
+                                      block_trace, cycles_delta);
 
                state->current_cycle = state->target_cycle - cycles_delta;
        }
@@ -1703,6 +1740,11 @@ struct lightrec_state * lightrec_init(char *argv0,
                return NULL;
        }
 
+       if (ops->cop2_notify)
+               pr_debug("Optional cop2_notify callback in lightrec_ops\n");
+       else
+               pr_debug("No optional cop2_notify callback in lightrec_ops\n");
+
        if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER
            && codebuf_map->address) {
                tlsf = tlsf_create_with_pool(codebuf_map->address,
@@ -1767,6 +1809,7 @@ struct lightrec_state * lightrec_init(char *argv0,
 
        state->c_wrappers[C_WRAPPER_RW] = lightrec_rw_cb;
        state->c_wrappers[C_WRAPPER_RW_GENERIC] = lightrec_rw_generic_cb;
+       state->c_wrappers[C_WRAPPER_MFC] = lightrec_mfc_cb;
        state->c_wrappers[C_WRAPPER_MTC] = lightrec_mtc_cb;
        state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;