ivm_core.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  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. unsigned char usByte = 0;
  1839. g_usIntelBufferSize = (unsigned short)ispVMDataSize();
  1840. /*
  1841. * Allocate memory for intel buffer.
  1842. *
  1843. */
  1844. ispVMMemManager(LHEAP, g_usIntelBufferSize);
  1845. /*
  1846. * Store the maximum size of the intelligent buffer.
  1847. * Used to convert VME to HEX.
  1848. */
  1849. if (g_usIntelBufferSize > g_usLCOUNTSize) {
  1850. g_usLCOUNTSize = g_usIntelBufferSize;
  1851. }
  1852. /*
  1853. * Copy intel data to the buffer.
  1854. */
  1855. for (usIntelBufferIndex = 0; usIntelBufferIndex < g_usIntelBufferSize;
  1856. usIntelBufferIndex++) {
  1857. g_pucIntelBuffer[usIntelBufferIndex] = GetByte();
  1858. }
  1859. /*
  1860. * Set the data type register to get data from the intelligent
  1861. * data buffer.
  1862. */
  1863. g_usDataType |= LHEAP_IN;
  1864. /*
  1865. *
  1866. * If the HEAP_IN flag is set, temporarily unset the flag so data will be
  1867. * retrieved from the status buffer.
  1868. *
  1869. **/
  1870. if (g_usDataType & HEAP_IN) {
  1871. g_usDataType &= ~HEAP_IN;
  1872. cRepeatHeap = 1;
  1873. }
  1874. #ifdef DEBUG
  1875. printf("LCOUNT %d;\n", a_usCountSize);
  1876. #endif /* DEBUG */
  1877. /*
  1878. * Iterate through the intelligent programming command.
  1879. */
  1880. for (usCountIndex = 0; usCountIndex < a_usCountSize; usCountIndex++) {
  1881. /*
  1882. *
  1883. * Initialize the intel data index to 0 before each iteration.
  1884. *
  1885. **/
  1886. g_usIntelDataIndex = 0;
  1887. cOpcode = 0;
  1888. ucState = 0;
  1889. usDelay = 0;
  1890. usToggle = 0;
  1891. usByte = 0;
  1892. usContinue = 1;
  1893. /*
  1894. *
  1895. * Begin looping through all the VME opcodes.
  1896. *
  1897. */
  1898. /*
  1899. * 4/1/09 Nguyen replaced the recursive function call codes on
  1900. * the ispVMLCOUNT function
  1901. *
  1902. */
  1903. while (usContinue) {
  1904. cOpcode = GetByte();
  1905. switch (cOpcode) {
  1906. case HIR:
  1907. case TIR:
  1908. case HDR:
  1909. case TDR:
  1910. /*
  1911. * Set the header/trailer of the device in order
  1912. * to bypass successfully.
  1913. */
  1914. ispVMAmble(cOpcode);
  1915. break;
  1916. case STATE:
  1917. /*
  1918. * Step the JTAG state machine.
  1919. */
  1920. ucState = GetByte();
  1921. /*
  1922. * Step the JTAG state machine to DRCAPTURE
  1923. * to support Looping.
  1924. */
  1925. if ((g_usDataType & LHEAP_IN) &&
  1926. (ucState == DRPAUSE) &&
  1927. (g_cCurrentJTAGState == ucState)) {
  1928. ispVMStateMachine(DRCAPTURE);
  1929. }
  1930. ispVMStateMachine(ucState);
  1931. #ifdef DEBUG
  1932. printf("LDELAY %s ", GetState(ucState));
  1933. #endif /* DEBUG */
  1934. break;
  1935. case SIR:
  1936. #ifdef DEBUG
  1937. printf("SIR ");
  1938. #endif /* DEBUG */
  1939. /*
  1940. * Shift in data into the device.
  1941. */
  1942. cRetCode = ispVMShift(cOpcode);
  1943. break;
  1944. case SDR:
  1945. #ifdef DEBUG
  1946. printf("LSDR ");
  1947. #endif /* DEBUG */
  1948. /*
  1949. * Shift in data into the device.
  1950. */
  1951. cRetCode = ispVMShift(cOpcode);
  1952. break;
  1953. case WAIT:
  1954. /*
  1955. *
  1956. * Observe delay.
  1957. *
  1958. */
  1959. usDelay = (unsigned short)ispVMDataSize();
  1960. ispVMDelay(usDelay);
  1961. #ifdef DEBUG
  1962. if (usDelay & 0x8000) {
  1963. /*
  1964. * Since MSB is set, the delay time must
  1965. * be decoded to millisecond. The
  1966. * SVF2VME encodes the MSB to represent
  1967. * millisecond.
  1968. */
  1969. usDelay &= ~0x8000;
  1970. printf("%.2E SEC;\n",
  1971. (float) usDelay / 1000);
  1972. } else {
  1973. /*
  1974. * Since MSB is not set, the delay time
  1975. * is given as microseconds.
  1976. */
  1977. printf("%.2E SEC;\n",
  1978. (float) usDelay / 1000000);
  1979. }
  1980. #endif /* DEBUG */
  1981. break;
  1982. case TCK:
  1983. /*
  1984. * Issue clock toggles.
  1985. */
  1986. usToggle = (unsigned short)ispVMDataSize();
  1987. ispVMClocks(usToggle);
  1988. #ifdef DEBUG
  1989. printf("RUNTEST %d TCK;\n", usToggle);
  1990. #endif /* DEBUG */
  1991. break;
  1992. case ENDLOOP:
  1993. /*
  1994. * Exit point from processing loops.
  1995. */
  1996. usContinue = 0;
  1997. break;
  1998. case COMMENT:
  1999. /*
  2000. * Display comment.
  2001. */
  2002. ispVMComment((unsigned short) ispVMDataSize());
  2003. break;
  2004. case ispEN:
  2005. ucState = GetByte();
  2006. if ((ucState == ON) || (ucState == 0x01))
  2007. writePort(g_ucPinENABLE, 0x01);
  2008. else
  2009. writePort(g_ucPinENABLE, 0x00);
  2010. ispVMDelay(1);
  2011. break;
  2012. case TRST:
  2013. if (GetByte() == 0x01)
  2014. writePort(g_ucPinTRST, 0x01);
  2015. else
  2016. writePort(g_ucPinTRST, 0x00);
  2017. ispVMDelay(1);
  2018. break;
  2019. default:
  2020. /*
  2021. * Invalid opcode encountered.
  2022. */
  2023. debug("\nINVALID OPCODE: 0x%.2X\n", cOpcode);
  2024. return VME_INVALID_FILE;
  2025. }
  2026. }
  2027. if (cRetCode >= 0) {
  2028. /*
  2029. * Break if intelligent programming is successful.
  2030. */
  2031. break;
  2032. }
  2033. }
  2034. /*
  2035. * If HEAP_IN flag was temporarily disabled,
  2036. * re-enable it before exiting
  2037. */
  2038. if (cRepeatHeap) {
  2039. g_usDataType |= HEAP_IN;
  2040. }
  2041. /*
  2042. * Set the data type register to not get data from the
  2043. * intelligent data buffer.
  2044. */
  2045. g_usDataType &= ~LHEAP_IN;
  2046. return cRetCode;
  2047. }
  2048. /*
  2049. *
  2050. * ispVMClocks
  2051. *
  2052. * Applies the specified number of pulses to TCK.
  2053. *
  2054. */
  2055. void ispVMClocks(unsigned short Clocks)
  2056. {
  2057. unsigned short iClockIndex = 0;
  2058. for (iClockIndex = 0; iClockIndex < Clocks; iClockIndex++) {
  2059. sclock();
  2060. }
  2061. }
  2062. /*
  2063. *
  2064. * ispVMBypass
  2065. *
  2066. * This procedure takes care of the HIR, HDR, TIR, TDR for the
  2067. * purpose of putting the other devices into Bypass mode. The
  2068. * current state is checked to find out if it is at DRPAUSE or
  2069. * IRPAUSE. If it is at DRPAUSE, perform bypass register scan.
  2070. * If it is at IRPAUSE, scan into instruction registers the bypass
  2071. * instruction.
  2072. *
  2073. */
  2074. void ispVMBypass(signed char ScanType, unsigned short Bits)
  2075. {
  2076. /* 09/11/07 NN added local variables initialization */
  2077. unsigned short iIndex = 0;
  2078. unsigned short iSourceIndex = 0;
  2079. unsigned char cBitState = 0;
  2080. unsigned char cCurByte = 0;
  2081. unsigned char *pcSource = NULL;
  2082. if (Bits <= 0) {
  2083. return;
  2084. }
  2085. switch (ScanType) {
  2086. case HIR:
  2087. pcSource = g_pucHIRData;
  2088. break;
  2089. case TIR:
  2090. pcSource = g_pucTIRData;
  2091. break;
  2092. case HDR:
  2093. pcSource = g_pucHDRData;
  2094. break;
  2095. case TDR:
  2096. pcSource = g_pucTDRData;
  2097. break;
  2098. default:
  2099. break;
  2100. }
  2101. iSourceIndex = 0;
  2102. cBitState = 0;
  2103. for (iIndex = 0; iIndex < Bits - 1; iIndex++) {
  2104. /* Scan instruction or bypass register */
  2105. if (iIndex % 8 == 0) {
  2106. cCurByte = pcSource[iSourceIndex++];
  2107. }
  2108. cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
  2109. ? 0x01 : 0x00);
  2110. writePort(g_ucPinTDI, cBitState);
  2111. sclock();
  2112. }
  2113. if (iIndex % 8 == 0) {
  2114. cCurByte = pcSource[iSourceIndex++];
  2115. }
  2116. cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
  2117. ? 0x01 : 0x00);
  2118. writePort(g_ucPinTDI, cBitState);
  2119. }
  2120. /*
  2121. *
  2122. * ispVMStateMachine
  2123. *
  2124. * This procedure steps all devices in the daisy chain from a given
  2125. * JTAG state to the next desirable state. If the next state is TLR,
  2126. * the JTAG state machine is brute forced into TLR by driving TMS
  2127. * high and pulse TCK 6 times.
  2128. *
  2129. */
  2130. void ispVMStateMachine(signed char cNextJTAGState)
  2131. {
  2132. /* 09/11/07 NN added local variables initialization */
  2133. signed char cPathIndex = 0;
  2134. signed char cStateIndex = 0;
  2135. if ((g_cCurrentJTAGState == cNextJTAGState) &&
  2136. (cNextJTAGState != RESET)) {
  2137. return;
  2138. }
  2139. for (cStateIndex = 0; cStateIndex < 25; cStateIndex++) {
  2140. if ((g_cCurrentJTAGState ==
  2141. g_JTAGTransistions[cStateIndex].CurState) &&
  2142. (cNextJTAGState ==
  2143. g_JTAGTransistions[cStateIndex].NextState)) {
  2144. break;
  2145. }
  2146. }
  2147. g_cCurrentJTAGState = cNextJTAGState;
  2148. for (cPathIndex = 0;
  2149. cPathIndex < g_JTAGTransistions[cStateIndex].Pulses;
  2150. cPathIndex++) {
  2151. if ((g_JTAGTransistions[cStateIndex].Pattern << cPathIndex)
  2152. & 0x80) {
  2153. writePort(g_ucPinTMS, (unsigned char) 0x01);
  2154. } else {
  2155. writePort(g_ucPinTMS, (unsigned char) 0x00);
  2156. }
  2157. sclock();
  2158. }
  2159. writePort(g_ucPinTDI, 0x00);
  2160. writePort(g_ucPinTMS, 0x00);
  2161. }
  2162. /*
  2163. *
  2164. * ispVMStart
  2165. *
  2166. * Enable the port to the device and set the state to RESET (TLR).
  2167. *
  2168. */
  2169. void ispVMStart()
  2170. {
  2171. #ifdef DEBUG
  2172. printf("// ISPVM EMBEDDED ADDED\n");
  2173. printf("STATE RESET;\n");
  2174. #endif
  2175. g_usFlowControl = 0;
  2176. g_usDataType = g_uiChecksumIndex = g_cCurrentJTAGState = 0;
  2177. g_usHeadDR = g_usHeadIR = g_usTailDR = g_usTailIR = 0;
  2178. g_usMaxSize = g_usShiftValue = g_usRepeatLoops = 0;
  2179. g_usTDOSize = g_usMASKSize = g_usTDISize = 0;
  2180. g_usDMASKSize = g_usLCOUNTSize = g_usHDRSize = 0;
  2181. g_usTDRSize = g_usHIRSize = g_usTIRSize = g_usHeapSize = 0;
  2182. g_pLVDSList = NULL;
  2183. g_usLVDSPairCount = 0;
  2184. previous_size = 0;
  2185. ispVMStateMachine(RESET); /*step devices to RESET state*/
  2186. }
  2187. /*
  2188. *
  2189. * ispVMEnd
  2190. *
  2191. * Set the state of devices to RESET to enable the devices and disable
  2192. * the port.
  2193. *
  2194. */
  2195. void ispVMEnd()
  2196. {
  2197. #ifdef DEBUG
  2198. printf("// ISPVM EMBEDDED ADDED\n");
  2199. printf("STATE RESET;\n");
  2200. printf("RUNTEST 1.00E-001 SEC;\n");
  2201. #endif
  2202. ispVMStateMachine(RESET); /*step devices to RESET state */
  2203. ispVMDelay(1000); /*wake up devices*/
  2204. }
  2205. /*
  2206. *
  2207. * ispVMSend
  2208. *
  2209. * Send the TDI data stream to devices. The data stream can be
  2210. * instructions or data.
  2211. *
  2212. */
  2213. signed char ispVMSend(unsigned short a_usiDataSize)
  2214. {
  2215. /* 09/11/07 NN added local variables initialization */
  2216. unsigned short iIndex = 0;
  2217. unsigned short iInDataIndex = 0;
  2218. unsigned char cCurByte = 0;
  2219. unsigned char cBitState = 0;
  2220. for (iIndex = 0; iIndex < a_usiDataSize - 1; iIndex++) {
  2221. if (iIndex % 8 == 0) {
  2222. cCurByte = g_pucInData[iInDataIndex++];
  2223. }
  2224. cBitState = (unsigned char)(((cCurByte << iIndex % 8) & 0x80)
  2225. ? 0x01 : 0x00);
  2226. writePort(g_ucPinTDI, cBitState);
  2227. sclock();
  2228. }
  2229. if (iIndex % 8 == 0) {
  2230. /* Take care of the last bit */
  2231. cCurByte = g_pucInData[iInDataIndex];
  2232. }
  2233. cBitState = (unsigned char) (((cCurByte << iIndex % 8) & 0x80)
  2234. ? 0x01 : 0x00);
  2235. writePort(g_ucPinTDI, cBitState);
  2236. if (g_usFlowControl & CASCADE) {
  2237. /*1/15/04 Clock in last bit for the first n-1 cascaded frames */
  2238. sclock();
  2239. }
  2240. return 0;
  2241. }
  2242. /*
  2243. *
  2244. * ispVMRead
  2245. *
  2246. * Read the data stream from devices and verify.
  2247. *
  2248. */
  2249. signed char ispVMRead(unsigned short a_usiDataSize)
  2250. {
  2251. /* 09/11/07 NN added local variables initialization */
  2252. unsigned short usDataSizeIndex = 0;
  2253. unsigned short usErrorCount = 0;
  2254. unsigned short usLastBitIndex = 0;
  2255. unsigned char cDataByte = 0;
  2256. unsigned char cMaskByte = 0;
  2257. unsigned char cInDataByte = 0;
  2258. unsigned char cCurBit = 0;
  2259. unsigned char cByteIndex = 0;
  2260. unsigned short usBufferIndex = 0;
  2261. unsigned char ucDisplayByte = 0x00;
  2262. unsigned char ucDisplayFlag = 0x01;
  2263. char StrChecksum[256] = {0};
  2264. unsigned char g_usCalculateChecksum = 0x00;
  2265. /* 09/11/07 NN Type cast mismatch variables */
  2266. usLastBitIndex = (unsigned short)(a_usiDataSize - 1);
  2267. #ifndef DEBUG
  2268. /*
  2269. * If mask is not all zeros, then set the display flag to 0x00,
  2270. * otherwise it shall be set to 0x01 to indicate that data read
  2271. * from the device shall be displayed. If DEBUG is defined,
  2272. * always display data.
  2273. */
  2274. for (usDataSizeIndex = 0; usDataSizeIndex < (a_usiDataSize + 7) / 8;
  2275. usDataSizeIndex++) {
  2276. if (g_usDataType & MASK_DATA) {
  2277. if (g_pucOutMaskData[usDataSizeIndex] != 0x00) {
  2278. ucDisplayFlag = 0x00;
  2279. break;
  2280. }
  2281. } else if (g_usDataType & CMASK_DATA) {
  2282. g_usCalculateChecksum = 0x01;
  2283. ucDisplayFlag = 0x00;
  2284. break;
  2285. } else {
  2286. ucDisplayFlag = 0x00;
  2287. break;
  2288. }
  2289. }
  2290. #endif /* DEBUG */
  2291. /*
  2292. *
  2293. * Begin shifting data in and out of the device.
  2294. *
  2295. **/
  2296. for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
  2297. usDataSizeIndex++) {
  2298. if (cByteIndex == 0) {
  2299. /*
  2300. * Grab byte from TDO buffer.
  2301. */
  2302. if (g_usDataType & TDO_DATA) {
  2303. cDataByte = g_pucOutData[usBufferIndex];
  2304. }
  2305. /*
  2306. * Grab byte from MASK buffer.
  2307. */
  2308. if (g_usDataType & MASK_DATA) {
  2309. cMaskByte = g_pucOutMaskData[usBufferIndex];
  2310. } else {
  2311. cMaskByte = 0xFF;
  2312. }
  2313. /*
  2314. * Grab byte from CMASK buffer.
  2315. */
  2316. if (g_usDataType & CMASK_DATA) {
  2317. cMaskByte = 0x00;
  2318. g_usCalculateChecksum = 0x01;
  2319. }
  2320. /*
  2321. * Grab byte from TDI buffer.
  2322. */
  2323. if (g_usDataType & TDI_DATA) {
  2324. cInDataByte = g_pucInData[usBufferIndex];
  2325. }
  2326. usBufferIndex++;
  2327. }
  2328. cCurBit = readPort();
  2329. if (ucDisplayFlag) {
  2330. ucDisplayByte <<= 1;
  2331. ucDisplayByte |= cCurBit;
  2332. }
  2333. /*
  2334. * Check if data read from port matches with expected TDO.
  2335. */
  2336. if (g_usDataType & TDO_DATA) {
  2337. /* 08/28/08 NN Added Calculate checksum support. */
  2338. if (g_usCalculateChecksum) {
  2339. if (cCurBit == 0x01)
  2340. g_usChecksum +=
  2341. (1 << (g_uiChecksumIndex % 8));
  2342. g_uiChecksumIndex++;
  2343. } else {
  2344. if ((((cMaskByte << cByteIndex) & 0x80)
  2345. ? 0x01 : 0x00)) {
  2346. if (cCurBit != (unsigned char)
  2347. (((cDataByte << cByteIndex) & 0x80)
  2348. ? 0x01 : 0x00)) {
  2349. usErrorCount++;
  2350. }
  2351. }
  2352. }
  2353. }
  2354. /*
  2355. * Write TDI data to the port.
  2356. */
  2357. writePort(g_ucPinTDI,
  2358. (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
  2359. ? 0x01 : 0x00));
  2360. if (usDataSizeIndex < usLastBitIndex) {
  2361. /*
  2362. * Clock data out from the data shift register.
  2363. */
  2364. sclock();
  2365. } else if (g_usFlowControl & CASCADE) {
  2366. /*
  2367. * Clock in last bit for the first N - 1 cascaded frames
  2368. */
  2369. sclock();
  2370. }
  2371. /*
  2372. * Increment the byte index. If it exceeds 7, then reset it back
  2373. * to zero.
  2374. */
  2375. cByteIndex++;
  2376. if (cByteIndex >= 8) {
  2377. if (ucDisplayFlag) {
  2378. /*
  2379. * Store displayed data in the TDO buffer. By reusing
  2380. * the TDO buffer to store displayed data, there is no
  2381. * need to allocate a buffer simply to hold display
  2382. * data. This will not cause any false verification
  2383. * errors because the true TDO byte has already
  2384. * been consumed.
  2385. */
  2386. g_pucOutData[usBufferIndex - 1] = ucDisplayByte;
  2387. ucDisplayByte = 0;
  2388. }
  2389. cByteIndex = 0;
  2390. }
  2391. /* 09/12/07 Nguyen changed to display the 1 bit expected data */
  2392. else if (a_usiDataSize == 1) {
  2393. if (ucDisplayFlag) {
  2394. /*
  2395. * Store displayed data in the TDO buffer.
  2396. * By reusing the TDO buffer to store displayed
  2397. * data, there is no need to allocate
  2398. * a buffer simply to hold display data. This
  2399. * will not cause any false verification errors
  2400. * because the true TDO byte has already
  2401. * been consumed.
  2402. */
  2403. /*
  2404. * Flip ucDisplayByte and store it in cDataByte.
  2405. */
  2406. cDataByte = 0x00;
  2407. for (usBufferIndex = 0; usBufferIndex < 8;
  2408. usBufferIndex++) {
  2409. cDataByte <<= 1;
  2410. if (ucDisplayByte & 0x01) {
  2411. cDataByte |= 0x01;
  2412. }
  2413. ucDisplayByte >>= 1;
  2414. }
  2415. g_pucOutData[0] = cDataByte;
  2416. ucDisplayByte = 0;
  2417. }
  2418. cByteIndex = 0;
  2419. }
  2420. }
  2421. if (ucDisplayFlag) {
  2422. #ifdef DEBUG
  2423. debug("RECEIVED TDO (");
  2424. #else
  2425. vme_out_string("Display Data: 0x");
  2426. #endif /* DEBUG */
  2427. /* 09/11/07 NN Type cast mismatch variables */
  2428. for (usDataSizeIndex = (unsigned short)
  2429. ((a_usiDataSize + 7) / 8);
  2430. usDataSizeIndex > 0 ; usDataSizeIndex--) {
  2431. cMaskByte = g_pucOutData[usDataSizeIndex - 1];
  2432. cDataByte = 0x00;
  2433. /*
  2434. * Flip cMaskByte and store it in cDataByte.
  2435. */
  2436. for (usBufferIndex = 0; usBufferIndex < 8;
  2437. usBufferIndex++) {
  2438. cDataByte <<= 1;
  2439. if (cMaskByte & 0x01) {
  2440. cDataByte |= 0x01;
  2441. }
  2442. cMaskByte >>= 1;
  2443. }
  2444. #ifdef DEBUG
  2445. printf("%.2X", cDataByte);
  2446. if ((((a_usiDataSize + 7) / 8) - usDataSizeIndex)
  2447. % 40 == 39) {
  2448. printf("\n\t\t");
  2449. }
  2450. #else
  2451. vme_out_hex(cDataByte);
  2452. #endif /* DEBUG */
  2453. }
  2454. #ifdef DEBUG
  2455. printf(")\n\n");
  2456. #else
  2457. vme_out_string("\n\n");
  2458. #endif /* DEBUG */
  2459. /* 09/02/08 Nguyen changed to display the data Checksum */
  2460. if (g_usChecksum != 0) {
  2461. g_usChecksum &= 0xFFFF;
  2462. sprintf(StrChecksum, "Data Checksum: %.4lX\n\n",
  2463. g_usChecksum);
  2464. vme_out_string(StrChecksum);
  2465. g_usChecksum = 0;
  2466. }
  2467. }
  2468. if (usErrorCount > 0) {
  2469. if (g_usFlowControl & VERIFYUES) {
  2470. vme_out_string(
  2471. "USERCODE verification failed. "
  2472. "Continue programming......\n\n");
  2473. g_usFlowControl &= ~(VERIFYUES);
  2474. return 0;
  2475. } else {
  2476. #ifdef DEBUG
  2477. printf("TOTAL ERRORS: %d\n", usErrorCount);
  2478. #endif /* DEBUG */
  2479. return VME_VERIFICATION_FAILURE;
  2480. }
  2481. } else {
  2482. if (g_usFlowControl & VERIFYUES) {
  2483. vme_out_string("USERCODE verification passed. "
  2484. "Programming aborted.\n\n");
  2485. g_usFlowControl &= ~(VERIFYUES);
  2486. return 1;
  2487. } else {
  2488. return 0;
  2489. }
  2490. }
  2491. }
  2492. /*
  2493. *
  2494. * ispVMReadandSave
  2495. *
  2496. * Support dynamic I/O.
  2497. *
  2498. */
  2499. signed char ispVMReadandSave(unsigned short int a_usiDataSize)
  2500. {
  2501. /* 09/11/07 NN added local variables initialization */
  2502. unsigned short int usDataSizeIndex = 0;
  2503. unsigned short int usLastBitIndex = 0;
  2504. unsigned short int usBufferIndex = 0;
  2505. unsigned short int usOutBitIndex = 0;
  2506. unsigned short int usLVDSIndex = 0;
  2507. unsigned char cDataByte = 0;
  2508. unsigned char cDMASKByte = 0;
  2509. unsigned char cInDataByte = 0;
  2510. unsigned char cCurBit = 0;
  2511. unsigned char cByteIndex = 0;
  2512. signed char cLVDSByteIndex = 0;
  2513. /* 09/11/07 NN Type cast mismatch variables */
  2514. usLastBitIndex = (unsigned short) (a_usiDataSize - 1);
  2515. /*
  2516. *
  2517. * Iterate through the data bits.
  2518. *
  2519. */
  2520. for (usDataSizeIndex = 0; usDataSizeIndex < a_usiDataSize;
  2521. usDataSizeIndex++) {
  2522. if (cByteIndex == 0) {
  2523. /*
  2524. * Grab byte from DMASK buffer.
  2525. */
  2526. if (g_usDataType & DMASK_DATA) {
  2527. cDMASKByte = g_pucOutDMaskData[usBufferIndex];
  2528. } else {
  2529. cDMASKByte = 0x00;
  2530. }
  2531. /*
  2532. * Grab byte from TDI buffer.
  2533. */
  2534. if (g_usDataType & TDI_DATA) {
  2535. cInDataByte = g_pucInData[usBufferIndex];
  2536. }
  2537. usBufferIndex++;
  2538. }
  2539. cCurBit = readPort();
  2540. cDataByte = (unsigned char)(((cInDataByte << cByteIndex) & 0x80)
  2541. ? 0x01 : 0x00);
  2542. /*
  2543. * Initialize the byte to be zero.
  2544. */
  2545. if (usOutBitIndex % 8 == 0) {
  2546. g_pucOutData[usOutBitIndex / 8] = 0x00;
  2547. }
  2548. /*
  2549. * Use TDI, DMASK, and device TDO to create new TDI (actually
  2550. * stored in g_pucOutData).
  2551. */
  2552. if ((((cDMASKByte << cByteIndex) & 0x80) ? 0x01 : 0x00)) {
  2553. if (g_pLVDSList) {
  2554. for (usLVDSIndex = 0;
  2555. usLVDSIndex < g_usLVDSPairCount;
  2556. usLVDSIndex++) {
  2557. if (g_pLVDSList[usLVDSIndex].
  2558. usNegativeIndex ==
  2559. usDataSizeIndex) {
  2560. g_pLVDSList[usLVDSIndex].
  2561. ucUpdate = 0x01;
  2562. break;
  2563. }
  2564. }
  2565. }
  2566. /*
  2567. * DMASK bit is 1, use TDI.
  2568. */
  2569. g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
  2570. (((cDataByte & 0x1) ? 0x01 : 0x00) <<
  2571. (7 - usOutBitIndex % 8));
  2572. } else {
  2573. /*
  2574. * DMASK bit is 0, use device TDO.
  2575. */
  2576. g_pucOutData[usOutBitIndex / 8] |= (unsigned char)
  2577. (((cCurBit & 0x1) ? 0x01 : 0x00) <<
  2578. (7 - usOutBitIndex % 8));
  2579. }
  2580. /*
  2581. * Shift in TDI in order to get TDO out.
  2582. */
  2583. usOutBitIndex++;
  2584. writePort(g_ucPinTDI, cDataByte);
  2585. if (usDataSizeIndex < usLastBitIndex) {
  2586. sclock();
  2587. }
  2588. /*
  2589. * Increment the byte index. If it exceeds 7, then reset it back
  2590. * to zero.
  2591. */
  2592. cByteIndex++;
  2593. if (cByteIndex >= 8) {
  2594. cByteIndex = 0;
  2595. }
  2596. }
  2597. /*
  2598. * If g_pLVDSList exists and pairs need updating, then update
  2599. * the negative-pair to receive the flipped positive-pair value.
  2600. */
  2601. if (g_pLVDSList) {
  2602. for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount;
  2603. usLVDSIndex++) {
  2604. if (g_pLVDSList[usLVDSIndex].ucUpdate) {
  2605. /*
  2606. * Read the positive value and flip it.
  2607. */
  2608. cDataByte = (unsigned char)
  2609. (((g_pucOutData[g_pLVDSList[usLVDSIndex].
  2610. usPositiveIndex / 8]
  2611. << (g_pLVDSList[usLVDSIndex].
  2612. usPositiveIndex % 8)) & 0x80) ?
  2613. 0x01 : 0x00);
  2614. /* 09/11/07 NN Type cast mismatch variables */
  2615. cDataByte = (unsigned char) (!cDataByte);
  2616. /*
  2617. * Get the byte that needs modification.
  2618. */
  2619. cInDataByte =
  2620. g_pucOutData[g_pLVDSList[usLVDSIndex].
  2621. usNegativeIndex / 8];
  2622. if (cDataByte) {
  2623. /*
  2624. * Copy over the current byte and
  2625. * set the negative bit to 1.
  2626. */
  2627. cDataByte = 0x00;
  2628. for (cLVDSByteIndex = 7;
  2629. cLVDSByteIndex >= 0;
  2630. cLVDSByteIndex--) {
  2631. cDataByte <<= 1;
  2632. if (7 -
  2633. (g_pLVDSList[usLVDSIndex].
  2634. usNegativeIndex % 8) ==
  2635. cLVDSByteIndex) {
  2636. /*
  2637. * Set negative bit to 1
  2638. */
  2639. cDataByte |= 0x01;
  2640. } else if (cInDataByte & 0x80) {
  2641. cDataByte |= 0x01;
  2642. }
  2643. cInDataByte <<= 1;
  2644. }
  2645. /*
  2646. * Store the modified byte.
  2647. */
  2648. g_pucOutData[g_pLVDSList[usLVDSIndex].
  2649. usNegativeIndex / 8] = cDataByte;
  2650. } else {
  2651. /*
  2652. * Copy over the current byte and set
  2653. * the negative bit to 0.
  2654. */
  2655. cDataByte = 0x00;
  2656. for (cLVDSByteIndex = 7;
  2657. cLVDSByteIndex >= 0;
  2658. cLVDSByteIndex--) {
  2659. cDataByte <<= 1;
  2660. if (7 -
  2661. (g_pLVDSList[usLVDSIndex].
  2662. usNegativeIndex % 8) ==
  2663. cLVDSByteIndex) {
  2664. /*
  2665. * Set negative bit to 0
  2666. */
  2667. cDataByte |= 0x00;
  2668. } else if (cInDataByte & 0x80) {
  2669. cDataByte |= 0x01;
  2670. }
  2671. cInDataByte <<= 1;
  2672. }
  2673. /*
  2674. * Store the modified byte.
  2675. */
  2676. g_pucOutData[g_pLVDSList[usLVDSIndex].
  2677. usNegativeIndex / 8] = cDataByte;
  2678. }
  2679. break;
  2680. }
  2681. }
  2682. }
  2683. return 0;
  2684. }
  2685. signed char ispVMProcessLVDS(unsigned short a_usLVDSCount)
  2686. {
  2687. unsigned short usLVDSIndex = 0;
  2688. /*
  2689. * Allocate memory to hold LVDS pairs.
  2690. */
  2691. ispVMMemManager(LVDS, a_usLVDSCount);
  2692. g_usLVDSPairCount = a_usLVDSCount;
  2693. #ifdef DEBUG
  2694. printf("LVDS %d (", a_usLVDSCount);
  2695. #endif /* DEBUG */
  2696. /*
  2697. * Iterate through each given LVDS pair.
  2698. */
  2699. for (usLVDSIndex = 0; usLVDSIndex < g_usLVDSPairCount; usLVDSIndex++) {
  2700. /*
  2701. * Assign the positive and negative indices of the LVDS pair.
  2702. */
  2703. /* 09/11/07 NN Type cast mismatch variables */
  2704. g_pLVDSList[usLVDSIndex].usPositiveIndex =
  2705. (unsigned short) ispVMDataSize();
  2706. /* 09/11/07 NN Type cast mismatch variables */
  2707. g_pLVDSList[usLVDSIndex].usNegativeIndex =
  2708. (unsigned short)ispVMDataSize();
  2709. #ifdef DEBUG
  2710. if (usLVDSIndex < g_usLVDSPairCount - 1) {
  2711. printf("%d:%d, ",
  2712. g_pLVDSList[usLVDSIndex].usPositiveIndex,
  2713. g_pLVDSList[usLVDSIndex].usNegativeIndex);
  2714. } else {
  2715. printf("%d:%d",
  2716. g_pLVDSList[usLVDSIndex].usPositiveIndex,
  2717. g_pLVDSList[usLVDSIndex].usNegativeIndex);
  2718. }
  2719. #endif /* DEBUG */
  2720. }
  2721. #ifdef DEBUG
  2722. printf(");\n", a_usLVDSCount);
  2723. #endif /* DEBUG */
  2724. return 0;
  2725. }