__init__.py revision 2759:d6b4f091d9dd
16184SN/A# Copyright (c) 2005 The Regents of The University of Michigan
26184SN/A# All rights reserved.
36184SN/A#
46184SN/A# Redistribution and use in source and binary forms, with or without
56184SN/A# modification, are permitted provided that the following conditions are
66184SN/A# met: redistributions of source code must retain the above copyright
76184SN/A# notice, this list of conditions and the following disclaimer;
86184SN/A# redistributions in binary form must reproduce the above copyright
96184SN/A# notice, this list of conditions and the following disclaimer in the
106184SN/A# documentation and/or other materials provided with the distribution;
116184SN/A# neither the name of the copyright holders nor the names of its
126184SN/A# contributors may be used to endorse or promote products derived from
136184SN/A# this software without specific prior written permission.
146184SN/A#
156184SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166184SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176184SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186184SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196184SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206184SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216184SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226184SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236184SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246184SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256184SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266184SN/A#
276184SN/A# Authors: Nathan Binkert
286184SN/A#          Steve Reinhardt
296184SN/A
306184SN/Aimport sys, os, time, atexit, optparse
316226Snate@binkert.org
326184SN/A# import the SWIG-wrapped main C++ functions
336184SN/Aimport main
346184SN/A# import a few SWIG-wrapped items (those that are likely to be used
356184SN/A# directly by user scripts) completely into this module for
366184SN/A# convenience
376184SN/Afrom main import simulate, SimLoopExitEvent
386184SN/A
396184SN/A# import the m5 compile options
406184SN/Aimport defines
416184SN/A
426184SN/A# define this here so we can use it right away if necessary
436184SN/Adef panic(string):
446184SN/A    print >>sys.stderr, 'panic:', string
456184SN/A    sys.exit(1)
466184SN/A
476184SN/A# Prepend given directory to system module search path.  We may not
486184SN/A# need this anymore if we can structure our config library more like a
496184SN/A# Python package.
506184SN/Adef AddToPath(path):
516184SN/A    # if it's a relative path and we know what directory the current
526184SN/A    # python script is in, make the path relative to that directory.
536184SN/A    if not os.path.isabs(path) and sys.path[0]:
546184SN/A        path = os.path.join(sys.path[0], path)
556184SN/A    path = os.path.realpath(path)
566184SN/A    # sys.path[0] should always refer to the current script's directory,
576184SN/A    # so place the new dir right after that.
586184SN/A    sys.path.insert(1, path)
596184SN/A
606184SN/A
616184SN/A# Callback to set trace flags.  Not necessarily the best way to do
626184SN/A# things in the long run (particularly if we change how these global
636184SN/A# options are handled).
646184SN/Adef setTraceFlags(option, opt_str, value, parser):
656184SN/A    objects.Trace.flags = value
666184SN/A
676184SN/Adef setTraceStart(option, opt_str, value, parser):
686184SN/A    objects.Trace.start = value
696184SN/A
706184SN/Adef setTraceFile(option, opt_str, value, parser):
716184SN/A    objects.Trace.file = value
726184SN/A
736184SN/Adef usePCSymbol(option, opt_str, value, parser):
746184SN/A    objects.ExecutionTrace.pc_symbol = value
756184SN/A
766184SN/Adef printCycle(option, opt_str, value, parser):
776184SN/A    objects.ExecutionTrace.print_cycle = value
786184SN/A
796184SN/Adef printOp(option, opt_str, value, parser):
806184SN/A    objects.ExecutionTrace.print_opclass = value
816184SN/A
826184SN/Adef printThread(option, opt_str, value, parser):
836184SN/A    objects.ExecutionTrace.print_thread = value
846184SN/A
85def printEA(option, opt_str, value, parser):
86    objects.ExecutionTrace.print_effaddr = value
87
88def printData(option, opt_str, value, parser):
89    objects.ExecutionTrace.print_data = value
90
91def printFetchseq(option, opt_str, value, parser):
92    objects.ExecutionTrace.print_fetchseq = value
93
94def printCpseq(option, opt_str, value, parser):
95    objects.ExecutionTrace.print_cpseq = value
96
97def dumpOnExit(option, opt_str, value, parser):
98    objects.Trace.dump_on_exit = value
99
100def debugBreak(option, opt_str, value, parser):
101    objects.Debug.break_cycles = value
102
103def statsTextFile(option, opt_str, value, parser):
104    objects.Statistics.text_file = value
105
106# Extra list to help for options that are true or false
107TrueOrFalse = ['True', 'False']
108TorF = "True | False"
109
110# Standard optparse options.  Need to be explicitly included by the
111# user script when it calls optparse.OptionParser().
112standardOptions = [
113    optparse.make_option("--traceflags", type="string", action="callback",
114                         callback=setTraceFlags),
115    optparse.make_option("--tracestart", type="int", action="callback",
116                         callback=setTraceStart),
117    optparse.make_option("--tracefile", type="string", action="callback",
118                         callback=setTraceFile),
119    optparse.make_option("--pcsymbol", type="choice", choices=TrueOrFalse,
120                         default="True", metavar=TorF,
121                         action="callback", callback=usePCSymbol,
122                         help="Use PC symbols in trace output"),
123    optparse.make_option("--printcycle", type="choice", choices=TrueOrFalse,
124                         default="True", metavar=TorF,
125                         action="callback", callback=printCycle,
126                         help="Print cycle numbers in trace output"),
127    optparse.make_option("--printopclass", type="choice",
128                         choices=TrueOrFalse,
129                         default="True", metavar=TorF,
130                         action="callback", callback=printOp,
131                         help="Print cycle numbers in trace output"),
132    optparse.make_option("--printthread", type="choice",
133                         choices=TrueOrFalse,
134                         default="True", metavar=TorF,
135                         action="callback", callback=printThread,
136                         help="Print thread number in trace output"),
137    optparse.make_option("--printeffaddr", type="choice",
138                         choices=TrueOrFalse,
139                         default="True", metavar=TorF,
140                         action="callback", callback=printEA,
141                         help="Print effective address in trace output"),
142    optparse.make_option("--printdata", type="choice",
143                         choices=TrueOrFalse,
144                         default="True", metavar=TorF,
145                         action="callback", callback=printData,
146                         help="Print result data in trace output"),
147    optparse.make_option("--printfetchseq", type="choice",
148                         choices=TrueOrFalse,
149                         default="True", metavar=TorF,
150                         action="callback", callback=printFetchseq,
151                         help="Print fetch sequence numbers in trace output"),
152    optparse.make_option("--printcpseq", type="choice",
153                         choices=TrueOrFalse,
154                         default="True", metavar=TorF,
155                         action="callback", callback=printCpseq,
156                         help="Print correct path sequence numbers in trace output"),
157    optparse.make_option("--dumponexit", type="choice",
158                         choices=TrueOrFalse,
159                         default="True", metavar=TorF,
160                         action="callback", callback=dumpOnExit,
161                         help="Dump trace buffer on exit"),
162    optparse.make_option("--debugbreak", type="int", metavar="CYCLE",
163                         action="callback", callback=debugBreak,
164                         help="Cycle to create a breakpoint"),
165    optparse.make_option("--statsfile", type="string", action="callback",
166                         callback=statsTextFile, metavar="FILE",
167                         help="Sets the output file for the statistics")
168    ]
169
170# make a SmartDict out of the build options for our local use
171import smartdict
172build_env = smartdict.SmartDict()
173build_env.update(defines.m5_build_env)
174
175# make a SmartDict out of the OS environment too
176env = smartdict.SmartDict()
177env.update(os.environ)
178
179
180# Function to provide to C++ so it can look up instances based on paths
181def resolveSimObject(name):
182    obj = config.instanceDict[name]
183    return obj.getCCObject()
184
185# The final hook to generate .ini files.  Called from the user script
186# once the config is built.
187def instantiate(root):
188    config.ticks_per_sec = float(root.clock.frequency)
189    # ugly temporary hack to get output to config.ini
190    sys.stdout = file('config.ini', 'w')
191    root.print_ini()
192    sys.stdout.close() # close config.ini
193    sys.stdout = sys.__stdout__ # restore to original
194    main.loadIniFile(resolveSimObject)  # load config.ini into C++
195    root.createCCObject()
196    root.connectPorts()
197    main.finalInit()
198    noDot = True # temporary until we fix dot
199    if not noDot:
200       dot = pydot.Dot()
201       instance.outputDot(dot)
202       dot.orientation = "portrait"
203       dot.size = "8.5,11"
204       dot.ranksep="equally"
205       dot.rank="samerank"
206       dot.write("config.dot")
207       dot.write_ps("config.ps")
208
209# Export curTick to user script.
210def curTick():
211    return main.cvar.curTick
212
213# register our C++ exit callback function with Python
214atexit.register(main.doExitCleanup)
215
216# This import allows user scripts to reference 'm5.objects.Foo' after
217# just doing an 'import m5' (without an 'import m5.objects').  May not
218# matter since most scripts will probably 'from m5.objects import *'.
219import objects
220