use sockets for comms instead of stdio

This commit is contained in:
Greg Hewgill 2011-08-18 20:39:28 +12:00
parent c36ad8b662
commit cd692af6f3
2 changed files with 34 additions and 21 deletions

View File

@ -5,6 +5,7 @@
import numpy import numpy
import pygame import pygame
import socket
import struct import struct
import subprocess import subprocess
import sys import sys
@ -357,30 +358,32 @@ class Apple2:
self.speaker = speaker self.speaker = speaker
self.softswitches = SoftSwitches(display, speaker, cassette) self.softswitches = SoftSwitches(display, speaker, cassette)
listener = socket.socket()
listener.bind(("127.0.0.1", 0))
listener.listen(0)
args = [ args = [
sys.executable, sys.executable,
"cpu6502.py", "cpu6502.py",
"--ui", str(listener.getsockname()[1]),
"--rom", options.rom, "--rom", options.rom,
] ]
if options.ram: if options.ram:
args.extend([ args.extend([
"--ram", options.ram, "--ram", options.ram,
]) ])
self.core = subprocess.Popen( self.core = subprocess.Popen(args)
args=args,
stdin=subprocess.PIPE, self.cpu, _ = listener.accept()
stdout=subprocess.PIPE,
)
def run(self): def run(self):
update_cycle = 0 update_cycle = 0
quit = False quit = False
while not quit: while not quit:
op = self.core.stdout.read(8) op = self.cpu.recv(8)
cycle, rw, addr, val = struct.unpack("<IBHB", op) cycle, rw, addr, val = struct.unpack("<IBHB", op)
if rw == 0: if rw == 0:
self.core.stdin.write(chr(self.softswitches.read_byte(cycle, addr))) self.cpu.send(chr(self.softswitches.read_byte(cycle, addr)))
self.core.stdin.flush()
elif rw == 1: elif rw == 1:
self.display.update(addr, val) self.display.update(addr, val)
else: else:

View File

@ -3,10 +3,14 @@
# originally written 2001, updated 2011 # originally written 2001, updated 2011
import socket
import struct import struct
import sys import sys
ui = None
def signed(x): def signed(x):
if x > 0x7F: if x > 0x7F:
x = x - 0x100 x = x - 0x100
@ -86,22 +90,20 @@ class Memory:
return 0 return 0
op = struct.pack("<IBHB", cycle, 0, address, 0) op = struct.pack("<IBHB", cycle, 0, address, 0)
try: try:
sys.stdout.write(op) ui.send(op)
sys.stdout.flush() b = ui.recv(1)
except IOError:
sys.exit(0)
b = sys.stdin.read(1)
if len(b) == 0: if len(b) == 0:
sys.exit(0) sys.exit(0)
return ord(b) return ord(b)
except socket.error:
sys.exit(0)
def bus_write(self, cycle, address, value): def bus_write(self, cycle, address, value):
if not self.use_stdio: if not self.use_stdio:
return return
op = struct.pack("<IBHB", cycle, 1, address, value) op = struct.pack("<IBHB", cycle, 1, address, value)
try: try:
sys.stdout.write(op) ui.send(op)
sys.stdout.flush()
except IOError: except IOError:
sys.exit(0) sys.exit(0)
@ -509,7 +511,10 @@ class CPU:
def reset(self): def reset(self):
self.program_counter = self.read_word(self.RESET_VECTOR) self.program_counter = self.read_word(self.RESET_VECTOR)
def run(self): def run(self, uisocket):
global ui
ui = socket.socket()
ui.connect(("127.0.0.1", uisocket))
while True: while True:
self.cycles += 2 # all instructions take this as a minimum self.cycles += 2 # all instructions take this as a minimum
op = self.read_pc_byte() op = self.read_pc_byte()
@ -975,6 +980,7 @@ def usage():
print >>sys.stderr print >>sys.stderr
print >>sys.stderr, " -R, --rom ROM file to use (default A2ROM.BIN)" print >>sys.stderr, " -R, --rom ROM file to use (default A2ROM.BIN)"
print >>sys.stderr, " -r, --ram RAM file to load (default none)" print >>sys.stderr, " -r, --ram RAM file to load (default none)"
print >>sys.stderr, " -u, --ui UI socket"
sys.exit(1) sys.exit(1)
@ -983,6 +989,7 @@ def get_options():
def __init__(self): def __init__(self):
self.rom = "A2ROM.BIN" self.rom = "A2ROM.BIN"
self.ram = None self.ram = None
self.ui = None
options = Options() options = Options()
a = 1 a = 1
@ -994,6 +1001,9 @@ def get_options():
elif sys.argv[a] in ("-r", "--ram"): elif sys.argv[a] in ("-r", "--ram"):
a += 1 a += 1
options.ram = sys.argv[a] options.ram = sys.argv[a]
elif sys.argv[a] in ("-u", "--ui"):
a += 1
options.ui = int(sys.argv[a])
else: else:
usage() usage()
else: else:
@ -1004,13 +1014,13 @@ def get_options():
if __name__ == "__main__": if __name__ == "__main__":
if sys.stdout.isatty(): options = get_options()
if options.ui is None:
print "ApplePy cpu core" print "ApplePy cpu core"
print "Run applepy.py instead" print "Run applepy.py instead"
sys.exit(0) sys.exit(0)
options = get_options()
mem = Memory(options) mem = Memory(options)
cpu = CPU(mem) cpu = CPU(mem)
cpu.run() cpu.run(options.ui)