5 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
7 * Lattice ispVME Embedded code to load Lattice's FPGA:
9 * Copyright 2009 Lattice Semiconductor Corp.
11 * ispVME Embedded allows programming of Lattice's suite of FPGA
12 * devices on embedded systems through the JTAG port. The software
13 * is distributed in source code form and is open to re - distribution
14 * and modification where applicable.
16 * Revision History of ivm_core.c module:
17 * 4/25/06 ht Change some variables from unsigned short or int
18 * to long int to make the code compiler independent.
19 * 5/24/06 ht Support using RESET (TRST) pin as a special purpose
20 * control pin such as triggering the loading of known
22 * 3/6/07 ht added functions to support output to terminals
24 * 09/11/07 NN Type cast mismatch variables
25 * Moved the sclock() function to hardware.c
26 * 08/28/08 NN Added Calculate checksum support.
27 * 4/1/09 Nguyen replaced the recursive function call codes on
28 * the ispVMLCOUNT function
29 * SPDX-License-Identifier: GPL-2.0+
33 #include <linux/string.h>
37 #define vme_out_char(c) printf("%c", c)
38 #define vme_out_hex(c) printf("%x", c)
39 #define vme_out_string(s) printf("%s", s)
43 * Global variables used to specify the flow control and data type.
45 * g_usFlowControl: flow control register. Each bit in the
46 * register can potentially change the
47 * personality of the embedded engine.
48 * g_usDataType: holds the data type of the current row.
52 static unsigned short g_usFlowControl;
53 unsigned short g_usDataType;
57 * Global variables used to specify the ENDDR and ENDIR.
59 * g_ucEndDR: the state that the device goes to after SDR.
60 * g_ucEndIR: the state that the device goes to after SIR.
64 unsigned char g_ucEndDR = DRPAUSE;
65 unsigned char g_ucEndIR = IRPAUSE;
69 * Global variables used to support header/trailer.
71 * g_usHeadDR: the number of lead devices in bypass.
72 * g_usHeadIR: the sum of IR length of lead devices.
73 * g_usTailDR: the number of tail devices in bypass.
74 * g_usTailIR: the sum of IR length of tail devices.
78 static unsigned short g_usHeadDR;
79 static unsigned short g_usHeadIR;
80 static unsigned short g_usTailDR;
81 static unsigned short g_usTailIR;
85 * Global variable to store the number of bits of data or instruction
86 * to be shifted into or out from the device.
90 static unsigned short g_usiDataSize;
94 * Stores the frequency. Default to 1 MHz.
98 static int g_iFrequency = 1000;
102 * Stores the maximum amount of ram needed to hold a row of data.
106 static unsigned short g_usMaxSize;
110 * Stores the LSH or RSH value.
114 static unsigned short g_usShiftValue;
118 * Stores the current repeat loop value.
122 static unsigned short g_usRepeatLoops;
126 * Stores the current vendor.
130 static signed char g_cVendor = LATTICE;
134 * Stores the VME file CRC.
138 unsigned short g_usCalculatedCRC;
142 * Stores the Device Checksum.
145 /* 08/28/08 NN Added Calculate checksum support. */
146 unsigned long g_usChecksum;
147 static unsigned int g_uiChecksumIndex;
151 * Stores the current state of the JTAG state machine.
155 static signed char g_cCurrentJTAGState;
159 * Global variables used to support looping.
161 * g_pucHeapMemory: holds the entire repeat loop.
162 * g_iHeapCounter: points to the current byte in the repeat loop.
163 * g_iHEAPSize: the current size of the repeat in bytes.
167 unsigned char *g_pucHeapMemory;
168 unsigned short g_iHeapCounter;
169 unsigned short g_iHEAPSize;
170 static unsigned short previous_size;
174 * Global variables used to support intelligent programming.
176 * g_usIntelDataIndex: points to the current byte of the
177 * intelligent buffer.
178 * g_usIntelBufferSize: holds the size of the intelligent
183 unsigned short g_usIntelDataIndex;
184 unsigned short g_usIntelBufferSize;
188 * Supported VME versions.
192 const char *const g_szSupportedVersions[] = {
193 "__VME2.0", "__VME3.0", "____12.0", "____12.1", 0};
197 * Holds the maximum size of each respective buffer. These variables are used
198 * to write the HEX files when converting VME to HEX.
202 static unsigned short g_usTDOSize;
203 static unsigned short g_usMASKSize;
204 static unsigned short g_usTDISize;
205 static unsigned short g_usDMASKSize;
206 static unsigned short g_usLCOUNTSize;
207 static unsigned short g_usHDRSize;
208 static unsigned short g_usTDRSize;
209 static unsigned short g_usHIRSize;
210 static unsigned short g_usTIRSize;
211 static unsigned short g_usHeapSize;
215 * Global variables used to store data.
217 * g_pucOutMaskData: local RAM to hold one row of MASK data.
218 * g_pucInData: local RAM to hold one row of TDI data.
219 * g_pucOutData: local RAM to hold one row of TDO data.
220 * g_pucHIRData: local RAM to hold the current SIR header.
221 * g_pucTIRData: local RAM to hold the current SIR trailer.
222 * g_pucHDRData: local RAM to hold the current SDR header.
223 * g_pucTDRData: local RAM to hold the current SDR trailer.
224 * g_pucIntelBuffer: local RAM to hold the current intelligent buffer
225 * g_pucOutDMaskData: local RAM to hold one row of DMASK data.
229 unsigned char *g_pucOutMaskData = NULL,
231 *g_pucOutData = NULL,
232 *g_pucHIRData = NULL,
233 *g_pucTIRData = NULL,
234 *g_pucHDRData = NULL,
235 *g_pucTDRData = NULL,
236 *g_pucIntelBuffer = NULL,
237 *g_pucOutDMaskData = NULL;
241 * JTAG state machine transition table.
246 unsigned char CurState; /* From this state */
247 unsigned char NextState; /* Step to this state */
248 unsigned char Pattern; /* The tragetory of TMS */
249 unsigned char Pulses; /* The number of steps */
250 } g_JTAGTransistions[25] = {
251 { RESET, RESET, 0xFC, 6 }, /* Transitions from RESET */
252 { RESET, IDLE, 0x00, 1 },
253 { RESET, DRPAUSE, 0x50, 5 },
254 { RESET, IRPAUSE, 0x68, 6 },
255 { IDLE, RESET, 0xE0, 3 }, /* Transitions from IDLE */
256 { IDLE, DRPAUSE, 0xA0, 4 },
257 { IDLE, IRPAUSE, 0xD0, 5 },
258 { DRPAUSE, RESET, 0xF8, 5 }, /* Transitions from DRPAUSE */
259 { DRPAUSE, IDLE, 0xC0, 3 },
260 { DRPAUSE, IRPAUSE, 0xF4, 7 },
261 { DRPAUSE, DRPAUSE, 0xE8, 6 },/* 06/14/06 Support POLL STATUS LOOP*/
262 { IRPAUSE, RESET, 0xF8, 5 }, /* Transitions from IRPAUSE */
263 { IRPAUSE, IDLE, 0xC0, 3 },
264 { IRPAUSE, DRPAUSE, 0xE8, 6 },
265 { DRPAUSE, SHIFTDR, 0x80, 2 }, /* Extra transitions using SHIFTDR */
266 { IRPAUSE, SHIFTDR, 0xE0, 5 },
267 { SHIFTDR, DRPAUSE, 0x80, 2 },
268 { SHIFTDR, IDLE, 0xC0, 3 },
269 { IRPAUSE, SHIFTIR, 0x80, 2 },/* Extra transitions using SHIFTIR */
270 { SHIFTIR, IRPAUSE, 0x80, 2 },
271 { SHIFTIR, IDLE, 0xC0, 3 },
272 { DRPAUSE, DRCAPTURE, 0xE0, 4 }, /* 11/15/05 Support DRCAPTURE*/
273 { DRCAPTURE, DRPAUSE, 0x80, 2 },
274 { IDLE, DRCAPTURE, 0x80, 2 },
275 { IRPAUSE, DRCAPTURE, 0xE0, 4 }
280 * List to hold all LVDS pairs.
284 LVDSPair *g_pLVDSList;
285 unsigned short g_usLVDSPairCount;
289 * Function prototypes.
293 static signed char ispVMDataCode(void);
294 static long int ispVMDataSize(void);
295 static void ispVMData(unsigned char *Data);
296 static signed char ispVMShift(signed char Code);
297 static signed char ispVMAmble(signed char Code);
298 static signed char ispVMLoop(unsigned short a_usLoopCount);
299 static signed char ispVMBitShift(signed char mode, unsigned short bits);
300 static void ispVMComment(unsigned short a_usCommentSize);
301 static void ispVMHeader(unsigned short a_usHeaderSize);
302 static signed char ispVMLCOUNT(unsigned short a_usCountSize);
303 static void ispVMClocks(unsigned short Clocks);
304 static void ispVMBypass(signed char ScanType, unsigned short Bits);
305 static void ispVMStateMachine(signed char NextState);
306 static signed char ispVMSend(unsigned short int);
307 static signed char ispVMRead(unsigned short int);
308 static signed char ispVMReadandSave(unsigned short int);
309 static signed char ispVMProcessLVDS(unsigned short a_usLVDSCount);
310 static void ispVMMemManager(signed char types, unsigned short size);
314 * External variables and functions in hardware.c module
317 static signed char g_cCurrentJTAGState;
325 * Returns the state as a string based on the opcode. Only used
326 * for debugging purposes.
330 const char *GetState(unsigned char a_ucState)
345 case DRCAPTURE:/* 11/15/05 support DRCAPTURE*/
358 * Prints the data. Only used for debugging purposes.
362 void PrintData(unsigned short a_iDataSize, unsigned char *a_pucData)
364 /* 09/11/07 NN added local variables initialization */
365 unsigned short usByteSize = 0;
366 unsigned short usBitIndex = 0;
367 signed short usByteIndex = 0;
368 unsigned char ucByte = 0;
369 unsigned char ucFlipByte = 0;
371 if (a_iDataSize % 8) {
372 /* 09/11/07 NN Type cast mismatch variables */
373 usByteSize = (unsigned short)(a_iDataSize / 8 + 1);
375 /* 09/11/07 NN Type cast mismatch variables */
376 usByteSize = (unsigned short)(a_iDataSize / 8);
379 /* 09/11/07 NN Type cast mismatch variables */
380 for (usByteIndex = (signed short)(usByteSize - 1);
381 usByteIndex >= 0; usByteIndex--) {
382 ucByte = a_pucData[usByteIndex];
391 for (usBitIndex = 0; usBitIndex < 8; usBitIndex++) {
402 * Print the flipped byte.
406 printf("%.02X", ucFlipByte);
407 if ((usByteSize - usByteIndex) % 40 == 39) {
417 void ispVMMemManager(signed char cTarget, unsigned short usSize)
422 if (g_pucInData != NULL) {
423 if (previous_size == usSize) {/*memory exist*/
430 g_pucInData = (unsigned char *) malloc(usSize / 8 + 2);
431 previous_size = usSize;
434 if (g_pucOutData != NULL) {
435 if (previous_size == usSize) { /*already exist*/
442 g_pucOutData = (unsigned char *) malloc(usSize / 8 + 2);
443 previous_size = usSize;
446 if (g_pucOutMaskData != NULL) {
447 if (previous_size == usSize) {/*already allocated*/
450 free(g_pucOutMaskData);
451 g_pucOutMaskData = NULL;
454 g_pucOutMaskData = (unsigned char *) malloc(usSize / 8 + 2);
455 previous_size = usSize;
458 if (g_pucHIRData != NULL) {
462 g_pucHIRData = (unsigned char *) malloc(usSize / 8 + 2);
465 if (g_pucTIRData != NULL) {
469 g_pucTIRData = (unsigned char *) malloc(usSize / 8 + 2);
472 if (g_pucHDRData != NULL) {
476 g_pucHDRData = (unsigned char *) malloc(usSize / 8 + 2);
479 if (g_pucTDRData != NULL) {
483 g_pucTDRData = (unsigned char *) malloc(usSize / 8 + 2);
486 if (g_pucHeapMemory != NULL) {
487 free(g_pucHeapMemory);
488 g_pucHeapMemory = NULL;
490 g_pucHeapMemory = (unsigned char *) malloc(usSize + 2);
493 if (g_pucOutDMaskData != NULL) {
494 if (previous_size == usSize) { /*already allocated*/
497 free(g_pucOutDMaskData);
498 g_pucOutDMaskData = NULL;
501 g_pucOutDMaskData = (unsigned char *) malloc(usSize / 8 + 2);
502 previous_size = usSize;
505 if (g_pucIntelBuffer != NULL) {
506 free(g_pucIntelBuffer);
507 g_pucIntelBuffer = NULL;
509 g_pucIntelBuffer = (unsigned char *) malloc(usSize + 2);
512 if (g_pLVDSList != NULL) {
516 g_pLVDSList = (LVDSPair *) malloc(usSize * sizeof(LVDSPair));
518 memset(g_pLVDSList, 0, usSize * sizeof(LVDSPair));
525 void ispVMFreeMem(void)
527 if (g_pucHeapMemory != NULL) {
528 free(g_pucHeapMemory);
529 g_pucHeapMemory = NULL;
532 if (g_pucOutMaskData != NULL) {
533 free(g_pucOutMaskData);
534 g_pucOutMaskData = NULL;
537 if (g_pucInData != NULL) {
542 if (g_pucOutData != NULL) {
547 if (g_pucHIRData != NULL) {
552 if (g_pucTIRData != NULL) {
557 if (g_pucHDRData != NULL) {
562 if (g_pucTDRData != NULL) {
567 if (g_pucOutDMaskData != NULL) {
568 free(g_pucOutDMaskData);
569 g_pucOutDMaskData = NULL;
572 if (g_pucIntelBuffer != NULL) {
573 free(g_pucIntelBuffer);
574 g_pucIntelBuffer = NULL;
577 if (g_pLVDSList != NULL) {
588 * Returns a VME-encoded number, usually used to indicate the
589 * bit length of an SIR/SDR command.
593 long int ispVMDataSize()
595 /* 09/11/07 NN added local variables initialization */
597 signed char cCurrentByte = 0;
598 signed char cIndex = 0;
600 while ((cCurrentByte = GetByte()) & 0x80) {
601 iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
604 iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
612 * This is the heart of the embedded engine. All the high-level opcodes
613 * are extracted here. Once they have been identified, then it
614 * will call other functions to handle the processing.
618 signed char ispVMCode()
620 /* 09/11/07 NN added local variables initialization */
621 unsigned short iRepeatSize = 0;
622 signed char cOpcode = 0;
623 signed char cRetCode = 0;
624 unsigned char ucState = 0;
625 unsigned short usDelay = 0;
626 unsigned short usToggle = 0;
627 unsigned char usByte = 0;
631 * Check the compression flag only if this is the first time
632 * this function is entered. Do not check the compression flag if
633 * it is being called recursively from other functions within
634 * the embedded engine.
638 if (!(g_usDataType & LHEAP_IN) && !(g_usDataType & HEAP_IN)) {
640 if (usByte == 0xf1) {
641 g_usDataType |= COMPRESS;
642 } else if (usByte == 0xf2) {
643 g_usDataType &= ~COMPRESS;
645 return VME_INVALID_FILE;
651 * Begin looping through all the VME opcodes.
655 while ((cOpcode = GetByte()) >= 0) {
661 * Step the JTAG state machine.
667 * Step the JTAG state machine to DRCAPTURE
668 * to support Looping.
671 if ((g_usDataType & LHEAP_IN) &&
672 (ucState == DRPAUSE) &&
673 (g_cCurrentJTAGState == ucState)) {
674 ispVMStateMachine(DRCAPTURE);
677 ispVMStateMachine(ucState);
680 if (g_usDataType & LHEAP_IN) {
681 debug("LDELAY %s ", GetState(ucState));
683 debug("STATE %s;\n", GetState(ucState));
698 if (g_usDataType & LHEAP_IN) {
708 * Shift in data into the device.
712 cRetCode = ispVMShift(cOpcode);
725 /* 09/11/07 NN Type cast mismatch variables */
726 usDelay = (unsigned short) ispVMDataSize();
730 if (usDelay & 0x8000) {
733 * Since MSB is set, the delay time must be
734 * decoded to millisecond. The SVF2VME encodes
735 * the MSB to represent millisecond.
739 if (g_usDataType & LHEAP_IN) {
740 printf("%.2E SEC;\n",
741 (float) usDelay / 1000);
743 printf("RUNTEST %.2E SEC;\n",
744 (float) usDelay / 1000);
748 * Since MSB is not set, the delay time
749 * is given as microseconds.
752 if (g_usDataType & LHEAP_IN) {
753 printf("%.2E SEC;\n",
754 (float) usDelay / 1000000);
756 printf("RUNTEST %.2E SEC;\n",
757 (float) usDelay / 1000000);
765 * Issue clock toggles.
768 /* 09/11/07 NN Type cast mismatch variables */
769 usToggle = (unsigned short) ispVMDataSize();
770 ispVMClocks(usToggle);
773 printf("RUNTEST %d TCK;\n", usToggle);
784 g_ucEndDR = GetByte();
787 printf("ENDDR %s;\n", GetState(g_ucEndDR));
798 g_ucEndIR = GetByte();
801 printf("ENDIR %s;\n", GetState(g_ucEndIR));
826 * Set the header/trailer of the device in order
831 cRetCode = ispVMAmble(cOpcode);
843 * The maximum RAM required to support
844 * processing one row of the VME file.
847 /* 09/11/07 NN Type cast mismatch variables */
848 g_usMaxSize = (unsigned short) ispVMDataSize();
851 printf("// MEMSIZE %d\n", g_usMaxSize);
858 * Set the VENDOR type.
866 puts("// VENDOR LATTICE\n");
872 puts("// VENDOR ALTERA\n");
878 puts("// VENDOR XILINX\n");
889 * Set the flow control. Flow control determines
890 * the personality of the embedded engine.
893 /* 09/11/07 NN Type cast mismatch variables */
894 g_usFlowControl |= (unsigned short) ispVMDataSize();
900 * Unset the flow control.
904 /* 09/11/07 NN Type cast mismatch variables */
905 g_usFlowControl &= (unsigned short) ~(ispVMDataSize());
911 * Allocate heap size to store loops.
915 cRetCode = GetByte();
916 if (cRetCode != SECUREHEAP) {
917 return VME_INVALID_FILE;
919 /* 09/11/07 NN Type cast mismatch variables */
920 g_iHEAPSize = (unsigned short) ispVMDataSize();
923 * Store the maximum size of the HEAP buffer.
924 * Used to convert VME to HEX.
927 if (g_iHEAPSize > g_usHeapSize) {
928 g_usHeapSize = g_iHEAPSize;
931 ispVMMemManager(HEAP, (unsigned short) g_iHEAPSize);
943 /* 09/11/07 NN Type cast mismatch variables */
944 iRepeatSize = (unsigned short) ispVMDataSize();
946 cRetCode = ispVMLoop((unsigned short) iRepeatSize);
955 * Exit point from processing loops.
963 * The only valid exit point that indicates
964 * end of programming.
972 * Right-shift address.
976 g_usFlowControl |= SHIFTRIGHT;
978 /* 09/11/07 NN Type cast mismatch variables */
979 g_usShiftValue = (unsigned short) (g_usRepeatLoops *
980 (unsigned short)GetByte());
985 * Left-shift address.
988 g_usFlowControl |= SHIFTLEFT;
990 /* 09/11/07 NN Type cast mismatch variables */
991 g_usShiftValue = (unsigned short) (g_usRepeatLoops *
992 (unsigned short)GetByte());
1002 /* 09/11/07 NN Type cast mismatch variables */
1003 g_iFrequency = (int) (ispVMDataSize() / 1000);
1004 if (g_iFrequency == 1)
1005 g_iFrequency = 1000;
1008 printf("FREQUENCY %.2E HZ;\n",
1009 (float) g_iFrequency * 1000);
1016 * Process LCOUNT command.
1020 cRetCode = ispVMLCOUNT((unsigned short)ispVMDataSize());
1021 if (cRetCode != 0) {
1029 * Set the flow control to verify USERCODE.
1033 g_usFlowControl |= VERIFYUES;
1043 ispVMComment((unsigned short) ispVMDataSize());
1049 * Process LVDS command.
1053 ispVMProcessLVDS((unsigned short) ispVMDataSize());
1063 ispVMHeader((unsigned short) ispVMDataSize());
1065 /* 03/14/06 Support Toggle ispENABLE signal*/
1067 ucState = GetByte();
1068 if ((ucState == ON) || (ucState == 0x01))
1069 writePort(g_ucPinENABLE, 0x01);
1071 writePort(g_ucPinENABLE, 0x00);
1074 /* 05/24/06 support Toggle TRST pin*/
1076 ucState = GetByte();
1077 if (ucState == 0x01)
1078 writePort(g_ucPinTRST, 0x01);
1080 writePort(g_ucPinTRST, 0x00);
1087 * Invalid opcode encountered.
1092 printf("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
1095 return VME_INVALID_FILE;
1101 * Invalid exit point. Processing the token 'ENDVME' is the only
1102 * valid way to exit the embedded engine.
1106 return VME_INVALID_FILE;
1113 * Processes the TDI/TDO/MASK/DMASK etc of an SIR/SDR command.
1117 signed char ispVMDataCode()
1119 /* 09/11/07 NN added local variables initialization */
1120 signed char cDataByte = 0;
1121 signed char siDataSource = 0; /*source of data from file by default*/
1123 if (g_usDataType & HEAP_IN) {
1124 siDataSource = 1; /*the source of data from memory*/
1129 * Clear the data type register.
1133 g_usDataType &= ~(MASK_DATA + TDI_DATA +
1134 TDO_DATA + DMASK_DATA + CMASK_DATA);
1137 * Iterate through SIR/SDR command and look for TDI,
1141 while ((cDataByte = GetByte()) >= 0) {
1142 ispVMMemManager(cDataByte, g_usMaxSize);
1143 switch (cDataByte) {
1147 * Store the maximum size of the TDI buffer.
1148 * Used to convert VME to HEX.
1151 if (g_usiDataSize > g_usTDISize) {
1152 g_usTDISize = g_usiDataSize;
1155 * Updated data type register to indicate that
1156 * TDI data is currently being used. Process the
1157 * data in the VME file into the TDI buffer.
1160 g_usDataType |= TDI_DATA;
1161 ispVMData(g_pucInData);
1166 * Store the maximum size of the TDO buffer.
1167 * Used to convert VME to HEX.
1170 if (g_usiDataSize > g_usTDOSize) {
1171 g_usTDOSize = g_usiDataSize;
1175 * Updated data type register to indicate that
1176 * TDO data is currently being used.
1179 g_usDataType |= TDO_DATA;
1184 * Store the maximum size of the TDO buffer.
1185 * Used to convert VME to HEX.
1188 if (g_usiDataSize > g_usTDOSize) {
1189 g_usTDOSize = g_usiDataSize;
1193 * Updated data type register to indicate
1194 * that TDO data is currently being used.
1195 * Process the data in the VME file into the
1199 g_usDataType |= TDO_DATA;
1200 ispVMData(g_pucOutData);
1205 * Store the maximum size of the MASK buffer.
1206 * Used to convert VME to HEX.
1209 if (g_usiDataSize > g_usMASKSize) {
1210 g_usMASKSize = g_usiDataSize;
1214 * Updated data type register to indicate that
1215 * MASK data is currently being used. Process
1216 * the data in the VME file into the MASK buffer
1219 g_usDataType |= MASK_DATA;
1220 ispVMData(g_pucOutMaskData);
1225 * Store the maximum size of the DMASK buffer.
1226 * Used to convert VME to HEX.
1229 if (g_usiDataSize > g_usDMASKSize) {
1230 g_usDMASKSize = g_usiDataSize;
1234 * Updated data type register to indicate that
1235 * DMASK data is currently being used. Process
1236 * the data in the VME file into the DMASK
1240 g_usDataType |= DMASK_DATA;
1241 ispVMData(g_pucOutDMaskData);
1246 * Updated data type register to indicate that
1247 * MASK data is currently being used. Process
1248 * the data in the VME file into the MASK buffer
1251 g_usDataType |= CMASK_DATA;
1252 ispVMData(g_pucOutMaskData);
1258 * Encountered invalid opcode.
1260 return VME_INVALID_FILE;
1263 switch (cDataByte) {
1267 * Left bit shift. Used when performing
1268 * algorithm looping.
1271 if (g_usFlowControl & SHIFTLEFT) {
1272 ispVMBitShift(SHL, g_usShiftValue);
1273 g_usFlowControl &= ~SHIFTLEFT;
1277 * Right bit shift. Used when performing
1278 * algorithm looping.
1281 if (g_usFlowControl & SHIFTRIGHT) {
1282 ispVMBitShift(SHR, g_usShiftValue);
1283 g_usFlowControl &= ~SHIFTRIGHT;
1290 g_usDataType |= HEAP_IN; /*restore from memory*/
1294 if (siDataSource) { /*fetch data from heap memory upon return*/
1295 g_usDataType |= HEAP_IN;
1298 if (cDataByte < 0) {
1301 * Encountered invalid opcode.
1304 return VME_INVALID_FILE;
1313 * Extract one row of data operand from the current data type opcode. Perform
1314 * the decompression if necessary. Extra RAM is not required for the
1315 * decompression process. The decompression scheme employed in this module
1316 * is on row by row basis. The format of the data stream:
1317 * [compression code][compressed data stream]
1318 * 0x00 --No compression
1319 * 0x01 --Compress by 0x00.
1321 * Original stream: 0x000000000000000000000001
1322 * Compressed stream: 0x01000901
1323 * Detail: 0x01 is the code, 0x00 is the key,
1324 * 0x09 is the count of 0x00 bytes,
1325 * 0x01 is the uncompressed byte.
1326 * 0x02 --Compress by 0xFF.
1328 * Original stream: 0xFFFFFFFFFFFFFFFFFFFFFF01
1329 * Compressed stream: 0x02FF0901
1330 * Detail: 0x02 is the code, 0xFF is the key,
1331 * 0x09 is the count of 0xFF bytes,
1332 * 0x01 is the uncompressed byte.
1335 * 0xFE -- Compress by nibble blocks.
1337 * Original stream: 0x84210842108421084210
1338 * Compressed stream: 0x0584210
1339 * Detail: 0x05 is the code, means 5 nibbles block.
1340 * 0x84210 is the 5 nibble blocks.
1341 * The whole row is 80 bits given by g_usiDataSize.
1342 * The number of times the block repeat itself
1343 * is found by g_usiDataSize/(4*0x05) which is 4.
1344 * 0xFF -- Compress by the most frequently happen byte.
1346 * Original stream: 0x04020401030904040404
1347 * Compressed stream: 0xFF04(0,1,0x02,0,1,0x01,1,0x03,1,0x09,0,0,0)
1348 * or: 0xFF044090181C240
1349 * Detail: 0xFF is the code, 0x04 is the key.
1350 * a bit of 0 represent the key shall be put into
1351 * the current bit position and a bit of 1
1352 * represent copying the next of 8 bits of data
1357 void ispVMData(unsigned char *ByteData)
1359 /* 09/11/07 NN added local variables initialization */
1360 unsigned short size = 0;
1361 unsigned short i, j, m, getData = 0;
1362 unsigned char cDataByte = 0;
1363 unsigned char compress = 0;
1364 unsigned short FFcount = 0;
1365 unsigned char compr_char = 0xFF;
1366 unsigned short index = 0;
1367 signed char compression = 0;
1369 /*convert number in bits to bytes*/
1370 if (g_usiDataSize % 8 > 0) {
1371 /* 09/11/07 NN Type cast mismatch variables */
1372 size = (unsigned short)(g_usiDataSize / 8 + 1);
1374 /* 09/11/07 NN Type cast mismatch variables */
1375 size = (unsigned short)(g_usiDataSize / 8);
1379 * If there is compression, then check if compress by key
1380 * of 0x00 or 0xFF or by other keys or by nibble blocks
1383 if (g_usDataType & COMPRESS) {
1385 compress = GetByte();
1386 if ((compress == VAR) && (g_usDataType & HEAP_IN)) {
1388 g_usDataType &= ~(HEAP_IN);
1389 compress = GetByte();
1394 /* No compression */
1398 /* Compress by byte 0x00 */
1402 /* Compress by byte 0xFF */
1406 /* Huffman encoding */
1407 compr_char = GetByte();
1409 for (index = 0; index < size; index++) {
1410 ByteData[index] = 0x00;
1412 cDataByte = GetByte();
1415 if ((cDataByte << i++) & 0x80)
1418 ByteData[index] = compr_char;
1422 for (j = 0; j < m; j++) {
1424 cDataByte = GetByte();
1428 ((cDataByte << i++) & 0x80) >> j;
1434 for (index = 0; index < size; index++)
1435 ByteData[index] = 0x00;
1436 for (index = 0; index < compress; index++) {
1438 cDataByte = GetByte();
1439 for (i = 0; i < size * 2 / compress; i++) {
1440 j = (unsigned short)(index +
1441 (i * (unsigned short)compress));
1442 /*clear the nibble to zero first*/
1467 /* Decompress by byte 0x00 or 0xFF */
1468 for (index = 0; index < size; index++) {
1470 cDataByte = GetByte();
1471 if ((cDataByte == VAR) && (g_usDataType&HEAP_IN) &&
1472 !getData && !(g_usDataType&COMPRESS)) {
1474 g_usDataType &= ~(HEAP_IN);
1475 cDataByte = GetByte();
1477 ByteData[index] = cDataByte;
1478 if ((compression) && (cDataByte == compr_char))
1479 /* 09/11/07 NN Type cast mismatch variables */
1480 FFcount = (unsigned short) ispVMDataSize();
1481 /*The number of 0xFF or 0x00 bytes*/
1483 FFcount--; /*Use up the 0xFF chain first*/
1484 ByteData[index] = compr_char;
1489 g_usDataType |= HEAP_IN;
1498 * Processes the SDR/XSDR/SIR commands.
1502 signed char ispVMShift(signed char a_cCode)
1504 /* 09/11/07 NN added local variables initialization */
1505 unsigned short iDataIndex = 0;
1506 unsigned short iReadLoop = 0;
1507 signed char cRetCode = 0;
1510 /* 09/11/07 NN Type cast mismatch variables */
1511 g_usiDataSize = (unsigned short) ispVMDataSize();
1513 /*clear the flags first*/
1514 g_usDataType &= ~(SIR_DATA + EXPRESS + SDR_DATA);
1517 g_usDataType |= SIR_DATA;
1519 * 1/15/04 If performing cascading, then go directly to SHIFTIR.
1520 * Else, go to IRPAUSE before going to SHIFTIR
1522 if (g_usFlowControl & CASCADE) {
1523 ispVMStateMachine(SHIFTIR);
1525 ispVMStateMachine(IRPAUSE);
1526 ispVMStateMachine(SHIFTIR);
1527 if (g_usHeadIR > 0) {
1528 ispVMBypass(HIR, g_usHeadIR);
1534 g_usDataType |= EXPRESS; /*mark simultaneous in and out*/
1536 g_usDataType |= SDR_DATA;
1538 * 1/15/04 If already in SHIFTDR, then do not move state or
1539 * shift in header. This would imply that the previously
1540 * shifted frame was a cascaded frame.
1542 if (g_cCurrentJTAGState != SHIFTDR) {
1544 * 1/15/04 If performing cascading, then go directly
1545 * to SHIFTDR. Else, go to DRPAUSE before going
1548 if (g_usFlowControl & CASCADE) {
1549 if (g_cCurrentJTAGState == DRPAUSE) {
1550 ispVMStateMachine(SHIFTDR);
1552 * 1/15/04 If cascade flag has been seat
1553 * and the current state is DRPAUSE,
1554 * this implies that the first cascaded
1555 * frame is about to be shifted in. The
1556 * header must be shifted prior to
1557 * shifting the first cascaded frame.
1559 if (g_usHeadDR > 0) {
1560 ispVMBypass(HDR, g_usHeadDR);
1564 ispVMStateMachine(SHIFTDR);
1567 ispVMStateMachine(DRPAUSE);
1568 ispVMStateMachine(SHIFTDR);
1569 if (g_usHeadDR > 0) {
1570 ispVMBypass(HDR, g_usHeadDR);
1577 return VME_INVALID_FILE;
1580 cRetCode = ispVMDataCode();
1582 if (cRetCode != 0) {
1583 return VME_INVALID_FILE;
1587 printf("%d ", g_usiDataSize);
1589 if (g_usDataType & TDI_DATA) {
1591 PrintData(g_usiDataSize, g_pucInData);
1594 if (g_usDataType & TDO_DATA) {
1596 PrintData(g_usiDataSize, g_pucOutData);
1599 if (g_usDataType & MASK_DATA) {
1600 puts("\n\t\tMASK ");
1601 PrintData(g_usiDataSize, g_pucOutMaskData);
1604 if (g_usDataType & DMASK_DATA) {
1605 puts("\n\t\tDMASK ");
1606 PrintData(g_usiDataSize, g_pucOutDMaskData);
1612 if (g_usDataType & TDO_DATA || g_usDataType & DMASK_DATA) {
1613 if (g_usDataType & DMASK_DATA) {
1614 cRetCode = ispVMReadandSave(g_usiDataSize);
1616 if (g_usTailDR > 0) {
1618 ispVMBypass(TDR, g_usTailDR);
1620 ispVMStateMachine(DRPAUSE);
1621 ispVMStateMachine(SHIFTDR);
1622 if (g_usHeadDR > 0) {
1623 ispVMBypass(HDR, g_usHeadDR);
1626 for (iDataIndex = 0;
1627 iDataIndex < g_usiDataSize / 8 + 1;
1629 g_pucInData[iDataIndex] =
1630 g_pucOutData[iDataIndex];
1631 g_usDataType &= ~(TDO_DATA + DMASK_DATA);
1632 cRetCode = ispVMSend(g_usiDataSize);
1635 cRetCode = ispVMRead(g_usiDataSize);
1636 if (cRetCode == -1 && g_cVendor == XILINX) {
1637 for (iReadLoop = 0; iReadLoop < 30;
1639 cRetCode = ispVMRead(g_usiDataSize);
1643 /* Always DRPAUSE */
1644 ispVMStateMachine(DRPAUSE);
1646 * Bypass other devices
1649 ispVMBypass(TDR, g_usTailDR);
1650 ispVMStateMachine(g_ucEndDR);
1651 ispVMStateMachine(IDLE);
1657 } else { /*TDI only*/
1658 cRetCode = ispVMSend(g_usiDataSize);
1661 /*transfer the input data to the output buffer for the next verify*/
1662 if ((g_usDataType & EXPRESS) || (a_cCode == SDR)) {
1664 for (iDataIndex = 0; iDataIndex < g_usiDataSize / 8 + 1;
1666 g_pucOutData[iDataIndex] =
1667 g_pucInData[iDataIndex];
1673 /* 1/15/04 If not performing cascading, then shift ENDIR */
1674 if (!(g_usFlowControl & CASCADE)) {
1675 if (g_usTailIR > 0) {
1677 ispVMBypass(TIR, g_usTailIR);
1679 ispVMStateMachine(g_ucEndIR);
1684 /* 1/15/04 If not performing cascading, then shift ENDDR */
1685 if (!(g_usFlowControl & CASCADE)) {
1686 if (g_usTailDR > 0) {
1688 ispVMBypass(TDR, g_usTailDR);
1690 ispVMStateMachine(g_ucEndDR);
1704 * This routine is to extract Header and Trailer parameter for SIR and
1707 * The Header and Trailer parameter are the pre-amble and post-amble bit
1708 * stream need to be shifted into TDI or out of TDO of the devices. Mostly
1709 * is for the purpose of bypassing the leading or trailing devices. ispVM
1710 * supports only shifting data into TDI to bypass the devices.
1712 * For a single device, the header and trailer parameters are all set to 0
1713 * as default by ispVM. If it is for multiple devices, the header and trailer
1714 * value will change as specified by the VME file.
1718 signed char ispVMAmble(signed char Code)
1720 signed char compress = 0;
1721 /* 09/11/07 NN Type cast mismatch variables */
1722 g_usiDataSize = (unsigned short)ispVMDataSize();
1725 printf("%d", g_usiDataSize);
1728 if (g_usiDataSize) {
1731 * Discard the TDI byte and set the compression bit in the data
1732 * type register to false if compression is set because TDI data
1733 * after HIR/HDR/TIR/TDR is not compressed.
1737 if (g_usDataType & COMPRESS) {
1738 g_usDataType &= ~(COMPRESS);
1747 * Store the maximum size of the HIR buffer.
1748 * Used to convert VME to HEX.
1751 if (g_usiDataSize > g_usHIRSize) {
1752 g_usHIRSize = g_usiDataSize;
1756 * Assign the HIR value and allocate memory.
1759 g_usHeadIR = g_usiDataSize;
1761 ispVMMemManager(HIR, g_usHeadIR);
1762 ispVMData(g_pucHIRData);
1766 PrintData(g_usHeadIR, g_pucHIRData);
1773 * Store the maximum size of the TIR buffer.
1774 * Used to convert VME to HEX.
1777 if (g_usiDataSize > g_usTIRSize) {
1778 g_usTIRSize = g_usiDataSize;
1782 * Assign the TIR value and allocate memory.
1785 g_usTailIR = g_usiDataSize;
1787 ispVMMemManager(TIR, g_usTailIR);
1788 ispVMData(g_pucTIRData);
1792 PrintData(g_usTailIR, g_pucTIRData);
1799 * Store the maximum size of the HDR buffer.
1800 * Used to convert VME to HEX.
1803 if (g_usiDataSize > g_usHDRSize) {
1804 g_usHDRSize = g_usiDataSize;
1808 * Assign the HDR value and allocate memory.
1812 g_usHeadDR = g_usiDataSize;
1814 ispVMMemManager(HDR, g_usHeadDR);
1815 ispVMData(g_pucHDRData);
1819 PrintData(g_usHeadDR, g_pucHDRData);
1826 * Store the maximum size of the TDR buffer.
1827 * Used to convert VME to HEX.
1830 if (g_usiDataSize > g_usTDRSize) {
1831 g_usTDRSize = g_usiDataSize;
1835 * Assign the TDR value and allocate memory.
1839 g_usTailDR = g_usiDataSize;
1841 ispVMMemManager(TDR, g_usTailDR);
1842 ispVMData(g_pucTDRData);
1846 PrintData(g_usTailDR, g_pucTDRData);
1856 * Re-enable compression if it was previously set.
1861 g_usDataType |= COMPRESS;
1864 if (g_usiDataSize) {
1866 if (Code == CONTINUE) {
1871 * Encountered invalid opcode.
1874 return VME_INVALID_FILE;
1885 * Perform the function call upon by the REPEAT opcode.
1886 * Memory is to be allocated to store the entire loop from REPEAT to ENDLOOP.
1887 * After the loop is stored then execution begin. The REPEATLOOP flag is set
1888 * on the g_usFlowControl register to indicate the repeat loop is in session
1889 * and therefore fetch opcode from the memory instead of from the file.
1893 signed char ispVMLoop(unsigned short a_usLoopCount)
1895 /* 09/11/07 NN added local variables initialization */
1896 signed char cRetCode = 0;
1897 unsigned short iHeapIndex = 0;
1898 unsigned short iLoopIndex = 0;
1901 for (iHeapIndex = 0; iHeapIndex < g_iHEAPSize; iHeapIndex++) {
1902 g_pucHeapMemory[iHeapIndex] = GetByte();
1905 if (g_pucHeapMemory[iHeapIndex - 1] != ENDLOOP) {
1906 return VME_INVALID_FILE;
1909 g_usFlowControl |= REPEATLOOP;
1910 g_usDataType |= HEAP_IN;
1912 for (iLoopIndex = 0; iLoopIndex < a_usLoopCount; iLoopIndex++) {
1914 cRetCode = ispVMCode();
1921 g_usDataType &= ~(HEAP_IN);
1922 g_usFlowControl &= ~(REPEATLOOP);
1930 * Shift the TDI stream left or right by the number of bits. The data in
1931 * *g_pucInData is of the VME format, so the actual shifting is the reverse of
1932 * IEEE 1532 or SVF format.
1936 signed char ispVMBitShift(signed char mode, unsigned short bits)
1938 /* 09/11/07 NN added local variables initialization */
1939 unsigned short i = 0;
1940 unsigned short size = 0;
1941 unsigned short tmpbits = 0;
1943 if (g_usiDataSize % 8 > 0) {
1944 /* 09/11/07 NN Type cast mismatch variables */
1945 size = (unsigned short)(g_usiDataSize / 8 + 1);
1947 /* 09/11/07 NN Type cast mismatch variables */
1948 size = (unsigned short)(g_usiDataSize / 8);
1953 for (i = 0; i < size; i++) {
1954 if (g_pucInData[i] != 0) {
1956 while (tmpbits > 0) {
1957 g_pucInData[i] <<= 1;
1958 if (g_pucInData[i] == 0) {
1968 for (i = 0; i < size; i++) {
1969 if (g_pucInData[i] != 0) {
1971 while (tmpbits > 0) {
1972 g_pucInData[i] >>= 1;
1973 if (g_pucInData[i] == 0) {
1983 return VME_INVALID_FILE;
1993 * Displays the SVF comments.
1997 void ispVMComment(unsigned short a_usCommentSize)
2000 for (; a_usCommentSize > 0; a_usCommentSize--) {
2003 * Print character to the terminal.
2006 cCurByte = GetByte();
2007 vme_out_char(cCurByte);
2010 vme_out_char(cCurByte);
2017 * Iterate the length of the header and discard it.
2021 void ispVMHeader(unsigned short a_usHeaderSize)
2023 for (; a_usHeaderSize > 0; a_usHeaderSize--) {
2030 * ispVMCalculateCRC32
2032 * Calculate the 32-bit CRC.
2036 void ispVMCalculateCRC32(unsigned char a_ucData)
2038 /* 09/11/07 NN added local variables initialization */
2039 unsigned char ucIndex = 0;
2040 unsigned char ucFlipData = 0;
2041 unsigned short usCRCTableEntry = 0;
2042 unsigned int crc_table[16] = {
2043 0x0000, 0xCC01, 0xD801,
2044 0x1400, 0xF001, 0x3C00,
2045 0x2800, 0xE401, 0xA001,
2046 0x6C00, 0x7800, 0xB401,
2047 0x5000, 0x9C01, 0x8801,
2051 for (ucIndex = 0; ucIndex < 8; ucIndex++) {
2053 if (a_ucData & 0x01) {
2059 /* 09/11/07 NN Type cast mismatch variables */
2060 usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
2061 g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
2062 g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
2063 usCRCTableEntry ^ crc_table[ucFlipData & 0xF]);
2064 usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
2065 g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
2066 g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
2067 usCRCTableEntry ^ crc_table[(ucFlipData >> 4) & 0xF]);
2074 * Process the intelligent programming loops.
2078 signed char ispVMLCOUNT(unsigned short a_usCountSize)
2080 unsigned short usContinue = 1;
2081 unsigned short usIntelBufferIndex = 0;
2082 unsigned short usCountIndex = 0;
2083 signed char cRetCode = 0;
2084 signed char cRepeatHeap = 0;
2085 signed char cOpcode = 0;
2086 unsigned char ucState = 0;
2087 unsigned short usDelay = 0;
2088 unsigned short usToggle = 0;
2090 g_usIntelBufferSize = (unsigned short)ispVMDataSize();
2093 * Allocate memory for intel buffer.
2097 ispVMMemManager(LHEAP, g_usIntelBufferSize);
2100 * Store the maximum size of the intelligent buffer.
2101 * Used to convert VME to HEX.
2104 if (g_usIntelBufferSize > g_usLCOUNTSize) {
2105 g_usLCOUNTSize = g_usIntelBufferSize;
2109 * Copy intel data to the buffer.
2112 for (usIntelBufferIndex = 0; usIntelBufferIndex < g_usIntelBufferSize;
2113 usIntelBufferIndex++) {
2114 g_pucIntelBuffer[usIntelBufferIndex] = GetByte();
2118 * Set the data type register to get data from the intelligent
2122 g_usDataType |= LHEAP_IN;
2126 * If the HEAP_IN flag is set, temporarily unset the flag so data will be
2127 * retrieved from the status buffer.
2131 if (g_usDataType & HEAP_IN) {
2132 g_usDataType &= ~HEAP_IN;
2137 printf("LCOUNT %d;\n", a_usCountSize);
2141 * Iterate through the intelligent programming command.
2144 for (usCountIndex = 0; usCountIndex < a_usCountSize; usCountIndex++) {
2148 * Initialize the intel data index to 0 before each iteration.
2152 g_usIntelDataIndex = 0;
2161 * Begin looping through all the VME opcodes.
2165 * 4/1/09 Nguyen replaced the recursive function call codes on
2166 * the ispVMLCOUNT function
2169 while (usContinue) {
2170 cOpcode = GetByte();
2177 * Set the header/trailer of the device in order
2178 * to bypass successfully.
2181 ispVMAmble(cOpcode);
2186 * Step the JTAG state machine.
2189 ucState = GetByte();
2191 * Step the JTAG state machine to DRCAPTURE
2192 * to support Looping.
2195 if ((g_usDataType & LHEAP_IN) &&
2196 (ucState == DRPAUSE) &&
2197 (g_cCurrentJTAGState == ucState)) {
2198 ispVMStateMachine(DRCAPTURE);
2200 ispVMStateMachine(ucState);
2202 printf("LDELAY %s ", GetState(ucState));
2210 * Shift in data into the device.
2213 cRetCode = ispVMShift(cOpcode);
2221 * Shift in data into the device.
2224 cRetCode = ispVMShift(cOpcode);
2234 usDelay = (unsigned short)ispVMDataSize();
2235 ispVMDelay(usDelay);
2238 if (usDelay & 0x8000) {
2241 * Since MSB is set, the delay time must
2242 * be decoded to millisecond. The
2243 * SVF2VME encodes the MSB to represent
2248 printf("%.2E SEC;\n",
2249 (float) usDelay / 1000);
2252 * Since MSB is not set, the delay time
2253 * is given as microseconds.
2256 printf("%.2E SEC;\n",
2257 (float) usDelay / 1000000);
2264 * Issue clock toggles.
2267 usToggle = (unsigned short)ispVMDataSize();
2268 ispVMClocks(usToggle);
2271 printf("RUNTEST %d TCK;\n", usToggle);
2277 * Exit point from processing loops.
2288 ispVMComment((unsigned short) ispVMDataSize());
2291 ucState = GetByte();
2292 if ((ucState == ON) || (ucState == 0x01))
2293 writePort(g_ucPinENABLE, 0x01);
2295 writePort(g_ucPinENABLE, 0x00);
2299 if (GetByte() == 0x01)
2300 writePort(g_ucPinTRST, 0x01);
2302 writePort(g_ucPinTRST, 0x00);
2308 * Invalid opcode encountered.
2311 debug("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
2313 return VME_INVALID_FILE;
2316 if (cRetCode >= 0) {
2318 * Break if intelligent programming is successful.
2326 * If HEAP_IN flag was temporarily disabled,
2327 * re-enable it before exiting
2331 g_usDataType |= HEAP_IN;
2335 * Set the data type register to not get data from the
2336 * intelligent data buffer.
2339 g_usDataType &= ~LHEAP_IN;
2346 * Applies the specified number of pulses to TCK.
2350 void ispVMClocks(unsigned short Clocks)
2352 unsigned short iClockIndex = 0;
2353 for (iClockIndex = 0; iClockIndex < Clocks; iClockIndex++) {
2362 * This procedure takes care of the HIR, HDR, TIR, TDR for the
2363 * purpose of putting the other devices into Bypass mode. The
2364 * current state is checked to find out if it is at DRPAUSE or
2365 * IRPAUSE. If it is at DRPAUSE, perform bypass register scan.
2366 * If it is at IRPAUSE, scan into instruction registers the bypass
2371 void ispVMBypass(signed char ScanType, unsigned short Bits)
2373 /* 09/11/07 NN added local variables initialization */
2374 unsigned short iIndex = 0;
2375 unsigned short iSourceIndex = 0;
2376 unsigned char cBitState = 0;
2377 unsigned char cCurByte = 0;
2378 unsigned char *pcSource = NULL;
2386 pcSource = g_pucHIRData;
2389 pcSource = g_pucTIRData;
2392 pcSource = g_pucHDRData;
2395 pcSource = g_pucTDRData;
2403 for (iIndex = 0; iIndex < Bits - 1; iIndex++) {
2404 /* Scan instruction or bypass register */
2405 if (iIndex % 8 == 0) {
2406 cCurByte = pcSource[iSourceIndex++];
2408 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2410 writePort(g_ucPinTDI, cBitState);
2414 if (iIndex % 8 == 0) {
2415 cCurByte = pcSource[iSourceIndex++];
2418 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2420 writePort(g_ucPinTDI, cBitState);
2427 * This procedure steps all devices in the daisy chain from a given
2428 * JTAG state to the next desirable state. If the next state is TLR,
2429 * the JTAG state machine is brute forced into TLR by driving TMS
2430 * high and pulse TCK 6 times.
2434 void ispVMStateMachine(signed char cNextJTAGState)
2436 /* 09/11/07 NN added local variables initialization */
2437 signed char cPathIndex = 0;
2438 signed char cStateIndex = 0;
2440 if ((g_cCurrentJTAGState == cNextJTAGState) &&
2441 (cNextJTAGState != RESET)) {
2445 for (cStateIndex = 0; cStateIndex < 25; cStateIndex++) {
2446 if ((g_cCurrentJTAGState ==
2447 g_JTAGTransistions[cStateIndex].CurState) &&
2449 g_JTAGTransistions[cStateIndex].NextState)) {
2454 g_cCurrentJTAGState = cNextJTAGState;
2455 for (cPathIndex = 0;
2456 cPathIndex < g_JTAGTransistions[cStateIndex].Pulses;
2458 if ((g_JTAGTransistions[cStateIndex].Pattern << cPathIndex)
2460 writePort(g_ucPinTMS, (unsigned char) 0x01);
2462 writePort(g_ucPinTMS, (unsigned char) 0x00);
2467 writePort(g_ucPinTDI, 0x00);
2468 writePort(g_ucPinTMS, 0x00);
2475 * Enable the port to the device and set the state to RESET (TLR).
2482 printf("// ISPVM EMBEDDED ADDED\n");
2483 printf("STATE RESET;\n");
2485 g_usFlowControl = 0;
2486 g_usDataType = g_uiChecksumIndex = g_cCurrentJTAGState = 0;
2487 g_usHeadDR = g_usHeadIR = g_usTailDR = g_usTailIR = 0;
2488 g_usMaxSize = g_usShiftValue = g_usRepeatLoops = 0;
2489 g_usTDOSize = g_usMASKSize = g_usTDISize = 0;
2490 g_usDMASKSize = g_usLCOUNTSize = g_usHDRSize = 0;
2491 g_usTDRSize = g_usHIRSize = g_usTIRSize = g_usHeapSize = 0;
2493 g_usLVDSPairCount = 0;
2496 ispVMStateMachine(RESET); /*step devices to RESET state*/
2503 * Set the state of devices to RESET to enable the devices and disable
2511 printf("// ISPVM EMBEDDED ADDED\n");
2512 printf("STATE RESET;\n");
2513 printf("RUNTEST 1.00E-001 SEC;\n");
2516 ispVMStateMachine(RESET); /*step devices to RESET state */
2517 ispVMDelay(1000); /*wake up devices*/
2524 * Send the TDI data stream to devices. The data stream can be
2525 * instructions or data.
2529 signed char ispVMSend(unsigned short a_usiDataSize)
2531 /* 09/11/07 NN added local variables initialization */
2532 unsigned short iIndex = 0;
2533 unsigned short iInDataIndex = 0;
2534 unsigned char cCurByte = 0;
2535 unsigned char cBitState = 0;
2537 for (iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++) {
2538 if (iIndex % 8 == 0) {
2539 cCurByte = g_pucInData[iInDataIndex++];
2541 cBitState = (unsigned char)(((cCurByte << iIndex % 8) & 0x80)
2543 writePort(g_ucPinTDI, cBitState);
2547 if (iIndex % 8 == 0) {
2548 /* Take care of the last bit */
2549 cCurByte = g_pucInData[iInDataIndex];
2552 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2555 writePort(g_ucPinTDI, cBitState);
2556 if (g_usFlowControl & CASCADE) {
2557 /*1/15/04 Clock in last bit for the first n-1 cascaded frames */
2568 * Read the data stream from devices and verify.
2572 signed char ispVMRead(unsigned short a_usiDataSize)
2574 /* 09/11/07 NN added local variables initialization */
2575 unsigned short usDataSizeIndex = 0;
2576 unsigned short usErrorCount = 0;
2577 unsigned short usLastBitIndex = 0;
2578 unsigned char cDataByte = 0;
2579 unsigned char cMaskByte = 0;
2580 unsigned char cInDataByte = 0;
2581 unsigned char cCurBit = 0;
2582 unsigned char cByteIndex = 0;
2583 unsigned short usBufferIndex = 0;
2584 unsigned char ucDisplayByte = 0x00;
2585 unsigned char ucDisplayFlag = 0x01;
2586 char StrChecksum[256] = {0};
2587 unsigned char g_usCalculateChecksum = 0x00;
2589 /* 09/11/07 NN Type cast mismatch variables */
2590 usLastBitIndex = (unsigned short)(a_usiDataSize - 1);
2594 * If mask is not all zeros, then set the display flag to 0x00,
2595 * otherwise it shall be set to 0x01 to indicate that data read
2596 * from the device shall be displayed. If DEBUG is defined,
2597 * always display data.
2600 for (usDataSizeIndex = 0; usDataSizeIndex < (a_usiDataSize + 7) / 8;
2601 usDataSizeIndex++) {
2602 if (g_usDataType & MASK_DATA) {
2603 if (g_pucOutMaskData[usDataSizeIndex] != 0x00) {
2604 ucDisplayFlag = 0x00;
2607 } else if (g_usDataType & CMASK_DATA) {
2608 g_usCalculateChecksum = 0x01;
2609 ucDisplayFlag = 0x00;
2612 ucDisplayFlag = 0x00;
2620 * Begin shifting data in and out of the device.
2624 for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
2625 usDataSizeIndex++) {
2626 if (cByteIndex == 0) {
2629 * Grab byte from TDO buffer.
2632 if (g_usDataType & TDO_DATA) {
2633 cDataByte = g_pucOutData[usBufferIndex];
2637 * Grab byte from MASK buffer.
2640 if (g_usDataType & MASK_DATA) {
2641 cMaskByte = g_pucOutMaskData[usBufferIndex];
2647 * Grab byte from CMASK buffer.
2650 if (g_usDataType & CMASK_DATA) {
2652 g_usCalculateChecksum = 0x01;
2656 * Grab byte from TDI buffer.
2659 if (g_usDataType & TDI_DATA) {
2660 cInDataByte = g_pucInData[usBufferIndex];
2666 cCurBit = readPort();
2668 if (ucDisplayFlag) {
2669 ucDisplayByte <<= 1;
2670 ucDisplayByte |= cCurBit;
2674 * Check if data read from port matches with expected TDO.
2677 if (g_usDataType & TDO_DATA) {
2678 /* 08/28/08 NN Added Calculate checksum support. */
2679 if (g_usCalculateChecksum) {
2680 if (cCurBit == 0x01)
2682 (1 << (g_uiChecksumIndex % 8));
2683 g_uiChecksumIndex++;
2685 if ((((cMaskByte << cByteIndex) & 0x80)
2687 if (cCurBit != (unsigned char)
2688 (((cDataByte << cByteIndex) & 0x80)
2697 * Write TDI data to the port.
2700 writePort(g_ucPinTDI,
2701 (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
2704 if (usDataSizeIndex < usLastBitIndex) {
2707 * Clock data out from the data shift register.
2711 } else if (g_usFlowControl & CASCADE) {
2714 * Clock in last bit for the first N - 1 cascaded frames
2721 * Increment the byte index. If it exceeds 7, then reset it back
2726 if (cByteIndex >= 8) {
2727 if (ucDisplayFlag) {
2730 * Store displayed data in the TDO buffer. By reusing
2731 * the TDO buffer to store displayed data, there is no
2732 * need to allocate a buffer simply to hold display
2733 * data. This will not cause any false verification
2734 * errors because the true TDO byte has already
2738 g_pucOutData[usBufferIndex - 1] = ucDisplayByte;
2744 /* 09/12/07 Nguyen changed to display the 1 bit expected data */
2745 else if (a_usiDataSize == 1) {
2746 if (ucDisplayFlag) {
2749 * Store displayed data in the TDO buffer.
2750 * By reusing the TDO buffer to store displayed
2751 * data, there is no need to allocate
2752 * a buffer simply to hold display data. This
2753 * will not cause any false verification errors
2754 * because the true TDO byte has already
2759 * Flip ucDisplayByte and store it in cDataByte.
2762 for (usBufferIndex = 0; usBufferIndex < 8;
2765 if (ucDisplayByte & 0x01) {
2768 ucDisplayByte >>= 1;
2770 g_pucOutData[0] = cDataByte;
2778 if (ucDisplayFlag) {
2781 debug("RECEIVED TDO (");
2783 vme_out_string("Display Data: 0x");
2786 /* 09/11/07 NN Type cast mismatch variables */
2787 for (usDataSizeIndex = (unsigned short)
2788 ((a_usiDataSize + 7) / 8);
2789 usDataSizeIndex > 0 ; usDataSizeIndex--) {
2790 cMaskByte = g_pucOutData[usDataSizeIndex - 1];
2794 * Flip cMaskByte and store it in cDataByte.
2797 for (usBufferIndex = 0; usBufferIndex < 8;
2800 if (cMaskByte & 0x01) {
2806 printf("%.2X", cDataByte);
2807 if ((((a_usiDataSize + 7) / 8) - usDataSizeIndex)
2812 vme_out_hex(cDataByte);
2819 vme_out_string("\n\n");
2821 /* 09/02/08 Nguyen changed to display the data Checksum */
2822 if (g_usChecksum != 0) {
2823 g_usChecksum &= 0xFFFF;
2824 sprintf(StrChecksum, "Data Checksum: %.4lX\n\n",
2826 vme_out_string(StrChecksum);
2831 if (usErrorCount > 0) {
2832 if (g_usFlowControl & VERIFYUES) {
2834 "USERCODE verification failed. "
2835 "Continue programming......\n\n");
2836 g_usFlowControl &= ~(VERIFYUES);
2841 printf("TOTAL ERRORS: %d\n", usErrorCount);
2844 return VME_VERIFICATION_FAILURE;
2847 if (g_usFlowControl & VERIFYUES) {
2848 vme_out_string("USERCODE verification passed. "
2849 "Programming aborted.\n\n");
2850 g_usFlowControl &= ~(VERIFYUES);
2862 * Support dynamic I/O.
2866 signed char ispVMReadandSave(unsigned short int a_usiDataSize)
2868 /* 09/11/07 NN added local variables initialization */
2869 unsigned short int usDataSizeIndex = 0;
2870 unsigned short int usLastBitIndex = 0;
2871 unsigned short int usBufferIndex = 0;
2872 unsigned short int usOutBitIndex = 0;
2873 unsigned short int usLVDSIndex = 0;
2874 unsigned char cDataByte = 0;
2875 unsigned char cDMASKByte = 0;
2876 unsigned char cInDataByte = 0;
2877 unsigned char cCurBit = 0;
2878 unsigned char cByteIndex = 0;
2879 signed char cLVDSByteIndex = 0;
2881 /* 09/11/07 NN Type cast mismatch variables */
2882 usLastBitIndex = (unsigned short) (a_usiDataSize - 1);
2886 * Iterate through the data bits.
2890 for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
2891 usDataSizeIndex++) {
2892 if (cByteIndex == 0) {
2895 * Grab byte from DMASK buffer.
2898 if (g_usDataType & DMASK_DATA) {
2899 cDMASKByte = g_pucOutDMaskData[usBufferIndex];
2905 * Grab byte from TDI buffer.
2908 if (g_usDataType & TDI_DATA) {
2909 cInDataByte = g_pucInData[usBufferIndex];
2915 cCurBit = readPort();
2916 cDataByte = (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
2920 * Initialize the byte to be zero.
2923 if (usOutBitIndex % 8 == 0) {
2924 g_pucOutData[usOutBitIndex / 8] = 0x00;
2928 * Use TDI, DMASK, and device TDO to create new TDI (actually
2929 * stored in g_pucOutData).
2932 if ((((cDMASKByte << cByteIndex) & 0x80) ? 0x01 : 0x00)) {
2935 for (usLVDSIndex = 0;
2936 usLVDSIndex < g_usLVDSPairCount;
2938 if (g_pLVDSList[usLVDSIndex].
2941 g_pLVDSList[usLVDSIndex].
2949 * DMASK bit is 1, use TDI.
2952 g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
2953 (((cDataByte & 0x1) ? 0x01 : 0x00) <<
2954 (7 - usOutBitIndex % 8));
2958 * DMASK bit is 0, use device TDO.
2961 g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
2962 (((cCurBit & 0x1) ? 0x01 : 0x00) <<
2963 (7 - usOutBitIndex % 8));
2967 * Shift in TDI in order to get TDO out.
2971 writePort(g_ucPinTDI, cDataByte);
2972 if (usDataSizeIndex < usLastBitIndex) {
2977 * Increment the byte index. If it exceeds 7, then reset it back
2982 if (cByteIndex >= 8) {
2988 * If g_pLVDSList exists and pairs need updating, then update
2989 * the negative-pair to receive the flipped positive-pair value.
2993 for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount;
2995 if (g_pLVDSList[usLVDSIndex].ucUpdate) {
2998 * Read the positive value and flip it.
3001 cDataByte = (unsigned char)
3002 (((g_pucOutData[g_pLVDSList[usLVDSIndex].
3003 usPositiveIndex / 8]
3004 << (g_pLVDSList[usLVDSIndex].
3005 usPositiveIndex % 8)) & 0x80) ?
3007 /* 09/11/07 NN Type cast mismatch variables */
3008 cDataByte = (unsigned char) (!cDataByte);
3011 * Get the byte that needs modification.
3015 g_pucOutData[g_pLVDSList[usLVDSIndex].
3016 usNegativeIndex / 8];
3021 * Copy over the current byte and
3022 * set the negative bit to 1.
3026 for (cLVDSByteIndex = 7;
3027 cLVDSByteIndex >= 0;
3031 (g_pLVDSList[usLVDSIndex].
3032 usNegativeIndex % 8) ==
3036 * Set negative bit to 1
3040 } else if (cInDataByte & 0x80) {
3048 * Store the modified byte.
3051 g_pucOutData[g_pLVDSList[usLVDSIndex].
3052 usNegativeIndex / 8] = cDataByte;
3056 * Copy over the current byte and set
3057 * the negative bit to 0.
3061 for (cLVDSByteIndex = 7;
3062 cLVDSByteIndex >= 0;
3066 (g_pLVDSList[usLVDSIndex].
3067 usNegativeIndex % 8) ==
3071 * Set negative bit to 0
3075 } else if (cInDataByte & 0x80) {
3083 * Store the modified byte.
3086 g_pucOutData[g_pLVDSList[usLVDSIndex].
3087 usNegativeIndex / 8] = cDataByte;
3098 signed char ispVMProcessLVDS(unsigned short a_usLVDSCount)
3100 unsigned short usLVDSIndex = 0;
3103 * Allocate memory to hold LVDS pairs.
3106 ispVMMemManager(LVDS, a_usLVDSCount);
3107 g_usLVDSPairCount = a_usLVDSCount;
3110 printf("LVDS %d (", a_usLVDSCount);
3114 * Iterate through each given LVDS pair.
3117 for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount; usLVDSIndex++) {
3120 * Assign the positive and negative indices of the LVDS pair.
3123 /* 09/11/07 NN Type cast mismatch variables */
3124 g_pLVDSList[usLVDSIndex].usPositiveIndex =
3125 (unsigned short) ispVMDataSize();
3126 /* 09/11/07 NN Type cast mismatch variables */
3127 g_pLVDSList[usLVDSIndex].usNegativeIndex =
3128 (unsigned short)ispVMDataSize();
3131 if (usLVDSIndex < g_usLVDSPairCount - 1) {
3133 g_pLVDSList[usLVDSIndex].usPositiveIndex,
3134 g_pLVDSList[usLVDSIndex].usNegativeIndex);
3137 g_pLVDSList[usLVDSIndex].usPositiveIndex,
3138 g_pLVDSList[usLVDSIndex].usNegativeIndex);