comparison cli.py @ 29:83e80c2c489c

seperated working emu code from broken emu code. wrote dbg interface
author james <jb302@eecs.qmul.ac.uk>
date Sun, 13 Apr 2014 22:42:57 +0100
parents
children c0c2e99b6bb0
comparison
equal deleted inserted replaced
28:6d32e54e5c16 29:83e80c2c489c
1 #!/usr/bin/env python
2 # cli.py - a command line to control the emulator using
3 # the debug control class
4
5 import struct
6 from StringIO import StringIO
7 from io import BytesIO
8 from subprocess import Popen, PIPE, STDOUT
9
10 from asm import asm
11 from dbg import dbg
12
13 syms = {
14 'R0_0':0,
15 'R1_0':1,
16 'R2_0':2,
17 'R3_0':3,
18 'DPH':4,
19 'SPH':5,
20 'PCH':6,
21 'TMPH':7,
22 'RO_1':8,
23 'R1_1':9,
24 'R2_1':10,
25 'R3_1':11,
26 'DPL':12,
27 'SPL':13,
28 'PCL':14,
29 'TMPL':15,
30 'BS':0,
31 'IE':1,
32 'OV':2,
33 'S':3,
34 'P':4,
35 'AC':5,
36 'Z':6,
37 'C':7
38 }
39
40 emu = dbg.controller()
41 emu.Emu = Popen(['bin/emu'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
42
43
44 # assemble and execute inline asm.
45 # bytecode is copied to where ever the PC is and then executed
46 def exc():
47
48 while True:
49 # file like objects for source and byte code
50 code = StringIO()
51 bytecode = BytesIO()
52
53 # get the PC
54 pch = struct.unpack('>B', emu.get_reg(syms['PCH']))[0]
55 pcl = struct.unpack('>B', emu.get_reg(syms['PCL']))[0]
56
57 # get user input
58 c = raw_input()
59 if c == 'exit':
60 break
61 code.write(c)
62
63 try:
64 # assemble and determine length bytes
65 a, l = asm.first_pass(code)
66 if l.keys() != []:
67 print 'labels not yet supported in interpreter mode'
68 continue
69 bytecode = asm.second_pass(bytecode, a, l)
70 bytecode.seek(0)
71 byte_array = bytearray(bytecode.read())
72 except:
73 print 'invalid instruction'
74 continue
75
76 print hex(pcl | (pch << 8)), [hex(b) for b in byte_array]
77 # write to emu memory and execute
78 emu.set_block(pch, pcl, byte_array)
79 emu.step()
80
81 cmds = {
82 'step':emu.step,
83 'run':emu.run,
84 'gr':emu.get_reg,
85 'sr':emu.set_reg,
86 'gf':emu.get_flag,
87 'sf':emu.set_flag,
88 'gb':emu.get_block,
89 'sb':emu.set_block,
90 'ga':emu.get_a,
91 'gfs':emu.get_flags,
92 'gir':emu.get_ir,
93 }
94
95 if __name__ == '__main__':
96
97 while True:
98
99 inp = raw_input('> ').split()
100 if inp == []:
101 continue
102 cmd = inp[0]
103 args = []
104
105 #try:
106 # deal with inline execution independently
107 if cmd == 'exc':
108 exc()
109 continue
110
111 # set block has unique argument syntax
112 if cmd == 'sb':
113 args.append(int(inp[1], 0))
114 args.append(int(inp[2], 0))
115 args.append([int(x, 0) for x in inp[3:]])
116 cmds[cmd](*args)
117 continue
118
119 # decode args
120 i = 0
121 for word in inp[1:]:
122 if word in syms.keys():
123 args.append(syms[word])
124 # only arguments after 3 will be data for set block
125 # this needs to be in a list
126 else:
127 args.append(int(word, 0))
128 i = i + 1
129
130 resp = cmds[cmd](*args)
131 if resp == None:
132 continue
133 else:
134 print [hex(struct.unpack('>B', x)[0]) for x in resp]
135
136 #except:
137 # print 'invalid command or argument syntax'
138 # continue
139