Dependencies:   mbed

Committer:
monpjc
Date:
Sat Jun 30 13:17:05 2012 +0000
Revision:
0:1be76329b246
removed returns for debug and corrected usages of fp in main()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
monpjc 0:1be76329b246 1 /*******************************************************/
monpjc 0:1be76329b246 2 /* file: lenval.c */
monpjc 0:1be76329b246 3 /* abstract: This file contains routines for using */
monpjc 0:1be76329b246 4 /* the lenVal data structure. */
monpjc 0:1be76329b246 5 /*******************************************************/
monpjc 0:1be76329b246 6 #include "lenval.h"
monpjc 0:1be76329b246 7 #include "ports.h"
monpjc 0:1be76329b246 8
monpjc 0:1be76329b246 9 /*****************************************************************************
monpjc 0:1be76329b246 10 * Function: value
monpjc 0:1be76329b246 11 * Description: Extract the long value from the lenval array.
monpjc 0:1be76329b246 12 * Parameters: plvValue - ptr to lenval.
monpjc 0:1be76329b246 13 * Returns: long - the extracted value.
monpjc 0:1be76329b246 14 *****************************************************************************/
monpjc 0:1be76329b246 15 long value( lenVal* plvValue )
monpjc 0:1be76329b246 16 {
monpjc 0:1be76329b246 17 long lValue; /* result to hold the accumulated result */
monpjc 0:1be76329b246 18 short sIndex;
monpjc 0:1be76329b246 19
monpjc 0:1be76329b246 20 lValue = 0;
monpjc 0:1be76329b246 21 for ( sIndex = 0; sIndex < plvValue->len ; ++sIndex )
monpjc 0:1be76329b246 22 {
monpjc 0:1be76329b246 23 lValue <<= 8; /* shift the accumulated result */
monpjc 0:1be76329b246 24 lValue |= plvValue->val[ sIndex]; /* get the last byte first */
monpjc 0:1be76329b246 25 }
monpjc 0:1be76329b246 26
monpjc 0:1be76329b246 27 return( lValue );
monpjc 0:1be76329b246 28 }
monpjc 0:1be76329b246 29
monpjc 0:1be76329b246 30 /*****************************************************************************
monpjc 0:1be76329b246 31 * Function: initLenVal
monpjc 0:1be76329b246 32 * Description: Initialize the lenval array with the given value.
monpjc 0:1be76329b246 33 * Assumes lValue is less than 256.
monpjc 0:1be76329b246 34 * Parameters: plv - ptr to lenval.
monpjc 0:1be76329b246 35 * lValue - the value to set.
monpjc 0:1be76329b246 36 * Returns: void.
monpjc 0:1be76329b246 37 *****************************************************************************/
monpjc 0:1be76329b246 38 void initLenVal( lenVal* plv,
monpjc 0:1be76329b246 39 long lValue )
monpjc 0:1be76329b246 40 {
monpjc 0:1be76329b246 41 plv->len = 1;
monpjc 0:1be76329b246 42 plv->val[0] = (unsigned char)lValue;
monpjc 0:1be76329b246 43 }
monpjc 0:1be76329b246 44
monpjc 0:1be76329b246 45 /*****************************************************************************
monpjc 0:1be76329b246 46 * Function: EqualLenVal
monpjc 0:1be76329b246 47 * Description: Compare two lenval arrays with an optional mask.
monpjc 0:1be76329b246 48 * Parameters: plvTdoExpected - ptr to lenval #1.
monpjc 0:1be76329b246 49 * plvTdoCaptured - ptr to lenval #2.
monpjc 0:1be76329b246 50 * plvTdoMask - optional ptr to mask (=0 if no mask).
monpjc 0:1be76329b246 51 * Returns: short - 0 = mismatch; 1 = equal.
monpjc 0:1be76329b246 52 *****************************************************************************/
monpjc 0:1be76329b246 53 short EqualLenVal( lenVal* plvTdoExpected,
monpjc 0:1be76329b246 54 lenVal* plvTdoCaptured,
monpjc 0:1be76329b246 55 lenVal* plvTdoMask )
monpjc 0:1be76329b246 56 {
monpjc 0:1be76329b246 57 short sEqual;
monpjc 0:1be76329b246 58 short sIndex;
monpjc 0:1be76329b246 59 unsigned char ucByteVal1;
monpjc 0:1be76329b246 60 unsigned char ucByteVal2;
monpjc 0:1be76329b246 61 unsigned char ucByteMask;
monpjc 0:1be76329b246 62
monpjc 0:1be76329b246 63 sEqual = 1;
monpjc 0:1be76329b246 64 sIndex = plvTdoExpected->len;
monpjc 0:1be76329b246 65
monpjc 0:1be76329b246 66 while ( sEqual && sIndex-- )
monpjc 0:1be76329b246 67 {
monpjc 0:1be76329b246 68 ucByteVal1 = plvTdoExpected->val[ sIndex ];
monpjc 0:1be76329b246 69 ucByteVal2 = plvTdoCaptured->val[ sIndex ];
monpjc 0:1be76329b246 70 if ( plvTdoMask )
monpjc 0:1be76329b246 71 {
monpjc 0:1be76329b246 72 ucByteMask = plvTdoMask->val[ sIndex ];
monpjc 0:1be76329b246 73 ucByteVal1 &= ucByteMask;
monpjc 0:1be76329b246 74 ucByteVal2 &= ucByteMask;
monpjc 0:1be76329b246 75 }
monpjc 0:1be76329b246 76 if ( ucByteVal1 != ucByteVal2 )
monpjc 0:1be76329b246 77 {
monpjc 0:1be76329b246 78 sEqual = 0;
monpjc 0:1be76329b246 79 }
monpjc 0:1be76329b246 80 }
monpjc 0:1be76329b246 81
monpjc 0:1be76329b246 82 return( sEqual );
monpjc 0:1be76329b246 83 }
monpjc 0:1be76329b246 84
monpjc 0:1be76329b246 85
monpjc 0:1be76329b246 86 /*****************************************************************************
monpjc 0:1be76329b246 87 * Function: RetBit
monpjc 0:1be76329b246 88 * Description: return the (byte, bit) of lv (reading from left to right).
monpjc 0:1be76329b246 89 * Parameters: plv - ptr to lenval.
monpjc 0:1be76329b246 90 * iByte - the byte to get the bit from.
monpjc 0:1be76329b246 91 * iBit - the bit number (0=msb)
monpjc 0:1be76329b246 92 * Returns: short - the bit value.
monpjc 0:1be76329b246 93 *****************************************************************************/
monpjc 0:1be76329b246 94 short RetBit( lenVal* plv,
monpjc 0:1be76329b246 95 int iByte,
monpjc 0:1be76329b246 96 int iBit )
monpjc 0:1be76329b246 97 {
monpjc 0:1be76329b246 98 /* assert( ( iByte >= 0 ) && ( iByte < plv->len ) ); */
monpjc 0:1be76329b246 99 /* assert( ( iBit >= 0 ) && ( iBit < 8 ) ); */
monpjc 0:1be76329b246 100 return( (short)( ( plv->val[ iByte ] >> ( 7 - iBit ) ) & 0x1 ) );
monpjc 0:1be76329b246 101 }
monpjc 0:1be76329b246 102
monpjc 0:1be76329b246 103 /*****************************************************************************
monpjc 0:1be76329b246 104 * Function: SetBit
monpjc 0:1be76329b246 105 * Description: set the (byte, bit) of lv equal to val
monpjc 0:1be76329b246 106 * Example: SetBit("00000000",byte, 1) equals "01000000".
monpjc 0:1be76329b246 107 * Parameters: plv - ptr to lenval.
monpjc 0:1be76329b246 108 * iByte - the byte to get the bit from.
monpjc 0:1be76329b246 109 * iBit - the bit number (0=msb).
monpjc 0:1be76329b246 110 * sVal - the bit value to set.
monpjc 0:1be76329b246 111 * Returns: void.
monpjc 0:1be76329b246 112 *****************************************************************************/
monpjc 0:1be76329b246 113 void SetBit( lenVal* plv,
monpjc 0:1be76329b246 114 int iByte,
monpjc 0:1be76329b246 115 int iBit,
monpjc 0:1be76329b246 116 short sVal )
monpjc 0:1be76329b246 117 {
monpjc 0:1be76329b246 118 unsigned char ucByteVal;
monpjc 0:1be76329b246 119 unsigned char ucBitMask;
monpjc 0:1be76329b246 120
monpjc 0:1be76329b246 121 ucBitMask = (unsigned char)(1 << ( 7 - iBit ));
monpjc 0:1be76329b246 122 ucByteVal = (unsigned char)(plv->val[ iByte ] & (~ucBitMask));
monpjc 0:1be76329b246 123
monpjc 0:1be76329b246 124 if ( sVal )
monpjc 0:1be76329b246 125 {
monpjc 0:1be76329b246 126 ucByteVal |= ucBitMask;
monpjc 0:1be76329b246 127 }
monpjc 0:1be76329b246 128 plv->val[ iByte ] = ucByteVal;
monpjc 0:1be76329b246 129 }
monpjc 0:1be76329b246 130
monpjc 0:1be76329b246 131 /*****************************************************************************
monpjc 0:1be76329b246 132 * Function: AddVal
monpjc 0:1be76329b246 133 * Description: add val1 to val2 and store in resVal;
monpjc 0:1be76329b246 134 * assumes val1 and val2 are of equal length.
monpjc 0:1be76329b246 135 * Parameters: plvResVal - ptr to result.
monpjc 0:1be76329b246 136 * plvVal1 - ptr of addendum.
monpjc 0:1be76329b246 137 * plvVal2 - ptr of addendum.
monpjc 0:1be76329b246 138 * Returns: void.
monpjc 0:1be76329b246 139 *****************************************************************************/
monpjc 0:1be76329b246 140 void addVal( lenVal* plvResVal,
monpjc 0:1be76329b246 141 lenVal* plvVal1,
monpjc 0:1be76329b246 142 lenVal* plvVal2 )
monpjc 0:1be76329b246 143 {
monpjc 0:1be76329b246 144 unsigned char ucCarry;
monpjc 0:1be76329b246 145 unsigned short usSum;
monpjc 0:1be76329b246 146 unsigned short usVal1;
monpjc 0:1be76329b246 147 unsigned short usVal2;
monpjc 0:1be76329b246 148 short sIndex;
monpjc 0:1be76329b246 149
monpjc 0:1be76329b246 150 plvResVal->len = plvVal1->len; /* set up length of result */
monpjc 0:1be76329b246 151
monpjc 0:1be76329b246 152 /* start at least significant bit and add bytes */
monpjc 0:1be76329b246 153 ucCarry = 0;
monpjc 0:1be76329b246 154 sIndex = plvVal1->len;
monpjc 0:1be76329b246 155 while ( sIndex-- )
monpjc 0:1be76329b246 156 {
monpjc 0:1be76329b246 157 usVal1 = plvVal1->val[ sIndex ]; /* i'th byte of val1 */
monpjc 0:1be76329b246 158 usVal2 = plvVal2->val[ sIndex ]; /* i'th byte of val2 */
monpjc 0:1be76329b246 159
monpjc 0:1be76329b246 160 /* add the two bytes plus carry from previous addition */
monpjc 0:1be76329b246 161 usSum = (unsigned short)( usVal1 + usVal2 + ucCarry );
monpjc 0:1be76329b246 162
monpjc 0:1be76329b246 163 /* set up carry for next byte */
monpjc 0:1be76329b246 164 ucCarry = (unsigned char)( ( usSum > 255 ) ? 1 : 0 );
monpjc 0:1be76329b246 165
monpjc 0:1be76329b246 166 /* set the i'th byte of the result */
monpjc 0:1be76329b246 167 plvResVal->val[ sIndex ] = (unsigned char)usSum;
monpjc 0:1be76329b246 168 }
monpjc 0:1be76329b246 169 }
monpjc 0:1be76329b246 170
monpjc 0:1be76329b246 171 /*****************************************************************************
monpjc 0:1be76329b246 172 * Function: readVal
monpjc 0:1be76329b246 173 * Description: read from XSVF numBytes bytes of data into x.
monpjc 0:1be76329b246 174 * Parameters: plv - ptr to lenval in which to put the bytes read.
monpjc 0:1be76329b246 175 * sNumBytes - the number of bytes to read.
monpjc 0:1be76329b246 176 * Returns: void.
monpjc 0:1be76329b246 177 *****************************************************************************/
monpjc 0:1be76329b246 178 void readVal( lenVal* plv,
monpjc 0:1be76329b246 179 short sNumBytes )
monpjc 0:1be76329b246 180 {
monpjc 0:1be76329b246 181 unsigned char* pucVal;
monpjc 0:1be76329b246 182
monpjc 0:1be76329b246 183 plv->len = sNumBytes; /* set the length of the lenVal */
monpjc 0:1be76329b246 184 for ( pucVal = plv->val; sNumBytes; --sNumBytes, ++pucVal )
monpjc 0:1be76329b246 185 {
monpjc 0:1be76329b246 186 /* read a byte of data into the lenVal */
monpjc 0:1be76329b246 187 readByte( pucVal );
monpjc 0:1be76329b246 188 }
monpjc 0:1be76329b246 189 }
monpjc 0:1be76329b246 190