Skip to content

Commit

Permalink
Use os.open, os.read; special Namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed May 4, 2012
1 parent d2772f6 commit 62fff5f
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions dcpu16.py
Expand Up @@ -18,6 +18,18 @@
SP, PC, O, LIT = 0x1001B, 0x1001C, 0x1001D, 0x1001E


class Namespace(object):
"""Replacement for argparse.Namespace"""

def __init__(self, object_file):
self.object_file = object_file
self.debug = False
self.trace = False
self.speed = False
self.term = 'curses'
self.geometry = '80x24'


def unpack(s):
"""Equivalent of struct.unpack(">H", s)[0]"""
assert len(s) == 2
Expand Down Expand Up @@ -251,22 +263,25 @@ def dump_stack(self):

def run(args, plugins):
program = []
with open(args.object_file, "rb") as f:
word = f.read(2)
while word:
program.append(unpack(word))
word = f.read(2)


fd = os.open(args.object_file, os.O_RDONLY, 0777)
while True:
word = os.read(fd, 2)
if len(word) == 0:
break
program.append(unpack(word))
os.close(fd)

plugins_loaded = []
try:
for p in plugins:
p = p(args)
if p.loaded:
print("Started plugin: %s" % p.name)
plugins_loaded.append(p)

dcpu16 = DCPU16(program, plugins_loaded)

dcpu16.run(trace=args.trace, show_speed=args.speed)
except KeyboardInterrupt:
pass
Expand All @@ -292,22 +307,18 @@ def main(argv):
args.trace = True

run(args, plugins)
return 0


def pypy_main(argv):
program = []
fd = os.open(argv[1], os.O_RDONLY, 0777)
while True:
word = os.read(fd, 2)
if len(word) == 0:
break
program.append(unpack(word))
os.close(fd)
args = Namespace(object_file=argv[-1])

dcpu16 = DCPU16(program, [])
dcpu16.run()
plugins = []

run(args, plugins)
return 0


def target(*args):
"""Target for PyPy translator."""
return pypy_main, None
Expand Down

0 comments on commit 62fff5f

Please sign in to comment.