#!/usr/bin/env python3 # -*- coding: utf-8 -*- # # AWL simulator - Commandline interface # Copyright 2012 Michael Buesch # # Licensed under the terms of the GNU General Public License version 2. # import getopt from awlsim import * opt_onecycle = False opt_quiet = False def usage(): print("%s [OPTIONS] AWL-source" % sys.argv[0]) print("") print("Options:") print(" -1|--onecycle Only run one cycle") print(" -q|--quiet No status messages") def writeStdout(message): if not opt_quiet: sys.stdout.write(message) sys.stdout.flush() nextScreenUpdate = 0.0 lastDump = "" def blockExitCallback(cpu): global nextScreenUpdate global lastDump now = time.time() if now < nextScreenUpdate and\ not opt_onecycle: return nextScreenUpdate = now + 0.1 dump = str(cpu) # Pad lines dump = [ line + (79 - len(line)) * ' ' + '|' for line in dump.splitlines() ] dump = '\n'.join(dump) lastDump = dump writeStdout("\x1B[H" + dump) def main(): global opt_onecycle global opt_quiet try: (opts, args) = getopt.getopt(sys.argv[1:], "h1q", [ "help", "onecycle", "quiet", ]) except getopt.GetoptError as e: print(e) usage() return 1 for (o, v) in opts: if o in ("-h", "--help"): usage() return 0 if o in ("-1", "--onecycle"): opt_onecycle = True if o in ("-q", "--quiet"): opt_quiet = True if len(args) != 1: usage() return 1 awlSource = args[0] try: p = AwlParser() p.parseFile(awlSource) s = AwlSim() s.load(p.getParseTree()) s.getCPU().setBlockExitCallback(blockExitCallback, s.getCPU()) try: writeStdout("\x1B[?25l\x1B[2J") while 1: s.runCycle() if opt_onecycle: break finally: writeStdout("\x1B[?25h\x1B[2J\x1B[H") writeStdout(lastDump + '\n') except AwlSimError as e: print("-- AWL simulator error --") print(str(e)) return 1 return 0 if __name__ == "__main__": sys.exit(main())