11 #include "../psxdma.h"
13 #include "../psxmem.h"
14 #include "../r3000a.h"
15 #include "../psxinterpreter.h"
16 #include "../new_dynarec/events.h"
18 #include "../frontend/main.h"
23 #if (defined(__arm__) || defined(__aarch64__)) && !defined(ALLOW_LIGHTREC_ON_ARM)
24 #error "Lightrec should not be used on ARM (please specify DYNAREC=ari64 to make)"
27 #define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
29 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
30 # define LE32TOH(x) __builtin_bswap32(x)
31 # define HTOLE32(x) __builtin_bswap32(x)
32 # define LE16TOH(x) __builtin_bswap16(x)
33 # define HTOLE16(x) __builtin_bswap16(x)
35 # define LE32TOH(x) (x)
36 # define HTOLE32(x) (x)
37 # define LE16TOH(x) (x)
38 # define HTOLE16(x) (x)
42 # define likely(x) __builtin_expect(!!(x),1)
43 # define unlikely(x) __builtin_expect(!!(x),0)
45 # define likely(x) (x)
46 # define unlikely(x) (x)
52 static struct lightrec_state *lightrec_state;
54 static char *name = "retroarch.exe";
56 static bool use_lightrec_interpreter;
57 static bool use_pcsx_interpreter;
58 static bool block_stepping;
85 static void (*cp2_ops[])(struct psxCP2Regs *) = {
86 [OP_CP2_RTPS] = gteRTPS,
87 [OP_CP2_RTPS] = gteRTPS,
88 [OP_CP2_NCLIP] = gteNCLIP,
90 [OP_CP2_DPCS] = gteDPCS,
91 [OP_CP2_INTPL] = gteINTPL,
92 [OP_CP2_MVMVA] = gteMVMVA,
93 [OP_CP2_NCDS] = gteNCDS,
94 [OP_CP2_CDP] = gteCDP,
95 [OP_CP2_NCDT] = gteNCDT,
96 [OP_CP2_NCCS] = gteNCCS,
98 [OP_CP2_NCS] = gteNCS,
99 [OP_CP2_NCT] = gteNCT,
100 [OP_CP2_SQR] = gteSQR,
101 [OP_CP2_DCPL] = gteDCPL,
102 [OP_CP2_DPCT] = gteDPCT,
103 [OP_CP2_AVSZ3] = gteAVSZ3,
104 [OP_CP2_AVSZ4] = gteAVSZ4,
105 [OP_CP2_RTPT] = gteRTPT,
106 [OP_CP2_GPF] = gteGPF,
107 [OP_CP2_GPL] = gteGPL,
108 [OP_CP2_NCCT] = gteNCCT,
111 static char cache_buf[64 * 1024];
113 static void cop2_op(struct lightrec_state *state, u32 func)
115 struct lightrec_registers *regs = lightrec_get_registers(state);
119 if (unlikely(!cp2_ops[func & 0x3f])) {
120 fprintf(stderr, "Invalid CP2 function %u\n", func);
122 /* This works because regs->cp2c comes right after regs->cp2d,
123 * so it can be cast to a pcsxCP2Regs pointer. */
124 cp2_ops[func & 0x3f]((psxCP2Regs *) regs->cp2d);
128 static bool has_interrupt(void)
130 struct lightrec_registers *regs = lightrec_get_registers(lightrec_state);
132 return ((psxHu32(0x1070) & psxHu32(0x1074)) &&
133 (regs->cp0[12] & 0x401) == 0x401) ||
134 (regs->cp0[12] & regs->cp0[13] & 0x0300);
137 static void lightrec_restore_state(struct lightrec_state *state)
139 lightrec_reset_cycle_count(state, psxRegs.cycle);
141 if (block_stepping || has_interrupt())
142 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
144 lightrec_set_target_cycle_count(state, next_interupt);
147 static void hw_write_byte(struct lightrec_state *state,
148 u32 op, void *host, u32 mem, u8 val)
150 psxRegs.cycle = lightrec_current_cycle_count(state);
152 psxHwWrite8(mem, val);
154 lightrec_restore_state(state);
157 static void hw_write_half(struct lightrec_state *state,
158 u32 op, void *host, u32 mem, u16 val)
160 psxRegs.cycle = lightrec_current_cycle_count(state);
162 psxHwWrite16(mem, val);
164 lightrec_restore_state(state);
167 static void hw_write_word(struct lightrec_state *state,
168 u32 op, void *host, u32 mem, u32 val)
170 psxRegs.cycle = lightrec_current_cycle_count(state);
172 psxHwWrite32(mem, val);
174 lightrec_restore_state(state);
177 static u8 hw_read_byte(struct lightrec_state *state, u32 op, void *host, u32 mem)
181 psxRegs.cycle = lightrec_current_cycle_count(state);
183 val = psxHwRead8(mem);
185 lightrec_restore_state(state);
190 static u16 hw_read_half(struct lightrec_state *state,
191 u32 op, void *host, u32 mem)
195 psxRegs.cycle = lightrec_current_cycle_count(state);
197 val = psxHwRead16(mem);
199 lightrec_restore_state(state);
204 static u32 hw_read_word(struct lightrec_state *state,
205 u32 op, void *host, u32 mem)
209 psxRegs.cycle = lightrec_current_cycle_count(state);
211 val = psxHwRead32(mem);
213 lightrec_restore_state(state);
218 static struct lightrec_mem_map_ops hw_regs_ops = {
227 static u32 cache_ctrl;
229 static void cache_ctrl_write_word(struct lightrec_state *state,
230 u32 op, void *host, u32 mem, u32 val)
235 static u32 cache_ctrl_read_word(struct lightrec_state *state,
236 u32 op, void *host, u32 mem)
241 static struct lightrec_mem_map_ops cache_ctrl_ops = {
242 .sw = cache_ctrl_write_word,
243 .lw = cache_ctrl_read_word,
246 static struct lightrec_mem_map lightrec_map[] = {
247 [PSX_MAP_KERNEL_USER_RAM] = {
248 /* Kernel and user memory */
257 [PSX_MAP_SCRATCH_PAD] = {
262 [PSX_MAP_PARALLEL_PORT] = {
267 [PSX_MAP_HW_REGISTERS] = {
268 /* Hardware registers */
273 [PSX_MAP_CACHE_CONTROL] = {
277 .ops = &cache_ctrl_ops,
280 /* Mirrors of the kernel/user memory */
281 [PSX_MAP_MIRROR1] = {
284 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
286 [PSX_MAP_MIRROR2] = {
289 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
291 [PSX_MAP_MIRROR3] = {
294 .mirror_of = &lightrec_map[PSX_MAP_KERNEL_USER_RAM],
296 [PSX_MAP_CODE_BUFFER] = {
297 .length = CODE_BUFFER_SIZE,
301 static void lightrec_enable_ram(struct lightrec_state *state, bool enable)
304 memcpy(psxM, cache_buf, sizeof(cache_buf));
306 memcpy(cache_buf, psxM, sizeof(cache_buf));
309 static bool lightrec_can_hw_direct(u32 kaddr, bool is_write, u8 size)
349 return kaddr < 0x1f801c00 || kaddr >= 0x1f801e00;
380 return !is_write || kaddr < 0x1f801c00 || kaddr >= 0x1f801e00;
385 static const struct lightrec_ops lightrec_ops = {
387 .enable_ram = lightrec_enable_ram,
388 .hw_direct = lightrec_can_hw_direct,
391 static int lightrec_plugin_init(void)
393 lightrec_map[PSX_MAP_KERNEL_USER_RAM].address = psxM;
394 lightrec_map[PSX_MAP_BIOS].address = psxR;
395 lightrec_map[PSX_MAP_SCRATCH_PAD].address = psxH;
396 lightrec_map[PSX_MAP_HW_REGISTERS].address = psxH + 0x1000;
397 lightrec_map[PSX_MAP_PARALLEL_PORT].address = psxP;
399 if (LIGHTREC_CUSTOM_MAP) {
400 lightrec_map[PSX_MAP_MIRROR1].address = psxM + 0x200000;
401 lightrec_map[PSX_MAP_MIRROR2].address = psxM + 0x400000;
402 lightrec_map[PSX_MAP_MIRROR3].address = psxM + 0x600000;
403 lightrec_map[PSX_MAP_CODE_BUFFER].address = code_buffer;
406 use_lightrec_interpreter = !!getenv("LIGHTREC_INTERPRETER");
408 lightrec_state = lightrec_init(name,
409 lightrec_map, ARRAY_SIZE(lightrec_map),
412 // fprintf(stderr, "M=0x%lx, P=0x%lx, R=0x%lx, H=0x%lx\n",
416 // (uintptr_t) psxH);
419 signal(SIGPIPE, exit);
424 static void lightrec_plugin_execute_internal(bool block_only)
426 struct lightrec_registers *regs;
429 regs = lightrec_get_registers(lightrec_state);
430 gen_interupt((psxCP0Regs *)regs->cp0);
432 // step during early boot so that 0x80030000 fastboot hack works
433 block_stepping = block_only;
435 next_interupt = psxRegs.cycle;
437 if (use_pcsx_interpreter) {
440 lightrec_reset_cycle_count(lightrec_state, psxRegs.cycle);
442 if (unlikely(use_lightrec_interpreter)) {
443 psxRegs.pc = lightrec_run_interpreter(lightrec_state,
447 psxRegs.pc = lightrec_execute(lightrec_state,
448 psxRegs.pc, next_interupt);
451 psxRegs.cycle = lightrec_current_cycle_count(lightrec_state);
453 flags = lightrec_exit_flags(lightrec_state);
455 if (flags & LIGHTREC_EXIT_SEGFAULT) {
456 fprintf(stderr, "Exiting at cycle 0x%08x\n",
461 if (flags & LIGHTREC_EXIT_SYSCALL)
462 psxException(0x20, 0, (psxCP0Regs *)regs->cp0);
465 if ((regs->cp0[13] & regs->cp0[12] & 0x300) && (regs->cp0[12] & 0x1)) {
466 /* Handle software interrupts */
467 regs->cp0[13] &= ~0x7c;
468 psxException(regs->cp0[13], 0, (psxCP0Regs *)regs->cp0);
472 static void lightrec_plugin_execute(void)
477 lightrec_plugin_execute_internal(false);
480 static void lightrec_plugin_execute_block(enum blockExecCaller caller)
482 lightrec_plugin_execute_internal(true);
485 static void lightrec_plugin_clear(u32 addr, u32 size)
487 if (addr == 0 && size == UINT32_MAX)
488 lightrec_invalidate_all(lightrec_state);
490 /* size * 4: PCSX uses DMA units */
491 lightrec_invalidate(lightrec_state, addr, size * 4);
494 static void lightrec_plugin_sync_regs_to_pcsx(void);
495 static void lightrec_plugin_sync_regs_from_pcsx(void);
497 static void lightrec_plugin_notify(enum R3000Anote note, void *data)
501 case R3000ACPU_NOTIFY_CACHE_ISOLATED:
502 case R3000ACPU_NOTIFY_CACHE_UNISOLATED:
503 /* not used, lightrec calls lightrec_enable_ram() instead */
505 case R3000ACPU_NOTIFY_BEFORE_SAVE:
506 lightrec_plugin_sync_regs_to_pcsx();
508 case R3000ACPU_NOTIFY_AFTER_LOAD:
509 lightrec_plugin_sync_regs_from_pcsx();
514 static void lightrec_plugin_apply_config()
518 static void lightrec_plugin_shutdown(void)
520 lightrec_destroy(lightrec_state);
523 static void lightrec_plugin_reset(void)
525 struct lightrec_registers *regs;
527 regs = lightrec_get_registers(lightrec_state);
529 /* Invalidate all blocks */
530 lightrec_invalidate_all(lightrec_state);
532 /* Reset registers */
533 memset(regs, 0, sizeof(*regs));
535 regs->cp0[12] = 0x10900000; // COP0 enabled | BEV = 1 | TS = 1
536 regs->cp0[15] = 0x00000002; // PRevID = Revision ID, same as R3000A
539 static void lightrec_plugin_sync_regs_from_pcsx(void)
541 struct lightrec_registers *regs;
543 regs = lightrec_get_registers(lightrec_state);
544 memcpy(regs->cp2d, &psxRegs.CP2, sizeof(regs->cp2d) + sizeof(regs->cp2c));
545 memcpy(regs->cp0, &psxRegs.CP0, sizeof(regs->cp0));
546 memcpy(regs->gpr, &psxRegs.GPR, sizeof(regs->gpr));
548 lightrec_invalidate_all(lightrec_state);
551 static void lightrec_plugin_sync_regs_to_pcsx(void)
553 struct lightrec_registers *regs;
555 regs = lightrec_get_registers(lightrec_state);
556 memcpy(&psxRegs.CP2, regs->cp2d, sizeof(regs->cp2d) + sizeof(regs->cp2c));
557 memcpy(&psxRegs.CP0, regs->cp0, sizeof(regs->cp0));
558 memcpy(&psxRegs.GPR, regs->gpr, sizeof(regs->gpr));
563 lightrec_plugin_init,
564 lightrec_plugin_reset,
565 lightrec_plugin_execute,
566 lightrec_plugin_execute_block,
567 lightrec_plugin_clear,
568 lightrec_plugin_notify,
569 lightrec_plugin_apply_config,
570 lightrec_plugin_shutdown,