cdrom: don't report read too early
[pcsx_rearmed.git] / deps / lightrec / lightrec-private.h
... / ...
CommitLineData
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_PRIVATE_H__
7#define __LIGHTREC_PRIVATE_H__
8
9#include "lightning-wrapper.h"
10#include "lightrec-config.h"
11#include "disassembler.h"
12#include "lightrec.h"
13#include "regcache.h"
14
15#if ENABLE_THREADED_COMPILER
16#include <stdatomic.h>
17#endif
18
19#ifdef _MSC_BUILD
20#include <immintrin.h>
21#endif
22
23#define ARRAY_SIZE(x) (sizeof(x) ? sizeof(x) / sizeof((x)[0]) : 0)
24
25#define GENMASK(h, l) \
26 (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
27
28#ifdef __GNUC__
29# define likely(x) __builtin_expect(!!(x),1)
30# define unlikely(x) __builtin_expect(!!(x),0)
31#else
32# define likely(x) (x)
33# define unlikely(x) (x)
34#endif
35
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)
41#else
42# define LE32TOH(x) (x)
43# define HTOLE32(x) (x)
44# define LE16TOH(x) (x)
45# define HTOLE16(x) (x)
46#endif
47
48#if HAS_DEFAULT_ELM
49#define SET_DEFAULT_ELM(table, value) [0 ... ARRAY_SIZE(table) - 1] = value
50#else
51#define SET_DEFAULT_ELM(table, value) [0] = NULL
52#endif
53
54#define fallthrough do {} while (0) /* fall-through */
55
56#define container_of(ptr, type, member) \
57 ((type *)((void *)(ptr) - offsetof(type, member)))
58
59#ifdef _MSC_BUILD
60# define popcount32(x) __popcnt(x)
61# define clz32(x) _lzcnt_u32(x)
62# define ctz32(x) _tzcnt_u32(x)
63#else
64# define popcount32(x) __builtin_popcount(x)
65# define clz32(x) __builtin_clz(x)
66# define ctz32(x) __builtin_ctz(x)
67#endif
68
69/* Flags for (struct block *)->flags */
70#define BLOCK_NEVER_COMPILE BIT(0)
71#define BLOCK_SHOULD_RECOMPILE BIT(1)
72#define BLOCK_FULLY_TAGGED BIT(2)
73#define BLOCK_IS_DEAD BIT(3)
74#define BLOCK_IS_MEMSET BIT(4)
75#define BLOCK_NO_OPCODE_LIST BIT(5)
76
77#define RAM_SIZE 0x200000
78#define BIOS_SIZE 0x80000
79
80#define CODE_LUT_SIZE ((RAM_SIZE + BIOS_SIZE) >> 2)
81
82#define REG_LO 32
83#define REG_HI 33
84
85/* Definition of jit_state_t (avoids inclusion of <lightning.h>) */
86struct jit_node;
87struct jit_state;
88typedef struct jit_state jit_state_t;
89
90struct blockcache;
91struct recompiler;
92struct regcache;
93struct opcode;
94struct reaper;
95
96struct u16x2 {
97#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
98 u16 h, l;
99#else
100 u16 l, h;
101#endif
102};
103
104struct block {
105 jit_state_t *_jit;
106 struct opcode *opcode_list;
107 void (*function)(void);
108 const u32 *code;
109 struct block *next;
110 u32 pc;
111 u32 hash;
112 u32 precompile_date;
113 unsigned int code_size;
114 u16 nb_ops;
115#if ENABLE_THREADED_COMPILER
116 _Atomic u8 flags;
117#else
118 u8 flags;
119#endif
120};
121
122struct lightrec_branch {
123 struct jit_node *branch;
124 u32 target;
125};
126
127struct lightrec_branch_target {
128 struct jit_node *label;
129 u32 offset;
130};
131
132enum c_wrappers {
133 C_WRAPPER_RW,
134 C_WRAPPER_RW_GENERIC,
135 C_WRAPPER_MFC,
136 C_WRAPPER_MTC,
137 C_WRAPPER_CP,
138 C_WRAPPERS_COUNT,
139};
140
141struct lightrec_cstate {
142 struct lightrec_state *state;
143
144 struct lightrec_branch local_branches[512];
145 struct lightrec_branch_target targets[512];
146 unsigned int nb_local_branches;
147 unsigned int nb_targets;
148 unsigned int cycles;
149
150 struct regcache *reg_cache;
151};
152
153struct lightrec_state {
154 struct lightrec_registers regs;
155 uintptr_t wrapper_regs[NUM_TEMPS];
156 u32 next_pc;
157 u32 current_cycle;
158 u32 target_cycle;
159 u32 exit_flags;
160 u32 old_cycle_counter;
161 struct block *dispatcher, *c_wrapper_block;
162 void *c_wrappers[C_WRAPPERS_COUNT];
163 void *wrappers_eps[C_WRAPPERS_COUNT];
164 struct blockcache *block_cache;
165 struct recompiler *rec;
166 struct lightrec_cstate *cstate;
167 struct reaper *reaper;
168 void *tlsf;
169 void (*eob_wrapper_func)(void);
170 void (*memset_func)(void);
171 void (*get_next_block)(void);
172 struct lightrec_ops ops;
173 unsigned int nb_precompile;
174 unsigned int nb_maps;
175 const struct lightrec_mem_map *maps;
176 uintptr_t offset_ram, offset_bios, offset_scratch, offset_io;
177 _Bool with_32bit_lut;
178 _Bool mirrors_mapped;
179 _Bool invalidate_from_dma_only;
180 void *code_lut[];
181};
182
183u32 lightrec_rw(struct lightrec_state *state, union code op,
184 u32 addr, u32 data, u32 *flags,
185 struct block *block);
186
187void lightrec_free_block(struct lightrec_state *state, struct block *block);
188
189void remove_from_code_lut(struct blockcache *cache, struct block *block);
190
191enum psx_map
192lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr);
193
194const struct lightrec_mem_map *
195lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr);
196
197static inline u32 kunseg(u32 addr)
198{
199 if (unlikely(addr >= 0xa0000000))
200 return addr - 0xa0000000;
201 else
202 return addr &~ 0x80000000;
203}
204
205static inline u32 lut_offset(u32 pc)
206{
207 if (pc & BIT(28))
208 return ((pc & (BIOS_SIZE - 1)) + RAM_SIZE) >> 2; // BIOS
209 else
210 return (pc & (RAM_SIZE - 1)) >> 2; // RAM
211}
212
213static inline _Bool is_big_endian(void)
214{
215 return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
216}
217
218static inline _Bool lut_is_32bit(const struct lightrec_state *state)
219{
220 return __WORDSIZE == 32 ||
221 (ENABLE_CODE_BUFFER && state->with_32bit_lut);
222}
223
224static inline size_t lut_elm_size(const struct lightrec_state *state)
225{
226 return lut_is_32bit(state) ? 4 : sizeof(void *);
227}
228
229static inline void ** lut_address(struct lightrec_state *state, u32 offset)
230{
231 if (lut_is_32bit(state))
232 return (void **) ((uintptr_t) state->code_lut + offset * 4);
233 else
234 return &state->code_lut[offset];
235}
236
237static inline void * lut_read(struct lightrec_state *state, u32 offset)
238{
239 void **lut_entry = lut_address(state, offset);
240
241 if (lut_is_32bit(state))
242 return (void *)(uintptr_t) *(u32 *) lut_entry;
243 else
244 return *lut_entry;
245}
246
247static inline void lut_write(struct lightrec_state *state, u32 offset, void *ptr)
248{
249 void **lut_entry = lut_address(state, offset);
250
251 if (lut_is_32bit(state))
252 *(u32 *) lut_entry = (u32)(uintptr_t) ptr;
253 else
254 *lut_entry = ptr;
255}
256
257static inline u32 get_ds_pc(const struct block *block, u16 offset, s16 imm)
258{
259 u16 flags = block->opcode_list[offset].flags;
260
261 offset += op_flag_no_ds(flags);
262
263 return block->pc + (offset + imm << 2);
264}
265
266static inline u32 get_branch_pc(const struct block *block, u16 offset, s16 imm)
267{
268 u16 flags = block->opcode_list[offset].flags;
269
270 offset -= op_flag_no_ds(flags);
271
272 return block->pc + (offset + imm << 2);
273}
274
275void lightrec_mtc(struct lightrec_state *state, union code op, u32 data);
276u32 lightrec_mfc(struct lightrec_state *state, union code op);
277void lightrec_rfe(struct lightrec_state *state);
278void lightrec_cp(struct lightrec_state *state, union code op);
279
280struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state);
281void lightrec_free_cstate(struct lightrec_cstate *cstate);
282
283union code lightrec_read_opcode(struct lightrec_state *state, u32 pc);
284
285int lightrec_compile_block(struct lightrec_cstate *cstate, struct block *block);
286void lightrec_free_opcode_list(struct lightrec_state *state,
287 struct opcode *list);
288
289unsigned int lightrec_cycles_of_opcode(union code code);
290
291static inline u8 get_mult_div_lo(union code c)
292{
293 return (OPT_FLAG_MULT_DIV && c.r.rd) ? c.r.rd : REG_LO;
294}
295
296static inline u8 get_mult_div_hi(union code c)
297{
298 return (OPT_FLAG_MULT_DIV && c.r.imm) ? c.r.imm : REG_HI;
299}
300
301static inline s16 s16_max(s16 a, s16 b)
302{
303 return a > b ? a : b;
304}
305
306static inline _Bool block_has_flag(struct block *block, u8 flag)
307{
308#if ENABLE_THREADED_COMPILER
309 return atomic_load_explicit(&block->flags, memory_order_relaxed) & flag;
310#else
311 return block->flags & flag;
312#endif
313}
314
315static inline u8 block_set_flags(struct block *block, u8 mask)
316{
317#if ENABLE_THREADED_COMPILER
318 return atomic_fetch_or_explicit(&block->flags, mask,
319 memory_order_relaxed);
320#else
321 u8 flags = block->flags;
322
323 block->flags |= mask;
324
325 return flags;
326#endif
327}
328
329static inline u8 block_clear_flags(struct block *block, u8 mask)
330{
331#if ENABLE_THREADED_COMPILER
332 return atomic_fetch_and_explicit(&block->flags, ~mask,
333 memory_order_relaxed);
334#else
335 u8 flags = block->flags;
336
337 block->flags &= ~mask;
338
339 return flags;
340#endif
341}
342
343#endif /* __LIGHTREC_PRIVATE_H__ */