__init__.py revision 2738
11736SN/A# Copyright (c) 2005 The Regents of The University of Michigan
21736SN/A# All rights reserved.
31736SN/A#
41736SN/A# Redistribution and use in source and binary forms, with or without
51736SN/A# modification, are permitted provided that the following conditions are
61736SN/A# met: redistributions of source code must retain the above copyright
71736SN/A# notice, this list of conditions and the following disclaimer;
81736SN/A# redistributions in binary form must reproduce the above copyright
91736SN/A# notice, this list of conditions and the following disclaimer in the
101736SN/A# documentation and/or other materials provided with the distribution;
111736SN/A# neither the name of the copyright holders nor the names of its
121736SN/A# contributors may be used to endorse or promote products derived from
131736SN/A# this software without specific prior written permission.
141736SN/A#
151736SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161736SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171736SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
181736SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
191736SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
201736SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
211736SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221736SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231736SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241736SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
251736SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262665Ssaidi@eecs.umich.edu#
272665Ssaidi@eecs.umich.edu# Authors: Nathan Binkert
282665Ssaidi@eecs.umich.edu#          Steve Reinhardt
291736SN/A
302667Sstever@eecs.umich.eduimport sys, os, time, atexit, optparse
312655Sstever@eecs.umich.edu
322667Sstever@eecs.umich.edu# import the SWIG-wrapped main C++ functions
332667Sstever@eecs.umich.eduimport main
342667Sstever@eecs.umich.edu# import a few SWIG-wrapped items (those that are likely to be used
352667Sstever@eecs.umich.edu# directly by user scripts) completely into this module for
362667Sstever@eecs.umich.edu# convenience
372667Sstever@eecs.umich.edufrom main import simulate, SimLoopExitEvent
382655Sstever@eecs.umich.edu
392667Sstever@eecs.umich.edu# import the m5 compile options
402667Sstever@eecs.umich.eduimport defines
411530SN/A
421530SN/A# define this here so we can use it right away if necessary
431530SN/Adef panic(string):
441530SN/A    print >>sys.stderr, 'panic:', string
451530SN/A    sys.exit(1)
461530SN/A
472667Sstever@eecs.umich.edu# Prepend given directory to system module search path.  We may not
482667Sstever@eecs.umich.edu# need this anymore if we can structure our config library more like a
492667Sstever@eecs.umich.edu# Python package.
501692SN/Adef AddToPath(path):
511869SN/A    # if it's a relative path and we know what directory the current
521869SN/A    # python script is in, make the path relative to that directory.
531869SN/A    if not os.path.isabs(path) and sys.path[0]:
541869SN/A        path = os.path.join(sys.path[0], path)
551692SN/A    path = os.path.realpath(path)
561869SN/A    # sys.path[0] should always refer to the current script's directory,
571869SN/A    # so place the new dir right after that.
581869SN/A    sys.path.insert(1, path)
591581SN/A
602667Sstever@eecs.umich.edu
612667Sstever@eecs.umich.edu# Callback to set trace flags.  Not necessarily the best way to do
622667Sstever@eecs.umich.edu# things in the long run (particularly if we change how these global
632667Sstever@eecs.umich.edu# options are handled).
642667Sstever@eecs.umich.edudef setTraceFlags(option, opt_str, value, parser):
652667Sstever@eecs.umich.edu    objects.Trace.flags = value
662667Sstever@eecs.umich.edu
672667Sstever@eecs.umich.edu# Standard optparse options.  Need to be explicitly included by the
682667Sstever@eecs.umich.edu# user script when it calls optparse.OptionParser().
692667Sstever@eecs.umich.edustandardOptions = [
702667Sstever@eecs.umich.edu    optparse.make_option("--traceflags", type="string", action="callback",
712667Sstever@eecs.umich.edu                         callback=setTraceFlags)
722667Sstever@eecs.umich.edu    ]
731530SN/A
741530SN/A# make a SmartDict out of the build options for our local use
751530SN/Aimport smartdict
761530SN/Abuild_env = smartdict.SmartDict()
772667Sstever@eecs.umich.edubuild_env.update(defines.m5_build_env)
781530SN/A
791530SN/A# make a SmartDict out of the OS environment too
801530SN/Aenv = smartdict.SmartDict()
811530SN/Aenv.update(os.environ)
821530SN/A
832738Sstever@eecs.umich.edu
842738Sstever@eecs.umich.edu# Function to provide to C++ so it can look up instances based on paths
852738Sstever@eecs.umich.edudef resolveSimObject(name):
862738Sstever@eecs.umich.edu    obj = config.instanceDict[name]
872738Sstever@eecs.umich.edu    if not obj._ccObject:
882738Sstever@eecs.umich.edu        obj.createCCObject()
892738Sstever@eecs.umich.edu    if obj._ccObject == -1:
902738Sstever@eecs.umich.edu        panic("resolveSimObject: recursive lookup error on %s" % name)
912738Sstever@eecs.umich.edu    return obj._ccObject
922738Sstever@eecs.umich.edu
932667Sstever@eecs.umich.edu# The final hook to generate .ini files.  Called from the user script
942667Sstever@eecs.umich.edu# once the config is built.
952667Sstever@eecs.umich.edudef instantiate(root):
962667Sstever@eecs.umich.edu    config.ticks_per_sec = float(root.clock.frequency)
972667Sstever@eecs.umich.edu    # ugly temporary hack to get output to config.ini
982667Sstever@eecs.umich.edu    sys.stdout = file('config.ini', 'w')
992667Sstever@eecs.umich.edu    root.print_ini()
1002667Sstever@eecs.umich.edu    sys.stdout.close() # close config.ini
1012667Sstever@eecs.umich.edu    sys.stdout = sys.__stdout__ # restore to original
1022738Sstever@eecs.umich.edu    main.loadIniFile(resolveSimObject)  # load config.ini into C++
1032738Sstever@eecs.umich.edu    root.createCCObject()
1042738Sstever@eecs.umich.edu    root.connectPorts()
1052738Sstever@eecs.umich.edu    main.finalInit()
1062667Sstever@eecs.umich.edu    noDot = True # temporary until we fix dot
1072667Sstever@eecs.umich.edu    if not noDot:
1082667Sstever@eecs.umich.edu       dot = pydot.Dot()
1092667Sstever@eecs.umich.edu       instance.outputDot(dot)
1102667Sstever@eecs.umich.edu       dot.orientation = "portrait"
1112667Sstever@eecs.umich.edu       dot.size = "8.5,11"
1122667Sstever@eecs.umich.edu       dot.ranksep="equally"
1132667Sstever@eecs.umich.edu       dot.rank="samerank"
1142667Sstever@eecs.umich.edu       dot.write("config.dot")
1152667Sstever@eecs.umich.edu       dot.write_ps("config.ps")
1161527SN/A
1172667Sstever@eecs.umich.edu# Export curTick to user script.
1182667Sstever@eecs.umich.edudef curTick():
1192667Sstever@eecs.umich.edu    return main.cvar.curTick
1201511SN/A
1212667Sstever@eecs.umich.edu# register our C++ exit callback function with Python
1222667Sstever@eecs.umich.eduatexit.register(main.doExitCleanup)
1232655Sstever@eecs.umich.edu
1242667Sstever@eecs.umich.edu# This import allows user scripts to reference 'm5.objects.Foo' after
1252667Sstever@eecs.umich.edu# just doing an 'import m5' (without an 'import m5.objects').  May not
1262667Sstever@eecs.umich.edu# matter since most scripts will probably 'from m5.objects import *'.
1272667Sstever@eecs.umich.eduimport objects
128