psxmem: Add support for Lightrec's custom mem init sequence
[pcsx_rearmed.git] / deps / lightrec / lightrec.c
index 3a6e112..9889272 100644 (file)
@@ -250,13 +250,13 @@ u32 lightrec_rw(struct lightrec_state *state, union code op,
        }
 
        if (unlikely(map->ops)) {
-               if (flags)
-                       *flags |= LIGHTREC_HW_IO;
+               if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
+                       *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
 
                ops = map->ops;
        } else {
-               if (flags)
-                       *flags |= LIGHTREC_DIRECT_IO;
+               if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
+                       *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
 
                ops = &lightrec_default_ops;
        }
@@ -323,16 +323,17 @@ static void lightrec_rw_helper(struct lightrec_state *state,
        }
 }
 
-static void lightrec_rw_cb(struct lightrec_state *state, union code op)
+static void lightrec_rw_cb(struct lightrec_state *state)
 {
-       lightrec_rw_helper(state, op, NULL, NULL);
+       lightrec_rw_helper(state, (union code)state->c_wrapper_arg, NULL, NULL);
 }
 
-static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
+static void lightrec_rw_generic_cb(struct lightrec_state *state)
 {
        struct block *block;
        struct opcode *op;
        bool was_tagged;
+       u32 arg = state->c_wrapper_arg;
        u16 offset = (u16)arg;
 
        block = lightrec_find_block_from_lut(state->block_cache,
@@ -344,7 +345,7 @@ static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
        }
 
        op = &block->opcode_list[offset];
-       was_tagged = op->flags & (LIGHTREC_HW_IO | LIGHTREC_DIRECT_IO);
+       was_tagged = LIGHTREC_FLAGS_GET_IO_MODE(op->flags);
 
        lightrec_rw_helper(state, op->c, &op->flags, block);
 
@@ -406,17 +407,9 @@ u32 lightrec_mfc(struct lightrec_state *state, union code op)
                return state->regs.cp2c[op.r.rd];
 }
 
-static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
-{
-       u32 rt = lightrec_mfc(state, op);
-
-       if (op.r.rt)
-               state->regs.gpr[op.r.rt] = rt;
-}
-
 static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
 {
-       u32 status, cause;
+       u32 status, oldstatus, cause;
 
        switch (reg) {
        case 1:
@@ -426,12 +419,13 @@ static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
        case 15:
                /* Those registers are read-only */
                return;
-       default: /* fall-through */
+       default:
                break;
        }
 
        if (reg == 12) {
                status = state->regs.cp0[12];
+               oldstatus = status;
 
                if (status & ~data & BIT(16)) {
                        state->ops.enable_ram(state, true);
@@ -441,14 +435,24 @@ static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
                }
        }
 
-       state->regs.cp0[reg] = data;
+       if (reg == 13) {
+               state->regs.cp0[13] &= ~0x300;
+               state->regs.cp0[13] |= data & 0x300;
+       } else {
+               state->regs.cp0[reg] = data;
+       }
 
        if (reg == 12 || reg == 13) {
                cause = state->regs.cp0[13];
                status = state->regs.cp0[12];
 
+               /* Handle software interrupts */
                if (!!(status & cause & 0x300) & status)
                        lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
+
+               /* Handle hardware interrupts */
+               if (reg == 12 && !(~status & 0x401) && (~oldstatus & 0x401))
+                       lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
        }
 }
 
@@ -526,8 +530,10 @@ void lightrec_mtc(struct lightrec_state *state, union code op, u32 data)
                lightrec_mtc2(state, op.r.rd, data);
 }
 
-static void lightrec_mtc_cb(struct lightrec_state *state, union code op)
+static void lightrec_mtc_cb(struct lightrec_state *state)
 {
+       union code op = (union code) state->c_wrapper_arg;
+
        lightrec_mtc(state, op, state->regs.gpr[op.r.rt]);
 }
 
@@ -555,12 +561,17 @@ void lightrec_cp(struct lightrec_state *state, union code op)
        (*state->ops.cop2_op)(state, op.opcode);
 }
 
-static void lightrec_syscall_cb(struct lightrec_state *state, union code op)
+static void lightrec_cp_cb(struct lightrec_state *state)
+{
+       lightrec_cp(state, (union code) state->c_wrapper_arg);
+}
+
+static void lightrec_syscall_cb(struct lightrec_state *state)
 {
        lightrec_set_exit_flags(state, LIGHTREC_EXIT_SYSCALL);
 }
 
-static void lightrec_break_cb(struct lightrec_state *state, union code op)
+static void lightrec_break_cb(struct lightrec_state *state)
 {
        lightrec_set_exit_flags(state, LIGHTREC_EXIT_BREAK);
 }
@@ -666,12 +677,11 @@ static void * get_next_block_func(struct lightrec_state *state, u32 pc)
 }
 
 static s32 c_function_wrapper(struct lightrec_state *state, s32 cycles_delta,
-                             void (*f)(struct lightrec_state *, u32 d),
-                             u32 d)
+                             void (*f)(struct lightrec_state *))
 {
        state->current_cycle = state->target_cycle - cycles_delta;
 
-       (*f)(state, d);
+       (*f)(state);
 
        return state->target_cycle - state->current_cycle;
 }
@@ -684,6 +694,7 @@ static struct block * generate_wrapper(struct lightrec_state *state)
        int stack_ptr;
        jit_word_t code_size;
        jit_node_t *to_tramp, *to_fn_epilog;
+       jit_node_t *addr[C_WRAPPERS_COUNT - 1];
 
        block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
        if (!block)
@@ -698,9 +709,22 @@ static struct block * generate_wrapper(struct lightrec_state *state)
 
        /* Wrapper entry point */
        jit_prolog();
+       jit_tramp(256);
+
+       /* Add entry points; separate them by opcodes that increment
+        * LIGHTREC_REG_STATE (since we cannot touch other registers).
+        * The difference will then tell us which C function to call. */
+       for (i = C_WRAPPERS_COUNT - 1; i > 0; i--) {
+               jit_addi(LIGHTREC_REG_STATE, LIGHTREC_REG_STATE, __WORDSIZE / 8);
+               addr[i - 1] = jit_indirect();
+       }
+
+       jit_epilog();
+       jit_prolog();
 
        stack_ptr = jit_allocai(sizeof(uintptr_t) * NUM_TEMPS);
 
+       /* Save all temporaries on stack */
        for (i = 0; i < NUM_TEMPS; i++)
                jit_stxi(stack_ptr + i * sizeof(uintptr_t), JIT_FP, JIT_R(i));
 
@@ -710,6 +734,7 @@ static struct block * generate_wrapper(struct lightrec_state *state)
        /* The trampoline will jump back here */
        to_fn_epilog = jit_label();
 
+       /* Restore temporaries from stack */
        for (i = 0; i < NUM_TEMPS; i++)
                jit_ldxi(JIT_R(i), JIT_FP, stack_ptr + i * sizeof(uintptr_t));
 
@@ -724,11 +749,17 @@ static struct block * generate_wrapper(struct lightrec_state *state)
        jit_tramp(256);
        jit_patch(to_tramp);
 
+       /* Retrieve the wrapper function */
+       jit_ldxi(JIT_R0, LIGHTREC_REG_STATE,
+                offsetof(struct lightrec_state, c_wrappers));
+
+       /* Restore LIGHTREC_REG_STATE to its correct value */
+       jit_movi(LIGHTREC_REG_STATE, (uintptr_t) state);
+
        jit_prepare();
        jit_pushargr(LIGHTREC_REG_STATE);
        jit_pushargr(LIGHTREC_REG_CYCLE);
        jit_pushargr(JIT_R0);
-       jit_pushargr(JIT_R1);
        jit_finishi(c_function_wrapper);
        jit_retval_i(LIGHTREC_REG_CYCLE);
 
@@ -741,6 +772,11 @@ static struct block * generate_wrapper(struct lightrec_state *state)
        block->flags = 0;
        block->nb_ops = 0;
 
+       state->wrappers_eps[C_WRAPPERS_COUNT - 1] = block->function;
+
+       for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
+               state->wrappers_eps[i] = jit_address(addr[i]);
+
        jit_get_code(&code_size);
        lightrec_register(MEM_FOR_CODE, code_size);
 
@@ -943,7 +979,7 @@ err_no_mem:
 
 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc)
 {
-       void *host;
+       void *host = NULL;
 
        lightrec_get_map(state, &host, kunseg(pc));
 
@@ -1100,8 +1136,7 @@ static bool lightrec_block_is_fully_tagged(const struct block *block)
                case OP_SWR:
                case OP_LWC2:
                case OP_SWC2:
-                       if (!(op->flags & (LIGHTREC_DIRECT_IO |
-                                          LIGHTREC_HW_IO)))
+                       if (!LIGHTREC_FLAGS_GET_IO_MODE(op->flags))
                                return false;
                default: /* fall-through */
                        continue;
@@ -1261,13 +1296,15 @@ int lightrec_compile_block(struct lightrec_cstate *cstate,
                         * finishes. */
                        if (ENABLE_THREADED_COMPILER)
                                lightrec_recompiler_remove(state->rec, block2);
+               }
 
-                       /* We know from now on that block2 isn't going to be
-                        * compiled. We can override the LUT entry with our
-                        * new block's entry point. */
-                       offset = lut_offset(block->pc) + target->offset;
-                       state->code_lut[offset] = jit_address(target->label);
+               /* We know from now on that block2 (if present) isn't going to
+                * be compiled. We can override the LUT entry with our new
+                * block's entry point. */
+               offset = lut_offset(block->pc) + target->offset;
+               state->code_lut[offset] = jit_address(target->label);
 
+               if (block2) {
                        pr_debug("Reap block 0x%08x as it's covered by block "
                                 "0x%08x\n", block2->pc, block->pc);
 
@@ -1487,13 +1524,10 @@ struct lightrec_state * lightrec_init(char *argv0,
        if (!state->c_wrapper_block)
                goto err_free_dispatcher;
 
-       state->c_wrapper = state->c_wrapper_block->function;
-
        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;
+       state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;
        state->c_wrappers[C_WRAPPER_SYSCALL] = lightrec_syscall_cb;
        state->c_wrappers[C_WRAPPER_BREAK] = lightrec_break_cb;