yet more timing hacks
[pcsx_rearmed.git] / libpcsxcore / plugins.c
index 34f2481..a34969f 100644 (file)
@@ -23,7 +23,6 @@
 
 #include "plugins.h"
 #include "cdriso.h"
-#include "../plugins/dfinput/externals.h"
 
 static char IsoFile[MAXPATHLEN] = "";
 static s64 cdOpenCaseTime = 0;
@@ -49,7 +48,6 @@ GPUmakeSnapshot       GPU_makeSnapshot;
 GPUfreeze             GPU_freeze;
 GPUgetScreenPic       GPU_getScreenPic;
 GPUshowScreenPic      GPU_showScreenPic;
-GPUclearDynarec       GPU_clearDynarec;
 GPUvBlank             GPU_vBlank;
 
 CDRinit               CDR_init;
@@ -72,18 +70,12 @@ CDRsetfilename        CDR_setfilename;
 CDRreadCDDA           CDR_readCDDA;
 CDRgetTE              CDR_getTE;
 
-SPUconfigure          SPU_configure;
-SPUabout              SPU_about;
 SPUinit               SPU_init;
 SPUshutdown           SPU_shutdown;
-SPUtest               SPU_test;
 SPUopen               SPU_open;
 SPUclose              SPU_close;
-SPUplaySample         SPU_playSample;
 SPUwriteRegister      SPU_writeRegister;
 SPUreadRegister       SPU_readRegister;
-SPUwriteDMA           SPU_writeDMA;
-SPUreadDMA            SPU_readDMA;
 SPUwriteDMAMem        SPU_writeDMAMem;
 SPUreadDMAMem         SPU_readDMAMem;
 SPUplayADPCMchannel   SPU_playADPCMchannel;
@@ -201,7 +193,6 @@ void CALLBACK GPU__makeSnapshot(void) {}
 void CALLBACK GPU__keypressed(int key) {}
 long CALLBACK GPU__getScreenPic(unsigned char *pMem) { return -1; }
 long CALLBACK GPU__showScreenPic(unsigned char *pMem) { return -1; }
-void CALLBACK GPU__clearDynarec(void (CALLBACK *callback)(void)) {}
 void CALLBACK GPU__vBlank(int val) {}
 
 #define LoadGpuSym1(dest, name) \
@@ -241,7 +232,6 @@ static int LoadGPUplugin(const char *GPUdll) {
        LoadGpuSym1(freeze, "GPUfreeze");
        LoadGpuSym0(getScreenPic, "GPUgetScreenPic");
        LoadGpuSym0(showScreenPic, "GPUshowScreenPic");
-       LoadGpuSym0(clearDynarec, "GPUclearDynarec");
        LoadGpuSym0(vBlank, "GPUvBlank");
        LoadGpuSym0(configure, "GPUconfigure");
        LoadGpuSym0(test, "GPUtest");
@@ -317,12 +307,8 @@ static int LoadCDRplugin(const char *CDRdll) {
        return 0;
 }
 
-void *hSPUDriver = NULL;
-
-long CALLBACK SPU__configure(void) { return 0; }
-void CALLBACK SPU__about(void) {}
-long CALLBACK SPU__test(void) { return 0; }
-void CALLBACK SPU__registerScheduleCb(void (CALLBACK *cb)(unsigned int)) {}
+static void *hSPUDriver = NULL;\r
+static void CALLBACK SPU__registerScheduleCb(void (CALLBACK *cb)(unsigned int)) {}\r
 
 #define LoadSpuSym1(dest, name) \
        LoadSym(SPU_##dest, SPU##dest, name, TRUE);
@@ -339,7 +325,6 @@ static int LoadSPUplugin(const char *SPUdll) {
 
        hSPUDriver = SysLoadLibrary(SPUdll);
        if (hSPUDriver == NULL) {
-               SPU_configure = NULL;
                SysMessage (_("Could not load SPU plugin %s!"), SPUdll); return -1;
        }
        drv = hSPUDriver;
@@ -347,13 +332,8 @@ static int LoadSPUplugin(const char *SPUdll) {
        LoadSpuSym1(shutdown, "SPUshutdown");
        LoadSpuSym1(open, "SPUopen");
        LoadSpuSym1(close, "SPUclose");
-       LoadSpuSym0(configure, "SPUconfigure");
-       LoadSpuSym0(about, "SPUabout");
-       LoadSpuSym0(test, "SPUtest");
        LoadSpuSym1(writeRegister, "SPUwriteRegister");
        LoadSpuSym1(readRegister, "SPUreadRegister");
-       LoadSpuSym1(writeDMA, "SPUwriteDMA");
-       LoadSpuSym1(readDMA, "SPUreadDMA");
        LoadSpuSym1(writeDMAMem, "SPUwriteDMAMem");
        LoadSpuSym1(readDMAMem, "SPUreadDMAMem");
        LoadSpuSym1(playADPCMchannel, "SPUplayADPCMchannel");
@@ -371,12 +351,12 @@ extern int in_type[8];
 void *hPAD1Driver = NULL;
 void *hPAD2Driver = NULL;
 
-static int multitap1 = -1;
-static int multitap2 = -1;
+static int multitap1;
+static int multitap2;
 //Pad information, keystate, mode, config mode, vibration
 static PadDataS pad[8];
 
-static int reqPos, respSize, req;
+static int reqPos, respSize;
 static int ledStateReq44[8];
 static int PadMode[8]; /* 0 : digital 1: analog */
 
@@ -573,8 +553,8 @@ void initBufForRequest(int padIndex, char value){
 
 
 
-void reqIndex2Treatment(int padIndex, char value){
-       switch (req){
+static void reqIndex2Treatment(int padIndex, char value) {
+       switch (pad[padIndex].txData[0]) {
                case CMD_CONFIG_MODE :
                        //0x43
                        if (value == 0) {
@@ -650,7 +630,7 @@ void _PADstartPoll(PadDataS *pad) {
                        memcpy(buf, stdpar, 8);
                        respSize = 8;
                        break;
-       case PSE_PAD_TYPE_GUNCON: // GUNCON - gun controller SLPH-00034 from Namco
+               case PSE_PAD_TYPE_GUNCON: // GUNCON - gun controller SLPH-00034 from Namco
                        stdpar[0] = 0x63;
                        stdpar[1] = 0x5a;
                        stdpar[2] = pad->buttonStatus & 0xff;
@@ -664,33 +644,30 @@ void _PADstartPoll(PadDataS *pad) {
                        int absX = (pad->absoluteX / 64) + 512;
                        int absY = (pad->absoluteY / 64) + 512;
 
-                       //Keep within limits
-                       if (absX > 1023) absX = 1023;
-                       if (absX < 0) absX = 0;
-                       if (absY > 1023) absY = 1023;
-                       if (absY < 0) absY = 0;
-
-                       stdpar[4] = 0x5a - (xres - 256) / 3 + (((xres - 256) / 3 + 356) * absX >> 10);
-                       stdpar[5] = (0x5a - (xres - 256) / 3 + (((xres - 256) / 3 + 356) * absX >> 10)) >> 8;
-                       stdpar[6] = 0x20 + (yres * absY >> 10);
-                       stdpar[7] = (0x20 + (yres * absY >> 10)) >> 8;
-
-                       //Offscreen - Point at the side of the screen so PSX thinks you are pointing offscreen
-                       //Required as a mouse can't be offscreen
-                       //Coordinates X=0001h, Y=000Ah indicates "no light"
-                       //This will mean you cannot shoot the very each of the screen
-                       //ToDo read offscreen range from settings if useful to change
-                       int OffscreenRange = 2;
-                       if (absX < (OffscreenRange) || absX > (1023 - OffscreenRange) || absY < (OffscreenRange) || absY > (1023 - OffscreenRange)) {
-                               stdpar[4] = 0x01;
-                               stdpar[5] = 0x00;
-                               stdpar[6] = 0x0A;
-                               stdpar[7] = 0x00;
+                       if (absX == 65536 || absY == 65536) {
+                          stdpar[4] = 0x01;
+                          stdpar[5] = 0x00;
+                          stdpar[6] = 0x0A;
+                          stdpar[7] = 0x00;
+                       }
+                       else {
+                          stdpar[4] = 0x5a - (xres - 256) / 3 + (((xres - 256) / 3 + 356) * absX >> 10);
+                          stdpar[5] = (0x5a - (xres - 256) / 3 + (((xres - 256) / 3 + 356) * absX >> 10)) >> 8;
+                          stdpar[6] = 0x20 + (yres * absY >> 10);
+                          stdpar[7] = (0x20 + (yres * absY >> 10)) >> 8;
                        }
 
                        memcpy(buf, stdpar, 8);
                        respSize = 8;
                        break;
+               case PSE_PAD_TYPE_GUN: // GUN CONTROLLER - gun controller SLPH-00014 from Konami
+                       stdpar[0] = 0x31;
+                       stdpar[1] = 0x5a;
+                       stdpar[2] = pad->buttonStatus & 0xff;
+                       stdpar[3] = pad->buttonStatus >> 8;
+                       memcpy(buf, stdpar, 4);
+                       respSize = 4;
+                       break;
                case PSE_PAD_TYPE_ANALOGPAD: // scph1150
                        stdpar[0] = 0x73;
                        stdpar[1] = 0x5a;
@@ -720,25 +697,11 @@ void _PADstartPoll(PadDataS *pad) {
                        stdpar[1] = 0x5a;
                        stdpar[2] = pad->buttonStatus & 0xff;
                        stdpar[3] = pad->buttonStatus >> 8;
-                       //avoid analog value in multitap mode if change pad type in game.
-                       stdpar[4] = 0xff;
-                       stdpar[5] = 0xff;
-                       stdpar[6] = 0xff;
-                       stdpar[7] = 0xff;
-                       memcpy(buf, stdpar, 8);
-                       respSize = 8;
+                       memcpy(buf, stdpar, 4);
+                       respSize = 4;
                        break;
                default:
-                       stdpar[0] = 0xff;
-                       stdpar[1] = 0xff;
-                       stdpar[2] = 0xff;
-                       stdpar[3] = 0xff;
-                       stdpar[4] = 0xff;
-                       stdpar[5] = 0xff;
-                       stdpar[6] = 0xff;
-                       stdpar[7] = 0xff;
-                       memcpy(buf, stdpar, 8);
-                       respSize = 8;
+                       respSize = 0;
                        break;
        }
 }
@@ -757,59 +720,52 @@ void _PADstartPollMultitap(PadDataS* padd) {
        respSize = 34;
 }
 
-
-unsigned char _PADpoll(int port, unsigned char value) {
-       if (reqPos == 0) {
-               //mem the request number
-               req = value;
-
-               // Don't enable Analog/Vibration for a standard pad
-               if (in_type[port] == PSE_PAD_TYPE_STANDARD ||
-                       in_type[port] == PSE_PAD_TYPE_NEGCON) {
-                       ; // Pad keystate already in buffer
-               }
-               else
-               {
-                       //copy the default value of request response in buffer instead of the keystate
+static void PADpoll_dualshock(int port, unsigned char value)
+{
+       switch (reqPos) {
+               case 0:
                        initBufForRequest(port, value);
-               }
-       }
-
-       //if no new request the pad return 0xff, for signaling connected
-       if (reqPos >= respSize
-#ifdef ICACHE_EMULATION
-        && writeok
-#endif
-        ) return 0xff;
-
-       switch(reqPos){
+                       break;
                case 2:
                        reqIndex2Treatment(port, value);
-               break;
+                       break;
                case 3:
-                       switch(req) {
-                               case CMD_SET_MODE_AND_LOCK :
-                                       //change mode on pad
-                               break;
-                               case CMD_READ_DATA_AND_VIBRATE:
-                               //mem the vibration value for Large motor;
+                       if (pad[port].txData[0] == CMD_READ_DATA_AND_VIBRATE) {
+                               // vibration value for the Large motor
                                pad[port].Vib[1] = value;
 
-                               if (in_type[port] == PSE_PAD_TYPE_STANDARD &&
-                                       in_type[port] == PSE_PAD_TYPE_NEGCON)
-                                       break;
-
-                               //vibration
                                vibrate(port);
-                               break;
                        }
-               break;
+                       break;
        }
-       return buf[reqPos++];
 }
 
+static unsigned char PADpoll_(int port, unsigned char value, int *more_data) {
+       if (reqPos < sizeof(pad[port].txData))
+               pad[port].txData[reqPos] = value;
+
+       if (reqPos == 0 && value != 0x42 && in_type[port] != PSE_PAD_TYPE_ANALOGPAD)
+               respSize = 1;
+
+       switch (in_type[port]) {
+               case PSE_PAD_TYPE_ANALOGPAD:
+                       PADpoll_dualshock(port, value);
+                       break;
+               case PSE_PAD_TYPE_GUN:
+                       if (reqPos == 2)
+                               pl_gun_byte2(port, value);
+                       break;
+       }
+
+       *more_data = reqPos < respSize - 1;
+       if (reqPos >= respSize)
+               return 0xff; // no response/HiZ
+
+       return buf[reqPos++];
+}
 
-unsigned char _PADpollMultitap(int port, unsigned char value) {
+static unsigned char PADpollMultitap(int port, unsigned char value, int *more_data) {
+       *more_data = reqPos < respSize - 1;
        if (reqPos >= respSize) return 0xff;
        return bufMulti[reqPos++];
 }
@@ -820,12 +776,6 @@ unsigned char _PADpollMultitap(int port, unsigned char value) {
 unsigned char CALLBACK PAD1__startPoll(int pad) {
        reqPos = 0;
        // first call the pad provide if a multitap is connected between the psx and himself
-       if (multitap1 == -1) {
-               PadDataS padd;
-               padd.requestPadIndex = 0;
-               PAD1_readPort1(&padd);
-               multitap1 = padd.portMultitap;
-       }
        // just one pad is on port 1 : NO MULTITAP
        if (multitap1 == 0) {
                PadDataS padd;
@@ -843,15 +793,15 @@ unsigned char CALLBACK PAD1__startPoll(int pad) {
                _PADstartPollMultitap(padd);
        }
        //printf("\npad 1 : ");
-       return 0x00;
+       return 0xff;
 }
 
-unsigned char CALLBACK PAD1__poll(unsigned char value) {
+unsigned char CALLBACK PAD1__poll(unsigned char value, int *more_data) {
        char tmp;
        if (multitap1 == 1) {
-               tmp = _PADpollMultitap(0, value);
+               tmp = PADpollMultitap(0, value, more_data);
        } else {
-               tmp = _PADpoll(0, value);
+               tmp = PADpoll_(0, value, more_data);
        }
        //printf("%2x:%2x, ",value,tmp);
        return tmp;
@@ -876,6 +826,7 @@ long CALLBACK PAD1__keypressed() { return 0; }
        if (PAD1_##dest == NULL) PAD1_##dest = (PAD##dest) PAD1__##dest;
 
 static int LoadPAD1plugin(const char *PAD1dll) {
+       PadDataS padd;
        void *drv;
 
        hPAD1Driver = SysLoadLibrary(PAD1dll);
@@ -898,6 +849,10 @@ static int LoadPAD1plugin(const char *PAD1dll) {
        LoadPad1Sym0(poll, "PADpoll");
        LoadPad1SymN(setSensitive, "PADsetSensitive");
 
+       padd.requestPadIndex = 0;
+       PAD1_readPort1(&padd);
+       multitap1 = padd.portMultitap;
+
        return 0;
 }
 
@@ -913,14 +868,6 @@ unsigned char CALLBACK PAD2__startPoll(int pad) {
                pad_index = 0;
        }
 
-       //first call the pad provide if a multitap is connected between the psx and himself
-       if (multitap2 == -1) {
-               PadDataS padd;
-               padd.requestPadIndex = pad_index;
-               PAD2_readPort2(&padd);
-               multitap2 = padd.portMultitap;
-       }
-
        // just one pad is on port 1 : NO MULTITAP
        if (multitap2 == 0) {
                PadDataS padd;
@@ -938,15 +885,15 @@ unsigned char CALLBACK PAD2__startPoll(int pad) {
                _PADstartPollMultitap(padd);
        }
        //printf("\npad 2 : ");
-       return 0x00;
+       return 0xff;
 }
 
-unsigned char CALLBACK PAD2__poll(unsigned char value) {
+unsigned char CALLBACK PAD2__poll(unsigned char value, int *more_data) {
        char tmp;
        if (multitap2 == 2) {
-               tmp = _PADpollMultitap(1, value);
+               tmp = PADpollMultitap(1, value, more_data);
        } else {
-               tmp = _PADpoll(1, value);
+               tmp = PADpoll_(1, value, more_data);
        }
        //printf("%2x:%2x, ",value,tmp);
        return tmp;
@@ -969,6 +916,7 @@ long CALLBACK PAD2__keypressed() { return 0; }
        LoadSym(PAD2_##dest, PAD##dest, name, FALSE);
 
 static int LoadPAD2plugin(const char *PAD2dll) {
+       PadDataS padd;
        void *drv;
 
        hPAD2Driver = SysLoadLibrary(PAD2dll);
@@ -991,6 +939,10 @@ static int LoadPAD2plugin(const char *PAD2dll) {
        LoadPad2Sym0(poll, "PADpoll");
        LoadPad2SymN(setSensitive, "PADsetSensitive");
 
+       padd.requestPadIndex = 0;
+       PAD2_readPort2(&padd);
+       multitap2 = padd.portMultitap;
+
        return 0;
 }
 
@@ -1139,10 +1091,6 @@ static int LoadSIO1plugin(const char *SIO1dll) {
 
 #endif
 
-void CALLBACK clearDynarec(void) {
-       psxCpu->Reset();
-}
-
 int LoadPlugins() {
        int ret;
        char Plugin[MAXPATHLEN * 2];