+static void rec_load_bios(struct lightrec_cstate *cstate,
+ const struct block *block, u16 offset,
+ jit_code_t code, jit_code_t swap_code, bool is_unsigned)
+{
+ _jit_note(block->_jit, __FILE__, __LINE__);
+
+ rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
+ cstate->state->offset_bios, 0x1fffffff);
+}
+
+static void rec_load_scratch(struct lightrec_cstate *cstate,
+ const struct block *block, u16 offset,
+ jit_code_t code, jit_code_t swap_code, bool is_unsigned)
+{
+ _jit_note(block->_jit, __FILE__, __LINE__);
+
+ rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
+ cstate->state->offset_scratch, 0x1fffffff);
+}
+
+static void rec_load_io(struct lightrec_cstate *cstate,
+ const struct block *block, u16 offset,
+ jit_code_t code, jit_code_t swap_code, bool is_unsigned)
+{
+ _jit_note(block->_jit, __FILE__, __LINE__);
+
+ rec_load_memory(cstate, block, offset, code, swap_code, is_unsigned,
+ cstate->state->offset_io, rec_io_mask(cstate->state));
+}
+
+static void rec_load_direct(struct lightrec_cstate *cstate,
+ const struct block *block, u16 offset,
+ jit_code_t code, jit_code_t swap_code,
+ bool is_unsigned)
+{
+ struct lightrec_state *state = cstate->state;
+ struct regcache *reg_cache = cstate->reg_cache;
+ struct opcode *op = &block->opcode_list[offset];
+ bool load_delay = op_flag_load_delay(op->flags) && !cstate->no_load_delay;
+ jit_state_t *_jit = block->_jit;
+ jit_node_t *to_not_ram, *to_not_bios, *to_end, *to_end2;
+ u8 tmp, rs, rt, out_reg, addr_reg, flags = REG_EXT;
+ union code c = op->c;
+ s16 imm;
+
+ if (load_delay || c.i.op == OP_LWC2)
+ out_reg = REG_TEMP;
+ else if (c.i.rt)
+ out_reg = c.i.rt;
+ else
+ return;
+
+ if (is_unsigned)
+ flags |= REG_ZEXT;
+
+ jit_note(__FILE__, __LINE__);
+ rs = lightrec_alloc_reg_in(reg_cache, _jit, c.i.rs, 0);
+ rt = lightrec_alloc_reg_out(reg_cache, _jit, out_reg, flags);
+
+ if ((state->offset_ram == state->offset_bios &&
+ state->offset_ram == state->offset_scratch &&
+ state->mirrors_mapped) || !c.i.imm) {
+ addr_reg = rs;
+ imm = (s16)c.i.imm;
+ } else {
+ jit_addi(rt, rs, (s16)c.i.imm);
+ addr_reg = rt;
+ imm = 0;
+
+ if (c.i.rs != c.i.rt)
+ lightrec_free_reg(reg_cache, rs);
+ }
+
+ tmp = lightrec_alloc_reg_temp(reg_cache, _jit);
+
+ if (state->offset_ram == state->offset_bios &&
+ state->offset_ram == state->offset_scratch) {
+ if (!state->mirrors_mapped) {
+ jit_andi(tmp, addr_reg, BIT(28));
+ jit_rshi_u(tmp, tmp, 28 - 22);
+ jit_ori(tmp, tmp, 0x1f800000 | (RAM_SIZE - 1));
+ jit_andr(rt, addr_reg, tmp);
+ } else {
+ jit_andi(rt, addr_reg, 0x1fffffff);
+ }
+
+ if (state->offset_ram)
+ jit_movi(tmp, state->offset_ram);
+ } else {
+ to_not_ram = jit_bmsi(addr_reg, BIT(28));
+
+ /* Convert to KUNSEG and avoid RAM mirrors */
+ jit_andi(rt, addr_reg, RAM_SIZE - 1);
+
+ if (state->offset_ram)
+ jit_movi(tmp, state->offset_ram);
+
+ to_end = jit_b();
+
+ jit_patch(to_not_ram);