home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FPGAWKII.ZIP / ADDTEST.C < prev    next >
C/C++ Source or Header  |  1995-04-10  |  4KB  |  111 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "jtag.h"
  4.  
  5. /*
  6.  * RTN ShiftOut: this subroutine will shift an n-bit number into the
  7.  *               shift register we built to hold the inputs to the
  8.  *               adder.
  9.  */
  10. #define SHIFT 2  /* port bit that enables the shifting of the input reg. */
  11. #define IN    3  /* port bit through which input bits are passed */
  12. void ShiftOut( int n, bitstream* s )
  13.     {
  14.     int i;
  15.  
  16.     /* The TMS port bit is used as the clock for the input shift
  17.        register. We lower TMS before raising the enable control bit
  18.        so we don't generate some spurious noise on the shift register
  19.        clock.  If that happened, we would get all kinds of strange
  20.        stuff in the shift register. */
  21.     SetTMSValue(0);         /* first lower the TMS bit */
  22.     SetPortBit( SHIFT, 1 ); /* now enable the input shift register */
  23.  
  24.     /* now shift bits into the register, starting with the MSB */
  25.     for( i=n-1; i>=0; i-- )
  26.         {
  27.         SetTMSValue(0);                /* lower the clock */
  28.         SetPortBit( IN, GetBit(s,i) ); /* set the input bit */
  29.         SetTMSValue(1);           /* raise the clock to shift in the bit */
  30.         }
  31.  
  32.     /* Now lower the TMS bit and disable the shifting of the register
  33.        by lowering the shift control line.  We use this sequence for
  34.        the same reason: eliminate spurious clock pulses. */
  35.     SetTMSValue(0);         /* first lower the TMS bit */
  36.     SetPortBit( SHIFT, 0 ); /* disable the input shift register */
  37.  
  38.     /* finally, force the TMS line high since this is its normal state */
  39.     SetTMSValue(1);
  40.     }
  41.  
  42. /*
  43.  * RTN DoAdd: this subroutine shifts two 8-bit numbers into the shift
  44.  *            register which feeds the 8-bit adder and then uses the
  45.  *            SAMPLE/PRELOAD JTAG instruction to read out the result.
  46.  */
  47. bitstream DoAdd( bitstream a, bitstream b )
  48.     {
  49.     bitstream sum;
  50.     bitstream *out = AllocBits(264);  /* allocate storage for the
  51.                                          EPX780 BSDR storage */
  52.  
  53.     /* shift the operands into the input shift register */
  54.     ShiftOut( 8, &b );
  55.     ShiftOut( 8, &a );
  56.  
  57.     /* now use the SAMPLE/PRELOAD instruction to get the adder output */
  58.     EPX780SamplePreload( NULL, out );
  59.  
  60.     /* now look at the bits in the BSDR storage that correspond to the
  61.        output bits of the adder.  We have to pull these out of the
  62.        ADD.RPT file by looking for the macrocells associated with
  63.        sum0, sum1, ... , sum7.  Take each output and put it in the
  64.        right place for the final result. */
  65.     sum = 0;
  66.     SetBit( &sum, 0, GetMacrocellOutput(out,29) );
  67.     SetBit( &sum, 1, GetMacrocellOutput(out,30) );
  68.     SetBit( &sum, 2, GetMacrocellOutput(out,35) );
  69.     SetBit( &sum, 3, GetMacrocellOutput(out,40) );
  70.     SetBit( &sum, 4, GetMacrocellOutput(out,20) );
  71.     SetBit( &sum, 5, GetMacrocellOutput(out,22) );
  72.     SetBit( &sum, 6, GetMacrocellOutput(out,24) );
  73.     SetBit( &sum, 7, GetMacrocellOutput(out,45) );
  74.  
  75.     /* free the storage used to hold the BSDR bits */
  76.     FreeBits( out );
  77.  
  78.     /* return the sum output by the adder */
  79.     return sum;
  80.     }
  81.  
  82.  
  83. main()
  84.     {
  85.     bitstream a, b, sum;
  86.  
  87.     SelectPort(1);   /* we'll assume the TAP is on printer port #1 */
  88.     JTAGPortInit();  /* initialize the port */
  89.     HardTAPReset();  /* reset the TAP controller */
  90.  
  91.     /* now add all possible 8-bit operands and make sure the sum
  92.        has the correct value */
  93.     for( a=0; a<256; a++ )
  94.         for( b=0; b<256; b++ )
  95.             {
  96.             sum = DoAdd(a,b);  /* do the sum in the FPGA and return
  97.                                   the result */
  98.  
  99.             /* now check the result with the one we calculate using the PC.
  100.                remember to use only the lower 8-bits of the sum from the
  101.                PC since the FPGA adder is limited to 8 bits. */
  102.             if( sum != ((a+b) & 0xff) )
  103.                 {
  104.                 printf( "ERROR: %ld + %ld != %ld\n", a, b, sum );
  105.                 exit(0);
  106.                 }
  107.             else
  108.                 printf( "%ld + %ld = %ld\n", a, b, sum );
  109.             }
  110.     }
  111.