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