1 /* JTAG routines 2 3 Copyright (C) 2004 Andrew Rogers 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 Changes: 20 Dmitry Teytelman [dimtey@gmail.com] 14 Jun 2006 [applied 13 Aug 2006]: 21 Code cleanup for clean -Wall compile. 22 */ 23 24 25 26 #ifndef JTAG_H 27 #define JTAG_H 28 29 #include <stdio.h> 30 #include <stdint.h> 31 #include <vector> 32 33 #include "iobase.h" 34 #include "bitrev.h" 35 36 #ifdef __WIN32__ 37 #include <windows.h> 38 #endif 39 40 typedef unsigned char byte; 41 typedef uint32_t DeviceID; 42 43 class Jtag 44 { 45 public: 46 enum tapState_t{ 47 TEST_LOGIC_RESET=0, 48 RUN_TEST_IDLE=1, 49 SELECT_DR_SCAN=2, 50 CAPTURE_DR=3, 51 SHIFT_DR=4, 52 EXIT1_DR=5, 53 PAUSE_DR=6, 54 EXIT2_DR=7, 55 UPDATE_DR=8, 56 SELECT_IR_SCAN=9, 57 CAPTURE_IR=10, 58 SHIFT_IR=11, 59 EXIT1_IR=12, 60 PAUSE_IR=13, 61 EXIT2_IR=14, 62 UPDATE_IR=15, 63 UNKNOWN=999 64 }; 65 66 private: 67 bool verbose; 68 tapState_t current_state; 69 static const int MAXNUMDEVICES=1000; 70 protected: 71 struct chainParam_t 72 { 73 DeviceID idcode; // Store IDCODE 74 //byte bypass[4]; // The bypass instruction. Most instruction register lengths are a lot less than 32 bits. 75 int irlen; // instruction register length. 76 }; 77 std::vector<chainParam_t> devices; 78 IOBase *io; 79 int numDevices; 80 tapState_t postDRState; 81 tapState_t postIRState; 82 int deviceIndex; 83 FILE *fp_svf; 84 bool shiftDRincomplete; 85 FILE *fp_dbg; 86 const char* getStateName(tapState_t s); 87 public: 88 Jtag(IOBase *iob); 89 ~Jtag(); setVerbose(bool v)90 void setVerbose(bool v) { verbose = v; } getVerbose(void)91 bool getVerbose(void) { return verbose; } 92 int getChain(bool detect = false); // Shift IDCODEs from devices setPostDRState(tapState_t s)93 inline void setPostDRState(tapState_t s){postDRState=s;} setPostIRState(tapState_t s)94 inline void setPostIRState(tapState_t s){postIRState=s;} 95 void setTapState(tapState_t state, int pre=0); 96 void tapTestLogicReset(void); 97 void nextTapState(bool tms); 98 void cycleTCK(int n, bool tdi=1); 99 tapState_t getTapState(void); 100 int setDeviceIRLength(int dev, int len); getDeviceID(unsigned int dev)101 DeviceID getDeviceID(unsigned int dev){ 102 if(dev>=devices.size())return 0; 103 return devices[dev].idcode; 104 } Usleep(unsigned int usec)105 void Usleep(unsigned int usec) {io->Usleep(usec);} 106 int selectDevice(int dev); 107 void shiftDR(const byte *tdi, byte *tdo, int length, int align=0, bool exit=true);// Some devices use TCK for aligning data, for example, Xilinx FPGAs for configuration data. 108 void shiftIR(const byte *tdi, byte *tdo=0); // No length argumant required as IR length specified in chainParam_t longToByteArray(unsigned long l,byte * b)109 inline void longToByteArray(unsigned long l, byte *b){ 110 b[0]=(byte)(l&0xff); 111 b[1]=(byte)((l>>8)&0xff); 112 b[2]=(byte)((l>>16)&0xff); 113 b[3]=(byte)((l>>24)&0xff); 114 } longToByteArrayRev(unsigned long l,byte * b)115 inline void longToByteArrayRev(unsigned long l, byte *b){ 116 b[0]=bitRevTable[ l & 0xff]; 117 b[1]=bitRevTable[(l>> 8) & 0xff]; 118 b[2]=bitRevTable[(l>>16) & 0xff]; 119 b[3]=bitRevTable[(l>>24) & 0xff]; 120 } shortToByteArray(const unsigned short l,byte * b)121 inline void shortToByteArray(const unsigned short l, byte *b){ 122 b[0]=(byte)(l&0xff); 123 b[1]=(byte)((l>>8)&0xff); 124 } byteArrayToLong(const byte * b)125 inline unsigned long byteArrayToLong(const byte *b){ 126 return ((unsigned long)b[3]<<24)+((unsigned long)b[2]<<16)+ 127 ((unsigned long)b[1]<<8)+(unsigned long)b[0]; 128 } byteArrayToShort(const byte * b)129 static inline uint16_t byteArrayToShort(const byte *b) { 130 return ((uint16_t)b[0]) | (((uint16_t)b[1]) << 8); 131 } 132 }; 133 134 #endif //JTAG_H 135