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 #ifndef __cnst
32 #   ifdef __GNUC__
33 #       define __cnst __attribute__((const))
34 #   else
35 #       define __cnst
36 #   endif
37 #endif
38 #ifndef __pure
39 #   ifdef __GNUC__
40 #       define __pure __attribute__((pure))
41 #   else
42 #       define __pure
43 #   endif
44 #endif
45
46 typedef uint64_t u64;
47 typedef uint32_t u32;
48 typedef uint16_t u16;
49 typedef uint8_t  u8;
50
51 typedef int64_t s64;
52 typedef int32_t s32;
53 typedef int16_t s16;
54 typedef int8_t  s8;
55
56 struct lightrec_state;
57 struct lightrec_mem_map;
58
59 /* Exit flags */
60 #define LIGHTREC_EXIT_NORMAL    (0)
61 #define LIGHTREC_EXIT_CHECK_INTERRUPT   (1 << 0)
62 #define LIGHTREC_EXIT_BREAK     (1 << 1)
63 #define LIGHTREC_EXIT_SYSCALL   (1 << 2)
64 #define LIGHTREC_EXIT_SEGFAULT  (1 << 3)
65 #define LIGHTREC_EXIT_NOMEM     (1 << 4)
66
67 enum psx_map {
68         PSX_MAP_KERNEL_USER_RAM,
69         PSX_MAP_BIOS,
70         PSX_MAP_SCRATCH_PAD,
71         PSX_MAP_PARALLEL_PORT,
72         PSX_MAP_HW_REGISTERS,
73         PSX_MAP_CACHE_CONTROL,
74         PSX_MAP_MIRROR1,
75         PSX_MAP_MIRROR2,
76         PSX_MAP_MIRROR3,
77         PSX_MAP_CODE_BUFFER,
78
79         PSX_MAP_UNKNOWN,
80 };
81
82 struct lightrec_mem_map_ops {
83         void (*sb)(struct lightrec_state *, u32 opcode,
84                    void *host, u32 addr, u8 data);
85         void (*sh)(struct lightrec_state *, u32 opcode,
86                    void *host, u32 addr, u16 data);
87         void (*sw)(struct lightrec_state *, u32 opcode,
88                    void *host, u32 addr, u32 data);
89         u8 (*lb)(struct lightrec_state *, u32 opcode, void *host, u32 addr);
90         u16 (*lh)(struct lightrec_state *, u32 opcode, void *host, u32 addr);
91         u32 (*lw)(struct lightrec_state *, u32 opcode, void *host, u32 addr);
92 };
93
94 struct lightrec_mem_map {
95         u32 pc;
96         u32 length;
97         void *address;
98         const struct lightrec_mem_map_ops *ops;
99         const struct lightrec_mem_map *mirror_of;
100 };
101
102 struct lightrec_ops {
103         void (*cop2_notify)(struct lightrec_state *state, u32 op, u32 data);
104         void (*cop2_op)(struct lightrec_state *state, u32 op);
105         void (*enable_ram)(struct lightrec_state *state, _Bool enable);
106         _Bool (*hw_direct)(u32 kaddr, _Bool is_write, u8 size);
107         void (*code_inv)(void *addr, u32 len);
108 };
109
110 struct lightrec_registers {
111         u32 gpr[34];
112         u32 cp0[32];
113         u32 cp2d[32];
114         u32 cp2c[32];
115 };
116
117 __api struct lightrec_state *lightrec_init(char *argv0,
118                                            const struct lightrec_mem_map *map,
119                                            size_t nb,
120                                            const struct lightrec_ops *ops);
121
122 __api void lightrec_destroy(struct lightrec_state *state);
123
124 __api u32 lightrec_execute(struct lightrec_state *state,
125                            u32 pc, u32 target_cycle);
126 __api u32 lightrec_run_interpreter(struct lightrec_state *state,
127                                    u32 pc, u32 target_cycle);
128
129 __api void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len);
130 __api void lightrec_invalidate_all(struct lightrec_state *state);
131 __api void lightrec_set_invalidate_mode(struct lightrec_state *state,
132                                         _Bool dma_only);
133
134 __api void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags);
135 __api u32 lightrec_exit_flags(struct lightrec_state *state);
136
137 __api __cnst struct lightrec_registers *
138 lightrec_get_registers(struct lightrec_state *state);
139
140 __api u32 lightrec_current_cycle_count(const struct lightrec_state *state);
141 __api void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles);
142 __api void lightrec_set_target_cycle_count(struct lightrec_state *state,
143                                            u32 cycles);
144
145 #ifdef __cplusplus
146 };
147 #endif
148
149 #endif /* __LIGHTREC_H__ */