1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <common.h>
33#include <log.h>
34#include <linux/string.h>
35#include <malloc.h>
36#include <lattice.h>
37
38#define vme_out_char(c) printf("%c", c)
39#define vme_out_hex(c) printf("%x", c)
40#define vme_out_string(s) printf("%s", s)
41
42
43
44
45
46
47
48
49
50
51
52
53static unsigned short g_usFlowControl;
54unsigned short g_usDataType;
55
56
57
58
59
60
61
62
63
64
65unsigned char g_ucEndDR = DRPAUSE;
66unsigned char g_ucEndIR = IRPAUSE;
67
68
69
70
71
72
73
74
75
76
77
78
79static unsigned short g_usHeadDR;
80static unsigned short g_usHeadIR;
81static unsigned short g_usTailDR;
82static unsigned short g_usTailIR;
83
84
85
86
87
88
89
90
91static unsigned short g_usiDataSize;
92
93
94
95
96
97
98
99static int g_iFrequency = 1000;
100
101
102
103
104
105
106
107static unsigned short g_usMaxSize;
108
109
110
111
112
113
114
115static unsigned short g_usShiftValue;
116
117
118
119
120
121
122
123static unsigned short g_usRepeatLoops;
124
125
126
127
128
129
130
131static signed char g_cVendor = LATTICE;
132
133
134
135
136
137
138
139unsigned short g_usCalculatedCRC;
140
141
142
143
144
145
146
147unsigned long g_usChecksum;
148static unsigned int g_uiChecksumIndex;
149
150
151
152
153
154
155
156static signed char g_cCurrentJTAGState;
157
158
159
160
161
162
163
164
165
166
167
168unsigned char *g_pucHeapMemory;
169unsigned short g_iHeapCounter;
170unsigned short g_iHEAPSize;
171static unsigned short previous_size;
172
173
174
175
176
177
178
179
180
181
182
183
184unsigned short g_usIntelDataIndex;
185unsigned short g_usIntelBufferSize;
186
187
188
189
190
191
192
193const char *const g_szSupportedVersions[] = {
194 "__VME2.0", "__VME3.0", "____12.0", "____12.1", 0};
195
196
197
198
199
200
201
202
203static unsigned short g_usTDOSize;
204static unsigned short g_usMASKSize;
205static unsigned short g_usTDISize;
206static unsigned short g_usDMASKSize;
207static unsigned short g_usLCOUNTSize;
208static unsigned short g_usHDRSize;
209static unsigned short g_usTDRSize;
210static unsigned short g_usHIRSize;
211static unsigned short g_usTIRSize;
212static unsigned short g_usHeapSize;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230unsigned char *g_pucOutMaskData = NULL,
231 *g_pucInData = NULL,
232 *g_pucOutData = NULL,
233 *g_pucHIRData = NULL,
234 *g_pucTIRData = NULL,
235 *g_pucHDRData = NULL,
236 *g_pucTDRData = NULL,
237 *g_pucIntelBuffer = NULL,
238 *g_pucOutDMaskData = NULL;
239
240
241
242
243
244
245
246struct {
247 unsigned char CurState;
248 unsigned char NextState;
249 unsigned char Pattern;
250 unsigned char Pulses;
251} g_JTAGTransistions[25] = {
252{ RESET, RESET, 0xFC, 6 },
253{ RESET, IDLE, 0x00, 1 },
254{ RESET, DRPAUSE, 0x50, 5 },
255{ RESET, IRPAUSE, 0x68, 6 },
256{ IDLE, RESET, 0xE0, 3 },
257{ IDLE, DRPAUSE, 0xA0, 4 },
258{ IDLE, IRPAUSE, 0xD0, 5 },
259{ DRPAUSE, RESET, 0xF8, 5 },
260{ DRPAUSE, IDLE, 0xC0, 3 },
261{ DRPAUSE, IRPAUSE, 0xF4, 7 },
262{ DRPAUSE, DRPAUSE, 0xE8, 6 },
263{ IRPAUSE, RESET, 0xF8, 5 },
264{ IRPAUSE, IDLE, 0xC0, 3 },
265{ IRPAUSE, DRPAUSE, 0xE8, 6 },
266{ DRPAUSE, SHIFTDR, 0x80, 2 },
267{ IRPAUSE, SHIFTDR, 0xE0, 5 },
268{ SHIFTDR, DRPAUSE, 0x80, 2 },
269{ SHIFTDR, IDLE, 0xC0, 3 },
270{ IRPAUSE, SHIFTIR, 0x80, 2 },
271{ SHIFTIR, IRPAUSE, 0x80, 2 },
272{ SHIFTIR, IDLE, 0xC0, 3 },
273{ DRPAUSE, DRCAPTURE, 0xE0, 4 },
274{ DRCAPTURE, DRPAUSE, 0x80, 2 },
275{ IDLE, DRCAPTURE, 0x80, 2 },
276{ IRPAUSE, DRCAPTURE, 0xE0, 4 }
277};
278
279
280
281
282
283
284
285LVDSPair *g_pLVDSList;
286unsigned short g_usLVDSPairCount;
287
288
289
290
291
292
293
294static signed char ispVMDataCode(void);
295static long int ispVMDataSize(void);
296static void ispVMData(unsigned char *Data);
297static signed char ispVMShift(signed char Code);
298static signed char ispVMAmble(signed char Code);
299static signed char ispVMLoop(unsigned short a_usLoopCount);
300static signed char ispVMBitShift(signed char mode, unsigned short bits);
301static void ispVMComment(unsigned short a_usCommentSize);
302static void ispVMHeader(unsigned short a_usHeaderSize);
303static signed char ispVMLCOUNT(unsigned short a_usCountSize);
304static void ispVMClocks(unsigned short Clocks);
305static void ispVMBypass(signed char ScanType, unsigned short Bits);
306static void ispVMStateMachine(signed char NextState);
307static signed char ispVMSend(unsigned short int);
308static signed char ispVMRead(unsigned short int);
309static signed char ispVMReadandSave(unsigned short int);
310static signed char ispVMProcessLVDS(unsigned short a_usLVDSCount);
311static void ispVMMemManager(signed char types, unsigned short size);
312
313
314
315
316
317
318static signed char g_cCurrentJTAGState;
319
320#ifdef DEBUG
321
322
323
324
325
326
327
328
329
330
331const char *GetState(unsigned char a_ucState)
332{
333 switch (a_ucState) {
334 case RESET:
335 return "RESET";
336 case IDLE:
337 return "IDLE";
338 case IRPAUSE:
339 return "IRPAUSE";
340 case DRPAUSE:
341 return "DRPAUSE";
342 case SHIFTIR:
343 return "SHIFTIR";
344 case SHIFTDR:
345 return "SHIFTDR";
346 case DRCAPTURE:
347 return "DRCAPTURE";
348 default:
349 break;
350 }
351
352 return 0;
353}
354
355
356
357
358
359
360
361
362
363void PrintData(unsigned short a_iDataSize, unsigned char *a_pucData)
364{
365
366 unsigned short usByteSize = 0;
367 unsigned short usBitIndex = 0;
368 signed short usByteIndex = 0;
369 unsigned char ucByte = 0;
370 unsigned char ucFlipByte = 0;
371
372 if (a_iDataSize % 8) {
373
374 usByteSize = (unsigned short)(a_iDataSize / 8 + 1);
375 } else {
376
377 usByteSize = (unsigned short)(a_iDataSize / 8);
378 }
379 puts("(");
380
381 for (usByteIndex = (signed short)(usByteSize - 1);
382 usByteIndex >= 0; usByteIndex--) {
383 ucByte = a_pucData[usByteIndex];
384 ucFlipByte = 0x00;
385
386
387
388
389
390
391
392 for (usBitIndex = 0; usBitIndex < 8; usBitIndex++) {
393 ucFlipByte <<= 1;
394 if (ucByte & 0x1) {
395 ucFlipByte |= 0x1;
396 }
397
398 ucByte >>= 1;
399 }
400
401
402
403
404
405
406
407 printf("%.02X", ucFlipByte);
408 if ((usByteSize - usByteIndex) % 40 == 39) {
409 puts("\n\t\t");
410 }
411 if (usByteIndex < 0)
412 break;
413 }
414 puts(")");
415}
416#endif
417
418void ispVMMemManager(signed char cTarget, unsigned short usSize)
419{
420 switch (cTarget) {
421 case XTDI:
422 case TDI:
423 if (g_pucInData != NULL) {
424 if (previous_size == usSize) {
425 break;
426 } else {
427 free(g_pucInData);
428 g_pucInData = NULL;
429 }
430 }
431 g_pucInData = (unsigned char *) malloc(usSize / 8 + 2);
432 previous_size = usSize;
433 case XTDO:
434 case TDO:
435 if (g_pucOutData != NULL) {
436 if (previous_size == usSize) {
437 break;
438 } else {
439 free(g_pucOutData);
440 g_pucOutData = NULL;
441 }
442 }
443 g_pucOutData = (unsigned char *) malloc(usSize / 8 + 2);
444 previous_size = usSize;
445 break;
446 case MASK:
447 if (g_pucOutMaskData != NULL) {
448 if (previous_size == usSize) {
449 break;
450 } else {
451 free(g_pucOutMaskData);
452 g_pucOutMaskData = NULL;
453 }
454 }
455 g_pucOutMaskData = (unsigned char *) malloc(usSize / 8 + 2);
456 previous_size = usSize;
457 break;
458 case HIR:
459 if (g_pucHIRData != NULL) {
460 free(g_pucHIRData);
461 g_pucHIRData = NULL;
462 }
463 g_pucHIRData = (unsigned char *) malloc(usSize / 8 + 2);
464 break;
465 case TIR:
466 if (g_pucTIRData != NULL) {
467 free(g_pucTIRData);
468 g_pucTIRData = NULL;
469 }
470 g_pucTIRData = (unsigned char *) malloc(usSize / 8 + 2);
471 break;
472 case HDR:
473 if (g_pucHDRData != NULL) {
474 free(g_pucHDRData);
475 g_pucHDRData = NULL;
476 }
477 g_pucHDRData = (unsigned char *) malloc(usSize / 8 + 2);
478 break;
479 case TDR:
480 if (g_pucTDRData != NULL) {
481 free(g_pucTDRData);
482 g_pucTDRData = NULL;
483 }
484 g_pucTDRData = (unsigned char *) malloc(usSize / 8 + 2);
485 break;
486 case HEAP:
487 if (g_pucHeapMemory != NULL) {
488 free(g_pucHeapMemory);
489 g_pucHeapMemory = NULL;
490 }
491 g_pucHeapMemory = (unsigned char *) malloc(usSize + 2);
492 break;
493 case DMASK:
494 if (g_pucOutDMaskData != NULL) {
495 if (previous_size == usSize) {
496 break;
497 } else {
498 free(g_pucOutDMaskData);
499 g_pucOutDMaskData = NULL;
500 }
501 }
502 g_pucOutDMaskData = (unsigned char *) malloc(usSize / 8 + 2);
503 previous_size = usSize;
504 break;
505 case LHEAP:
506 if (g_pucIntelBuffer != NULL) {
507 free(g_pucIntelBuffer);
508 g_pucIntelBuffer = NULL;
509 }
510 g_pucIntelBuffer = (unsigned char *) malloc(usSize + 2);
511 break;
512 case LVDS:
513 if (g_pLVDSList != NULL) {
514 free(g_pLVDSList);
515 g_pLVDSList = NULL;
516 }
517 g_pLVDSList = (LVDSPair *) malloc(usSize * sizeof(LVDSPair));
518 if (g_pLVDSList)
519 memset(g_pLVDSList, 0, usSize * sizeof(LVDSPair));
520 break;
521 default:
522 return;
523 }
524}
525
526void ispVMFreeMem(void)
527{
528 if (g_pucHeapMemory != NULL) {
529 free(g_pucHeapMemory);
530 g_pucHeapMemory = NULL;
531 }
532
533 if (g_pucOutMaskData != NULL) {
534 free(g_pucOutMaskData);
535 g_pucOutMaskData = NULL;
536 }
537
538 if (g_pucInData != NULL) {
539 free(g_pucInData);
540 g_pucInData = NULL;
541 }
542
543 if (g_pucOutData != NULL) {
544 free(g_pucOutData);
545 g_pucOutData = NULL;
546 }
547
548 if (g_pucHIRData != NULL) {
549 free(g_pucHIRData);
550 g_pucHIRData = NULL;
551 }
552
553 if (g_pucTIRData != NULL) {
554 free(g_pucTIRData);
555 g_pucTIRData = NULL;
556 }
557
558 if (g_pucHDRData != NULL) {
559 free(g_pucHDRData);
560 g_pucHDRData = NULL;
561 }
562
563 if (g_pucTDRData != NULL) {
564 free(g_pucTDRData);
565 g_pucTDRData = NULL;
566 }
567
568 if (g_pucOutDMaskData != NULL) {
569 free(g_pucOutDMaskData);
570 g_pucOutDMaskData = NULL;
571 }
572
573 if (g_pucIntelBuffer != NULL) {
574 free(g_pucIntelBuffer);
575 g_pucIntelBuffer = NULL;
576 }
577
578 if (g_pLVDSList != NULL) {
579 free(g_pLVDSList);
580 g_pLVDSList = NULL;
581 }
582}
583
584
585
586
587
588
589
590
591
592
593
594long int ispVMDataSize()
595{
596
597 long int iSize = 0;
598 signed char cCurrentByte = 0;
599 signed char cIndex = 0;
600 cIndex = 0;
601 while ((cCurrentByte = GetByte()) & 0x80) {
602 iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
603 cIndex += 7;
604 }
605 iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
606 return iSize;
607}
608
609
610
611
612
613
614
615
616
617
618
619signed char ispVMCode()
620{
621
622 unsigned short iRepeatSize = 0;
623 signed char cOpcode = 0;
624 signed char cRetCode = 0;
625 unsigned char ucState = 0;
626 unsigned short usDelay = 0;
627 unsigned short usToggle = 0;
628 unsigned char usByte = 0;
629
630
631
632
633
634
635
636
637
638
639 if (!(g_usDataType & LHEAP_IN) && !(g_usDataType & HEAP_IN)) {
640 usByte = GetByte();
641 if (usByte == 0xf1) {
642 g_usDataType |= COMPRESS;
643 } else if (usByte == 0xf2) {
644 g_usDataType &= ~COMPRESS;
645 } else {
646 return VME_INVALID_FILE;
647 }
648 }
649
650
651
652
653
654
655
656 while ((cOpcode = GetByte()) >= 0) {
657
658 switch (cOpcode) {
659 case STATE:
660
661
662
663
664
665 ucState = GetByte();
666
667
668
669
670
671
672 if ((g_usDataType & LHEAP_IN) &&
673 (ucState == DRPAUSE) &&
674 (g_cCurrentJTAGState == ucState)) {
675 ispVMStateMachine(DRCAPTURE);
676 }
677
678 ispVMStateMachine(ucState);
679
680#ifdef DEBUG
681 if (g_usDataType & LHEAP_IN) {
682 debug("LDELAY %s ", GetState(ucState));
683 } else {
684 debug("STATE %s;\n", GetState(ucState));
685 }
686#endif
687 break;
688 case SIR:
689 case SDR:
690 case XSDR:
691
692#ifdef DEBUG
693 switch (cOpcode) {
694 case SIR:
695 puts("SIR ");
696 break;
697 case SDR:
698 case XSDR:
699 if (g_usDataType & LHEAP_IN) {
700 puts("LSDR ");
701 } else {
702 puts("SDR ");
703 }
704 break;
705 }
706#endif
707
708
709
710
711
712
713 cRetCode = ispVMShift(cOpcode);
714 if (cRetCode != 0) {
715 return cRetCode;
716 }
717 break;
718 case WAIT:
719
720
721
722
723
724
725
726
727 usDelay = (unsigned short) ispVMDataSize();
728 ispVMDelay(usDelay);
729
730#ifdef DEBUG
731 if (usDelay & 0x8000) {
732
733
734
735
736
737
738
739 usDelay &= ~0x8000;
740 if (g_usDataType & LHEAP_IN) {
741 printf("%.2E SEC;\n",
742 (float) usDelay / 1000);
743 } else {
744 printf("RUNTEST %.2E SEC;\n",
745 (float) usDelay / 1000);
746 }
747 } else {
748
749
750
751
752
753 if (g_usDataType & LHEAP_IN) {
754 printf("%.2E SEC;\n",
755 (float) usDelay / 1000000);
756 } else {
757 printf("RUNTEST %.2E SEC;\n",
758 (float) usDelay / 1000000);
759 }
760 }
761#endif
762 break;
763 case TCK:
764
765
766
767
768
769
770 usToggle = (unsigned short) ispVMDataSize();
771 ispVMClocks(usToggle);
772
773#ifdef DEBUG
774 printf("RUNTEST %d TCK;\n", usToggle);
775#endif
776 break;
777 case ENDDR:
778
779
780
781
782
783
784
785 g_ucEndDR = GetByte();
786
787#ifdef DEBUG
788 printf("ENDDR %s;\n", GetState(g_ucEndDR));
789#endif
790 break;
791 case ENDIR:
792
793
794
795
796
797
798
799 g_ucEndIR = GetByte();
800
801#ifdef DEBUG
802 printf("ENDIR %s;\n", GetState(g_ucEndIR));
803#endif
804 break;
805 case HIR:
806 case TIR:
807 case HDR:
808 case TDR:
809
810#ifdef DEBUG
811 switch (cOpcode) {
812 case HIR:
813 puts("HIR ");
814 break;
815 case TIR:
816 puts("TIR ");
817 break;
818 case HDR:
819 puts("HDR ");
820 break;
821 case TDR:
822 puts("TDR ");
823 break;
824 }
825#endif
826
827
828
829
830
831
832 cRetCode = ispVMAmble(cOpcode);
833 if (cRetCode != 0) {
834 return cRetCode;
835 }
836
837#ifdef DEBUG
838 puts(";\n");
839#endif
840 break;
841 case MEM:
842
843
844
845
846
847
848
849 g_usMaxSize = (unsigned short) ispVMDataSize();
850
851#ifdef DEBUG
852 printf("// MEMSIZE %d\n", g_usMaxSize);
853#endif
854 break;
855 case VENDOR:
856
857
858
859
860
861
862
863 cOpcode = GetByte();
864 switch (cOpcode) {
865 case LATTICE:
866#ifdef DEBUG
867 puts("// VENDOR LATTICE\n");
868#endif
869 g_cVendor = LATTICE;
870 break;
871 case ALTERA:
872#ifdef DEBUG
873 puts("// VENDOR ALTERA\n");
874#endif
875 g_cVendor = ALTERA;
876 break;
877 case XILINX:
878#ifdef DEBUG
879 puts("// VENDOR XILINX\n");
880#endif
881 g_cVendor = XILINX;
882 break;
883 default:
884 break;
885 }
886 break;
887 case SETFLOW:
888
889
890
891
892
893
894
895 g_usFlowControl |= (unsigned short) ispVMDataSize();
896 break;
897 case RESETFLOW:
898
899
900
901
902
903
904
905
906 g_usFlowControl &= (unsigned short) ~(ispVMDataSize());
907 break;
908 case HEAP:
909
910
911
912
913
914
915
916 cRetCode = GetByte();
917 if (cRetCode != SECUREHEAP) {
918 return VME_INVALID_FILE;
919 }
920
921 g_iHEAPSize = (unsigned short) ispVMDataSize();
922
923
924
925
926
927
928 if (g_iHEAPSize > g_usHeapSize) {
929 g_usHeapSize = g_iHEAPSize;
930 }
931
932 ispVMMemManager(HEAP, (unsigned short) g_iHEAPSize);
933 break;
934 case REPEAT:
935
936
937
938
939
940
941
942 g_usRepeatLoops = 0;
943
944
945 iRepeatSize = (unsigned short) ispVMDataSize();
946
947 cRetCode = ispVMLoop((unsigned short) iRepeatSize);
948 if (cRetCode != 0) {
949 return cRetCode;
950 }
951 break;
952 case ENDLOOP:
953
954
955
956
957
958
959
960 return cRetCode;
961 case ENDVME:
962
963
964
965
966
967
968 return cRetCode;
969 case SHR:
970
971
972
973
974
975
976
977 g_usFlowControl |= SHIFTRIGHT;
978
979
980 g_usShiftValue = (unsigned short) (g_usRepeatLoops *
981 (unsigned short)GetByte());
982 break;
983 case SHL:
984
985
986
987
988
989 g_usFlowControl |= SHIFTLEFT;
990
991
992 g_usShiftValue = (unsigned short) (g_usRepeatLoops *
993 (unsigned short)GetByte());
994 break;
995 case FREQUENCY:
996
997
998
999
1000
1001
1002
1003
1004 g_iFrequency = (int) (ispVMDataSize() / 1000);
1005 if (g_iFrequency == 1)
1006 g_iFrequency = 1000;
1007
1008#ifdef DEBUG
1009 printf("FREQUENCY %.2E HZ;\n",
1010 (float) g_iFrequency * 1000);
1011#endif
1012 break;
1013 case LCOUNT:
1014
1015
1016
1017
1018
1019
1020
1021 cRetCode = ispVMLCOUNT((unsigned short)ispVMDataSize());
1022 if (cRetCode != 0) {
1023 return cRetCode;
1024 }
1025 break;
1026 case VUES:
1027
1028
1029
1030
1031
1032
1033
1034 g_usFlowControl |= VERIFYUES;
1035 break;
1036 case COMMENT:
1037
1038
1039
1040
1041
1042
1043
1044 ispVMComment((unsigned short) ispVMDataSize());
1045 break;
1046 case LVDS:
1047
1048
1049
1050
1051
1052
1053
1054 ispVMProcessLVDS((unsigned short) ispVMDataSize());
1055 break;
1056 case HEADER:
1057
1058
1059
1060
1061
1062
1063
1064 ispVMHeader((unsigned short) ispVMDataSize());
1065 break;
1066
1067 case ispEN:
1068 ucState = GetByte();
1069 if ((ucState == ON) || (ucState == 0x01))
1070 writePort(g_ucPinENABLE, 0x01);
1071 else
1072 writePort(g_ucPinENABLE, 0x00);
1073 ispVMDelay(1);
1074 break;
1075
1076 case TRST:
1077 ucState = GetByte();
1078 if (ucState == 0x01)
1079 writePort(g_ucPinTRST, 0x01);
1080 else
1081 writePort(g_ucPinTRST, 0x00);
1082 ispVMDelay(1);
1083 break;
1084 default:
1085
1086
1087
1088
1089
1090
1091
1092#ifdef DEBUG
1093 printf("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
1094#endif
1095
1096 return VME_INVALID_FILE;
1097 }
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107 return VME_INVALID_FILE;
1108}
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118signed char ispVMDataCode()
1119{
1120
1121 signed char cDataByte = 0;
1122 signed char siDataSource = 0;
1123
1124 if (g_usDataType & HEAP_IN) {
1125 siDataSource = 1;
1126 }
1127
1128
1129
1130
1131
1132
1133
1134 g_usDataType &= ~(MASK_DATA + TDI_DATA +
1135 TDO_DATA + DMASK_DATA + CMASK_DATA);
1136
1137
1138
1139
1140
1141
1142 while ((cDataByte = GetByte()) >= 0) {
1143 ispVMMemManager(cDataByte, g_usMaxSize);
1144 switch (cDataByte) {
1145 case TDI:
1146
1147
1148
1149
1150
1151
1152 if (g_usiDataSize > g_usTDISize) {
1153 g_usTDISize = g_usiDataSize;
1154 }
1155
1156
1157
1158
1159
1160
1161 g_usDataType |= TDI_DATA;
1162 ispVMData(g_pucInData);
1163 break;
1164 case XTDO:
1165
1166
1167
1168
1169
1170
1171 if (g_usiDataSize > g_usTDOSize) {
1172 g_usTDOSize = g_usiDataSize;
1173 }
1174
1175
1176
1177
1178
1179
1180 g_usDataType |= TDO_DATA;
1181 break;
1182 case TDO:
1183
1184
1185
1186
1187
1188
1189 if (g_usiDataSize > g_usTDOSize) {
1190 g_usTDOSize = g_usiDataSize;
1191 }
1192
1193
1194
1195
1196
1197
1198
1199
1200 g_usDataType |= TDO_DATA;
1201 ispVMData(g_pucOutData);
1202 break;
1203 case MASK:
1204
1205
1206
1207
1208
1209
1210 if (g_usiDataSize > g_usMASKSize) {
1211 g_usMASKSize = g_usiDataSize;
1212 }
1213
1214
1215
1216
1217
1218
1219
1220 g_usDataType |= MASK_DATA;
1221 ispVMData(g_pucOutMaskData);
1222 break;
1223 case DMASK:
1224
1225
1226
1227
1228
1229
1230 if (g_usiDataSize > g_usDMASKSize) {
1231 g_usDMASKSize = g_usiDataSize;
1232 }
1233
1234
1235
1236
1237
1238
1239
1240
1241 g_usDataType |= DMASK_DATA;
1242 ispVMData(g_pucOutDMaskData);
1243 break;
1244 case CMASK:
1245
1246
1247
1248
1249
1250
1251
1252 g_usDataType |= CMASK_DATA;
1253 ispVMData(g_pucOutMaskData);
1254 break;
1255 case CONTINUE:
1256 return 0;
1257 default:
1258
1259
1260
1261 return VME_INVALID_FILE;
1262 }
1263
1264 switch (cDataByte) {
1265 case TDI:
1266
1267
1268
1269
1270
1271
1272 if (g_usFlowControl & SHIFTLEFT) {
1273 ispVMBitShift(SHL, g_usShiftValue);
1274 g_usFlowControl &= ~SHIFTLEFT;
1275 }
1276
1277
1278
1279
1280
1281
1282 if (g_usFlowControl & SHIFTRIGHT) {
1283 ispVMBitShift(SHR, g_usShiftValue);
1284 g_usFlowControl &= ~SHIFTRIGHT;
1285 }
1286 default:
1287 break;
1288 }
1289
1290 if (siDataSource) {
1291 g_usDataType |= HEAP_IN;
1292 }
1293 }
1294
1295 if (siDataSource) {
1296 g_usDataType |= HEAP_IN;
1297 }
1298
1299 if (cDataByte < 0) {
1300
1301
1302
1303
1304
1305 return VME_INVALID_FILE;
1306 } else {
1307 return 0;
1308 }
1309}
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358void ispVMData(unsigned char *ByteData)
1359{
1360
1361 unsigned short size = 0;
1362 unsigned short i, j, m, getData = 0;
1363 unsigned char cDataByte = 0;
1364 unsigned char compress = 0;
1365 unsigned short FFcount = 0;
1366 unsigned char compr_char = 0xFF;
1367 unsigned short index = 0;
1368 signed char compression = 0;
1369
1370
1371 if (g_usiDataSize % 8 > 0) {
1372
1373 size = (unsigned short)(g_usiDataSize / 8 + 1);
1374 } else {
1375
1376 size = (unsigned short)(g_usiDataSize / 8);
1377 }
1378
1379
1380
1381
1382
1383
1384 if (g_usDataType & COMPRESS) {
1385 compression = 1;
1386 compress = GetByte();
1387 if ((compress == VAR) && (g_usDataType & HEAP_IN)) {
1388 getData = 1;
1389 g_usDataType &= ~(HEAP_IN);
1390 compress = GetByte();
1391 }
1392
1393 switch (compress) {
1394 case 0x00:
1395
1396 compression = 0;
1397 break;
1398 case 0x01:
1399
1400 compr_char = 0x00;
1401 break;
1402 case 0x02:
1403
1404 compr_char = 0xFF;
1405 break;
1406 case 0xFF:
1407
1408 compr_char = GetByte();
1409 i = 8;
1410 for (index = 0; index < size; index++) {
1411 ByteData[index] = 0x00;
1412 if (i > 7) {
1413 cDataByte = GetByte();
1414 i = 0;
1415 }
1416 if ((cDataByte << i++) & 0x80)
1417 m = 8;
1418 else {
1419 ByteData[index] = compr_char;
1420 m = 0;
1421 }
1422
1423 for (j = 0; j < m; j++) {
1424 if (i > 7) {
1425 cDataByte = GetByte();
1426 i = 0;
1427 }
1428 ByteData[index] |=
1429 ((cDataByte << i++) & 0x80) >> j;
1430 }
1431 }
1432 size = 0;
1433 break;
1434 default:
1435 for (index = 0; index < size; index++)
1436 ByteData[index] = 0x00;
1437 for (index = 0; index < compress; index++) {
1438 if (index % 2 == 0)
1439 cDataByte = GetByte();
1440 for (i = 0; i < size * 2 / compress; i++) {
1441 j = (unsigned short)(index +
1442 (i * (unsigned short)compress));
1443
1444 if (j%2) {
1445 if (index % 2)
1446 ByteData[j/2] |=
1447 cDataByte & 0xF;
1448 else
1449 ByteData[j/2] |=
1450 cDataByte >> 4;
1451 } else {
1452 if (index % 2)
1453 ByteData[j/2] |=
1454 cDataByte << 4;
1455 else
1456 ByteData[j/2] |=
1457 cDataByte & 0xF0;
1458 }
1459 }
1460 }
1461 size = 0;
1462 break;
1463 }
1464 }
1465
1466 FFcount = 0;
1467
1468
1469 for (index = 0; index < size; index++) {
1470 if (FFcount <= 0) {
1471 cDataByte = GetByte();
1472 if ((cDataByte == VAR) && (g_usDataType&HEAP_IN) &&
1473 !getData && !(g_usDataType&COMPRESS)) {
1474 getData = 1;
1475 g_usDataType &= ~(HEAP_IN);
1476 cDataByte = GetByte();
1477 }
1478 ByteData[index] = cDataByte;
1479 if ((compression) && (cDataByte == compr_char))
1480
1481 FFcount = (unsigned short) ispVMDataSize();
1482
1483 } else {
1484 FFcount--;
1485 ByteData[index] = compr_char;
1486 }
1487 }
1488
1489 if (getData) {
1490 g_usDataType |= HEAP_IN;
1491 getData = 0;
1492 }
1493}
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503signed char ispVMShift(signed char a_cCode)
1504{
1505
1506 unsigned short iDataIndex = 0;
1507 unsigned short iReadLoop = 0;
1508 signed char cRetCode = 0;
1509
1510 cRetCode = 0;
1511
1512 g_usiDataSize = (unsigned short) ispVMDataSize();
1513
1514
1515 g_usDataType &= ~(SIR_DATA + EXPRESS + SDR_DATA);
1516 switch (a_cCode) {
1517 case SIR:
1518 g_usDataType |= SIR_DATA;
1519
1520
1521
1522
1523 if (g_usFlowControl & CASCADE) {
1524 ispVMStateMachine(SHIFTIR);
1525 } else {
1526 ispVMStateMachine(IRPAUSE);
1527 ispVMStateMachine(SHIFTIR);
1528 if (g_usHeadIR > 0) {
1529 ispVMBypass(HIR, g_usHeadIR);
1530 sclock();
1531 }
1532 }
1533 break;
1534 case XSDR:
1535 g_usDataType |= EXPRESS;
1536 case SDR:
1537 g_usDataType |= SDR_DATA;
1538
1539
1540
1541
1542
1543 if (g_cCurrentJTAGState != SHIFTDR) {
1544
1545
1546
1547
1548
1549 if (g_usFlowControl & CASCADE) {
1550 if (g_cCurrentJTAGState == DRPAUSE) {
1551 ispVMStateMachine(SHIFTDR);
1552
1553
1554
1555
1556
1557
1558
1559
1560 if (g_usHeadDR > 0) {
1561 ispVMBypass(HDR, g_usHeadDR);
1562 sclock();
1563 }
1564 } else {
1565 ispVMStateMachine(SHIFTDR);
1566 }
1567 } else {
1568 ispVMStateMachine(DRPAUSE);
1569 ispVMStateMachine(SHIFTDR);
1570 if (g_usHeadDR > 0) {
1571 ispVMBypass(HDR, g_usHeadDR);
1572 sclock();
1573 }
1574 }
1575 }
1576 break;
1577 default:
1578 return VME_INVALID_FILE;
1579 }
1580
1581 cRetCode = ispVMDataCode();
1582
1583 if (cRetCode != 0) {
1584 return VME_INVALID_FILE;
1585 }
1586
1587#ifdef DEBUG
1588 printf("%d ", g_usiDataSize);
1589
1590 if (g_usDataType & TDI_DATA) {
1591 puts("TDI ");
1592 PrintData(g_usiDataSize, g_pucInData);
1593 }
1594
1595 if (g_usDataType & TDO_DATA) {
1596 puts("\n\t\tTDO ");
1597 PrintData(g_usiDataSize, g_pucOutData);
1598 }
1599
1600 if (g_usDataType & MASK_DATA) {
1601 puts("\n\t\tMASK ");
1602 PrintData(g_usiDataSize, g_pucOutMaskData);
1603 }
1604
1605 if (g_usDataType & DMASK_DATA) {
1606 puts("\n\t\tDMASK ");
1607 PrintData(g_usiDataSize, g_pucOutDMaskData);
1608 }
1609
1610 puts(";\n");
1611#endif
1612
1613 if (g_usDataType & TDO_DATA || g_usDataType & DMASK_DATA) {
1614 if (g_usDataType & DMASK_DATA) {
1615 cRetCode = ispVMReadandSave(g_usiDataSize);
1616 if (!cRetCode) {
1617 if (g_usTailDR > 0) {
1618 sclock();
1619 ispVMBypass(TDR, g_usTailDR);
1620 }
1621 ispVMStateMachine(DRPAUSE);
1622 ispVMStateMachine(SHIFTDR);
1623 if (g_usHeadDR > 0) {
1624 ispVMBypass(HDR, g_usHeadDR);
1625 sclock();
1626 }
1627 for (iDataIndex = 0;
1628 iDataIndex < g_usiDataSize / 8 + 1;
1629 iDataIndex++)
1630 g_pucInData[iDataIndex] =
1631 g_pucOutData[iDataIndex];
1632 g_usDataType &= ~(TDO_DATA + DMASK_DATA);
1633 cRetCode = ispVMSend(g_usiDataSize);
1634 }
1635 } else {
1636 cRetCode = ispVMRead(g_usiDataSize);
1637 if (cRetCode == -1 && g_cVendor == XILINX) {
1638 for (iReadLoop = 0; iReadLoop < 30;
1639 iReadLoop++) {
1640 cRetCode = ispVMRead(g_usiDataSize);
1641 if (!cRetCode) {
1642 break;
1643 } else {
1644
1645 ispVMStateMachine(DRPAUSE);
1646
1647
1648
1649
1650 ispVMBypass(TDR, g_usTailDR);
1651 ispVMStateMachine(g_ucEndDR);
1652 ispVMStateMachine(IDLE);
1653 ispVMDelay(1000);
1654 }
1655 }
1656 }
1657 }
1658 } else {
1659 cRetCode = ispVMSend(g_usiDataSize);
1660 }
1661
1662
1663 if ((g_usDataType & EXPRESS) || (a_cCode == SDR)) {
1664 if (g_pucOutData) {
1665 for (iDataIndex = 0; iDataIndex < g_usiDataSize / 8 + 1;
1666 iDataIndex++)
1667 g_pucOutData[iDataIndex] =
1668 g_pucInData[iDataIndex];
1669 }
1670 }
1671
1672 switch (a_cCode) {
1673 case SIR:
1674
1675 if (!(g_usFlowControl & CASCADE)) {
1676 if (g_usTailIR > 0) {
1677 sclock();
1678 ispVMBypass(TIR, g_usTailIR);
1679 }
1680 ispVMStateMachine(g_ucEndIR);
1681 }
1682 break;
1683 case XSDR:
1684 case SDR:
1685
1686 if (!(g_usFlowControl & CASCADE)) {
1687 if (g_usTailDR > 0) {
1688 sclock();
1689 ispVMBypass(TDR, g_usTailDR);
1690 }
1691 ispVMStateMachine(g_ucEndDR);
1692 }
1693 break;
1694 default:
1695 break;
1696 }
1697
1698 return cRetCode;
1699}
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719signed char ispVMAmble(signed char Code)
1720{
1721 signed char compress = 0;
1722
1723 g_usiDataSize = (unsigned short)ispVMDataSize();
1724
1725#ifdef DEBUG
1726 printf("%d", g_usiDataSize);
1727#endif
1728
1729 if (g_usiDataSize) {
1730
1731
1732
1733
1734
1735
1736
1737 GetByte();
1738 if (g_usDataType & COMPRESS) {
1739 g_usDataType &= ~(COMPRESS);
1740 compress = 1;
1741 }
1742 }
1743
1744 switch (Code) {
1745 case HIR:
1746
1747
1748
1749
1750
1751
1752 if (g_usiDataSize > g_usHIRSize) {
1753 g_usHIRSize = g_usiDataSize;
1754 }
1755
1756
1757
1758
1759
1760 g_usHeadIR = g_usiDataSize;
1761 if (g_usHeadIR) {
1762 ispVMMemManager(HIR, g_usHeadIR);
1763 ispVMData(g_pucHIRData);
1764
1765#ifdef DEBUG
1766 puts(" TDI ");
1767 PrintData(g_usHeadIR, g_pucHIRData);
1768#endif
1769 }
1770 break;
1771 case TIR:
1772
1773
1774
1775
1776
1777
1778 if (g_usiDataSize > g_usTIRSize) {
1779 g_usTIRSize = g_usiDataSize;
1780 }
1781
1782
1783
1784
1785
1786 g_usTailIR = g_usiDataSize;
1787 if (g_usTailIR) {
1788 ispVMMemManager(TIR, g_usTailIR);
1789 ispVMData(g_pucTIRData);
1790
1791#ifdef DEBUG
1792 puts(" TDI ");
1793 PrintData(g_usTailIR, g_pucTIRData);
1794#endif
1795 }
1796 break;
1797 case HDR:
1798
1799
1800
1801
1802
1803
1804 if (g_usiDataSize > g_usHDRSize) {
1805 g_usHDRSize = g_usiDataSize;
1806 }
1807
1808
1809
1810
1811
1812
1813 g_usHeadDR = g_usiDataSize;
1814 if (g_usHeadDR) {
1815 ispVMMemManager(HDR, g_usHeadDR);
1816 ispVMData(g_pucHDRData);
1817
1818#ifdef DEBUG
1819 puts(" TDI ");
1820 PrintData(g_usHeadDR, g_pucHDRData);
1821#endif
1822 }
1823 break;
1824 case TDR:
1825
1826
1827
1828
1829
1830
1831 if (g_usiDataSize > g_usTDRSize) {
1832 g_usTDRSize = g_usiDataSize;
1833 }
1834
1835
1836
1837
1838
1839
1840 g_usTailDR = g_usiDataSize;
1841 if (g_usTailDR) {
1842 ispVMMemManager(TDR, g_usTailDR);
1843 ispVMData(g_pucTDRData);
1844
1845#ifdef DEBUG
1846 puts(" TDI ");
1847 PrintData(g_usTailDR, g_pucTDRData);
1848#endif
1849 }
1850 break;
1851 default:
1852 break;
1853 }
1854
1855
1856
1857
1858
1859
1860
1861 if (compress) {
1862 g_usDataType |= COMPRESS;
1863 }
1864
1865 if (g_usiDataSize) {
1866 Code = GetByte();
1867 if (Code == CONTINUE) {
1868 return 0;
1869 } else {
1870
1871
1872
1873
1874
1875 return VME_INVALID_FILE;
1876 }
1877 }
1878
1879 return 0;
1880}
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894signed char ispVMLoop(unsigned short a_usLoopCount)
1895{
1896
1897 signed char cRetCode = 0;
1898 unsigned short iHeapIndex = 0;
1899 unsigned short iLoopIndex = 0;
1900
1901 g_usShiftValue = 0;
1902 for (iHeapIndex = 0; iHeapIndex < g_iHEAPSize; iHeapIndex++) {
1903 g_pucHeapMemory[iHeapIndex] = GetByte();
1904 }
1905
1906 if (g_pucHeapMemory[iHeapIndex - 1] != ENDLOOP) {
1907 return VME_INVALID_FILE;
1908 }
1909
1910 g_usFlowControl |= REPEATLOOP;
1911 g_usDataType |= HEAP_IN;
1912
1913 for (iLoopIndex = 0; iLoopIndex < a_usLoopCount; iLoopIndex++) {
1914 g_iHeapCounter = 0;
1915 cRetCode = ispVMCode();
1916 g_usRepeatLoops++;
1917 if (cRetCode < 0) {
1918 break;
1919 }
1920 }
1921
1922 g_usDataType &= ~(HEAP_IN);
1923 g_usFlowControl &= ~(REPEATLOOP);
1924 return cRetCode;
1925}
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937signed char ispVMBitShift(signed char mode, unsigned short bits)
1938{
1939
1940 unsigned short i = 0;
1941 unsigned short size = 0;
1942 unsigned short tmpbits = 0;
1943
1944 if (g_usiDataSize % 8 > 0) {
1945
1946 size = (unsigned short)(g_usiDataSize / 8 + 1);
1947 } else {
1948
1949 size = (unsigned short)(g_usiDataSize / 8);
1950 }
1951
1952 switch (mode) {
1953 case SHR:
1954 for (i = 0; i < size; i++) {
1955 if (g_pucInData[i] != 0) {
1956 tmpbits = bits;
1957 while (tmpbits > 0) {
1958 g_pucInData[i] <<= 1;
1959 if (g_pucInData[i] == 0) {
1960 i--;
1961 g_pucInData[i] = 1;
1962 }
1963 tmpbits--;
1964 }
1965 }
1966 }
1967 break;
1968 case SHL:
1969 for (i = 0; i < size; i++) {
1970 if (g_pucInData[i] != 0) {
1971 tmpbits = bits;
1972 while (tmpbits > 0) {
1973 g_pucInData[i] >>= 1;
1974 if (g_pucInData[i] == 0) {
1975 i--;
1976 g_pucInData[i] = 8;
1977 }
1978 tmpbits--;
1979 }
1980 }
1981 }
1982 break;
1983 default:
1984 return VME_INVALID_FILE;
1985 }
1986
1987 return 0;
1988}
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998void ispVMComment(unsigned short a_usCommentSize)
1999{
2000 char cCurByte = 0;
2001 for (; a_usCommentSize > 0; a_usCommentSize--) {
2002
2003
2004
2005
2006
2007 cCurByte = GetByte();
2008 vme_out_char(cCurByte);
2009 }
2010 cCurByte = '\n';
2011 vme_out_char(cCurByte);
2012}
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022void ispVMHeader(unsigned short a_usHeaderSize)
2023{
2024 for (; a_usHeaderSize > 0; a_usHeaderSize--) {
2025 GetByte();
2026 }
2027}
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037void ispVMCalculateCRC32(unsigned char a_ucData)
2038{
2039
2040 unsigned char ucIndex = 0;
2041 unsigned char ucFlipData = 0;
2042 unsigned short usCRCTableEntry = 0;
2043 unsigned int crc_table[16] = {
2044 0x0000, 0xCC01, 0xD801,
2045 0x1400, 0xF001, 0x3C00,
2046 0x2800, 0xE401, 0xA001,
2047 0x6C00, 0x7800, 0xB401,
2048 0x5000, 0x9C01, 0x8801,
2049 0x4400
2050 };
2051
2052 for (ucIndex = 0; ucIndex < 8; ucIndex++) {
2053 ucFlipData <<= 1;
2054 if (a_ucData & 0x01) {
2055 ucFlipData |= 0x01;
2056 }
2057 a_ucData >>= 1;
2058 }
2059
2060
2061 usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
2062 g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
2063 g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
2064 usCRCTableEntry ^ crc_table[ucFlipData & 0xF]);
2065 usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
2066 g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
2067 g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
2068 usCRCTableEntry ^ crc_table[(ucFlipData >> 4) & 0xF]);
2069}
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079signed char ispVMLCOUNT(unsigned short a_usCountSize)
2080{
2081 unsigned short usContinue = 1;
2082 unsigned short usIntelBufferIndex = 0;
2083 unsigned short usCountIndex = 0;
2084 signed char cRetCode = 0;
2085 signed char cRepeatHeap = 0;
2086 signed char cOpcode = 0;
2087 unsigned char ucState = 0;
2088 unsigned short usDelay = 0;
2089 unsigned short usToggle = 0;
2090
2091 g_usIntelBufferSize = (unsigned short)ispVMDataSize();
2092
2093
2094
2095
2096
2097
2098 ispVMMemManager(LHEAP, g_usIntelBufferSize);
2099
2100
2101
2102
2103
2104
2105 if (g_usIntelBufferSize > g_usLCOUNTSize) {
2106 g_usLCOUNTSize = g_usIntelBufferSize;
2107 }
2108
2109
2110
2111
2112
2113 for (usIntelBufferIndex = 0; usIntelBufferIndex < g_usIntelBufferSize;
2114 usIntelBufferIndex++) {
2115 g_pucIntelBuffer[usIntelBufferIndex] = GetByte();
2116 }
2117
2118
2119
2120
2121
2122
2123 g_usDataType |= LHEAP_IN;
2124
2125
2126
2127
2128
2129
2130
2131
2132 if (g_usDataType & HEAP_IN) {
2133 g_usDataType &= ~HEAP_IN;
2134 cRepeatHeap = 1;
2135 }
2136
2137#ifdef DEBUG
2138 printf("LCOUNT %d;\n", a_usCountSize);
2139#endif
2140
2141
2142
2143
2144
2145 for (usCountIndex = 0; usCountIndex < a_usCountSize; usCountIndex++) {
2146
2147
2148
2149
2150
2151
2152
2153 g_usIntelDataIndex = 0;
2154 cOpcode = 0;
2155 ucState = 0;
2156 usDelay = 0;
2157 usToggle = 0;
2158 usContinue = 1;
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170 while (usContinue) {
2171 cOpcode = GetByte();
2172 switch (cOpcode) {
2173 case HIR:
2174 case TIR:
2175 case HDR:
2176 case TDR:
2177
2178
2179
2180
2181
2182 ispVMAmble(cOpcode);
2183 break;
2184 case STATE:
2185
2186
2187
2188
2189
2190 ucState = GetByte();
2191
2192
2193
2194
2195
2196 if ((g_usDataType & LHEAP_IN) &&
2197 (ucState == DRPAUSE) &&
2198 (g_cCurrentJTAGState == ucState)) {
2199 ispVMStateMachine(DRCAPTURE);
2200 }
2201 ispVMStateMachine(ucState);
2202#ifdef DEBUG
2203 printf("LDELAY %s ", GetState(ucState));
2204#endif
2205 break;
2206 case SIR:
2207#ifdef DEBUG
2208 printf("SIR ");
2209#endif
2210
2211
2212
2213
2214 cRetCode = ispVMShift(cOpcode);
2215 break;
2216 case SDR:
2217
2218#ifdef DEBUG
2219 printf("LSDR ");
2220#endif
2221
2222
2223
2224
2225 cRetCode = ispVMShift(cOpcode);
2226 break;
2227 case WAIT:
2228
2229
2230
2231
2232
2233
2234
2235 usDelay = (unsigned short)ispVMDataSize();
2236 ispVMDelay(usDelay);
2237
2238#ifdef DEBUG
2239 if (usDelay & 0x8000) {
2240
2241
2242
2243
2244
2245
2246
2247
2248 usDelay &= ~0x8000;
2249 printf("%.2E SEC;\n",
2250 (float) usDelay / 1000);
2251 } else {
2252
2253
2254
2255
2256
2257 printf("%.2E SEC;\n",
2258 (float) usDelay / 1000000);
2259 }
2260#endif
2261 break;
2262 case TCK:
2263
2264
2265
2266
2267
2268 usToggle = (unsigned short)ispVMDataSize();
2269 ispVMClocks(usToggle);
2270
2271#ifdef DEBUG
2272 printf("RUNTEST %d TCK;\n", usToggle);
2273#endif
2274 break;
2275 case ENDLOOP:
2276
2277
2278
2279
2280 usContinue = 0;
2281 break;
2282
2283 case COMMENT:
2284
2285
2286
2287
2288
2289 ispVMComment((unsigned short) ispVMDataSize());
2290 break;
2291 case ispEN:
2292 ucState = GetByte();
2293 if ((ucState == ON) || (ucState == 0x01))
2294 writePort(g_ucPinENABLE, 0x01);
2295 else
2296 writePort(g_ucPinENABLE, 0x00);
2297 ispVMDelay(1);
2298 break;
2299 case TRST:
2300 if (GetByte() == 0x01)
2301 writePort(g_ucPinTRST, 0x01);
2302 else
2303 writePort(g_ucPinTRST, 0x00);
2304 ispVMDelay(1);
2305 break;
2306 default:
2307
2308
2309
2310
2311
2312 debug("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
2313
2314 return VME_INVALID_FILE;
2315 }
2316 }
2317 if (cRetCode >= 0) {
2318
2319
2320
2321
2322 break;
2323 }
2324
2325 }
2326
2327
2328
2329
2330
2331 if (cRepeatHeap) {
2332 g_usDataType |= HEAP_IN;
2333 }
2334
2335
2336
2337
2338
2339
2340 g_usDataType &= ~LHEAP_IN;
2341 return cRetCode;
2342}
2343
2344
2345
2346
2347
2348
2349
2350
2351void ispVMClocks(unsigned short Clocks)
2352{
2353 unsigned short iClockIndex = 0;
2354 for (iClockIndex = 0; iClockIndex < Clocks; iClockIndex++) {
2355 sclock();
2356 }
2357}
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372void ispVMBypass(signed char ScanType, unsigned short Bits)
2373{
2374
2375 unsigned short iIndex = 0;
2376 unsigned short iSourceIndex = 0;
2377 unsigned char cBitState = 0;
2378 unsigned char cCurByte = 0;
2379 unsigned char *pcSource = NULL;
2380
2381 if (Bits <= 0) {
2382 return;
2383 }
2384
2385 switch (ScanType) {
2386 case HIR:
2387 pcSource = g_pucHIRData;
2388 break;
2389 case TIR:
2390 pcSource = g_pucTIRData;
2391 break;
2392 case HDR:
2393 pcSource = g_pucHDRData;
2394 break;
2395 case TDR:
2396 pcSource = g_pucTDRData;
2397 break;
2398 default:
2399 break;
2400 }
2401
2402 iSourceIndex = 0;
2403 cBitState = 0;
2404 for (iIndex = 0; iIndex < Bits - 1; iIndex++) {
2405
2406 if (iIndex % 8 == 0) {
2407 cCurByte = pcSource[iSourceIndex++];
2408 }
2409 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2410 ? 0x01 : 0x00);
2411 writePort(g_ucPinTDI, cBitState);
2412 sclock();
2413 }
2414
2415 if (iIndex % 8 == 0) {
2416 cCurByte = pcSource[iSourceIndex++];
2417 }
2418
2419 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2420 ? 0x01 : 0x00);
2421 writePort(g_ucPinTDI, cBitState);
2422}
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435void ispVMStateMachine(signed char cNextJTAGState)
2436{
2437
2438 signed char cPathIndex = 0;
2439 signed char cStateIndex = 0;
2440
2441 if ((g_cCurrentJTAGState == cNextJTAGState) &&
2442 (cNextJTAGState != RESET)) {
2443 return;
2444 }
2445
2446 for (cStateIndex = 0; cStateIndex < 25; cStateIndex++) {
2447 if ((g_cCurrentJTAGState ==
2448 g_JTAGTransistions[cStateIndex].CurState) &&
2449 (cNextJTAGState ==
2450 g_JTAGTransistions[cStateIndex].NextState)) {
2451 break;
2452 }
2453 }
2454
2455 g_cCurrentJTAGState = cNextJTAGState;
2456 for (cPathIndex = 0;
2457 cPathIndex < g_JTAGTransistions[cStateIndex].Pulses;
2458 cPathIndex++) {
2459 if ((g_JTAGTransistions[cStateIndex].Pattern << cPathIndex)
2460 & 0x80) {
2461 writePort(g_ucPinTMS, (unsigned char) 0x01);
2462 } else {
2463 writePort(g_ucPinTMS, (unsigned char) 0x00);
2464 }
2465 sclock();
2466 }
2467
2468 writePort(g_ucPinTDI, 0x00);
2469 writePort(g_ucPinTMS, 0x00);
2470}
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480void ispVMStart()
2481{
2482#ifdef DEBUG
2483 printf("// ISPVM EMBEDDED ADDED\n");
2484 printf("STATE RESET;\n");
2485#endif
2486 g_usFlowControl = 0;
2487 g_usDataType = g_uiChecksumIndex = g_cCurrentJTAGState = 0;
2488 g_usHeadDR = g_usHeadIR = g_usTailDR = g_usTailIR = 0;
2489 g_usMaxSize = g_usShiftValue = g_usRepeatLoops = 0;
2490 g_usTDOSize = g_usMASKSize = g_usTDISize = 0;
2491 g_usDMASKSize = g_usLCOUNTSize = g_usHDRSize = 0;
2492 g_usTDRSize = g_usHIRSize = g_usTIRSize = g_usHeapSize = 0;
2493 g_pLVDSList = NULL;
2494 g_usLVDSPairCount = 0;
2495 previous_size = 0;
2496
2497 ispVMStateMachine(RESET);
2498}
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509void ispVMEnd()
2510{
2511#ifdef DEBUG
2512 printf("// ISPVM EMBEDDED ADDED\n");
2513 printf("STATE RESET;\n");
2514 printf("RUNTEST 1.00E-001 SEC;\n");
2515#endif
2516
2517 ispVMStateMachine(RESET);
2518 ispVMDelay(1000);
2519}
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530signed char ispVMSend(unsigned short a_usiDataSize)
2531{
2532
2533 unsigned short iIndex = 0;
2534 unsigned short iInDataIndex = 0;
2535 unsigned char cCurByte = 0;
2536 unsigned char cBitState = 0;
2537
2538 for (iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++) {
2539 if (iIndex % 8 == 0) {
2540 cCurByte = g_pucInData[iInDataIndex++];
2541 }
2542 cBitState = (unsigned char)(((cCurByte << iIndex % 8) & 0x80)
2543 ? 0x01 : 0x00);
2544 writePort(g_ucPinTDI, cBitState);
2545 sclock();
2546 }
2547
2548 if (iIndex % 8 == 0) {
2549
2550 cCurByte = g_pucInData[iInDataIndex];
2551 }
2552
2553 cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
2554 ? 0x01 : 0x00);
2555
2556 writePort(g_ucPinTDI, cBitState);
2557 if (g_usFlowControl & CASCADE) {
2558
2559 sclock();
2560 }
2561
2562 return 0;
2563}
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573signed char ispVMRead(unsigned short a_usiDataSize)
2574{
2575
2576 unsigned short usDataSizeIndex = 0;
2577 unsigned short usErrorCount = 0;
2578 unsigned short usLastBitIndex = 0;
2579 unsigned char cDataByte = 0;
2580 unsigned char cMaskByte = 0;
2581 unsigned char cInDataByte = 0;
2582 unsigned char cCurBit = 0;
2583 unsigned char cByteIndex = 0;
2584 unsigned short usBufferIndex = 0;
2585 unsigned char ucDisplayByte = 0x00;
2586 unsigned char ucDisplayFlag = 0x01;
2587 char StrChecksum[256] = {0};
2588 unsigned char g_usCalculateChecksum = 0x00;
2589
2590
2591 usLastBitIndex = (unsigned short)(a_usiDataSize - 1);
2592
2593#ifndef DEBUG
2594
2595
2596
2597
2598
2599
2600
2601 for (usDataSizeIndex = 0; usDataSizeIndex < (a_usiDataSize + 7) / 8;
2602 usDataSizeIndex++) {
2603 if (g_usDataType & MASK_DATA) {
2604 if (g_pucOutMaskData[usDataSizeIndex] != 0x00) {
2605 ucDisplayFlag = 0x00;
2606 break;
2607 }
2608 } else if (g_usDataType & CMASK_DATA) {
2609 g_usCalculateChecksum = 0x01;
2610 ucDisplayFlag = 0x00;
2611 break;
2612 } else {
2613 ucDisplayFlag = 0x00;
2614 break;
2615 }
2616 }
2617#endif
2618
2619
2620
2621
2622
2623
2624
2625 for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
2626 usDataSizeIndex++) {
2627 if (cByteIndex == 0) {
2628
2629
2630
2631
2632
2633 if (g_usDataType & TDO_DATA) {
2634 cDataByte = g_pucOutData[usBufferIndex];
2635 }
2636
2637
2638
2639
2640
2641 if (g_usDataType & MASK_DATA) {
2642 cMaskByte = g_pucOutMaskData[usBufferIndex];
2643 } else {
2644 cMaskByte = 0xFF;
2645 }
2646
2647
2648
2649
2650
2651 if (g_usDataType & CMASK_DATA) {
2652 cMaskByte = 0x00;
2653 g_usCalculateChecksum = 0x01;
2654 }
2655
2656
2657
2658
2659
2660 if (g_usDataType & TDI_DATA) {
2661 cInDataByte = g_pucInData[usBufferIndex];
2662 }
2663
2664 usBufferIndex++;
2665 }
2666
2667 cCurBit = readPort();
2668
2669 if (ucDisplayFlag) {
2670 ucDisplayByte <<= 1;
2671 ucDisplayByte |= cCurBit;
2672 }
2673
2674
2675
2676
2677
2678 if (g_usDataType & TDO_DATA) {
2679
2680 if (g_usCalculateChecksum) {
2681 if (cCurBit == 0x01)
2682 g_usChecksum +=
2683 (1 << (g_uiChecksumIndex % 8));
2684 g_uiChecksumIndex++;
2685 } else {
2686 if ((((cMaskByte << cByteIndex) & 0x80)
2687 ? 0x01 : 0x00)) {
2688 if (cCurBit != (unsigned char)
2689 (((cDataByte << cByteIndex) & 0x80)
2690 ? 0x01 : 0x00)) {
2691 usErrorCount++;
2692 }
2693 }
2694 }
2695 }
2696
2697
2698
2699
2700
2701 writePort(g_ucPinTDI,
2702 (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
2703 ? 0x01 : 0x00));
2704
2705 if (usDataSizeIndex < usLastBitIndex) {
2706
2707
2708
2709
2710
2711 sclock();
2712 } else if (g_usFlowControl & CASCADE) {
2713
2714
2715
2716
2717
2718 sclock();
2719 }
2720
2721
2722
2723
2724
2725
2726 cByteIndex++;
2727 if (cByteIndex >= 8) {
2728 if (ucDisplayFlag) {
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739 g_pucOutData[usBufferIndex - 1] = ucDisplayByte;
2740 ucDisplayByte = 0;
2741 }
2742
2743 cByteIndex = 0;
2744 }
2745
2746 else if (a_usiDataSize == 1) {
2747 if (ucDisplayFlag) {
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762 cDataByte = 0x00;
2763 for (usBufferIndex = 0; usBufferIndex < 8;
2764 usBufferIndex++) {
2765 cDataByte <<= 1;
2766 if (ucDisplayByte & 0x01) {
2767 cDataByte |= 0x01;
2768 }
2769 ucDisplayByte >>= 1;
2770 }
2771 g_pucOutData[0] = cDataByte;
2772 ucDisplayByte = 0;
2773 }
2774
2775 cByteIndex = 0;
2776 }
2777 }
2778
2779 if (ucDisplayFlag) {
2780
2781#ifdef DEBUG
2782 debug("RECEIVED TDO (");
2783#else
2784 vme_out_string("Display Data: 0x");
2785#endif
2786
2787
2788 for (usDataSizeIndex = (unsigned short)
2789 ((a_usiDataSize + 7) / 8);
2790 usDataSizeIndex > 0 ; usDataSizeIndex--) {
2791 cMaskByte = g_pucOutData[usDataSizeIndex - 1];
2792 cDataByte = 0x00;
2793
2794
2795
2796
2797
2798 for (usBufferIndex = 0; usBufferIndex < 8;
2799 usBufferIndex++) {
2800 cDataByte <<= 1;
2801 if (cMaskByte & 0x01) {
2802 cDataByte |= 0x01;
2803 }
2804 cMaskByte >>= 1;
2805 }
2806#ifdef DEBUG
2807 printf("%.2X", cDataByte);
2808 if ((((a_usiDataSize + 7) / 8) - usDataSizeIndex)
2809 % 40 == 39) {
2810 printf("\n\t\t");
2811 }
2812#else
2813 vme_out_hex(cDataByte);
2814#endif
2815 }
2816
2817#ifdef DEBUG
2818 printf(")\n\n");
2819#else
2820 vme_out_string("\n\n");
2821#endif
2822
2823 if (g_usChecksum != 0) {
2824 g_usChecksum &= 0xFFFF;
2825 sprintf(StrChecksum, "Data Checksum: %.4lX\n\n",
2826 g_usChecksum);
2827 vme_out_string(StrChecksum);
2828 g_usChecksum = 0;
2829 }
2830 }
2831
2832 if (usErrorCount > 0) {
2833 if (g_usFlowControl & VERIFYUES) {
2834 vme_out_string(
2835 "USERCODE verification failed. "
2836 "Continue programming......\n\n");
2837 g_usFlowControl &= ~(VERIFYUES);
2838 return 0;
2839 } else {
2840
2841#ifdef DEBUG
2842 printf("TOTAL ERRORS: %d\n", usErrorCount);
2843#endif
2844
2845 return VME_VERIFICATION_FAILURE;
2846 }
2847 } else {
2848 if (g_usFlowControl & VERIFYUES) {
2849 vme_out_string("USERCODE verification passed. "
2850 "Programming aborted.\n\n");
2851 g_usFlowControl &= ~(VERIFYUES);
2852 return 1;
2853 } else {
2854 return 0;
2855 }
2856 }
2857}
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867signed char ispVMReadandSave(unsigned short int a_usiDataSize)
2868{
2869
2870 unsigned short int usDataSizeIndex = 0;
2871 unsigned short int usLastBitIndex = 0;
2872 unsigned short int usBufferIndex = 0;
2873 unsigned short int usOutBitIndex = 0;
2874 unsigned short int usLVDSIndex = 0;
2875 unsigned char cDataByte = 0;
2876 unsigned char cDMASKByte = 0;
2877 unsigned char cInDataByte = 0;
2878 unsigned char cCurBit = 0;
2879 unsigned char cByteIndex = 0;
2880 signed char cLVDSByteIndex = 0;
2881
2882
2883 usLastBitIndex = (unsigned short) (a_usiDataSize - 1);
2884
2885
2886
2887
2888
2889
2890
2891 for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
2892 usDataSizeIndex++) {
2893 if (cByteIndex == 0) {
2894
2895
2896
2897
2898
2899 if (g_usDataType & DMASK_DATA) {
2900 cDMASKByte = g_pucOutDMaskData[usBufferIndex];
2901 } else {
2902 cDMASKByte = 0x00;
2903 }
2904
2905
2906
2907
2908
2909 if (g_usDataType & TDI_DATA) {
2910 cInDataByte = g_pucInData[usBufferIndex];
2911 }
2912
2913 usBufferIndex++;
2914 }
2915
2916 cCurBit = readPort();
2917 cDataByte = (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
2918 ? 0x01 : 0x00);
2919
2920
2921
2922
2923
2924 if (usOutBitIndex % 8 == 0) {
2925 g_pucOutData[usOutBitIndex / 8] = 0x00;
2926 }
2927
2928
2929
2930
2931
2932
2933 if ((((cDMASKByte << cByteIndex) & 0x80) ? 0x01 : 0x00)) {
2934
2935 if (g_pLVDSList) {
2936 for (usLVDSIndex = 0;
2937 usLVDSIndex < g_usLVDSPairCount;
2938 usLVDSIndex++) {
2939 if (g_pLVDSList[usLVDSIndex].
2940 usNegativeIndex ==
2941 usDataSizeIndex) {
2942 g_pLVDSList[usLVDSIndex].
2943 ucUpdate = 0x01;
2944 break;
2945 }
2946 }
2947 }
2948
2949
2950
2951
2952
2953 g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
2954 (((cDataByte & 0x1) ? 0x01 : 0x00) <<
2955 (7 - usOutBitIndex % 8));
2956 } else {
2957
2958
2959
2960
2961
2962 g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
2963 (((cCurBit & 0x1) ? 0x01 : 0x00) <<
2964 (7 - usOutBitIndex % 8));
2965 }
2966
2967
2968
2969
2970
2971 usOutBitIndex++;
2972 writePort(g_ucPinTDI, cDataByte);
2973 if (usDataSizeIndex < usLastBitIndex) {
2974 sclock();
2975 }
2976
2977
2978
2979
2980
2981
2982 cByteIndex++;
2983 if (cByteIndex >= 8) {
2984 cByteIndex = 0;
2985 }
2986 }
2987
2988
2989
2990
2991
2992
2993 if (g_pLVDSList) {
2994 for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount;
2995 usLVDSIndex++) {
2996 if (g_pLVDSList[usLVDSIndex].ucUpdate) {
2997
2998
2999
3000
3001
3002 cDataByte = (unsigned char)
3003 (((g_pucOutData[g_pLVDSList[usLVDSIndex].
3004 usPositiveIndex / 8]
3005 << (g_pLVDSList[usLVDSIndex].
3006 usPositiveIndex % 8)) & 0x80) ?
3007 0x01 : 0x00);
3008
3009 cDataByte = (unsigned char) (!cDataByte);
3010
3011
3012
3013
3014
3015 cInDataByte =
3016 g_pucOutData[g_pLVDSList[usLVDSIndex].
3017 usNegativeIndex / 8];
3018
3019 if (cDataByte) {
3020
3021
3022
3023
3024
3025
3026 cDataByte = 0x00;
3027 for (cLVDSByteIndex = 7;
3028 cLVDSByteIndex >= 0;
3029 cLVDSByteIndex--) {
3030 cDataByte <<= 1;
3031 if (7 -
3032 (g_pLVDSList[usLVDSIndex].
3033 usNegativeIndex % 8) ==
3034 cLVDSByteIndex) {
3035
3036
3037
3038
3039
3040 cDataByte |= 0x01;
3041 } else if (cInDataByte & 0x80) {
3042 cDataByte |= 0x01;
3043 }
3044
3045 cInDataByte <<= 1;
3046 }
3047
3048
3049
3050
3051
3052 g_pucOutData[g_pLVDSList[usLVDSIndex].
3053 usNegativeIndex / 8] = cDataByte;
3054 } else {
3055
3056
3057
3058
3059
3060
3061 cDataByte = 0x00;
3062 for (cLVDSByteIndex = 7;
3063 cLVDSByteIndex >= 0;
3064 cLVDSByteIndex--) {
3065 cDataByte <<= 1;
3066 if (7 -
3067 (g_pLVDSList[usLVDSIndex].
3068 usNegativeIndex % 8) ==
3069 cLVDSByteIndex) {
3070
3071
3072
3073
3074
3075 cDataByte |= 0x00;
3076 } else if (cInDataByte & 0x80) {
3077 cDataByte |= 0x01;
3078 }
3079
3080 cInDataByte <<= 1;
3081 }
3082
3083
3084
3085
3086
3087 g_pucOutData[g_pLVDSList[usLVDSIndex].
3088 usNegativeIndex / 8] = cDataByte;
3089 }
3090
3091 break;
3092 }
3093 }
3094 }
3095
3096 return 0;
3097}
3098
3099signed char ispVMProcessLVDS(unsigned short a_usLVDSCount)
3100{
3101 unsigned short usLVDSIndex = 0;
3102
3103
3104
3105
3106
3107 ispVMMemManager(LVDS, a_usLVDSCount);
3108 g_usLVDSPairCount = a_usLVDSCount;
3109
3110#ifdef DEBUG
3111 printf("LVDS %d (", a_usLVDSCount);
3112#endif
3113
3114
3115
3116
3117
3118 for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount; usLVDSIndex++) {
3119
3120
3121
3122
3123
3124
3125 g_pLVDSList[usLVDSIndex].usPositiveIndex =
3126 (unsigned short) ispVMDataSize();
3127
3128 g_pLVDSList[usLVDSIndex].usNegativeIndex =
3129 (unsigned short)ispVMDataSize();
3130
3131#ifdef DEBUG
3132 if (usLVDSIndex < g_usLVDSPairCount - 1) {
3133 printf("%d:%d, ",
3134 g_pLVDSList[usLVDSIndex].usPositiveIndex,
3135 g_pLVDSList[usLVDSIndex].usNegativeIndex);
3136 } else {
3137 printf("%d:%d",
3138 g_pLVDSList[usLVDSIndex].usPositiveIndex,
3139 g_pLVDSList[usLVDSIndex].usNegativeIndex);
3140 }
3141#endif
3142
3143 }
3144
3145#ifdef DEBUG
3146 printf(");\n");
3147#endif
3148
3149 return 0;
3150}
3151