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