| 1 | |
| 2 | import os |
| 3 | import sys |
| 4 | import atexit |
| 5 | import readline |
| 6 | import Pfe_Version |
| 7 | |
| 8 | from optparse import OptionParser, Option, OptionValueError |
| 9 | from string import join |
| 10 | from copy import copy |
| 11 | from Pfe_Slicer import Slicer |
| 12 | |
| 13 | |
| 14 | def history(name): |
| 15 | histfile = os.path.join(os.environ["HOME"],name) |
| 16 | try: |
| 17 | readline.read_history_file(histfile) |
| 18 | atexit.register(readline.write_history_file, histfile) |
| 19 | except IOError: |
| 20 | pass |
| 21 | |
| 22 | |
| 23 | def check_slice(option,opt,value): |
| 24 | for s in value.split(','): |
| 25 | try: |
| 26 | eval('slice('+join(s.split(':'),',')+')') |
| 27 | except NameError, ValueError: |
| 28 | raise OptionValueError("option %s: invalid slice: %r" % (opt, value)) |
| 29 | return value |
| 30 | |
| 31 | |
| 32 | class SliceOption (Option): |
| 33 | TYPES = Option.TYPES + ("slice",) |
| 34 | TYPE_CHECKER = copy(Option.TYPE_CHECKER) |
| 35 | TYPE_CHECKER["slice"] = check_slice |
| 36 | |
| 37 | |
| 38 | opts = None |
| 39 | args = [] # The script files to execute |
| 40 | argv = [] # The arguments after -- |
| 41 | |
| 42 | def options(prog): |
| 43 | argv_parser = OptionParser(usage="usage: "+prog+" [options]", |
| 44 | version=Pfe_Version.version(), |
| 45 | option_class=SliceOption) |
| 46 | argv_parser.disable_interspersed_args() |
| 47 | |
| 48 | argv_parser.add_option( |
| 49 | "-i","--interactive", |
| 50 | action="store_true",dest="interactive",default=False, |
| 51 | help="switch to interactive (inspect) mode") |
| 52 | |
| 53 | #argv_parser.add_option( |
| 54 | #"-f","--file", |
| 55 | #action="append",type="string",dest="file",default=[], |
| 56 | #metavar="FILE",help="load one or more elf or memory image files") |
| 57 | |
| 58 | argv_parser.add_option( |
| 59 | "-l","--lstmode", |
| 60 | action="store_true",dest="lstmode",default=False, |
| 61 | help="switch to lstmode, default is runmode") |
| 62 | |
| 63 | #argv_parser.add_option( |
| 64 | #"-n","--steps", |
| 65 | #action="store",type="int",dest="steps", |
| 66 | #metavar="STEPS",help="step the simulator STEPS instructions per strand") |
| 67 | |
| 68 | #argv_parser.add_option( |
| 69 | #"-a","--available", |
| 70 | #action="store",type="slice",dest="available",default="0", |
| 71 | #metavar="SLICE",help="CMP: Make the SLICE of strands available for stepping (default=0)") |
| 72 | |
| 73 | #argv_parser.add_option( |
| 74 | #"-e","--enable", |
| 75 | #action="store",type="slice",dest="enable",default="0", |
| 76 | #metavar="SLICE",help="CMP: Set the SLICE of strands to enabled for stepping (default=0)") |
| 77 | |
| 78 | #argv_parser.add_option( |
| 79 | #"-r","--running", |
| 80 | #action="store",type="slice",dest="running",default="0", |
| 81 | #metavar="SLICE",help="CMP: Put the SLICE of strands in running mode for stepping (default=0)") |
| 82 | |
| 83 | #argv_parser.add_option( |
| 84 | #"-s","--stepping", |
| 85 | #action="store",type="slice",dest="stepping",default="0", |
| 86 | #metavar="SLICE",help="Enable the SLICE of strands for stepping (default=0)") |
| 87 | |
| 88 | global opts |
| 89 | global args |
| 90 | global argv |
| 91 | |
| 92 | (opts,args_argv) = argv_parser.parse_args() |
| 93 | |
| 94 | # Split the file arguments around the '--' if it is present. |
| 95 | # There is one problem "-- no pyfile" fails, while |
| 96 | # "pyfile -- no pyfile" succeeds. Awel, that;'s not a big |
| 97 | # issue as anything after '--' is means for the pyfile anyways. |
| 98 | |
| 99 | while args_argv: |
| 100 | arg = args_argv[0] |
| 101 | if arg == '--': |
| 102 | argv = args_argv[1:] |
| 103 | break |
| 104 | else: |
| 105 | args.append(arg) |
| 106 | del args_argv[0] |
| 107 | |
| 108 | |
| 109 | def setup(sim): |
| 110 | global opts |
| 111 | global args |
| 112 | |
| 113 | #for strand in eval('Slicer(sim.s)['+opts.available+']'): |
| 114 | #strand.available() |
| 115 | #for strand in eval('Slicer(sim.s)['+opts.enable+']'): |
| 116 | #strand.enable() |
| 117 | #for strand in eval('Slicer(sim.s)['+opts.running+']'): |
| 118 | #strand.running() |
| 119 | #for strand in eval('Slicer(sim.s)['+opts.stepping+']'): |
| 120 | #strand.stepping() |
| 121 | |
| 122 | #for file in opts.file: |
| 123 | #sim.mem.load(file) |
| 124 | |
| 125 | for strand in sim.s: |
| 126 | strand.lstmode(opts.lstmode and 1 or 0) |
| 127 | |
| 128 | if opts.interactive or len(args) == 0: |
| 129 | os.environ['PYTHONINSPECT'] = 'x' |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |