home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 571.lha / AmigaPowerGloveHi-ResHack / glovehack / glove.c < prev    next >
C/C++ Source or Header  |  1991-10-12  |  13KB  |  789 lines

  1. /**********************************************************************
  2.  
  3.     This ugly hack is based on the ATARI 1040ST power glove
  4.     hack by  manfredo@opal.cs.tu-berlin.de.
  5.  
  6.      This program is without any WARRANTY use at your OWN risk
  7.         @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  8.  
  9.     This Amiga hack was done by Alan Bland.  It is not directly
  10.     compatible with the AC Tech hack, the BYTE hack, nor the
  11.     ATARI 1040ST hack, but you should be able to modify the
  12.     code to work with any of those hacks.
  13.  
  14.     The Amiga code is ugly, as was the original ST code.  The
  15.     parallel port hardware is accessed directly without knowledge
  16.     of the operating system.
  17.  
  18.     Connect NINTENDO POWERGLOVE to Amiga parallel port
  19.           ------
  20.          /   1 |
  21.     | 5  2 |
  22.     | 6  3 |
  23.     | 7  4 |
  24.     --------
  25.  
  26.     GLOVE                       AMIGA
  27.     =====                       =====
  28.  
  29.     GND pin1                    pin18 parallel port
  30.  
  31.     clock pin2                  pin2  parallel port
  32.  
  33.      latch pin3                  pin3  parallel port
  34.  
  35.     data pin4                   pin4  parallel port
  36.  
  37.     +5V pin7                    pin14 power +5V
  38.  
  39.  
  40.     Datapacket: (12 bytes)
  41.  
  42.     1.    2.    3.    4.    5.    6.    7.    8.    9.    10.   11.    12.
  43.  
  44.     A0    X     Y     Z    rot  finger  keys  00    00    3F    FF     FF
  45.  
  46.  
  47. **********************************************************************/
  48.     
  49. #include <stdio.h>
  50. #include <hardware/cia.h>
  51.  
  52. extern struct CIA far ciaa;
  53.  
  54. /* bits from parallel port -- Alan's hack */
  55. #define        GDATA           0x04    /* glove data in */
  56. #define        GLATCH          0x02    /* glove latch out */
  57. #define        GCLOCK          0x01    /* glove clock out */
  58. #define        GCLOLAT         (GLATCH|GCLOCK) /* latch and clock */
  59.  
  60. #define getbit()      (ciaa.ciaprb & GDATA) >> 2
  61. #define initport()    ciaa.ciaddrb = GCLOLAT
  62.  
  63. /* delays in microseconds */
  64. #define     D2BYTES      96
  65. #define     D2BITS       22
  66. #define     D2SLOW       14720
  67.  
  68. #define     C0L0()       ciaa.ciaprb = 0        /* clock 0 latch 0 */
  69. #define     C0L1()       ciaa.ciaprb = GLATCH   /* clock 0 latch 1 */
  70. #define     C1L0()       ciaa.ciaprb = GCLOCK   /* clock 1 latch 0 */
  71. #define     C1L1()       ciaa.ciaprb = GCLOLAT  /* clock 1 latch 1 */
  72.  
  73. #define     setporta()    delay(3)
  74. #define     setportb()  delay(3)
  75.  
  76. void Hires (void);
  77. unsigned char getbyte (void);
  78.  
  79. /* convert microseconds to cia ticks */
  80. #define delay(usec) timersleep((usec*1397)/1000)
  81.  
  82. int control_c()
  83. {
  84.     closetimer();
  85.     printf("<<goodbye>>\n");
  86.     return 1; /* causes exit */
  87. }
  88.  
  89. void main ()
  90. {
  91.     unsigned char buf[12];
  92.     register unsigned char *bp;
  93.  
  94.     opentimer();
  95.     onbreak(control_c);
  96.  
  97.     Hires (); /* set PG into 'hires' mode */
  98.  
  99.     printf(" Press ^C to stop\n\nx   y   z       rot   finger button\n");
  100.     for ( ; ; ) /* read 12 byte packets */
  101.     {
  102.         bp = buf;
  103.         *bp++ = getbyte ();
  104.         delay (D2BYTES);
  105.         *bp++ = getbyte ();
  106.         delay (D2BYTES);
  107.         *bp++ = getbyte ();
  108.         delay (D2BYTES);
  109.         *bp++ = getbyte ();
  110.         delay (D2BYTES);
  111.         *bp++ = getbyte ();
  112.         delay (D2BYTES);
  113.         *bp++ = getbyte ();
  114.         delay (D2BYTES);
  115.         *bp++ = getbyte ();
  116.         delay (D2BYTES);
  117.         *bp++ = getbyte ();
  118.         delay (D2SLOW);
  119.         *bp++ = getbyte ();
  120.         delay (D2SLOW);
  121.         *bp++ = getbyte ();
  122.         delay (D2SLOW);
  123.         *bp++ = getbyte ();
  124.         delay (D2SLOW);
  125.         *bp++ = getbyte ();
  126.         delay (D2SLOW);
  127.  
  128.         /* Glove packet isn't quite as described above */
  129.         /* Let's see if we can figure it out */
  130.         {
  131.         int i,n;
  132.  
  133.         /* look for FF FF A0 */
  134.         n = -1;
  135.         for (i=0; i<12; ++i) {
  136.             if (buf[i] == (unsigned char)0xff &&
  137.                 buf[(i+1)%12] == (unsigned char)0xff &&
  138.                 buf[(i+2)%12] == (unsigned char)0xa0) {
  139.  
  140.                 /* yah! */
  141.                 n = (i+3)%12;
  142.  
  143.                 printf("%-3d %-3d %-3d     %-3d     %-2x     %-2x\n",
  144.                     buf[n], buf[(n+1)%12], buf[(n+2)%12],
  145.                     buf[(n+3)%12],
  146.                     buf[(n+4)%12],
  147.                     buf[(n+5)%12]);
  148.                 break;
  149.             }
  150.         }
  151.         if (n < 0) printf("\033[K\n");
  152.         printf ("Glove %-2x %-2x %-2x %-2x %-2x %-2x %-2x %-2x %-2x %-2x %-2x %-2x\r\033[A",
  153.                 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
  154.                 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
  155.         }
  156.     }
  157. }
  158.  
  159. unsigned char getbyte ()
  160. {
  161.     register unsigned Glov = 0;    
  162.  
  163.     /* prepare port b as output port */
  164.     setportb ();
  165.  
  166.         /* generate a reset (latch) pulse */
  167.     C1L0 ();
  168.     C1L1 ();
  169.     delay(5); /* 5 us delay */
  170.     C1L0 ();
  171.  
  172.     /* configure port a as input */
  173.     setporta ();
  174.  
  175.     /* read a bit */
  176.     Glov <<= 1;
  177.     Glov |= getbit();
  178.  
  179.     /* prepare port b as output port */
  180.     setportb ();
  181.  
  182.     /* generate a clock pulse */
  183.     C0L0 ();
  184.     C1L0 ();
  185.     
  186.     /* configure port a as input */
  187.     setporta ();
  188.  
  189.     /* read a bit */
  190.     Glov <<= 1;
  191.     Glov |= getbit();
  192.  
  193.     /* prepare port b as output port */
  194.     setportb ();
  195.  
  196.     /* generate a clock pulse */
  197.     C0L0 ();
  198.     C1L0 ();
  199.     
  200.     /* configure port a as input */
  201.     setporta ();
  202.  
  203.     /* read a bit */
  204.     Glov <<= 1;
  205.     Glov |= getbit();
  206.  
  207.     /* prepare port b as output port */
  208.     setportb ();
  209.  
  210.     /* generate a clock pulse */
  211.     C0L0 ();
  212.     C1L0 ();
  213.     
  214.     /* configure port a as input */
  215.     setporta ();
  216.  
  217.     /* read a bit */
  218.     Glov <<= 1;
  219.     Glov |= getbit();
  220.  
  221.     /* prepare port b as output port */
  222.     setportb ();
  223.  
  224.     /* generate a clock pulse */
  225.     C0L0 ();
  226.     C1L0 ();
  227.     
  228.     /* configure port a as input */
  229.     setporta ();
  230.  
  231.     /* read a bit */
  232.     Glov <<= 1;
  233.     Glov |= getbit();
  234.  
  235.     /* prepare port b as output port */
  236.     setportb ();
  237.  
  238.     /* generate a clock pulse */
  239.     C0L0 ();
  240.     C1L0 ();
  241.     
  242.     /* configure port a as input */
  243.     setporta ();
  244.  
  245.     /* read a bit */
  246.     Glov <<= 1;
  247.     Glov |= getbit();
  248.  
  249.     /* prepare port b as output port */
  250.     setportb ();
  251.  
  252.     /* generate a clock pulse */
  253.     C0L0 ();
  254.     C1L0 ();
  255.     
  256.     /* configure port a as input */
  257.     setporta ();
  258.  
  259.     /* read a bit */
  260.     Glov <<= 1;
  261.     Glov |= getbit();
  262.  
  263.     /* prepare port b as output port */
  264.     setportb ();
  265.  
  266.     /* generate a clock pulse */
  267.     C0L0 ();
  268.     C1L0 ();
  269.     
  270.     /* configure port a as input */
  271.     setporta ();
  272.  
  273.     /* read a bit */
  274.     Glov <<= 1;
  275.     Glov |= getbit();
  276.  
  277.     /* prepare port b as output port */
  278.     setportb ();
  279.  
  280.     /* generate a clock pulse */
  281.     C0L0 ();
  282.     C1L0 ();
  283.  
  284.     return (unsigned char) Glov;  /* return the byte */
  285. }
  286.  
  287.  
  288. void Hires ()
  289. {
  290.     register unsigned char Glov = 0;
  291.  
  292.     /* initialize hardware interface */
  293.     initport();
  294.  
  295.     /* read 4 bits from glove */
  296.     setportb ();
  297.  
  298.         /* generate a reset (latch) pulse */
  299.     C1L0 ();
  300.     C1L1 ();
  301.     delay(5);  /* 5 us delay */
  302.     C1L0 ();
  303.  
  304.     /* configure port a as input */
  305.     setporta ();
  306.  
  307.     /* read a bit */
  308.     Glov <<= 1;
  309.     Glov |= getbit();
  310.  
  311.     /* prepare port b as output port */
  312.     setportb ();
  313.  
  314.     /* generate a clock pulse */
  315.     C0L0 ();
  316.     C1L0 ();
  317.     
  318.     /* configure port a as input */
  319.     setporta ();
  320.  
  321.     /* read a bit */
  322.     Glov <<= 1;
  323.     Glov |= getbit();
  324.  
  325.     /* prepare port b as output port */
  326.     setportb ();
  327.  
  328.     /* generate a clock pulse */
  329.     C0L0 ();
  330.     C1L0 ();
  331.     
  332.     /* configure port a as input */
  333.     setporta ();
  334.  
  335.     /* read a bit */
  336.     Glov <<= 1;
  337.     Glov |= getbit();
  338.  
  339.     /* prepare port b as output port */
  340.     setportb ();
  341.  
  342.     /* generate a clock pulse */
  343.     C0L0 ();
  344.     C1L0 ();
  345.     
  346.     /* configure port a as input */
  347.     setporta ();
  348.  
  349.     /* read a bit */
  350.     Glov <<= 1;
  351.     Glov |= getbit();
  352.  
  353.     /* prepare port b as output port */
  354.     setportb ();
  355.  
  356.     /* generate a clock pulse */
  357.     C0L0 ();
  358.     C1L0 ();
  359.     
  360.     /* end of read 4 bits */
  361.  
  362.     /* prepare port b as output port */
  363.     setportb ();
  364.  
  365.     C1L0 ();
  366.     delay(7212);
  367. /*    delay (16950);  /* 7212 us delay */
  368.  
  369.     setportb ();
  370.  
  371.     C1L1 ();
  372.     delay(2260);
  373. /*    delay (4750);   /* 2260 us delay */
  374.  
  375.     /* prepare port b as output port */
  376.     setportb ();
  377.  
  378.     C1L0 ();    /* Start of 1. Byte */
  379.     C0L0 ();
  380.     C1L0 ();
  381.     delay (D2BITS);
  382.  
  383.     /* prepare port b as output port */
  384.     setportb ();
  385.  
  386.     C0L0 ();
  387.     C1L0 ();
  388.     delay (D2BITS);
  389.     /* prepare port b as output port */
  390.     setportb ();
  391.  
  392.     C0L0 ();
  393.     C1L0 ();
  394.     delay (D2BITS);    
  395.  
  396.     /* prepare port b as output port */
  397.     setportb ();
  398.  
  399.     C0L0 ();
  400.     C1L0 ();
  401.     delay (D2BITS);
  402.  
  403.     /* prepare port b as output port */
  404.     setportb ();
  405.  
  406.     C0L0 ();
  407.     C1L0 ();
  408.     delay (D2BITS);
  409.  
  410.     /* prepare port b as output port */
  411.     setportb ();
  412.  
  413.     C1L1 ();
  414.     C0L1 ();
  415.     C1L1 ();
  416.     delay (D2BITS);
  417.     
  418.     /* prepare port b as output port */
  419.     setportb ();
  420.  
  421.     C0L1 ();
  422.     C1L1 ();
  423.     delay (D2BITS);    
  424.  
  425.     /* prepare port b as output port */
  426.     setportb ();
  427.  
  428.     C1L0 ();
  429.     C0L0 ();
  430.     C1L0 ();
  431.     delay (D2BYTES);        
  432.  
  433.     /* prepare port b as output port */
  434.     setportb ();
  435.  
  436.     C1L1 ();    /* Start of 2. Byte */
  437.     C0L1 ();
  438.     C1L1 ();
  439.     delay (D2BITS);
  440.  
  441.     /* prepare port b as output port */
  442.     setportb ();
  443.  
  444.     C0L1 ();
  445.     C1L1 ();
  446.     delay (D2BITS);
  447.  
  448.     /* prepare port b as output port */
  449.     setportb ();
  450.  
  451.     C1L0 ();
  452.     C0L0 ();
  453.     C1L0 ();
  454.     delay (D2BITS);
  455.  
  456.     /* prepare port b as output port */
  457.     setportb ();
  458.  
  459.     C0L0 ();
  460.     C1L0 ();
  461.     delay (D2BITS);
  462.  
  463.     /* prepare port b as output port */
  464.     setportb ();
  465.  
  466.     C0L0 ();
  467.     C1L0 ();
  468.     delay (D2BITS);
  469.  
  470.     /* prepare port b as output port */
  471.     setportb ();
  472.  
  473.     C0L0 ();
  474.     C1L0 ();
  475.     delay (D2BITS);
  476.  
  477.     /* prepare port b as output port */
  478.     setportb ();
  479.  
  480.     C0L0 ();
  481.     C1L0 ();
  482.     delay (D2BITS);
  483.  
  484.     /* prepare port b as output port */
  485.     setportb ();
  486.  
  487.     C1L1 ();
  488.     C0L1 ();
  489.     C1L1 ();
  490.     delay (D2BYTES);
  491.  
  492.     /* prepare port b as output port */
  493.     setportb ();
  494.  
  495.     C1L0 ();    /* Start of 3. Byte */
  496.     C0L0 ();
  497.     C1L0 ();
  498.     delay (D2BITS);
  499.  
  500.     /* prepare port b as output port */
  501.     setportb ();
  502.  
  503.     C0L0 ();
  504.     C1L0 ();
  505.     delay (D2BITS);    
  506.  
  507.     /* prepare port b as output port */
  508.     setportb ();
  509.  
  510.     C0L0 ();
  511.     C1L0 ();
  512.     delay (D2BITS);    
  513.  
  514.     /* prepare port b as output port */
  515.     setportb ();
  516.  
  517.     C0L0 ();
  518.     C1L0 ();
  519.     delay (D2BITS);    
  520.  
  521.     /* prepare port b as output port */
  522.     setportb ();
  523.  
  524.     C1L1 ();
  525.     C0L1 ();
  526.     C1L1 ();
  527.     delay (D2BITS);
  528.  
  529.     /* prepare port b as output port */
  530.     setportb ();
  531.  
  532.     C1L0 ();
  533.     C0L0 ();
  534.     C1L0 ();
  535.     delay (D2BITS);
  536.  
  537.     /* prepare port b as output port */
  538.     setportb ();
  539.  
  540.     C0L0 ();
  541.     C1L0 ();
  542.     delay (D2BITS);
  543.  
  544.     /* prepare port b as output port */
  545.     setportb ();
  546.  
  547.     C0L0 ();
  548.     C1L0 ();
  549.     delay (D2BYTES);
  550.  
  551.     /* prepare port b as output port */
  552.     setportb ();
  553.  
  554.     C0L0 ();    /* start of 4. byte */
  555.     C1L0 ();
  556.     delay (D2BITS);
  557.  
  558.     /* prepare port b as output port */
  559.     setportb ();
  560.  
  561.     C0L0 ();
  562.     C1L0 ();
  563.     delay (D2BITS);
  564.  
  565.     /* prepare port b as output port */
  566.     setportb ();
  567.  
  568.     C0L0 ();
  569.     C1L0 ();
  570.     delay (D2BITS);
  571.  
  572.     /* prepare port b as output port */
  573.     setportb ();
  574.  
  575.     C0L0 ();
  576.     C1L0 ();
  577.     delay (D2BITS);
  578.  
  579.     /* prepare port b as output port */
  580.     setportb ();
  581.  
  582.     C0L0 ();
  583.     C1L0 ();
  584.     delay (D2BITS);
  585.  
  586.     /* prepare port b as output port */
  587.     setportb ();
  588.  
  589.     C0L0 ();
  590.     C1L0 ();
  591.     delay (D2BITS);
  592.  
  593.     /* prepare port b as output port */
  594.     setportb ();
  595.  
  596.     C0L0 ();
  597.     C1L0 ();
  598.     delay (D2BITS);
  599.  
  600.     /* prepare port b as output port */
  601.     setportb ();
  602.  
  603.     C0L0 ();
  604.     C1L0 ();
  605.     delay (D2BYTES);
  606.  
  607.     /* prepare port b as output port */
  608.     setportb ();
  609.  
  610.     C0L0 ();    /* start of 5. byte */
  611.     C1L0 ();
  612.     delay (D2BITS);
  613.  
  614.     /* prepare port b as output port */
  615.     setportb ();
  616.  
  617.     C0L0 ();
  618.     C1L0 ();
  619.     delay (D2BITS);
  620.  
  621.     /* prepare port b as output port */
  622.     setportb ();
  623.  
  624.     C0L0 ();
  625.     C1L0 ();
  626.     delay (D2BITS);
  627.  
  628.     /* prepare port b as output port */
  629.     setportb ();
  630.  
  631.     C0L0 ();
  632.     C1L0 ();
  633.     delay (D2BITS);
  634.  
  635.     /* prepare port b as output port */
  636.     setportb ();
  637.  
  638.     C0L0 ();
  639.     C1L0 ();
  640.     delay (D2BITS);
  641.  
  642.     /* prepare port b as output port */
  643.     setportb ();
  644.  
  645.     C0L0 ();
  646.     C1L0 ();
  647.     delay (D2BITS);
  648.  
  649.     /* prepare port b as output port */
  650.     setportb ();
  651.  
  652.     C1L1 ();
  653.     C0L1 ();
  654.     C1L1 ();
  655.     delay (D2BITS);
  656.  
  657.     /* prepare port b as output port */
  658.     setportb ();
  659.  
  660.     C1L0 ();
  661.     C0L0 ();
  662.     C1L0 ();
  663.     delay (D2BYTES);
  664.  
  665.     /* prepare port b as output port */
  666.     setportb ();
  667.  
  668.     C1L1 ();    /* start of 6. byte */
  669.     C0L1 ();
  670.     C1L1 ();
  671.     delay (D2BITS);
  672.  
  673.     /* prepare port b as output port */
  674.     setportb ();
  675.  
  676.     C0L1 ();
  677.     C1L1 ();
  678.     delay (D2BITS);
  679.  
  680.     /* prepare port b as output port */
  681.     setportb ();
  682.  
  683.     C0L1 ();
  684.     C1L1 ();
  685.     delay (D2BITS);
  686.  
  687.     /* prepare port b as output port */
  688.     setportb ();
  689.  
  690.     C0L1 ();
  691.     C1L1 ();
  692.     delay (D2BITS);
  693.  
  694.     /* prepare port b as output port */
  695.     setportb ();
  696.  
  697.     C0L1 ();
  698.     C1L1 ();
  699.     delay (D2BITS);
  700.  
  701.     /* prepare port b as output port */
  702.     setportb ();
  703.  
  704.     C0L1 ();
  705.     C1L1 ();
  706.     delay (D2BITS);
  707.  
  708.     /* prepare port b as output port */
  709.     setportb ();
  710.  
  711.     C0L1 ();
  712.     C1L1 ();
  713.     delay (D2BITS);
  714.  
  715.     /* prepare port b as output port */
  716.     setportb ();
  717.  
  718.     C0L1 ();
  719.     C1L1 ();
  720.     delay (D2BYTES);
  721.  
  722.     /* prepare port b as output port */
  723.     setportb ();
  724.  
  725.     C1L0 ();    /* start of 7. byte */
  726.     C0L0 ();
  727.     C1L0 ();
  728.     delay (D2BITS);
  729.  
  730.     /* prepare port b as output port */
  731.     setportb ();
  732.  
  733.     C0L0 ();
  734.     C1L0 ();
  735.     delay (D2BITS);
  736.  
  737.     /* prepare port b as output port */
  738.     setportb ();
  739.  
  740.     C0L0 ();
  741.     C1L0 ();
  742.     delay (D2BITS);
  743.  
  744.     /* prepare port b as output port */
  745.     setportb ();
  746.  
  747.     C0L0 ();
  748.     C1L0 ();
  749.     delay (D2BITS);
  750.  
  751.     /* prepare port b as output port */
  752.     setportb ();
  753.  
  754.     C0L0 ();
  755.     C1L0 ();
  756.     delay (D2BITS);
  757.  
  758.     /* prepare port b as output port */
  759.     setportb ();
  760.  
  761.     C0L0 ();
  762.     C1L0 ();
  763.     delay (D2BITS);
  764.  
  765.     /* prepare port b as output port */
  766.     setportb ();
  767.  
  768.     C0L0 ();
  769.     C1L0 ();
  770.     delay (D2BITS);
  771.  
  772.     /* prepare port b as output port */
  773.     setportb ();
  774.  
  775.     C1L1 ();
  776.     C0L1 ();
  777.     C1L1 ();
  778.     delay(892);
  779. /*    delay (1090); /* 892 us delay (end of 7. byte) */
  780.  
  781.     /* prepare port b as output port */
  782.     setportb ();
  783.  
  784.     C1L0 ();
  785.     delay(50000);
  786. /*    delay (60000);   /* some time for the glove controller
  787.                         to relax */
  788. }
  789.