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