ivm_core.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Porting to u-boot:
  4. *
  5. * (C) Copyright 2010
  6. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  7. *
  8. * Lattice ispVME Embedded code to load Lattice's FPGA:
  9. *
  10. * Copyright 2009 Lattice Semiconductor Corp.
  11. *
  12. * ispVME Embedded allows programming of Lattice's suite of FPGA
  13. * devices on embedded systems through the JTAG port. The software
  14. * is distributed in source code form and is open to re - distribution
  15. * and modification where applicable.
  16. *
  17. * Revision History of ivm_core.c module:
  18. * 4/25/06 ht Change some variables from unsigned short or int
  19. * to long int to make the code compiler independent.
  20. * 5/24/06 ht Support using RESET (TRST) pin as a special purpose
  21. * control pin such as triggering the loading of known
  22. * state exit.
  23. * 3/6/07 ht added functions to support output to terminals
  24. *
  25. * 09/11/07 NN Type cast mismatch variables
  26. * Moved the sclock() function to hardware.c
  27. * 08/28/08 NN Added Calculate checksum support.
  28. * 4/1/09 Nguyen replaced the recursive function call codes on
  29. * the ispVMLCOUNT function
  30. */
  31. #include <common.h>
  32. #include <log.h>
  33. #include <linux/string.h>
  34. #include <malloc.h>
  35. #include <lattice.h>
  36. #define vme_out_char(c) printf("%c", c)
  37. #define vme_out_hex(c) printf("%x", c)
  38. #define vme_out_string(s) printf("%s", s)
  39. /*
  40. *
  41. * Global variables used to specify the flow control and data type.
  42. *
  43. * g_usFlowControl: flow control register. Each bit in the
  44. * register can potentially change the
  45. * personality of the embedded engine.
  46. * g_usDataType: holds the data type of the current row.
  47. *
  48. */
  49. static unsigned short g_usFlowControl;
  50. unsigned short g_usDataType;
  51. /*
  52. *
  53. * Global variables used to specify the ENDDR and ENDIR.
  54. *
  55. * g_ucEndDR: the state that the device goes to after SDR.
  56. * g_ucEndIR: the state that the device goes to after SIR.
  57. *
  58. */
  59. unsigned char g_ucEndDR = DRPAUSE;
  60. unsigned char g_ucEndIR = IRPAUSE;
  61. /*
  62. *
  63. * Global variables used to support header/trailer.
  64. *
  65. * g_usHeadDR: the number of lead devices in bypass.
  66. * g_usHeadIR: the sum of IR length of lead devices.
  67. * g_usTailDR: the number of tail devices in bypass.
  68. * g_usTailIR: the sum of IR length of tail devices.
  69. *
  70. */
  71. static unsigned short g_usHeadDR;
  72. static unsigned short g_usHeadIR;
  73. static unsigned short g_usTailDR;
  74. static unsigned short g_usTailIR;
  75. /*
  76. *
  77. * Global variable to store the number of bits of data or instruction
  78. * to be shifted into or out from the device.
  79. *
  80. */
  81. static unsigned short g_usiDataSize;
  82. /*
  83. *
  84. * Stores the frequency. Default to 1 MHz.
  85. *
  86. */
  87. static int g_iFrequency = 1000;
  88. /*
  89. *
  90. * Stores the maximum amount of ram needed to hold a row of data.
  91. *
  92. */
  93. static unsigned short g_usMaxSize;
  94. /*
  95. *
  96. * Stores the LSH or RSH value.
  97. *
  98. */
  99. static unsigned short g_usShiftValue;
  100. /*
  101. *
  102. * Stores the current repeat loop value.
  103. *
  104. */
  105. static unsigned short g_usRepeatLoops;
  106. /*
  107. *
  108. * Stores the current vendor.
  109. *
  110. */
  111. static signed char g_cVendor = LATTICE;
  112. /*
  113. *
  114. * Stores the VME file CRC.
  115. *
  116. */
  117. unsigned short g_usCalculatedCRC;
  118. /*
  119. *
  120. * Stores the Device Checksum.
  121. *
  122. */
  123. /* 08/28/08 NN Added Calculate checksum support. */
  124. unsigned long g_usChecksum;
  125. static unsigned int g_uiChecksumIndex;
  126. /*
  127. *
  128. * Stores the current state of the JTAG state machine.
  129. *
  130. */
  131. static signed char g_cCurrentJTAGState;
  132. /*
  133. *
  134. * Global variables used to support looping.
  135. *
  136. * g_pucHeapMemory: holds the entire repeat loop.
  137. * g_iHeapCounter: points to the current byte in the repeat loop.
  138. * g_iHEAPSize: the current size of the repeat in bytes.
  139. *
  140. */
  141. unsigned char *g_pucHeapMemory;
  142. unsigned short g_iHeapCounter;
  143. unsigned short g_iHEAPSize;
  144. static unsigned short previous_size;
  145. /*
  146. *
  147. * Global variables used to support intelligent programming.
  148. *
  149. * g_usIntelDataIndex: points to the current byte of the
  150. * intelligent buffer.
  151. * g_usIntelBufferSize: holds the size of the intelligent
  152. * buffer.
  153. *
  154. */
  155. unsigned short g_usIntelDataIndex;
  156. unsigned short g_usIntelBufferSize;
  157. /*
  158. *
  159. * Supported VME versions.
  160. *
  161. */
  162. const char *const g_szSupportedVersions[] = {
  163. "__VME2.0", "__VME3.0", "____12.0", "____12.1", 0};
  164. /*
  165. *
  166. * Holds the maximum size of each respective buffer. These variables are used
  167. * to write the HEX files when converting VME to HEX.
  168. *
  169. */
  170. static unsigned short g_usTDOSize;
  171. static unsigned short g_usMASKSize;
  172. static unsigned short g_usTDISize;
  173. static unsigned short g_usDMASKSize;
  174. static unsigned short g_usLCOUNTSize;
  175. static unsigned short g_usHDRSize;
  176. static unsigned short g_usTDRSize;
  177. static unsigned short g_usHIRSize;
  178. static unsigned short g_usTIRSize;
  179. static unsigned short g_usHeapSize;
  180. /*
  181. *
  182. * Global variables used to store data.
  183. *
  184. * g_pucOutMaskData: local RAM to hold one row of MASK data.
  185. * g_pucInData: local RAM to hold one row of TDI data.
  186. * g_pucOutData: local RAM to hold one row of TDO data.
  187. * g_pucHIRData: local RAM to hold the current SIR header.
  188. * g_pucTIRData: local RAM to hold the current SIR trailer.
  189. * g_pucHDRData: local RAM to hold the current SDR header.
  190. * g_pucTDRData: local RAM to hold the current SDR trailer.
  191. * g_pucIntelBuffer: local RAM to hold the current intelligent buffer
  192. * g_pucOutDMaskData: local RAM to hold one row of DMASK data.
  193. *
  194. */
  195. unsigned char *g_pucOutMaskData = NULL,
  196. *g_pucInData = NULL,
  197. *g_pucOutData = NULL,
  198. *g_pucHIRData = NULL,
  199. *g_pucTIRData = NULL,
  200. *g_pucHDRData = NULL,
  201. *g_pucTDRData = NULL,
  202. *g_pucIntelBuffer = NULL,
  203. *g_pucOutDMaskData = NULL;
  204. /*
  205. *
  206. * JTAG state machine transition table.
  207. *
  208. */
  209. struct {
  210. unsigned char CurState; /* From this state */
  211. unsigned char NextState; /* Step to this state */
  212. unsigned char Pattern; /* The tragetory of TMS */
  213. unsigned char Pulses; /* The number of steps */
  214. } g_JTAGTransistions[25] = {
  215. { RESET, RESET, 0xFC, 6 }, /* Transitions from RESET */
  216. { RESET, IDLE, 0x00, 1 },
  217. { RESET, DRPAUSE, 0x50, 5 },
  218. { RESET, IRPAUSE, 0x68, 6 },
  219. { IDLE, RESET, 0xE0, 3 }, /* Transitions from IDLE */
  220. { IDLE, DRPAUSE, 0xA0, 4 },
  221. { IDLE, IRPAUSE, 0xD0, 5 },
  222. { DRPAUSE, RESET, 0xF8, 5 }, /* Transitions from DRPAUSE */
  223. { DRPAUSE, IDLE, 0xC0, 3 },
  224. { DRPAUSE, IRPAUSE, 0xF4, 7 },
  225. { DRPAUSE, DRPAUSE, 0xE8, 6 },/* 06/14/06 Support POLL STATUS LOOP*/
  226. { IRPAUSE, RESET, 0xF8, 5 }, /* Transitions from IRPAUSE */
  227. { IRPAUSE, IDLE, 0xC0, 3 },
  228. { IRPAUSE, DRPAUSE, 0xE8, 6 },
  229. { DRPAUSE, SHIFTDR, 0x80, 2 }, /* Extra transitions using SHIFTDR */
  230. { IRPAUSE, SHIFTDR, 0xE0, 5 },
  231. { SHIFTDR, DRPAUSE, 0x80, 2 },
  232. { SHIFTDR, IDLE, 0xC0, 3 },
  233. { IRPAUSE, SHIFTIR, 0x80, 2 },/* Extra transitions using SHIFTIR */
  234. { SHIFTIR, IRPAUSE, 0x80, 2 },
  235. { SHIFTIR, IDLE, 0xC0, 3 },
  236. { DRPAUSE, DRCAPTURE, 0xE0, 4 }, /* 11/15/05 Support DRCAPTURE*/
  237. { DRCAPTURE, DRPAUSE, 0x80, 2 },
  238. { IDLE, DRCAPTURE, 0x80, 2 },
  239. { IRPAUSE, DRCAPTURE, 0xE0, 4 }
  240. };
  241. /*
  242. *
  243. * List to hold all LVDS pairs.
  244. *
  245. */
  246. LVDSPair *g_pLVDSList;
  247. unsigned short g_usLVDSPairCount;
  248. /*
  249. *
  250. * Function prototypes.
  251. *
  252. */
  253. static signed char ispVMDataCode(void);
  254. static long int ispVMDataSize(void);
  255. static void ispVMData(unsigned char *Data);
  256. static signed char ispVMShift(signed char Code);
  257. static signed char ispVMAmble(signed char Code);
  258. static signed char ispVMLoop(unsigned short a_usLoopCount);
  259. static signed char ispVMBitShift(signed char mode, unsigned short bits);
  260. static void ispVMComment(unsigned short a_usCommentSize);
  261. static void ispVMHeader(unsigned short a_usHeaderSize);
  262. static signed char ispVMLCOUNT(unsigned short a_usCountSize);
  263. static void ispVMClocks(unsigned short Clocks);
  264. static void ispVMBypass(signed char ScanType, unsigned short Bits);
  265. static void ispVMStateMachine(signed char NextState);
  266. static signed char ispVMSend(unsigned short int);
  267. static signed char ispVMRead(unsigned short int);
  268. static signed char ispVMReadandSave(unsigned short int);
  269. static signed char ispVMProcessLVDS(unsigned short a_usLVDSCount);
  270. static void ispVMMemManager(signed char types, unsigned short size);
  271. /*
  272. *
  273. * External variables and functions in hardware.c module
  274. *
  275. */
  276. static signed char g_cCurrentJTAGState;
  277. #ifdef DEBUG
  278. /*
  279. *
  280. * GetState
  281. *
  282. * Returns the state as a string based on the opcode. Only used
  283. * for debugging purposes.
  284. *
  285. */
  286. const char *GetState(unsigned char a_ucState)
  287. {
  288. switch (a_ucState) {
  289. case RESET:
  290. return "RESET";
  291. case IDLE:
  292. return "IDLE";
  293. case IRPAUSE:
  294. return "IRPAUSE";
  295. case DRPAUSE:
  296. return "DRPAUSE";
  297. case SHIFTIR:
  298. return "SHIFTIR";
  299. case SHIFTDR:
  300. return "SHIFTDR";
  301. case DRCAPTURE:/* 11/15/05 support DRCAPTURE*/
  302. return "DRCAPTURE";
  303. default:
  304. break;
  305. }
  306. return 0;
  307. }
  308. /*
  309. *
  310. * PrintData
  311. *
  312. * Prints the data. Only used for debugging purposes.
  313. *
  314. */
  315. void PrintData(unsigned short a_iDataSize, unsigned char *a_pucData)
  316. {
  317. /* 09/11/07 NN added local variables initialization */
  318. unsigned short usByteSize = 0;
  319. unsigned short usBitIndex = 0;
  320. signed short usByteIndex = 0;
  321. unsigned char ucByte = 0;
  322. unsigned char ucFlipByte = 0;
  323. if (a_iDataSize % 8) {
  324. /* 09/11/07 NN Type cast mismatch variables */
  325. usByteSize = (unsigned short)(a_iDataSize / 8 + 1);
  326. } else {
  327. /* 09/11/07 NN Type cast mismatch variables */
  328. usByteSize = (unsigned short)(a_iDataSize / 8);
  329. }
  330. puts("(");
  331. /* 09/11/07 NN Type cast mismatch variables */
  332. for (usByteIndex = (signed short)(usByteSize - 1);
  333. usByteIndex >= 0; usByteIndex--) {
  334. ucByte = a_pucData[usByteIndex];
  335. ucFlipByte = 0x00;
  336. /*
  337. *
  338. * Flip each byte.
  339. *
  340. */
  341. for (usBitIndex = 0; usBitIndex < 8; usBitIndex++) {
  342. ucFlipByte <<= 1;
  343. if (ucByte & 0x1) {
  344. ucFlipByte |= 0x1;
  345. }
  346. ucByte >>= 1;
  347. }
  348. /*
  349. *
  350. * Print the flipped byte.
  351. *
  352. */
  353. printf("%.02X", ucFlipByte);
  354. if ((usByteSize - usByteIndex) % 40 == 39) {
  355. puts("\n\t\t");
  356. }
  357. if (usByteIndex < 0)
  358. break;
  359. }
  360. puts(")");
  361. }
  362. #endif /* DEBUG */
  363. void ispVMMemManager(signed char cTarget, unsigned short usSize)
  364. {
  365. switch (cTarget) {
  366. case XTDI:
  367. case TDI:
  368. if (g_pucInData != NULL) {
  369. if (previous_size == usSize) {/*memory exist*/
  370. break;
  371. } else {
  372. free(g_pucInData);
  373. g_pucInData = NULL;
  374. }
  375. }
  376. g_pucInData = (unsigned char *) malloc(usSize / 8 + 2);
  377. previous_size = usSize;
  378. case XTDO:
  379. case TDO:
  380. if (g_pucOutData != NULL) {
  381. if (previous_size == usSize) { /*already exist*/
  382. break;
  383. } else {
  384. free(g_pucOutData);
  385. g_pucOutData = NULL;
  386. }
  387. }
  388. g_pucOutData = (unsigned char *) malloc(usSize / 8 + 2);
  389. previous_size = usSize;
  390. break;
  391. case MASK:
  392. if (g_pucOutMaskData != NULL) {
  393. if (previous_size == usSize) {/*already allocated*/
  394. break;
  395. } else {
  396. free(g_pucOutMaskData);
  397. g_pucOutMaskData = NULL;
  398. }
  399. }
  400. g_pucOutMaskData = (unsigned char *) malloc(usSize / 8 + 2);
  401. previous_size = usSize;
  402. break;
  403. case HIR:
  404. if (g_pucHIRData != NULL) {
  405. free(g_pucHIRData);
  406. g_pucHIRData = NULL;
  407. }
  408. g_pucHIRData = (unsigned char *) malloc(usSize / 8 + 2);
  409. break;
  410. case TIR:
  411. if (g_pucTIRData != NULL) {
  412. free(g_pucTIRData);
  413. g_pucTIRData = NULL;
  414. }
  415. g_pucTIRData = (unsigned char *) malloc(usSize / 8 + 2);
  416. break;
  417. case HDR:
  418. if (g_pucHDRData != NULL) {
  419. free(g_pucHDRData);
  420. g_pucHDRData = NULL;
  421. }
  422. g_pucHDRData = (unsigned char *) malloc(usSize / 8 + 2);
  423. break;
  424. case TDR:
  425. if (g_pucTDRData != NULL) {
  426. free(g_pucTDRData);
  427. g_pucTDRData = NULL;
  428. }
  429. g_pucTDRData = (unsigned char *) malloc(usSize / 8 + 2);
  430. break;
  431. case HEAP:
  432. if (g_pucHeapMemory != NULL) {
  433. free(g_pucHeapMemory);
  434. g_pucHeapMemory = NULL;
  435. }
  436. g_pucHeapMemory = (unsigned char *) malloc(usSize + 2);
  437. break;
  438. case DMASK:
  439. if (g_pucOutDMaskData != NULL) {
  440. if (previous_size == usSize) { /*already allocated*/
  441. break;
  442. } else {
  443. free(g_pucOutDMaskData);
  444. g_pucOutDMaskData = NULL;
  445. }
  446. }
  447. g_pucOutDMaskData = (unsigned char *) malloc(usSize / 8 + 2);
  448. previous_size = usSize;
  449. break;
  450. case LHEAP:
  451. if (g_pucIntelBuffer != NULL) {
  452. free(g_pucIntelBuffer);
  453. g_pucIntelBuffer = NULL;
  454. }
  455. g_pucIntelBuffer = (unsigned char *) malloc(usSize + 2);
  456. break;
  457. case LVDS:
  458. if (g_pLVDSList != NULL) {
  459. free(g_pLVDSList);
  460. g_pLVDSList = NULL;
  461. }
  462. g_pLVDSList = (LVDSPair *) malloc(usSize * sizeof(LVDSPair));
  463. if (g_pLVDSList)
  464. memset(g_pLVDSList, 0, usSize * sizeof(LVDSPair));
  465. break;
  466. default:
  467. return;
  468. }
  469. }
  470. void ispVMFreeMem(void)
  471. {
  472. if (g_pucHeapMemory != NULL) {
  473. free(g_pucHeapMemory);
  474. g_pucHeapMemory = NULL;
  475. }
  476. if (g_pucOutMaskData != NULL) {
  477. free(g_pucOutMaskData);
  478. g_pucOutMaskData = NULL;
  479. }
  480. if (g_pucInData != NULL) {
  481. free(g_pucInData);
  482. g_pucInData = NULL;
  483. }
  484. if (g_pucOutData != NULL) {
  485. free(g_pucOutData);
  486. g_pucOutData = NULL;
  487. }
  488. if (g_pucHIRData != NULL) {
  489. free(g_pucHIRData);
  490. g_pucHIRData = NULL;
  491. }
  492. if (g_pucTIRData != NULL) {
  493. free(g_pucTIRData);
  494. g_pucTIRData = NULL;
  495. }
  496. if (g_pucHDRData != NULL) {
  497. free(g_pucHDRData);
  498. g_pucHDRData = NULL;
  499. }
  500. if (g_pucTDRData != NULL) {
  501. free(g_pucTDRData);
  502. g_pucTDRData = NULL;
  503. }
  504. if (g_pucOutDMaskData != NULL) {
  505. free(g_pucOutDMaskData);
  506. g_pucOutDMaskData = NULL;
  507. }
  508. if (g_pucIntelBuffer != NULL) {
  509. free(g_pucIntelBuffer);
  510. g_pucIntelBuffer = NULL;
  511. }
  512. if (g_pLVDSList != NULL) {
  513. free(g_pLVDSList);
  514. g_pLVDSList = NULL;
  515. }
  516. }
  517. /*
  518. *
  519. * ispVMDataSize
  520. *
  521. * Returns a VME-encoded number, usually used to indicate the
  522. * bit length of an SIR/SDR command.
  523. *
  524. */
  525. long int ispVMDataSize()
  526. {
  527. /* 09/11/07 NN added local variables initialization */
  528. long int iSize = 0;
  529. signed char cCurrentByte = 0;
  530. signed char cIndex = 0;
  531. cIndex = 0;
  532. while ((cCurrentByte = GetByte()) & 0x80) {
  533. iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
  534. cIndex += 7;
  535. }
  536. iSize |= ((long int) (cCurrentByte & 0x7F)) << cIndex;
  537. return iSize;
  538. }
  539. /*
  540. *
  541. * ispVMCode
  542. *
  543. * This is the heart of the embedded engine. All the high-level opcodes
  544. * are extracted here. Once they have been identified, then it
  545. * will call other functions to handle the processing.
  546. *
  547. */
  548. signed char ispVMCode()
  549. {
  550. /* 09/11/07 NN added local variables initialization */
  551. unsigned short iRepeatSize = 0;
  552. signed char cOpcode = 0;
  553. signed char cRetCode = 0;
  554. unsigned char ucState = 0;
  555. unsigned short usDelay = 0;
  556. unsigned short usToggle = 0;
  557. unsigned char usByte = 0;
  558. /*
  559. *
  560. * Check the compression flag only if this is the first time
  561. * this function is entered. Do not check the compression flag if
  562. * it is being called recursively from other functions within
  563. * the embedded engine.
  564. *
  565. */
  566. if (!(g_usDataType & LHEAP_IN) && !(g_usDataType & HEAP_IN)) {
  567. usByte = GetByte();
  568. if (usByte == 0xf1) {
  569. g_usDataType |= COMPRESS;
  570. } else if (usByte == 0xf2) {
  571. g_usDataType &= ~COMPRESS;
  572. } else {
  573. return VME_INVALID_FILE;
  574. }
  575. }
  576. /*
  577. *
  578. * Begin looping through all the VME opcodes.
  579. *
  580. */
  581. while ((cOpcode = GetByte()) >= 0) {
  582. switch (cOpcode) {
  583. case STATE:
  584. /*
  585. * Step the JTAG state machine.
  586. */
  587. ucState = GetByte();
  588. /*
  589. * Step the JTAG state machine to DRCAPTURE
  590. * to support Looping.
  591. */
  592. if ((g_usDataType & LHEAP_IN) &&
  593. (ucState == DRPAUSE) &&
  594. (g_cCurrentJTAGState == ucState)) {
  595. ispVMStateMachine(DRCAPTURE);
  596. }
  597. ispVMStateMachine(ucState);
  598. #ifdef DEBUG
  599. if (g_usDataType & LHEAP_IN) {
  600. debug("LDELAY %s ", GetState(ucState));
  601. } else {
  602. debug("STATE %s;\n", GetState(ucState));
  603. }
  604. #endif /* DEBUG */
  605. break;
  606. case SIR:
  607. case SDR:
  608. case XSDR:
  609. #ifdef DEBUG
  610. switch (cOpcode) {
  611. case SIR:
  612. puts("SIR ");
  613. break;
  614. case SDR:
  615. case XSDR:
  616. if (g_usDataType & LHEAP_IN) {
  617. puts("LSDR ");
  618. } else {
  619. puts("SDR ");
  620. }
  621. break;
  622. }
  623. #endif /* DEBUG */
  624. /*
  625. *
  626. * Shift in data into the device.
  627. *
  628. */
  629. cRetCode = ispVMShift(cOpcode);
  630. if (cRetCode != 0) {
  631. return cRetCode;
  632. }
  633. break;
  634. case WAIT:
  635. /*
  636. *
  637. * Observe delay.
  638. *
  639. */
  640. /* 09/11/07 NN Type cast mismatch variables */
  641. usDelay = (unsigned short) ispVMDataSize();
  642. ispVMDelay(usDelay);
  643. #ifdef DEBUG
  644. if (usDelay & 0x8000) {
  645. /*
  646. * Since MSB is set, the delay time must be
  647. * decoded to millisecond. The SVF2VME encodes
  648. * the MSB to represent millisecond.
  649. */
  650. usDelay &= ~0x8000;
  651. if (g_usDataType & LHEAP_IN) {
  652. printf("%.2E SEC;\n",
  653. (float) usDelay / 1000);
  654. } else {
  655. printf("RUNTEST %.2E SEC;\n",
  656. (float) usDelay / 1000);
  657. }
  658. } else {
  659. /*
  660. * Since MSB is not set, the delay time
  661. * is given as microseconds.
  662. */
  663. if (g_usDataType & LHEAP_IN) {
  664. printf("%.2E SEC;\n",
  665. (float) usDelay / 1000000);
  666. } else {
  667. printf("RUNTEST %.2E SEC;\n",
  668. (float) usDelay / 1000000);
  669. }
  670. }
  671. #endif /* DEBUG */
  672. break;
  673. case TCK:
  674. /*
  675. * Issue clock toggles.
  676. */
  677. /* 09/11/07 NN Type cast mismatch variables */
  678. usToggle = (unsigned short) ispVMDataSize();
  679. ispVMClocks(usToggle);
  680. #ifdef DEBUG
  681. printf("RUNTEST %d TCK;\n", usToggle);
  682. #endif /* DEBUG */
  683. break;
  684. case ENDDR:
  685. /*
  686. *
  687. * Set the ENDDR.
  688. *
  689. */
  690. g_ucEndDR = GetByte();
  691. #ifdef DEBUG
  692. printf("ENDDR %s;\n", GetState(g_ucEndDR));
  693. #endif /* DEBUG */
  694. break;
  695. case ENDIR:
  696. /*
  697. *
  698. * Set the ENDIR.
  699. *
  700. */
  701. g_ucEndIR = GetByte();
  702. #ifdef DEBUG
  703. printf("ENDIR %s;\n", GetState(g_ucEndIR));
  704. #endif /* DEBUG */
  705. break;
  706. case HIR:
  707. case TIR:
  708. case HDR:
  709. case TDR:
  710. #ifdef DEBUG
  711. switch (cOpcode) {
  712. case HIR:
  713. puts("HIR ");
  714. break;
  715. case TIR:
  716. puts("TIR ");
  717. break;
  718. case HDR:
  719. puts("HDR ");
  720. break;
  721. case TDR:
  722. puts("TDR ");
  723. break;
  724. }
  725. #endif /* DEBUG */
  726. /*
  727. * Set the header/trailer of the device in order
  728. * to bypass
  729. * successfully.
  730. */
  731. cRetCode = ispVMAmble(cOpcode);
  732. if (cRetCode != 0) {
  733. return cRetCode;
  734. }
  735. #ifdef DEBUG
  736. puts(";\n");
  737. #endif /* DEBUG */
  738. break;
  739. case MEM:
  740. /*
  741. * The maximum RAM required to support
  742. * processing one row of the VME file.
  743. */
  744. /* 09/11/07 NN Type cast mismatch variables */
  745. g_usMaxSize = (unsigned short) ispVMDataSize();
  746. #ifdef DEBUG
  747. printf("// MEMSIZE %d\n", g_usMaxSize);
  748. #endif /* DEBUG */
  749. break;
  750. case VENDOR:
  751. /*
  752. *
  753. * Set the VENDOR type.
  754. *
  755. */
  756. cOpcode = GetByte();
  757. switch (cOpcode) {
  758. case LATTICE:
  759. #ifdef DEBUG
  760. puts("// VENDOR LATTICE\n");
  761. #endif /* DEBUG */
  762. g_cVendor = LATTICE;
  763. break;
  764. case ALTERA:
  765. #ifdef DEBUG
  766. puts("// VENDOR ALTERA\n");
  767. #endif /* DEBUG */
  768. g_cVendor = ALTERA;
  769. break;
  770. case XILINX:
  771. #ifdef DEBUG
  772. puts("// VENDOR XILINX\n");
  773. #endif /* DEBUG */
  774. g_cVendor = XILINX;
  775. break;
  776. default:
  777. break;
  778. }
  779. break;
  780. case SETFLOW:
  781. /*
  782. * Set the flow control. Flow control determines
  783. * the personality of the embedded engine.
  784. */
  785. /* 09/11/07 NN Type cast mismatch variables */
  786. g_usFlowControl |= (unsigned short) ispVMDataSize();
  787. break;
  788. case RESETFLOW:
  789. /*
  790. *
  791. * Unset the flow control.
  792. *
  793. */
  794. /* 09/11/07 NN Type cast mismatch variables */
  795. g_usFlowControl &= (unsigned short) ~(ispVMDataSize());
  796. break;
  797. case HEAP:
  798. /*
  799. *
  800. * Allocate heap size to store loops.
  801. *
  802. */
  803. cRetCode = GetByte();
  804. if (cRetCode != SECUREHEAP) {
  805. return VME_INVALID_FILE;
  806. }
  807. /* 09/11/07 NN Type cast mismatch variables */
  808. g_iHEAPSize = (unsigned short) ispVMDataSize();
  809. /*
  810. * Store the maximum size of the HEAP buffer.
  811. * Used to convert VME to HEX.
  812. */
  813. if (g_iHEAPSize > g_usHeapSize) {
  814. g_usHeapSize = g_iHEAPSize;
  815. }
  816. ispVMMemManager(HEAP, (unsigned short) g_iHEAPSize);
  817. break;
  818. case REPEAT:
  819. /*
  820. *
  821. * Execute loops.
  822. *
  823. */
  824. g_usRepeatLoops = 0;
  825. /* 09/11/07 NN Type cast mismatch variables */
  826. iRepeatSize = (unsigned short) ispVMDataSize();
  827. cRetCode = ispVMLoop((unsigned short) iRepeatSize);
  828. if (cRetCode != 0) {
  829. return cRetCode;
  830. }
  831. break;
  832. case ENDLOOP:
  833. /*
  834. *
  835. * Exit point from processing loops.
  836. *
  837. */
  838. return cRetCode;
  839. case ENDVME:
  840. /*
  841. * The only valid exit point that indicates
  842. * end of programming.
  843. */
  844. return cRetCode;
  845. case SHR:
  846. /*
  847. *
  848. * Right-shift address.
  849. *
  850. */
  851. g_usFlowControl |= SHIFTRIGHT;
  852. /* 09/11/07 NN Type cast mismatch variables */
  853. g_usShiftValue = (unsigned short) (g_usRepeatLoops *
  854. (unsigned short)GetByte());
  855. break;
  856. case SHL:
  857. /*
  858. * Left-shift address.
  859. */
  860. g_usFlowControl |= SHIFTLEFT;
  861. /* 09/11/07 NN Type cast mismatch variables */
  862. g_usShiftValue = (unsigned short) (g_usRepeatLoops *
  863. (unsigned short)GetByte());
  864. break;
  865. case FREQUENCY:
  866. /*
  867. *
  868. * Set the frequency.
  869. *
  870. */
  871. /* 09/11/07 NN Type cast mismatch variables */
  872. g_iFrequency = (int) (ispVMDataSize() / 1000);
  873. if (g_iFrequency == 1)
  874. g_iFrequency = 1000;
  875. #ifdef DEBUG
  876. printf("FREQUENCY %.2E HZ;\n",
  877. (float) g_iFrequency * 1000);
  878. #endif /* DEBUG */
  879. break;
  880. case LCOUNT:
  881. /*
  882. *
  883. * Process LCOUNT command.
  884. *
  885. */
  886. cRetCode = ispVMLCOUNT((unsigned short)ispVMDataSize());
  887. if (cRetCode != 0) {
  888. return cRetCode;
  889. }
  890. break;
  891. case VUES:
  892. /*
  893. *
  894. * Set the flow control to verify USERCODE.
  895. *
  896. */
  897. g_usFlowControl |= VERIFYUES;
  898. break;
  899. case COMMENT:
  900. /*
  901. *
  902. * Display comment.
  903. *
  904. */
  905. ispVMComment((unsigned short) ispVMDataSize());
  906. break;
  907. case LVDS:
  908. /*
  909. *
  910. * Process LVDS command.
  911. *
  912. */
  913. ispVMProcessLVDS((unsigned short) ispVMDataSize());
  914. break;
  915. case HEADER:
  916. /*
  917. *
  918. * Discard header.
  919. *
  920. */
  921. ispVMHeader((unsigned short) ispVMDataSize());
  922. break;
  923. /* 03/14/06 Support Toggle ispENABLE signal*/
  924. case ispEN:
  925. ucState = GetByte();
  926. if ((ucState == ON) || (ucState == 0x01))
  927. writePort(g_ucPinENABLE, 0x01);
  928. else
  929. writePort(g_ucPinENABLE, 0x00);
  930. ispVMDelay(1);
  931. break;
  932. /* 05/24/06 support Toggle TRST pin*/
  933. case TRST:
  934. ucState = GetByte();
  935. if (ucState == 0x01)
  936. writePort(g_ucPinTRST, 0x01);
  937. else
  938. writePort(g_ucPinTRST, 0x00);
  939. ispVMDelay(1);
  940. break;
  941. default:
  942. /*
  943. *
  944. * Invalid opcode encountered.
  945. *
  946. */
  947. #ifdef DEBUG
  948. printf("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
  949. #endif /* DEBUG */
  950. return VME_INVALID_FILE;
  951. }
  952. }
  953. /*
  954. *
  955. * Invalid exit point. Processing the token 'ENDVME' is the only
  956. * valid way to exit the embedded engine.
  957. *
  958. */
  959. return VME_INVALID_FILE;
  960. }
  961. /*
  962. *
  963. * ispVMDataCode
  964. *
  965. * Processes the TDI/TDO/MASK/DMASK etc of an SIR/SDR command.
  966. *
  967. */
  968. signed char ispVMDataCode()
  969. {
  970. /* 09/11/07 NN added local variables initialization */
  971. signed char cDataByte = 0;
  972. signed char siDataSource = 0; /*source of data from file by default*/
  973. if (g_usDataType & HEAP_IN) {
  974. siDataSource = 1; /*the source of data from memory*/
  975. }
  976. /*
  977. *
  978. * Clear the data type register.
  979. *
  980. **/
  981. g_usDataType &= ~(MASK_DATA + TDI_DATA +
  982. TDO_DATA + DMASK_DATA + CMASK_DATA);
  983. /*
  984. * Iterate through SIR/SDR command and look for TDI,
  985. * TDO, MASK, etc.
  986. */
  987. while ((cDataByte = GetByte()) >= 0) {
  988. ispVMMemManager(cDataByte, g_usMaxSize);
  989. switch (cDataByte) {
  990. case TDI:
  991. /*
  992. * Store the maximum size of the TDI buffer.
  993. * Used to convert VME to HEX.
  994. */
  995. if (g_usiDataSize > g_usTDISize) {
  996. g_usTDISize = g_usiDataSize;
  997. }
  998. /*
  999. * Updated data type register to indicate that
  1000. * TDI data is currently being used. Process the
  1001. * data in the VME file into the TDI buffer.
  1002. */
  1003. g_usDataType |= TDI_DATA;
  1004. ispVMData(g_pucInData);
  1005. break;
  1006. case XTDO:
  1007. /*
  1008. * Store the maximum size of the TDO buffer.
  1009. * Used to convert VME to HEX.
  1010. */
  1011. if (g_usiDataSize > g_usTDOSize) {
  1012. g_usTDOSize = g_usiDataSize;
  1013. }
  1014. /*
  1015. * Updated data type register to indicate that
  1016. * TDO data is currently being used.
  1017. */
  1018. g_usDataType |= TDO_DATA;
  1019. break;
  1020. case TDO:
  1021. /*
  1022. * Store the maximum size of the TDO buffer.
  1023. * Used to convert VME to HEX.
  1024. */
  1025. if (g_usiDataSize > g_usTDOSize) {
  1026. g_usTDOSize = g_usiDataSize;
  1027. }
  1028. /*
  1029. * Updated data type register to indicate
  1030. * that TDO data is currently being used.
  1031. * Process the data in the VME file into the
  1032. * TDO buffer.
  1033. */
  1034. g_usDataType |= TDO_DATA;
  1035. ispVMData(g_pucOutData);
  1036. break;
  1037. case MASK:
  1038. /*
  1039. * Store the maximum size of the MASK buffer.
  1040. * Used to convert VME to HEX.
  1041. */
  1042. if (g_usiDataSize > g_usMASKSize) {
  1043. g_usMASKSize = g_usiDataSize;
  1044. }
  1045. /*
  1046. * Updated data type register to indicate that
  1047. * MASK data is currently being used. Process
  1048. * the data in the VME file into the MASK buffer
  1049. */
  1050. g_usDataType |= MASK_DATA;
  1051. ispVMData(g_pucOutMaskData);
  1052. break;
  1053. case DMASK:
  1054. /*
  1055. * Store the maximum size of the DMASK buffer.
  1056. * Used to convert VME to HEX.
  1057. */
  1058. if (g_usiDataSize > g_usDMASKSize) {
  1059. g_usDMASKSize = g_usiDataSize;
  1060. }
  1061. /*
  1062. * Updated data type register to indicate that
  1063. * DMASK data is currently being used. Process
  1064. * the data in the VME file into the DMASK
  1065. * buffer.
  1066. */
  1067. g_usDataType |= DMASK_DATA;
  1068. ispVMData(g_pucOutDMaskData);
  1069. break;
  1070. case CMASK:
  1071. /*
  1072. * Updated data type register to indicate that
  1073. * MASK data is currently being used. Process
  1074. * the data in the VME file into the MASK buffer
  1075. */
  1076. g_usDataType |= CMASK_DATA;
  1077. ispVMData(g_pucOutMaskData);
  1078. break;
  1079. case CONTINUE:
  1080. return 0;
  1081. default:
  1082. /*
  1083. * Encountered invalid opcode.
  1084. */
  1085. return VME_INVALID_FILE;
  1086. }
  1087. switch (cDataByte) {
  1088. case TDI:
  1089. /*
  1090. * Left bit shift. Used when performing
  1091. * algorithm looping.
  1092. */
  1093. if (g_usFlowControl & SHIFTLEFT) {
  1094. ispVMBitShift(SHL, g_usShiftValue);
  1095. g_usFlowControl &= ~SHIFTLEFT;
  1096. }
  1097. /*
  1098. * Right bit shift. Used when performing
  1099. * algorithm looping.
  1100. */
  1101. if (g_usFlowControl & SHIFTRIGHT) {
  1102. ispVMBitShift(SHR, g_usShiftValue);
  1103. g_usFlowControl &= ~SHIFTRIGHT;
  1104. }
  1105. default:
  1106. break;
  1107. }
  1108. if (siDataSource) {
  1109. g_usDataType |= HEAP_IN; /*restore from memory*/
  1110. }
  1111. }
  1112. if (siDataSource) { /*fetch data from heap memory upon return*/
  1113. g_usDataType |= HEAP_IN;
  1114. }
  1115. if (cDataByte < 0) {
  1116. /*
  1117. * Encountered invalid opcode.
  1118. */
  1119. return VME_INVALID_FILE;
  1120. } else {
  1121. return 0;
  1122. }
  1123. }
  1124. /*
  1125. *
  1126. * ispVMData
  1127. * Extract one row of data operand from the current data type opcode. Perform
  1128. * the decompression if necessary. Extra RAM is not required for the
  1129. * decompression process. The decompression scheme employed in this module
  1130. * is on row by row basis. The format of the data stream:
  1131. * [compression code][compressed data stream]
  1132. * 0x00 --No compression
  1133. * 0x01 --Compress by 0x00.
  1134. * Example:
  1135. * Original stream: 0x000000000000000000000001
  1136. * Compressed stream: 0x01000901
  1137. * Detail: 0x01 is the code, 0x00 is the key,
  1138. * 0x09 is the count of 0x00 bytes,
  1139. * 0x01 is the uncompressed byte.
  1140. * 0x02 --Compress by 0xFF.
  1141. * Example:
  1142. * Original stream: 0xFFFFFFFFFFFFFFFFFFFFFF01
  1143. * Compressed stream: 0x02FF0901
  1144. * Detail: 0x02 is the code, 0xFF is the key,
  1145. * 0x09 is the count of 0xFF bytes,
  1146. * 0x01 is the uncompressed byte.
  1147. * 0x03
  1148. * : :
  1149. * 0xFE -- Compress by nibble blocks.
  1150. * Example:
  1151. * Original stream: 0x84210842108421084210
  1152. * Compressed stream: 0x0584210
  1153. * Detail: 0x05 is the code, means 5 nibbles block.
  1154. * 0x84210 is the 5 nibble blocks.
  1155. * The whole row is 80 bits given by g_usiDataSize.
  1156. * The number of times the block repeat itself
  1157. * is found by g_usiDataSize/(4*0x05) which is 4.
  1158. * 0xFF -- Compress by the most frequently happen byte.
  1159. * Example:
  1160. * Original stream: 0x04020401030904040404
  1161. * Compressed stream: 0xFF04(0,1,0x02,0,1,0x01,1,0x03,1,0x09,0,0,0)
  1162. * or: 0xFF044090181C240
  1163. * Detail: 0xFF is the code, 0x04 is the key.
  1164. * a bit of 0 represent the key shall be put into
  1165. * the current bit position and a bit of 1
  1166. * represent copying the next of 8 bits of data
  1167. * in.
  1168. *
  1169. */
  1170. void ispVMData(unsigned char *ByteData)
  1171. {
  1172. /* 09/11/07 NN added local variables initialization */
  1173. unsigned short size = 0;
  1174. unsigned short i, j, m, getData = 0;
  1175. unsigned char cDataByte = 0;
  1176. unsigned char compress = 0;
  1177. unsigned short FFcount = 0;
  1178. unsigned char compr_char = 0xFF;
  1179. unsigned short index = 0;
  1180. signed char compression = 0;
  1181. /*convert number in bits to bytes*/
  1182. if (g_usiDataSize % 8 > 0) {
  1183. /* 09/11/07 NN Type cast mismatch variables */
  1184. size = (unsigned short)(g_usiDataSize / 8 + 1);
  1185. } else {
  1186. /* 09/11/07 NN Type cast mismatch variables */
  1187. size = (unsigned short)(g_usiDataSize / 8);
  1188. }
  1189. /*
  1190. * If there is compression, then check if compress by key
  1191. * of 0x00 or 0xFF or by other keys or by nibble blocks
  1192. */
  1193. if (g_usDataType & COMPRESS) {
  1194. compression = 1;
  1195. compress = GetByte();
  1196. if ((compress == VAR) && (g_usDataType & HEAP_IN)) {
  1197. getData = 1;
  1198. g_usDataType &= ~(HEAP_IN);
  1199. compress = GetByte();
  1200. }
  1201. switch (compress) {
  1202. case 0x00:
  1203. /* No compression */
  1204. compression = 0;
  1205. break;
  1206. case 0x01:
  1207. /* Compress by byte 0x00 */
  1208. compr_char = 0x00;
  1209. break;
  1210. case 0x02:
  1211. /* Compress by byte 0xFF */
  1212. compr_char = 0xFF;
  1213. break;
  1214. case 0xFF:
  1215. /* Huffman encoding */
  1216. compr_char = GetByte();
  1217. i = 8;
  1218. for (index = 0; index < size; index++) {
  1219. ByteData[index] = 0x00;
  1220. if (i > 7) {
  1221. cDataByte = GetByte();
  1222. i = 0;
  1223. }
  1224. if ((cDataByte << i++) & 0x80)
  1225. m = 8;
  1226. else {
  1227. ByteData[index] = compr_char;
  1228. m = 0;
  1229. }
  1230. for (j = 0; j < m; j++) {
  1231. if (i > 7) {
  1232. cDataByte = GetByte();
  1233. i = 0;
  1234. }
  1235. ByteData[index] |=
  1236. ((cDataByte << i++) & 0x80) >> j;
  1237. }
  1238. }
  1239. size = 0;
  1240. break;
  1241. default:
  1242. for (index = 0; index < size; index++)
  1243. ByteData[index] = 0x00;
  1244. for (index = 0; index < compress; index++) {
  1245. if (index % 2 == 0)
  1246. cDataByte = GetByte();
  1247. for (i = 0; i < size * 2 / compress; i++) {
  1248. j = (unsigned short)(index +
  1249. (i * (unsigned short)compress));
  1250. /*clear the nibble to zero first*/
  1251. if (j%2) {
  1252. if (index % 2)
  1253. ByteData[j/2] |=
  1254. cDataByte & 0xF;
  1255. else
  1256. ByteData[j/2] |=
  1257. cDataByte >> 4;
  1258. } else {
  1259. if (index % 2)
  1260. ByteData[j/2] |=
  1261. cDataByte << 4;
  1262. else
  1263. ByteData[j/2] |=
  1264. cDataByte & 0xF0;
  1265. }
  1266. }
  1267. }
  1268. size = 0;
  1269. break;
  1270. }
  1271. }
  1272. FFcount = 0;
  1273. /* Decompress by byte 0x00 or 0xFF */
  1274. for (index = 0; index < size; index++) {
  1275. if (FFcount <= 0) {
  1276. cDataByte = GetByte();
  1277. if ((cDataByte == VAR) && (g_usDataType&HEAP_IN) &&
  1278. !getData && !(g_usDataType&COMPRESS)) {
  1279. getData = 1;
  1280. g_usDataType &= ~(HEAP_IN);
  1281. cDataByte = GetByte();
  1282. }
  1283. ByteData[index] = cDataByte;
  1284. if ((compression) && (cDataByte == compr_char))
  1285. /* 09/11/07 NN Type cast mismatch variables */
  1286. FFcount = (unsigned short) ispVMDataSize();
  1287. /*The number of 0xFF or 0x00 bytes*/
  1288. } else {
  1289. FFcount--; /*Use up the 0xFF chain first*/
  1290. ByteData[index] = compr_char;
  1291. }
  1292. }
  1293. if (getData) {
  1294. g_usDataType |= HEAP_IN;
  1295. getData = 0;
  1296. }
  1297. }
  1298. /*
  1299. *
  1300. * ispVMShift
  1301. *
  1302. * Processes the SDR/XSDR/SIR commands.
  1303. *
  1304. */
  1305. signed char ispVMShift(signed char a_cCode)
  1306. {
  1307. /* 09/11/07 NN added local variables initialization */
  1308. unsigned short iDataIndex = 0;
  1309. unsigned short iReadLoop = 0;
  1310. signed char cRetCode = 0;
  1311. cRetCode = 0;
  1312. /* 09/11/07 NN Type cast mismatch variables */
  1313. g_usiDataSize = (unsigned short) ispVMDataSize();
  1314. /*clear the flags first*/
  1315. g_usDataType &= ~(SIR_DATA + EXPRESS + SDR_DATA);
  1316. switch (a_cCode) {
  1317. case SIR:
  1318. g_usDataType |= SIR_DATA;
  1319. /*
  1320. * 1/15/04 If performing cascading, then go directly to SHIFTIR.
  1321. * Else, go to IRPAUSE before going to SHIFTIR
  1322. */
  1323. if (g_usFlowControl & CASCADE) {
  1324. ispVMStateMachine(SHIFTIR);
  1325. } else {
  1326. ispVMStateMachine(IRPAUSE);
  1327. ispVMStateMachine(SHIFTIR);
  1328. if (g_usHeadIR > 0) {
  1329. ispVMBypass(HIR, g_usHeadIR);
  1330. sclock();
  1331. }
  1332. }
  1333. break;
  1334. case XSDR:
  1335. g_usDataType |= EXPRESS; /*mark simultaneous in and out*/
  1336. case SDR:
  1337. g_usDataType |= SDR_DATA;
  1338. /*
  1339. * 1/15/04 If already in SHIFTDR, then do not move state or
  1340. * shift in header. This would imply that the previously
  1341. * shifted frame was a cascaded frame.
  1342. */
  1343. if (g_cCurrentJTAGState != SHIFTDR) {
  1344. /*
  1345. * 1/15/04 If performing cascading, then go directly
  1346. * to SHIFTDR. Else, go to DRPAUSE before going
  1347. * to SHIFTDR
  1348. */
  1349. if (g_usFlowControl & CASCADE) {
  1350. if (g_cCurrentJTAGState == DRPAUSE) {
  1351. ispVMStateMachine(SHIFTDR);
  1352. /*
  1353. * 1/15/04 If cascade flag has been seat
  1354. * and the current state is DRPAUSE,
  1355. * this implies that the first cascaded
  1356. * frame is about to be shifted in. The
  1357. * header must be shifted prior to
  1358. * shifting the first cascaded frame.
  1359. */
  1360. if (g_usHeadDR > 0) {
  1361. ispVMBypass(HDR, g_usHeadDR);
  1362. sclock();
  1363. }
  1364. } else {
  1365. ispVMStateMachine(SHIFTDR);
  1366. }
  1367. } else {
  1368. ispVMStateMachine(DRPAUSE);
  1369. ispVMStateMachine(SHIFTDR);
  1370. if (g_usHeadDR > 0) {
  1371. ispVMBypass(HDR, g_usHeadDR);
  1372. sclock();
  1373. }
  1374. }
  1375. }
  1376. break;
  1377. default:
  1378. return VME_INVALID_FILE;
  1379. }
  1380. cRetCode = ispVMDataCode();
  1381. if (cRetCode != 0) {
  1382. return VME_INVALID_FILE;
  1383. }
  1384. #ifdef DEBUG
  1385. printf("%d ", g_usiDataSize);
  1386. if (g_usDataType & TDI_DATA) {
  1387. puts("TDI ");
  1388. PrintData(g_usiDataSize, g_pucInData);
  1389. }
  1390. if (g_usDataType & TDO_DATA) {
  1391. puts("\n\t\tTDO ");
  1392. PrintData(g_usiDataSize, g_pucOutData);
  1393. }
  1394. if (g_usDataType & MASK_DATA) {
  1395. puts("\n\t\tMASK ");
  1396. PrintData(g_usiDataSize, g_pucOutMaskData);
  1397. }
  1398. if (g_usDataType & DMASK_DATA) {
  1399. puts("\n\t\tDMASK ");
  1400. PrintData(g_usiDataSize, g_pucOutDMaskData);
  1401. }
  1402. puts(";\n");
  1403. #endif /* DEBUG */
  1404. if (g_usDataType & TDO_DATA || g_usDataType & DMASK_DATA) {
  1405. if (g_usDataType & DMASK_DATA) {
  1406. cRetCode = ispVMReadandSave(g_usiDataSize);
  1407. if (!cRetCode) {
  1408. if (g_usTailDR > 0) {
  1409. sclock();
  1410. ispVMBypass(TDR, g_usTailDR);
  1411. }
  1412. ispVMStateMachine(DRPAUSE);
  1413. ispVMStateMachine(SHIFTDR);
  1414. if (g_usHeadDR > 0) {
  1415. ispVMBypass(HDR, g_usHeadDR);
  1416. sclock();
  1417. }
  1418. for (iDataIndex = 0;
  1419. iDataIndex < g_usiDataSize / 8 + 1;
  1420. iDataIndex++)
  1421. g_pucInData[iDataIndex] =
  1422. g_pucOutData[iDataIndex];
  1423. g_usDataType &= ~(TDO_DATA + DMASK_DATA);
  1424. cRetCode = ispVMSend(g_usiDataSize);
  1425. }
  1426. } else {
  1427. cRetCode = ispVMRead(g_usiDataSize);
  1428. if (cRetCode == -1 && g_cVendor == XILINX) {
  1429. for (iReadLoop = 0; iReadLoop < 30;
  1430. iReadLoop++) {
  1431. cRetCode = ispVMRead(g_usiDataSize);
  1432. if (!cRetCode) {
  1433. break;
  1434. } else {
  1435. /* Always DRPAUSE */
  1436. ispVMStateMachine(DRPAUSE);
  1437. /*
  1438. * Bypass other devices
  1439. * when appropriate
  1440. */
  1441. ispVMBypass(TDR, g_usTailDR);
  1442. ispVMStateMachine(g_ucEndDR);
  1443. ispVMStateMachine(IDLE);
  1444. ispVMDelay(1000);
  1445. }
  1446. }
  1447. }
  1448. }
  1449. } else { /*TDI only*/
  1450. cRetCode = ispVMSend(g_usiDataSize);
  1451. }
  1452. /*transfer the input data to the output buffer for the next verify*/
  1453. if ((g_usDataType & EXPRESS) || (a_cCode == SDR)) {
  1454. if (g_pucOutData) {
  1455. for (iDataIndex = 0; iDataIndex < g_usiDataSize / 8 + 1;
  1456. iDataIndex++)
  1457. g_pucOutData[iDataIndex] =
  1458. g_pucInData[iDataIndex];
  1459. }
  1460. }
  1461. switch (a_cCode) {
  1462. case SIR:
  1463. /* 1/15/04 If not performing cascading, then shift ENDIR */
  1464. if (!(g_usFlowControl & CASCADE)) {
  1465. if (g_usTailIR > 0) {
  1466. sclock();
  1467. ispVMBypass(TIR, g_usTailIR);
  1468. }
  1469. ispVMStateMachine(g_ucEndIR);
  1470. }
  1471. break;
  1472. case XSDR:
  1473. case SDR:
  1474. /* 1/15/04 If not performing cascading, then shift ENDDR */
  1475. if (!(g_usFlowControl & CASCADE)) {
  1476. if (g_usTailDR > 0) {
  1477. sclock();
  1478. ispVMBypass(TDR, g_usTailDR);
  1479. }
  1480. ispVMStateMachine(g_ucEndDR);
  1481. }
  1482. break;
  1483. default:
  1484. break;
  1485. }
  1486. return cRetCode;
  1487. }
  1488. /*
  1489. *
  1490. * ispVMAmble
  1491. *
  1492. * This routine is to extract Header and Trailer parameter for SIR and
  1493. * SDR operations.
  1494. *
  1495. * The Header and Trailer parameter are the pre-amble and post-amble bit
  1496. * stream need to be shifted into TDI or out of TDO of the devices. Mostly
  1497. * is for the purpose of bypassing the leading or trailing devices. ispVM
  1498. * supports only shifting data into TDI to bypass the devices.
  1499. *
  1500. * For a single device, the header and trailer parameters are all set to 0
  1501. * as default by ispVM. If it is for multiple devices, the header and trailer
  1502. * value will change as specified by the VME file.
  1503. *
  1504. */
  1505. signed char ispVMAmble(signed char Code)
  1506. {
  1507. signed char compress = 0;
  1508. /* 09/11/07 NN Type cast mismatch variables */
  1509. g_usiDataSize = (unsigned short)ispVMDataSize();
  1510. #ifdef DEBUG
  1511. printf("%d", g_usiDataSize);
  1512. #endif /* DEBUG */
  1513. if (g_usiDataSize) {
  1514. /*
  1515. * Discard the TDI byte and set the compression bit in the data
  1516. * type register to false if compression is set because TDI data
  1517. * after HIR/HDR/TIR/TDR is not compressed.
  1518. */
  1519. GetByte();
  1520. if (g_usDataType & COMPRESS) {
  1521. g_usDataType &= ~(COMPRESS);
  1522. compress = 1;
  1523. }
  1524. }
  1525. switch (Code) {
  1526. case HIR:
  1527. /*
  1528. * Store the maximum size of the HIR buffer.
  1529. * Used to convert VME to HEX.
  1530. */
  1531. if (g_usiDataSize > g_usHIRSize) {
  1532. g_usHIRSize = g_usiDataSize;
  1533. }
  1534. /*
  1535. * Assign the HIR value and allocate memory.
  1536. */
  1537. g_usHeadIR = g_usiDataSize;
  1538. if (g_usHeadIR) {
  1539. ispVMMemManager(HIR, g_usHeadIR);
  1540. ispVMData(g_pucHIRData);
  1541. #ifdef DEBUG
  1542. puts(" TDI ");
  1543. PrintData(g_usHeadIR, g_pucHIRData);
  1544. #endif /* DEBUG */
  1545. }
  1546. break;
  1547. case TIR:
  1548. /*
  1549. * Store the maximum size of the TIR buffer.
  1550. * Used to convert VME to HEX.
  1551. */
  1552. if (g_usiDataSize > g_usTIRSize) {
  1553. g_usTIRSize = g_usiDataSize;
  1554. }
  1555. /*
  1556. * Assign the TIR value and allocate memory.
  1557. */
  1558. g_usTailIR = g_usiDataSize;
  1559. if (g_usTailIR) {
  1560. ispVMMemManager(TIR, g_usTailIR);
  1561. ispVMData(g_pucTIRData);
  1562. #ifdef DEBUG
  1563. puts(" TDI ");
  1564. PrintData(g_usTailIR, g_pucTIRData);
  1565. #endif /* DEBUG */
  1566. }
  1567. break;
  1568. case HDR:
  1569. /*
  1570. * Store the maximum size of the HDR buffer.
  1571. * Used to convert VME to HEX.
  1572. */
  1573. if (g_usiDataSize > g_usHDRSize) {
  1574. g_usHDRSize = g_usiDataSize;
  1575. }
  1576. /*
  1577. * Assign the HDR value and allocate memory.
  1578. *
  1579. */
  1580. g_usHeadDR = g_usiDataSize;
  1581. if (g_usHeadDR) {
  1582. ispVMMemManager(HDR, g_usHeadDR);
  1583. ispVMData(g_pucHDRData);
  1584. #ifdef DEBUG
  1585. puts(" TDI ");
  1586. PrintData(g_usHeadDR, g_pucHDRData);
  1587. #endif /* DEBUG */
  1588. }
  1589. break;
  1590. case TDR:
  1591. /*
  1592. * Store the maximum size of the TDR buffer.
  1593. * Used to convert VME to HEX.
  1594. */
  1595. if (g_usiDataSize > g_usTDRSize) {
  1596. g_usTDRSize = g_usiDataSize;
  1597. }
  1598. /*
  1599. * Assign the TDR value and allocate memory.
  1600. *
  1601. */
  1602. g_usTailDR = g_usiDataSize;
  1603. if (g_usTailDR) {
  1604. ispVMMemManager(TDR, g_usTailDR);
  1605. ispVMData(g_pucTDRData);
  1606. #ifdef DEBUG
  1607. puts(" TDI ");
  1608. PrintData(g_usTailDR, g_pucTDRData);
  1609. #endif /* DEBUG */
  1610. }
  1611. break;
  1612. default:
  1613. break;
  1614. }
  1615. /*
  1616. *
  1617. * Re-enable compression if it was previously set.
  1618. *
  1619. **/
  1620. if (compress) {
  1621. g_usDataType |= COMPRESS;
  1622. }
  1623. if (g_usiDataSize) {
  1624. Code = GetByte();
  1625. if (Code == CONTINUE) {
  1626. return 0;
  1627. } else {
  1628. /*
  1629. * Encountered invalid opcode.
  1630. */
  1631. return VME_INVALID_FILE;
  1632. }
  1633. }
  1634. return 0;
  1635. }
  1636. /*
  1637. *
  1638. * ispVMLoop
  1639. *
  1640. * Perform the function call upon by the REPEAT opcode.
  1641. * Memory is to be allocated to store the entire loop from REPEAT to ENDLOOP.
  1642. * After the loop is stored then execution begin. The REPEATLOOP flag is set
  1643. * on the g_usFlowControl register to indicate the repeat loop is in session
  1644. * and therefore fetch opcode from the memory instead of from the file.
  1645. *
  1646. */
  1647. signed char ispVMLoop(unsigned short a_usLoopCount)
  1648. {
  1649. /* 09/11/07 NN added local variables initialization */
  1650. signed char cRetCode = 0;
  1651. unsigned short iHeapIndex = 0;
  1652. unsigned short iLoopIndex = 0;
  1653. g_usShiftValue = 0;
  1654. for (iHeapIndex = 0; iHeapIndex < g_iHEAPSize; iHeapIndex++) {
  1655. g_pucHeapMemory[iHeapIndex] = GetByte();
  1656. }
  1657. if (g_pucHeapMemory[iHeapIndex - 1] != ENDLOOP) {
  1658. return VME_INVALID_FILE;
  1659. }
  1660. g_usFlowControl |= REPEATLOOP;
  1661. g_usDataType |= HEAP_IN;
  1662. for (iLoopIndex = 0; iLoopIndex < a_usLoopCount; iLoopIndex++) {
  1663. g_iHeapCounter = 0;
  1664. cRetCode = ispVMCode();
  1665. g_usRepeatLoops++;
  1666. if (cRetCode < 0) {
  1667. break;
  1668. }
  1669. }
  1670. g_usDataType &= ~(HEAP_IN);
  1671. g_usFlowControl &= ~(REPEATLOOP);
  1672. return cRetCode;
  1673. }
  1674. /*
  1675. *
  1676. * ispVMBitShift
  1677. *
  1678. * Shift the TDI stream left or right by the number of bits. The data in
  1679. * *g_pucInData is of the VME format, so the actual shifting is the reverse of
  1680. * IEEE 1532 or SVF format.
  1681. *
  1682. */
  1683. signed char ispVMBitShift(signed char mode, unsigned short bits)
  1684. {
  1685. /* 09/11/07 NN added local variables initialization */
  1686. unsigned short i = 0;
  1687. unsigned short size = 0;
  1688. unsigned short tmpbits = 0;
  1689. if (g_usiDataSize % 8 > 0) {
  1690. /* 09/11/07 NN Type cast mismatch variables */
  1691. size = (unsigned short)(g_usiDataSize / 8 + 1);
  1692. } else {
  1693. /* 09/11/07 NN Type cast mismatch variables */
  1694. size = (unsigned short)(g_usiDataSize / 8);
  1695. }
  1696. switch (mode) {
  1697. case SHR:
  1698. for (i = 0; i < size; i++) {
  1699. if (g_pucInData[i] != 0) {
  1700. tmpbits = bits;
  1701. while (tmpbits > 0) {
  1702. g_pucInData[i] <<= 1;
  1703. if (g_pucInData[i] == 0) {
  1704. i--;
  1705. g_pucInData[i] = 1;
  1706. }
  1707. tmpbits--;
  1708. }
  1709. }
  1710. }
  1711. break;
  1712. case SHL:
  1713. for (i = 0; i < size; i++) {
  1714. if (g_pucInData[i] != 0) {
  1715. tmpbits = bits;
  1716. while (tmpbits > 0) {
  1717. g_pucInData[i] >>= 1;
  1718. if (g_pucInData[i] == 0) {
  1719. i--;
  1720. g_pucInData[i] = 8;
  1721. }
  1722. tmpbits--;
  1723. }
  1724. }
  1725. }
  1726. break;
  1727. default:
  1728. return VME_INVALID_FILE;
  1729. }
  1730. return 0;
  1731. }
  1732. /*
  1733. *
  1734. * ispVMComment
  1735. *
  1736. * Displays the SVF comments.
  1737. *
  1738. */
  1739. void ispVMComment(unsigned short a_usCommentSize)
  1740. {
  1741. char cCurByte = 0;
  1742. for (; a_usCommentSize > 0; a_usCommentSize--) {
  1743. /*
  1744. *
  1745. * Print character to the terminal.
  1746. *
  1747. **/
  1748. cCurByte = GetByte();
  1749. vme_out_char(cCurByte);
  1750. }
  1751. cCurByte = '\n';
  1752. vme_out_char(cCurByte);
  1753. }
  1754. /*
  1755. *
  1756. * ispVMHeader
  1757. *
  1758. * Iterate the length of the header and discard it.
  1759. *
  1760. */
  1761. void ispVMHeader(unsigned short a_usHeaderSize)
  1762. {
  1763. for (; a_usHeaderSize > 0; a_usHeaderSize--) {
  1764. GetByte();
  1765. }
  1766. }
  1767. /*
  1768. *
  1769. * ispVMCalculateCRC32
  1770. *
  1771. * Calculate the 32-bit CRC.
  1772. *
  1773. */
  1774. void ispVMCalculateCRC32(unsigned char a_ucData)
  1775. {
  1776. /* 09/11/07 NN added local variables initialization */
  1777. unsigned char ucIndex = 0;
  1778. unsigned char ucFlipData = 0;
  1779. unsigned short usCRCTableEntry = 0;
  1780. unsigned int crc_table[16] = {
  1781. 0x0000, 0xCC01, 0xD801,
  1782. 0x1400, 0xF001, 0x3C00,
  1783. 0x2800, 0xE401, 0xA001,
  1784. 0x6C00, 0x7800, 0xB401,
  1785. 0x5000, 0x9C01, 0x8801,
  1786. 0x4400
  1787. };
  1788. for (ucIndex = 0; ucIndex < 8; ucIndex++) {
  1789. ucFlipData <<= 1;
  1790. if (a_ucData & 0x01) {
  1791. ucFlipData |= 0x01;
  1792. }
  1793. a_ucData >>= 1;
  1794. }
  1795. /* 09/11/07 NN Type cast mismatch variables */
  1796. usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
  1797. g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
  1798. g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
  1799. usCRCTableEntry ^ crc_table[ucFlipData & 0xF]);
  1800. usCRCTableEntry = (unsigned short)(crc_table[g_usCalculatedCRC & 0xF]);
  1801. g_usCalculatedCRC = (unsigned short)((g_usCalculatedCRC >> 4) & 0x0FFF);
  1802. g_usCalculatedCRC = (unsigned short)(g_usCalculatedCRC ^
  1803. usCRCTableEntry ^ crc_table[(ucFlipData >> 4) & 0xF]);
  1804. }
  1805. /*
  1806. *
  1807. * ispVMLCOUNT
  1808. *
  1809. * Process the intelligent programming loops.
  1810. *
  1811. */
  1812. signed char ispVMLCOUNT(unsigned short a_usCountSize)
  1813. {
  1814. unsigned short usContinue = 1;
  1815. unsigned short usIntelBufferIndex = 0;
  1816. unsigned short usCountIndex = 0;
  1817. signed char cRetCode = 0;
  1818. signed char cRepeatHeap = 0;
  1819. signed char cOpcode = 0;
  1820. unsigned char ucState = 0;
  1821. unsigned short usDelay = 0;
  1822. unsigned short usToggle = 0;
  1823. g_usIntelBufferSize = (unsigned short)ispVMDataSize();
  1824. /*
  1825. * Allocate memory for intel buffer.
  1826. *
  1827. */
  1828. ispVMMemManager(LHEAP, g_usIntelBufferSize);
  1829. /*
  1830. * Store the maximum size of the intelligent buffer.
  1831. * Used to convert VME to HEX.
  1832. */
  1833. if (g_usIntelBufferSize > g_usLCOUNTSize) {
  1834. g_usLCOUNTSize = g_usIntelBufferSize;
  1835. }
  1836. /*
  1837. * Copy intel data to the buffer.
  1838. */
  1839. for (usIntelBufferIndex = 0; usIntelBufferIndex < g_usIntelBufferSize;
  1840. usIntelBufferIndex++) {
  1841. g_pucIntelBuffer[usIntelBufferIndex] = GetByte();
  1842. }
  1843. /*
  1844. * Set the data type register to get data from the intelligent
  1845. * data buffer.
  1846. */
  1847. g_usDataType |= LHEAP_IN;
  1848. /*
  1849. *
  1850. * If the HEAP_IN flag is set, temporarily unset the flag so data will be
  1851. * retrieved from the status buffer.
  1852. *
  1853. **/
  1854. if (g_usDataType & HEAP_IN) {
  1855. g_usDataType &= ~HEAP_IN;
  1856. cRepeatHeap = 1;
  1857. }
  1858. #ifdef DEBUG
  1859. printf("LCOUNT %d;\n", a_usCountSize);
  1860. #endif /* DEBUG */
  1861. /*
  1862. * Iterate through the intelligent programming command.
  1863. */
  1864. for (usCountIndex = 0; usCountIndex < a_usCountSize; usCountIndex++) {
  1865. /*
  1866. *
  1867. * Initialize the intel data index to 0 before each iteration.
  1868. *
  1869. **/
  1870. g_usIntelDataIndex = 0;
  1871. cOpcode = 0;
  1872. ucState = 0;
  1873. usDelay = 0;
  1874. usToggle = 0;
  1875. usContinue = 1;
  1876. /*
  1877. *
  1878. * Begin looping through all the VME opcodes.
  1879. *
  1880. */
  1881. /*
  1882. * 4/1/09 Nguyen replaced the recursive function call codes on
  1883. * the ispVMLCOUNT function
  1884. *
  1885. */
  1886. while (usContinue) {
  1887. cOpcode = GetByte();
  1888. switch (cOpcode) {
  1889. case HIR:
  1890. case TIR:
  1891. case HDR:
  1892. case TDR:
  1893. /*
  1894. * Set the header/trailer of the device in order
  1895. * to bypass successfully.
  1896. */
  1897. ispVMAmble(cOpcode);
  1898. break;
  1899. case STATE:
  1900. /*
  1901. * Step the JTAG state machine.
  1902. */
  1903. ucState = GetByte();
  1904. /*
  1905. * Step the JTAG state machine to DRCAPTURE
  1906. * to support Looping.
  1907. */
  1908. if ((g_usDataType & LHEAP_IN) &&
  1909. (ucState == DRPAUSE) &&
  1910. (g_cCurrentJTAGState == ucState)) {
  1911. ispVMStateMachine(DRCAPTURE);
  1912. }
  1913. ispVMStateMachine(ucState);
  1914. #ifdef DEBUG
  1915. printf("LDELAY %s ", GetState(ucState));
  1916. #endif /* DEBUG */
  1917. break;
  1918. case SIR:
  1919. #ifdef DEBUG
  1920. printf("SIR ");
  1921. #endif /* DEBUG */
  1922. /*
  1923. * Shift in data into the device.
  1924. */
  1925. cRetCode = ispVMShift(cOpcode);
  1926. break;
  1927. case SDR:
  1928. #ifdef DEBUG
  1929. printf("LSDR ");
  1930. #endif /* DEBUG */
  1931. /*
  1932. * Shift in data into the device.
  1933. */
  1934. cRetCode = ispVMShift(cOpcode);
  1935. break;
  1936. case WAIT:
  1937. /*
  1938. *
  1939. * Observe delay.
  1940. *
  1941. */
  1942. usDelay = (unsigned short)ispVMDataSize();
  1943. ispVMDelay(usDelay);
  1944. #ifdef DEBUG
  1945. if (usDelay & 0x8000) {
  1946. /*
  1947. * Since MSB is set, the delay time must
  1948. * be decoded to millisecond. The
  1949. * SVF2VME encodes the MSB to represent
  1950. * millisecond.
  1951. */
  1952. usDelay &= ~0x8000;
  1953. printf("%.2E SEC;\n",
  1954. (float) usDelay / 1000);
  1955. } else {
  1956. /*
  1957. * Since MSB is not set, the delay time
  1958. * is given as microseconds.
  1959. */
  1960. printf("%.2E SEC;\n",
  1961. (float) usDelay / 1000000);
  1962. }
  1963. #endif /* DEBUG */
  1964. break;
  1965. case TCK:
  1966. /*
  1967. * Issue clock toggles.
  1968. */
  1969. usToggle = (unsigned short)ispVMDataSize();
  1970. ispVMClocks(usToggle);
  1971. #ifdef DEBUG
  1972. printf("RUNTEST %d TCK;\n", usToggle);
  1973. #endif /* DEBUG */
  1974. break;
  1975. case ENDLOOP:
  1976. /*
  1977. * Exit point from processing loops.
  1978. */
  1979. usContinue = 0;
  1980. break;
  1981. case COMMENT:
  1982. /*
  1983. * Display comment.
  1984. */
  1985. ispVMComment((unsigned short) ispVMDataSize());
  1986. break;
  1987. case ispEN:
  1988. ucState = GetByte();
  1989. if ((ucState == ON) || (ucState == 0x01))
  1990. writePort(g_ucPinENABLE, 0x01);
  1991. else
  1992. writePort(g_ucPinENABLE, 0x00);
  1993. ispVMDelay(1);
  1994. break;
  1995. case TRST:
  1996. if (GetByte() == 0x01)
  1997. writePort(g_ucPinTRST, 0x01);
  1998. else
  1999. writePort(g_ucPinTRST, 0x00);
  2000. ispVMDelay(1);
  2001. break;
  2002. default:
  2003. /*
  2004. * Invalid opcode encountered.
  2005. */
  2006. debug("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
  2007. return VME_INVALID_FILE;
  2008. }
  2009. }
  2010. if (cRetCode >= 0) {
  2011. /*
  2012. * Break if intelligent programming is successful.
  2013. */
  2014. break;
  2015. }
  2016. }
  2017. /*
  2018. * If HEAP_IN flag was temporarily disabled,
  2019. * re-enable it before exiting
  2020. */
  2021. if (cRepeatHeap) {
  2022. g_usDataType |= HEAP_IN;
  2023. }
  2024. /*
  2025. * Set the data type register to not get data from the
  2026. * intelligent data buffer.
  2027. */
  2028. g_usDataType &= ~LHEAP_IN;
  2029. return cRetCode;
  2030. }
  2031. /*
  2032. *
  2033. * ispVMClocks
  2034. *
  2035. * Applies the specified number of pulses to TCK.
  2036. *
  2037. */
  2038. void ispVMClocks(unsigned short Clocks)
  2039. {
  2040. unsigned short iClockIndex = 0;
  2041. for (iClockIndex = 0; iClockIndex < Clocks; iClockIndex++) {
  2042. sclock();
  2043. }
  2044. }
  2045. /*
  2046. *
  2047. * ispVMBypass
  2048. *
  2049. * This procedure takes care of the HIR, HDR, TIR, TDR for the
  2050. * purpose of putting the other devices into Bypass mode. The
  2051. * current state is checked to find out if it is at DRPAUSE or
  2052. * IRPAUSE. If it is at DRPAUSE, perform bypass register scan.
  2053. * If it is at IRPAUSE, scan into instruction registers the bypass
  2054. * instruction.
  2055. *
  2056. */
  2057. void ispVMBypass(signed char ScanType, unsigned short Bits)
  2058. {
  2059. /* 09/11/07 NN added local variables initialization */
  2060. unsigned short iIndex = 0;
  2061. unsigned short iSourceIndex = 0;
  2062. unsigned char cBitState = 0;
  2063. unsigned char cCurByte = 0;
  2064. unsigned char *pcSource = NULL;
  2065. if (Bits <= 0) {
  2066. return;
  2067. }
  2068. switch (ScanType) {
  2069. case HIR:
  2070. pcSource = g_pucHIRData;
  2071. break;
  2072. case TIR:
  2073. pcSource = g_pucTIRData;
  2074. break;
  2075. case HDR:
  2076. pcSource = g_pucHDRData;
  2077. break;
  2078. case TDR:
  2079. pcSource = g_pucTDRData;
  2080. break;
  2081. default:
  2082. break;
  2083. }
  2084. iSourceIndex = 0;
  2085. cBitState = 0;
  2086. for (iIndex = 0; iIndex < Bits - 1; iIndex++) {
  2087. /* Scan instruction or bypass register */
  2088. if (iIndex % 8 == 0) {
  2089. cCurByte = pcSource[iSourceIndex++];
  2090. }
  2091. cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
  2092. ? 0x01 : 0x00);
  2093. writePort(g_ucPinTDI, cBitState);
  2094. sclock();
  2095. }
  2096. if (iIndex % 8 == 0) {
  2097. cCurByte = pcSource[iSourceIndex++];
  2098. }
  2099. cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
  2100. ? 0x01 : 0x00);
  2101. writePort(g_ucPinTDI, cBitState);
  2102. }
  2103. /*
  2104. *
  2105. * ispVMStateMachine
  2106. *
  2107. * This procedure steps all devices in the daisy chain from a given
  2108. * JTAG state to the next desirable state. If the next state is TLR,
  2109. * the JTAG state machine is brute forced into TLR by driving TMS
  2110. * high and pulse TCK 6 times.
  2111. *
  2112. */
  2113. void ispVMStateMachine(signed char cNextJTAGState)
  2114. {
  2115. /* 09/11/07 NN added local variables initialization */
  2116. signed char cPathIndex = 0;
  2117. signed char cStateIndex = 0;
  2118. if ((g_cCurrentJTAGState == cNextJTAGState) &&
  2119. (cNextJTAGState != RESET)) {
  2120. return;
  2121. }
  2122. for (cStateIndex = 0; cStateIndex < 25; cStateIndex++) {
  2123. if ((g_cCurrentJTAGState ==
  2124. g_JTAGTransistions[cStateIndex].CurState) &&
  2125. (cNextJTAGState ==
  2126. g_JTAGTransistions[cStateIndex].NextState)) {
  2127. break;
  2128. }
  2129. }
  2130. g_cCurrentJTAGState = cNextJTAGState;
  2131. for (cPathIndex = 0;
  2132. cPathIndex < g_JTAGTransistions[cStateIndex].Pulses;
  2133. cPathIndex++) {
  2134. if ((g_JTAGTransistions[cStateIndex].Pattern << cPathIndex)
  2135. & 0x80) {
  2136. writePort(g_ucPinTMS, (unsigned char) 0x01);
  2137. } else {
  2138. writePort(g_ucPinTMS, (unsigned char) 0x00);
  2139. }
  2140. sclock();
  2141. }
  2142. writePort(g_ucPinTDI, 0x00);
  2143. writePort(g_ucPinTMS, 0x00);
  2144. }
  2145. /*
  2146. *
  2147. * ispVMStart
  2148. *
  2149. * Enable the port to the device and set the state to RESET (TLR).
  2150. *
  2151. */
  2152. void ispVMStart()
  2153. {
  2154. #ifdef DEBUG
  2155. printf("// ISPVM EMBEDDED ADDED\n");
  2156. printf("STATE RESET;\n");
  2157. #endif
  2158. g_usFlowControl = 0;
  2159. g_usDataType = g_uiChecksumIndex = g_cCurrentJTAGState = 0;
  2160. g_usHeadDR = g_usHeadIR = g_usTailDR = g_usTailIR = 0;
  2161. g_usMaxSize = g_usShiftValue = g_usRepeatLoops = 0;
  2162. g_usTDOSize = g_usMASKSize = g_usTDISize = 0;
  2163. g_usDMASKSize = g_usLCOUNTSize = g_usHDRSize = 0;
  2164. g_usTDRSize = g_usHIRSize = g_usTIRSize = g_usHeapSize = 0;
  2165. g_pLVDSList = NULL;
  2166. g_usLVDSPairCount = 0;
  2167. previous_size = 0;
  2168. ispVMStateMachine(RESET); /*step devices to RESET state*/
  2169. }
  2170. /*
  2171. *
  2172. * ispVMEnd
  2173. *
  2174. * Set the state of devices to RESET to enable the devices and disable
  2175. * the port.
  2176. *
  2177. */
  2178. void ispVMEnd()
  2179. {
  2180. #ifdef DEBUG
  2181. printf("// ISPVM EMBEDDED ADDED\n");
  2182. printf("STATE RESET;\n");
  2183. printf("RUNTEST 1.00E-001 SEC;\n");
  2184. #endif
  2185. ispVMStateMachine(RESET); /*step devices to RESET state */
  2186. ispVMDelay(1000); /*wake up devices*/
  2187. }
  2188. /*
  2189. *
  2190. * ispVMSend
  2191. *
  2192. * Send the TDI data stream to devices. The data stream can be
  2193. * instructions or data.
  2194. *
  2195. */
  2196. signed char ispVMSend(unsigned short a_usiDataSize)
  2197. {
  2198. /* 09/11/07 NN added local variables initialization */
  2199. unsigned short iIndex = 0;
  2200. unsigned short iInDataIndex = 0;
  2201. unsigned char cCurByte = 0;
  2202. unsigned char cBitState = 0;
  2203. for (iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++) {
  2204. if (iIndex % 8 == 0) {
  2205. cCurByte = g_pucInData[iInDataIndex++];
  2206. }
  2207. cBitState = (unsigned char)(((cCurByte << iIndex % 8) & 0x80)
  2208. ? 0x01 : 0x00);
  2209. writePort(g_ucPinTDI, cBitState);
  2210. sclock();
  2211. }
  2212. if (iIndex % 8 == 0) {
  2213. /* Take care of the last bit */
  2214. cCurByte = g_pucInData[iInDataIndex];
  2215. }
  2216. cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
  2217. ? 0x01 : 0x00);
  2218. writePort(g_ucPinTDI, cBitState);
  2219. if (g_usFlowControl & CASCADE) {
  2220. /*1/15/04 Clock in last bit for the first n-1 cascaded frames */
  2221. sclock();
  2222. }
  2223. return 0;
  2224. }
  2225. /*
  2226. *
  2227. * ispVMRead
  2228. *
  2229. * Read the data stream from devices and verify.
  2230. *
  2231. */
  2232. signed char ispVMRead(unsigned short a_usiDataSize)
  2233. {
  2234. /* 09/11/07 NN added local variables initialization */
  2235. unsigned short usDataSizeIndex = 0;
  2236. unsigned short usErrorCount = 0;
  2237. unsigned short usLastBitIndex = 0;
  2238. unsigned char cDataByte = 0;
  2239. unsigned char cMaskByte = 0;
  2240. unsigned char cInDataByte = 0;
  2241. unsigned char cCurBit = 0;
  2242. unsigned char cByteIndex = 0;
  2243. unsigned short usBufferIndex = 0;
  2244. unsigned char ucDisplayByte = 0x00;
  2245. unsigned char ucDisplayFlag = 0x01;
  2246. char StrChecksum[256] = {0};
  2247. unsigned char g_usCalculateChecksum = 0x00;
  2248. /* 09/11/07 NN Type cast mismatch variables */
  2249. usLastBitIndex = (unsigned short)(a_usiDataSize - 1);
  2250. #ifndef DEBUG
  2251. /*
  2252. * If mask is not all zeros, then set the display flag to 0x00,
  2253. * otherwise it shall be set to 0x01 to indicate that data read
  2254. * from the device shall be displayed. If DEBUG is defined,
  2255. * always display data.
  2256. */
  2257. for (usDataSizeIndex = 0; usDataSizeIndex < (a_usiDataSize + 7) / 8;
  2258. usDataSizeIndex++) {
  2259. if (g_usDataType & MASK_DATA) {
  2260. if (g_pucOutMaskData[usDataSizeIndex] != 0x00) {
  2261. ucDisplayFlag = 0x00;
  2262. break;
  2263. }
  2264. } else if (g_usDataType & CMASK_DATA) {
  2265. g_usCalculateChecksum = 0x01;
  2266. ucDisplayFlag = 0x00;
  2267. break;
  2268. } else {
  2269. ucDisplayFlag = 0x00;
  2270. break;
  2271. }
  2272. }
  2273. #endif /* DEBUG */
  2274. /*
  2275. *
  2276. * Begin shifting data in and out of the device.
  2277. *
  2278. **/
  2279. for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
  2280. usDataSizeIndex++) {
  2281. if (cByteIndex == 0) {
  2282. /*
  2283. * Grab byte from TDO buffer.
  2284. */
  2285. if (g_usDataType & TDO_DATA) {
  2286. cDataByte = g_pucOutData[usBufferIndex];
  2287. }
  2288. /*
  2289. * Grab byte from MASK buffer.
  2290. */
  2291. if (g_usDataType & MASK_DATA) {
  2292. cMaskByte = g_pucOutMaskData[usBufferIndex];
  2293. } else {
  2294. cMaskByte = 0xFF;
  2295. }
  2296. /*
  2297. * Grab byte from CMASK buffer.
  2298. */
  2299. if (g_usDataType & CMASK_DATA) {
  2300. cMaskByte = 0x00;
  2301. g_usCalculateChecksum = 0x01;
  2302. }
  2303. /*
  2304. * Grab byte from TDI buffer.
  2305. */
  2306. if (g_usDataType & TDI_DATA) {
  2307. cInDataByte = g_pucInData[usBufferIndex];
  2308. }
  2309. usBufferIndex++;
  2310. }
  2311. cCurBit = readPort();
  2312. if (ucDisplayFlag) {
  2313. ucDisplayByte <<= 1;
  2314. ucDisplayByte |= cCurBit;
  2315. }
  2316. /*
  2317. * Check if data read from port matches with expected TDO.
  2318. */
  2319. if (g_usDataType & TDO_DATA) {
  2320. /* 08/28/08 NN Added Calculate checksum support. */
  2321. if (g_usCalculateChecksum) {
  2322. if (cCurBit == 0x01)
  2323. g_usChecksum +=
  2324. (1 << (g_uiChecksumIndex % 8));
  2325. g_uiChecksumIndex++;
  2326. } else {
  2327. if ((((cMaskByte << cByteIndex) & 0x80)
  2328. ? 0x01 : 0x00)) {
  2329. if (cCurBit != (unsigned char)
  2330. (((cDataByte << cByteIndex) & 0x80)
  2331. ? 0x01 : 0x00)) {
  2332. usErrorCount++;
  2333. }
  2334. }
  2335. }
  2336. }
  2337. /*
  2338. * Write TDI data to the port.
  2339. */
  2340. writePort(g_ucPinTDI,
  2341. (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
  2342. ? 0x01 : 0x00));
  2343. if (usDataSizeIndex < usLastBitIndex) {
  2344. /*
  2345. * Clock data out from the data shift register.
  2346. */
  2347. sclock();
  2348. } else if (g_usFlowControl & CASCADE) {
  2349. /*
  2350. * Clock in last bit for the first N - 1 cascaded frames
  2351. */
  2352. sclock();
  2353. }
  2354. /*
  2355. * Increment the byte index. If it exceeds 7, then reset it back
  2356. * to zero.
  2357. */
  2358. cByteIndex++;
  2359. if (cByteIndex >= 8) {
  2360. if (ucDisplayFlag) {
  2361. /*
  2362. * Store displayed data in the TDO buffer. By reusing
  2363. * the TDO buffer to store displayed data, there is no
  2364. * need to allocate a buffer simply to hold display
  2365. * data. This will not cause any false verification
  2366. * errors because the true TDO byte has already
  2367. * been consumed.
  2368. */
  2369. g_pucOutData[usBufferIndex - 1] = ucDisplayByte;
  2370. ucDisplayByte = 0;
  2371. }
  2372. cByteIndex = 0;
  2373. }
  2374. /* 09/12/07 Nguyen changed to display the 1 bit expected data */
  2375. else if (a_usiDataSize == 1) {
  2376. if (ucDisplayFlag) {
  2377. /*
  2378. * Store displayed data in the TDO buffer.
  2379. * By reusing the TDO buffer to store displayed
  2380. * data, there is no need to allocate
  2381. * a buffer simply to hold display data. This
  2382. * will not cause any false verification errors
  2383. * because the true TDO byte has already
  2384. * been consumed.
  2385. */
  2386. /*
  2387. * Flip ucDisplayByte and store it in cDataByte.
  2388. */
  2389. cDataByte = 0x00;
  2390. for (usBufferIndex = 0; usBufferIndex < 8;
  2391. usBufferIndex++) {
  2392. cDataByte <<= 1;
  2393. if (ucDisplayByte & 0x01) {
  2394. cDataByte |= 0x01;
  2395. }
  2396. ucDisplayByte >>= 1;
  2397. }
  2398. g_pucOutData[0] = cDataByte;
  2399. ucDisplayByte = 0;
  2400. }
  2401. cByteIndex = 0;
  2402. }
  2403. }
  2404. if (ucDisplayFlag) {
  2405. #ifdef DEBUG
  2406. debug("RECEIVED TDO (");
  2407. #else
  2408. vme_out_string("Display Data: 0x");
  2409. #endif /* DEBUG */
  2410. /* 09/11/07 NN Type cast mismatch variables */
  2411. for (usDataSizeIndex = (unsigned short)
  2412. ((a_usiDataSize + 7) / 8);
  2413. usDataSizeIndex > 0 ; usDataSizeIndex--) {
  2414. cMaskByte = g_pucOutData[usDataSizeIndex - 1];
  2415. cDataByte = 0x00;
  2416. /*
  2417. * Flip cMaskByte and store it in cDataByte.
  2418. */
  2419. for (usBufferIndex = 0; usBufferIndex < 8;
  2420. usBufferIndex++) {
  2421. cDataByte <<= 1;
  2422. if (cMaskByte & 0x01) {
  2423. cDataByte |= 0x01;
  2424. }
  2425. cMaskByte >>= 1;
  2426. }
  2427. #ifdef DEBUG
  2428. printf("%.2X", cDataByte);
  2429. if ((((a_usiDataSize + 7) / 8) - usDataSizeIndex)
  2430. % 40 == 39) {
  2431. printf("\n\t\t");
  2432. }
  2433. #else
  2434. vme_out_hex(cDataByte);
  2435. #endif /* DEBUG */
  2436. }
  2437. #ifdef DEBUG
  2438. printf(")\n\n");
  2439. #else
  2440. vme_out_string("\n\n");
  2441. #endif /* DEBUG */
  2442. /* 09/02/08 Nguyen changed to display the data Checksum */
  2443. if (g_usChecksum != 0) {
  2444. g_usChecksum &= 0xFFFF;
  2445. sprintf(StrChecksum, "Data Checksum: %.4lX\n\n",
  2446. g_usChecksum);
  2447. vme_out_string(StrChecksum);
  2448. g_usChecksum = 0;
  2449. }
  2450. }
  2451. if (usErrorCount > 0) {
  2452. if (g_usFlowControl & VERIFYUES) {
  2453. vme_out_string(
  2454. "USERCODE verification failed. "
  2455. "Continue programming......\n\n");
  2456. g_usFlowControl &= ~(VERIFYUES);
  2457. return 0;
  2458. } else {
  2459. #ifdef DEBUG
  2460. printf("TOTAL ERRORS: %d\n", usErrorCount);
  2461. #endif /* DEBUG */
  2462. return VME_VERIFICATION_FAILURE;
  2463. }
  2464. } else {
  2465. if (g_usFlowControl & VERIFYUES) {
  2466. vme_out_string("USERCODE verification passed. "
  2467. "Programming aborted.\n\n");
  2468. g_usFlowControl &= ~(VERIFYUES);
  2469. return 1;
  2470. } else {
  2471. return 0;
  2472. }
  2473. }
  2474. }
  2475. /*
  2476. *
  2477. * ispVMReadandSave
  2478. *
  2479. * Support dynamic I/O.
  2480. *
  2481. */
  2482. signed char ispVMReadandSave(unsigned short int a_usiDataSize)
  2483. {
  2484. /* 09/11/07 NN added local variables initialization */
  2485. unsigned short int usDataSizeIndex = 0;
  2486. unsigned short int usLastBitIndex = 0;
  2487. unsigned short int usBufferIndex = 0;
  2488. unsigned short int usOutBitIndex = 0;
  2489. unsigned short int usLVDSIndex = 0;
  2490. unsigned char cDataByte = 0;
  2491. unsigned char cDMASKByte = 0;
  2492. unsigned char cInDataByte = 0;
  2493. unsigned char cCurBit = 0;
  2494. unsigned char cByteIndex = 0;
  2495. signed char cLVDSByteIndex = 0;
  2496. /* 09/11/07 NN Type cast mismatch variables */
  2497. usLastBitIndex = (unsigned short) (a_usiDataSize - 1);
  2498. /*
  2499. *
  2500. * Iterate through the data bits.
  2501. *
  2502. */
  2503. for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
  2504. usDataSizeIndex++) {
  2505. if (cByteIndex == 0) {
  2506. /*
  2507. * Grab byte from DMASK buffer.
  2508. */
  2509. if (g_usDataType & DMASK_DATA) {
  2510. cDMASKByte = g_pucOutDMaskData[usBufferIndex];
  2511. } else {
  2512. cDMASKByte = 0x00;
  2513. }
  2514. /*
  2515. * Grab byte from TDI buffer.
  2516. */
  2517. if (g_usDataType & TDI_DATA) {
  2518. cInDataByte = g_pucInData[usBufferIndex];
  2519. }
  2520. usBufferIndex++;
  2521. }
  2522. cCurBit = readPort();
  2523. cDataByte = (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
  2524. ? 0x01 : 0x00);
  2525. /*
  2526. * Initialize the byte to be zero.
  2527. */
  2528. if (usOutBitIndex % 8 == 0) {
  2529. g_pucOutData[usOutBitIndex / 8] = 0x00;
  2530. }
  2531. /*
  2532. * Use TDI, DMASK, and device TDO to create new TDI (actually
  2533. * stored in g_pucOutData).
  2534. */
  2535. if ((((cDMASKByte << cByteIndex) & 0x80) ? 0x01 : 0x00)) {
  2536. if (g_pLVDSList) {
  2537. for (usLVDSIndex = 0;
  2538. usLVDSIndex < g_usLVDSPairCount;
  2539. usLVDSIndex++) {
  2540. if (g_pLVDSList[usLVDSIndex].
  2541. usNegativeIndex ==
  2542. usDataSizeIndex) {
  2543. g_pLVDSList[usLVDSIndex].
  2544. ucUpdate = 0x01;
  2545. break;
  2546. }
  2547. }
  2548. }
  2549. /*
  2550. * DMASK bit is 1, use TDI.
  2551. */
  2552. g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
  2553. (((cDataByte & 0x1) ? 0x01 : 0x00) <<
  2554. (7 - usOutBitIndex % 8));
  2555. } else {
  2556. /*
  2557. * DMASK bit is 0, use device TDO.
  2558. */
  2559. g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
  2560. (((cCurBit & 0x1) ? 0x01 : 0x00) <<
  2561. (7 - usOutBitIndex % 8));
  2562. }
  2563. /*
  2564. * Shift in TDI in order to get TDO out.
  2565. */
  2566. usOutBitIndex++;
  2567. writePort(g_ucPinTDI, cDataByte);
  2568. if (usDataSizeIndex < usLastBitIndex) {
  2569. sclock();
  2570. }
  2571. /*
  2572. * Increment the byte index. If it exceeds 7, then reset it back
  2573. * to zero.
  2574. */
  2575. cByteIndex++;
  2576. if (cByteIndex >= 8) {
  2577. cByteIndex = 0;
  2578. }
  2579. }
  2580. /*
  2581. * If g_pLVDSList exists and pairs need updating, then update
  2582. * the negative-pair to receive the flipped positive-pair value.
  2583. */
  2584. if (g_pLVDSList) {
  2585. for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount;
  2586. usLVDSIndex++) {
  2587. if (g_pLVDSList[usLVDSIndex].ucUpdate) {
  2588. /*
  2589. * Read the positive value and flip it.
  2590. */
  2591. cDataByte = (unsigned char)
  2592. (((g_pucOutData[g_pLVDSList[usLVDSIndex].
  2593. usPositiveIndex / 8]
  2594. << (g_pLVDSList[usLVDSIndex].
  2595. usPositiveIndex % 8)) & 0x80) ?
  2596. 0x01 : 0x00);
  2597. /* 09/11/07 NN Type cast mismatch variables */
  2598. cDataByte = (unsigned char) (!cDataByte);
  2599. /*
  2600. * Get the byte that needs modification.
  2601. */
  2602. cInDataByte =
  2603. g_pucOutData[g_pLVDSList[usLVDSIndex].
  2604. usNegativeIndex / 8];
  2605. if (cDataByte) {
  2606. /*
  2607. * Copy over the current byte and
  2608. * set the negative bit to 1.
  2609. */
  2610. cDataByte = 0x00;
  2611. for (cLVDSByteIndex = 7;
  2612. cLVDSByteIndex >= 0;
  2613. cLVDSByteIndex--) {
  2614. cDataByte <<= 1;
  2615. if (7 -
  2616. (g_pLVDSList[usLVDSIndex].
  2617. usNegativeIndex % 8) ==
  2618. cLVDSByteIndex) {
  2619. /*
  2620. * Set negative bit to 1
  2621. */
  2622. cDataByte |= 0x01;
  2623. } else if (cInDataByte & 0x80) {
  2624. cDataByte |= 0x01;
  2625. }
  2626. cInDataByte <<= 1;
  2627. }
  2628. /*
  2629. * Store the modified byte.
  2630. */
  2631. g_pucOutData[g_pLVDSList[usLVDSIndex].
  2632. usNegativeIndex / 8] = cDataByte;
  2633. } else {
  2634. /*
  2635. * Copy over the current byte and set
  2636. * the negative bit to 0.
  2637. */
  2638. cDataByte = 0x00;
  2639. for (cLVDSByteIndex = 7;
  2640. cLVDSByteIndex >= 0;
  2641. cLVDSByteIndex--) {
  2642. cDataByte <<= 1;
  2643. if (7 -
  2644. (g_pLVDSList[usLVDSIndex].
  2645. usNegativeIndex % 8) ==
  2646. cLVDSByteIndex) {
  2647. /*
  2648. * Set negative bit to 0
  2649. */
  2650. cDataByte |= 0x00;
  2651. } else if (cInDataByte & 0x80) {
  2652. cDataByte |= 0x01;
  2653. }
  2654. cInDataByte <<= 1;
  2655. }
  2656. /*
  2657. * Store the modified byte.
  2658. */
  2659. g_pucOutData[g_pLVDSList[usLVDSIndex].
  2660. usNegativeIndex / 8] = cDataByte;
  2661. }
  2662. break;
  2663. }
  2664. }
  2665. }
  2666. return 0;
  2667. }
  2668. signed char ispVMProcessLVDS(unsigned short a_usLVDSCount)
  2669. {
  2670. unsigned short usLVDSIndex = 0;
  2671. /*
  2672. * Allocate memory to hold LVDS pairs.
  2673. */
  2674. ispVMMemManager(LVDS, a_usLVDSCount);
  2675. g_usLVDSPairCount = a_usLVDSCount;
  2676. #ifdef DEBUG
  2677. printf("LVDS %d (", a_usLVDSCount);
  2678. #endif /* DEBUG */
  2679. /*
  2680. * Iterate through each given LVDS pair.
  2681. */
  2682. for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount; usLVDSIndex++) {
  2683. /*
  2684. * Assign the positive and negative indices of the LVDS pair.
  2685. */
  2686. /* 09/11/07 NN Type cast mismatch variables */
  2687. g_pLVDSList[usLVDSIndex].usPositiveIndex =
  2688. (unsigned short) ispVMDataSize();
  2689. /* 09/11/07 NN Type cast mismatch variables */
  2690. g_pLVDSList[usLVDSIndex].usNegativeIndex =
  2691. (unsigned short)ispVMDataSize();
  2692. #ifdef DEBUG
  2693. if (usLVDSIndex < g_usLVDSPairCount - 1) {
  2694. printf("%d:%d, ",
  2695. g_pLVDSList[usLVDSIndex].usPositiveIndex,
  2696. g_pLVDSList[usLVDSIndex].usNegativeIndex);
  2697. } else {
  2698. printf("%d:%d",
  2699. g_pLVDSList[usLVDSIndex].usPositiveIndex,
  2700. g_pLVDSList[usLVDSIndex].usNegativeIndex);
  2701. }
  2702. #endif /* DEBUG */
  2703. }
  2704. #ifdef DEBUG
  2705. printf(");\n");
  2706. #endif /* DEBUG */
  2707. return 0;
  2708. }