You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

269 lines
7.8 KiB

  1. #!/usr/bin/python3.0
  2. # Copyright 2008, SoftPLC Corporation http://softplc.com
  3. # Dick Hollenbeck dick@softplc.com
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, you may find one here:
  16. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  17. # or you may search the http://www.gnu.org website for the version 2 license,
  18. # or you may write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. # Dump an Xilinx XSVF file to stdout
  21. # This program is written for python 3.0, and it is not easy to change this
  22. # back to 2.x. You may find it easier to use python 3.x even if that means
  23. # building it.
  24. import sys
  25. import struct
  26. LABEL = "A script to dump an XSVF file to stdout"
  27. Xsdrsize = 0
  28. (XCOMPLETE,XTDOMASK,XSIR,XSDR,XRUNTEST,hole0,hole1,XREPEAT,XSDRSIZE,XSDRTDO,
  29. XSETSDRMASKS,XSDRINC,XSDRB,XSDRC,XSDRE,XSDRTDOB,XSDRTDOC,
  30. XSDRTDOE,XSTATE,XENDIR,XENDDR,XSIR2,XCOMMENT,XWAIT,XWAITSTATE,
  31. LCOUNT,LDELAY,LSDR,XTRST) = range(29)
  32. (RESET,IDLE,
  33. DRSELECT,DRCAPTURE,DRSHIFT,DREXIT1,DRPAUSE,DREXIT2,DRUPDATE,
  34. IRSELECT,IRCAPTURE,IRSHIFT,IREXIT1,IRPAUSE,IREXIT2,IRUPDATE) = range(16)
  35. State = ("RESET","IDLE",
  36. "DRSELECT","DRCAPTURE","DRSHIFT","DREXIT1","DRPAUSE","DREXIT2","DRUPDATE",
  37. "IRSELECT","IRCAPTURE","IRSHIFT","IREXIT1","IRPAUSE","IREXIT2","IRUPDATE")
  38. trst_mode_allowed = ('ON', 'OFF', 'Z', 'ABSENT')
  39. Setsdrmasks = 0
  40. SetsdrmasksOnesCount = 0
  41. def ReadSDRMASKS( f, len ):
  42. global Setsdrmasks, SetsdrmasksOnesCount
  43. byteCount = (len+7)//8
  44. Setsdrmasks = f.read( byteCount )
  45. ls = []
  46. SetsdrmasksOnesCount = 0
  47. for b in Setsdrmasks:
  48. ls.append( "%x" % ((b & 0xf0) >> 4) )
  49. ls.append( "%x" % ( b & 0x0f ) )
  50. for i in range(8):
  51. if b & (1<<i):
  52. SetsdrmasksOnesCount = SetsdrmasksOnesCount +1
  53. return ''.join(ls)
  54. def bytes2hexString( f, len ):
  55. byteCount = (len+7)//8
  56. bytebuf = f.read( byteCount )
  57. ls = []
  58. for b in bytebuf:
  59. ls.append( "%x" % ((b & 0xf0) >> 4) )
  60. ls.append( "%x" % ( b & 0x0f ) )
  61. return ''.join(ls)
  62. def ReadByte( f ):
  63. """Read a byte from a file and return it as an int in least significant 8 bits"""
  64. b = f.read(1)
  65. if b:
  66. return 0xff & b[0];
  67. else:
  68. return -1
  69. def ShowState( state ):
  70. """return the given state int as a state string"""
  71. #return "0x%02x" % state # comment this out to get textual state form
  72. global State
  73. if 0 <= state <= IRUPDATE:
  74. return State[state]
  75. else:
  76. return "Unknown state 0x%02x" % state
  77. def ShowOpcode( op, f ):
  78. """return the given byte as an opcode string"""
  79. global Xsdrsize
  80. if op == XCOMPLETE:
  81. print("XCOMPLETE")
  82. elif op == XTDOMASK:
  83. buf = bytes2hexString( f, Xsdrsize )
  84. print("XTDOMASK 0x%s" % buf)
  85. elif op == XSIR:
  86. len = ReadByte( f )
  87. buf = bytes2hexString( f, len )
  88. print("XSIR 0x%02X 0x%s" % (len, buf))
  89. elif op == XSDR:
  90. tdi = bytes2hexString( f, Xsdrsize )
  91. print("XSDR 0x%s" % tdi)
  92. elif op == XRUNTEST:
  93. len = struct.unpack( '>i', f.read(4) )[0]
  94. print("XRUNTEST 0x%08X" % len)
  95. elif op == XREPEAT:
  96. len = ReadByte( f )
  97. print("XREPEAT 0x%02X" % len)
  98. elif op == XSDRSIZE:
  99. Xsdrsize = struct.unpack( '>i', f.read(4) )[0]
  100. #print("XSDRSIZE 0x%08X" % Xsdrsize, file=sys.stderr )
  101. print("XSDRSIZE 0x%08X %d" % (Xsdrsize, Xsdrsize) )
  102. elif op == XSDRTDO:
  103. tdi = bytes2hexString( f, Xsdrsize )
  104. tdo = bytes2hexString( f, Xsdrsize )
  105. print("XSDRTDO 0x%s 0x%s" % (tdi, tdo) )
  106. elif op == XSETSDRMASKS:
  107. addrmask = bytes2hexString( f, Xsdrsize )
  108. datamask = ReadSDRMASKS( f, Xsdrsize )
  109. print("XSETSDRMASKS 0x%s 0x%s" % (addrmask, datamask) )
  110. elif op == XSDRINC:
  111. startaddr = bytes2hexString( f, Xsdrsize )
  112. len = ReadByte(f)
  113. print("XSDRINC 0x%s 0x%02X" % (startaddr, len), end='' )
  114. for numTimes in range(len):
  115. data = bytes2hexString( f, SetsdrmasksOnesCount)
  116. print(" 0x%s" % data )
  117. print() # newline
  118. elif op == XSDRB:
  119. tdi = bytes2hexString( f, Xsdrsize )
  120. print("XSDRB 0x%s" % tdi )
  121. elif op == XSDRC:
  122. tdi = bytes2hexString( f, Xsdrsize )
  123. print("XSDRC 0x%s" % tdi )
  124. elif op == XSDRE:
  125. tdi = bytes2hexString( f, Xsdrsize )
  126. print("XSDRE 0x%s" % tdi )
  127. elif op == XSDRTDOB:
  128. tdo = bytes2hexString( f, Xsdrsize )
  129. print("XSDRTDOB 0x%s" % tdo )
  130. elif op == XSDRTDOC:
  131. tdi = bytes2hexString( f, Xsdrsize )
  132. tdo = bytes2hexString( f, Xsdrsize )
  133. print("XSDRTDOC 0x%s 0x%s" % (tdi, tdo) )
  134. elif op == XSDRTDOE:
  135. tdi = bytes2hexString( f, Xsdrsize )
  136. tdo = bytes2hexString( f, Xsdrsize )
  137. print("XSDRTDOE 0x%s 0x%s" % (tdi, tdo) )
  138. elif op == XSTATE:
  139. b = ReadByte(f)
  140. print("XSTATE %s" % ShowState(b))
  141. elif op == XENDIR:
  142. b = ReadByte( f )
  143. print("XENDIR %s" % 'IRPAUSE' if b==1 else 'IDLE')
  144. elif op == XENDDR:
  145. b = ReadByte( f )
  146. print("XENDDR %s" % 'DRPAUSE' if b==1 else 'IDLE')
  147. elif op == XSIR2:
  148. len = struct.unpack( '>H', f.read(2) )[0]
  149. buf = bytes2hexString( f, len )
  150. print("XSIR2 0x%04X 0x%s" % (len, buf))
  151. elif op == XCOMMENT:
  152. cmt = []
  153. while 1:
  154. b = ReadByte(f)
  155. if b == 0: # terminating nul
  156. break;
  157. cmt.append( chr(b) )
  158. print("XCOMMENT \"%s\"" % ''.join(cmt) )
  159. elif op == XWAIT:
  160. run_state = ReadByte(f)
  161. end_state = ReadByte(f)
  162. useconds = struct.unpack( '>i', f.read(4) )[0]
  163. print("XWAIT %s %s" % (ShowState(run_state), ShowState(end_state)), useconds)
  164. elif op == XWAITSTATE:
  165. run_state = ReadByte(f)
  166. end_state = ReadByte(f)
  167. clocks = struct.unpack( '>i', f.read(4) )[0]
  168. useconds = struct.unpack( '>i', f.read(4) )[0]
  169. print("XWAITSTATE %s %s CLOCKS=%d USECS=%d" % (ShowState(run_state), ShowState(end_state), clocks, useconds) )
  170. elif op == LCOUNT:
  171. loop_count = struct.unpack( '>i', f.read(4) )[0]
  172. print("LCOUNT", loop_count )
  173. elif op == LDELAY:
  174. run_state = ReadByte(f)
  175. clocks = struct.unpack( '>i', f.read(4) )[0]
  176. useconds = struct.unpack( '>i', f.read(4) )[0]
  177. print("LDELAY %s CLOCKS=%d USECS=%d" % (ShowState(run_state), clocks, useconds) )
  178. elif op == LSDR:
  179. tdi = bytes2hexString( f, Xsdrsize )
  180. tdo = bytes2hexString( f, Xsdrsize )
  181. print("LSDR 0x%s 0x%s" % (tdi, tdo) )
  182. elif op == XTRST:
  183. # the argument is a single byte and it is the index into "trst_mode_allowed"
  184. trst_mode = ReadByte(f)
  185. if trst_mode <= 3:
  186. print("TRST %s" % trst_mode_allowed[trst_mode] )
  187. else:
  188. print("TRST 0x%02X" % trst_mode );
  189. else:
  190. print("UNKNOWN op 0x%02X %d" % (op, op))
  191. exit(1)
  192. def main():
  193. if len( sys.argv ) < 2:
  194. print("usage %s <xsvf_filename>" % sys.argv[0])
  195. exit(1)
  196. f = open( sys.argv[1], 'rb' )
  197. opcode = ReadByte( f )
  198. while opcode != -1:
  199. # print the position within the file, then the command
  200. print( "%d: " % f.tell(), end='' )
  201. ShowOpcode( opcode, f )
  202. opcode = ReadByte(f)
  203. if __name__ == "__main__":
  204. main()