ivm_core.c 65 KB

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