cdrom: some debugging aid
[pcsx_rearmed.git] / frontend / plugin.c
CommitLineData
e906c010 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
69af03a2 12#include "plugin_lib.h"
e906c010 13#include "plugin.h"
da710571 14#include "psemu_plugin_defs.h"
201c21e2 15#include "../libpcsxcore/system.h"
47bf65ab 16#include "../plugins/cdrcimg/cdrcimg.h"
e906c010 17
78de2879 18// this can't be __stdcall like it was in PSEmu API as too many functions are mixed up
19#undef CALLBACK
003cfc63 20#define CALLBACK
003cfc63 21
78de2879 22static long CALLBACK dummy_func() {
e906c010 23 return 0;
24}
25
e906c010 26/* SPU */
003cfc63 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 *);
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);
e906c010 47
48/* PAD */
78de2879 49static long CALLBACK PADreadPort1(PadDataS *pad) {
e288923d 50 int pad_index = pad->requestPadIndex;
7dea987c 51
52 pad->controllerType = in_type[pad_index];
53 pad->buttonStatus = ~in_keystate[pad_index];
54
55 if (multitap1 == 1)
56 pad->portMultitap = 1;
57 else
58 pad->portMultitap = 0;
59
60 if (in_type[pad_index] == PSE_PAD_TYPE_ANALOGJOY || in_type[pad_index] == PSE_PAD_TYPE_ANALOGPAD || in_type[pad_index] == PSE_PAD_TYPE_NEGCON || in_type[pad_index] == PSE_PAD_TYPE_GUNCON)
61 {
62 pad->leftJoyX = in_analog_left[pad_index][0];
63 pad->leftJoyY = in_analog_left[pad_index][1];
64 pad->rightJoyX = in_analog_right[pad_index][0];
65 pad->rightJoyY = in_analog_right[pad_index][1];
66
67 pad->absoluteX = in_analog_left[pad_index][0];
68 pad->absoluteY = in_analog_left[pad_index][1];
69 }
70
71 if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
72 {
73 pad->moveX = in_mouse[pad_index][0];
74 pad->moveY = in_mouse[pad_index][1];
75 }
76
77 return 0;
e906c010 78}
79
78de2879 80static long CALLBACK PADreadPort2(PadDataS *pad) {
e288923d 81 int pad_index = pad->requestPadIndex;
7dea987c 82
83 pad->controllerType = in_type[pad_index];
84 pad->buttonStatus = ~in_keystate[pad_index];
85
86 if (multitap2 == 1)
87 pad->portMultitap = 2;
88 else
89 pad->portMultitap = 0;
90
91 if (in_type[pad_index] == PSE_PAD_TYPE_ANALOGJOY || in_type[pad_index] == PSE_PAD_TYPE_ANALOGPAD || in_type[pad_index] == PSE_PAD_TYPE_NEGCON || in_type[pad_index] == PSE_PAD_TYPE_GUNCON)
92 {
93 pad->leftJoyX = in_analog_left[pad_index][0];
94 pad->leftJoyY = in_analog_left[pad_index][1];
95 pad->rightJoyX = in_analog_right[pad_index][0];
96 pad->rightJoyY = in_analog_right[pad_index][1];
97
98 pad->absoluteX = in_analog_left[pad_index][0];
99 pad->absoluteY = in_analog_left[pad_index][1];
100 }
101
102 if (in_type[pad_index] == PSE_PAD_TYPE_MOUSE)
103 {
104 pad->moveX = in_mouse[pad_index][0];
105 pad->moveY = in_mouse[pad_index][1];
106 }
107
108 return 0;
e906c010 109}
110
111/* GPU */
112extern long GPUopen(unsigned long *, char *, char *);
113extern long GPUinit(void);
114extern long GPUshutdown(void);
115extern long GPUclose(void);
116extern void GPUwriteStatus(uint32_t);
117extern void GPUwriteData(uint32_t);
118extern void GPUwriteDataMem(uint32_t *, int);
119extern uint32_t GPUreadStatus(void);
120extern uint32_t GPUreadData(void);
121extern void GPUreadDataMem(uint32_t *, int);
122extern long GPUdmaChain(uint32_t *,uint32_t);
123extern void GPUupdateLace(void);
e906c010 124extern long GPUfreeze(uint32_t, void *);
72e5023f 125extern void GPUvBlank(int, int);
e64dc4c5 126extern void GPUrearmedCallbacks(const struct rearmed_cbs *cbs);
e906c010 127
128
129#define DUMMY(id, name) \
130 { id, #name, dummy_func }
131
132#define DIRECT(id, name) \
133 { id, #name, name }
134
c5061935 135#define DUMMY_GPU(name) DUMMY(PLUGIN_GPU, name)
e906c010 136#define DUMMY_CDR(name) DUMMY(PLUGIN_CDR, name)
137#define DUMMY_PAD(name) DUMMY(PLUGIN_PAD, name)
138#define DIRECT_SPU(name) DIRECT(PLUGIN_SPU, name)
139#define DIRECT_GPU(name) DIRECT(PLUGIN_GPU, name)
140#define DIRECT_PAD(name) DIRECT(PLUGIN_PAD, name)
141
142static const struct {
143 int id;
144 const char *name;
145 void *func;
146} plugin_funcs[] = {
147 /* CDR */
148 DUMMY_CDR(CDRinit),
149 DUMMY_CDR(CDRshutdown),
150 DUMMY_CDR(CDRopen),
151 DUMMY_CDR(CDRclose),
152 DUMMY_CDR(CDRtest),
153 DUMMY_CDR(CDRgetTN),
154 DUMMY_CDR(CDRgetTD),
155 DUMMY_CDR(CDRreadTrack),
156 DUMMY_CDR(CDRgetBuffer),
157 DUMMY_CDR(CDRgetBufferSub),
158 DUMMY_CDR(CDRplay),
159 DUMMY_CDR(CDRstop),
160 DUMMY_CDR(CDRgetStatus),
161 DUMMY_CDR(CDRgetDriveLetter),
162 DUMMY_CDR(CDRconfigure),
163 DUMMY_CDR(CDRabout),
164 DUMMY_CDR(CDRsetfilename),
165 DUMMY_CDR(CDRreadCDDA),
166 DUMMY_CDR(CDRgetTE),
e906c010 167 /* SPU */
168 DIRECT_SPU(SPUconfigure),
169 DIRECT_SPU(SPUabout),
170 DIRECT_SPU(SPUinit),
171 DIRECT_SPU(SPUshutdown),
172 DIRECT_SPU(SPUtest),
173 DIRECT_SPU(SPUopen),
174 DIRECT_SPU(SPUclose),
175// DIRECT_SPU(SPUplaySample), // unused?
176 DIRECT_SPU(SPUwriteRegister),
177 DIRECT_SPU(SPUreadRegister),
178 DIRECT_SPU(SPUwriteDMA),
179 DIRECT_SPU(SPUreadDMA),
180 DIRECT_SPU(SPUwriteDMAMem),
181 DIRECT_SPU(SPUreadDMAMem),
182 DIRECT_SPU(SPUplayADPCMchannel),
183 DIRECT_SPU(SPUfreeze),
184 DIRECT_SPU(SPUregisterCallback),
2b30c129 185 DIRECT_SPU(SPUregisterScheduleCb),
e906c010 186 DIRECT_SPU(SPUasync),
187 DIRECT_SPU(SPUplayCDDAchannel),
188 /* PAD */
e906c010 189 DUMMY_PAD(PADinit),
190 DUMMY_PAD(PADshutdown),
e906c010 191 DUMMY_PAD(PADopen),
192 DUMMY_PAD(PADclose),
e1d2234d 193 DUMMY_PAD(PADsetSensitive),
194 DIRECT_PAD(PADreadPort1),
195 DIRECT_PAD(PADreadPort2),
196/*
e906c010 197 DUMMY_PAD(PADquery),
e1d2234d 198 DUMMY_PAD(PADconfigure),
199 DUMMY_PAD(PADtest),
200 DUMMY_PAD(PADabout),
e906c010 201 DUMMY_PAD(PADkeypressed),
e1d2234d 202 DUMMY_PAD(PADstartPoll),
203 DUMMY_PAD(PADpoll),
204*/
e906c010 205 /* GPU */
206 DIRECT_GPU(GPUupdateLace),
207 DIRECT_GPU(GPUinit),
208 DIRECT_GPU(GPUshutdown),
e906c010 209 DIRECT_GPU(GPUopen),
210 DIRECT_GPU(GPUclose),
211 DIRECT_GPU(GPUreadStatus),
212 DIRECT_GPU(GPUreadData),
213 DIRECT_GPU(GPUreadDataMem),
214 DIRECT_GPU(GPUwriteStatus),
215 DIRECT_GPU(GPUwriteData),
216 DIRECT_GPU(GPUwriteDataMem),
217 DIRECT_GPU(GPUdmaChain),
c5061935 218 DIRECT_GPU(GPUfreeze),
72e5023f 219 DIRECT_GPU(GPUvBlank),
e64dc4c5 220 DIRECT_GPU(GPUrearmedCallbacks),
c5061935 221
222 DUMMY_GPU(GPUdisplayText),
223/*
e906c010 224 DIRECT_GPU(GPUkeypressed),
e906c010 225 DIRECT_GPU(GPUmakeSnapshot),
c5061935 226 DIRECT_GPU(GPUconfigure),
227 DIRECT_GPU(GPUtest),
228 DIRECT_GPU(GPUabout),
e906c010 229 DIRECT_GPU(GPUgetScreenPic),
230 DIRECT_GPU(GPUshowScreenPic),
c5061935 231*/
e906c010 232// DIRECT_GPU(GPUclearDynarec),
e906c010 233};
234
235void *plugin_link(enum builtint_plugins_e id, const char *sym)
236{
237 int i;
238
47bf65ab 239 if (id == PLUGIN_CDRCIMG)
240 return cdrcimg_get_sym(sym);
241
e906c010 242 for (i = 0; i < ARRAY_SIZE(plugin_funcs); i++) {
243 if (id != plugin_funcs[i].id)
244 continue;
245
246 if (strcmp(sym, plugin_funcs[i].name) != 0)
247 continue;
248
249 return plugin_funcs[i].func;
250 }
251
e1d2234d 252 //fprintf(stderr, "plugin_link: missing symbol %d %s\n", id, sym);
e906c010 253 return NULL;
254}
255
201c21e2 256void plugin_call_rearmed_cbs(void)
257{
258 extern void *hGPUDriver;
259 void (*rearmed_set_cbs)(const struct rearmed_cbs *cbs);
260
261 rearmed_set_cbs = SysLoadSym(hGPUDriver, "GPUrearmedCallbacks");
262 if (rearmed_set_cbs != NULL)
263 rearmed_set_cbs(&pl_rearmed_cbs);
264}
265
fa9cfe0a 266#ifdef PCNT
267
14dffdb7 268/* basic profile stuff */
269#include "pcnt.h"
270
271unsigned int pcounters[PCNT_CNT];
272unsigned int pcounter_starts[PCNT_CNT];
273
274#define pc_hook_func(name, args, pargs, cnt) \
275extern void (*name) args; \
276static void (*o_##name) args; \
277static void w_##name args \
278{ \
279 unsigned int pc_start = pcnt_get(); \
280 o_##name pargs; \
281 pcounters[cnt] += pcnt_get() - pc_start; \
282}
283
284#define pc_hook_func_ret(retn, name, args, pargs, cnt) \
285extern retn (*name) args; \
286static retn (*o_##name) args; \
287static retn w_##name args \
288{ \
289 retn ret; \
290 unsigned int pc_start = pcnt_get(); \
291 ret = o_##name pargs; \
292 pcounters[cnt] += pcnt_get() - pc_start; \
293 return ret; \
294}
295
296pc_hook_func (GPU_writeStatus, (uint32_t a0), (a0), PCNT_GPU)
297pc_hook_func (GPU_writeData, (uint32_t a0), (a0), PCNT_GPU)
298pc_hook_func (GPU_writeDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
299pc_hook_func_ret(uint32_t, GPU_readStatus, (void), (), PCNT_GPU)
300pc_hook_func_ret(uint32_t, GPU_readData, (void), (), PCNT_GPU)
301pc_hook_func (GPU_readDataMem, (uint32_t *a0, int a1), (a0, a1), PCNT_GPU)
302pc_hook_func_ret(long, GPU_dmaChain, (uint32_t *a0, int32_t a1), (a0, a1), PCNT_GPU)
303pc_hook_func (GPU_updateLace, (void), (), PCNT_GPU)
304
650adfd2 305pc_hook_func (SPU_writeRegister, (unsigned long a0, unsigned short a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
14dffdb7 306pc_hook_func_ret(unsigned short,SPU_readRegister, (unsigned long a0), (a0), PCNT_SPU)
307pc_hook_func (SPU_writeDMA, (unsigned short a0), (a0), PCNT_SPU)
308pc_hook_func_ret(unsigned short,SPU_readDMA, (void), (), PCNT_SPU)
650adfd2 309pc_hook_func (SPU_writeDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
310pc_hook_func (SPU_readDMAMem, (unsigned short *a0, int a1, uint32_t a2), (a0, a1, a2), PCNT_SPU)
14dffdb7 311pc_hook_func (SPU_playADPCMchannel, (void *a0), (a0), PCNT_SPU)
650adfd2 312pc_hook_func (SPU_async, (uint32_t a0, uint32_t a1), (a0, a1), PCNT_SPU)
983a7cfd 313pc_hook_func_ret(int, SPU_playCDDAchannel, (short *a0, int a1), (a0, a1), PCNT_SPU)
14dffdb7 314
315#define hook_it(name) { \
316 o_##name = name; \
317 name = w_##name; \
318}
319
320void pcnt_hook_plugins(void)
321{
19e57cbf 322 pcnt_init();
f95a77f7 323
14dffdb7 324 hook_it(GPU_writeStatus);
325 hook_it(GPU_writeData);
326 hook_it(GPU_writeDataMem);
327 hook_it(GPU_readStatus);
328 hook_it(GPU_readData);
329 hook_it(GPU_readDataMem);
330 hook_it(GPU_dmaChain);
331 hook_it(GPU_updateLace);
332 hook_it(SPU_writeRegister);
333 hook_it(SPU_readRegister);
334 hook_it(SPU_writeDMA);
335 hook_it(SPU_readDMA);
336 hook_it(SPU_writeDMAMem);
337 hook_it(SPU_readDMAMem);
338 hook_it(SPU_playADPCMchannel);
339 hook_it(SPU_async);
340 hook_it(SPU_playCDDAchannel);
341}
342
82ed88eb 343// hooked into recompiler
344void pcnt_gte_start(int op)
345{
346 pcnt_start(PCNT_GTE);
347}
348
349void pcnt_gte_end(int op)
350{
351 pcnt_end(PCNT_GTE);
352}
353
fa9cfe0a 354#endif