3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
5 * SPDX-License-Identifier: GPL-2.0+
8 /*******************************************************/
10 /* abstract: This file contains routines for using */
11 /* the lenVal data structure. */
12 /*******************************************************/
15 #include <asm/processor.h>
21 /*****************************************************************************
23 * Description: Extract the long value from the lenval array.
24 * Parameters: plvValue - ptr to lenval.
25 * Returns: long - the extracted value.
26 *****************************************************************************/
27 long value( lenVal* plvValue )
29 long lValue; /* result to hold the accumulated result */
33 for ( sIndex = 0; sIndex < plvValue->len ; ++sIndex )
35 lValue <<= 8; /* shift the accumulated result */
36 lValue |= plvValue->val[ sIndex]; /* get the last byte first */
42 /*****************************************************************************
43 * Function: initLenVal
44 * Description: Initialize the lenval array with the given value.
45 * Assumes lValue is less than 256.
46 * Parameters: plv - ptr to lenval.
47 * lValue - the value to set.
49 *****************************************************************************/
50 void initLenVal( lenVal* plv,
54 plv->val[0] = (unsigned char)lValue;
57 /*****************************************************************************
58 * Function: EqualLenVal
59 * Description: Compare two lenval arrays with an optional mask.
60 * Parameters: plvTdoExpected - ptr to lenval #1.
61 * plvTdoCaptured - ptr to lenval #2.
62 * plvTdoMask - optional ptr to mask (=0 if no mask).
63 * Returns: short - 0 = mismatch; 1 = equal.
64 *****************************************************************************/
65 short EqualLenVal( lenVal* plvTdoExpected,
66 lenVal* plvTdoCaptured,
71 unsigned char ucByteVal1;
72 unsigned char ucByteVal2;
73 unsigned char ucByteMask;
76 sIndex = plvTdoExpected->len;
78 while ( sEqual && sIndex-- )
80 ucByteVal1 = plvTdoExpected->val[ sIndex ];
81 ucByteVal2 = plvTdoCaptured->val[ sIndex ];
84 ucByteMask = plvTdoMask->val[ sIndex ];
85 ucByteVal1 &= ucByteMask;
86 ucByteVal2 &= ucByteMask;
88 if ( ucByteVal1 != ucByteVal2 )
98 /*****************************************************************************
100 * Description: return the (byte, bit) of lv (reading from left to right).
101 * Parameters: plv - ptr to lenval.
102 * iByte - the byte to get the bit from.
103 * iBit - the bit number (0=msb)
104 * Returns: short - the bit value.
105 *****************************************************************************/
106 short RetBit( lenVal* plv,
110 /* assert( ( iByte >= 0 ) && ( iByte < plv->len ) ); */
111 /* assert( ( iBit >= 0 ) && ( iBit < 8 ) ); */
112 return( (short)( ( plv->val[ iByte ] >> ( 7 - iBit ) ) & 0x1 ) );
115 /*****************************************************************************
117 * Description: set the (byte, bit) of lv equal to val
118 * Example: SetBit("00000000",byte, 1) equals "01000000".
119 * Parameters: plv - ptr to lenval.
120 * iByte - the byte to get the bit from.
121 * iBit - the bit number (0=msb).
122 * sVal - the bit value to set.
124 *****************************************************************************/
125 void SetBit( lenVal* plv,
130 unsigned char ucByteVal;
131 unsigned char ucBitMask;
133 ucBitMask = (unsigned char)(1 << ( 7 - iBit ));
134 ucByteVal = (unsigned char)(plv->val[ iByte ] & (~ucBitMask));
138 ucByteVal |= ucBitMask;
140 plv->val[ iByte ] = ucByteVal;
143 /*****************************************************************************
145 * Description: add val1 to val2 and store in resVal;
146 * assumes val1 and val2 are of equal length.
147 * Parameters: plvResVal - ptr to result.
148 * plvVal1 - ptr of addendum.
149 * plvVal2 - ptr of addendum.
151 *****************************************************************************/
152 void addVal( lenVal* plvResVal,
156 unsigned char ucCarry;
157 unsigned short usSum;
158 unsigned short usVal1;
159 unsigned short usVal2;
162 plvResVal->len = plvVal1->len; /* set up length of result */
164 /* start at least significant bit and add bytes */
166 sIndex = plvVal1->len;
169 usVal1 = plvVal1->val[ sIndex ]; /* i'th byte of val1 */
170 usVal2 = plvVal2->val[ sIndex ]; /* i'th byte of val2 */
172 /* add the two bytes plus carry from previous addition */
173 usSum = (unsigned short)( usVal1 + usVal2 + ucCarry );
175 /* set up carry for next byte */
176 ucCarry = (unsigned char)( ( usSum > 255 ) ? 1 : 0 );
178 /* set the i'th byte of the result */
179 plvResVal->val[ sIndex ] = (unsigned char)usSum;
183 /*****************************************************************************
185 * Description: read from XSVF numBytes bytes of data into x.
186 * Parameters: plv - ptr to lenval in which to put the bytes read.
187 * sNumBytes - the number of bytes to read.
189 *****************************************************************************/
190 void readVal( lenVal* plv,
193 unsigned char* pucVal;
195 plv->len = sNumBytes; /* set the length of the lenVal */
196 for ( pucVal = plv->val; sNumBytes; --sNumBytes, ++pucVal )
198 /* read a byte of data into the lenVal */