Update Lightrec 2023-02-08 (#715)
[pcsx_rearmed.git] / deps / lightrec / lightrec.c
index 43636b2..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,7 +428,10 @@ 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)
+
+       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];
@@ -461,7 +461,9 @@ static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
 {
        u32 rt = lightrec_mfc(state, op);
 
-       if (op.r.rt)
+       if (op.i.op == OP_SWC2)
+               state->cp2_temp_reg = rt;
+       else if (op.r.rt)
                state->regs.gpr[op.r.rt] = rt;
 }
 
@@ -579,15 +581,15 @@ 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);
+               lightrec_mtc0(state, reg, data);
        } else {
-               if (op.r.rs == OP_CP2_BASIC_CTC2)
-                       lightrec_ctc2(state, op.r.rd, data);
+               if (op.i.op == OP_LWC2 || op.r.rs != OP_CP2_BASIC_CTC2)
+                       lightrec_mtc2(state, reg, data);
                else
-                       lightrec_mtc2(state, op.r.rd, data);
+                       lightrec_ctc2(state, reg, data);
 
                if (state->ops.cop2_notify)
                        (*state->ops.cop2_notify)(state, op.opcode, data);
@@ -597,8 +599,18 @@ void lightrec_mtc(struct lightrec_state *state, union code op, u32 data)
 static void lightrec_mtc_cb(struct lightrec_state *state, u32 arg)
 {
        union code op = (union code) arg;
+       u32 data;
+       u8 reg;
 
-       lightrec_mtc(state, op, state->regs.gpr[op.r.rt]);
+       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, reg, data);
 }
 
 void lightrec_rfe(struct lightrec_state *state)
@@ -674,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;
@@ -743,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;
@@ -850,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;
 }
 
@@ -1012,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());
 
@@ -1019,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 */
@@ -1118,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);
 
@@ -1402,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;
@@ -1423,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);
@@ -1606,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;
 
@@ -1623,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;
        }