psxbios: remove some globals
[pcsx_rearmed.git] / frontend / plugin.c
... / ...
CommitLineData
1/*
2 * (C) notaz, 2010
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
9#include <string.h>
10#include <stdint.h>
11
12#include "plugin_lib.h"
13#include "plugin.h"
14#include "psemu_plugin_defs.h"
15#include "../libpcsxcore/system.h"
16#include "../plugins/cdrcimg/cdrcimg.h"
17
18// this can't be __stdcall like it was in PSEmu API as too many functions are mixed up
19#undef CALLBACK
20#define CALLBACK
21
22static long CALLBACK dummy_func() {
23 return 0;
24}
25
26/* SPU */
27extern long CALLBACK SPUopen(void);
28extern long CALLBACK SPUinit(void);
29extern long CALLBACK SPUshutdown(void);
30extern long CALLBACK SPUclose(void);
31extern void CALLBACK SPUplaySample(unsigned char);
32extern void CALLBACK SPUwriteRegister(unsigned long, unsigned short, unsigned int);
33extern unsigned short CALLBACK SPUreadRegister(unsigned long);
34extern void CALLBACK SPUwriteDMA(unsigned short);
35extern unsigned short CALLBACK SPUreadDMA(void);
36extern void CALLBACK SPUwriteDMAMem(unsigned short *, int, unsigned int);
37extern void CALLBACK SPUreadDMAMem(unsigned short *, int, unsigned int);
38extern void CALLBACK SPUplayADPCMchannel(void *, unsigned int, int);
39extern void CALLBACK SPUregisterCallback(void (*cb)(void));
40extern void CALLBACK SPUregisterScheduleCb(void (*cb)(unsigned int));
41extern long CALLBACK SPUconfigure(void);
42extern long CALLBACK SPUtest(void);
43extern void CALLBACK SPUabout(void);
44extern long CALLBACK SPUfreeze(unsigned int, void *, unsigned int);
45extern void CALLBACK SPUasync(unsigned int, unsigned int);
46extern int CALLBACK SPUplayCDDAchannel(short *, int, unsigned int, int);
47
48/* PAD */
49static long CALLBACK PADreadPort1(PadDataS *pad)
50{
51 pad->controllerType = in_type[0];
52 pad->buttonStatus = ~in_keystate[0];
53 if (in_type[0] == PSE_PAD_TYPE_ANALOGPAD) {
54 pad->leftJoyX = in_analog_left[0][0];
55 pad->leftJoyY = in_analog_left[0][1];
56 pad->rightJoyX = in_analog_right[0][0];
57 pad->rightJoyY = in_analog_right[0][1];
58 }
59 return 0;
60}
61
62static long CALLBACK PADreadPort2(PadDataS *pad)
63{
64 pad->controllerType = in_type[1];
65 pad->buttonStatus = ~in_keystate[0] >> 16;
66 return 0;
67}
68
69/* GPU */
70extern long GPUopen(unsigned long *, char *, char *);
71extern long GPUinit(void);
72extern long GPUshutdown(void);
73extern long GPUclose(void);
74extern void GPUwriteStatus(uint32_t);
75extern void GPUwriteData(uint32_t);
76extern void GPUwriteDataMem(uint32_t *, int);
77extern uint32_t GPUreadStatus(void);
78extern uint32_t GPUreadData(void);
79extern void GPUreadDataMem(uint32_t *, int);
80extern long GPUdmaChain(uint32_t *,uint32_t);
81extern void GPUupdateLace(void);
82extern long GPUfreeze(uint32_t, void *);
83extern void GPUvBlank(int, int);
84extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
85
86
87#define DUMMY(id, name) \
88 { id, #name, dummy_func }
89
90#define DIRECT(id, name) \
91 { id, #name, name }
92
93#define DUMMY_GPU(name) DUMMY(PLUGIN_GPU, name)
94#define DUMMY_CDR(name) DUMMY(PLUGIN_CDR, name)
95#define DUMMY_PAD(name) DUMMY(PLUGIN_PAD, name)
96#define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
97#define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
98#define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
99
100static const struct {
101 int id;
102 const char *name;
103 void *func;
104} plugin_funcs[] = {
105 /* CDR */
106 DUMMY_CDR(CDRinit),
107 DUMMY_CDR(CDRshutdown),
108 DUMMY_CDR(CDRopen),
109 DUMMY_CDR(CDRclose),
110 DUMMY_CDR(CDRtest),
111 DUMMY_CDR(CDRgetTN),
112 DUMMY_CDR(CDRgetTD),
113 DUMMY_CDR(CDRreadTrack),
114 DUMMY_CDR(CDRgetBuffer),
115 DUMMY_CDR(CDRgetBufferSub),
116 DUMMY_CDR(CDRplay),
117 DUMMY_CDR(CDRstop),
118 DUMMY_CDR(CDRgetStatus),
119 DUMMY_CDR(CDRgetDriveLetter),
120 DUMMY_CDR(CDRconfigure),
121 DUMMY_CDR(CDRabout),
122 DUMMY_CDR(CDRsetfilename),
123 DUMMY_CDR(CDRreadCDDA),
124 DUMMY_CDR(CDRgetTE),
125 /* SPU */
126 DIRECT_SPU(SPUconfigure),
127 DIRECT_SPU(SPUabout),
128 DIRECT_SPU(SPUinit),
129 DIRECT_SPU(SPUshutdown),
130 DIRECT_SPU(SPUtest),
131 DIRECT_SPU(SPUopen),
132 DIRECT_SPU(SPUclose),
133// DIRECT_SPU(SPUplaySample), // unused?
134 DIRECT_SPU(SPUwriteRegister),
135 DIRECT_SPU(SPUreadRegister),
136 DIRECT_SPU(SPUwriteDMA),
137 DIRECT_SPU(SPUreadDMA),
138 DIRECT_SPU(SPUwriteDMAMem),
139 DIRECT_SPU(SPUreadDMAMem),
140 DIRECT_SPU(SPUplayADPCMchannel),
141 DIRECT_SPU(SPUfreeze),
142 DIRECT_SPU(SPUregisterCallback),
143 DIRECT_SPU(SPUregisterScheduleCb),
144 DIRECT_SPU(SPUasync),
145 DIRECT_SPU(SPUplayCDDAchannel),
146 /* PAD */
147 DUMMY_PAD(PADinit),
148 DUMMY_PAD(PADshutdown),
149 DUMMY_PAD(PADopen),
150 DUMMY_PAD(PADclose),
151 DUMMY_PAD(PADsetSensitive),
152 DIRECT_PAD(PADreadPort1),
153 DIRECT_PAD(PADreadPort2),
154/*
155 DUMMY_PAD(PADquery),
156 DUMMY_PAD(PADconfigure),
157 DUMMY_PAD(PADtest),
158 DUMMY_PAD(PADabout),
159 DUMMY_PAD(PADkeypressed),
160 DUMMY_PAD(PADstartPoll),
161 DUMMY_PAD(PADpoll),
162*/
163 /* GPU */
164 DIRECT_GPU(GPUupdateLace),
165 DIRECT_GPU(GPUinit),
166 DIRECT_GPU(GPUshutdown),
167 DIRECT_GPU(GPUopen),
168 DIRECT_GPU(GPUclose),
169 DIRECT_GPU(GPUreadStatus),
170 DIRECT_GPU(GPUreadData),
171 DIRECT_GPU(GPUreadDataMem),
172 DIRECT_GPU(GPUwriteStatus),
173 DIRECT_GPU(GPUwriteData),
174 DIRECT_GPU(GPUwriteDataMem),
175 DIRECT_GPU(GPUdmaChain),
176 DIRECT_GPU(GPUfreeze),
177 DIRECT_GPU(GPUvBlank),
178 DIRECT_GPU(GPUrearmedCallbacks),
179
180 DUMMY_GPU(GPUdisplayText),
181/*
182 DIRECT_GPU(GPUkeypressed),
183 DIRECT_GPU(GPUmakeSnapshot),
184 DIRECT_GPU(GPUconfigure),
185 DIRECT_GPU(GPUtest),
186 DIRECT_GPU(GPUabout),
187 DIRECT_GPU(GPUgetScreenPic),
188 DIRECT_GPU(GPUshowScreenPic),
189*/
190};
191
192void *plugin_link(enum builtint_plugins_e id, const char *sym)
193{
194 int i;
195
196 if (id == PLUGIN_CDRCIMG)
197 return cdrcimg_get_sym(sym);
198
199 for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
200 if (id != plugin_funcs[i].id)
201 continue;
202
203 if (strcmp(sym, plugin_funcs[i].name) != 0)
204 continue;
205
206 return plugin_funcs[i].func;
207 }
208
209 //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
210 return NULL;
211}
212
213void plugin_call_rearmed_cbs(void)
214{
215 extern void *hGPUDriver;
216 void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
217
218 rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
219 if (rearmed_set_cbs != NULL)
220 rearmed_set_cbs(&pl_rearmed_cbs);
221}
222
223#ifdef PCNT
224
225/* basic profile stuff */
226#include "pcnt.h"
227
228unsigned int pcounters[PCNT_CNT];
229unsigned int pcounter_starts[PCNT_CNT];
230
231#define pc_hook_func(name, args, pargs, cnt) \
232extern void (*name) args; \
233static void (*o_##name) args; \
234static void w_##name args \
235{ \
236 unsigned int pc_start = pcnt_get(); \
237 o_##name pargs; \
238 pcounters[cnt] += pcnt_get() - pc_start; \
239}
240
241#define pc_hook_func_ret(retn, name, args, pargs, cnt) \
242extern retn (*name) args; \
243static retn (*o_##name) args; \
244static retn w_##name args \
245{ \
246 retn ret; \
247 unsigned int pc_start = pcnt_get(); \
248 ret = o_##name pargs; \
249 pcounters[cnt] += pcnt_get() - pc_start; \
250 return ret; \
251}
252
253pc_hook_func (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
254pc_hook_func (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
255pc_hook_func (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
256pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
257pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
258pc_hook_func (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
259pc_hook_func_ret(long, GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
260pc_hook_func (GPU_updateLace, (void), (), PCNT_GPU)
261
262pc_hook_func (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
263pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
264pc_hook_func (SPU_writeDMA, (unsigned short a0), (a0), PCNT_SPU)
265pc_hook_func_ret(unsigned short,SPU_readDMA, (void), (), PCNT_SPU)
266pc_hook_func (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
267pc_hook_func (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
268pc_hook_func (SPU_playADPCMchannel, (void *a0, unsigned int a1, int a2), (a0, a1, a2), PCNT_SPU)
269pc_hook_func (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
270pc_hook_func_ret(int, SPU_playCDDAchannel, (short *a0, int a1, unsigned int a2, int a3), (a0, a1, a2, a3), PCNT_SPU)
271
272#define hook_it(name) { \
273 o_##name = name; \
274 name = w_##name; \
275}
276
277void pcnt_hook_plugins(void)
278{
279 pcnt_init();
280
281 hook_it(GPU_writeStatus);
282 hook_it(GPU_writeData);
283 hook_it(GPU_writeDataMem);
284 hook_it(GPU_readStatus);
285 hook_it(GPU_readData);
286 hook_it(GPU_readDataMem);
287 hook_it(GPU_dmaChain);
288 hook_it(GPU_updateLace);
289 hook_it(SPU_writeRegister);
290 hook_it(SPU_readRegister);
291 hook_it(SPU_writeDMA);
292 hook_it(SPU_readDMA);
293 hook_it(SPU_writeDMAMem);
294 hook_it(SPU_readDMAMem);
295 hook_it(SPU_playADPCMchannel);
296 hook_it(SPU_async);
297 hook_it(SPU_playCDDAchannel);
298}
299
300// hooked into recompiler
301void pcnt_gte_start(int op)
302{
303 pcnt_start(PCNT_GTE);
304}
305
306void pcnt_gte_end(int op)
307{
308 pcnt_end(PCNT_GTE);
309}
310
311#endif