git subrepo pull --force deps/lightrec
[pcsx_rearmed.git] / deps / lightrec / lightrec.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2016-2021 Paul Cercueil <paul@crapouillou.net>
4  */
5
6 #ifndef __LIGHTREC_H__
7 #define __LIGHTREC_H__
8
9 #ifdef __cplusplus
10 #define _Bool bool
11 extern "C" {
12 #endif
13
14 #include <stddef.h>
15 #include <stdint.h>
16
17 #ifdef _WIN32
18 #   ifdef lightrec_EXPORTS
19 #       define __api __declspec(dllexport)
20 #   elif !defined(LIGHTREC_STATIC)
21 #       define __api __declspec(dllimport)
22 #   else
23 #       define __api
24 #   endif
25 #elif __GNUC__ >= 4
26 #   define __api __attribute__((visibility ("default")))
27 #else
28 #   define __api
29 #endif
30
31 typedef uint64_t u64;
32 typedef uint32_t u32;
33 typedef uint16_t u16;
34 typedef uint8_t  u8;
35
36 typedef int64_t s64;
37 typedef int32_t s32;
38 typedef int16_t s16;
39 typedef int8_t  s8;
40
41 struct lightrec_state;
42 struct lightrec_mem_map;
43
44 /* Exit flags */
45 #define LIGHTREC_EXIT_NORMAL    (0)
46 #define LIGHTREC_EXIT_CHECK_INTERRUPT   (1 << 0)
47 #define LIGHTREC_EXIT_BREAK     (1 << 1)
48 #define LIGHTREC_EXIT_SYSCALL   (1 << 2)
49 #define LIGHTREC_EXIT_SEGFAULT  (1 << 3)
50
51 enum psx_map {
52         PSX_MAP_KERNEL_USER_RAM,
53         PSX_MAP_BIOS,
54         PSX_MAP_SCRATCH_PAD,
55         PSX_MAP_PARALLEL_PORT,
56         PSX_MAP_HW_REGISTERS,
57         PSX_MAP_CACHE_CONTROL,
58         PSX_MAP_MIRROR1,
59         PSX_MAP_MIRROR2,
60         PSX_MAP_MIRROR3,
61         PSX_MAP_CODE_BUFFER,
62
63         PSX_MAP_UNKNOWN,
64 };
65
66 struct lightrec_mem_map_ops {
67         void (*sb)(struct lightrec_state *, u32 opcode,
68                    void *host, u32 addr, u8 data);
69         void (*sh)(struct lightrec_state *, u32 opcode,
70                    void *host, u32 addr, u16 data);
71         void (*sw)(struct lightrec_state *, u32 opcode,
72                    void *host, u32 addr, u32 data);
73         u8 (*lb)(struct lightrec_state *, u32 opcode, void *host, u32 addr);
74         u16 (*lh)(struct lightrec_state *, u32 opcode, void *host, u32 addr);
75         u32 (*lw)(struct lightrec_state *, u32 opcode, void *host, u32 addr);
76 };
77
78 struct lightrec_mem_map {
79         u32 pc;
80         u32 length;
81         void *address;
82         const struct lightrec_mem_map_ops *ops;
83         const struct lightrec_mem_map *mirror_of;
84 };
85
86 struct lightrec_ops {
87         void (*cop2_op)(struct lightrec_state *state, u32 op);
88         void (*enable_ram)(struct lightrec_state *state, _Bool enable);
89 };
90
91 struct lightrec_registers {
92         u32 gpr[34];
93         u32 cp0[32];
94         u32 cp2d[32];
95         u32 cp2c[32];
96 };
97
98 __api struct lightrec_state *lightrec_init(char *argv0,
99                                            const struct lightrec_mem_map *map,
100                                            size_t nb,
101                                            const struct lightrec_ops *ops);
102
103 __api void lightrec_destroy(struct lightrec_state *state);
104
105 __api u32 lightrec_execute(struct lightrec_state *state,
106                            u32 pc, u32 target_cycle);
107 __api u32 lightrec_execute_one(struct lightrec_state *state, u32 pc);
108 __api u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc);
109
110 __api void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len);
111 __api void lightrec_invalidate_all(struct lightrec_state *state);
112 __api void lightrec_set_invalidate_mode(struct lightrec_state *state,
113                                         _Bool dma_only);
114
115 __api void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags);
116 __api u32 lightrec_exit_flags(struct lightrec_state *state);
117
118 __api struct lightrec_registers * lightrec_get_registers(struct lightrec_state *state);
119
120 __api u32 lightrec_current_cycle_count(const struct lightrec_state *state);
121 __api void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles);
122 __api void lightrec_set_target_cycle_count(struct lightrec_state *state,
123                                            u32 cycles);
124
125 #ifdef __cplusplus
126 };
127 #endif
128
129 #endif /* __LIGHTREC_H__ */