diff 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
line wrap: on
line diff
--- a/cli.py	Tue Apr 15 15:49:16 2014 +0100
+++ b/cli.py	Wed Apr 16 16:51:39 2014 +0100
@@ -10,6 +10,11 @@
 from asm import asm
 from dbg import dbg
 
+# start emu instance
+emu = dbg.controller()
+emu.Emu = Popen(['bin/emu'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
+
+# symbol encodings
 syms = {
         'R0_0':0,
         'R1_0':1,
@@ -37,9 +42,22 @@
         'C':7
         }
 
-emu = dbg.controller()
-emu.Emu = Popen(['bin/emu'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
-
+# command line command strings
+cmds = {
+        'step':emu.step,
+        'run':emu.run,
+        'runl':emu.run_len,
+        'gr':emu.get_reg,
+        'sr':emu.set_reg,
+        'gf':emu.get_flag,
+        'sf':emu.set_flag,
+        'gb':emu.get_block,
+        'sb':emu.set_block,
+        'ga':emu.get_a,
+        'gfs':emu.get_flags,
+        'gi':emu.get_ir,
+        'sbp':emu.set_bp
+        }
 
 # assemble and execute inline asm.
 # bytecode is copied to where ever the PC is and then executed
@@ -55,8 +73,8 @@
         pcl = struct.unpack('>B', emu.get_reg(syms['PCL']))[0]
         
         # get user input
-        c = raw_input()
-        if c == 'exit':
+        c = raw_input('>> ')
+        if c == 'end':
             break
         code.write(c)
         
@@ -66,75 +84,68 @@
             if l.keys() != []:
                 print 'labels not yet supported in interpreter mode'
                 continue
-            bytecode = asm.second_pass(bytecode, a, l)    
+            bytecode, d = asm.second_pass(bytecode, a, l)    
             bytecode.seek(0)
             byte_array = bytearray(bytecode.read())
         except:
             print 'invalid instruction'
             continue
         
-        print 'PC -> ', hex(pcl | (pch << 8)), [hex(b) for b in byte_array]
+        print 'PC:', hex(pcl | (pch << 8)),'BC:', [hex(b) for b in byte_array]
         # write to emu memory and execute
         emu.set_block(pch, pcl, byte_array)
         emu.step()
 
-cmds = {
-        'step':emu.step,
-        'run':emu.run,
-        'gr':emu.get_reg,
-        'sr':emu.set_reg,
-        'gf':emu.get_flag,
-        'sf':emu.set_flag,
-        'gb':emu.get_block,
-        'sb':emu.set_block,
-        'ga':emu.get_a,
-        'gfs':emu.get_flags,
-        'gir':emu.get_ir,
-        }
+
+# request input and run command
+def req_run():
+    inp = raw_input('> ').split()
+    if inp == []:
+        return
+    cmd = inp[0]
+    args = []
+
+    try:
+        if cmd == 'exit':
+            emu.Emu.kill()
+            exit()
+        # deal with inline execution independently
+        if cmd == 'exc':
+            exc()
+            return
+
+        # set block has unique argument syntax
+        if cmd == 'sb':
+            args.append(int(inp[1], 0))
+            args.append(int(inp[2], 0))
+            args.append([int(x, 0) for x in inp[3:]])
+            cmds[cmd](*args)
+            return
+            
+        # decode args
+        i = 0
+        for word in inp[1:]:
+            if word in syms.keys():
+                args.append(syms[word])
+            # only arguments after 3 will be data for set block
+            # this needs to be in a list
+            else:
+                args.append(int(word, 0))
+            i = i + 1
+        
+        resp = cmds[cmd](*args)
+        if resp == None:
+            return
+        else:
+            print [hex(struct.unpack('>B', x)[0]) for x in resp]
+
+    except Exception, e:
+        print e
+        print 'invalid command or argument syntax'
+        return
+
 
 if __name__ == '__main__':
-    
     while True:
-    
-        inp = raw_input('> ').split()
-        if inp == []:
-            continue
-        cmd = inp[0]
-        args = []
-    
-        try:
-            # deal with inline execution independently
-            if cmd == 'exc':
-                exc()
-                continue
+        req_run()
 
-            # set block has unique argument syntax
-            if cmd == 'sb':
-                args.append(int(inp[1], 0))
-                args.append(int(inp[2], 0))
-                args.append([int(x, 0) for x in inp[3:]])
-                cmds[cmd](*args)
-                continue
-                
-            # decode args
-            i = 0
-            for word in inp[1:]:
-                if word in syms.keys():
-                    args.append(syms[word])
-                # only arguments after 3 will be data for set block
-                # this needs to be in a list
-                else:
-                    args.append(int(word, 0))
-                i = i + 1
-            
-            resp = cmds[cmd](*args)
-            if resp == None:
-                continue
-            else:
-                print [hex(struct.unpack('>B', x)[0]) for x in resp]
-
-        except Exception, e:
-            print e
-            print 'invalid command or argument syntax'
-            continue
-