Mercurial > hg > ede
comparison cli.py @ 34:4411dee34085
cleaned out docs (don't worry they are comming back)
and added all my test files
author | james <jb302@eecs.qmul.ac.uk> |
---|---|
date | Wed, 16 Apr 2014 16:51:39 +0100 |
parents | c0c2e99b6bb0 |
children | 6b947f6d69d9 |
comparison
equal
deleted
inserted
replaced
33:02241452f397 | 34:4411dee34085 |
---|---|
8 from subprocess import Popen, PIPE, STDOUT | 8 from subprocess import Popen, PIPE, STDOUT |
9 | 9 |
10 from asm import asm | 10 from asm import asm |
11 from dbg import dbg | 11 from dbg import dbg |
12 | 12 |
13 # start emu instance | |
14 emu = dbg.controller() | |
15 emu.Emu = Popen(['bin/emu'], stdout=PIPE, stdin=PIPE, stderr=PIPE) | |
16 | |
17 # symbol encodings | |
13 syms = { | 18 syms = { |
14 'R0_0':0, | 19 'R0_0':0, |
15 'R1_0':1, | 20 'R1_0':1, |
16 'R2_0':2, | 21 'R2_0':2, |
17 'R3_0':3, | 22 'R3_0':3, |
35 'AC':5, | 40 'AC':5, |
36 'Z':6, | 41 'Z':6, |
37 'C':7 | 42 'C':7 |
38 } | 43 } |
39 | 44 |
40 emu = dbg.controller() | 45 # command line command strings |
41 emu.Emu = Popen(['bin/emu'], stdout=PIPE, stdin=PIPE, stderr=PIPE) | 46 cmds = { |
42 | 47 'step':emu.step, |
48 'run':emu.run, | |
49 'runl':emu.run_len, | |
50 'gr':emu.get_reg, | |
51 'sr':emu.set_reg, | |
52 'gf':emu.get_flag, | |
53 'sf':emu.set_flag, | |
54 'gb':emu.get_block, | |
55 'sb':emu.set_block, | |
56 'ga':emu.get_a, | |
57 'gfs':emu.get_flags, | |
58 'gi':emu.get_ir, | |
59 'sbp':emu.set_bp | |
60 } | |
43 | 61 |
44 # assemble and execute inline asm. | 62 # assemble and execute inline asm. |
45 # bytecode is copied to where ever the PC is and then executed | 63 # bytecode is copied to where ever the PC is and then executed |
46 def exc(): | 64 def exc(): |
47 | 65 |
53 # get the PC | 71 # get the PC |
54 pch = struct.unpack('>B', emu.get_reg(syms['PCH']))[0] | 72 pch = struct.unpack('>B', emu.get_reg(syms['PCH']))[0] |
55 pcl = struct.unpack('>B', emu.get_reg(syms['PCL']))[0] | 73 pcl = struct.unpack('>B', emu.get_reg(syms['PCL']))[0] |
56 | 74 |
57 # get user input | 75 # get user input |
58 c = raw_input() | 76 c = raw_input('>> ') |
59 if c == 'exit': | 77 if c == 'end': |
60 break | 78 break |
61 code.write(c) | 79 code.write(c) |
62 | 80 |
63 try: | 81 try: |
64 # assemble and determine length bytes | 82 # assemble and determine length bytes |
65 a, l = asm.first_pass(code) | 83 a, l = asm.first_pass(code) |
66 if l.keys() != []: | 84 if l.keys() != []: |
67 print 'labels not yet supported in interpreter mode' | 85 print 'labels not yet supported in interpreter mode' |
68 continue | 86 continue |
69 bytecode = asm.second_pass(bytecode, a, l) | 87 bytecode, d = asm.second_pass(bytecode, a, l) |
70 bytecode.seek(0) | 88 bytecode.seek(0) |
71 byte_array = bytearray(bytecode.read()) | 89 byte_array = bytearray(bytecode.read()) |
72 except: | 90 except: |
73 print 'invalid instruction' | 91 print 'invalid instruction' |
74 continue | 92 continue |
75 | 93 |
76 print 'PC -> ', hex(pcl | (pch << 8)), [hex(b) for b in byte_array] | 94 print 'PC:', hex(pcl | (pch << 8)),'BC:', [hex(b) for b in byte_array] |
77 # write to emu memory and execute | 95 # write to emu memory and execute |
78 emu.set_block(pch, pcl, byte_array) | 96 emu.set_block(pch, pcl, byte_array) |
79 emu.step() | 97 emu.step() |
80 | 98 |
81 cmds = { | 99 |
82 'step':emu.step, | 100 # request input and run command |
83 'run':emu.run, | 101 def req_run(): |
84 'gr':emu.get_reg, | 102 inp = raw_input('> ').split() |
85 'sr':emu.set_reg, | 103 if inp == []: |
86 'gf':emu.get_flag, | 104 return |
87 'sf':emu.set_flag, | 105 cmd = inp[0] |
88 'gb':emu.get_block, | 106 args = [] |
89 'sb':emu.set_block, | 107 |
90 'ga':emu.get_a, | 108 try: |
91 'gfs':emu.get_flags, | 109 if cmd == 'exit': |
92 'gir':emu.get_ir, | 110 emu.Emu.kill() |
93 } | 111 exit() |
112 # deal with inline execution independently | |
113 if cmd == 'exc': | |
114 exc() | |
115 return | |
116 | |
117 # set block has unique argument syntax | |
118 if cmd == 'sb': | |
119 args.append(int(inp[1], 0)) | |
120 args.append(int(inp[2], 0)) | |
121 args.append([int(x, 0) for x in inp[3:]]) | |
122 cmds[cmd](*args) | |
123 return | |
124 | |
125 # decode args | |
126 i = 0 | |
127 for word in inp[1:]: | |
128 if word in syms.keys(): | |
129 args.append(syms[word]) | |
130 # only arguments after 3 will be data for set block | |
131 # this needs to be in a list | |
132 else: | |
133 args.append(int(word, 0)) | |
134 i = i + 1 | |
135 | |
136 resp = cmds[cmd](*args) | |
137 if resp == None: | |
138 return | |
139 else: | |
140 print [hex(struct.unpack('>B', x)[0]) for x in resp] | |
141 | |
142 except Exception, e: | |
143 print e | |
144 print 'invalid command or argument syntax' | |
145 return | |
146 | |
94 | 147 |
95 if __name__ == '__main__': | 148 if __name__ == '__main__': |
96 | |
97 while True: | 149 while True: |
98 | 150 req_run() |
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 | 151 |
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 Exception, e: | |
137 print e | |
138 print 'invalid command or argument syntax' | |
139 continue | |
140 |