Permalink
Browse files

use sockets for comms instead of stdio

  • Loading branch information...
1 parent c36ad8b commit cd692af6f33d24a117bd0ad5db0011db63b13366 @ghewgill ghewgill committed Aug 18, 2011
Showing with 34 additions and 21 deletions.
  1. +11 −8 applepy.py
  2. +23 −13 cpu6502.py
View
@@ -5,6 +5,7 @@
import numpy
import pygame
+import socket
import struct
import subprocess
import sys
@@ -357,30 +358,32 @@ def __init__(self, options, display, speaker, cassette):
self.speaker = speaker
self.softswitches = SoftSwitches(display, speaker, cassette)
+ listener = socket.socket()
+ listener.bind(("127.0.0.1", 0))
+ listener.listen(0)
+
args = [
sys.executable,
"cpu6502.py",
+ "--ui", str(listener.getsockname()[1]),
"--rom", options.rom,
]
if options.ram:
args.extend([
"--ram", options.ram,
])
- self.core = subprocess.Popen(
- args=args,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- )
+ self.core = subprocess.Popen(args)
+
+ self.cpu, _ = listener.accept()
def run(self):
update_cycle = 0
quit = False
while not quit:
- op = self.core.stdout.read(8)
+ op = self.cpu.recv(8)
cycle, rw, addr, val = struct.unpack("<IBHB", op)
if rw == 0:
- self.core.stdin.write(chr(self.softswitches.read_byte(cycle, addr)))
- self.core.stdin.flush()
+ self.cpu.send(chr(self.softswitches.read_byte(cycle, addr)))
elif rw == 1:
self.display.update(addr, val)
else:
View
@@ -3,10 +3,14 @@
# originally written 2001, updated 2011
+import socket
import struct
import sys
+ui = None
+
+
def signed(x):
if x > 0x7F:
x = x - 0x100
@@ -86,22 +90,20 @@ def bus_read(self, cycle, address):
return 0
op = struct.pack("<IBHB", cycle, 0, address, 0)
try:
- sys.stdout.write(op)
- sys.stdout.flush()
- except IOError:
- sys.exit(0)
- b = sys.stdin.read(1)
- if len(b) == 0:
+ ui.send(op)
+ b = ui.recv(1)
+ if len(b) == 0:
+ sys.exit(0)
+ return ord(b)
+ except socket.error:
sys.exit(0)
- return ord(b)
def bus_write(self, cycle, address, value):
if not self.use_stdio:
return
op = struct.pack("<IBHB", cycle, 1, address, value)
try:
- sys.stdout.write(op)
- sys.stdout.flush()
+ ui.send(op)
except IOError:
sys.exit(0)
@@ -509,7 +511,10 @@ def setup_ops(self):
def reset(self):
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:
self.cycles += 2 # all instructions take this as a minimum
op = self.read_pc_byte()
@@ -975,6 +980,7 @@ def usage():
print >>sys.stderr
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, " -u, --ui UI socket"
sys.exit(1)
@@ -983,6 +989,7 @@ class Options:
def __init__(self):
self.rom = "A2ROM.BIN"
self.ram = None
+ self.ui = None
options = Options()
a = 1
@@ -994,6 +1001,9 @@ def __init__(self):
elif sys.argv[a] in ("-r", "--ram"):
a += 1
options.ram = sys.argv[a]
+ elif sys.argv[a] in ("-u", "--ui"):
+ a += 1
+ options.ui = int(sys.argv[a])
else:
usage()
else:
@@ -1004,13 +1014,13 @@ def __init__(self):
if __name__ == "__main__":
- if sys.stdout.isatty():
+ options = get_options()
+ if options.ui is None:
print "ApplePy cpu core"
print "Run applepy.py instead"
sys.exit(0)
- options = get_options()
mem = Memory(options)
cpu = CPU(mem)
- cpu.run()
+ cpu.run(options.ui)

0 comments on commit cd692af

Please sign in to comment.