params.py revision 8597
13101Sstever@eecs.umich.edu# Copyright (c) 2004-2006 The Regents of The University of Michigan
28579Ssteve.reinhardt@amd.com# Copyright (c) 2010-2011 Advanced Micro Devices, Inc.
33101Sstever@eecs.umich.edu# All rights reserved.
43101Sstever@eecs.umich.edu#
53101Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
63101Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
73101Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
83101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
93101Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
103101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
113101Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
123101Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
133101Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
143101Sstever@eecs.umich.edu# this software without specific prior written permission.
153101Sstever@eecs.umich.edu#
163101Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173101Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183101Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193101Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203101Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213101Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223101Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233101Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243101Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253101Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263101Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273101Sstever@eecs.umich.edu#
283101Sstever@eecs.umich.edu# Authors: Steve Reinhardt
293101Sstever@eecs.umich.edu#          Nathan Binkert
307778Sgblack@eecs.umich.edu#          Gabe Black
313101Sstever@eecs.umich.edu
323101Sstever@eecs.umich.edu#####################################################################
333101Sstever@eecs.umich.edu#
343101Sstever@eecs.umich.edu# Parameter description classes
353101Sstever@eecs.umich.edu#
363101Sstever@eecs.umich.edu# The _params dictionary in each class maps parameter names to either
373101Sstever@eecs.umich.edu# a Param or a VectorParam object.  These objects contain the
383101Sstever@eecs.umich.edu# parameter description string, the parameter type, and the default
393101Sstever@eecs.umich.edu# value (if any).  The convert() method on these objects is used to
403101Sstever@eecs.umich.edu# force whatever value is assigned to the parameter to the appropriate
413101Sstever@eecs.umich.edu# type.
423101Sstever@eecs.umich.edu#
433101Sstever@eecs.umich.edu# Note that the default values are loaded into the class's attribute
443101Sstever@eecs.umich.edu# space when the parameter dictionary is initialized (in
453101Sstever@eecs.umich.edu# MetaSimObject._new_param()); after that point they aren't used.
463101Sstever@eecs.umich.edu#
473101Sstever@eecs.umich.edu#####################################################################
483101Sstever@eecs.umich.edu
493885Sbinkertn@umich.eduimport copy
503885Sbinkertn@umich.eduimport datetime
514762Snate@binkert.orgimport re
523885Sbinkertn@umich.eduimport sys
533885Sbinkertn@umich.eduimport time
547528Ssteve.reinhardt@amd.comimport math
553885Sbinkertn@umich.edu
564380Sbinkertn@umich.eduimport proxy
574167Sbinkertn@umich.eduimport ticks
583102Sstever@eecs.umich.edufrom util import *
593101Sstever@eecs.umich.edu
604762Snate@binkert.orgdef isSimObject(*args, **kwargs):
614762Snate@binkert.org    return SimObject.isSimObject(*args, **kwargs)
624762Snate@binkert.org
634762Snate@binkert.orgdef isSimObjectSequence(*args, **kwargs):
644762Snate@binkert.org    return SimObject.isSimObjectSequence(*args, **kwargs)
654762Snate@binkert.org
664762Snate@binkert.orgdef isSimObjectClass(*args, **kwargs):
674762Snate@binkert.org    return SimObject.isSimObjectClass(*args, **kwargs)
684762Snate@binkert.org
695033Smilesck@eecs.umich.eduallParams = {}
705033Smilesck@eecs.umich.edu
715033Smilesck@eecs.umich.educlass MetaParamValue(type):
725033Smilesck@eecs.umich.edu    def __new__(mcls, name, bases, dct):
735033Smilesck@eecs.umich.edu        cls = super(MetaParamValue, mcls).__new__(mcls, name, bases, dct)
745033Smilesck@eecs.umich.edu        assert name not in allParams
755033Smilesck@eecs.umich.edu        allParams[name] = cls
765033Smilesck@eecs.umich.edu        return cls
775033Smilesck@eecs.umich.edu
785033Smilesck@eecs.umich.edu
793101Sstever@eecs.umich.edu# Dummy base class to identify types that are legitimate for SimObject
803101Sstever@eecs.umich.edu# parameters.
813101Sstever@eecs.umich.educlass ParamValue(object):
825033Smilesck@eecs.umich.edu    __metaclass__ = MetaParamValue
833101Sstever@eecs.umich.edu
848596Ssteve.reinhardt@amd.com
858596Ssteve.reinhardt@amd.com    # Generate the code needed as a prerequisite for declaring a C++
868596Ssteve.reinhardt@amd.com    # object of this type.  Typically generates one or more #include
878596Ssteve.reinhardt@amd.com    # statements.  Used when declaring parameters of this type.
887673Snate@binkert.org    @classmethod
897673Snate@binkert.org    def cxx_predecls(cls, code):
907673Snate@binkert.org        pass
917673Snate@binkert.org
928596Ssteve.reinhardt@amd.com    # Generate the code needed as a prerequisite for including a
938596Ssteve.reinhardt@amd.com    # reference to a C++ object of this type in a SWIG .i file.
948596Ssteve.reinhardt@amd.com    # Typically generates one or more %import or %include statements.
957673Snate@binkert.org    @classmethod
967673Snate@binkert.org    def swig_predecls(cls, code):
977673Snate@binkert.org        pass
983101Sstever@eecs.umich.edu
993101Sstever@eecs.umich.edu    # default for printing to .ini file is regular string conversion.
1003101Sstever@eecs.umich.edu    # will be overridden in some cases
1013101Sstever@eecs.umich.edu    def ini_str(self):
1023101Sstever@eecs.umich.edu        return str(self)
1033101Sstever@eecs.umich.edu
1043101Sstever@eecs.umich.edu    # allows us to blithely call unproxy() on things without checking
1053101Sstever@eecs.umich.edu    # if they're really proxies or not
1063101Sstever@eecs.umich.edu    def unproxy(self, base):
1073101Sstever@eecs.umich.edu        return self
1083101Sstever@eecs.umich.edu
1093101Sstever@eecs.umich.edu# Regular parameter description.
1103101Sstever@eecs.umich.educlass ParamDesc(object):
1113101Sstever@eecs.umich.edu    def __init__(self, ptype_str, ptype, *args, **kwargs):
1123101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
1133101Sstever@eecs.umich.edu        # remember ptype only if it is provided
1143101Sstever@eecs.umich.edu        if ptype != None:
1153101Sstever@eecs.umich.edu            self.ptype = ptype
1163101Sstever@eecs.umich.edu
1173101Sstever@eecs.umich.edu        if args:
1183101Sstever@eecs.umich.edu            if len(args) == 1:
1193101Sstever@eecs.umich.edu                self.desc = args[0]
1203101Sstever@eecs.umich.edu            elif len(args) == 2:
1213101Sstever@eecs.umich.edu                self.default = args[0]
1223101Sstever@eecs.umich.edu                self.desc = args[1]
1233101Sstever@eecs.umich.edu            else:
1243101Sstever@eecs.umich.edu                raise TypeError, 'too many arguments'
1253101Sstever@eecs.umich.edu
1263101Sstever@eecs.umich.edu        if kwargs.has_key('desc'):
1273101Sstever@eecs.umich.edu            assert(not hasattr(self, 'desc'))
1283101Sstever@eecs.umich.edu            self.desc = kwargs['desc']
1293101Sstever@eecs.umich.edu            del kwargs['desc']
1303101Sstever@eecs.umich.edu
1313101Sstever@eecs.umich.edu        if kwargs.has_key('default'):
1323101Sstever@eecs.umich.edu            assert(not hasattr(self, 'default'))
1333101Sstever@eecs.umich.edu            self.default = kwargs['default']
1343101Sstever@eecs.umich.edu            del kwargs['default']
1353101Sstever@eecs.umich.edu
1363101Sstever@eecs.umich.edu        if kwargs:
1373101Sstever@eecs.umich.edu            raise TypeError, 'extra unknown kwargs %s' % kwargs
1383101Sstever@eecs.umich.edu
1393101Sstever@eecs.umich.edu        if not hasattr(self, 'desc'):
1403101Sstever@eecs.umich.edu            raise TypeError, 'desc attribute missing'
1413101Sstever@eecs.umich.edu
1423101Sstever@eecs.umich.edu    def __getattr__(self, attr):
1433101Sstever@eecs.umich.edu        if attr == 'ptype':
1445033Smilesck@eecs.umich.edu            ptype = SimObject.allClasses[self.ptype_str]
1456656Snate@binkert.org            assert isSimObjectClass(ptype)
1465033Smilesck@eecs.umich.edu            self.ptype = ptype
1475033Smilesck@eecs.umich.edu            return ptype
1485033Smilesck@eecs.umich.edu
1493101Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
1503101Sstever@eecs.umich.edu              (type(self).__name__, attr)
1513101Sstever@eecs.umich.edu
1523101Sstever@eecs.umich.edu    def convert(self, value):
1533101Sstever@eecs.umich.edu        if isinstance(value, proxy.BaseProxy):
1543101Sstever@eecs.umich.edu            value.set_param_desc(self)
1553101Sstever@eecs.umich.edu            return value
1563101Sstever@eecs.umich.edu        if not hasattr(self, 'ptype') and isNullPointer(value):
1573101Sstever@eecs.umich.edu            # deferred evaluation of SimObject; continue to defer if
1583101Sstever@eecs.umich.edu            # we're just assigning a null pointer
1593101Sstever@eecs.umich.edu            return value
1603101Sstever@eecs.umich.edu        if isinstance(value, self.ptype):
1613101Sstever@eecs.umich.edu            return value
1623102Sstever@eecs.umich.edu        if isNullPointer(value) and isSimObjectClass(self.ptype):
1633101Sstever@eecs.umich.edu            return value
1643101Sstever@eecs.umich.edu        return self.ptype(value)
1653101Sstever@eecs.umich.edu
1667673Snate@binkert.org    def cxx_predecls(self, code):
1677673Snate@binkert.org        self.ptype.cxx_predecls(code)
1683101Sstever@eecs.umich.edu
1697673Snate@binkert.org    def swig_predecls(self, code):
1707673Snate@binkert.org        self.ptype.swig_predecls(code)
1713101Sstever@eecs.umich.edu
1727673Snate@binkert.org    def cxx_decl(self, code):
1737673Snate@binkert.org        code('${{self.ptype.cxx_type}} ${{self.name}};')
1743101Sstever@eecs.umich.edu
1753101Sstever@eecs.umich.edu# Vector-valued parameter description.  Just like ParamDesc, except
1763101Sstever@eecs.umich.edu# that the value is a vector (list) of the specified type instead of a
1773101Sstever@eecs.umich.edu# single value.
1783101Sstever@eecs.umich.edu
1793101Sstever@eecs.umich.educlass VectorParamValue(list):
1805033Smilesck@eecs.umich.edu    __metaclass__ = MetaParamValue
1815475Snate@binkert.org    def __setattr__(self, attr, value):
1825475Snate@binkert.org        raise AttributeError, \
1835475Snate@binkert.org              "Not allowed to set %s on '%s'" % (attr, type(self).__name__)
1845475Snate@binkert.org
1853101Sstever@eecs.umich.edu    def ini_str(self):
1863101Sstever@eecs.umich.edu        return ' '.join([v.ini_str() for v in self])
1873101Sstever@eecs.umich.edu
1884762Snate@binkert.org    def getValue(self):
1894762Snate@binkert.org        return [ v.getValue() for v in self ]
1904762Snate@binkert.org
1913101Sstever@eecs.umich.edu    def unproxy(self, base):
1928460SAli.Saidi@ARM.com        if len(self) == 1 and isinstance(self[0], proxy.AllProxy):
1938459SAli.Saidi@ARM.com            return self[0].unproxy(base)
1948459SAli.Saidi@ARM.com        else:
1958459SAli.Saidi@ARM.com             return [v.unproxy(base) for v in self]
1963101Sstever@eecs.umich.edu
1977528Ssteve.reinhardt@amd.comclass SimObjectVector(VectorParamValue):
1987528Ssteve.reinhardt@amd.com    # support clone operation
1997528Ssteve.reinhardt@amd.com    def __call__(self, **kwargs):
2007528Ssteve.reinhardt@amd.com        return SimObjectVector([v(**kwargs) for v in self])
2017528Ssteve.reinhardt@amd.com
2027528Ssteve.reinhardt@amd.com    def clear_parent(self, old_parent):
2033101Sstever@eecs.umich.edu        for v in self:
2047528Ssteve.reinhardt@amd.com            v.clear_parent(old_parent)
2057528Ssteve.reinhardt@amd.com
2067528Ssteve.reinhardt@amd.com    def set_parent(self, parent, name):
2077528Ssteve.reinhardt@amd.com        if len(self) == 1:
2087528Ssteve.reinhardt@amd.com            self[0].set_parent(parent, name)
2097528Ssteve.reinhardt@amd.com        else:
2107528Ssteve.reinhardt@amd.com            width = int(math.ceil(math.log(len(self))/math.log(10)))
2117528Ssteve.reinhardt@amd.com            for i,v in enumerate(self):
2127528Ssteve.reinhardt@amd.com                v.set_parent(parent, "%s%0*d" % (name, width, i))
2137528Ssteve.reinhardt@amd.com
2148321Ssteve.reinhardt@amd.com    def has_parent(self):
2158321Ssteve.reinhardt@amd.com        return reduce(lambda x,y: x and y, [v.has_parent() for v in self])
2167528Ssteve.reinhardt@amd.com
2177528Ssteve.reinhardt@amd.com    # return 'cpu0 cpu1' etc. for print_ini()
2187528Ssteve.reinhardt@amd.com    def get_name(self):
2197528Ssteve.reinhardt@amd.com        return ' '.join([v._name for v in self])
2207528Ssteve.reinhardt@amd.com
2217528Ssteve.reinhardt@amd.com    # By iterating through the constituent members of the vector here
2227528Ssteve.reinhardt@amd.com    # we can nicely handle iterating over all a SimObject's children
2237528Ssteve.reinhardt@amd.com    # without having to provide lots of special functions on
2247528Ssteve.reinhardt@amd.com    # SimObjectVector directly.
2257528Ssteve.reinhardt@amd.com    def descendants(self):
2267528Ssteve.reinhardt@amd.com        for v in self:
2277528Ssteve.reinhardt@amd.com            for obj in v.descendants():
2287528Ssteve.reinhardt@amd.com                yield obj
2293101Sstever@eecs.umich.edu
2303101Sstever@eecs.umich.educlass VectorParamDesc(ParamDesc):
2313101Sstever@eecs.umich.edu    # Convert assigned value to appropriate type.  If the RHS is not a
2323101Sstever@eecs.umich.edu    # list or tuple, it generates a single-element list.
2333101Sstever@eecs.umich.edu    def convert(self, value):
2343101Sstever@eecs.umich.edu        if isinstance(value, (list, tuple)):
2353101Sstever@eecs.umich.edu            # list: coerce each element into new list
2363101Sstever@eecs.umich.edu            tmp_list = [ ParamDesc.convert(self, v) for v in value ]
2373101Sstever@eecs.umich.edu        else:
2384762Snate@binkert.org            # singleton: coerce to a single-element list
2394762Snate@binkert.org            tmp_list = [ ParamDesc.convert(self, value) ]
2404762Snate@binkert.org
2414762Snate@binkert.org        if isSimObjectSequence(tmp_list):
2427528Ssteve.reinhardt@amd.com            return SimObjectVector(tmp_list)
2434762Snate@binkert.org        else:
2444762Snate@binkert.org            return VectorParamValue(tmp_list)
2454762Snate@binkert.org
2468596Ssteve.reinhardt@amd.com    def swig_module_name(self):
2478596Ssteve.reinhardt@amd.com        return "%s_vector" % self.ptype_str
2488596Ssteve.reinhardt@amd.com
2497673Snate@binkert.org    def swig_predecls(self, code):
2508596Ssteve.reinhardt@amd.com        code('%import "${{self.swig_module_name()}}.i"')
2514762Snate@binkert.org
2527673Snate@binkert.org    def swig_decl(self, code):
2538596Ssteve.reinhardt@amd.com        code('%module(package="m5.internal") ${{self.swig_module_name()}}')
2547675Snate@binkert.org        code('%{')
2557675Snate@binkert.org        self.ptype.cxx_predecls(code)
2567675Snate@binkert.org        code('%}')
2577675Snate@binkert.org        code()
2587675Snate@binkert.org        self.ptype.swig_predecls(code)
2597675Snate@binkert.org        code()
2607673Snate@binkert.org        code('%include "std_vector.i"')
2617675Snate@binkert.org        code()
2627675Snate@binkert.org
2637675Snate@binkert.org        ptype = self.ptype_str
2647675Snate@binkert.org        cxx_type = self.ptype.cxx_type
2657675Snate@binkert.org
2667673Snate@binkert.org        code('''\
2677675Snate@binkert.org%typemap(in) std::vector< $cxx_type >::value_type {
2687675Snate@binkert.org    if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
2697675Snate@binkert.org        if (SWIG_ConvertPtr($$input, (void **)&$$1,
2707675Snate@binkert.org                            $$descriptor($cxx_type), 0) == -1) {
2717675Snate@binkert.org            return NULL;
2727675Snate@binkert.org        }
2737675Snate@binkert.org    }
2747675Snate@binkert.org}
2757675Snate@binkert.org
2767675Snate@binkert.org%typemap(in) std::vector< $cxx_type >::value_type * {
2777675Snate@binkert.org    if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
2787675Snate@binkert.org        if (SWIG_ConvertPtr($$input, (void **)&$$1,
2797675Snate@binkert.org                            $$descriptor($cxx_type *), 0) == -1) {
2807675Snate@binkert.org            return NULL;
2817675Snate@binkert.org        }
2827675Snate@binkert.org    }
2837673Snate@binkert.org}
2847673Snate@binkert.org''')
2853101Sstever@eecs.umich.edu
2867675Snate@binkert.org        code('%template(vector_$ptype) std::vector< $cxx_type >;')
2877675Snate@binkert.org
2887673Snate@binkert.org    def cxx_predecls(self, code):
2897673Snate@binkert.org        code('#include <vector>')
2907673Snate@binkert.org        self.ptype.cxx_predecls(code)
2913101Sstever@eecs.umich.edu
2927673Snate@binkert.org    def cxx_decl(self, code):
2937673Snate@binkert.org        code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
2943101Sstever@eecs.umich.edu
2953101Sstever@eecs.umich.educlass ParamFactory(object):
2963101Sstever@eecs.umich.edu    def __init__(self, param_desc_class, ptype_str = None):
2973101Sstever@eecs.umich.edu        self.param_desc_class = param_desc_class
2983101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
2993101Sstever@eecs.umich.edu
3003101Sstever@eecs.umich.edu    def __getattr__(self, attr):
3013101Sstever@eecs.umich.edu        if self.ptype_str:
3023101Sstever@eecs.umich.edu            attr = self.ptype_str + '.' + attr
3033101Sstever@eecs.umich.edu        return ParamFactory(self.param_desc_class, attr)
3043101Sstever@eecs.umich.edu
3053101Sstever@eecs.umich.edu    # E.g., Param.Int(5, "number of widgets")
3063101Sstever@eecs.umich.edu    def __call__(self, *args, **kwargs):
3073101Sstever@eecs.umich.edu        ptype = None
3083101Sstever@eecs.umich.edu        try:
3095033Smilesck@eecs.umich.edu            ptype = allParams[self.ptype_str]
3105033Smilesck@eecs.umich.edu        except KeyError:
3113101Sstever@eecs.umich.edu            # if name isn't defined yet, assume it's a SimObject, and
3123101Sstever@eecs.umich.edu            # try to resolve it later
3133101Sstever@eecs.umich.edu            pass
3143101Sstever@eecs.umich.edu        return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs)
3153101Sstever@eecs.umich.edu
3163101Sstever@eecs.umich.eduParam = ParamFactory(ParamDesc)
3173101Sstever@eecs.umich.eduVectorParam = ParamFactory(VectorParamDesc)
3183101Sstever@eecs.umich.edu
3193101Sstever@eecs.umich.edu#####################################################################
3203101Sstever@eecs.umich.edu#
3213101Sstever@eecs.umich.edu# Parameter Types
3223101Sstever@eecs.umich.edu#
3233101Sstever@eecs.umich.edu# Though native Python types could be used to specify parameter types
3243101Sstever@eecs.umich.edu# (the 'ptype' field of the Param and VectorParam classes), it's more
3253101Sstever@eecs.umich.edu# flexible to define our own set of types.  This gives us more control
3263101Sstever@eecs.umich.edu# over how Python expressions are converted to values (via the
3273101Sstever@eecs.umich.edu# __init__() constructor) and how these values are printed out (via
3283101Sstever@eecs.umich.edu# the __str__() conversion method).
3293101Sstever@eecs.umich.edu#
3303101Sstever@eecs.umich.edu#####################################################################
3313101Sstever@eecs.umich.edu
3323101Sstever@eecs.umich.edu# String-valued parameter.  Just mixin the ParamValue class with the
3333101Sstever@eecs.umich.edu# built-in str class.
3343101Sstever@eecs.umich.educlass String(ParamValue,str):
3353101Sstever@eecs.umich.edu    cxx_type = 'std::string'
3367673Snate@binkert.org
3377673Snate@binkert.org    @classmethod
3387673Snate@binkert.org    def cxx_predecls(self, code):
3397673Snate@binkert.org        code('#include <string>')
3407673Snate@binkert.org
3417673Snate@binkert.org    @classmethod
3427673Snate@binkert.org    def swig_predecls(cls, code):
3437673Snate@binkert.org        code('%include "std_string.i"')
3444762Snate@binkert.org
3454762Snate@binkert.org    def getValue(self):
3464762Snate@binkert.org        return self
3473101Sstever@eecs.umich.edu
3483101Sstever@eecs.umich.edu# superclass for "numeric" parameter values, to emulate math
3493101Sstever@eecs.umich.edu# operations in a type-safe way.  e.g., a Latency times an int returns
3503101Sstever@eecs.umich.edu# a new Latency object.
3513101Sstever@eecs.umich.educlass NumericParamValue(ParamValue):
3523101Sstever@eecs.umich.edu    def __str__(self):
3533101Sstever@eecs.umich.edu        return str(self.value)
3543101Sstever@eecs.umich.edu
3553101Sstever@eecs.umich.edu    def __float__(self):
3563101Sstever@eecs.umich.edu        return float(self.value)
3573101Sstever@eecs.umich.edu
3583714Sstever@eecs.umich.edu    def __long__(self):
3593714Sstever@eecs.umich.edu        return long(self.value)
3603714Sstever@eecs.umich.edu
3613714Sstever@eecs.umich.edu    def __int__(self):
3623714Sstever@eecs.umich.edu        return int(self.value)
3633714Sstever@eecs.umich.edu
3643101Sstever@eecs.umich.edu    # hook for bounds checking
3653101Sstever@eecs.umich.edu    def _check(self):
3663101Sstever@eecs.umich.edu        return
3673101Sstever@eecs.umich.edu
3683101Sstever@eecs.umich.edu    def __mul__(self, other):
3693101Sstever@eecs.umich.edu        newobj = self.__class__(self)
3703101Sstever@eecs.umich.edu        newobj.value *= other
3713101Sstever@eecs.umich.edu        newobj._check()
3723101Sstever@eecs.umich.edu        return newobj
3733101Sstever@eecs.umich.edu
3743101Sstever@eecs.umich.edu    __rmul__ = __mul__
3753101Sstever@eecs.umich.edu
3763101Sstever@eecs.umich.edu    def __div__(self, other):
3773101Sstever@eecs.umich.edu        newobj = self.__class__(self)
3783101Sstever@eecs.umich.edu        newobj.value /= other
3793101Sstever@eecs.umich.edu        newobj._check()
3803101Sstever@eecs.umich.edu        return newobj
3813101Sstever@eecs.umich.edu
3823101Sstever@eecs.umich.edu    def __sub__(self, other):
3833101Sstever@eecs.umich.edu        newobj = self.__class__(self)
3843101Sstever@eecs.umich.edu        newobj.value -= other
3853101Sstever@eecs.umich.edu        newobj._check()
3863101Sstever@eecs.umich.edu        return newobj
3873101Sstever@eecs.umich.edu
3883101Sstever@eecs.umich.edu# Metaclass for bounds-checked integer parameters.  See CheckedInt.
3895033Smilesck@eecs.umich.educlass CheckedIntType(MetaParamValue):
3903101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
3913101Sstever@eecs.umich.edu        super(CheckedIntType, cls).__init__(name, bases, dict)
3923101Sstever@eecs.umich.edu
3933101Sstever@eecs.umich.edu        # CheckedInt is an abstract base class, so we actually don't
3943101Sstever@eecs.umich.edu        # want to do any processing on it... the rest of this code is
3953101Sstever@eecs.umich.edu        # just for classes that derive from CheckedInt.
3963101Sstever@eecs.umich.edu        if name == 'CheckedInt':
3973101Sstever@eecs.umich.edu            return
3983101Sstever@eecs.umich.edu
3993101Sstever@eecs.umich.edu        if not (hasattr(cls, 'min') and hasattr(cls, 'max')):
4003101Sstever@eecs.umich.edu            if not (hasattr(cls, 'size') and hasattr(cls, 'unsigned')):
4013101Sstever@eecs.umich.edu                panic("CheckedInt subclass %s must define either\n" \
4025822Ssaidi@eecs.umich.edu                      "    'min' and 'max' or 'size' and 'unsigned'\n",
4035822Ssaidi@eecs.umich.edu                      name);
4043101Sstever@eecs.umich.edu            if cls.unsigned:
4053101Sstever@eecs.umich.edu                cls.min = 0
4063101Sstever@eecs.umich.edu                cls.max = 2 ** cls.size - 1
4073101Sstever@eecs.umich.edu            else:
4083101Sstever@eecs.umich.edu                cls.min = -(2 ** (cls.size - 1))
4093101Sstever@eecs.umich.edu                cls.max = (2 ** (cls.size - 1)) - 1
4103101Sstever@eecs.umich.edu
4113101Sstever@eecs.umich.edu# Abstract superclass for bounds-checked integer parameters.  This
4123101Sstever@eecs.umich.edu# class is subclassed to generate parameter classes with specific
4133101Sstever@eecs.umich.edu# bounds.  Initialization of the min and max bounds is done in the
4143101Sstever@eecs.umich.edu# metaclass CheckedIntType.__init__.
4153101Sstever@eecs.umich.educlass CheckedInt(NumericParamValue):
4163101Sstever@eecs.umich.edu    __metaclass__ = CheckedIntType
4173101Sstever@eecs.umich.edu
4183101Sstever@eecs.umich.edu    def _check(self):
4193101Sstever@eecs.umich.edu        if not self.min <= self.value <= self.max:
4203101Sstever@eecs.umich.edu            raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
4213101Sstever@eecs.umich.edu                  (self.min, self.value, self.max)
4223101Sstever@eecs.umich.edu
4233101Sstever@eecs.umich.edu    def __init__(self, value):
4243101Sstever@eecs.umich.edu        if isinstance(value, str):
4253102Sstever@eecs.umich.edu            self.value = convert.toInteger(value)
4263714Sstever@eecs.umich.edu        elif isinstance(value, (int, long, float, NumericParamValue)):
4273101Sstever@eecs.umich.edu            self.value = long(value)
4283714Sstever@eecs.umich.edu        else:
4293714Sstever@eecs.umich.edu            raise TypeError, "Can't convert object of type %s to CheckedInt" \
4303714Sstever@eecs.umich.edu                  % type(value).__name__
4313101Sstever@eecs.umich.edu        self._check()
4323101Sstever@eecs.umich.edu
4337673Snate@binkert.org    @classmethod
4347673Snate@binkert.org    def cxx_predecls(cls, code):
4357673Snate@binkert.org        # most derived types require this, so we just do it here once
4367673Snate@binkert.org        code('#include "base/types.hh"')
4377673Snate@binkert.org
4387673Snate@binkert.org    @classmethod
4397673Snate@binkert.org    def swig_predecls(cls, code):
4407673Snate@binkert.org        # most derived types require this, so we just do it here once
4417673Snate@binkert.org        code('%import "stdint.i"')
4427673Snate@binkert.org        code('%import "base/types.hh"')
4437673Snate@binkert.org
4444762Snate@binkert.org    def getValue(self):
4454762Snate@binkert.org        return long(self.value)
4464762Snate@binkert.org
4473101Sstever@eecs.umich.educlass Int(CheckedInt):      cxx_type = 'int';      size = 32; unsigned = False
4483101Sstever@eecs.umich.educlass Unsigned(CheckedInt): cxx_type = 'unsigned'; size = 32; unsigned = True
4493101Sstever@eecs.umich.edu
4503101Sstever@eecs.umich.educlass Int8(CheckedInt):     cxx_type =   'int8_t'; size =  8; unsigned = False
4513101Sstever@eecs.umich.educlass UInt8(CheckedInt):    cxx_type =  'uint8_t'; size =  8; unsigned = True
4523101Sstever@eecs.umich.educlass Int16(CheckedInt):    cxx_type =  'int16_t'; size = 16; unsigned = False
4533101Sstever@eecs.umich.educlass UInt16(CheckedInt):   cxx_type = 'uint16_t'; size = 16; unsigned = True
4543101Sstever@eecs.umich.educlass Int32(CheckedInt):    cxx_type =  'int32_t'; size = 32; unsigned = False
4553101Sstever@eecs.umich.educlass UInt32(CheckedInt):   cxx_type = 'uint32_t'; size = 32; unsigned = True
4563101Sstever@eecs.umich.educlass Int64(CheckedInt):    cxx_type =  'int64_t'; size = 64; unsigned = False
4573101Sstever@eecs.umich.educlass UInt64(CheckedInt):   cxx_type = 'uint64_t'; size = 64; unsigned = True
4583101Sstever@eecs.umich.edu
4593101Sstever@eecs.umich.educlass Counter(CheckedInt):  cxx_type = 'Counter';  size = 64; unsigned = True
4603101Sstever@eecs.umich.educlass Tick(CheckedInt):     cxx_type = 'Tick';     size = 64; unsigned = True
4613101Sstever@eecs.umich.educlass TcpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
4623101Sstever@eecs.umich.educlass UdpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
4633101Sstever@eecs.umich.edu
4643101Sstever@eecs.umich.educlass Percent(CheckedInt):  cxx_type = 'int'; min = 0; max = 100
4653101Sstever@eecs.umich.edu
4663101Sstever@eecs.umich.educlass Float(ParamValue, float):
4674446Sbinkertn@umich.edu    cxx_type = 'double'
4683101Sstever@eecs.umich.edu
4695468Snate@binkert.org    def __init__(self, value):
4705468Snate@binkert.org        if isinstance(value, (int, long, float, NumericParamValue, Float)):
4715468Snate@binkert.org            self.value = float(value)
4725468Snate@binkert.org        else:
4735468Snate@binkert.org            raise TypeError, "Can't convert object of type %s to Float" \
4745468Snate@binkert.org                  % type(value).__name__
4755468Snate@binkert.org
4764762Snate@binkert.org    def getValue(self):
4774762Snate@binkert.org        return float(self.value)
4784762Snate@binkert.org
4793101Sstever@eecs.umich.educlass MemorySize(CheckedInt):
4803101Sstever@eecs.umich.edu    cxx_type = 'uint64_t'
4813101Sstever@eecs.umich.edu    size = 64
4823101Sstever@eecs.umich.edu    unsigned = True
4833101Sstever@eecs.umich.edu    def __init__(self, value):
4843101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
4853101Sstever@eecs.umich.edu            self.value = value.value
4863101Sstever@eecs.umich.edu        else:
4873102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
4883101Sstever@eecs.umich.edu        self._check()
4893101Sstever@eecs.umich.edu
4903101Sstever@eecs.umich.educlass MemorySize32(CheckedInt):
4914168Sbinkertn@umich.edu    cxx_type = 'uint32_t'
4923101Sstever@eecs.umich.edu    size = 32
4933101Sstever@eecs.umich.edu    unsigned = True
4943101Sstever@eecs.umich.edu    def __init__(self, value):
4953101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
4963101Sstever@eecs.umich.edu            self.value = value.value
4973101Sstever@eecs.umich.edu        else:
4983102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
4993101Sstever@eecs.umich.edu        self._check()
5003101Sstever@eecs.umich.edu
5013101Sstever@eecs.umich.educlass Addr(CheckedInt):
5023101Sstever@eecs.umich.edu    cxx_type = 'Addr'
5033101Sstever@eecs.umich.edu    size = 64
5043101Sstever@eecs.umich.edu    unsigned = True
5053101Sstever@eecs.umich.edu    def __init__(self, value):
5063101Sstever@eecs.umich.edu        if isinstance(value, Addr):
5073101Sstever@eecs.umich.edu            self.value = value.value
5083101Sstever@eecs.umich.edu        else:
5093101Sstever@eecs.umich.edu            try:
5103102Sstever@eecs.umich.edu                self.value = convert.toMemorySize(value)
5113101Sstever@eecs.umich.edu            except TypeError:
5123101Sstever@eecs.umich.edu                self.value = long(value)
5133101Sstever@eecs.umich.edu        self._check()
5143584Ssaidi@eecs.umich.edu    def __add__(self, other):
5153584Ssaidi@eecs.umich.edu        if isinstance(other, Addr):
5163584Ssaidi@eecs.umich.edu            return self.value + other.value
5173584Ssaidi@eecs.umich.edu        else:
5183584Ssaidi@eecs.umich.edu            return self.value + other
5193101Sstever@eecs.umich.edu
5203101Sstever@eecs.umich.edu
5215033Smilesck@eecs.umich.educlass MetaRange(MetaParamValue):
5223101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
5233101Sstever@eecs.umich.edu        super(MetaRange, cls).__init__(name, bases, dict)
5243101Sstever@eecs.umich.edu        if name == 'Range':
5253101Sstever@eecs.umich.edu            return
5263101Sstever@eecs.umich.edu        cls.cxx_type = 'Range< %s >' % cls.type.cxx_type
5273101Sstever@eecs.umich.edu
5283101Sstever@eecs.umich.educlass Range(ParamValue):
5293101Sstever@eecs.umich.edu    __metaclass__ = MetaRange
5303101Sstever@eecs.umich.edu    type = Int # default; can be overridden in subclasses
5313101Sstever@eecs.umich.edu    def __init__(self, *args, **kwargs):
5323101Sstever@eecs.umich.edu        def handle_kwargs(self, kwargs):
5333101Sstever@eecs.umich.edu            if 'end' in kwargs:
5343101Sstever@eecs.umich.edu                self.second = self.type(kwargs.pop('end'))
5353101Sstever@eecs.umich.edu            elif 'size' in kwargs:
5363101Sstever@eecs.umich.edu                self.second = self.first + self.type(kwargs.pop('size')) - 1
5373101Sstever@eecs.umich.edu            else:
5383101Sstever@eecs.umich.edu                raise TypeError, "Either end or size must be specified"
5393101Sstever@eecs.umich.edu
5403101Sstever@eecs.umich.edu        if len(args) == 0:
5413101Sstever@eecs.umich.edu            self.first = self.type(kwargs.pop('start'))
5423101Sstever@eecs.umich.edu            handle_kwargs(self, kwargs)
5433101Sstever@eecs.umich.edu
5443101Sstever@eecs.umich.edu        elif len(args) == 1:
5453101Sstever@eecs.umich.edu            if kwargs:
5463101Sstever@eecs.umich.edu                self.first = self.type(args[0])
5473101Sstever@eecs.umich.edu                handle_kwargs(self, kwargs)
5483101Sstever@eecs.umich.edu            elif isinstance(args[0], Range):
5493101Sstever@eecs.umich.edu                self.first = self.type(args[0].first)
5503101Sstever@eecs.umich.edu                self.second = self.type(args[0].second)
5515219Ssaidi@eecs.umich.edu            elif isinstance(args[0], (list, tuple)):
5525219Ssaidi@eecs.umich.edu                self.first = self.type(args[0][0])
5535219Ssaidi@eecs.umich.edu                self.second = self.type(args[0][1])
5543101Sstever@eecs.umich.edu            else:
5553101Sstever@eecs.umich.edu                self.first = self.type(0)
5563101Sstever@eecs.umich.edu                self.second = self.type(args[0]) - 1
5573101Sstever@eecs.umich.edu
5583101Sstever@eecs.umich.edu        elif len(args) == 2:
5593101Sstever@eecs.umich.edu            self.first = self.type(args[0])
5603101Sstever@eecs.umich.edu            self.second = self.type(args[1])
5613101Sstever@eecs.umich.edu        else:
5623101Sstever@eecs.umich.edu            raise TypeError, "Too many arguments specified"
5633101Sstever@eecs.umich.edu
5643101Sstever@eecs.umich.edu        if kwargs:
5653101Sstever@eecs.umich.edu            raise TypeError, "too many keywords: %s" % kwargs.keys()
5663101Sstever@eecs.umich.edu
5673101Sstever@eecs.umich.edu    def __str__(self):
5683101Sstever@eecs.umich.edu        return '%s:%s' % (self.first, self.second)
5693101Sstever@eecs.umich.edu
5707673Snate@binkert.org    @classmethod
5717673Snate@binkert.org    def cxx_predecls(cls, code):
5727675Snate@binkert.org        cls.type.cxx_predecls(code)
5737673Snate@binkert.org        code('#include "base/range.hh"')
5747675Snate@binkert.org
5757675Snate@binkert.org    @classmethod
5767675Snate@binkert.org    def swig_predecls(cls, code):
5777675Snate@binkert.org        cls.type.swig_predecls(code)
5787675Snate@binkert.org        code('%import "python/swig/range.i"')
5797673Snate@binkert.org
5803101Sstever@eecs.umich.educlass AddrRange(Range):
5813101Sstever@eecs.umich.edu    type = Addr
5827673Snate@binkert.org
5834762Snate@binkert.org    def getValue(self):
5847675Snate@binkert.org        from m5.internal.range import AddrRange
5854762Snate@binkert.org
5864762Snate@binkert.org        value = AddrRange()
5874762Snate@binkert.org        value.start = long(self.first)
5884762Snate@binkert.org        value.end = long(self.second)
5894762Snate@binkert.org        return value
5903101Sstever@eecs.umich.edu
5913101Sstever@eecs.umich.educlass TickRange(Range):
5923101Sstever@eecs.umich.edu    type = Tick
5937673Snate@binkert.org
5944762Snate@binkert.org    def getValue(self):
5957675Snate@binkert.org        from m5.internal.range import TickRange
5964762Snate@binkert.org
5974762Snate@binkert.org        value = TickRange()
5984762Snate@binkert.org        value.start = long(self.first)
5994762Snate@binkert.org        value.end = long(self.second)
6004762Snate@binkert.org        return value
6013101Sstever@eecs.umich.edu
6023101Sstever@eecs.umich.edu# Boolean parameter type.  Python doesn't let you subclass bool, since
6033101Sstever@eecs.umich.edu# it doesn't want to let you create multiple instances of True and
6043101Sstever@eecs.umich.edu# False.  Thus this is a little more complicated than String.
6053101Sstever@eecs.umich.educlass Bool(ParamValue):
6063101Sstever@eecs.umich.edu    cxx_type = 'bool'
6073101Sstever@eecs.umich.edu    def __init__(self, value):
6083101Sstever@eecs.umich.edu        try:
6093102Sstever@eecs.umich.edu            self.value = convert.toBool(value)
6103101Sstever@eecs.umich.edu        except TypeError:
6113101Sstever@eecs.umich.edu            self.value = bool(value)
6123101Sstever@eecs.umich.edu
6134762Snate@binkert.org    def getValue(self):
6144762Snate@binkert.org        return bool(self.value)
6154762Snate@binkert.org
6163101Sstever@eecs.umich.edu    def __str__(self):
6173101Sstever@eecs.umich.edu        return str(self.value)
6183101Sstever@eecs.umich.edu
6193101Sstever@eecs.umich.edu    def ini_str(self):
6203101Sstever@eecs.umich.edu        if self.value:
6213101Sstever@eecs.umich.edu            return 'true'
6223101Sstever@eecs.umich.edu        return 'false'
6233101Sstever@eecs.umich.edu
6243101Sstever@eecs.umich.edudef IncEthernetAddr(addr, val = 1):
6253101Sstever@eecs.umich.edu    bytes = map(lambda x: int(x, 16), addr.split(':'))
6263101Sstever@eecs.umich.edu    bytes[5] += val
6273101Sstever@eecs.umich.edu    for i in (5, 4, 3, 2, 1):
6283101Sstever@eecs.umich.edu        val,rem = divmod(bytes[i], 256)
6293101Sstever@eecs.umich.edu        bytes[i] = rem
6303101Sstever@eecs.umich.edu        if val == 0:
6313101Sstever@eecs.umich.edu            break
6323101Sstever@eecs.umich.edu        bytes[i - 1] += val
6333101Sstever@eecs.umich.edu    assert(bytes[0] <= 255)
6343101Sstever@eecs.umich.edu    return ':'.join(map(lambda x: '%02x' % x, bytes))
6353101Sstever@eecs.umich.edu
6364380Sbinkertn@umich.edu_NextEthernetAddr = "00:90:00:00:00:01"
6374380Sbinkertn@umich.edudef NextEthernetAddr():
6384380Sbinkertn@umich.edu    global _NextEthernetAddr
6393101Sstever@eecs.umich.edu
6404380Sbinkertn@umich.edu    value = _NextEthernetAddr
6414380Sbinkertn@umich.edu    _NextEthernetAddr = IncEthernetAddr(_NextEthernetAddr, 1)
6424380Sbinkertn@umich.edu    return value
6433101Sstever@eecs.umich.edu
6443101Sstever@eecs.umich.educlass EthernetAddr(ParamValue):
6453101Sstever@eecs.umich.edu    cxx_type = 'Net::EthAddr'
6467673Snate@binkert.org
6477673Snate@binkert.org    @classmethod
6487673Snate@binkert.org    def cxx_predecls(cls, code):
6497673Snate@binkert.org        code('#include "base/inet.hh"')
6507673Snate@binkert.org
6517673Snate@binkert.org    @classmethod
6527673Snate@binkert.org    def swig_predecls(cls, code):
6537673Snate@binkert.org        code('%include "python/swig/inet.i"')
6547673Snate@binkert.org
6553101Sstever@eecs.umich.edu    def __init__(self, value):
6563101Sstever@eecs.umich.edu        if value == NextEthernetAddr:
6573101Sstever@eecs.umich.edu            self.value = value
6583101Sstever@eecs.umich.edu            return
6593101Sstever@eecs.umich.edu
6603101Sstever@eecs.umich.edu        if not isinstance(value, str):
6613101Sstever@eecs.umich.edu            raise TypeError, "expected an ethernet address and didn't get one"
6623101Sstever@eecs.umich.edu
6633101Sstever@eecs.umich.edu        bytes = value.split(':')
6643101Sstever@eecs.umich.edu        if len(bytes) != 6:
6653101Sstever@eecs.umich.edu            raise TypeError, 'invalid ethernet address %s' % value
6663101Sstever@eecs.umich.edu
6673101Sstever@eecs.umich.edu        for byte in bytes:
6687743Sgblack@eecs.umich.edu            if not 0 <= int(byte) <= 0xff:
6693101Sstever@eecs.umich.edu                raise TypeError, 'invalid ethernet address %s' % value
6703101Sstever@eecs.umich.edu
6713101Sstever@eecs.umich.edu        self.value = value
6723101Sstever@eecs.umich.edu
6733101Sstever@eecs.umich.edu    def unproxy(self, base):
6743101Sstever@eecs.umich.edu        if self.value == NextEthernetAddr:
6754380Sbinkertn@umich.edu            return EthernetAddr(self.value())
6763101Sstever@eecs.umich.edu        return self
6773101Sstever@eecs.umich.edu
6784762Snate@binkert.org    def getValue(self):
6797677Snate@binkert.org        from m5.internal.params import EthAddr
6804762Snate@binkert.org        return EthAddr(self.value)
6814762Snate@binkert.org
6824380Sbinkertn@umich.edu    def ini_str(self):
6834380Sbinkertn@umich.edu        return self.value
6843101Sstever@eecs.umich.edu
6857777Sgblack@eecs.umich.edu# When initializing an IpAddress, pass in an existing IpAddress, a string of
6867777Sgblack@eecs.umich.edu# the form "a.b.c.d", or an integer representing an IP.
6877777Sgblack@eecs.umich.educlass IpAddress(ParamValue):
6887777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpAddress'
6897777Sgblack@eecs.umich.edu
6907777Sgblack@eecs.umich.edu    @classmethod
6917777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
6927777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
6937777Sgblack@eecs.umich.edu
6947777Sgblack@eecs.umich.edu    @classmethod
6957777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
6967777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
6977777Sgblack@eecs.umich.edu
6987777Sgblack@eecs.umich.edu    def __init__(self, value):
6997777Sgblack@eecs.umich.edu        if isinstance(value, IpAddress):
7007777Sgblack@eecs.umich.edu            self.ip = value.ip
7017777Sgblack@eecs.umich.edu        else:
7027777Sgblack@eecs.umich.edu            try:
7037777Sgblack@eecs.umich.edu                self.ip = convert.toIpAddress(value)
7047777Sgblack@eecs.umich.edu            except TypeError:
7057777Sgblack@eecs.umich.edu                self.ip = long(value)
7067777Sgblack@eecs.umich.edu        self.verifyIp()
7077777Sgblack@eecs.umich.edu
7088579Ssteve.reinhardt@amd.com    def __str__(self):
7098579Ssteve.reinhardt@amd.com        tup = [(self.ip >> i)  & 0xff for i in (24, 16, 8, 0)]
7108579Ssteve.reinhardt@amd.com        return '%d.%d.%d.%d' % tuple(tup)
7118579Ssteve.reinhardt@amd.com
7128579Ssteve.reinhardt@amd.com    def __eq__(self, other):
7138579Ssteve.reinhardt@amd.com        if isinstance(other, IpAddress):
7148579Ssteve.reinhardt@amd.com            return self.ip == other.ip
7158579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
7168579Ssteve.reinhardt@amd.com            try:
7178579Ssteve.reinhardt@amd.com                return self.ip == convert.toIpAddress(other)
7188579Ssteve.reinhardt@amd.com            except:
7198579Ssteve.reinhardt@amd.com                return False
7208579Ssteve.reinhardt@amd.com        else:
7218579Ssteve.reinhardt@amd.com            return self.ip == other
7228579Ssteve.reinhardt@amd.com
7238579Ssteve.reinhardt@amd.com    def __ne__(self, other):
7248579Ssteve.reinhardt@amd.com        return not (self == other)
7258579Ssteve.reinhardt@amd.com
7267777Sgblack@eecs.umich.edu    def verifyIp(self):
7277777Sgblack@eecs.umich.edu        if self.ip < 0 or self.ip >= (1 << 32):
7287798Sgblack@eecs.umich.edu            raise TypeError, "invalid ip address %#08x" % self.ip
7297777Sgblack@eecs.umich.edu
7307777Sgblack@eecs.umich.edu    def getValue(self):
7317777Sgblack@eecs.umich.edu        from m5.internal.params import IpAddress
7327777Sgblack@eecs.umich.edu        return IpAddress(self.ip)
7337777Sgblack@eecs.umich.edu
7347777Sgblack@eecs.umich.edu# When initializing an IpNetmask, pass in an existing IpNetmask, a string of
7357777Sgblack@eecs.umich.edu# the form "a.b.c.d/n" or "a.b.c.d/e.f.g.h", or an ip and netmask as
7367777Sgblack@eecs.umich.edu# positional or keyword arguments.
7377777Sgblack@eecs.umich.educlass IpNetmask(IpAddress):
7387777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpNetmask'
7397777Sgblack@eecs.umich.edu
7407777Sgblack@eecs.umich.edu    @classmethod
7417777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
7427777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
7437777Sgblack@eecs.umich.edu
7447777Sgblack@eecs.umich.edu    @classmethod
7457777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
7467777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
7477777Sgblack@eecs.umich.edu
7487777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
7497777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
7507777Sgblack@eecs.umich.edu            if key in kwargs:
7517777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
7527777Sgblack@eecs.umich.edu            elif elseVal:
7537777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
7547777Sgblack@eecs.umich.edu            else:
7557777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
7567777Sgblack@eecs.umich.edu
7577777Sgblack@eecs.umich.edu        if len(args) == 0:
7587777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
7597777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'netmask')
7607777Sgblack@eecs.umich.edu
7617777Sgblack@eecs.umich.edu        elif len(args) == 1:
7627777Sgblack@eecs.umich.edu            if kwargs:
7637777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'netmask' in kwargs:
7647777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
7657777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
7667777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'netmask', args[0])
7677777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpNetmask):
7687777Sgblack@eecs.umich.edu                self.ip = args[0].ip
7697777Sgblack@eecs.umich.edu                self.netmask = args[0].netmask
7707777Sgblack@eecs.umich.edu            else:
7717777Sgblack@eecs.umich.edu                (self.ip, self.netmask) = convert.toIpNetmask(args[0])
7727777Sgblack@eecs.umich.edu
7737777Sgblack@eecs.umich.edu        elif len(args) == 2:
7747777Sgblack@eecs.umich.edu            self.ip = args[0]
7757777Sgblack@eecs.umich.edu            self.netmask = args[1]
7767777Sgblack@eecs.umich.edu        else:
7777777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
7787777Sgblack@eecs.umich.edu
7797777Sgblack@eecs.umich.edu        if kwargs:
7807777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
7817777Sgblack@eecs.umich.edu
7827777Sgblack@eecs.umich.edu        self.verify()
7837777Sgblack@eecs.umich.edu
7848579Ssteve.reinhardt@amd.com    def __str__(self):
7858579Ssteve.reinhardt@amd.com        return "%s/%d" % (super(IpNetmask, self).__str__(), self.netmask)
7868579Ssteve.reinhardt@amd.com
7878579Ssteve.reinhardt@amd.com    def __eq__(self, other):
7888579Ssteve.reinhardt@amd.com        if isinstance(other, IpNetmask):
7898579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.netmask == other.netmask
7908579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
7918579Ssteve.reinhardt@amd.com            try:
7928579Ssteve.reinhardt@amd.com                return (self.ip, self.netmask) == convert.toIpNetmask(other)
7938579Ssteve.reinhardt@amd.com            except:
7948579Ssteve.reinhardt@amd.com                return False
7958579Ssteve.reinhardt@amd.com        else:
7968579Ssteve.reinhardt@amd.com            return False
7978579Ssteve.reinhardt@amd.com
7987777Sgblack@eecs.umich.edu    def verify(self):
7997777Sgblack@eecs.umich.edu        self.verifyIp()
8007777Sgblack@eecs.umich.edu        if self.netmask < 0 or self.netmask > 32:
8017777Sgblack@eecs.umich.edu            raise TypeError, "invalid netmask %d" % netmask
8027777Sgblack@eecs.umich.edu
8037777Sgblack@eecs.umich.edu    def getValue(self):
8047777Sgblack@eecs.umich.edu        from m5.internal.params import IpNetmask
8057777Sgblack@eecs.umich.edu        return IpNetmask(self.ip, self.netmask)
8067777Sgblack@eecs.umich.edu
8077777Sgblack@eecs.umich.edu# When initializing an IpWithPort, pass in an existing IpWithPort, a string of
8087777Sgblack@eecs.umich.edu# the form "a.b.c.d:p", or an ip and port as positional or keyword arguments.
8097777Sgblack@eecs.umich.educlass IpWithPort(IpAddress):
8107777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpWithPort'
8117777Sgblack@eecs.umich.edu
8127777Sgblack@eecs.umich.edu    @classmethod
8137777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
8147777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
8157777Sgblack@eecs.umich.edu
8167777Sgblack@eecs.umich.edu    @classmethod
8177777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
8187777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
8197777Sgblack@eecs.umich.edu
8207777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
8217777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
8227777Sgblack@eecs.umich.edu            if key in kwargs:
8237777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
8247777Sgblack@eecs.umich.edu            elif elseVal:
8257777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
8267777Sgblack@eecs.umich.edu            else:
8277777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
8287777Sgblack@eecs.umich.edu
8297777Sgblack@eecs.umich.edu        if len(args) == 0:
8307777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
8317777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'port')
8327777Sgblack@eecs.umich.edu
8337777Sgblack@eecs.umich.edu        elif len(args) == 1:
8347777Sgblack@eecs.umich.edu            if kwargs:
8357777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'port' in kwargs:
8367777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
8377777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
8387777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'port', args[0])
8397777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpWithPort):
8407777Sgblack@eecs.umich.edu                self.ip = args[0].ip
8417777Sgblack@eecs.umich.edu                self.port = args[0].port
8427777Sgblack@eecs.umich.edu            else:
8437777Sgblack@eecs.umich.edu                (self.ip, self.port) = convert.toIpWithPort(args[0])
8447777Sgblack@eecs.umich.edu
8457777Sgblack@eecs.umich.edu        elif len(args) == 2:
8467777Sgblack@eecs.umich.edu            self.ip = args[0]
8477777Sgblack@eecs.umich.edu            self.port = args[1]
8487777Sgblack@eecs.umich.edu        else:
8497777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
8507777Sgblack@eecs.umich.edu
8517777Sgblack@eecs.umich.edu        if kwargs:
8527777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
8537777Sgblack@eecs.umich.edu
8547777Sgblack@eecs.umich.edu        self.verify()
8557777Sgblack@eecs.umich.edu
8568579Ssteve.reinhardt@amd.com    def __str__(self):
8578579Ssteve.reinhardt@amd.com        return "%s:%d" % (super(IpWithPort, self).__str__(), self.port)
8588579Ssteve.reinhardt@amd.com
8598579Ssteve.reinhardt@amd.com    def __eq__(self, other):
8608579Ssteve.reinhardt@amd.com        if isinstance(other, IpWithPort):
8618579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.port == other.port
8628579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
8638579Ssteve.reinhardt@amd.com            try:
8648579Ssteve.reinhardt@amd.com                return (self.ip, self.port) == convert.toIpWithPort(other)
8658579Ssteve.reinhardt@amd.com            except:
8668579Ssteve.reinhardt@amd.com                return False
8678579Ssteve.reinhardt@amd.com        else:
8688579Ssteve.reinhardt@amd.com            return False
8698579Ssteve.reinhardt@amd.com
8707777Sgblack@eecs.umich.edu    def verify(self):
8717777Sgblack@eecs.umich.edu        self.verifyIp()
8727777Sgblack@eecs.umich.edu        if self.port < 0 or self.port > 0xffff:
8737777Sgblack@eecs.umich.edu            raise TypeError, "invalid port %d" % self.port
8747777Sgblack@eecs.umich.edu
8757777Sgblack@eecs.umich.edu    def getValue(self):
8767777Sgblack@eecs.umich.edu        from m5.internal.params import IpWithPort
8777777Sgblack@eecs.umich.edu        return IpWithPort(self.ip, self.port)
8787777Sgblack@eecs.umich.edu
8793932Sbinkertn@umich.edutime_formats = [ "%a %b %d %H:%M:%S %Z %Y",
8803932Sbinkertn@umich.edu                 "%a %b %d %H:%M:%S %Z %Y",
8813932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M:%S",
8823932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M",
8833932Sbinkertn@umich.edu                 "%Y/%m/%d",
8843932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M:%S",
8853932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M",
8863932Sbinkertn@umich.edu                 "%m/%d/%Y",
8873932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M:%S",
8883932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M",
8893932Sbinkertn@umich.edu                 "%m/%d/%y"]
8903932Sbinkertn@umich.edu
8913932Sbinkertn@umich.edu
8923885Sbinkertn@umich.edudef parse_time(value):
8933932Sbinkertn@umich.edu    from time import gmtime, strptime, struct_time, time
8943932Sbinkertn@umich.edu    from datetime import datetime, date
8953885Sbinkertn@umich.edu
8963932Sbinkertn@umich.edu    if isinstance(value, struct_time):
8973932Sbinkertn@umich.edu        return value
8983932Sbinkertn@umich.edu
8993932Sbinkertn@umich.edu    if isinstance(value, (int, long)):
9003932Sbinkertn@umich.edu        return gmtime(value)
9013932Sbinkertn@umich.edu
9023932Sbinkertn@umich.edu    if isinstance(value, (datetime, date)):
9033932Sbinkertn@umich.edu        return value.timetuple()
9043932Sbinkertn@umich.edu
9053932Sbinkertn@umich.edu    if isinstance(value, str):
9063932Sbinkertn@umich.edu        if value in ('Now', 'Today'):
9073932Sbinkertn@umich.edu            return time.gmtime(time.time())
9083932Sbinkertn@umich.edu
9093932Sbinkertn@umich.edu        for format in time_formats:
9103932Sbinkertn@umich.edu            try:
9113932Sbinkertn@umich.edu                return strptime(value, format)
9123932Sbinkertn@umich.edu            except ValueError:
9133932Sbinkertn@umich.edu                pass
9143885Sbinkertn@umich.edu
9153885Sbinkertn@umich.edu    raise ValueError, "Could not parse '%s' as a time" % value
9163885Sbinkertn@umich.edu
9173885Sbinkertn@umich.educlass Time(ParamValue):
9184762Snate@binkert.org    cxx_type = 'tm'
9197673Snate@binkert.org
9207673Snate@binkert.org    @classmethod
9217673Snate@binkert.org    def cxx_predecls(cls, code):
9227673Snate@binkert.org        code('#include <time.h>')
9237673Snate@binkert.org
9247673Snate@binkert.org    @classmethod
9257673Snate@binkert.org    def swig_predecls(cls, code):
9267673Snate@binkert.org        code('%include "python/swig/time.i"')
9277673Snate@binkert.org
9283885Sbinkertn@umich.edu    def __init__(self, value):
9293932Sbinkertn@umich.edu        self.value = parse_time(value)
9303885Sbinkertn@umich.edu
9314762Snate@binkert.org    def getValue(self):
9327677Snate@binkert.org        from m5.internal.params import tm
9334762Snate@binkert.org
9344762Snate@binkert.org        c_time = tm()
9354762Snate@binkert.org        py_time = self.value
9364762Snate@binkert.org
9374762Snate@binkert.org        # UNIX is years since 1900
9384762Snate@binkert.org        c_time.tm_year = py_time.tm_year - 1900;
9394762Snate@binkert.org
9404762Snate@binkert.org        # Python starts at 1, UNIX starts at 0
9414762Snate@binkert.org        c_time.tm_mon =  py_time.tm_mon - 1;
9424762Snate@binkert.org        c_time.tm_mday = py_time.tm_mday;
9434762Snate@binkert.org        c_time.tm_hour = py_time.tm_hour;
9444762Snate@binkert.org        c_time.tm_min = py_time.tm_min;
9454762Snate@binkert.org        c_time.tm_sec = py_time.tm_sec;
9464762Snate@binkert.org
9474762Snate@binkert.org        # Python has 0 as Monday, UNIX is 0 as sunday
9484762Snate@binkert.org        c_time.tm_wday = py_time.tm_wday + 1
9494762Snate@binkert.org        if c_time.tm_wday > 6:
9504762Snate@binkert.org            c_time.tm_wday -= 7;
9514762Snate@binkert.org
9524762Snate@binkert.org        # Python starts at 1, Unix starts at 0
9534762Snate@binkert.org        c_time.tm_yday = py_time.tm_yday - 1;
9544762Snate@binkert.org
9554762Snate@binkert.org        return c_time
9564762Snate@binkert.org
9573885Sbinkertn@umich.edu    def __str__(self):
9584762Snate@binkert.org        return time.asctime(self.value)
9593885Sbinkertn@umich.edu
9603885Sbinkertn@umich.edu    def ini_str(self):
9613932Sbinkertn@umich.edu        return str(self)
9623885Sbinkertn@umich.edu
9633101Sstever@eecs.umich.edu# Enumerated types are a little more complex.  The user specifies the
9643101Sstever@eecs.umich.edu# type as Enum(foo) where foo is either a list or dictionary of
9653101Sstever@eecs.umich.edu# alternatives (typically strings, but not necessarily so).  (In the
9663101Sstever@eecs.umich.edu# long run, the integer value of the parameter will be the list index
9673101Sstever@eecs.umich.edu# or the corresponding dictionary value.  For now, since we only check
9683101Sstever@eecs.umich.edu# that the alternative is valid and then spit it into a .ini file,
9693101Sstever@eecs.umich.edu# there's not much point in using the dictionary.)
9703101Sstever@eecs.umich.edu
9713101Sstever@eecs.umich.edu# What Enum() must do is generate a new type encapsulating the
9723101Sstever@eecs.umich.edu# provided list/dictionary so that specific values of the parameter
9733101Sstever@eecs.umich.edu# can be instances of that type.  We define two hidden internal
9743101Sstever@eecs.umich.edu# classes (_ListEnum and _DictEnum) to serve as base classes, then
9753101Sstever@eecs.umich.edu# derive the new type from the appropriate base class on the fly.
9763101Sstever@eecs.umich.edu
9774762Snate@binkert.orgallEnums = {}
9783101Sstever@eecs.umich.edu# Metaclass for Enum types
9795033Smilesck@eecs.umich.educlass MetaEnum(MetaParamValue):
9804762Snate@binkert.org    def __new__(mcls, name, bases, dict):
9814762Snate@binkert.org        assert name not in allEnums
9824762Snate@binkert.org
9834762Snate@binkert.org        cls = super(MetaEnum, mcls).__new__(mcls, name, bases, dict)
9844762Snate@binkert.org        allEnums[name] = cls
9854762Snate@binkert.org        return cls
9864762Snate@binkert.org
9873101Sstever@eecs.umich.edu    def __init__(cls, name, bases, init_dict):
9883101Sstever@eecs.umich.edu        if init_dict.has_key('map'):
9893101Sstever@eecs.umich.edu            if not isinstance(cls.map, dict):
9903101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'map' " \
9913101Sstever@eecs.umich.edu                      "must be of type dict"
9923101Sstever@eecs.umich.edu            # build list of value strings from map
9933101Sstever@eecs.umich.edu            cls.vals = cls.map.keys()
9943101Sstever@eecs.umich.edu            cls.vals.sort()
9953101Sstever@eecs.umich.edu        elif init_dict.has_key('vals'):
9963101Sstever@eecs.umich.edu            if not isinstance(cls.vals, list):
9973101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'vals' " \
9983101Sstever@eecs.umich.edu                      "must be of type list"
9993101Sstever@eecs.umich.edu            # build string->value map from vals sequence
10003101Sstever@eecs.umich.edu            cls.map = {}
10013101Sstever@eecs.umich.edu            for idx,val in enumerate(cls.vals):
10023101Sstever@eecs.umich.edu                cls.map[val] = idx
10033101Sstever@eecs.umich.edu        else:
10043101Sstever@eecs.umich.edu            raise TypeError, "Enum-derived class must define "\
10053101Sstever@eecs.umich.edu                  "attribute 'map' or 'vals'"
10063101Sstever@eecs.umich.edu
10074762Snate@binkert.org        cls.cxx_type = 'Enums::%s' % name
10083101Sstever@eecs.umich.edu
10093101Sstever@eecs.umich.edu        super(MetaEnum, cls).__init__(name, bases, init_dict)
10103101Sstever@eecs.umich.edu
10113101Sstever@eecs.umich.edu    # Generate C++ class declaration for this enum type.
10123101Sstever@eecs.umich.edu    # Note that we wrap the enum in a class/struct to act as a namespace,
10133101Sstever@eecs.umich.edu    # so that the enum strings can be brief w/o worrying about collisions.
10147673Snate@binkert.org    def cxx_decl(cls, code):
10156654Snate@binkert.org        name = cls.__name__
10167673Snate@binkert.org        code('''\
10177673Snate@binkert.org#ifndef __ENUM__${name}__
10187673Snate@binkert.org#define __ENUM__${name}__
10197673Snate@binkert.org
10207673Snate@binkert.orgnamespace Enums {
10217673Snate@binkert.org    enum $name {
10227673Snate@binkert.org''')
10237673Snate@binkert.org        code.indent(2)
10244762Snate@binkert.org        for val in cls.vals:
10257673Snate@binkert.org            code('$val = ${{cls.map[val]}},')
10267673Snate@binkert.org        code('Num_$name = ${{len(cls.vals)}},')
10277673Snate@binkert.org        code.dedent(2)
10287673Snate@binkert.org        code('''\
10297673Snate@binkert.org    };
10307673Snate@binkert.orgextern const char *${name}Strings[Num_${name}];
10317673Snate@binkert.org}
10324762Snate@binkert.org
10337673Snate@binkert.org#endif // __ENUM__${name}__
10347673Snate@binkert.org''')
10357673Snate@binkert.org
10367673Snate@binkert.org    def cxx_def(cls, code):
10376654Snate@binkert.org        name = cls.__name__
10387673Snate@binkert.org        code('''\
10397677Snate@binkert.org#include "enums/$name.hh"
10407673Snate@binkert.orgnamespace Enums {
10417673Snate@binkert.org    const char *${name}Strings[Num_${name}] =
10427673Snate@binkert.org    {
10437673Snate@binkert.org''')
10447673Snate@binkert.org        code.indent(2)
10454762Snate@binkert.org        for val in cls.vals:
10467673Snate@binkert.org            code('"$val",')
10477673Snate@binkert.org        code.dedent(2)
10487673Snate@binkert.org        code('''
10497673Snate@binkert.org    };
10507811Ssteve.reinhardt@amd.com} // namespace Enums
10517673Snate@binkert.org''')
10523101Sstever@eecs.umich.edu
10538596Ssteve.reinhardt@amd.com    def swig_decl(cls, code):
10548596Ssteve.reinhardt@amd.com        name = cls.__name__
10558596Ssteve.reinhardt@amd.com        code('''\
10568596Ssteve.reinhardt@amd.com%module(package="m5.internal") enum_$name
10578596Ssteve.reinhardt@amd.com
10588596Ssteve.reinhardt@amd.com%{
10598596Ssteve.reinhardt@amd.com#include "enums/$name.hh"
10608596Ssteve.reinhardt@amd.com%}
10618596Ssteve.reinhardt@amd.com
10628596Ssteve.reinhardt@amd.com%include "enums/$name.hh"
10638596Ssteve.reinhardt@amd.com''')
10648596Ssteve.reinhardt@amd.com
10658596Ssteve.reinhardt@amd.com
10663101Sstever@eecs.umich.edu# Base class for enum types.
10673101Sstever@eecs.umich.educlass Enum(ParamValue):
10683101Sstever@eecs.umich.edu    __metaclass__ = MetaEnum
10693101Sstever@eecs.umich.edu    vals = []
10703101Sstever@eecs.umich.edu
10713101Sstever@eecs.umich.edu    def __init__(self, value):
10723101Sstever@eecs.umich.edu        if value not in self.map:
10733101Sstever@eecs.umich.edu            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
10743101Sstever@eecs.umich.edu                  % (value, self.vals)
10753101Sstever@eecs.umich.edu        self.value = value
10763101Sstever@eecs.umich.edu
10777675Snate@binkert.org    @classmethod
10787675Snate@binkert.org    def cxx_predecls(cls, code):
10797675Snate@binkert.org        code('#include "enums/$0.hh"', cls.__name__)
10807675Snate@binkert.org
10817675Snate@binkert.org    @classmethod
10827675Snate@binkert.org    def swig_predecls(cls, code):
10837677Snate@binkert.org        code('%import "python/m5/internal/enum_$0.i"', cls.__name__)
10847675Snate@binkert.org
10854762Snate@binkert.org    def getValue(self):
10864762Snate@binkert.org        return int(self.map[self.value])
10874762Snate@binkert.org
10883101Sstever@eecs.umich.edu    def __str__(self):
10893101Sstever@eecs.umich.edu        return self.value
10903101Sstever@eecs.umich.edu
10913101Sstever@eecs.umich.edu# how big does a rounding error need to be before we warn about it?
10923101Sstever@eecs.umich.edufrequency_tolerance = 0.001  # 0.1%
10933101Sstever@eecs.umich.edu
10944167Sbinkertn@umich.educlass TickParamValue(NumericParamValue):
10953101Sstever@eecs.umich.edu    cxx_type = 'Tick'
10967673Snate@binkert.org
10977673Snate@binkert.org    @classmethod
10987673Snate@binkert.org    def cxx_predecls(cls, code):
10997673Snate@binkert.org        code('#include "base/types.hh"')
11007673Snate@binkert.org
11017673Snate@binkert.org    @classmethod
11027673Snate@binkert.org    def swig_predecls(cls, code):
11037673Snate@binkert.org        code('%import "stdint.i"')
11047673Snate@binkert.org        code('%import "base/types.hh"')
11054167Sbinkertn@umich.edu
11064762Snate@binkert.org    def getValue(self):
11074762Snate@binkert.org        return long(self.value)
11084762Snate@binkert.org
11094167Sbinkertn@umich.educlass Latency(TickParamValue):
11103101Sstever@eecs.umich.edu    def __init__(self, value):
11114167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11124167Sbinkertn@umich.edu            self.ticks = value.ticks
11134167Sbinkertn@umich.edu            self.value = value.value
11144167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11154167Sbinkertn@umich.edu            self.ticks = value.ticks
11164167Sbinkertn@umich.edu            self.value = 1.0 / value.value
11174167Sbinkertn@umich.edu        elif value.endswith('t'):
11184167Sbinkertn@umich.edu            self.ticks = True
11194167Sbinkertn@umich.edu            self.value = int(value[:-1])
11204167Sbinkertn@umich.edu        else:
11214167Sbinkertn@umich.edu            self.ticks = False
11224167Sbinkertn@umich.edu            self.value = convert.toLatency(value)
11233101Sstever@eecs.umich.edu
11243101Sstever@eecs.umich.edu    def __getattr__(self, attr):
11253101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
11263101Sstever@eecs.umich.edu            return self
11273101Sstever@eecs.umich.edu        if attr == 'frequency':
11283101Sstever@eecs.umich.edu            return Frequency(self)
11293101Sstever@eecs.umich.edu        raise AttributeError, "Latency object has no attribute '%s'" % attr
11303101Sstever@eecs.umich.edu
11314762Snate@binkert.org    def getValue(self):
11324762Snate@binkert.org        if self.ticks or self.value == 0:
11334762Snate@binkert.org            value = self.value
11344762Snate@binkert.org        else:
11354762Snate@binkert.org            value = ticks.fromSeconds(self.value)
11364762Snate@binkert.org        return long(value)
11374762Snate@binkert.org
11383101Sstever@eecs.umich.edu    # convert latency to ticks
11393101Sstever@eecs.umich.edu    def ini_str(self):
11404762Snate@binkert.org        return '%d' % self.getValue()
11413101Sstever@eecs.umich.edu
11424167Sbinkertn@umich.educlass Frequency(TickParamValue):
11433101Sstever@eecs.umich.edu    def __init__(self, value):
11444167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11454167Sbinkertn@umich.edu            if value.value == 0:
11464167Sbinkertn@umich.edu                self.value = 0
11474167Sbinkertn@umich.edu            else:
11484167Sbinkertn@umich.edu                self.value = 1.0 / value.value
11494167Sbinkertn@umich.edu            self.ticks = value.ticks
11504167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11514167Sbinkertn@umich.edu            self.value = value.value
11524167Sbinkertn@umich.edu            self.ticks = value.ticks
11534167Sbinkertn@umich.edu        else:
11544167Sbinkertn@umich.edu            self.ticks = False
11554167Sbinkertn@umich.edu            self.value = convert.toFrequency(value)
11563101Sstever@eecs.umich.edu
11573101Sstever@eecs.umich.edu    def __getattr__(self, attr):
11583101Sstever@eecs.umich.edu        if attr == 'frequency':
11593101Sstever@eecs.umich.edu            return self
11603101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
11613101Sstever@eecs.umich.edu            return Latency(self)
11623101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
11633101Sstever@eecs.umich.edu
11644167Sbinkertn@umich.edu    # convert latency to ticks
11654762Snate@binkert.org    def getValue(self):
11664762Snate@binkert.org        if self.ticks or self.value == 0:
11674762Snate@binkert.org            value = self.value
11684762Snate@binkert.org        else:
11694762Snate@binkert.org            value = ticks.fromSeconds(1.0 / self.value)
11704762Snate@binkert.org        return long(value)
11714762Snate@binkert.org
11723101Sstever@eecs.umich.edu    def ini_str(self):
11734762Snate@binkert.org        return '%d' % self.getValue()
11743101Sstever@eecs.umich.edu
11753101Sstever@eecs.umich.edu# A generic frequency and/or Latency value.  Value is stored as a latency,
11763101Sstever@eecs.umich.edu# but to avoid ambiguity this object does not support numeric ops (* or /).
11773101Sstever@eecs.umich.edu# An explicit conversion to a Latency or Frequency must be made first.
11783101Sstever@eecs.umich.educlass Clock(ParamValue):
11793101Sstever@eecs.umich.edu    cxx_type = 'Tick'
11807673Snate@binkert.org
11817673Snate@binkert.org    @classmethod
11827673Snate@binkert.org    def cxx_predecls(cls, code):
11837673Snate@binkert.org        code('#include "base/types.hh"')
11847673Snate@binkert.org
11857673Snate@binkert.org    @classmethod
11867673Snate@binkert.org    def swig_predecls(cls, code):
11877673Snate@binkert.org        code('%import "stdint.i"')
11887673Snate@binkert.org        code('%import "base/types.hh"')
11897673Snate@binkert.org
11903101Sstever@eecs.umich.edu    def __init__(self, value):
11914167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11924167Sbinkertn@umich.edu            self.ticks = value.ticks
11934167Sbinkertn@umich.edu            self.value = value.value
11944167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11954167Sbinkertn@umich.edu            self.ticks = value.ticks
11964167Sbinkertn@umich.edu            self.value = 1.0 / value.value
11974167Sbinkertn@umich.edu        elif value.endswith('t'):
11984167Sbinkertn@umich.edu            self.ticks = True
11994167Sbinkertn@umich.edu            self.value = int(value[:-1])
12004167Sbinkertn@umich.edu        else:
12014167Sbinkertn@umich.edu            self.ticks = False
12024167Sbinkertn@umich.edu            self.value = convert.anyToLatency(value)
12033101Sstever@eecs.umich.edu
12043101Sstever@eecs.umich.edu    def __getattr__(self, attr):
12053101Sstever@eecs.umich.edu        if attr == 'frequency':
12063101Sstever@eecs.umich.edu            return Frequency(self)
12073101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
12083101Sstever@eecs.umich.edu            return Latency(self)
12093101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
12103101Sstever@eecs.umich.edu
12114762Snate@binkert.org    def getValue(self):
12124762Snate@binkert.org        return self.period.getValue()
12134762Snate@binkert.org
12143101Sstever@eecs.umich.edu    def ini_str(self):
12153101Sstever@eecs.umich.edu        return self.period.ini_str()
12163101Sstever@eecs.umich.edu
12173101Sstever@eecs.umich.educlass NetworkBandwidth(float,ParamValue):
12183101Sstever@eecs.umich.edu    cxx_type = 'float'
12193101Sstever@eecs.umich.edu    def __new__(cls, value):
12204167Sbinkertn@umich.edu        # convert to bits per second
12214167Sbinkertn@umich.edu        val = convert.toNetworkBandwidth(value)
12223101Sstever@eecs.umich.edu        return super(cls, NetworkBandwidth).__new__(cls, val)
12233101Sstever@eecs.umich.edu
12243101Sstever@eecs.umich.edu    def __str__(self):
12253101Sstever@eecs.umich.edu        return str(self.val)
12263101Sstever@eecs.umich.edu
12274762Snate@binkert.org    def getValue(self):
12284167Sbinkertn@umich.edu        # convert to seconds per byte
12294167Sbinkertn@umich.edu        value = 8.0 / float(self)
12304167Sbinkertn@umich.edu        # convert to ticks per byte
12314762Snate@binkert.org        value = ticks.fromSeconds(value)
12324762Snate@binkert.org        return float(value)
12334762Snate@binkert.org
12344762Snate@binkert.org    def ini_str(self):
12354762Snate@binkert.org        return '%f' % self.getValue()
12363101Sstever@eecs.umich.edu
12373101Sstever@eecs.umich.educlass MemoryBandwidth(float,ParamValue):
12383101Sstever@eecs.umich.edu    cxx_type = 'float'
12395469Snate@binkert.org    def __new__(cls, value):
12407743Sgblack@eecs.umich.edu        # convert to bytes per second
12413102Sstever@eecs.umich.edu        val = convert.toMemoryBandwidth(value)
12423101Sstever@eecs.umich.edu        return super(cls, MemoryBandwidth).__new__(cls, val)
12433101Sstever@eecs.umich.edu
12443101Sstever@eecs.umich.edu    def __str__(self):
12453101Sstever@eecs.umich.edu        return str(self.val)
12463101Sstever@eecs.umich.edu
12474762Snate@binkert.org    def getValue(self):
12484167Sbinkertn@umich.edu        # convert to seconds per byte
12495468Snate@binkert.org        value = float(self)
12505468Snate@binkert.org        if value:
12515468Snate@binkert.org            value = 1.0 / float(self)
12524167Sbinkertn@umich.edu        # convert to ticks per byte
12534762Snate@binkert.org        value = ticks.fromSeconds(value)
12544762Snate@binkert.org        return float(value)
12554762Snate@binkert.org
12564762Snate@binkert.org    def ini_str(self):
12574762Snate@binkert.org        return '%f' % self.getValue()
12583101Sstever@eecs.umich.edu
12593101Sstever@eecs.umich.edu#
12603101Sstever@eecs.umich.edu# "Constants"... handy aliases for various values.
12613101Sstever@eecs.umich.edu#
12623101Sstever@eecs.umich.edu
12633102Sstever@eecs.umich.edu# Special class for NULL pointers.  Note the special check in
12643102Sstever@eecs.umich.edu# make_param_value() above that lets these be assigned where a
12653102Sstever@eecs.umich.edu# SimObject is required.
12663102Sstever@eecs.umich.edu# only one copy of a particular node
12673102Sstever@eecs.umich.educlass NullSimObject(object):
12683102Sstever@eecs.umich.edu    __metaclass__ = Singleton
12693102Sstever@eecs.umich.edu
12703102Sstever@eecs.umich.edu    def __call__(cls):
12713102Sstever@eecs.umich.edu        return cls
12723102Sstever@eecs.umich.edu
12733102Sstever@eecs.umich.edu    def _instantiate(self, parent = None, path = ''):
12743102Sstever@eecs.umich.edu        pass
12753102Sstever@eecs.umich.edu
12763102Sstever@eecs.umich.edu    def ini_str(self):
12773102Sstever@eecs.umich.edu        return 'Null'
12783102Sstever@eecs.umich.edu
12793102Sstever@eecs.umich.edu    def unproxy(self, base):
12803102Sstever@eecs.umich.edu        return self
12813102Sstever@eecs.umich.edu
12823102Sstever@eecs.umich.edu    def set_path(self, parent, name):
12833102Sstever@eecs.umich.edu        pass
12844762Snate@binkert.org
12853102Sstever@eecs.umich.edu    def __str__(self):
12863102Sstever@eecs.umich.edu        return 'Null'
12873102Sstever@eecs.umich.edu
12884762Snate@binkert.org    def getValue(self):
12894762Snate@binkert.org        return None
12904762Snate@binkert.org
12913102Sstever@eecs.umich.edu# The only instance you'll ever need...
12923102Sstever@eecs.umich.eduNULL = NullSimObject()
12933102Sstever@eecs.umich.edu
12943102Sstever@eecs.umich.edudef isNullPointer(value):
12953102Sstever@eecs.umich.edu    return isinstance(value, NullSimObject)
12963102Sstever@eecs.umich.edu
12973101Sstever@eecs.umich.edu# Some memory range specifications use this as a default upper bound.
12983101Sstever@eecs.umich.eduMaxAddr = Addr.max
12993101Sstever@eecs.umich.eduMaxTick = Tick.max
13003101Sstever@eecs.umich.eduAllMemory = AddrRange(0, MaxAddr)
13013101Sstever@eecs.umich.edu
13023101Sstever@eecs.umich.edu
13033101Sstever@eecs.umich.edu#####################################################################
13043101Sstever@eecs.umich.edu#
13053101Sstever@eecs.umich.edu# Port objects
13063101Sstever@eecs.umich.edu#
13073101Sstever@eecs.umich.edu# Ports are used to interconnect objects in the memory system.
13083101Sstever@eecs.umich.edu#
13093101Sstever@eecs.umich.edu#####################################################################
13103101Sstever@eecs.umich.edu
13113101Sstever@eecs.umich.edu# Port reference: encapsulates a reference to a particular port on a
13123101Sstever@eecs.umich.edu# particular SimObject.
13133101Sstever@eecs.umich.educlass PortRef(object):
13143105Sstever@eecs.umich.edu    def __init__(self, simobj, name):
13153105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
13163101Sstever@eecs.umich.edu        self.simobj = simobj
13173101Sstever@eecs.umich.edu        self.name = name
13183101Sstever@eecs.umich.edu        self.peer = None   # not associated with another port yet
13193101Sstever@eecs.umich.edu        self.ccConnected = False # C++ port connection done?
13203105Sstever@eecs.umich.edu        self.index = -1  # always -1 for non-vector ports
13213101Sstever@eecs.umich.edu
13223103Sstever@eecs.umich.edu    def __str__(self):
13233105Sstever@eecs.umich.edu        return '%s.%s' % (self.simobj, self.name)
13243103Sstever@eecs.umich.edu
13253105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
13263105Sstever@eecs.umich.edu    def ini_str(self):
13273105Sstever@eecs.umich.edu        return str(self.peer)
13283105Sstever@eecs.umich.edu
13293105Sstever@eecs.umich.edu    def __getattr__(self, attr):
13303105Sstever@eecs.umich.edu        if attr == 'peerObj':
13313105Sstever@eecs.umich.edu            # shorthand for proxies
13323105Sstever@eecs.umich.edu            return self.peer.simobj
13333105Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
13343105Sstever@eecs.umich.edu              (self.__class__.__name__, attr)
13353105Sstever@eecs.umich.edu
13363105Sstever@eecs.umich.edu    # Full connection is symmetric (both ways).  Called via
13373105Sstever@eecs.umich.edu    # SimObject.__setattr__ as a result of a port assignment, e.g.,
13383109Sstever@eecs.umich.edu    # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,
13393105Sstever@eecs.umich.edu    # e.g., "obj1.portA[3] = obj2.portB".
13403105Sstever@eecs.umich.edu    def connect(self, other):
13413105Sstever@eecs.umich.edu        if isinstance(other, VectorPortRef):
13423105Sstever@eecs.umich.edu            # reference to plain VectorPort is implicit append
13433105Sstever@eecs.umich.edu            other = other._get_next()
13443105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
13453105Sstever@eecs.umich.edu            print "warning: overwriting port", self, \
13463105Sstever@eecs.umich.edu                  "value", self.peer, "with", other
13475578SSteve.Reinhardt@amd.com            self.peer.peer = None
13483101Sstever@eecs.umich.edu        self.peer = other
13493109Sstever@eecs.umich.edu        if proxy.isproxy(other):
13503109Sstever@eecs.umich.edu            other.set_param_desc(PortParamDesc())
13513109Sstever@eecs.umich.edu        elif isinstance(other, PortRef):
13523109Sstever@eecs.umich.edu            if other.peer is not self:
13533109Sstever@eecs.umich.edu                other.connect(self)
13543109Sstever@eecs.umich.edu        else:
13553109Sstever@eecs.umich.edu            raise TypeError, \
13563109Sstever@eecs.umich.edu                  "assigning non-port reference '%s' to port '%s'" \
13573109Sstever@eecs.umich.edu                  % (other, self)
13583101Sstever@eecs.umich.edu
13593105Sstever@eecs.umich.edu    def clone(self, simobj, memo):
13603105Sstever@eecs.umich.edu        if memo.has_key(self):
13613105Sstever@eecs.umich.edu            return memo[self]
13623101Sstever@eecs.umich.edu        newRef = copy.copy(self)
13633105Sstever@eecs.umich.edu        memo[self] = newRef
13643105Sstever@eecs.umich.edu        newRef.simobj = simobj
13653101Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
13663105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
13673179Sstever@eecs.umich.edu            peerObj = self.peer.simobj(_memo=memo)
13683105Sstever@eecs.umich.edu            newRef.peer = self.peer.clone(peerObj, memo)
13693105Sstever@eecs.umich.edu            assert(not isinstance(newRef.peer, VectorPortRef))
13703101Sstever@eecs.umich.edu        return newRef
13713101Sstever@eecs.umich.edu
13723105Sstever@eecs.umich.edu    def unproxy(self, simobj):
13733105Sstever@eecs.umich.edu        assert(simobj is self.simobj)
13743105Sstever@eecs.umich.edu        if proxy.isproxy(self.peer):
13753105Sstever@eecs.umich.edu            try:
13763105Sstever@eecs.umich.edu                realPeer = self.peer.unproxy(self.simobj)
13773105Sstever@eecs.umich.edu            except:
13783105Sstever@eecs.umich.edu                print "Error in unproxying port '%s' of %s" % \
13793105Sstever@eecs.umich.edu                      (self.name, self.simobj.path())
13803105Sstever@eecs.umich.edu                raise
13813105Sstever@eecs.umich.edu            self.connect(realPeer)
13823105Sstever@eecs.umich.edu
13833101Sstever@eecs.umich.edu    # Call C++ to create corresponding port connection between C++ objects
13843101Sstever@eecs.umich.edu    def ccConnect(self):
13858597Ssteve.reinhardt@amd.com        from m5.internal.pyobject import connectPorts
13864762Snate@binkert.org
13873101Sstever@eecs.umich.edu        if self.ccConnected: # already done this
13883101Sstever@eecs.umich.edu            return
13893101Sstever@eecs.umich.edu        peer = self.peer
13905578SSteve.Reinhardt@amd.com        if not self.peer: # nothing to connect to
13915578SSteve.Reinhardt@amd.com            return
13927526Ssteve.reinhardt@amd.com        try:
13937526Ssteve.reinhardt@amd.com            connectPorts(self.simobj.getCCObject(), self.name, self.index,
13947526Ssteve.reinhardt@amd.com                         peer.simobj.getCCObject(), peer.name, peer.index)
13957526Ssteve.reinhardt@amd.com        except:
13967526Ssteve.reinhardt@amd.com            print "Error connecting port %s.%s to %s.%s" % \
13977526Ssteve.reinhardt@amd.com                  (self.simobj.path(), self.name,
13987526Ssteve.reinhardt@amd.com                   peer.simobj.path(), peer.name)
13997526Ssteve.reinhardt@amd.com            raise
14003101Sstever@eecs.umich.edu        self.ccConnected = True
14013101Sstever@eecs.umich.edu        peer.ccConnected = True
14023101Sstever@eecs.umich.edu
14033105Sstever@eecs.umich.edu# A reference to an individual element of a VectorPort... much like a
14043105Sstever@eecs.umich.edu# PortRef, but has an index.
14053105Sstever@eecs.umich.educlass VectorPortElementRef(PortRef):
14063105Sstever@eecs.umich.edu    def __init__(self, simobj, name, index):
14073105Sstever@eecs.umich.edu        PortRef.__init__(self, simobj, name)
14083105Sstever@eecs.umich.edu        self.index = index
14093105Sstever@eecs.umich.edu
14103105Sstever@eecs.umich.edu    def __str__(self):
14113105Sstever@eecs.umich.edu        return '%s.%s[%d]' % (self.simobj, self.name, self.index)
14123105Sstever@eecs.umich.edu
14133105Sstever@eecs.umich.edu# A reference to a complete vector-valued port (not just a single element).
14143105Sstever@eecs.umich.edu# Can be indexed to retrieve individual VectorPortElementRef instances.
14153105Sstever@eecs.umich.educlass VectorPortRef(object):
14163105Sstever@eecs.umich.edu    def __init__(self, simobj, name):
14173105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
14183105Sstever@eecs.umich.edu        self.simobj = simobj
14193105Sstever@eecs.umich.edu        self.name = name
14203105Sstever@eecs.umich.edu        self.elements = []
14213105Sstever@eecs.umich.edu
14223109Sstever@eecs.umich.edu    def __str__(self):
14233109Sstever@eecs.umich.edu        return '%s.%s[:]' % (self.simobj, self.name)
14243109Sstever@eecs.umich.edu
14253105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
14263105Sstever@eecs.umich.edu    def ini_str(self):
14273105Sstever@eecs.umich.edu        return ' '.join([el.ini_str() for el in self.elements])
14283105Sstever@eecs.umich.edu
14293105Sstever@eecs.umich.edu    def __getitem__(self, key):
14303105Sstever@eecs.umich.edu        if not isinstance(key, int):
14313105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
14323105Sstever@eecs.umich.edu        if key >= len(self.elements):
14333105Sstever@eecs.umich.edu            # need to extend list
14343105Sstever@eecs.umich.edu            ext = [VectorPortElementRef(self.simobj, self.name, i)
14353105Sstever@eecs.umich.edu                   for i in range(len(self.elements), key+1)]
14363105Sstever@eecs.umich.edu            self.elements.extend(ext)
14373105Sstever@eecs.umich.edu        return self.elements[key]
14383105Sstever@eecs.umich.edu
14393105Sstever@eecs.umich.edu    def _get_next(self):
14403105Sstever@eecs.umich.edu        return self[len(self.elements)]
14413105Sstever@eecs.umich.edu
14423105Sstever@eecs.umich.edu    def __setitem__(self, key, value):
14433105Sstever@eecs.umich.edu        if not isinstance(key, int):
14443105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
14453105Sstever@eecs.umich.edu        self[key].connect(value)
14463105Sstever@eecs.umich.edu
14473105Sstever@eecs.umich.edu    def connect(self, other):
14483109Sstever@eecs.umich.edu        if isinstance(other, (list, tuple)):
14493109Sstever@eecs.umich.edu            # Assign list of port refs to vector port.
14503109Sstever@eecs.umich.edu            # For now, append them... not sure if that's the right semantics
14513109Sstever@eecs.umich.edu            # or if it should replace the current vector.
14523109Sstever@eecs.umich.edu            for ref in other:
14533109Sstever@eecs.umich.edu                self._get_next().connect(ref)
14543109Sstever@eecs.umich.edu        else:
14553109Sstever@eecs.umich.edu            # scalar assignment to plain VectorPort is implicit append
14563109Sstever@eecs.umich.edu            self._get_next().connect(other)
14573109Sstever@eecs.umich.edu
14583109Sstever@eecs.umich.edu    def clone(self, simobj, memo):
14593109Sstever@eecs.umich.edu        if memo.has_key(self):
14603109Sstever@eecs.umich.edu            return memo[self]
14613109Sstever@eecs.umich.edu        newRef = copy.copy(self)
14623109Sstever@eecs.umich.edu        memo[self] = newRef
14633109Sstever@eecs.umich.edu        newRef.simobj = simobj
14643109Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
14653109Sstever@eecs.umich.edu        newRef.elements = [el.clone(simobj, memo) for el in self.elements]
14663109Sstever@eecs.umich.edu        return newRef
14673105Sstever@eecs.umich.edu
14683105Sstever@eecs.umich.edu    def unproxy(self, simobj):
14693105Sstever@eecs.umich.edu        [el.unproxy(simobj) for el in self.elements]
14703105Sstever@eecs.umich.edu
14713105Sstever@eecs.umich.edu    def ccConnect(self):
14723105Sstever@eecs.umich.edu        [el.ccConnect() for el in self.elements]
14733105Sstever@eecs.umich.edu
14743101Sstever@eecs.umich.edu# Port description object.  Like a ParamDesc object, this represents a
14753101Sstever@eecs.umich.edu# logical port in the SimObject class, not a particular port on a
14763101Sstever@eecs.umich.edu# SimObject instance.  The latter are represented by PortRef objects.
14773101Sstever@eecs.umich.educlass Port(object):
14783105Sstever@eecs.umich.edu    # Port("description") or Port(default, "description")
14793105Sstever@eecs.umich.edu    def __init__(self, *args):
14803105Sstever@eecs.umich.edu        if len(args) == 1:
14813105Sstever@eecs.umich.edu            self.desc = args[0]
14823105Sstever@eecs.umich.edu        elif len(args) == 2:
14833105Sstever@eecs.umich.edu            self.default = args[0]
14843105Sstever@eecs.umich.edu            self.desc = args[1]
14853105Sstever@eecs.umich.edu        else:
14863105Sstever@eecs.umich.edu            raise TypeError, 'wrong number of arguments'
14873105Sstever@eecs.umich.edu        # self.name is set by SimObject class on assignment
14883105Sstever@eecs.umich.edu        # e.g., pio_port = Port("blah") sets self.name to 'pio_port'
14893101Sstever@eecs.umich.edu
14903101Sstever@eecs.umich.edu    # Generate a PortRef for this port on the given SimObject with the
14913101Sstever@eecs.umich.edu    # given name
14923105Sstever@eecs.umich.edu    def makeRef(self, simobj):
14933105Sstever@eecs.umich.edu        return PortRef(simobj, self.name)
14943101Sstever@eecs.umich.edu
14953101Sstever@eecs.umich.edu    # Connect an instance of this port (on the given SimObject with
14963101Sstever@eecs.umich.edu    # the given name) with the port described by the supplied PortRef
14973105Sstever@eecs.umich.edu    def connect(self, simobj, ref):
14983105Sstever@eecs.umich.edu        self.makeRef(simobj).connect(ref)
14993101Sstever@eecs.umich.edu
15003101Sstever@eecs.umich.edu# VectorPort description object.  Like Port, but represents a vector
15013101Sstever@eecs.umich.edu# of connections (e.g., as on a Bus).
15023101Sstever@eecs.umich.educlass VectorPort(Port):
15033105Sstever@eecs.umich.edu    def __init__(self, *args):
15043105Sstever@eecs.umich.edu        Port.__init__(self, *args)
15053101Sstever@eecs.umich.edu        self.isVec = True
15063101Sstever@eecs.umich.edu
15073105Sstever@eecs.umich.edu    def makeRef(self, simobj):
15083105Sstever@eecs.umich.edu        return VectorPortRef(simobj, self.name)
15093105Sstever@eecs.umich.edu
15103109Sstever@eecs.umich.edu# 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
15113109Sstever@eecs.umich.edu# proxy objects (via set_param_desc()) so that proxy error messages
15123109Sstever@eecs.umich.edu# make sense.
15133109Sstever@eecs.umich.educlass PortParamDesc(object):
15143109Sstever@eecs.umich.edu    __metaclass__ = Singleton
15153109Sstever@eecs.umich.edu
15163109Sstever@eecs.umich.edu    ptype_str = 'Port'
15173109Sstever@eecs.umich.edu    ptype = Port
15183105Sstever@eecs.umich.edu
15196654Snate@binkert.orgbaseEnums = allEnums.copy()
15206654Snate@binkert.orgbaseParams = allParams.copy()
15216654Snate@binkert.org
15226654Snate@binkert.orgdef clear():
15236654Snate@binkert.org    global allEnums, allParams
15246654Snate@binkert.org
15256654Snate@binkert.org    allEnums = baseEnums.copy()
15266654Snate@binkert.org    allParams = baseParams.copy()
15276654Snate@binkert.org
15283101Sstever@eecs.umich.edu__all__ = ['Param', 'VectorParam',
15293101Sstever@eecs.umich.edu           'Enum', 'Bool', 'String', 'Float',
15303101Sstever@eecs.umich.edu           'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
15313101Sstever@eecs.umich.edu           'Int32', 'UInt32', 'Int64', 'UInt64',
15323101Sstever@eecs.umich.edu           'Counter', 'Addr', 'Tick', 'Percent',
15333101Sstever@eecs.umich.edu           'TcpPort', 'UdpPort', 'EthernetAddr',
15347777Sgblack@eecs.umich.edu           'IpAddress', 'IpNetmask', 'IpWithPort',
15353101Sstever@eecs.umich.edu           'MemorySize', 'MemorySize32',
15364167Sbinkertn@umich.edu           'Latency', 'Frequency', 'Clock',
15373101Sstever@eecs.umich.edu           'NetworkBandwidth', 'MemoryBandwidth',
15383101Sstever@eecs.umich.edu           'Range', 'AddrRange', 'TickRange',
15393101Sstever@eecs.umich.edu           'MaxAddr', 'MaxTick', 'AllMemory',
15403885Sbinkertn@umich.edu           'Time',
15413102Sstever@eecs.umich.edu           'NextEthernetAddr', 'NULL',
15423101Sstever@eecs.umich.edu           'Port', 'VectorPort']
15436654Snate@binkert.org
15446654Snate@binkert.orgimport SimObject
1545