view dbg/dbg.py @ 28:6d32e54e5c16

emulator overhauled really, new a better memory structure also cleaned up the file system some what.
author james <jb302@eecs.qmul.ac.uk>
date Fri, 11 Apr 2014 14:38:09 +0100
parents
children 83e80c2c489c
line wrap: on
line source
#!/usr/bin/env python
# dbg.py - debug client
import struct
import os, sys
from subprocess import Popen, PIPE, STDOUT

emu = Popen(['./a.out'], stdout=PIPE, stdin=PIPE, stderr=PIPE)

def snd(m):
    emu.stdin.write(struct.pack('>B', m))

def rcv():
    with open('out', 'r') as f:
        c = f.read()
    return c

def step():
    snd(0x00)

def run():
    snd(0x01)

def set_reg(reg, data):
    snd(0x02)
    snd(reg)  # reg
    snd(data) # data

def get_reg(reg):
    snd(0x03)
    snd(reg)  # reg
    #return rcv()

def set_flag(flag, on):
    snd(0x04)
    if on == 0:
        snd(flag)
        snd(0)
    else:
        snd(flag)
        snd(1)

def get_flag(flag):
    snd(0x05)
    snd(flag)
    #return rcv()

def set_block(addrh, addrl, lenh, lenl, data):
    snd(0x06)
    snd(addrh) # address high byte
    snd(addrl) # address low byte
    snd(lenh)
    snd(lenl)
    for b in data:
        snd(b) # data

def get_block(addrh, addrl, lenh, lenl):
    block = []
    snd(0x07)
    snd(addrh) # address high byte
    snd(addrl) # address low byte
    snd(lenh)  
    snd(lenl) 
    #lc = 0
    #while lc != l:
    #    lc = os.path.getsize('out');
    #return rcv()

registers = {   
        'r0':0,
        'r1':1,
        'r2':2,
        'r3':3,
        'dph':4,
        'dpl':5,
        'sph':6,
        'spl':7,
        'a':8,
        'flags':9
        }

cmds = {
        'step':step,
        'run':run,
        'sr':set_reg,
        'gr':get_reg,
        'sf':set_flag,
        'gf':get_flag,
        'sb':set_block,
        'gb':get_block
        }