12 #include "../psxdma.h"
14 #include "../psxmem.h"
15 #include "../r3000a.h"
16 #include "../psxinterpreter.h"
17 #include "../new_dynarec/events.h"
19 #include "../frontend/main.h"
24 #if (defined(__arm__) || defined(__aarch64__)) && !defined(ALLOW_LIGHTREC_ON_ARM)
25 #error "Lightrec should not be used on ARM (please specify DYNAREC=ari64 to make)"
28 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
30 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
31 # define LE32TOH(x) __builtin_bswap32(x)
32 # define HTOLE32(x) __builtin_bswap32(x)
33 # define LE16TOH(x) __builtin_bswap16(x)
34 # define HTOLE16(x) __builtin_bswap16(x)
36 # define LE32TOH(x) (x)
37 # define HTOLE32(x) (x)
38 # define LE16TOH(x) (x)
39 # define HTOLE16(x) (x)
43 # define likely(x) __builtin_expect(!!(x),1)
44 # define unlikely(x) __builtin_expect(!!(x),0)
46 # define likely(x) (x)
47 # define unlikely(x) (x)
53 static struct lightrec_state *lightrec_state;
55 static char *name = "retroarch.exe";
57 static bool use_lightrec_interpreter;
58 static bool use_pcsx_interpreter;
59 static bool block_stepping;
60 static u32 cycle_mult_to_pcsx; // 22.10 fractional
61 static u32 cycle_mult_from_pcsx;
88 static void (*cp2_ops[])(struct psxCP2Regs *) = {
89 [OP_CP2_RTPS] = gteRTPS,
90 [OP_CP2_RTPS] = gteRTPS,
91 [OP_CP2_NCLIP] = gteNCLIP,
93 [OP_CP2_DPCS] = gteDPCS,
94 [OP_CP2_INTPL] = gteINTPL,
95 [OP_CP2_MVMVA] = gteMVMVA,
96 [OP_CP2_NCDS] = gteNCDS,
97 [OP_CP2_CDP] = gteCDP,
98 [OP_CP2_NCDT] = gteNCDT,
99 [OP_CP2_NCCS] = gteNCCS,
101 [OP_CP2_NCS] = gteNCS,
102 [OP_CP2_NCT] = gteNCT,
103 [OP_CP2_SQR] = gteSQR,
104 [OP_CP2_DCPL] = gteDCPL,
105 [OP_CP2_DPCT] = gteDPCT,
106 [OP_CP2_AVSZ3] = gteAVSZ3,
107 [OP_CP2_AVSZ4] = gteAVSZ4,
108 [OP_CP2_RTPT] = gteRTPT,
109 [OP_CP2_GPF] = gteGPF,
110 [OP_CP2_GPL] = gteGPL,
111 [OP_CP2_NCCT] = gteNCCT,
114 static char cache_buf[64 * 1024];
116 static void cop2_op(struct lightrec_state *state, u32 func)
118 struct lightrec_registers *regs = lightrec_get_registers(state);
122 if (unlikely(!cp2_ops[func & 0x3f])) {
123 fprintf(stderr, "Invalid CP2 function %u\n", func);
125 /* This works because regs->cp2c comes right after regs->cp2d,
126 * so it can be cast to a pcsxCP2Regs pointer. */
127 cp2_ops[func & 0x3f]((psxCP2Regs *) regs->cp2d);
131 static bool has_interrupt(void)
133 struct lightrec_registers *regs = lightrec_get_registers(lightrec_state);
135 return ((psxHu32(0x1070) & psxHu32(0x1074)) &&
136 (regs->cp0[12] & 0x401) == 0x401) ||
137 (regs->cp0[12] & regs->cp0[13] & 0x0300);
140 static u32 cycles_pcsx_to_lightrec(u32 c)
142 assert((u64)c * cycle_mult_from_pcsx <= (u32)-1);
143 return c * cycle_mult_from_pcsx >> 10;
146 static void lightrec_tansition_to_pcsx(struct lightrec_state *state)
148 psxRegs.cycle += lightrec_current_cycle_count(state) * cycle_mult_to_pcsx >> 10;
149 lightrec_reset_cycle_count(state, 0);
152 static void lightrec_tansition_from_pcsx(struct lightrec_state *state)
154 s32 cycles_left = next_interupt - psxRegs.cycle;
156 if (block_stepping || cycles_left <= 0 || has_interrupt())
157 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
159 lightrec_set_target_cycle_count(state,
160 cycles_pcsx_to_lightrec(cycles_left));
164 static void hw_write_byte(struct lightrec_state *state,
165 u32 op, void *host, u32 mem, u8 val)
167 lightrec_tansition_to_pcsx(state);
169 psxHwWrite8(mem, val);
171 lightrec_tansition_from_pcsx(state);
174 static void hw_write_half(struct lightrec_state *state,
175 u32 op, void *host, u32 mem, u16 val)
177 lightrec_tansition_to_pcsx(state);
179 psxHwWrite16(mem, val);
181 lightrec_tansition_from_pcsx(state);
184 static void hw_write_word(struct lightrec_state *state,
185 u32 op, void *host, u32 mem, u32 val)
187 lightrec_tansition_to_pcsx(state);
189 psxHwWrite32(mem, val);
191 lightrec_tansition_from_pcsx(state);
194 static u8 hw_read_byte(struct lightrec_state *state, u32 op, void *host, u32 mem)
198 lightrec_tansition_to_pcsx(state);
200 val = psxHwRead8(mem);
202 lightrec_tansition_from_pcsx(state);
207 static u16 hw_read_half(struct lightrec_state *state,
208 u32 op, void *host, u32 mem)
212 lightrec_tansition_to_pcsx(state);
214 val = psxHwRead16(mem);
216 lightrec_tansition_from_pcsx(state);
221 static u32 hw_read_word(struct lightrec_state *state,
222 u32 op, void *host, u32 mem)
226 lightrec_tansition_to_pcsx(state);
228 val = psxHwRead32(mem);
230 lightrec_tansition_from_pcsx(state);
235 static struct lightrec_mem_map_ops hw_regs_ops = {
244 static u32 cache_ctrl;
246 static void cache_ctrl_write_word(struct lightrec_state *state,
247 u32 op, void *host, u32 mem, u32 val)
252 static u32 cache_ctrl_read_word(struct lightrec_state *state,
253 u32 op, void *host, u32 mem)
258 static struct lightrec_mem_map_ops cache_ctrl_ops = {
259 .sw = cache_ctrl_write_word,
260 .lw = cache_ctrl_read_word,
263 static struct lightrec_mem_map lightrec_map[] = {
264 [PSX_MAP_KERNEL_USER_RAM] = {
265 /* Kernel and user memory */
274 [PSX_MAP_SCRATCH_PAD] = {
279 [PSX_MAP_PARALLEL_PORT] = {
284 [PSX_MAP_HW_REGISTERS] = {
285 /* Hardware registers */
290 [PSX_MAP_CACHE_CONTROL] = {
294 .ops = &cache_ctrl_ops,
297 /* Mirrors of the kernel/user memory */
298 [PSX_MAP_MIRROR1] = {
301 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
303 [PSX_MAP_MIRROR2] = {
306 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
308 [PSX_MAP_MIRROR3] = {
311 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
313 [PSX_MAP_CODE_BUFFER] = {
314 .length = CODE_BUFFER_SIZE,
318 static void lightrec_enable_ram(struct lightrec_state *state, bool enable)
321 memcpy(psxM, cache_buf, sizeof(cache_buf));
323 memcpy(cache_buf, psxM, sizeof(cache_buf));
326 static bool lightrec_can_hw_direct(u32 kaddr, bool is_write, u8 size)
366 return kaddr < 0x1f801c00 || kaddr >= 0x1f801e00;
397 return !is_write || kaddr < 0x1f801c00 || kaddr >= 0x1f801e00;
402 static const struct lightrec_ops lightrec_ops = {
404 .enable_ram = lightrec_enable_ram,
405 .hw_direct = lightrec_can_hw_direct,
408 static int lightrec_plugin_init(void)
410 lightrec_map[PSX_MAP_KERNEL_USER_RAM].address = psxM;
411 lightrec_map[PSX_MAP_BIOS].address = psxR;
412 lightrec_map[PSX_MAP_SCRATCH_PAD].address = psxH;
413 lightrec_map[PSX_MAP_HW_REGISTERS].address = psxH + 0x1000;
414 lightrec_map[PSX_MAP_PARALLEL_PORT].address = psxP;
416 if (LIGHTREC_CUSTOM_MAP) {
417 lightrec_map[PSX_MAP_MIRROR1].address = psxM + 0x200000;
418 lightrec_map[PSX_MAP_MIRROR2].address = psxM + 0x400000;
419 lightrec_map[PSX_MAP_MIRROR3].address = psxM + 0x600000;
420 lightrec_map[PSX_MAP_CODE_BUFFER].address = code_buffer;
423 use_lightrec_interpreter = !!getenv("LIGHTREC_INTERPRETER");
425 lightrec_state = lightrec_init(name,
426 lightrec_map, ARRAY_SIZE(lightrec_map),
429 // fprintf(stderr, "M=0x%lx, P=0x%lx, R=0x%lx, H=0x%lx\n",
433 // (uintptr_t) psxH);
436 signal(SIGPIPE, exit);
441 static void lightrec_plugin_execute_internal(bool block_only)
443 struct lightrec_registers *regs;
444 u32 flags, cycles_pcsx;
446 regs = lightrec_get_registers(lightrec_state);
447 gen_interupt((psxCP0Regs *)regs->cp0);
448 cycles_pcsx = next_interupt - psxRegs.cycle;
449 assert((s32)cycles_pcsx > 0);
451 // step during early boot so that 0x80030000 fastboot hack works
452 block_stepping = block_only;
456 if (use_pcsx_interpreter) {
459 u32 cycles_lightrec = cycles_pcsx_to_lightrec(cycles_pcsx);
460 if (unlikely(use_lightrec_interpreter)) {
461 psxRegs.pc = lightrec_run_interpreter(lightrec_state,
465 psxRegs.pc = lightrec_execute(lightrec_state,
466 psxRegs.pc, cycles_lightrec);
469 lightrec_tansition_to_pcsx(lightrec_state);
471 flags = lightrec_exit_flags(lightrec_state);
473 if (flags & LIGHTREC_EXIT_SEGFAULT) {
474 fprintf(stderr, "Exiting at cycle 0x%08x\n",
479 if (flags & LIGHTREC_EXIT_SYSCALL)
480 psxException(0x20, 0, (psxCP0Regs *)regs->cp0);
483 if ((regs->cp0[13] & regs->cp0[12] & 0x300) && (regs->cp0[12] & 0x1)) {
484 /* Handle software interrupts */
485 regs->cp0[13] &= ~0x7c;
486 psxException(regs->cp0[13], 0, (psxCP0Regs *)regs->cp0);
490 static void lightrec_plugin_execute(void)
495 lightrec_plugin_execute_internal(false);
498 static void lightrec_plugin_execute_block(enum blockExecCaller caller)
500 lightrec_plugin_execute_internal(true);
503 static void lightrec_plugin_clear(u32 addr, u32 size)
505 if (addr == 0 && size == UINT32_MAX)
506 lightrec_invalidate_all(lightrec_state);
508 /* size * 4: PCSX uses DMA units */
509 lightrec_invalidate(lightrec_state, addr, size * 4);
512 static void lightrec_plugin_sync_regs_to_pcsx(void);
513 static void lightrec_plugin_sync_regs_from_pcsx(void);
515 static void lightrec_plugin_notify(enum R3000Anote note, void *data)
519 case R3000ACPU_NOTIFY_CACHE_ISOLATED:
520 case R3000ACPU_NOTIFY_CACHE_UNISOLATED:
521 /* not used, lightrec calls lightrec_enable_ram() instead */
523 case R3000ACPU_NOTIFY_BEFORE_SAVE:
524 lightrec_plugin_sync_regs_to_pcsx();
526 case R3000ACPU_NOTIFY_AFTER_LOAD:
527 lightrec_plugin_sync_regs_from_pcsx();
532 static void lightrec_plugin_apply_config()
534 u32 cycle_mult = Config.cycle_multiplier_override && Config.cycle_multiplier == CYCLE_MULT_DEFAULT
535 ? Config.cycle_multiplier_override : Config.cycle_multiplier;
537 cycle_mult_to_pcsx = (cycle_mult * 1024 + 199) / 200;
538 cycle_mult_from_pcsx = (200 * 1024 + cycle_mult/2) / cycle_mult;
541 static void lightrec_plugin_shutdown(void)
543 lightrec_destroy(lightrec_state);
546 static void lightrec_plugin_reset(void)
548 struct lightrec_registers *regs;
550 regs = lightrec_get_registers(lightrec_state);
552 /* Invalidate all blocks */
553 lightrec_invalidate_all(lightrec_state);
555 /* Reset registers */
556 memset(regs, 0, sizeof(*regs));
558 regs->cp0[12] = 0x10900000; // COP0 enabled | BEV = 1 | TS = 1
559 regs->cp0[15] = 0x00000002; // PRevID = Revision ID, same as R3000A
562 static void lightrec_plugin_sync_regs_from_pcsx(void)
564 struct lightrec_registers *regs;
566 regs = lightrec_get_registers(lightrec_state);
567 memcpy(regs->cp2d, &psxRegs.CP2, sizeof(regs->cp2d) + sizeof(regs->cp2c));
568 memcpy(regs->cp0, &psxRegs.CP0, sizeof(regs->cp0));
569 memcpy(regs->gpr, &psxRegs.GPR, sizeof(regs->gpr));
571 lightrec_invalidate_all(lightrec_state);
574 static void lightrec_plugin_sync_regs_to_pcsx(void)
576 struct lightrec_registers *regs;
578 regs = lightrec_get_registers(lightrec_state);
579 memcpy(&psxRegs.CP2, regs->cp2d, sizeof(regs->cp2d) + sizeof(regs->cp2c));
580 memcpy(&psxRegs.CP0, regs->cp0, sizeof(regs->cp0));
581 memcpy(&psxRegs.GPR, regs->gpr, sizeof(regs->gpr));
586 lightrec_plugin_init,
587 lightrec_plugin_reset,
588 lightrec_plugin_execute,
589 lightrec_plugin_execute_block,
590 lightrec_plugin_clear,
591 lightrec_plugin_notify,
592 lightrec_plugin_apply_config,
593 lightrec_plugin_shutdown,