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 * See file CREDITS for list of people who contributed to this
32 * This program is free software; you can redistribute it and/or
33 * modify it under the terms of the GNU General Public License as
34 * published by the Free Software Foundation; either version 2 of
35 * the License, or (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
49 #include <linux/string.h>
53 #define vme_out_char(c) printf("%c", c)
54 #define vme_out_hex(c) printf("%x", c)
55 #define vme_out_string(s) printf("%s", s)
59 * Global variables used to specify the flow control and data type.
61 * g_usFlowControl: flow control register. Each bit in the
62 * register can potentially change the
63 * personality of the embedded engine.
64 * g_usDataType: holds the data type of the current row.
68 static unsigned short g_usFlowControl;
69 unsigned short g_usDataType;
73 * Global variables used to specify the ENDDR and ENDIR.
75 * g_ucEndDR: the state that the device goes to after SDR.
76 * g_ucEndIR: the state that the device goes to after SIR.
80 unsigned char g_ucEndDR = DRPAUSE;
81 unsigned char g_ucEndIR = IRPAUSE;
85 * Global variables used to support header/trailer.
87 * g_usHeadDR: the number of lead devices in bypass.
88 * g_usHeadIR: the sum of IR length of lead devices.
89 * g_usTailDR: the number of tail devices in bypass.
90 * g_usTailIR: the sum of IR length of tail devices.
94 static unsigned short g_usHeadDR;
95 static unsigned short g_usHeadIR;
96 static unsigned short g_usTailDR;
97 static unsigned short g_usTailIR;
101 * Global variable to store the number of bits of data or instruction
102 * to be shifted into or out from the device.
106 static unsigned short g_usiDataSize;
110 * Stores the frequency. Default to 1 MHz.
114 static int g_iFrequency = 1000;
118 * Stores the maximum amount of ram needed to hold a row of data.
122 static unsigned short g_usMaxSize;
126 * Stores the LSH or RSH value.
130 static unsigned short g_usShiftValue;
134 * Stores the current repeat loop value.
138 static unsigned short g_usRepeatLoops;
142 * Stores the current vendor.
146 static signed char g_cVendor = LATTICE;
150 * Stores the VME file CRC.
154 unsigned short g_usCalculatedCRC;
158 * Stores the Device Checksum.
161 /* 08/28/08 NN Added Calculate checksum support. */
162 unsigned long g_usChecksum;
163 static unsigned int g_uiChecksumIndex;
167 * Stores the current state of the JTAG state machine.
171 static signed char g_cCurrentJTAGState;
175 * Global variables used to support looping.
177 * g_pucHeapMemory: holds the entire repeat loop.
178 * g_iHeapCounter: points to the current byte in the repeat loop.
179 * g_iHEAPSize: the current size of the repeat in bytes.
183 unsigned char *g_pucHeapMemory;
184 unsigned short g_iHeapCounter;
185 unsigned short g_iHEAPSize;
186 static unsigned short previous_size;
190 * Global variables used to support intelligent programming.
192 * g_usIntelDataIndex: points to the current byte of the
193 * intelligent buffer.
194 * g_usIntelBufferSize: holds the size of the intelligent
199 unsigned short g_usIntelDataIndex;
200 unsigned short g_usIntelBufferSize;
204 * Supported VME versions.
208 const char *const g_szSupportedVersions[] = {
209 "__VME2.0", "__VME3.0", "____12.0", "____12.1", 0};
213 * Holds the maximum size of each respective buffer. These variables are used
214 * to write the HEX files when converting VME to HEX.
218 static unsigned short g_usTDOSize;
219 static unsigned short g_usMASKSize;
220 static unsigned short g_usTDISize;
221 static unsigned short g_usDMASKSize;
222 static unsigned short g_usLCOUNTSize;
223 static unsigned short g_usHDRSize;
224 static unsigned short g_usTDRSize;
225 static unsigned short g_usHIRSize;
226 static unsigned short g_usTIRSize;
227 static unsigned short g_usHeapSize;
231 * Global variables used to store data.
233 * g_pucOutMaskData: local RAM to hold one row of MASK data.
234 * g_pucInData: local RAM to hold one row of TDI data.
235 * g_pucOutData: local RAM to hold one row of TDO data.
236 * g_pucHIRData: local RAM to hold the current SIR header.
237 * g_pucTIRData: local RAM to hold the current SIR trailer.
238 * g_pucHDRData: local RAM to hold the current SDR header.
239 * g_pucTDRData: local RAM to hold the current SDR trailer.
240 * g_pucIntelBuffer: local RAM to hold the current intelligent buffer
241 * g_pucOutDMaskData: local RAM to hold one row of DMASK data.
245 unsigned char *g_pucOutMaskData = NULL,
247 *g_pucOutData = NULL,
248 *g_pucHIRData = NULL,
249 *g_pucTIRData = NULL,
250 *g_pucHDRData = NULL,
251 *g_pucTDRData = NULL,
252 *g_pucIntelBuffer = NULL,
253 *g_pucOutDMaskData = NULL;
257 * JTAG state machine transition table.
262 unsigned char CurState; /* From this state */
263 unsigned char NextState; /* Step to this state */
264 unsigned char Pattern; /* The tragetory of TMS */
265 unsigned char Pulses; /* The number of steps */
266 } g_JTAGTransistions[25] = {
267 { RESET, RESET, 0xFC, 6 }, /* Transitions from RESET */
268 { RESET, IDLE, 0x00, 1 },
269 { RESET, DRPAUSE, 0x50, 5 },
270 { RESET, IRPAUSE, 0x68, 6 },
271 { IDLE, RESET, 0xE0, 3 }, /* Transitions from IDLE */
272 { IDLE, DRPAUSE, 0xA0, 4 },
273 { IDLE, IRPAUSE, 0xD0, 5 },
274 { DRPAUSE, RESET, 0xF8, 5 }, /* Transitions from DRPAUSE */
275 { DRPAUSE, IDLE, 0xC0, 3 },
276 { DRPAUSE, IRPAUSE, 0xF4, 7 },
277 { DRPAUSE, DRPAUSE, 0xE8, 6 },/* 06/14/06 Support POLL STATUS LOOP*/
278 { IRPAUSE, RESET, 0xF8, 5 }, /* Transitions from IRPAUSE */
279 { IRPAUSE, IDLE, 0xC0, 3 },
280 { IRPAUSE, DRPAUSE, 0xE8, 6 },
281 { DRPAUSE, SHIFTDR, 0x80, 2 }, /* Extra transitions using SHIFTDR */
282 { IRPAUSE, SHIFTDR, 0xE0, 5 },
283 { SHIFTDR, DRPAUSE, 0x80, 2 },
284 { SHIFTDR, IDLE, 0xC0, 3 },
285 { IRPAUSE, SHIFTIR, 0x80, 2 },/* Extra transitions using SHIFTIR */
286 { SHIFTIR, IRPAUSE, 0x80, 2 },
287 { SHIFTIR, IDLE, 0xC0, 3 },
288 { DRPAUSE, DRCAPTURE, 0xE0, 4 }, /* 11/15/05 Support DRCAPTURE*/
289 { DRCAPTURE, DRPAUSE, 0x80, 2 },
290 { IDLE, DRCAPTURE, 0x80, 2 },
291 { IRPAUSE, DRCAPTURE, 0xE0, 4 }
296 * List to hold all LVDS pairs.
300 LVDSPair *g_pLVDSList;
301 unsigned short g_usLVDSPairCount;
305 * Function prototypes.
309 static signed char ispVMDataCode(void);
310 static long int ispVMDataSize(void);
311 static void ispVMData(unsigned char *Data);
312 static signed char ispVMShift(signed char Code);
313 static signed char ispVMAmble(signed char Code);
314 static signed char ispVMLoop(unsigned short a_usLoopCount);
315 static signed char ispVMBitShift(signed char mode, unsigned short bits);
316 static void ispVMComment(unsigned short a_usCommentSize);
317 static void ispVMHeader(unsigned short a_usHeaderSize);
318 static signed char ispVMLCOUNT(unsigned short a_usCountSize);
319 static void ispVMClocks(unsigned short Clocks);
320 static void ispVMBypass(signed char ScanType, unsigned short Bits);
321 static void ispVMStateMachine(signed char NextState);
322 static signed char ispVMSend(unsigned short int);
323 static signed char ispVMRead(unsigned short int);
324 static signed char ispVMReadandSave(unsigned short int);
325 static signed char ispVMProcessLVDS(unsigned short a_usLVDSCount);
326 static void ispVMMemManager(signed char types, unsigned short size);
330 * External variables and functions in hardware.c module
333 static signed char g_cCurrentJTAGState;
341 * Returns the state as a string based on the opcode. Only used
342 * for debugging purposes.
346 const char *GetState(unsigned char a_ucState)
361 case DRCAPTURE:/* 11/15/05 support DRCAPTURE*/
374 * Prints the data. Only used for debugging purposes.
378 void PrintData(unsigned short a_iDataSize, unsigned char *a_pucData)
380 /* 09/11/07 NN added local variables initialization */
381 unsigned short usByteSize = 0;
382 unsigned short usBitIndex = 0;
383 signed short usByteIndex = 0;
384 unsigned char ucByte = 0;
385 unsigned char ucFlipByte = 0;
387 if (a_iDataSize % 8) {
388 /* 09/11/07 NN Type cast mismatch variables */
389 usByteSize = (unsigned short)(a_iDataSize / 8 + 1);
391 /* 09/11/07 NN Type cast mismatch variables */
392 usByteSize = (unsigned short)(a_iDataSize / 8);
395 /* 09/11/07 NN Type cast mismatch variables */
396 for (usByteIndex = (signed short)(usByteSize - 1);
397 usByteIndex >= 0; usByteIndex--) {
398 ucByte = a_pucData[usByteIndex];
407 for (usBitIndex = 0; usBitIndex < 8; usBitIndex++) {
418 * Print the flipped byte.
422 printf("%.02X", ucFlipByte);
423 if ((usByteSize - usByteIndex) % 40 == 39) {
433 void ispVMMemManager(signed char cTarget, unsigned short usSize)
438 if (g_pucInData != NULL) {
439 if (previous_size == usSize) {/*memory exist*/
446 g_pucInData = (unsigned char *) malloc(usSize / 8 + 2);
447 previous_size = usSize;
450 if (g_pucOutData != NULL) {
451 if (previous_size == usSize) { /*already exist*/
458 g_pucOutData = (unsigned char *) malloc(usSize / 8 + 2);
459 previous_size = usSize;
462 if (g_pucOutMaskData != NULL) {
463 if (previous_size == usSize) {/*already allocated*/
466 free(g_pucOutMaskData);
467 g_pucOutMaskData = NULL;
470 g_pucOutMaskData = (unsigned char *) malloc(usSize / 8 + 2);
471 previous_size = usSize;
474 if (g_pucHIRData != NULL) {
478 g_pucHIRData = (unsigned char *) malloc(usSize / 8 + 2);
481 if (g_pucTIRData != NULL) {
485 g_pucTIRData = (unsigned char *) malloc(usSize / 8 + 2);
488 if (g_pucHDRData != NULL) {
492 g_pucHDRData = (unsigned char *) malloc(usSize / 8 + 2);
495 if (g_pucTDRData != NULL) {
499 g_pucTDRData = (unsigned char *) malloc(usSize / 8 + 2);
502 if (g_pucHeapMemory != NULL) {
503 free(g_pucHeapMemory);
504 g_pucHeapMemory = NULL;
506 g_pucHeapMemory = (unsigned char *) malloc(usSize + 2);
509 if (g_pucOutDMaskData != NULL) {
510 if (previous_size == usSize) { /*already allocated*/
513 free(g_pucOutDMaskData);
514 g_pucOutDMaskData = NULL;
517 g_pucOutDMaskData = (unsigned char *) malloc(usSize / 8 + 2);
518 previous_size = usSize;
521 if (g_pucIntelBuffer != NULL) {
522 free(g_pucIntelBuffer);
523 g_pucIntelBuffer = NULL;
525 g_pucIntelBuffer = (unsigned char *) malloc(usSize + 2);
528 if (g_pLVDSList != NULL) {
532 g_pLVDSList = (LVDSPair *) malloc(usSize * sizeof(LVDSPair));
534 memset(g_pLVDSList, 0, usSize * sizeof(LVDSPair));
541 void ispVMFreeMem(void)
543 if (g_pucHeapMemory != NULL) {
544 free(g_pucHeapMemory);
545 g_pucHeapMemory = NULL;
548 if (g_pucOutMaskData != NULL) {
549 free(g_pucOutMaskData);
550 g_pucOutMaskData = NULL;
553 if (g_pucInData != NULL) {
558 if (g_pucOutData != NULL) {
563 if (g_pucHIRData != NULL) {
568 if (g_pucTIRData != NULL) {
573 if (g_pucHDRData != NULL) {
578 if (g_pucTDRData != NULL) {
583 if (g_pucOutDMaskData != NULL) {
584 free(g_pucOutDMaskData);
585 g_pucOutDMaskData = NULL;
588 if (g_pucIntelBuffer != NULL) {
589 free(g_pucIntelBuffer);
590 g_pucIntelBuffer = NULL;
593 if (g_pLVDSList != NULL) {
604 * Returns a VME-encoded number, usually used to indicate the
605 * bit length of an SIR/SDR command.
609 long int ispVMDataSize()
611 /* 09/11/07 NN added local variables initialization */
613 signed char cCurrentByte = 0;
614 signed char cIndex = 0;
616 while ((cCurrentByte = GetByte()) & 0x80) {
617 iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
620 iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
628 * This is the heart of the embedded engine. All the high-level opcodes
629 * are extracted here. Once they have been identified, then it
630 * will call other functions to handle the processing.
634 signed char ispVMCode()
636 /* 09/11/07 NN added local variables initialization */
637 unsigned short iRepeatSize = 0;
638 signed char cOpcode = 0;
639 signed char cRetCode = 0;
640 unsigned char ucState = 0;
641 unsigned short usDelay = 0;
642 unsigned short usToggle = 0;
643 unsigned char usByte = 0;
647 * Check the compression flag only if this is the first time
648 * this function is entered. Do not check the compression flag if
649 * it is being called recursively from other functions within
650 * the embedded engine.
654 if (!(g_usDataType & LHEAP_IN) && !(g_usDataType & HEAP_IN)) {
656 if (usByte == 0xf1) {
657 g_usDataType |= COMPRESS;
658 } else if (usByte == 0xf2) {
659 g_usDataType &= ~COMPRESS;
661 return VME_INVALID_FILE;
667 * Begin looping through all the VME opcodes.
671 while ((cOpcode = GetByte()) >= 0) {
677 * Step the JTAG state machine.
683 * Step the JTAG state machine to DRCAPTURE
684 * to support Looping.
687 if ((g_usDataType & LHEAP_IN) &&
688 (ucState == DRPAUSE) &&
689 (g_cCurrentJTAGState == ucState)) {
690 ispVMStateMachine(DRCAPTURE);
693 ispVMStateMachine(ucState);
696 if (g_usDataType & LHEAP_IN) {
697 debug("LDELAY %s ", GetState(ucState));
699 debug("STATE %s;\n", GetState(ucState));
714 if (g_usDataType & LHEAP_IN) {
724 * Shift in data into the device.
728 cRetCode = ispVMShift(cOpcode);
741 /* 09/11/07 NN Type cast mismatch variables */
742 usDelay = (unsigned short) ispVMDataSize();
746 if (usDelay & 0x8000) {
749 * Since MSB is set, the delay time must be
750 * decoded to millisecond. The SVF2VME encodes
751 * the MSB to represent millisecond.
755 if (g_usDataType & LHEAP_IN) {
756 printf("%.2E SEC;\n",
757 (float) usDelay / 1000);
759 printf("RUNTEST %.2E SEC;\n",
760 (float) usDelay / 1000);
764 * Since MSB is not set, the delay time
765 * is given as microseconds.
768 if (g_usDataType & LHEAP_IN) {
769 printf("%.2E SEC;\n",
770 (float) usDelay / 1000000);
772 printf("RUNTEST %.2E SEC;\n",
773 (float) usDelay / 1000000);
781 * Issue clock toggles.
784 /* 09/11/07 NN Type cast mismatch variables */
785 usToggle = (unsigned short) ispVMDataSize();
786 ispVMClocks(usToggle);
789 printf("RUNTEST %d TCK;\n", usToggle);
800 g_ucEndDR = GetByte();
803 printf("ENDDR %s;\n", GetState(g_ucEndDR));
814 g_ucEndIR = GetByte();
817 printf("ENDIR %s;\n", GetState(g_ucEndIR));
842 * Set the header/trailer of the device in order
847 cRetCode = ispVMAmble(cOpcode);
859 * The maximum RAM required to support
860 * processing one row of the VME file.
863 /* 09/11/07 NN Type cast mismatch variables */
864 g_usMaxSize = (unsigned short) ispVMDataSize();
867 printf("// MEMSIZE %d\n", g_usMaxSize);
874 * Set the VENDOR type.
882 puts("// VENDOR LATTICE\n");
888 puts("// VENDOR ALTERA\n");
894 puts("// VENDOR XILINX\n");
905 * Set the flow control. Flow control determines
906 * the personality of the embedded engine.
909 /* 09/11/07 NN Type cast mismatch variables */
910 g_usFlowControl |= (unsigned short) ispVMDataSize();
916 * Unset the flow control.
920 /* 09/11/07 NN Type cast mismatch variables */
921 g_usFlowControl &= (unsigned short) ~(ispVMDataSize());
927 * Allocate heap size to store loops.
931 cRetCode = GetByte();
932 if (cRetCode != SECUREHEAP) {
933 return VME_INVALID_FILE;
935 /* 09/11/07 NN Type cast mismatch variables */
936 g_iHEAPSize = (unsigned short) ispVMDataSize();
939 * Store the maximum size of the HEAP buffer.
940 * Used to convert VME to HEX.
943 if (g_iHEAPSize > g_usHeapSize) {
944 g_usHeapSize = g_iHEAPSize;
947 ispVMMemManager(HEAP, (unsigned short) g_iHEAPSize);
959 /* 09/11/07 NN Type cast mismatch variables */
960 iRepeatSize = (unsigned short) ispVMDataSize();
962 cRetCode = ispVMLoop((unsigned short) iRepeatSize);
971 * Exit point from processing loops.
979 * The only valid exit point that indicates
980 * end of programming.
988 * Right-shift address.
992 g_usFlowControl |= SHIFTRIGHT;
994 /* 09/11/07 NN Type cast mismatch variables */
995 g_usShiftValue = (unsigned short) (g_usRepeatLoops *
996 (unsigned short)GetByte());
1001 * Left-shift address.
1004 g_usFlowControl |= SHIFTLEFT;
1006 /* 09/11/07 NN Type cast mismatch variables */
1007 g_usShiftValue = (unsigned short) (g_usRepeatLoops *
1008 (unsigned short)GetByte());
1014 * Set the frequency.
1018 /* 09/11/07 NN Type cast mismatch variables */
1019 g_iFrequency = (int) (ispVMDataSize() / 1000);
1020 if (g_iFrequency == 1)
1021 g_iFrequency = 1000;
1024 printf("FREQUENCY %.2E HZ;\n",
1025 (float) g_iFrequency * 1000);
1032 * Process LCOUNT command.
1036 cRetCode = ispVMLCOUNT((unsigned short)ispVMDataSize());
1037 if (cRetCode != 0) {
1045 * Set the flow control to verify USERCODE.
1049 g_usFlowControl |= VERIFYUES;
1059 ispVMComment((unsigned short) ispVMDataSize());
1065 * Process LVDS command.
1069 ispVMProcessLVDS((unsigned short) ispVMDataSize());
1079 ispVMHeader((unsigned short) ispVMDataSize());
1081 /* 03/14/06 Support Toggle ispENABLE signal*/
1083 ucState = GetByte();
1084 if ((ucState == ON) || (ucState == 0x01))
1085 writePort(g_ucPinENABLE, 0x01);
1087 writePort(g_ucPinENABLE, 0x00);
1090 /* 05/24/06 support Toggle TRST pin*/
1092 ucState = GetByte();
1093 if (ucState == 0x01)
1094 writePort(g_ucPinTRST, 0x01);
1096 writePort(g_ucPinTRST, 0x00);
1103 * Invalid opcode encountered.
1108 printf("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
1111 return VME_INVALID_FILE;
1117 * Invalid exit point. Processing the token 'ENDVME' is the only
1118 * valid way to exit the embedded engine.
1122 return VME_INVALID_FILE;
1129 * Processes the TDI/TDO/MASK/DMASK etc of an SIR/SDR command.
1133 signed char ispVMDataCode()
1135 /* 09/11/07 NN added local variables initialization */
1136 signed char cDataByte = 0;
1137 signed char siDataSource = 0; /*source of data from file by default*/
1139 if (g_usDataType & HEAP_IN) {
1140 siDataSource = 1; /*the source of data from memory*/
1145 * Clear the data type register.
1149 g_usDataType &= ~(MASK_DATA + TDI_DATA +
1150 TDO_DATA + DMASK_DATA + CMASK_DATA);
1153 * Iterate through SIR/SDR command and look for TDI,
1157 while ((cDataByte = GetByte()) >= 0) {
1158 ispVMMemManager(cDataByte, g_usMaxSize);
1159 switch (cDataByte) {
1163 * Store the maximum size of the TDI buffer.
1164 * Used to convert VME to HEX.
1167 if (g_usiDataSize > g_usTDISize) {
1168 g_usTDISize = g_usiDataSize;
1171 * Updated data type register to indicate that
1172 * TDI data is currently being used. Process the
1173 * data in the VME file into the TDI buffer.
1176 g_usDataType |= TDI_DATA;
1177 ispVMData(g_pucInData);
1182 * Store the maximum size of the TDO buffer.
1183 * Used to convert VME to HEX.
1186 if (g_usiDataSize > g_usTDOSize) {
1187 g_usTDOSize = g_usiDataSize;
1191 * Updated data type register to indicate that
1192 * TDO data is currently being used.
1195 g_usDataType |= TDO_DATA;
1200 * Store the maximum size of the TDO buffer.
1201 * Used to convert VME to HEX.
1204 if (g_usiDataSize > g_usTDOSize) {
1205 g_usTDOSize = g_usiDataSize;
1209 * Updated data type register to indicate
1210 * that TDO data is currently being used.
1211 * Process the data in the VME file into the
1215 g_usDataType |= TDO_DATA;
1216 ispVMData(g_pucOutData);
1221 * Store the maximum size of the MASK buffer.
1222 * Used to convert VME to HEX.
1225 if (g_usiDataSize > g_usMASKSize) {
1226 g_usMASKSize = g_usiDataSize;
1230 * Updated data type register to indicate that
1231 * MASK data is currently being used. Process
1232 * the data in the VME file into the MASK buffer
1235 g_usDataType |= MASK_DATA;
1236 ispVMData(g_pucOutMaskData);
1241 * Store the maximum size of the DMASK buffer.
1242 * Used to convert VME to HEX.
1245 if (g_usiDataSize > g_usDMASKSize) {
1246 g_usDMASKSize = g_usiDataSize;
1250 * Updated data type register to indicate that
1251 * DMASK data is currently being used. Process
1252 * the data in the VME file into the DMASK
1256 g_usDataType |= DMASK_DATA;
1257 ispVMData(g_pucOutDMaskData);
1262 * Updated data type register to indicate that
1263 * MASK data is currently being used. Process
1264 * the data in the VME file into the MASK buffer
1267 g_usDataType |= CMASK_DATA;
1268 ispVMData(g_pucOutMaskData);
1274 * Encountered invalid opcode.
1276 return VME_INVALID_FILE;
1279 switch (cDataByte) {
1283 * Left bit shift. Used when performing
1284 * algorithm looping.
1287 if (g_usFlowControl & SHIFTLEFT) {
1288 ispVMBitShift(SHL, g_usShiftValue);
1289 g_usFlowControl &= ~SHIFTLEFT;
1293 * Right bit shift. Used when performing
1294 * algorithm looping.
1297 if (g_usFlowControl & SHIFTRIGHT) {
1298 ispVMBitShift(SHR, g_usShiftValue);
1299 g_usFlowControl &= ~SHIFTRIGHT;
1306 g_usDataType |= HEAP_IN; /*restore from memory*/
1310 if (siDataSource) { /*fetch data from heap memory upon return*/
1311 g_usDataType |= HEAP_IN;
1314 if (cDataByte < 0) {
1317 * Encountered invalid opcode.
1320 return VME_INVALID_FILE;
1329 * Extract one row of data operand from the current data type opcode. Perform
1330 * the decompression if necessary. Extra RAM is not required for the
1331 * decompression process. The decompression scheme employed in this module
1332 * is on row by row basis. The format of the data stream:
1333 * [compression code][compressed data stream]
1334 * 0x00 --No compression
1335 * 0x01 --Compress by 0x00.
1337 * Original stream: 0x000000000000000000000001
1338 * Compressed stream: 0x01000901
1339 * Detail: 0x01 is the code, 0x00 is the key,
1340 * 0x09 is the count of 0x00 bytes,
1341 * 0x01 is the uncompressed byte.
1342 * 0x02 --Compress by 0xFF.
1344 * Original stream: 0xFFFFFFFFFFFFFFFFFFFFFF01
1345 * Compressed stream: 0x02FF0901
1346 * Detail: 0x02 is the code, 0xFF is the key,
1347 * 0x09 is the count of 0xFF bytes,
1348 * 0x01 is the uncompressed byte.
1351 * 0xFE -- Compress by nibble blocks.
1353 * Original stream: 0x84210842108421084210
1354 * Compressed stream: 0x0584210
1355 * Detail: 0x05 is the code, means 5 nibbles block.
1356 * 0x84210 is the 5 nibble blocks.
1357 * The whole row is 80 bits given by g_usiDataSize.
1358 * The number of times the block repeat itself
1359 * is found by g_usiDataSize/(4*0x05) which is 4.
1360 * 0xFF -- Compress by the most frequently happen byte.
1362 * Original stream: 0x04020401030904040404
1363 * Compressed stream: 0xFF04(0,1,0x02,0,1,0x01,1,0x03,1,0x09,0,0,0)
1364 * or: 0xFF044090181C240
1365 * Detail: 0xFF is the code, 0x04 is the key.
1366 * a bit of 0 represent the key shall be put into
1367 * the current bit position and a bit of 1
1368 * represent copying the next of 8 bits of data
1373 void ispVMData(unsigned char *ByteData)
1375 /* 09/11/07 NN added local variables initialization */
1376 unsigned short size = 0;
1377 unsigned short i, j, m, getData = 0;
1378 unsigned char cDataByte = 0;
1379 unsigned char compress = 0;
1380 unsigned short FFcount = 0;
1381 unsigned char compr_char = 0xFF;
1382 unsigned short index = 0;
1383 signed char compression = 0;
1385 /*convert number in bits to bytes*/
1386 if (g_usiDataSize % 8 > 0) {
1387 /* 09/11/07 NN Type cast mismatch variables */
1388 size = (unsigned short)(g_usiDataSize / 8 + 1);
1390 /* 09/11/07 NN Type cast mismatch variables */
1391 size = (unsigned short)(g_usiDataSize / 8);
1395 * If there is compression, then check if compress by key
1396 * of 0x00 or 0xFF or by other keys or by nibble blocks
1399 if (g_usDataType & COMPRESS) {
1401 compress = GetByte();
1402 if ((compress == VAR) && (g_usDataType & HEAP_IN)) {
1404 g_usDataType &= ~(HEAP_IN);
1405 compress = GetByte();
1410 /* No compression */
1414 /* Compress by byte 0x00 */
1418 /* Compress by byte 0xFF */
1422 /* Huffman encoding */
1423 compr_char = GetByte();
1425 for (index = 0; index < size; index++) {
1426 ByteData[index] = 0x00;
1428 cDataByte = GetByte();
1431 if ((cDataByte << i++) & 0x80)
1434 ByteData[index] = compr_char;
1438 for (j = 0; j < m; j++) {
1440 cDataByte = GetByte();
1444 ((cDataByte << i++) & 0x80) >> j;
1450 for (index = 0; index < size; index++)
1451 ByteData[index] = 0x00;
1452 for (index = 0; index < compress; index++) {
1454 cDataByte = GetByte();
1455 for (i = 0; i < size * 2 / compress; i++) {
1456 j = (unsigned short)(index +
1457 (i * (unsigned short)compress));
1458 /*clear the nibble to zero first*/
1483 /* Decompress by byte 0x00 or 0xFF */
1484 for (index = 0; index < size; index++) {
1486 cDataByte = GetByte();
1487 if ((cDataByte == VAR) && (g_usDataType&HEAP_IN) &&
1488 !getData && !(g_usDataType&COMPRESS)) {
1490 g_usDataType &= ~(HEAP_IN);
1491 cDataByte = GetByte();
1493 ByteData[index] = cDataByte;
1494 if ((compression) && (cDataByte == compr_char))
1495 /* 09/11/07 NN Type cast mismatch variables */
1496 FFcount = (unsigned short) ispVMDataSize();
1497 /*The number of 0xFF or 0x00 bytes*/
1499 FFcount--; /*Use up the 0xFF chain first*/
1500 ByteData[index] = compr_char;
1505 g_usDataType |= HEAP_IN;
1514 * Processes the SDR/XSDR/SIR commands.
1518 signed char ispVMShift(signed char a_cCode)
1520 /* 09/11/07 NN added local variables initialization */
1521 unsigned short iDataIndex = 0;
1522 unsigned short iReadLoop = 0;
1523 signed char cRetCode = 0;
1526 /* 09/11/07 NN Type cast mismatch variables */
1527 g_usiDataSize = (unsigned short) ispVMDataSize();
1529 /*clear the flags first*/
1530 g_usDataType &= ~(SIR_DATA + EXPRESS + SDR_DATA);
1533 g_usDataType |= SIR_DATA;
1535 * 1/15/04 If performing cascading, then go directly to SHIFTIR.
1536 * Else, go to IRPAUSE before going to SHIFTIR
1538 if (g_usFlowControl & CASCADE) {
1539 ispVMStateMachine(SHIFTIR);
1541 ispVMStateMachine(IRPAUSE);
1542 ispVMStateMachine(SHIFTIR);
1543 if (g_usHeadIR > 0) {
1544 ispVMBypass(HIR, g_usHeadIR);
1550 g_usDataType |= EXPRESS; /*mark simultaneous in and out*/
1552 g_usDataType |= SDR_DATA;
1554 * 1/15/04 If already in SHIFTDR, then do not move state or
1555 * shift in header. This would imply that the previously
1556 * shifted frame was a cascaded frame.
1558 if (g_cCurrentJTAGState != SHIFTDR) {
1560 * 1/15/04 If performing cascading, then go directly
1561 * to SHIFTDR. Else, go to DRPAUSE before going
1564 if (g_usFlowControl & CASCADE) {
1565 if (g_cCurrentJTAGState == DRPAUSE) {
1566 ispVMStateMachine(SHIFTDR);
1568 * 1/15/04 If cascade flag has been seat
1569 * and the current state is DRPAUSE,
1570 * this implies that the first cascaded
1571 * frame is about to be shifted in. The
1572 * header must be shifted prior to
1573 * shifting the first cascaded frame.
1575 if (g_usHeadDR > 0) {
1576 ispVMBypass(HDR, g_usHeadDR);
1580 ispVMStateMachine(SHIFTDR);
1583 ispVMStateMachine(DRPAUSE);
1584 ispVMStateMachine(SHIFTDR);
1585 if (g_usHeadDR > 0) {
1586 ispVMBypass(HDR, g_usHeadDR);
1593 return VME_INVALID_FILE;
1596 cRetCode = ispVMDataCode();
1598 if (cRetCode != 0) {
1599 return VME_INVALID_FILE;
1603 printf("%d ", g_usiDataSize);
1605 if (g_usDataType & TDI_DATA) {
1607 PrintData(g_usiDataSize, g_pucInData);
1610 if (g_usDataType & TDO_DATA) {
1612 PrintData(g_usiDataSize, g_pucOutData);
1615 if (g_usDataType & MASK_DATA) {
1616 puts("\n\t\tMASK ");
1617 PrintData(g_usiDataSize, g_pucOutMaskData);
1620 if (g_usDataType & DMASK_DATA) {
1621 puts("\n\t\tDMASK ");
1622 PrintData(g_usiDataSize, g_pucOutDMaskData);
1628 if (g_usDataType & TDO_DATA || g_usDataType & DMASK_DATA) {
1629 if (g_usDataType & DMASK_DATA) {
1630 cRetCode = ispVMReadandSave(g_usiDataSize);
1632 if (g_usTailDR > 0) {
1634 ispVMBypass(TDR, g_usTailDR);
1636 ispVMStateMachine(DRPAUSE);
1637 ispVMStateMachine(SHIFTDR);
1638 if (g_usHeadDR > 0) {
1639 ispVMBypass(HDR, g_usHeadDR);
1642 for (iDataIndex = 0;
1643 iDataIndex < g_usiDataSize / 8 + 1;
1645 g_pucInData[iDataIndex] =
1646 g_pucOutData[iDataIndex];
1647 g_usDataType &= ~(TDO_DATA + DMASK_DATA);
1648 cRetCode = ispVMSend(g_usiDataSize);
1651 cRetCode = ispVMRead(g_usiDataSize);
1652 if (cRetCode == -1 && g_cVendor == XILINX) {
1653 for (iReadLoop = 0; iReadLoop < 30;
1655 cRetCode = ispVMRead(g_usiDataSize);
1659 /* Always DRPAUSE */
1660 ispVMStateMachine(DRPAUSE);
1662 * Bypass other devices
1665 ispVMBypass(TDR, g_usTailDR);
1666 ispVMStateMachine(g_ucEndDR);
1667 ispVMStateMachine(IDLE);
1673 } else { /*TDI only*/
1674 cRetCode = ispVMSend(g_usiDataSize);
1677 /*transfer the input data to the output buffer for the next verify*/
1678 if ((g_usDataType & EXPRESS) || (a_cCode == SDR)) {
1680 for (iDataIndex = 0; iDataIndex < g_usiDataSize / 8 + 1;
1682 g_pucOutData[iDataIndex] =
1683 g_pucInData[iDataIndex];
1689 /* 1/15/04 If not performing cascading, then shift ENDIR */
1690 if (!(g_usFlowControl & CASCADE)) {
1691 if (g_usTailIR > 0) {
1693 ispVMBypass(TIR, g_usTailIR);
1695 ispVMStateMachine(g_ucEndIR);
1700 /* 1/15/04 If not performing cascading, then shift ENDDR */
1701 if (!(g_usFlowControl & CASCADE)) {
1702 if (g_usTailDR > 0) {
1704 ispVMBypass(TDR, g_usTailDR);
1706 ispVMStateMachine(g_ucEndDR);
1720 * This routine is to extract Header and Trailer parameter for SIR and
1723 * The Header and Trailer parameter are the pre-amble and post-amble bit
1724 * stream need to be shifted into TDI or out of TDO of the devices. Mostly
1725 * is for the purpose of bypassing the leading or trailing devices. ispVM
1726 * supports only shifting data into TDI to bypass the devices.
1728 * For a single device, the header and trailer parameters are all set to 0
1729 * as default by ispVM. If it is for multiple devices, the header and trailer
1730 * value will change as specified by the VME file.
1734 signed char ispVMAmble(signed char Code)
1736 signed char compress = 0;
1737 /* 09/11/07 NN Type cast mismatch variables */
1738 g_usiDataSize = (unsigned short)ispVMDataSize();
1741 printf("%d", g_usiDataSize);
1744 if (g_usiDataSize) {
1747 * Discard the TDI byte and set the compression bit in the data
1748 * type register to false if compression is set because TDI data
1749 * after HIR/HDR/TIR/TDR is not compressed.
1753 if (g_usDataType & COMPRESS) {
1754 g_usDataType &= ~(COMPRESS);
1763 * Store the maximum size of the HIR buffer.
1764 * Used to convert VME to HEX.
1767 if (g_usiDataSize > g_usHIRSize) {
1768 g_usHIRSize = g_usiDataSize;
1772 * Assign the HIR value and allocate memory.
1775 g_usHeadIR = g_usiDataSize;
1777 ispVMMemManager(HIR, g_usHeadIR);
1778 ispVMData(g_pucHIRData);
1782 PrintData(g_usHeadIR, g_pucHIRData);
1789 * Store the maximum size of the TIR buffer.
1790 * Used to convert VME to HEX.
1793 if (g_usiDataSize > g_usTIRSize) {
1794 g_usTIRSize = g_usiDataSize;
1798 * Assign the TIR value and allocate memory.
1801 g_usTailIR = g_usiDataSize;
1803 ispVMMemManager(TIR, g_usTailIR);
1804 ispVMData(g_pucTIRData);
1808 PrintData(g_usTailIR, g_pucTIRData);
1815 * Store the maximum size of the HDR buffer.
1816 * Used to convert VME to HEX.
1819 if (g_usiDataSize > g_usHDRSize) {
1820 g_usHDRSize = g_usiDataSize;
1824 * Assign the HDR value and allocate memory.
1828 g_usHeadDR = g_usiDataSize;
1830 ispVMMemManager(HDR, g_usHeadDR);
1831 ispVMData(g_pucHDRData);
1835 PrintData(g_usHeadDR, g_pucHDRData);
1842 * Store the maximum size of the TDR buffer.
1843 * Used to convert VME to HEX.
1846 if (g_usiDataSize > g_usTDRSize) {
1847 g_usTDRSize = g_usiDataSize;
1851 * Assign the TDR value and allocate memory.
1855 g_usTailDR = g_usiDataSize;
1857 ispVMMemManager(TDR, g_usTailDR);
1858 ispVMData(g_pucTDRData);
1862 PrintData(g_usTailDR, g_pucTDRData);
1872 * Re-enable compression if it was previously set.
1877 g_usDataType |= COMPRESS;
1880 if (g_usiDataSize) {
1882 if (Code == CONTINUE) {
1887 * Encountered invalid opcode.
1890 return VME_INVALID_FILE;
1901 * Perform the function call upon by the REPEAT opcode.
1902 * Memory is to be allocated to store the entire loop from REPEAT to ENDLOOP.
1903 * After the loop is stored then execution begin. The REPEATLOOP flag is set
1904 * on the g_usFlowControl register to indicate the repeat loop is in session
1905 * and therefore fetch opcode from the memory instead of from the file.
1909 signed char ispVMLoop(unsigned short a_usLoopCount)
1911 /* 09/11/07 NN added local variables initialization */
1912 signed char cRetCode = 0;
1913 unsigned short iHeapIndex = 0;
1914 unsigned short iLoopIndex = 0;
1917 for (iHeapIndex = 0; iHeapIndex < g_iHEAPSize; iHeapIndex++) {
1918 g_pucHeapMemory[iHeapIndex] = GetByte();
1921 if (g_pucHeapMemory[iHeapIndex - 1] != ENDLOOP) {
1922 return VME_INVALID_FILE;
1925 g_usFlowControl |= REPEATLOOP;
1926 g_usDataType |= HEAP_IN;
1928 for (iLoopIndex = 0; iLoopIndex < a_usLoopCount; iLoopIndex++) {
1930 cRetCode = ispVMCode();
1937 g_usDataType &= ~(HEAP_IN);
1938 g_usFlowControl &= ~(REPEATLOOP);
1946 * Shift the TDI stream left or right by the number of bits. The data in
1947 * *g_pucInData is of the VME format, so the actual shifting is the reverse of
1948 * IEEE 1532 or SVF format.
1952 signed char ispVMBitShift(signed char mode, unsigned short bits)
1954 /* 09/11/07 NN added local variables initialization */
1955 unsigned short i = 0;
1956 unsigned short size = 0;
1957 unsigned short tmpbits = 0;
1959 if (g_usiDataSize % 8 > 0) {
1960 /* 09/11/07 NN Type cast mismatch variables */
1961 size = (unsigned short)(g_usiDataSize / 8 + 1);
1963 /* 09/11/07 NN Type cast mismatch variables */
1964 size = (unsigned short)(g_usiDataSize / 8);
1969 for (i = 0; i < size; i++) {
1970 if (g_pucInData[i] != 0) {
1972 while (tmpbits > 0) {
1973 g_pucInData[i] <<= 1;
1974 if (g_pucInData[i] == 0) {
1984 for (i = 0; i < size; i++) {
1985 if (g_pucInData[i] != 0) {
1987 while (tmpbits > 0) {
1988 g_pucInData[i] >>= 1;
1989 if (g_pucInData[i] == 0) {
1999 return VME_INVALID_FILE;
2009 * Displays the SVF comments.
2013 void ispVMComment(unsigned short a_usCommentSize)
2016 for (; a_usCommentSize > 0; a_usCommentSize--) {
2019 * Print character to the terminal.
2022 cCurByte = GetByte();
2023 vme_out_char(cCurByte);
2026 vme_out_char(cCurByte);
2033 * Iterate the length of the header and discard it.
2037 void ispVMHeader(unsigned short a_usHeaderSize)
2039 for (; a_usHeaderSize > 0; a_usHeaderSize--) {
2046 * ispVMCalculateCRC32
2048 * Calculate the 32-bit CRC.
2052 void ispVMCalculateCRC32(unsigned char a_ucData)
2054 /* 09/11/07 NN added local variables initialization */
2055 unsigned char ucIndex = 0;
2056 unsigned char ucFlipData = 0;
2057 unsigned short usCRCTableEntry = 0;
2058 unsigned int crc_table[16] = {
2059 0x0000, 0xCC01, 0xD801,
2060 0x1400, 0xF001, 0x3C00,
2061 0x2800, 0xE401, 0xA001,
2062 0x6C00, 0x7800, 0xB401,
2063 0x5000, 0x9C01, 0x8801,
2067 for (ucIndex = 0; ucIndex < 8; ucIndex++) {
2069 if (a_ucData & 0x01) {
2075 /* 09/11/07 NN Type cast mismatch variables */
2076 usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
2077 g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
2078 g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
2079 usCRCTableEntry ^ crc_table[ucFlipData & 0xF]);
2080 usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
2081 g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
2082 g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
2083 usCRCTableEntry ^ crc_table[(ucFlipData >> 4) & 0xF]);
2090 * Process the intelligent programming loops.
2094 signed char ispVMLCOUNT(unsigned short a_usCountSize)
2096 unsigned short usContinue = 1;
2097 unsigned short usIntelBufferIndex = 0;
2098 unsigned short usCountIndex = 0;
2099 signed char cRetCode = 0;
2100 signed char cRepeatHeap = 0;
2101 signed char cOpcode = 0;
2102 unsigned char ucState = 0;
2103 unsigned short usDelay = 0;
2104 unsigned short usToggle = 0;
2105 unsigned char usByte = 0;
2107 g_usIntelBufferSize = (unsigned short)ispVMDataSize();
2110 * Allocate memory for intel buffer.
2114 ispVMMemManager(LHEAP, g_usIntelBufferSize);
2117 * Store the maximum size of the intelligent buffer.
2118 * Used to convert VME to HEX.
2121 if (g_usIntelBufferSize > g_usLCOUNTSize) {
2122 g_usLCOUNTSize = g_usIntelBufferSize;
2126 * Copy intel data to the buffer.
2129 for (usIntelBufferIndex = 0; usIntelBufferIndex < g_usIntelBufferSize;
2130 usIntelBufferIndex++) {
2131 g_pucIntelBuffer[usIntelBufferIndex] = GetByte();
2135 * Set the data type register to get data from the intelligent
2139 g_usDataType |= LHEAP_IN;
2143 * If the HEAP_IN flag is set, temporarily unset the flag so data will be
2144 * retrieved from the status buffer.
2148 if (g_usDataType & HEAP_IN) {
2149 g_usDataType &= ~HEAP_IN;
2154 printf("LCOUNT %d;\n", a_usCountSize);
2158 * Iterate through the intelligent programming command.
2161 for (usCountIndex = 0; usCountIndex < a_usCountSize; usCountIndex++) {
2165 * Initialize the intel data index to 0 before each iteration.
2169 g_usIntelDataIndex = 0;
2179 * Begin looping through all the VME opcodes.
2183 * 4/1/09 Nguyen replaced the recursive function call codes on
2184 * the ispVMLCOUNT function
2187 while (usContinue) {
2188 cOpcode = GetByte();
2195 * Set the header/trailer of the device in order
2196 * to bypass successfully.
2199 ispVMAmble(cOpcode);
2204 * Step the JTAG state machine.
2207 ucState = GetByte();
2209 * Step the JTAG state machine to DRCAPTURE
2210 * to support Looping.
2213 if ((g_usDataType & LHEAP_IN) &&
2214 (ucState == DRPAUSE) &&
2215 (g_cCurrentJTAGState == ucState)) {
2216 ispVMStateMachine(DRCAPTURE);
2218 ispVMStateMachine(ucState);
2220 printf("LDELAY %s ", GetState(ucState));
2228 * Shift in data into the device.
2231 cRetCode = ispVMShift(cOpcode);
2239 * Shift in data into the device.
2242 cRetCode = ispVMShift(cOpcode);
2252 usDelay = (unsigned short)ispVMDataSize();
2253 ispVMDelay(usDelay);
2256 if (usDelay & 0x8000) {
2259 * Since MSB is set, the delay time must
2260 * be decoded to millisecond. The
2261 * SVF2VME encodes the MSB to represent
2266 printf("%.2E SEC;\n",
2267 (float) usDelay / 1000);
2270 * Since MSB is not set, the delay time
2271 * is given as microseconds.
2274 printf("%.2E SEC;\n",
2275 (float) usDelay / 1000000);
2282 * Issue clock toggles.
2285 usToggle = (unsigned short)ispVMDataSize();
2286 ispVMClocks(usToggle);
2289 printf("RUNTEST %d TCK;\n", usToggle);
2295 * Exit point from processing loops.
2306 ispVMComment((unsigned short) ispVMDataSize());
2309 ucState = GetByte();
2310 if ((ucState == ON) || (ucState == 0x01))
2311 writePort(g_ucPinENABLE, 0x01);
2313 writePort(g_ucPinENABLE, 0x00);
2317 if (GetByte() == 0x01)
2318 writePort(g_ucPinTRST, 0x01);
2320 writePort(g_ucPinTRST, 0x00);
2326 * Invalid opcode encountered.
2329 debug("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
2331 return VME_INVALID_FILE;
2334 if (cRetCode >= 0) {
2336 * Break if intelligent programming is successful.
2344 * If HEAP_IN flag was temporarily disabled,
2345 * re-enable it before exiting
2349 g_usDataType |= HEAP_IN;
2353 * Set the data type register to not get data from the
2354 * intelligent data buffer.
2357 g_usDataType &= ~LHEAP_IN;
2364 * Applies the specified number of pulses to TCK.
2368 void ispVMClocks(unsigned short Clocks)
2370 unsigned short iClockIndex = 0;
2371 for (iClockIndex = 0; iClockIndex < Clocks; iClockIndex++) {
2380 * This procedure takes care of the HIR, HDR, TIR, TDR for the
2381 * purpose of putting the other devices into Bypass mode. The
2382 * current state is checked to find out if it is at DRPAUSE or
2383 * IRPAUSE. If it is at DRPAUSE, perform bypass register scan.
2384 * If it is at IRPAUSE, scan into instruction registers the bypass
2389 void ispVMBypass(signed char ScanType, unsigned short Bits)
2391 /* 09/11/07 NN added local variables initialization */
2392 unsigned short iIndex = 0;
2393 unsigned short iSourceIndex = 0;
2394 unsigned char cBitState = 0;
2395 unsigned char cCurByte = 0;
2396 unsigned char *pcSource = NULL;
2404 pcSource = g_pucHIRData;
2407 pcSource = g_pucTIRData;
2410 pcSource = g_pucHDRData;
2413 pcSource = g_pucTDRData;
2421 for (iIndex = 0; iIndex < Bits - 1; iIndex++) {
2422 /* Scan instruction or bypass register */
2423 if (iIndex % 8 == 0) {
2424 cCurByte = pcSource[iSourceIndex++];
2426 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2428 writePort(g_ucPinTDI, cBitState);
2432 if (iIndex % 8 == 0) {
2433 cCurByte = pcSource[iSourceIndex++];
2436 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2438 writePort(g_ucPinTDI, cBitState);
2445 * This procedure steps all devices in the daisy chain from a given
2446 * JTAG state to the next desirable state. If the next state is TLR,
2447 * the JTAG state machine is brute forced into TLR by driving TMS
2448 * high and pulse TCK 6 times.
2452 void ispVMStateMachine(signed char cNextJTAGState)
2454 /* 09/11/07 NN added local variables initialization */
2455 signed char cPathIndex = 0;
2456 signed char cStateIndex = 0;
2458 if ((g_cCurrentJTAGState == cNextJTAGState) &&
2459 (cNextJTAGState != RESET)) {
2463 for (cStateIndex = 0; cStateIndex < 25; cStateIndex++) {
2464 if ((g_cCurrentJTAGState ==
2465 g_JTAGTransistions[cStateIndex].CurState) &&
2467 g_JTAGTransistions[cStateIndex].NextState)) {
2472 g_cCurrentJTAGState = cNextJTAGState;
2473 for (cPathIndex = 0;
2474 cPathIndex < g_JTAGTransistions[cStateIndex].Pulses;
2476 if ((g_JTAGTransistions[cStateIndex].Pattern << cPathIndex)
2478 writePort(g_ucPinTMS, (unsigned char) 0x01);
2480 writePort(g_ucPinTMS, (unsigned char) 0x00);
2485 writePort(g_ucPinTDI, 0x00);
2486 writePort(g_ucPinTMS, 0x00);
2493 * Enable the port to the device and set the state to RESET (TLR).
2500 printf("// ISPVM EMBEDDED ADDED\n");
2501 printf("STATE RESET;\n");
2503 g_usFlowControl = 0;
2504 g_usDataType = g_uiChecksumIndex = g_cCurrentJTAGState = 0;
2505 g_usHeadDR = g_usHeadIR = g_usTailDR = g_usTailIR = 0;
2506 g_usMaxSize = g_usShiftValue = g_usRepeatLoops = 0;
2507 g_usTDOSize = g_usMASKSize = g_usTDISize = 0;
2508 g_usDMASKSize = g_usLCOUNTSize = g_usHDRSize = 0;
2509 g_usTDRSize = g_usHIRSize = g_usTIRSize = g_usHeapSize = 0;
2511 g_usLVDSPairCount = 0;
2514 ispVMStateMachine(RESET); /*step devices to RESET state*/
2521 * Set the state of devices to RESET to enable the devices and disable
2529 printf("// ISPVM EMBEDDED ADDED\n");
2530 printf("STATE RESET;\n");
2531 printf("RUNTEST 1.00E-001 SEC;\n");
2534 ispVMStateMachine(RESET); /*step devices to RESET state */
2535 ispVMDelay(1000); /*wake up devices*/
2542 * Send the TDI data stream to devices. The data stream can be
2543 * instructions or data.
2547 signed char ispVMSend(unsigned short a_usiDataSize)
2549 /* 09/11/07 NN added local variables initialization */
2550 unsigned short iIndex = 0;
2551 unsigned short iInDataIndex = 0;
2552 unsigned char cCurByte = 0;
2553 unsigned char cBitState = 0;
2555 for (iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++) {
2556 if (iIndex % 8 == 0) {
2557 cCurByte = g_pucInData[iInDataIndex++];
2559 cBitState = (unsigned char)(((cCurByte << iIndex % 8) & 0x80)
2561 writePort(g_ucPinTDI, cBitState);
2565 if (iIndex % 8 == 0) {
2566 /* Take care of the last bit */
2567 cCurByte = g_pucInData[iInDataIndex];
2570 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2573 writePort(g_ucPinTDI, cBitState);
2574 if (g_usFlowControl & CASCADE) {
2575 /*1/15/04 Clock in last bit for the first n-1 cascaded frames */
2586 * Read the data stream from devices and verify.
2590 signed char ispVMRead(unsigned short a_usiDataSize)
2592 /* 09/11/07 NN added local variables initialization */
2593 unsigned short usDataSizeIndex = 0;
2594 unsigned short usErrorCount = 0;
2595 unsigned short usLastBitIndex = 0;
2596 unsigned char cDataByte = 0;
2597 unsigned char cMaskByte = 0;
2598 unsigned char cInDataByte = 0;
2599 unsigned char cCurBit = 0;
2600 unsigned char cByteIndex = 0;
2601 unsigned short usBufferIndex = 0;
2602 unsigned char ucDisplayByte = 0x00;
2603 unsigned char ucDisplayFlag = 0x01;
2604 char StrChecksum[256] = {0};
2605 unsigned char g_usCalculateChecksum = 0x00;
2607 /* 09/11/07 NN Type cast mismatch variables */
2608 usLastBitIndex = (unsigned short)(a_usiDataSize - 1);
2612 * If mask is not all zeros, then set the display flag to 0x00,
2613 * otherwise it shall be set to 0x01 to indicate that data read
2614 * from the device shall be displayed. If DEBUG is defined,
2615 * always display data.
2618 for (usDataSizeIndex = 0; usDataSizeIndex < (a_usiDataSize + 7) / 8;
2619 usDataSizeIndex++) {
2620 if (g_usDataType & MASK_DATA) {
2621 if (g_pucOutMaskData[usDataSizeIndex] != 0x00) {
2622 ucDisplayFlag = 0x00;
2625 } else if (g_usDataType & CMASK_DATA) {
2626 g_usCalculateChecksum = 0x01;
2627 ucDisplayFlag = 0x00;
2630 ucDisplayFlag = 0x00;
2638 * Begin shifting data in and out of the device.
2642 for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
2643 usDataSizeIndex++) {
2644 if (cByteIndex == 0) {
2647 * Grab byte from TDO buffer.
2650 if (g_usDataType & TDO_DATA) {
2651 cDataByte = g_pucOutData[usBufferIndex];
2655 * Grab byte from MASK buffer.
2658 if (g_usDataType & MASK_DATA) {
2659 cMaskByte = g_pucOutMaskData[usBufferIndex];
2665 * Grab byte from CMASK buffer.
2668 if (g_usDataType & CMASK_DATA) {
2670 g_usCalculateChecksum = 0x01;
2674 * Grab byte from TDI buffer.
2677 if (g_usDataType & TDI_DATA) {
2678 cInDataByte = g_pucInData[usBufferIndex];
2684 cCurBit = readPort();
2686 if (ucDisplayFlag) {
2687 ucDisplayByte <<= 1;
2688 ucDisplayByte |= cCurBit;
2692 * Check if data read from port matches with expected TDO.
2695 if (g_usDataType & TDO_DATA) {
2696 /* 08/28/08 NN Added Calculate checksum support. */
2697 if (g_usCalculateChecksum) {
2698 if (cCurBit == 0x01)
2700 (1 << (g_uiChecksumIndex % 8));
2701 g_uiChecksumIndex++;
2703 if ((((cMaskByte << cByteIndex) & 0x80)
2705 if (cCurBit != (unsigned char)
2706 (((cDataByte << cByteIndex) & 0x80)
2715 * Write TDI data to the port.
2718 writePort(g_ucPinTDI,
2719 (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
2722 if (usDataSizeIndex < usLastBitIndex) {
2725 * Clock data out from the data shift register.
2729 } else if (g_usFlowControl & CASCADE) {
2732 * Clock in last bit for the first N - 1 cascaded frames
2739 * Increment the byte index. If it exceeds 7, then reset it back
2744 if (cByteIndex >= 8) {
2745 if (ucDisplayFlag) {
2748 * Store displayed data in the TDO buffer. By reusing
2749 * the TDO buffer to store displayed data, there is no
2750 * need to allocate a buffer simply to hold display
2751 * data. This will not cause any false verification
2752 * errors because the true TDO byte has already
2756 g_pucOutData[usBufferIndex - 1] = ucDisplayByte;
2762 /* 09/12/07 Nguyen changed to display the 1 bit expected data */
2763 else if (a_usiDataSize == 1) {
2764 if (ucDisplayFlag) {
2767 * Store displayed data in the TDO buffer.
2768 * By reusing the TDO buffer to store displayed
2769 * data, there is no need to allocate
2770 * a buffer simply to hold display data. This
2771 * will not cause any false verification errors
2772 * because the true TDO byte has already
2777 * Flip ucDisplayByte and store it in cDataByte.
2780 for (usBufferIndex = 0; usBufferIndex < 8;
2783 if (ucDisplayByte & 0x01) {
2786 ucDisplayByte >>= 1;
2788 g_pucOutData[0] = cDataByte;
2796 if (ucDisplayFlag) {
2799 debug("RECEIVED TDO (");
2801 vme_out_string("Display Data: 0x");
2804 /* 09/11/07 NN Type cast mismatch variables */
2805 for (usDataSizeIndex = (unsigned short)
2806 ((a_usiDataSize + 7) / 8);
2807 usDataSizeIndex > 0 ; usDataSizeIndex--) {
2808 cMaskByte = g_pucOutData[usDataSizeIndex - 1];
2812 * Flip cMaskByte and store it in cDataByte.
2815 for (usBufferIndex = 0; usBufferIndex < 8;
2818 if (cMaskByte & 0x01) {
2824 printf("%.2X", cDataByte);
2825 if ((((a_usiDataSize + 7) / 8) - usDataSizeIndex)
2830 vme_out_hex(cDataByte);
2837 vme_out_string("\n\n");
2839 /* 09/02/08 Nguyen changed to display the data Checksum */
2840 if (g_usChecksum != 0) {
2841 g_usChecksum &= 0xFFFF;
2842 sprintf(StrChecksum, "Data Checksum: %.4lX\n\n",
2844 vme_out_string(StrChecksum);
2849 if (usErrorCount > 0) {
2850 if (g_usFlowControl & VERIFYUES) {
2852 "USERCODE verification failed. "
2853 "Continue programming......\n\n");
2854 g_usFlowControl &= ~(VERIFYUES);
2859 printf("TOTAL ERRORS: %d\n", usErrorCount);
2862 return VME_VERIFICATION_FAILURE;
2865 if (g_usFlowControl & VERIFYUES) {
2866 vme_out_string("USERCODE verification passed. "
2867 "Programming aborted.\n\n");
2868 g_usFlowControl &= ~(VERIFYUES);
2880 * Support dynamic I/O.
2884 signed char ispVMReadandSave(unsigned short int a_usiDataSize)
2886 /* 09/11/07 NN added local variables initialization */
2887 unsigned short int usDataSizeIndex = 0;
2888 unsigned short int usLastBitIndex = 0;
2889 unsigned short int usBufferIndex = 0;
2890 unsigned short int usOutBitIndex = 0;
2891 unsigned short int usLVDSIndex = 0;
2892 unsigned char cDataByte = 0;
2893 unsigned char cDMASKByte = 0;
2894 unsigned char cInDataByte = 0;
2895 unsigned char cCurBit = 0;
2896 unsigned char cByteIndex = 0;
2897 signed char cLVDSByteIndex = 0;
2899 /* 09/11/07 NN Type cast mismatch variables */
2900 usLastBitIndex = (unsigned short) (a_usiDataSize - 1);
2904 * Iterate through the data bits.
2908 for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
2909 usDataSizeIndex++) {
2910 if (cByteIndex == 0) {
2913 * Grab byte from DMASK buffer.
2916 if (g_usDataType & DMASK_DATA) {
2917 cDMASKByte = g_pucOutDMaskData[usBufferIndex];
2923 * Grab byte from TDI buffer.
2926 if (g_usDataType & TDI_DATA) {
2927 cInDataByte = g_pucInData[usBufferIndex];
2933 cCurBit = readPort();
2934 cDataByte = (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
2938 * Initialize the byte to be zero.
2941 if (usOutBitIndex % 8 == 0) {
2942 g_pucOutData[usOutBitIndex / 8] = 0x00;
2946 * Use TDI, DMASK, and device TDO to create new TDI (actually
2947 * stored in g_pucOutData).
2950 if ((((cDMASKByte << cByteIndex) & 0x80) ? 0x01 : 0x00)) {
2953 for (usLVDSIndex = 0;
2954 usLVDSIndex < g_usLVDSPairCount;
2956 if (g_pLVDSList[usLVDSIndex].
2959 g_pLVDSList[usLVDSIndex].
2967 * DMASK bit is 1, use TDI.
2970 g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
2971 (((cDataByte & 0x1) ? 0x01 : 0x00) <<
2972 (7 - usOutBitIndex % 8));
2976 * DMASK bit is 0, use device TDO.
2979 g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
2980 (((cCurBit & 0x1) ? 0x01 : 0x00) <<
2981 (7 - usOutBitIndex % 8));
2985 * Shift in TDI in order to get TDO out.
2989 writePort(g_ucPinTDI, cDataByte);
2990 if (usDataSizeIndex < usLastBitIndex) {
2995 * Increment the byte index. If it exceeds 7, then reset it back
3000 if (cByteIndex >= 8) {
3006 * If g_pLVDSList exists and pairs need updating, then update
3007 * the negative-pair to receive the flipped positive-pair value.
3011 for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount;
3013 if (g_pLVDSList[usLVDSIndex].ucUpdate) {
3016 * Read the positive value and flip it.
3019 cDataByte = (unsigned char)
3020 (((g_pucOutData[g_pLVDSList[usLVDSIndex].
3021 usPositiveIndex / 8]
3022 << (g_pLVDSList[usLVDSIndex].
3023 usPositiveIndex % 8)) & 0x80) ?
3025 /* 09/11/07 NN Type cast mismatch variables */
3026 cDataByte = (unsigned char) (!cDataByte);
3029 * Get the byte that needs modification.
3033 g_pucOutData[g_pLVDSList[usLVDSIndex].
3034 usNegativeIndex / 8];
3039 * Copy over the current byte and
3040 * set the negative bit to 1.
3044 for (cLVDSByteIndex = 7;
3045 cLVDSByteIndex >= 0;
3049 (g_pLVDSList[usLVDSIndex].
3050 usNegativeIndex % 8) ==
3054 * Set negative bit to 1
3058 } else if (cInDataByte & 0x80) {
3066 * Store the modified byte.
3069 g_pucOutData[g_pLVDSList[usLVDSIndex].
3070 usNegativeIndex / 8] = cDataByte;
3074 * Copy over the current byte and set
3075 * the negative bit to 0.
3079 for (cLVDSByteIndex = 7;
3080 cLVDSByteIndex >= 0;
3084 (g_pLVDSList[usLVDSIndex].
3085 usNegativeIndex % 8) ==
3089 * Set negative bit to 0
3093 } else if (cInDataByte & 0x80) {
3101 * Store the modified byte.
3104 g_pucOutData[g_pLVDSList[usLVDSIndex].
3105 usNegativeIndex / 8] = cDataByte;
3116 signed char ispVMProcessLVDS(unsigned short a_usLVDSCount)
3118 unsigned short usLVDSIndex = 0;
3121 * Allocate memory to hold LVDS pairs.
3124 ispVMMemManager(LVDS, a_usLVDSCount);
3125 g_usLVDSPairCount = a_usLVDSCount;
3128 printf("LVDS %d (", a_usLVDSCount);
3132 * Iterate through each given LVDS pair.
3135 for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount; usLVDSIndex++) {
3138 * Assign the positive and negative indices of the LVDS pair.
3141 /* 09/11/07 NN Type cast mismatch variables */
3142 g_pLVDSList[usLVDSIndex].usPositiveIndex =
3143 (unsigned short) ispVMDataSize();
3144 /* 09/11/07 NN Type cast mismatch variables */
3145 g_pLVDSList[usLVDSIndex].usNegativeIndex =
3146 (unsigned short)ispVMDataSize();
3149 if (usLVDSIndex < g_usLVDSPairCount - 1) {
3151 g_pLVDSList[usLVDSIndex].usPositiveIndex,
3152 g_pLVDSList[usLVDSIndex].usNegativeIndex);
3155 g_pLVDSList[usLVDSIndex].usPositiveIndex,
3156 g_pLVDSList[usLVDSIndex].usNegativeIndex);
3163 printf(");\n", a_usLVDSCount);