17 #include "../psxdma.h"
19 #include "../psxmem.h"
20 #include "../r3000a.h"
21 #include "../psxinterpreter.h"
22 #include "../psxhle.h"
23 #include "../psxevents.h"
25 #include "../frontend/main.h"
30 #if (defined(__arm__) || defined(__aarch64__)) && !defined(ALLOW_LIGHTREC_ON_ARM)
31 #error "Lightrec should not be used on ARM (please specify DYNAREC=ari64 to make)"
34 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
36 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
37 # define LE32TOH(x) __builtin_bswap32(x)
38 # define HTOLE32(x) __builtin_bswap32(x)
39 # define LE16TOH(x) __builtin_bswap16(x)
40 # define HTOLE16(x) __builtin_bswap16(x)
42 # define LE32TOH(x) (x)
43 # define HTOLE32(x) (x)
44 # define LE16TOH(x) (x)
45 # define HTOLE16(x) (x)
49 # define likely(x) __builtin_expect(!!(x),1)
50 # define unlikely(x) __builtin_expect(!!(x),0)
52 # define likely(x) (x)
53 # define unlikely(x) (x)
56 #ifndef LIGHTREC_PROG_NAME
58 # define LIGHTREC_PROG_NAME "/proc/self/exe"
60 # define LIGHTREC_PROG_NAME "retroarch.exe"
69 static struct lightrec_state *lightrec_state;
71 static bool use_lightrec_interpreter;
72 static bool use_pcsx_interpreter;
73 static bool block_stepping;
75 extern u32 lightrec_hacks;
77 extern void lightrec_code_inv(void *ptr, uint32_t len);
104 static void (*cp2_ops[])(struct psxCP2Regs *) = {
105 [OP_CP2_RTPS] = gteRTPS,
106 [OP_CP2_RTPS] = gteRTPS,
107 [OP_CP2_NCLIP] = gteNCLIP,
109 [OP_CP2_DPCS] = gteDPCS,
110 [OP_CP2_INTPL] = gteINTPL,
111 [OP_CP2_MVMVA] = gteMVMVA,
112 [OP_CP2_NCDS] = gteNCDS,
113 [OP_CP2_CDP] = gteCDP,
114 [OP_CP2_NCDT] = gteNCDT,
115 [OP_CP2_NCCS] = gteNCCS,
117 [OP_CP2_NCS] = gteNCS,
118 [OP_CP2_NCT] = gteNCT,
119 [OP_CP2_SQR] = gteSQR,
120 [OP_CP2_DCPL] = gteDCPL,
121 [OP_CP2_DPCT] = gteDPCT,
122 [OP_CP2_AVSZ3] = gteAVSZ3,
123 [OP_CP2_AVSZ4] = gteAVSZ4,
124 [OP_CP2_RTPT] = gteRTPT,
125 [OP_CP2_GPF] = gteGPF,
126 [OP_CP2_GPL] = gteGPL,
127 [OP_CP2_NCCT] = gteNCCT,
130 static char cache_buf[64 * 1024];
132 static void cop2_op(struct lightrec_state *state, u32 func)
134 struct lightrec_registers *regs = lightrec_get_registers(state);
138 if (unlikely(!cp2_ops[func & 0x3f])) {
139 fprintf(stderr, "Invalid CP2 function %u\n", func);
141 /* This works because regs->cp2c comes right after regs->cp2d,
142 * so it can be cast to a pcsxCP2Regs pointer. */
143 cp2_ops[func & 0x3f]((psxCP2Regs *) regs->cp2d);
147 static bool has_interrupt(void)
149 struct lightrec_registers *regs = lightrec_get_registers(lightrec_state);
151 return ((psxHu32(0x1070) & psxHu32(0x1074)) &&
152 (regs->cp0[12] & 0x401) == 0x401) ||
153 (regs->cp0[12] & regs->cp0[13] & 0x0300);
156 static void lightrec_tansition_to_pcsx(struct lightrec_state *state)
158 psxRegs.cycle += lightrec_current_cycle_count(state) / 1024;
159 lightrec_reset_cycle_count(state, 0);
162 static void lightrec_tansition_from_pcsx(struct lightrec_state *state)
164 s32 cycles_left = next_interupt - psxRegs.cycle;
166 if (block_stepping || cycles_left <= 0 || has_interrupt())
167 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
169 lightrec_set_target_cycle_count(state, cycles_left * 1024);
173 static void hw_write_byte(struct lightrec_state *state,
174 u32 op, void *host, u32 mem, u32 val)
176 lightrec_tansition_to_pcsx(state);
178 psxHwWrite8(mem, val);
180 lightrec_tansition_from_pcsx(state);
183 static void hw_write_half(struct lightrec_state *state,
184 u32 op, void *host, u32 mem, u32 val)
186 lightrec_tansition_to_pcsx(state);
188 psxHwWrite16(mem, val);
190 lightrec_tansition_from_pcsx(state);
193 static void hw_write_word(struct lightrec_state *state,
194 u32 op, void *host, u32 mem, u32 val)
196 lightrec_tansition_to_pcsx(state);
198 psxHwWrite32(mem, val);
200 lightrec_tansition_from_pcsx(state);
203 static u8 hw_read_byte(struct lightrec_state *state, u32 op, void *host, u32 mem)
207 lightrec_tansition_to_pcsx(state);
209 val = psxHwRead8(mem);
211 lightrec_tansition_from_pcsx(state);
216 static u16 hw_read_half(struct lightrec_state *state,
217 u32 op, void *host, u32 mem)
221 lightrec_tansition_to_pcsx(state);
223 val = psxHwRead16(mem);
225 lightrec_tansition_from_pcsx(state);
230 static u32 hw_read_word(struct lightrec_state *state,
231 u32 op, void *host, u32 mem)
235 lightrec_tansition_to_pcsx(state);
237 val = psxHwRead32(mem);
239 lightrec_tansition_from_pcsx(state);
244 static struct lightrec_mem_map_ops hw_regs_ops = {
253 static u32 cache_ctrl;
255 static void cache_ctrl_write_word(struct lightrec_state *state,
256 u32 op, void *host, u32 mem, u32 val)
261 static u32 cache_ctrl_read_word(struct lightrec_state *state,
262 u32 op, void *host, u32 mem)
267 static struct lightrec_mem_map_ops cache_ctrl_ops = {
268 .sw = cache_ctrl_write_word,
269 .lw = cache_ctrl_read_word,
272 static struct lightrec_mem_map lightrec_map[] = {
273 [PSX_MAP_KERNEL_USER_RAM] = {
274 /* Kernel and user memory */
283 [PSX_MAP_SCRATCH_PAD] = {
288 [PSX_MAP_PARALLEL_PORT] = {
293 [PSX_MAP_HW_REGISTERS] = {
294 /* Hardware registers */
299 [PSX_MAP_CACHE_CONTROL] = {
303 .ops = &cache_ctrl_ops,
306 /* Mirrors of the kernel/user memory */
307 [PSX_MAP_MIRROR1] = {
310 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
312 [PSX_MAP_MIRROR2] = {
315 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
317 [PSX_MAP_MIRROR3] = {
320 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
323 /* Mirror of the parallel port. Only used by the PS2/PS3 BIOS */
324 [PSX_MAP_PPORT_MIRROR] = {
327 .mirror_of = &lightrec_map[PSX_MAP_PARALLEL_PORT],
331 [PSX_MAP_CODE_BUFFER] = {
332 .length = CODE_BUFFER_SIZE,
336 static void lightrec_enable_ram(struct lightrec_state *state, bool enable)
339 memcpy(psxM, cache_buf, sizeof(cache_buf));
341 memcpy(cache_buf, psxM, sizeof(cache_buf));
344 static bool lightrec_can_hw_direct(u32 kaddr, bool is_write, u8 size)
346 if (is_write && size != 32) {
347 // force32 so must go through handlers
348 if (0x1f801000 <= kaddr && kaddr < 0x1f801024)
350 if ((kaddr & 0x1fffff80) == 0x1f801080) // dma
392 return kaddr < 0x1f801c00 || kaddr >= 0x1f801e00;
423 return !is_write || kaddr < 0x1f801c00 || kaddr >= 0x1f801e00;
428 static const struct lightrec_ops lightrec_ops = {
430 .enable_ram = lightrec_enable_ram,
431 .hw_direct = lightrec_can_hw_direct,
432 .code_inv = LIGHTREC_CODE_INV ? lightrec_code_inv : NULL,
435 static int lightrec_plugin_init(void)
437 lightrec_map[PSX_MAP_KERNEL_USER_RAM].address = psxM;
438 lightrec_map[PSX_MAP_BIOS].address = psxR;
439 lightrec_map[PSX_MAP_SCRATCH_PAD].address = psxH;
440 lightrec_map[PSX_MAP_HW_REGISTERS].address = psxH + 0x1000;
441 lightrec_map[PSX_MAP_PARALLEL_PORT].address = psxP;
443 if (!LIGHTREC_CUSTOM_MAP) {
445 code_buffer = mmap(0, CODE_BUFFER_SIZE,
446 PROT_EXEC | PROT_READ | PROT_WRITE,
447 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
448 if (code_buffer == MAP_FAILED)
451 code_buffer = malloc(CODE_BUFFER_SIZE);
457 if (LIGHTREC_CUSTOM_MAP) {
458 lightrec_map[PSX_MAP_MIRROR1].address = psxM + 0x200000;
459 lightrec_map[PSX_MAP_MIRROR2].address = psxM + 0x400000;
460 lightrec_map[PSX_MAP_MIRROR3].address = psxM + 0x600000;
463 lightrec_map[PSX_MAP_CODE_BUFFER].address = code_buffer;
465 use_lightrec_interpreter = !!getenv("LIGHTREC_INTERPRETER");
467 lightrec_state = lightrec_init(LIGHTREC_PROG_NAME,
468 lightrec_map, ARRAY_SIZE(lightrec_map),
471 // fprintf(stderr, "M=0x%lx, P=0x%lx, R=0x%lx, H=0x%lx\n",
475 // (uintptr_t) psxH);
478 signal(SIGPIPE, exit);
483 static void lightrec_plugin_sync_regs_to_pcsx(bool need_cp2);
484 static void lightrec_plugin_sync_regs_from_pcsx(bool need_cp2);
486 static void lightrec_plugin_execute_internal(bool block_only)
488 struct lightrec_registers *regs;
489 u32 flags, cycles_pcsx;
491 regs = lightrec_get_registers(lightrec_state);
492 gen_interupt((psxCP0Regs *)regs->cp0);
493 if (!block_only && stop)
496 cycles_pcsx = next_interupt - psxRegs.cycle;
497 assert((s32)cycles_pcsx > 0);
499 // step during early boot so that 0x80030000 fastboot hack works
500 block_stepping = block_only;
504 if (use_pcsx_interpreter) {
507 u32 cycles_lightrec = cycles_pcsx * 1024;
508 if (unlikely(use_lightrec_interpreter)) {
509 psxRegs.pc = lightrec_run_interpreter(lightrec_state,
513 psxRegs.pc = lightrec_execute(lightrec_state,
514 psxRegs.pc, cycles_lightrec);
517 lightrec_tansition_to_pcsx(lightrec_state);
519 flags = lightrec_exit_flags(lightrec_state);
521 if (flags & LIGHTREC_EXIT_SEGFAULT) {
522 fprintf(stderr, "Exiting at cycle 0x%08x\n",
527 if (flags & LIGHTREC_EXIT_SYSCALL)
528 psxException(R3000E_Syscall << 2, 0, (psxCP0Regs *)regs->cp0);
529 if (flags & LIGHTREC_EXIT_BREAK)
530 psxException(R3000E_Bp << 2, 0, (psxCP0Regs *)regs->cp0);
531 else if (flags & LIGHTREC_EXIT_UNKNOWN_OP) {
532 u32 op = intFakeFetch(psxRegs.pc);
533 u32 hlec = op & 0x03ffffff;
534 if ((op >> 26) == 0x3b && hlec < ARRAY_SIZE(psxHLEt) && Config.HLE) {
535 lightrec_plugin_sync_regs_to_pcsx(0);
537 lightrec_plugin_sync_regs_from_pcsx(0);
540 psxException(R3000E_RI << 2, 0, (psxCP0Regs *)regs->cp0);
544 if ((regs->cp0[13] & regs->cp0[12] & 0x300) && (regs->cp0[12] & 0x1)) {
545 /* Handle software interrupts */
546 regs->cp0[13] &= ~0x7c;
547 psxException(regs->cp0[13], 0, (psxCP0Regs *)regs->cp0);
551 static void lightrec_plugin_execute(void)
554 lightrec_plugin_execute_internal(false);
557 static void lightrec_plugin_execute_block(enum blockExecCaller caller)
559 lightrec_plugin_execute_internal(true);
562 static void lightrec_plugin_clear(u32 addr, u32 size)
564 if ((addr == 0 && size == UINT32_MAX)
565 || (lightrec_hacks & LIGHTREC_OPT_INV_DMA_ONLY))
566 lightrec_invalidate_all(lightrec_state);
568 /* size * 4: PCSX uses DMA units */
569 lightrec_invalidate(lightrec_state, addr, size * 4);
572 static void lightrec_plugin_notify(enum R3000Anote note, void *data)
576 case R3000ACPU_NOTIFY_CACHE_ISOLATED:
577 case R3000ACPU_NOTIFY_CACHE_UNISOLATED:
578 /* not used, lightrec calls lightrec_enable_ram() instead */
580 case R3000ACPU_NOTIFY_BEFORE_SAVE:
581 /* non-null 'data' means this is HLE related sync */
582 lightrec_plugin_sync_regs_to_pcsx(data == NULL);
584 case R3000ACPU_NOTIFY_AFTER_LOAD:
585 lightrec_plugin_sync_regs_from_pcsx(data == NULL);
587 lightrec_invalidate_all(lightrec_state);
592 static void lightrec_plugin_apply_config()
594 static u32 cycles_per_op_old;
595 u32 cycle_mult = Config.cycle_multiplier_override && Config.cycle_multiplier == CYCLE_MULT_DEFAULT
596 ? Config.cycle_multiplier_override : Config.cycle_multiplier;
597 u32 cycles_per_op = cycle_mult * 1024 / 100;
598 assert(cycles_per_op);
600 if (cycles_per_op_old && cycles_per_op_old != cycles_per_op) {
601 SysPrintf("lightrec: reinit block cache for cycles_per_op %.2f\n",
602 cycles_per_op / 1024.f);
604 cycles_per_op_old = cycles_per_op;
605 lightrec_set_cycles_per_opcode(lightrec_state, cycles_per_op);
608 static void lightrec_plugin_shutdown(void)
610 lightrec_destroy(lightrec_state);
612 if (!LIGHTREC_CUSTOM_MAP) {
614 munmap(code_buffer, CODE_BUFFER_SIZE);
621 static void lightrec_plugin_reset(void)
623 struct lightrec_registers *regs;
625 regs = lightrec_get_registers(lightrec_state);
627 /* Invalidate all blocks */
628 lightrec_invalidate_all(lightrec_state);
630 /* Reset registers */
631 memset(regs, 0, sizeof(*regs));
633 regs->cp0[12] = 0x10900000; // COP0 enabled | BEV = 1 | TS = 1
634 regs->cp0[15] = 0x00000002; // PRevID = Revision ID, same as R3000A
636 lightrec_set_unsafe_opt_flags(lightrec_state, lightrec_hacks);
639 static void lightrec_plugin_sync_regs_from_pcsx(bool need_cp2)
641 struct lightrec_registers *regs;
643 regs = lightrec_get_registers(lightrec_state);
644 memcpy(regs->gpr, &psxRegs.GPR, sizeof(regs->gpr));
645 memcpy(regs->cp0, &psxRegs.CP0, sizeof(regs->cp0));
647 memcpy(regs->cp2d, &psxRegs.CP2, sizeof(regs->cp2d) + sizeof(regs->cp2c));
650 static void lightrec_plugin_sync_regs_to_pcsx(bool need_cp2)
652 struct lightrec_registers *regs;
654 regs = lightrec_get_registers(lightrec_state);
655 memcpy(&psxRegs.GPR, regs->gpr, sizeof(regs->gpr));
656 memcpy(&psxRegs.CP0, regs->cp0, sizeof(regs->cp0));
658 memcpy(&psxRegs.CP2, regs->cp2d, sizeof(regs->cp2d) + sizeof(regs->cp2c));
663 lightrec_plugin_init,
664 lightrec_plugin_reset,
665 lightrec_plugin_execute,
666 lightrec_plugin_execute_block,
667 lightrec_plugin_clear,
668 lightrec_plugin_notify,
669 lightrec_plugin_apply_config,
670 lightrec_plugin_shutdown,