ivm_core.c 65 KB

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