|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #!/usr/bin/python3.0
-
- # Copyright 2008, SoftPLC Corporation http://softplc.com
- # Dick Hollenbeck dick@softplc.com
-
- # This program is free software; you can redistribute it and/or
- # modify it under the terms of the GNU General Public License
- # as published by the Free Software Foundation; either version 2
- # of the License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, you may find one here:
- # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
- # or you may search the http://www.gnu.org website for the version 2 license,
- # or you may write to the Free Software Foundation, Inc.,
- # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
-
- # Dump an Xilinx XSVF file to stdout
-
- # This program is written for python 3.0, and it is not easy to change this
- # back to 2.x. You may find it easier to use python 3.x even if that means
- # building it.
-
-
- import sys
- import struct
-
-
- LABEL = "A script to dump an XSVF file to stdout"
-
-
- Xsdrsize = 0
-
-
- (XCOMPLETE,XTDOMASK,XSIR,XSDR,XRUNTEST,hole0,hole1,XREPEAT,XSDRSIZE,XSDRTDO,
- XSETSDRMASKS,XSDRINC,XSDRB,XSDRC,XSDRE,XSDRTDOB,XSDRTDOC,
- XSDRTDOE,XSTATE,XENDIR,XENDDR,XSIR2,XCOMMENT,XWAIT,XWAITSTATE,
- LCOUNT,LDELAY,LSDR,XTRST) = range(29)
-
-
- (RESET,IDLE,
- DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
- IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
-
-
- State = ("RESET","IDLE",
- "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
- "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
-
-
- trst_mode_allowed = ('ON', 'OFF', 'Z', 'ABSENT')
-
-
- Setsdrmasks = 0
- SetsdrmasksOnesCount = 0
-
- def ReadSDRMASKS( f, len ):
- global Setsdrmasks, SetsdrmasksOnesCount
- byteCount = (len+7)//8
- Setsdrmasks = f.read( byteCount )
- ls = []
- SetsdrmasksOnesCount = 0
- for b in Setsdrmasks:
- ls.append( "%x" % ((b & 0xf0) >> 4) )
- ls.append( "%x" % ( b & 0x0f ) )
- for i in range(8):
- if b & (1<<i):
- SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
- return ''.join(ls)
-
-
- def bytes2hexString( f, len ):
- byteCount = (len+7)//8
- bytebuf = f.read( byteCount )
- ls = []
- for b in bytebuf:
- ls.append( "%x" % ((b & 0xf0) >> 4) )
- ls.append( "%x" % ( b & 0x0f ) )
- return ''.join(ls)
-
-
- def ReadByte( f ):
- """Read a byte from a file and return it as an int in least significant 8 bits"""
- b = f.read(1)
- if b:
- return 0xff & b[0];
- else:
- return -1
-
-
- def ShowState( state ):
- """return the given state int as a state string"""
- #return "0x%02x" % state # comment this out to get textual state form
- global State
- if 0 <= state <= IRUPDATE:
- return State[state]
- else:
- return "Unknown state 0x%02x" % state
-
-
- def ShowOpcode( op, f ):
- """return the given byte as an opcode string"""
- global Xsdrsize
- if op == XCOMPLETE:
- print("XCOMPLETE")
-
- elif op == XTDOMASK:
- buf = bytes2hexString( f, Xsdrsize )
- print("XTDOMASK 0x%s" % buf)
-
- elif op == XSIR:
- len = ReadByte( f )
- buf = bytes2hexString( f, len )
- print("XSIR 0x%02X 0x%s" % (len, buf))
-
- elif op == XSDR:
- tdi = bytes2hexString( f, Xsdrsize )
- print("XSDR 0x%s" % tdi)
-
- elif op == XRUNTEST:
- len = struct.unpack( '>i', f.read(4) )[0]
- print("XRUNTEST 0x%08X" % len)
-
- elif op == XREPEAT:
- len = ReadByte( f )
- print("XREPEAT 0x%02X" % len)
-
- elif op == XSDRSIZE:
- Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
- #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
- print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
-
- elif op == XSDRTDO:
- tdi = bytes2hexString( f, Xsdrsize )
- tdo = bytes2hexString( f, Xsdrsize )
- print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
-
- elif op == XSETSDRMASKS:
- addrmask = bytes2hexString( f, Xsdrsize )
- datamask = ReadSDRMASKS( f, Xsdrsize )
- print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
-
- elif op == XSDRINC:
- startaddr = bytes2hexString( f, Xsdrsize )
- len = ReadByte(f)
- print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
- for numTimes in range(len):
- data = bytes2hexString( f, SetsdrmasksOnesCount)
- print(" 0x%s" % data )
- print() # newline
-
- elif op == XSDRB:
- tdi = bytes2hexString( f, Xsdrsize )
- print("XSDRB 0x%s" % tdi )
-
- elif op == XSDRC:
- tdi = bytes2hexString( f, Xsdrsize )
- print("XSDRC 0x%s" % tdi )
-
- elif op == XSDRE:
- tdi = bytes2hexString( f, Xsdrsize )
- print("XSDRE 0x%s" % tdi )
-
- elif op == XSDRTDOB:
- tdo = bytes2hexString( f, Xsdrsize )
- print("XSDRTDOB 0x%s" % tdo )
-
- elif op == XSDRTDOC:
- tdi = bytes2hexString( f, Xsdrsize )
- tdo = bytes2hexString( f, Xsdrsize )
- print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
-
- elif op == XSDRTDOE:
- tdi = bytes2hexString( f, Xsdrsize )
- tdo = bytes2hexString( f, Xsdrsize )
- print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
-
- elif op == XSTATE:
- b = ReadByte(f)
- print("XSTATE %s" % ShowState(b))
-
- elif op == XENDIR:
- b = ReadByte( f )
- print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
-
- elif op == XENDDR:
- b = ReadByte( f )
- print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
-
- elif op == XSIR2:
- len = struct.unpack( '>H', f.read(2) )[0]
- buf = bytes2hexString( f, len )
- print("XSIR2 0x%04X 0x%s" % (len, buf))
-
- elif op == XCOMMENT:
- cmt = []
- while 1:
- b = ReadByte(f)
- if b == 0: # terminating nul
- break;
- cmt.append( chr(b) )
- print("XCOMMENT \"%s\"" % ''.join(cmt) )
-
- elif op == XWAIT:
- run_state = ReadByte(f)
- end_state = ReadByte(f)
- useconds = struct.unpack( '>i', f.read(4) )[0]
- print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
-
- elif op == XWAITSTATE:
- run_state = ReadByte(f)
- end_state = ReadByte(f)
- clocks = struct.unpack( '>i', f.read(4) )[0]
- useconds = struct.unpack( '>i', f.read(4) )[0]
- print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
-
- elif op == LCOUNT:
- loop_count = struct.unpack( '>i', f.read(4) )[0]
- print("LCOUNT", loop_count )
-
- elif op == LDELAY:
- run_state = ReadByte(f)
- clocks = struct.unpack( '>i', f.read(4) )[0]
- useconds = struct.unpack( '>i', f.read(4) )[0]
- print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
-
- elif op == LSDR:
- tdi = bytes2hexString( f, Xsdrsize )
- tdo = bytes2hexString( f, Xsdrsize )
- print("LSDR 0x%s 0x%s" % (tdi, tdo) )
-
- elif op == XTRST:
- # the argument is a single byte and it is the index into "trst_mode_allowed"
- trst_mode = ReadByte(f)
- if trst_mode <= 3:
- print("TRST %s" % trst_mode_allowed[trst_mode] )
- else:
- print("TRST 0x%02X" % trst_mode );
-
- else:
- print("UNKNOWN op 0x%02X %d" % (op, op))
- exit(1)
-
-
- def main():
-
- if len( sys.argv ) < 2:
- print("usage %s <xsvf_filename>" % sys.argv[0])
- exit(1)
-
- f = open( sys.argv[1], 'rb' )
-
- opcode = ReadByte( f )
- while opcode != -1:
- # print the position within the file, then the command
- print( "%d: " % f.tell(), end='' )
- ShowOpcode( opcode, f )
- opcode = ReadByte(f)
-
-
- if __name__ == "__main__":
- main()
-
|