User_Interface.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ## Copyright (C) 2016 Jeremiah Orians
  2. ## This file is part of stage0.
  3. ##
  4. ## stage0 is free software: you an redistribute it and/or modify
  5. ## it under the terms of the GNU General Public License as published by
  6. ## the Free Software Foundation, either version 3 of the License, or
  7. ## (at your option) any later version.
  8. ##
  9. ## stage0 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 stage0. If not, see <http://www.gnu.org/licenses/>.
  16. import subprocess
  17. import ctypes
  18. import re
  19. import sys, getopt
  20. vm = ctypes.CDLL('./libvm.so')
  21. vm.initialize_lilith.argtype = ctypes.c_uint
  22. vm.get_memory.argtype = ctypes.c_uint
  23. vm.get_memory.restype = ctypes.c_char_p
  24. vm.step_lilith.restype = ctypes.c_uint
  25. vm.set_register.argtype = (ctypes.c_uint, ctypes.c_uint)
  26. vm.set_memory.argtype = (ctypes.c_uint, ctypes.c_ubyte)
  27. vm.get_register.argtype = ctypes.c_uint
  28. vm.get_register.restype = ctypes.c_uint
  29. def Reset_lilith():
  30. global Memory_Size
  31. if 0 <= Memory_Size < 1024:
  32. unit = 'Bytes'
  33. chunks = Memory_Size
  34. elif 1024 <= Memory_Size < (1024 * 1024):
  35. unit = 'KB'
  36. chunks = Memory_Size / 1024
  37. elif (1024 * 1024) <= Memory_Size < (1024 * 1024 * 1024):
  38. unit = 'MB'
  39. chunks = Memory_Size / (1024 * 1024)
  40. else:
  41. unit = 'GB'
  42. chunks = Memory_Size / (1024 * 1024 * 1024)
  43. print("Current Memory Size is: " + str(chunks) + unit)
  44. vm.initialize_lilith(Memory_Size)
  45. global Current_IP
  46. Current_IP = 0
  47. global Watchpoints
  48. Watchpoints = {0}
  49. global ROM_Name
  50. vm.load_lilith(ctypes.create_string_buffer(ROM_Name.encode('ascii')))
  51. def Step_lilith():
  52. global Current_IP
  53. Current_IP = vm.step_lilith()
  54. global Count
  55. Count = Count + 1
  56. return
  57. def Set_Memory(address, value):
  58. vm.set_memory(address, value)
  59. return
  60. def Set_Register(register, value):
  61. vm.set_register(register, value)
  62. return
  63. def returnPage():
  64. return get_header() + (vm.get_memory(Current_Page)).decode('utf-8') + get_spacer1() + get_registers(0) + get_registers(9) + get_spacer2() + get_Window_shortcut() + get_disassembled() + get_footer()
  65. hexlookup = { 0 : '0', 1 : '1', 2 : '2', 3 : '3', 4 : '4', 5 : '5', 6 : '6', 7 : '7', 8 : '8', 9 : '9', 10 : 'A', 11 : 'B', 12 : 'C', 13 : 'D', 14 : 'E', 15 : 'F' }
  66. def formatByte(a):
  67. first = a >> 4
  68. second = a % 16
  69. return str(hexlookup[first]+hexlookup[second])
  70. def formatAddress(a):
  71. first = a >> 24
  72. second = (a % 16777216) >> 16
  73. third = (a % 65536) >> 8
  74. fourth = a % 256
  75. myreturn = formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth)
  76. return myreturn[:-1] + "x"
  77. def formatRegister(a):
  78. first = a >> 24
  79. second = (a % 16777216) >> 16
  80. third = (a % 65536) >> 8
  81. fourth = a % 256
  82. return formatByte(first) + formatByte(second) + formatByte(third) + formatByte(fourth)
  83. def get_header():
  84. return """
  85. <!DOCTYPE html>
  86. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  87. <head>
  88. <meta charset="utf-8" />
  89. <title>Knight CPU Debugger</title>
  90. <link rel="stylesheet" type="text/css" href="static/style.css" />
  91. <script type="text/javascript" src="static/jquery-1.8.3.js"> </script>
  92. <script type="text/javascript" src="static/script.js"></script>
  93. </head>
  94. <body>
  95. <div>
  96. <a href="RUN"><button type="button">RUN</button></a>
  97. <a href="STOP"><button type="button">STOP</button></a>
  98. <a href="PAGEDOWN"><button type="button">PAGE DOWN</button></a>
  99. <a href="PAGEUP"><button type="button">PAGE UP</button></a>
  100. <a href="STEP"><button type="button">STEP</button></a>
  101. <a href="RESET"><button type="button">RESET</button></a>
  102. <a href="SPEEDBREAKPOINT"><button type="button">Run Until Breakpoint</button></a>
  103. </div>
  104. <div>
  105. <div style="height:230px; width:60%; float:left; overflow-y: scroll;">
  106. <table class="Memory">
  107. <thead>
  108. <tr>
  109. <th>Index</th>
  110. <th>0</th>
  111. <th>1</th>
  112. <th>2</th>
  113. <th>3</th>
  114. <th>4</th>
  115. <th>5</th>
  116. <th>6</th>
  117. <th>7</th>
  118. <th>8</th>
  119. <th>8</th>
  120. <th>A</th>
  121. <th>B</th>
  122. <th>C</th>
  123. <th>D</th>
  124. <th>E</th>
  125. <th>F</th>
  126. </tr>
  127. </thead>
  128. <tbody>"""
  129. def get_spacer1():
  130. return """ </tbody>
  131. </table>
  132. </div>
  133. <div>
  134. """
  135. def get_registers(index):
  136. temp = """ <table class="Registers">
  137. <thead>
  138. <tr>
  139. <th>Register</th>
  140. <th>Value</th>
  141. <th>Name</th>
  142. </tr>
  143. </thead>
  144. <tbody>"""
  145. if (0 == index):
  146. for i in range(0,9):
  147. temp = temp + """<tr><td>R""" + str(index + i) + "</td><td>" + formatRegister(vm.get_register(index + i)) + "</td>" + "<td>REG" + str(index + i) +"</tr>\n"
  148. else:
  149. for i in range(0,7):
  150. temp = temp + """<tr><td>R""" + str(index + i) + "</td><td>" + formatRegister(vm.get_register(index + i)) + "</td>" + "<td>REG" + str(index + i) +"</tr>\n"
  151. temp = temp + """<tr><td>R16</td><td>""" + formatRegister(vm.get_register(16)) + "</td><td> PC </td></tr>\n"
  152. temp = temp + """<tr><td>R17</td><td>""" + formatRegister(vm.get_register(17)) + "</td><td>Counter</td></tr>\n"
  153. return temp + """ </tbody>
  154. </table> """
  155. def get_spacer2():
  156. return """
  157. </div>
  158. </div>
  159. """
  160. def get_Window_shortcut():
  161. return """
  162. <form action="WINDOW" style="position:absolute; left:10px; top:260px;">
  163. <input type="submit" value="Window Fast Move"> <input type="text" name="Window">
  164. </form>
  165. """
  166. def get_disassembled():
  167. Current_IP = vm.get_register(16)
  168. f = open('z_disassembled', "r")
  169. temp = """ <div style="position:absolute; left:10px; top:280px; overflow-y: scroll; height:200px; width:60%;">
  170. <table class="Debug"><tbody>"""
  171. for line in f:
  172. pieces = re.split(r'\t+', line)
  173. i = int(pieces[0], 16)
  174. if (i < Current_IP):
  175. temp = temp + ""
  176. elif (i == Current_IP):
  177. temp = temp + """<tr class="current"><td>""" + pieces[0] + "</td><td>" + pieces[1] + "</td><td>" + pieces[2] + "</td></tr>\n"
  178. elif i in Watchpoints:
  179. temp = temp + """<tr class="breakpoint"><td>""" + pieces[0] + "</td><td>" + pieces[1] + "</td><td>" + pieces[2] + "</td></tr>\n"
  180. else:
  181. temp = temp + "<tr><td>" + pieces[0] + "</td><td>" + pieces[1] + "</td><td>" + pieces[2] + "</td></tr>\n"
  182. f.close()
  183. return temp + "</tbody></table>"
  184. def get_footer():
  185. return """
  186. </div>
  187. </body>
  188. </html>
  189. """
  190. def main(argv):
  191. global Debug_Point
  192. help_string = 'Knight.py --ROM=$NAME [--DEBUG=$NUMBER] [--WINDOW=$NUMBER] [--MEMORY=$SIZE]\n'
  193. try:
  194. opts, args = getopt.getopt(argv,"R:D:W:M:",["ROM=","DEBUG=","WINDOW=", "MEMORY="])
  195. except getopt.GetoptError:
  196. print (help_string)
  197. sys.exit(2)
  198. for opt, arg in opts:
  199. if opt == '-h':
  200. print (help_string)
  201. sys.exit()
  202. elif opt in ("-R", "--ROM"):
  203. global ROM_Name
  204. ROM_Name = arg
  205. elif opt in ("-D", "--DEBUG"):
  206. global Debug_Point
  207. Debug_Point = int(arg)
  208. elif opt in ("-W", "--WINDOW"):
  209. global Current_Page
  210. Current_Page = int(arg, 16)
  211. elif opt in ("-M", "--MEMORY"):
  212. global Memory_Size
  213. if arg.endswith('K'):
  214. Memory_Size = (int(arg[:-1]) * 1024)
  215. elif arg.endswith('M'):
  216. Memory_Size = (int(arg[:-1]) * 1024 * 1024)
  217. elif arg.endswith('G'):
  218. Memory_Size = (int(arg[:-1]) * 1024 * 1024 * 1024)
  219. subprocess.call("./bin/dis " + ROM_Name + " >| z_disassembled", shell=True)
  220. Reset_lilith()
  221. Current_IP = 0
  222. Memory_Size = 16 * 1024
  223. Current_Page = 0
  224. Watchpoints = {0}
  225. Count=0
  226. Debug_Point = 0
  227. ROM_Name = "rom"