-
Notifications
You must be signed in to change notification settings - Fork 47
/
csim
executable file
·358 lines (275 loc) · 9.49 KB
/
csim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python
"""A C to Verilog compiler - Command line interface"""
__author__ = "Jon Dawson"
__copyright__ = "Copyright (C) 2012, Jonathan P Dawson"
__version__ = "0.1"
import sys
import os
import subprocess
import cmd
from chips.compiler.compiler import compile_python_model
from chips.compiler.exceptions import NoProfile, StopSim, ChipsAssertionFail
import chips.compiler.profiler as profiler
from chips.compiler.register_map import rregmap, frame, tos
from chips.compiler.types import size_of
if len(sys.argv) < 2 or "help" in sys.argv or "h" in sys.argv:
print "Usage: csim [options] <input_file>"
print
sys.exit(-1)
input_file = sys.argv[-1]
options = sys.argv[1:-1] + ["profile"]
model, inputs, outputs, name = compile_python_model(input_file, options)
model.simulation_reset()
def clear():
subprocess.call("cls" if os.name == "nt" else "clear")
def print_file_line(filename, lineno):
lines = open(filename).read().splitlines()
#show file and line number
print "\033[34m\033[1mFile:", filename
print "Line:", "[", lineno, "of", len(lines), "]"
print "\033[0m",
print "\033[39m"
#Print a fragment of the file to show in context
for i in range(lineno - 10, lineno + 10):
if i < 1:
print
elif i > len(lines):
print
elif i == lineno:
print "\033[33m\033[1m", str(i).rjust(4), "\033[32m->", lines[i-1], "\033[0m\033[39m"
else:
print "\033[33m\033[1m", str(i).rjust(4), "\033[0m\033[39m ", lines[i-1]
def print_process_state():
filename = model.get_file()
lineno = model.get_line()
print_file_line(filename, lineno)
class command_interpreter(cmd.Cmd):
intro="""
Chips-2.0 Interactive Debug
Copyright (C) Jonathan P Dawson 2015
"""
prompt=">>> "
def do_exit(self, line):
"""Exit the interpreter session"""
sys.exit(-1)
def do_reset(self, line):
"""Reset the simulation"""
clear()
model.simulation_reset()
print_process_state()
def do_run(self, line):
"""Run the simulation until the process terminated"""
clear()
while True:
try:
model.simulation_step()
except StopSim:
break
print_process_state()
def do_into(self, line):
"""Step *into* a nested statement"""
clear()
try:
model.step_into()
except StopSim:
print "Process has completed"
print_process_state()
def do_over(self, line):
"""Step *over* a nested statement"""
clear()
try:
model.step_over()
except StopSim:
print "Process has completed"
print_process_state()
def do_set_breakpoint(self, line):
"""Set the breakpoint to a line in a file"""
clear()
try:
#print list of files
print "Code Files:"
code_files = sorted(profiler.code_files(model.instructions))
for i, f in enumerate(code_files):
print "[%u] %s"%(i, f)
#get user selection
print "\nEnter file:"
selection = raw_input()
f = code_files[int(selection)]
line = 0
while 1:
clear()
print_file_line(f, line)
print
print "n=next, p=prev, j=down, k=up, blank=enter"
command = raw_input()
if command == "n":
line = abs(line + 10)
elif command == "n":
line = abs(line - 10)
elif command == "j":
line = abs(line + 1)
elif command == "k":
line = abs(line - 1)
elif command == "":
break
model.set_breakpoint(f, line)
except StopSim:
print "Process has completed"
except (ValueError, IndexError):
print "Invalid selection"
def do_stack_use(self, line):
"""Print the maximum stack usage"""
print "Maximum Stack Usage: %s"%model.max_stack
def do_registers(self, line):
"""Print the state of machine registers"""
clear()
registers = model.get_registers()
for number in range(16):
register = registers.get(number, 0)
print "%0.10d:"%number, "%0.10u"%register, rregmap.get(number, "reserved")
def do_instructions(self, line):
"""Print the machine code instructions"""
clear()
instructions = model.instructions
for i, instruction in enumerate(instructions):
if i == model.get_program_counter():
print "->",
else:
print " ",
z = instruction.get("z", 0)
a = instruction.get("a", 0)
b = instruction.get("b", 0)
o = instruction.get("op", "unknown")
literal = instruction.get("literal", 0)
label = instruction.get("label", 0)
print "%0.10d:"%i, o.ljust(11), "%0.2d"%z, "%0.2d"%a, "%0.10d"%(b | label | literal)
def do_locals(self, line):
"""Print the values of local variables"""
clear()
instruction = model.get_instruction()
trace = instruction["trace"]
function = trace.function
registers = model.get_registers()
frameval = registers.get(frame, 0)
memory = model.get_memory()
variables = function.local_variables
print "locals"
print "======"
for name, instance in variables.iteritems():
offset = instance.offset
print offset
size = size_of(instance)
if size == 4:
print name, ":", memory.get(frameval+offset, 0)
elif size == 8:
print name, ":", memory.get(frameval+offset+1, 0) << 32 | memory.get(frameval+offset, 0)
else:
print name, ":"
for i in range((size//4+8)//8):
print "%4x:"%(i*8),
for j in range(8):
print "%08x"%memory.get(frameval+offset+8*i+j, 0),
print
def do_globals(self, line):
"""Print the values of global variables"""
clear()
instruction = model.get_instruction()
trace = instruction["trace"]
global_scope = trace.global_scope
memory = model.get_memory()
print "globals"
print "======="
for name, instance in global_scope.global_variables.iteritems():
offset = instance.offset
size = size_of(instance)
if size == 4:
print name, ":", memory.get(offset, 0)
elif size == 8:
print name, ":", memory.get(offset+1, 0) << 32 | memory.get(tos+offset, 0)
else:
print name, ":"
for i in range((size//4)//8):
print "%4x:"%(i*8),
for j in range(8):
print "%08x"%memory.get(offset+8*i+j, 0),
print
def do_memory(self, line):
"""Print the contents of the memory"""
clear()
registers = model.get_registers()
memory = model.get_memory()
start_of_frame = registers.get(frame, None)
end_of_frame = registers.get(tos, 0)
for number in range(end_of_frame):
if number == end_of_frame:
break
if number == start_of_frame:
print "->",
else:
print " ",
location = memory.get(number, 0)
print "%0.10d:"%number, "%0.10d"%location, "%0.8x"%location
def do_run_to_breakpoint(self, line):
"""Run simulation until the breakpoint is encountered"""
clear()
try:
model.run_to_breakpoint()
except StopSim:
pass
print_process_state()
def do_mstep(self, line):
"""step the simulator by a single machine instruction"""
clear()
try:
model.simulation_step()
except StopSim:
"process completed"
print_process_state()
def do_coverage(self, line):
"""Print a summary of the code coverage"""
clear()
try:
profile = model.get_profile()
profiler.report_coverage(profile, model.instructions)
except NoProfile:
print "Profiling must be enabled to use this feature"
def do_profile(self, line):
"""Print proportion of execution time in each code line"""
clear()
try:
profile = model.get_profile()
profiler.report_profile(profile, model.instructions)
except NoProfile:
print "Profiling must be enabled to use this feature"
def do_annotate(self, line):
"""Annotate a source file showing which files have been executed"""
clear()
try:
#print list of files
print "Code Files:"
code_files = sorted(profiler.code_files(model.instructions))
for i, f in enumerate(code_files):
print "[%u] %s"%(i, f)
#get user selection
print "\nEnter file:"
selection = raw_input()
f = code_files[int(selection)]
#annotate
profile = model.get_profile()
profiler.annotate_coverage(f, profile, model.instructions)
except NoProfile:
print "Profiling must be enabled to use this feature"
except (ValueError, IndexError):
print "Invalid selection"
if "interactive" in options:
command_interpreter().cmdloop()
else:
while True:
try:
model.simulation_step()
except StopSim:
break
except ChipsAssertionFail as e:
print e
exit(1)
break