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