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,LCOUNT,LDELAY,LSDR) = range(28)
46 DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
47 IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
50 State = ("RESET","IDLE",
51 "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
52 "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
55 SetsdrmasksOnesCount = 0
57 def ReadSDRMASKS( f, len ):
58 global Setsdrmasks, SetsdrmasksOnesCount
59 byteCount = (len+7)//8
60 Setsdrmasks = f.read( byteCount )
62 SetsdrmasksOnesCount = 0
64 ls.append( "%x" % ((b & 0xf0) >> 4) )
65 ls.append( "%x" % ( b & 0x0f ) )
68 SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
72 def bytes2hexString( f, len ):
73 byteCount = (len+7)//8
74 bytebuf = f.read( byteCount )
77 ls.append( "%x" % ((b & 0xf0) >> 4) )
78 ls.append( "%x" % ( b & 0x0f ) )
83 """Read a byte from a file and return it as an int in least significant 8 bits"""
91 def ShowState( state ):
92 """return the given state int as a state string"""
93 #return "0x%02x" % state # comment this out to get textual state form
95 if 0 <= state <= IRUPDATE:
98 return "Unknown state 0x%02x" % state
101 def ShowOpcode( op, f ):
102 """return the given byte as an opcode string"""
108 buf = bytes2hexString( f, Xsdrsize )
109 print("XTDOMASK 0x%s" % buf)
113 buf = bytes2hexString( f, len )
114 print("XSIR 0x%02X 0x%s" % (len, buf))
117 tdi = bytes2hexString( f, Xsdrsize )
118 print("XSDR 0x%s" % tdi)
121 len = struct.unpack( '>i', f.read(4) )[0]
122 print("XRUNTEST 0x%08X" % len)
126 print("XREPEAT 0x%02X" % len)
129 Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
130 #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
131 print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
134 tdi = bytes2hexString( f, Xsdrsize )
135 tdo = bytes2hexString( f, Xsdrsize )
136 print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
138 elif op == XSETSDRMASKS:
139 addrmask = bytes2hexString( f, Xsdrsize )
140 datamask = ReadSDRMASKS( f, Xsdrsize )
141 print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
144 startaddr = bytes2hexString( f, Xsdrsize )
146 print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
147 for numTimes in range(len):
148 data = bytes2hexString( f, SetsdrmasksOnesCount)
149 print(" 0x%s" % data )
153 tdi = bytes2hexString( f, Xsdrsize )
154 print("XSDRB 0x%s" % tdi )
157 tdi = bytes2hexString( f, Xsdrsize )
158 print("XSDRC 0x%s" % tdi )
161 tdi = bytes2hexString( f, Xsdrsize )
162 print("XSDRE 0x%s" % tdi )
165 tdo = bytes2hexString( f, Xsdrsize )
166 print("XSDRTDOB 0x%s" % tdo )
169 tdi = bytes2hexString( f, Xsdrsize )
170 tdo = bytes2hexString( f, Xsdrsize )
171 print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
174 tdi = bytes2hexString( f, Xsdrsize )
175 tdo = bytes2hexString( f, Xsdrsize )
176 print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
180 print("XSTATE %s" % ShowState(b))
184 print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
188 print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
191 len = struct.unpack( '>H', f.read(2) )[0]
192 buf = bytes2hexString( f, len )
193 print("XSIR2 0x%04X 0x%s" % (len, buf))
199 if b == 0: # terminating nul
202 print("XCOMMENT \"%s\"" % ''.join(cmt) )
205 run_state = ReadByte(f)
206 end_state = ReadByte(f)
207 useconds = struct.unpack( '>i', f.read(4) )[0]
208 print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
210 elif op == XWAITSTATE:
211 run_state = ReadByte(f)
212 end_state = ReadByte(f)
213 clocks = struct.unpack( '>i', f.read(4) )[0]
214 useconds = struct.unpack( '>i', f.read(4) )[0]
215 print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
218 loop_count = struct.unpack( '>i', f.read(4) )[0]
219 print("LCOUNT", loop_count )
222 run_state = ReadByte(f)
223 clocks = struct.unpack( '>i', f.read(4) )[0]
224 useconds = struct.unpack( '>i', f.read(4) )[0]
225 print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
228 tdi = bytes2hexString( f, Xsdrsize )
229 tdo = bytes2hexString( f, Xsdrsize )
230 print("LSDR 0x%s 0x%s" % (tdi, tdo) )
233 print("UNKNOWN op 0x%02X %d" % (op, op))
239 if len( sys.argv ) < 2:
240 print("usage %s <xsvf_filename>" % sys.argv[0])
243 f = open( sys.argv[1], 'rb' )
245 opcode = ReadByte( f )
247 # print the position within the file, then the command
248 print( "%d: " % f.tell(), end='' )
249 ShowOpcode( opcode, f )
253 if __name__ == "__main__":