3 # Copyright 2008, SoftPLC Corporation http://softplc.com
4 # Dick Hollenbeck dick@softplc.com
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, you may find one here:
18 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 # or you may search the http://www.gnu.org website for the version 2 license,
20 # or you may write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 # Dump an Xilinx XSVF file to stdout
25 # This program is written for python 3.0, and it is not easy to change this
26 # back to 2.x. You may find it easier to use python 3.x even if that means
34 LABEL = "A script to dump an XSVF file to stdout"
40 (XCOMPLETE,XTDOMASK,XSIR,XSDR,XRUNTEST,hole0,hole1,XREPEAT,XSDRSIZE,XSDRTDO,
41 XSETSDRMASKS,XSDRINC,XSDRB,XSDRC,XSDRE,XSDRTDOB,XSDRTDOC,
42 XSDRTDOE,XSTATE,XENDIR,XENDDR,XSIR2,XCOMMENT,XWAIT,XWAITSTATE,
43 LCOUNT,LDELAY,LSDR,XTRST) = range(29)
47 DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
48 IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
51 State = ("RESET","IDLE",
52 "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
53 "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
56 trst_mode_allowed = ('ON', 'OFF', 'Z', 'ABSENT')
60 SetsdrmasksOnesCount = 0
62 def ReadSDRMASKS( f, len ):
63 global Setsdrmasks, SetsdrmasksOnesCount
64 byteCount = (len+7)//8
65 Setsdrmasks = f.read( byteCount )
67 SetsdrmasksOnesCount = 0
69 ls.append( "%x" % ((b & 0xf0) >> 4) )
70 ls.append( "%x" % ( b & 0x0f ) )
73 SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
77 def bytes2hexString( f, len ):
78 byteCount = (len+7)//8
79 bytebuf = f.read( byteCount )
82 ls.append( "%x" % ((b & 0xf0) >> 4) )
83 ls.append( "%x" % ( b & 0x0f ) )
88 """Read a byte from a file and return it as an int in least significant 8 bits"""
96 def ShowState( state ):
97 """return the given state int as a state string"""
98 #return "0x%02x" % state # comment this out to get textual state form
100 if 0 <= state <= IRUPDATE:
103 return "Unknown state 0x%02x" % state
106 def ShowOpcode( op, f ):
107 """return the given byte as an opcode string"""
113 buf = bytes2hexString( f, Xsdrsize )
114 print("XTDOMASK 0x%s" % buf)
118 buf = bytes2hexString( f, len )
119 print("XSIR 0x%02X 0x%s" % (len, buf))
122 tdi = bytes2hexString( f, Xsdrsize )
123 print("XSDR 0x%s" % tdi)
126 len = struct.unpack( '>i', f.read(4) )[0]
127 print("XRUNTEST 0x%08X" % len)
131 print("XREPEAT 0x%02X" % len)
134 Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
135 #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
136 print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
139 tdi = bytes2hexString( f, Xsdrsize )
140 tdo = bytes2hexString( f, Xsdrsize )
141 print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
143 elif op == XSETSDRMASKS:
144 addrmask = bytes2hexString( f, Xsdrsize )
145 datamask = ReadSDRMASKS( f, Xsdrsize )
146 print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
149 startaddr = bytes2hexString( f, Xsdrsize )
151 print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
152 for numTimes in range(len):
153 data = bytes2hexString( f, SetsdrmasksOnesCount)
154 print(" 0x%s" % data )
158 tdi = bytes2hexString( f, Xsdrsize )
159 print("XSDRB 0x%s" % tdi )
162 tdi = bytes2hexString( f, Xsdrsize )
163 print("XSDRC 0x%s" % tdi )
166 tdi = bytes2hexString( f, Xsdrsize )
167 print("XSDRE 0x%s" % tdi )
170 tdo = bytes2hexString( f, Xsdrsize )
171 print("XSDRTDOB 0x%s" % tdo )
174 tdi = bytes2hexString( f, Xsdrsize )
175 tdo = bytes2hexString( f, Xsdrsize )
176 print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
179 tdi = bytes2hexString( f, Xsdrsize )
180 tdo = bytes2hexString( f, Xsdrsize )
181 print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
185 print("XSTATE %s" % ShowState(b))
189 print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
193 print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
196 len = struct.unpack( '>H', f.read(2) )[0]
197 buf = bytes2hexString( f, len )
198 print("XSIR2 0x%04X 0x%s" % (len, buf))
204 if b == 0: # terminating nul
207 print("XCOMMENT \"%s\"" % ''.join(cmt) )
210 run_state = ReadByte(f)
211 end_state = ReadByte(f)
212 useconds = struct.unpack( '>i', f.read(4) )[0]
213 print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
215 elif op == XWAITSTATE:
216 run_state = ReadByte(f)
217 end_state = ReadByte(f)
218 clocks = struct.unpack( '>i', f.read(4) )[0]
219 useconds = struct.unpack( '>i', f.read(4) )[0]
220 print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
223 loop_count = struct.unpack( '>i', f.read(4) )[0]
224 print("LCOUNT", loop_count )
227 run_state = ReadByte(f)
228 clocks = struct.unpack( '>i', f.read(4) )[0]
229 useconds = struct.unpack( '>i', f.read(4) )[0]
230 print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
233 tdi = bytes2hexString( f, Xsdrsize )
234 tdo = bytes2hexString( f, Xsdrsize )
235 print("LSDR 0x%s 0x%s" % (tdi, tdo) )
238 # the argument is a single byte and it is the index into "trst_mode_allowed"
239 trst_mode = ReadByte(f)
241 print("TRST %s" % trst_mode_allowed[trst_mode] )
243 print("TRST 0x%02X" % trst_mode );
246 print("UNKNOWN op 0x%02X %d" % (op, op))
252 if len( sys.argv ) < 2:
253 print("usage %s <xsvf_filename>" % sys.argv[0])
256 f = open( sys.argv[1], 'rb' )
258 opcode = ReadByte( f )
260 # print the position within the file, then the command
261 print( "%d: " % f.tell(), end='' )
262 ShowOpcode( opcode, f )
266 if __name__ == "__main__":