Adaptateur de manette pour ; - Master system - Megadrive - Saturn - Dreamcast - Nes - Snes - N64 - PS1 - PS2

Dependencies:   mbed mbed-MapleMini USBDevice_STM32F103 USBJoystick

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sega.cpp Source File

sega.cpp

00001 #include "MapleMini.h"
00002 #include "mbed.h"
00003 #include "nintendo.h"
00004  
00005 int *SegaMasterGenesisSaturn()    
00006 {                  // AUTRE   SATURN      SATURN 3D
00007     USB2.input();  // UP      DATA 1  0v  DATA 1  0v
00008     USB3.input();  // DOWN    DATA 0  0v  DATA 0  5v
00009     USB4.input();  // LEFT    SELECT0     SELECT
00010     USB5.input();  // RIGHT   DATA 3  5v  DATA 3  0v
00011     USB6.input();  // B       DATA 2  5v  DATA 2  0v
00012     USB7.output(); // SELECT  SELECT1     REQ
00013     USB8.input();  // C       --          ACK
00014     
00015     USB2.mode(PullUp);
00016     USB3.mode(PullUp);
00017     USB4.mode(PullUp);
00018     USB5.mode(PullUp);
00019     USB6.mode(PullUp);
00020     USB8.mode(PullUp);
00021     
00022     // Flag des boutton
00023     const int UP    = 1;
00024     const int DOWN  = 2;
00025     const int LEFT  = 4;
00026     const int RIGHT = 8;
00027     const int X     = 16;
00028     const int Z     = 32;
00029     const int C     = 64;
00030     const int B     = 128;
00031     const int Y     = 256;  
00032     const int A     = 512;
00033     const int START = 2048;
00034     const int GND   = 0;
00035     const int MODE  = 8192;
00036 
00037     // Tableau avec les pin et leur valeur
00038     // pin, Master System, 3 btn select 0V, 3 btn select 5V, 6 btn........
00039 /*    int buttonMap[6][11] = {
00040        //SMS    SMD    -      SMD6   -      -      -      -    -     -  -
00041        //0V     0V     5V     0      1      2      3      4    5     6  7
00042         {UP,    UP,    UP,    UP,    UP,    UP,    UP,    GND, Z,    0, 0}, //USB2
00043         {DOWN,  DOWN,  DOWN,  DOWN,  DOWN,  DOWN,  DOWN,  GND, Y,    0, 0}, //USB3
00044         {LEFT,  GND,   LEFT,  GND,   LEFT,  GND,   LEFT,  0,   X,    0, 0}, //USB4
00045         {RIGHT, GND,   RIGHT, GND,   RIGHT, GND,   RIGHT, 0,   MODE, 0, 0}, //USB5
00046         {B,     A,     B,     A,     B,     A,     B,     0,   0,    0, 0}, //USB6
00047         {C,     START, C,     START, C,     START, C,     0,   0,    0, 0}  //USB8
00048     };
00049     
00050     USB2 DATA 1 
00051     USB3 DATA 0
00052     USB4 SELECT0 SELECT
00053     USB5 DATA 3
00054     USB6 DATA 2
00055     USB7 SELECT1 REQ
00056     USB8         ACK
00057     
00058     */
00059     
00060     int buttons = 0;
00061     int analog_x = 0;
00062     int analog_y = 0;
00063     int analog_l = -128;
00064     int analog_r = -128;
00065     
00066     int controller = 0; // 0 master system, 3 btn megadrive, 6 btn megadrive
00067                         // 4 saturn,        5 saturn 3d
00068     int controllerOld = 0;
00069     
00070 
00071   USB7 = 0;
00072   wait_us(1700);
00073   for (int i = 0; i < 8; i++)
00074   {
00075     if (i % 2 == 0) //nombre pair
00076     {
00077       USB7 = 0;
00078       wait_us(20);
00079       if (i == 2 && !USB4 && !USB5)
00080       {
00081         controller = 3; // Magadrive
00082       }
00083       if (i == 4 && !USB2 && !USB3)
00084       {
00085         controller = 6; // Megadrive 6 btn
00086       }
00087     }
00088     else //nombre impair
00089     {
00090       USB7 = 1;
00091       wait_us(20);
00092       if (i == 7 && !USB2 && !USB3 && USB6) // && USB5 && USB6)
00093       {
00094         controller = 4; // Saturn
00095         break;
00096       }
00097       if (i == 7 && !USB2 && USB3 && !USB6) // && !USB5 && !USB6)
00098       {
00099         controller = 5; // Saturn 3D
00100         break;
00101       }
00102       if (i == 5 && controller == 6 && controllerOld == 0 && !USB5) // Utile ou pas utile ????
00103       {
00104         controller = 3; // Magadrive
00105       }  
00106     }
00107   }
00108   
00109   switch (controller) {
00110     case 0: // Master system
00111         // Lecture des boutons
00112         if(!USB2) buttons |= UP;
00113         if(!USB3) buttons |= DOWN;
00114         if(!USB4) buttons |= LEFT;
00115         if(!USB5) buttons |= RIGHT;
00116         if(!USB6) buttons |= B;
00117         if(!USB8) buttons |= C;
00118         
00119       //buttons |= 16384;
00120       break;
00121     case 3: // Megadrive 3 btns
00122         USB7 = 0;
00123         wait_us(1700);
00124         if(!USB2) buttons |= UP;
00125         if(!USB3) buttons |= DOWN;
00126      // if(!USB4) buttons |= GND;
00127      // if(!USB5) buttons |= GND;
00128         if(!USB6) buttons |= A;
00129         if(!USB8) buttons |= START;
00130         USB7 = 1;
00131         wait_us(20);
00132         if(!USB2) buttons |= UP;
00133         if(!USB3) buttons |= DOWN;
00134         if(!USB4) buttons |= LEFT;
00135         if(!USB5) buttons |= RIGHT;
00136         if(!USB6) buttons |= B;
00137         if(!USB8) buttons |= C; 
00138         
00139       //buttons |= 32768;
00140       //getMegadrive3Buttons();
00141       //return controller;
00142       break;
00143     case 4: // Saturn
00144         //buttons = 2;
00145         // Lecture des boutons
00146         USB4.output();
00147         
00148         USB4 = 1;
00149         USB7 = 1;
00150         wait_us(100);
00151         
00152         USB4 = 0;
00153         USB7 = 1;
00154         wait_us(30);
00155         
00156         if(!USB5) buttons |= RIGHT;
00157         if(!USB6) buttons |= LEFT;
00158         if(!USB2) buttons |= DOWN;
00159         if(!USB3) buttons |= UP;
00160         
00161         USB4 = 1;
00162         USB7 = 0;
00163         wait_us(30);
00164         
00165         if(!USB5) buttons |= START;
00166         if(!USB6) buttons |= A;
00167         if(!USB2) buttons |= C;
00168         if(!USB3) buttons |= B;
00169         
00170         USB4 = 0;
00171         USB7 = 0;
00172         wait_us(30);
00173         
00174         if(!USB5) analog_r = 127;  // 8192
00175         if(!USB6) buttons |= X;
00176         if(!USB2) buttons |= Y;
00177         if(!USB3) buttons |= Z;
00178         
00179         USB4 = 1;
00180         USB7 = 1;
00181         wait_us(30);
00182         
00183         if(!USB5) analog_l = 127; // 4096
00184         
00185         //buttons |= 131072;
00186         
00187       break;
00188     case 5: // Saturn 3D
00189         //buttons = 4;
00190         int bite = 0;
00191         bool analog = false;
00192         
00193         USB4.output();
00194         
00195         USB4 = 0;
00196         wait_us(50);
00197         USB7 = 0;
00198   
00199         for (int i = 0; i <= 100; i++) {
00200             if(!USB8) break;
00201             wait_us(5);
00202         }
00203         
00204         if(USB3) analog = true;
00205         
00206         USB7 = 1;
00207         
00208         for (int i = 0; i <= 100; i++) {
00209             if(USB8) break;
00210             wait_us(5);
00211         }
00212         
00213         if(USB3) bite |= 1;
00214         if(USB2) bite |= 2;
00215         if(USB6) bite |= 4;
00216         if(USB5) bite |= 8;
00217         
00218         USB7 = 0;
00219         
00220         for (int i = 0; i <= 100; i++) {
00221             if(!USB8) break;
00222             wait_us(5);
00223         }
00224         
00225         if(!USB3) buttons |= UP;
00226         if(!USB2) buttons |= DOWN;
00227         if(!USB6) buttons |= LEFT;
00228         if(!USB5) buttons |= RIGHT;
00229         
00230         USB7 = 1;
00231         
00232         for (int i = 0; i <= 100; i++) {
00233             if(USB8) break;
00234             wait_us(5);
00235         }
00236         
00237         if(!USB3) buttons |= B;
00238         if(!USB2) buttons |= C;
00239         if(!USB6) buttons |= A;
00240         if(!USB5) buttons |= START;
00241         
00242         USB7 = 0;
00243         
00244         for (int i = 0; i <= 100; i++) {
00245             if(!USB8) break;
00246             wait_us(5);
00247         }
00248         
00249         if(!USB3) buttons |= Z;
00250         if(!USB2) buttons |= Y;
00251         if(!USB6) buttons |= X;
00252         if(!USB5) analog_r = 127;
00253         
00254         USB7 = 1;
00255         
00256         for (int i = 0; i <= 100; i++) {
00257             if(USB8) break;
00258             wait_us(5);
00259         }
00260         
00261         /*if(!USB3) buttons |= 4096;
00262         if(!USB2) buttons |= 8192;
00263         if(!USB6) buttons |= 16384;*/
00264         if(!USB5) analog_l = 127;
00265         
00266         if(bite == 6 && analog){
00267             
00268             analog_l = 0;
00269             analog_r = 0;
00270             
00271             USB7 = 0;
00272             
00273             for (int i = 0; i <= 100; i++) {
00274                 if(!USB8) break;
00275                 wait_us(5);
00276             }
00277             
00278             if(USB3) analog_x |= 16;
00279             if(USB2) analog_x |= 32;
00280             if(USB6) analog_x |= 64;
00281             if(USB5) analog_x |= 128;
00282             
00283             USB7 = 1;
00284             
00285             for (int i = 0; i <= 100; i++) {
00286                 if(USB8) break;
00287                 wait_us(5);
00288             }
00289             
00290             if(USB3) analog_x |= 1;
00291             if(USB2) analog_x |= 2;
00292             if(USB6) analog_x |= 4;
00293             if(USB5) analog_x |= 8;
00294             
00295             USB7 = 0;
00296             
00297             for (int i = 0; i <= 100; i++) {
00298                 if(!USB8) break;
00299                 wait_us(5);
00300             }
00301             
00302             if(USB3) analog_y |= 16;
00303             if(USB2) analog_y |= 32;
00304             if(USB6) analog_y |= 64;
00305             if(USB5) analog_y |= 128;
00306             
00307             USB7 = 1;
00308             
00309             for (int i = 0; i <= 100; i++) {
00310                 if(USB8) break;
00311                 wait_us(5);
00312             }
00313             
00314             if(USB3) analog_y |= 1;
00315             if(USB2) analog_y |= 2;
00316             if(USB6) analog_y |= 4;
00317             if(USB5) analog_y |= 8;
00318             
00319             USB7 = 0;
00320             
00321             for (int i = 0; i <= 100; i++) {
00322                 if(!USB8) break;
00323                 wait_us(5);
00324             }
00325             
00326             if(USB3) analog_r |= 16;
00327             if(USB2) analog_r |= 32;
00328             if(USB6) analog_r |= 64;
00329             if(USB5) analog_r |= 128;
00330             
00331             USB7 = 1;
00332             
00333             for (int i = 0; i <= 100; i++) {
00334                 if(USB8) break;
00335                 wait_us(5);
00336             }
00337             
00338             if(USB3) analog_r |= 1;
00339             if(USB2) analog_r |= 2;
00340             if(USB6) analog_r |= 4;
00341             if(USB5) analog_r |= 8;
00342             
00343             USB7 = 0;
00344             
00345             for (int i = 0; i <= 100; i++) {
00346                 if(!USB8) break;
00347                 wait_us(5);
00348             }
00349             
00350             if(USB3) analog_l |= 16;
00351             if(USB2) analog_l |= 32;
00352             if(USB6) analog_l |= 64;
00353             if(USB5) analog_l |= 128;
00354             
00355             USB7 = 1;
00356             
00357             for (int i = 0; i <= 100; i++) {
00358                 if(USB8) break;
00359                 wait_us(5);
00360             }
00361             
00362             if(USB3) analog_l |= 1;
00363             if(USB2) analog_l |= 2;
00364             if(USB6) analog_l |= 4;
00365             if(USB5) analog_l |= 8;
00366             
00367             analog_x = analog_x - 128;
00368             analog_y = analog_y - 128;
00369             analog_l = analog_l - 128;
00370             analog_r = analog_r - 128;
00371         
00372         }
00373         
00374         //buttons |= 262144;
00375         
00376       break;
00377     case 6: // Megadrive 6 btns
00378         USB7 = 0;
00379         wait_us(1700);
00380         
00381         USB7 = 1;
00382         wait_us(20);
00383         
00384         USB7 = 0;
00385         wait_us(20);
00386         if(!USB2) buttons |= UP;
00387         if(!USB3) buttons |= DOWN;
00388      // if(!USB4) buttons |= GND;
00389      // if(!USB5) buttons |= GND;
00390         if(!USB6) buttons |= A;
00391         if(!USB8) buttons |= START;
00392         USB7 = 1;
00393         wait_us(20);
00394         if(!USB2) buttons |= UP;
00395         if(!USB3) buttons |= DOWN;
00396         if(!USB4) buttons |= LEFT;
00397         if(!USB5) buttons |= RIGHT;
00398         if(!USB6) buttons |= B;
00399         if(!USB8) buttons |= C; 
00400         USB7 = 0;
00401         wait_us(20);
00402      // if(!USB2) buttons |= GND;
00403      // if(!USB3) buttons |= GND;
00404         USB7 = 1;
00405         wait_us(20);
00406         if(!USB2) buttons |= Z;
00407         if(!USB3) buttons |= Y;
00408         if(!USB4) buttons |= X;
00409         if(!USB5) buttons |= MODE;
00410         
00411       //buttons |= 65536; // 15
00412       //getMegadrive6Buttons();
00413       //return controller;
00414       break;
00415   }
00416   
00417     controllerOld = controller;
00418     int array[] = {analog_r, analog_l, analog_x, analog_y, buttons, 0};
00419     return array;
00420 }
00421 
00422 int *SegaDreamcast()    
00423 {
00424     int array[] = {0, 0, 0, 0, 0, 0};
00425     return array;
00426 }