Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from subprocess import Popen, PIPE, STDOUT
- import threading
- import time
- import os
- class MSXClient:
- def __init__(self, rom_filename, machine='C-BIOS_MSX1'):
- # open emulator as a pipe
- self.pipe = Popen(['openmsx', '-control', 'stdio', '-machine', machine], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
- # run stdout listener thread
- self.finished = False
- th = threading.Thread(target=self.stdout_listener)
- th.start()
- # send some init commands
- self.command("cart %s" % rom_filename)
- self.command("set power on")
- self.command("set renderer SDL")
- self.command("set scale_factor 1")
- self.command("set pause on")
- self.command("set throttle off")
- self.command("reverse stop")
- self.frame_num = 0
- self.afters = {}
- def command(self, c, need_print=True):
- """
- Send a command to openMSX's stdin, print if needed.
- """
- cmd = "<command>%s</command>\n" % c
- self.pipe.stdin.write(cmd)
- if need_print:
- print cmd[:-1]
- def readline(self, need_print=False):
- """
- Read a line from openMSX's stdout, print if needed.
- Also, detect if frame is complete (screen saved).
- """
- out = self.pipe.stdout.readline().strip()
- if out.find("Screen saved") >= 0 or out.find("frameisready") >= 0:
- self.paused = True
- if out.find("after#") >= 0:
- after = out.split(">")[1].split("<")[0]
- self.afters[after] = True
- if need_print:
- print out
- return out
- def reset(self):
- """
- Shortcut to 'reset' command.
- """
- self.command("reset")
- def shutdown(self):
- """
- Send 'exit' command to openMSX and wait until subprocess if finished.
- """
- self.command("exit")
- self.finished = True
- self.pipe.communicate()
- def stdout_listener(self):
- while not self.finished:
- self.readline()
- def process_frame(self):
- """
- A frame is in ./screen.png now, process it here.
- """
- pass
- def step(self):
- """
- Wait for the next frame and process it.
- """
- self.paused = False
- self.command("after frame \"set pause on; screenshot -raw ./screen.png\"")
- #self.command("after frame \"set pause on; message frameisready\"")
- self.command("set pause off")
- while not self.paused:
- time.sleep(0.001)
- for after in self.afters.keys():
- self.command("after cancel %s" % after)
- del self.afters[after]
- self.process_frame()
- if __name__ == "__main__":
- client = MSXClient("./roms/area5.rom")
- for i in xrange(10000):
- client.step()
- client.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement