typedef unsigned char  INT8  ;         /*   BYTE addressable             */

extern INT8 Reverse_Byte( INT8 );

static INT8 TB2M_Calculate_J1_CRC (INT8 *J1_Ptr)
{
   INT8 point_IN_1, point_IN_2, point_IN_3, point_C1, point_C2;
   INT8 point_C3, point_C4, point_C5, point_C6, point_C7, temp_byte;
   INT8 i, j;
   INT8 J1_CRC_Byte;

   /*
      Evaluate C1 to C7 before the Path Trace Frame is written to
      the ASIC. Use the Algorithm as specified in 1SAA 60590 AAG-BRA 8.2.1.

   IN1 ___                Schematic Of Algorithm.
   -->|   |
      | X |-----------------------------------------------------------------
   -->|___|                                     |                           |
  |          X = XOR                      ___   |                           |
  |          D = 1 Delay                 |   |<-- IN2                       |
  |                                    --| X |                              |
  |                                   |  |___|<--                           |
  |    ___     ___     ___     ___    |         |    ___     ___     ___    |
  |   |   |   |   |   |   |   |   |   |         |   |   |   |   |   |   |   |
  <---| D |<--| D |<--| D |<--| D |<---          <--| D |<--| D |<--| D |<--
  |   |___| | |___| | |___| | |___|  IN3          | |___| | |___| | |___| IN2
  |         |       |       |                     |       |       |
  C1        C2      C3      C4                    C5      C6      C7

   */

   J1_CRC_Byte= *J1_Ptr;

   point_IN_1 = 0x00;
   point_IN_2 = 0x00;
   point_IN_3 = 0x00;
   point_C1   = 0x00;
   point_C2   = 0x00;
   point_C3   = 0x00;
   point_C4   = 0x00;
   point_C5   = 0x00;
   point_C6   = 0x00;
   point_C7   = 0x00;
   /* Now write the calculated CRC-7 value into the First TX J1 byte */

   for ( i = 0; i < 16; i++ ) /* Loop all 16 bytes of the J1 frame. */
   {
      temp_byte = Reverse_Byte( *J1_Ptr++ );

      for ( j = 0; j < 8; j++ ) /* Loop all 8 bits of the byte. */
      {
         /* Next update values with next bit value */
         point_IN_1 = temp_byte & 0x01;
         point_IN_2 = point_IN_1 ^ point_C1;
         point_IN_3 = point_IN_2 ^ point_C5;

         /* First propagate previous delay gates - no change 1-st time round */
         point_C1   = point_C2;
         point_C2   = point_C3;
         point_C3   = point_C4;
         point_C4   = point_IN_3;
         point_C5   = point_C6;
         point_IN_3 = point_IN_2 ^ point_C5; /* shown for clarity, but overwritten */
         point_C6   = point_C7;
         point_C7   = point_IN_2;

         /* Shift byte for next bit to input */
         temp_byte = temp_byte >> 1;
      }
   }

   /* Now write the calculated CRC-7 value into the First TX J1 byte */
   J1_CRC_Byte += point_C7;
   J1_CRC_Byte += point_C6 << 1;
   J1_CRC_Byte += point_C5 << 2;
   J1_CRC_Byte += point_C4 << 3;
   J1_CRC_Byte += point_C3 << 4;
   J1_CRC_Byte += point_C2 << 5;
   J1_CRC_Byte += point_C1 << 6;

   return(J1_CRC_Byte);

}  /* end of TB2M_Calculate_J1_CRC */
