refactor control command processing

This commit is contained in:
Greg Hewgill 2011-08-20 13:59:22 +12:00
parent 19693bc905
commit 8b62860152
1 changed files with 41 additions and 33 deletions

View File

@ -335,30 +335,20 @@ class ControlHandler:
self.sock.send("ApplePy 6502 core\n")
self.buffer = ""
def handle_read(self):
buf = self.sock.recv(1024)
if not buf:
self.cpu.control.remove(self)
return
self.buffer += buf
while True:
i = self.buffer.find("\n")
if i < 0:
break
s = self.buffer[:i].strip()
self.buffer = self.buffer[i+1:]
a = s.split()
if a[0] == "help":
self.sock.send("commands: help, peek, poke, status, quit, reset\n")
elif a[0] == "peek":
addr = int(a[1])
def cmd_help(self, args):
self.sock.send("commands: %s\n" % ", ".join(sorted(x[4:] for x in dir(self) if x.startswith("cmd_"))))
def cmd_peek(self, args):
addr = int(args[1])
self.sock.send("%02X\n" % self.cpu.memory.read_byte(self.cpu.cycles, addr))
elif a[0] == "poke":
addr = int(a[1])
val = int(a[2])
def cmd_poke(self, args):
addr = int(args[1])
val = int(args[2])
self.cpu.memory.write_byte(self.cpu.cycles, addr, val)
self.sock.send("poked\n")
elif a[0] == "status":
def cmd_status(self, args):
self.sock.send("A=%02X X=%02X Y=%02X S=%02X PC=%04X F=%c%c0%c%c%c%c%c\n" % (
self.cpu.accumulator,
self.cpu.x_index,
@ -373,14 +363,32 @@ class ControlHandler:
"Z" if self.cpu.zero_flag else "z",
"C" if self.cpu.carry_flag else "c",
))
elif a[0] == "quit":
def cmd_quit(self, args):
self.cpu.quit = True
self.sock.send("quitting\n")
elif a[0] == "reset":
def cmd_reset(self, args):
self.cpu.reset()
self.cpu.running = True
self.sock.send("resetting\n")
else:
def handle_read(self):
buf = self.sock.recv(1024)
if not buf:
self.cpu.control.remove(self)
return
self.buffer += buf
while True:
i = self.buffer.find("\n")
if i < 0:
break
s = self.buffer[:i].strip()
self.buffer = self.buffer[i+1:]
a = s.split()
try:
getattr(self, "cmd_" + a[0])(a)
except AttributeError:
self.sock.send("unknown command\n")