aboutsummaryrefslogtreecommitdiff
path: root/board/esd/common/xilinx_jtag/lenval.c
blob: a18a16e03ef112f69aa1005261b012ed47ea3e7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * (C) Copyright 2003
 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*******************************************************/
/* file: lenval.c                                      */
/* abstract:  This file contains routines for using    */
/*            the lenVal data structure.               */
/*******************************************************/

#include <common.h>
#include <asm/processor.h>

#include "lenval.h"
#include "ports.h"


/*****************************************************************************
 * Function:     value
 * Description:  Extract the long value from the lenval array.
 * Parameters:   plvValue    - ptr to lenval.
 * Returns:      long        - the extracted value.
 *****************************************************************************/
long value( lenVal*     plvValue )
{
	long    lValue;         /* result to hold the accumulated result */
	short   sIndex;

	lValue  = 0;
	for ( sIndex = 0; sIndex < plvValue->len ; ++sIndex )
	{
		lValue <<= 8;                       /* shift the accumulated result */
		lValue |= plvValue->val[ sIndex];   /* get the last byte first */
	}

	return( lValue );
}

/*****************************************************************************
 * Function:     initLenVal
 * Description:  Initialize the lenval array with the given value.
 *               Assumes lValue is less than 256.
 * Parameters:   plv         - ptr to lenval.
 *               lValue      - the value to set.
 * Returns:      void.
 *****************************************************************************/
void initLenVal( lenVal*    plv,
                 long       lValue )
{
	plv->len    = 1;
	plv->val[0] = (unsigned char)lValue;
}

/*****************************************************************************
 * Function:     EqualLenVal
 * Description:  Compare two lenval arrays with an optional mask.
 * Parameters:   plvTdoExpected  - ptr to lenval #1.
 *               plvTdoCaptured  - ptr to lenval #2.
 *               plvTdoMask      - optional ptr to mask (=0 if no mask).
 * Returns:      short   - 0 = mismatch; 1 = equal.
 *****************************************************************************/
short EqualLenVal( lenVal*  plvTdoExpected,
                   lenVal*  plvTdoCaptured,
                   lenVal*  plvTdoMask )
{
	short           sEqual;
	short           sIndex;
	unsigned char   ucByteVal1;
	unsigned char   ucByteVal2;
	unsigned char   ucByteMask;

	sEqual  = 1;
	sIndex  = plvTdoExpected->len;

	while ( sEqual && sIndex-- )
	{
		ucByteVal1  = plvTdoExpected->val[ sIndex ];
		ucByteVal2  = plvTdoCaptured->val[ sIndex ];
		if ( plvTdoMask )
		{
			ucByteMask  = plvTdoMask->val[ sIndex ];
			ucByteVal1  &= ucByteMask;
			ucByteVal2  &= ucByteMask;
		}
		if ( ucByteVal1 != ucByteVal2 )
		{
			sEqual  = 0;
		}
	}

	return( sEqual );
}


/*****************************************************************************
 * Function:     RetBit
 * Description:  return the (byte, bit) of lv (reading from left to right).
 * Parameters:   plv     - ptr to lenval.
 *               iByte   - the byte to get the bit from.
 *               iBit    - the bit number (0=msb)
 * Returns:      short   - the bit value.
 *****************************************************************************/
short RetBit( lenVal*   plv,
              int       iByte,
              int       iBit )
{
	/* assert( ( iByte >= 0 ) && ( iByte < plv->len ) ); */
	/* assert( ( iBit >= 0 ) && ( iBit < 8 ) ); */
	return( (short)( ( plv->val[ iByte ] >> ( 7 - iBit ) ) & 0x1 ) );
}

/*****************************************************************************
 * Function:     SetBit
 * Description:  set the (byte, bit) of lv equal to val
 * Example:      SetBit("00000000",byte, 1) equals "01000000".
 * Parameters:   plv     - ptr to lenval.
 *               iByte   - the byte to get the bit from.
 *               iBit    - the bit number (0=msb).
 *               sVal    - the bit value to set.
 * Returns:      void.
 *****************************************************************************/
void SetBit( lenVal*    plv,
             int        iByte,
             int        iBit,
             short      sVal )
{
	unsigned char   ucByteVal;
	unsigned char   ucBitMask;

	ucBitMask   = (unsigned char)(1 << ( 7 - iBit ));
	ucByteVal   = (unsigned char)(plv->val[ iByte ] & (~ucBitMask));

	if ( sVal )
	{
		ucByteVal   |= ucBitMask;
	}
	plv->val[ iByte ]   = ucByteVal;
}

/*****************************************************************************
 * Function:     AddVal
 * Description:  add val1 to val2 and store in resVal;
 *               assumes val1 and val2  are of equal length.
 * Parameters:   plvResVal   - ptr to result.
 *               plvVal1     - ptr of addendum.
 *               plvVal2     - ptr of addendum.
 * Returns:      void.
 *****************************************************************************/
void addVal( lenVal*    plvResVal,
             lenVal*    plvVal1,
             lenVal*    plvVal2 )
{
	unsigned char   ucCarry;
	unsigned short  usSum;
	unsigned short  usVal1;
	unsigned short  usVal2;
	short           sIndex;

	plvResVal->len  = plvVal1->len;         /* set up length of result */

	/* start at least significant bit and add bytes    */
	ucCarry = 0;
	sIndex  = plvVal1->len;
	while ( sIndex-- )
	{
		usVal1  = plvVal1->val[ sIndex ];   /* i'th byte of val1 */
		usVal2  = plvVal2->val[ sIndex ];   /* i'th byte of val2 */

		/* add the two bytes plus carry from previous addition */
		usSum   = (unsigned short)( usVal1 + usVal2 + ucCarry );

		/* set up carry for next byte */
		ucCarry = (unsigned char)( ( usSum > 255 ) ? 1 : 0 );

		/* set the i'th byte of the result */
		plvResVal->val[ sIndex ]    = (unsigned char)usSum;
	}
}

/*****************************************************************************
 * Function:     readVal
 * Description:  read from XSVF numBytes bytes of data into x.
 * Parameters:   plv         - ptr to lenval in which to put the bytes read.
 *               sNumBytes   - the number of bytes to read.
 * Returns:      void.
 *****************************************************************************/
void readVal( lenVal*   plv,
              short     sNumBytes )
{
	unsigned char*  pucVal;

	plv->len    = sNumBytes;        /* set the length of the lenVal        */
	for ( pucVal = plv->val; sNumBytes; --sNumBytes, ++pucVal )
	{
		/* read a byte of data into the lenVal */
		readByte( pucVal );
	}
}