params.py revision 10019
19544Sandreas.hansson@arm.com# Copyright (c) 2012-2013 ARM Limited
28839Sandreas.hansson@arm.com# All rights reserved.
38839Sandreas.hansson@arm.com#
48839Sandreas.hansson@arm.com# The license below extends only to copyright in the software and shall
58839Sandreas.hansson@arm.com# not be construed as granting a license to any other intellectual
68839Sandreas.hansson@arm.com# property including but not limited to intellectual property relating
78839Sandreas.hansson@arm.com# to a hardware implementation of the functionality of the software
88839Sandreas.hansson@arm.com# licensed hereunder.  You may use the software subject to the license
98839Sandreas.hansson@arm.com# terms below provided that you ensure that this notice is replicated
108839Sandreas.hansson@arm.com# unmodified and in its entirety in all distributions of the software,
118839Sandreas.hansson@arm.com# modified or unmodified, in source code or in binary form.
128839Sandreas.hansson@arm.com#
133101Sstever@eecs.umich.edu# Copyright (c) 2004-2006 The Regents of The University of Michigan
148579Ssteve.reinhardt@amd.com# Copyright (c) 2010-2011 Advanced Micro Devices, Inc.
153101Sstever@eecs.umich.edu# All rights reserved.
163101Sstever@eecs.umich.edu#
173101Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
183101Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
193101Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
203101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
213101Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
223101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
233101Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
243101Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
253101Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
263101Sstever@eecs.umich.edu# this software without specific prior written permission.
273101Sstever@eecs.umich.edu#
283101Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
293101Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
303101Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
313101Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
323101Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
333101Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
343101Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
353101Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
363101Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
373101Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
383101Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
393101Sstever@eecs.umich.edu#
403101Sstever@eecs.umich.edu# Authors: Steve Reinhardt
413101Sstever@eecs.umich.edu#          Nathan Binkert
427778Sgblack@eecs.umich.edu#          Gabe Black
438839Sandreas.hansson@arm.com#          Andreas Hansson
443101Sstever@eecs.umich.edu
453101Sstever@eecs.umich.edu#####################################################################
463101Sstever@eecs.umich.edu#
473101Sstever@eecs.umich.edu# Parameter description classes
483101Sstever@eecs.umich.edu#
493101Sstever@eecs.umich.edu# The _params dictionary in each class maps parameter names to either
503101Sstever@eecs.umich.edu# a Param or a VectorParam object.  These objects contain the
513101Sstever@eecs.umich.edu# parameter description string, the parameter type, and the default
523101Sstever@eecs.umich.edu# value (if any).  The convert() method on these objects is used to
533101Sstever@eecs.umich.edu# force whatever value is assigned to the parameter to the appropriate
543101Sstever@eecs.umich.edu# type.
553101Sstever@eecs.umich.edu#
563101Sstever@eecs.umich.edu# Note that the default values are loaded into the class's attribute
573101Sstever@eecs.umich.edu# space when the parameter dictionary is initialized (in
583101Sstever@eecs.umich.edu# MetaSimObject._new_param()); after that point they aren't used.
593101Sstever@eecs.umich.edu#
603101Sstever@eecs.umich.edu#####################################################################
613101Sstever@eecs.umich.edu
623885Sbinkertn@umich.eduimport copy
633885Sbinkertn@umich.eduimport datetime
644762Snate@binkert.orgimport re
653885Sbinkertn@umich.eduimport sys
663885Sbinkertn@umich.eduimport time
677528Ssteve.reinhardt@amd.comimport math
683885Sbinkertn@umich.edu
694380Sbinkertn@umich.eduimport proxy
704167Sbinkertn@umich.eduimport ticks
713102Sstever@eecs.umich.edufrom util import *
723101Sstever@eecs.umich.edu
734762Snate@binkert.orgdef isSimObject(*args, **kwargs):
744762Snate@binkert.org    return SimObject.isSimObject(*args, **kwargs)
754762Snate@binkert.org
764762Snate@binkert.orgdef isSimObjectSequence(*args, **kwargs):
774762Snate@binkert.org    return SimObject.isSimObjectSequence(*args, **kwargs)
784762Snate@binkert.org
794762Snate@binkert.orgdef isSimObjectClass(*args, **kwargs):
804762Snate@binkert.org    return SimObject.isSimObjectClass(*args, **kwargs)
814762Snate@binkert.org
825033Smilesck@eecs.umich.eduallParams = {}
835033Smilesck@eecs.umich.edu
845033Smilesck@eecs.umich.educlass MetaParamValue(type):
855033Smilesck@eecs.umich.edu    def __new__(mcls, name, bases, dct):
865033Smilesck@eecs.umich.edu        cls = super(MetaParamValue, mcls).__new__(mcls, name, bases, dct)
875033Smilesck@eecs.umich.edu        assert name not in allParams
885033Smilesck@eecs.umich.edu        allParams[name] = cls
895033Smilesck@eecs.umich.edu        return cls
905033Smilesck@eecs.umich.edu
915033Smilesck@eecs.umich.edu
923101Sstever@eecs.umich.edu# Dummy base class to identify types that are legitimate for SimObject
933101Sstever@eecs.umich.edu# parameters.
943101Sstever@eecs.umich.educlass ParamValue(object):
955033Smilesck@eecs.umich.edu    __metaclass__ = MetaParamValue
963101Sstever@eecs.umich.edu
978596Ssteve.reinhardt@amd.com
988596Ssteve.reinhardt@amd.com    # Generate the code needed as a prerequisite for declaring a C++
998596Ssteve.reinhardt@amd.com    # object of this type.  Typically generates one or more #include
1008596Ssteve.reinhardt@amd.com    # statements.  Used when declaring parameters of this type.
1017673Snate@binkert.org    @classmethod
1027673Snate@binkert.org    def cxx_predecls(cls, code):
1037673Snate@binkert.org        pass
1047673Snate@binkert.org
1058596Ssteve.reinhardt@amd.com    # Generate the code needed as a prerequisite for including a
1068596Ssteve.reinhardt@amd.com    # reference to a C++ object of this type in a SWIG .i file.
1078596Ssteve.reinhardt@amd.com    # Typically generates one or more %import or %include statements.
1087673Snate@binkert.org    @classmethod
1097673Snate@binkert.org    def swig_predecls(cls, code):
1107673Snate@binkert.org        pass
1113101Sstever@eecs.umich.edu
1123101Sstever@eecs.umich.edu    # default for printing to .ini file is regular string conversion.
1133101Sstever@eecs.umich.edu    # will be overridden in some cases
1143101Sstever@eecs.umich.edu    def ini_str(self):
1153101Sstever@eecs.umich.edu        return str(self)
1163101Sstever@eecs.umich.edu
1173101Sstever@eecs.umich.edu    # allows us to blithely call unproxy() on things without checking
1183101Sstever@eecs.umich.edu    # if they're really proxies or not
1193101Sstever@eecs.umich.edu    def unproxy(self, base):
1203101Sstever@eecs.umich.edu        return self
1213101Sstever@eecs.umich.edu
1223101Sstever@eecs.umich.edu# Regular parameter description.
1233101Sstever@eecs.umich.educlass ParamDesc(object):
1243101Sstever@eecs.umich.edu    def __init__(self, ptype_str, ptype, *args, **kwargs):
1253101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
1263101Sstever@eecs.umich.edu        # remember ptype only if it is provided
1273101Sstever@eecs.umich.edu        if ptype != None:
1283101Sstever@eecs.umich.edu            self.ptype = ptype
1293101Sstever@eecs.umich.edu
1303101Sstever@eecs.umich.edu        if args:
1313101Sstever@eecs.umich.edu            if len(args) == 1:
1323101Sstever@eecs.umich.edu                self.desc = args[0]
1333101Sstever@eecs.umich.edu            elif len(args) == 2:
1343101Sstever@eecs.umich.edu                self.default = args[0]
1353101Sstever@eecs.umich.edu                self.desc = args[1]
1363101Sstever@eecs.umich.edu            else:
1373101Sstever@eecs.umich.edu                raise TypeError, 'too many arguments'
1383101Sstever@eecs.umich.edu
1393101Sstever@eecs.umich.edu        if kwargs.has_key('desc'):
1403101Sstever@eecs.umich.edu            assert(not hasattr(self, 'desc'))
1413101Sstever@eecs.umich.edu            self.desc = kwargs['desc']
1423101Sstever@eecs.umich.edu            del kwargs['desc']
1433101Sstever@eecs.umich.edu
1443101Sstever@eecs.umich.edu        if kwargs.has_key('default'):
1453101Sstever@eecs.umich.edu            assert(not hasattr(self, 'default'))
1463101Sstever@eecs.umich.edu            self.default = kwargs['default']
1473101Sstever@eecs.umich.edu            del kwargs['default']
1483101Sstever@eecs.umich.edu
1493101Sstever@eecs.umich.edu        if kwargs:
1503101Sstever@eecs.umich.edu            raise TypeError, 'extra unknown kwargs %s' % kwargs
1513101Sstever@eecs.umich.edu
1523101Sstever@eecs.umich.edu        if not hasattr(self, 'desc'):
1533101Sstever@eecs.umich.edu            raise TypeError, 'desc attribute missing'
1543101Sstever@eecs.umich.edu
1553101Sstever@eecs.umich.edu    def __getattr__(self, attr):
1563101Sstever@eecs.umich.edu        if attr == 'ptype':
1575033Smilesck@eecs.umich.edu            ptype = SimObject.allClasses[self.ptype_str]
1586656Snate@binkert.org            assert isSimObjectClass(ptype)
1595033Smilesck@eecs.umich.edu            self.ptype = ptype
1605033Smilesck@eecs.umich.edu            return ptype
1615033Smilesck@eecs.umich.edu
1623101Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
1633101Sstever@eecs.umich.edu              (type(self).__name__, attr)
1643101Sstever@eecs.umich.edu
1653101Sstever@eecs.umich.edu    def convert(self, value):
1663101Sstever@eecs.umich.edu        if isinstance(value, proxy.BaseProxy):
1673101Sstever@eecs.umich.edu            value.set_param_desc(self)
1683101Sstever@eecs.umich.edu            return value
1693101Sstever@eecs.umich.edu        if not hasattr(self, 'ptype') and isNullPointer(value):
1703101Sstever@eecs.umich.edu            # deferred evaluation of SimObject; continue to defer if
1713101Sstever@eecs.umich.edu            # we're just assigning a null pointer
1723101Sstever@eecs.umich.edu            return value
1733101Sstever@eecs.umich.edu        if isinstance(value, self.ptype):
1743101Sstever@eecs.umich.edu            return value
1753102Sstever@eecs.umich.edu        if isNullPointer(value) and isSimObjectClass(self.ptype):
1763101Sstever@eecs.umich.edu            return value
1773101Sstever@eecs.umich.edu        return self.ptype(value)
1783101Sstever@eecs.umich.edu
1797673Snate@binkert.org    def cxx_predecls(self, code):
1808607Sgblack@eecs.umich.edu        code('#include <cstddef>')
1817673Snate@binkert.org        self.ptype.cxx_predecls(code)
1823101Sstever@eecs.umich.edu
1837673Snate@binkert.org    def swig_predecls(self, code):
1847673Snate@binkert.org        self.ptype.swig_predecls(code)
1853101Sstever@eecs.umich.edu
1867673Snate@binkert.org    def cxx_decl(self, code):
1877673Snate@binkert.org        code('${{self.ptype.cxx_type}} ${{self.name}};')
1883101Sstever@eecs.umich.edu
1893101Sstever@eecs.umich.edu# Vector-valued parameter description.  Just like ParamDesc, except
1903101Sstever@eecs.umich.edu# that the value is a vector (list) of the specified type instead of a
1913101Sstever@eecs.umich.edu# single value.
1923101Sstever@eecs.umich.edu
1933101Sstever@eecs.umich.educlass VectorParamValue(list):
1945033Smilesck@eecs.umich.edu    __metaclass__ = MetaParamValue
1955475Snate@binkert.org    def __setattr__(self, attr, value):
1965475Snate@binkert.org        raise AttributeError, \
1975475Snate@binkert.org              "Not allowed to set %s on '%s'" % (attr, type(self).__name__)
1985475Snate@binkert.org
1993101Sstever@eecs.umich.edu    def ini_str(self):
2003101Sstever@eecs.umich.edu        return ' '.join([v.ini_str() for v in self])
2013101Sstever@eecs.umich.edu
2024762Snate@binkert.org    def getValue(self):
2034762Snate@binkert.org        return [ v.getValue() for v in self ]
2044762Snate@binkert.org
2053101Sstever@eecs.umich.edu    def unproxy(self, base):
2068460SAli.Saidi@ARM.com        if len(self) == 1 and isinstance(self[0], proxy.AllProxy):
2078459SAli.Saidi@ARM.com            return self[0].unproxy(base)
2088459SAli.Saidi@ARM.com        else:
2098459SAli.Saidi@ARM.com             return [v.unproxy(base) for v in self]
2103101Sstever@eecs.umich.edu
2117528Ssteve.reinhardt@amd.comclass SimObjectVector(VectorParamValue):
2127528Ssteve.reinhardt@amd.com    # support clone operation
2137528Ssteve.reinhardt@amd.com    def __call__(self, **kwargs):
2147528Ssteve.reinhardt@amd.com        return SimObjectVector([v(**kwargs) for v in self])
2157528Ssteve.reinhardt@amd.com
2167528Ssteve.reinhardt@amd.com    def clear_parent(self, old_parent):
2173101Sstever@eecs.umich.edu        for v in self:
2187528Ssteve.reinhardt@amd.com            v.clear_parent(old_parent)
2197528Ssteve.reinhardt@amd.com
2207528Ssteve.reinhardt@amd.com    def set_parent(self, parent, name):
2217528Ssteve.reinhardt@amd.com        if len(self) == 1:
2227528Ssteve.reinhardt@amd.com            self[0].set_parent(parent, name)
2237528Ssteve.reinhardt@amd.com        else:
2247528Ssteve.reinhardt@amd.com            width = int(math.ceil(math.log(len(self))/math.log(10)))
2257528Ssteve.reinhardt@amd.com            for i,v in enumerate(self):
2267528Ssteve.reinhardt@amd.com                v.set_parent(parent, "%s%0*d" % (name, width, i))
2277528Ssteve.reinhardt@amd.com
2288321Ssteve.reinhardt@amd.com    def has_parent(self):
2298321Ssteve.reinhardt@amd.com        return reduce(lambda x,y: x and y, [v.has_parent() for v in self])
2307528Ssteve.reinhardt@amd.com
2317528Ssteve.reinhardt@amd.com    # return 'cpu0 cpu1' etc. for print_ini()
2327528Ssteve.reinhardt@amd.com    def get_name(self):
2337528Ssteve.reinhardt@amd.com        return ' '.join([v._name for v in self])
2347528Ssteve.reinhardt@amd.com
2357528Ssteve.reinhardt@amd.com    # By iterating through the constituent members of the vector here
2367528Ssteve.reinhardt@amd.com    # we can nicely handle iterating over all a SimObject's children
2377528Ssteve.reinhardt@amd.com    # without having to provide lots of special functions on
2387528Ssteve.reinhardt@amd.com    # SimObjectVector directly.
2397528Ssteve.reinhardt@amd.com    def descendants(self):
2407528Ssteve.reinhardt@amd.com        for v in self:
2417528Ssteve.reinhardt@amd.com            for obj in v.descendants():
2427528Ssteve.reinhardt@amd.com                yield obj
2433101Sstever@eecs.umich.edu
2448664SAli.Saidi@ARM.com    def get_config_as_dict(self):
2458664SAli.Saidi@ARM.com        a = []
2468664SAli.Saidi@ARM.com        for v in self:
2478664SAli.Saidi@ARM.com            a.append(v.get_config_as_dict())
2488664SAli.Saidi@ARM.com        return a
2498664SAli.Saidi@ARM.com
2509953Sgeoffrey.blake@arm.com    # If we are replacing an item in the vector, make sure to set the
2519953Sgeoffrey.blake@arm.com    # parent reference of the new SimObject to be the same as the parent
2529953Sgeoffrey.blake@arm.com    # of the SimObject being replaced. Useful to have if we created
2539953Sgeoffrey.blake@arm.com    # a SimObjectVector of temporary objects that will be modified later in
2549953Sgeoffrey.blake@arm.com    # configuration scripts.
2559953Sgeoffrey.blake@arm.com    def __setitem__(self, key, value):
2569953Sgeoffrey.blake@arm.com        val = self[key]
2579953Sgeoffrey.blake@arm.com        if value.has_parent():
2589953Sgeoffrey.blake@arm.com            warn("SimObject %s already has a parent" % value.get_name() +\
2599953Sgeoffrey.blake@arm.com                 " that is being overwritten by a SimObjectVector")
2609953Sgeoffrey.blake@arm.com        value.set_parent(val.get_parent(), val._name)
2619953Sgeoffrey.blake@arm.com        super(SimObjectVector, self).__setitem__(key, value)
2629953Sgeoffrey.blake@arm.com
2633101Sstever@eecs.umich.educlass VectorParamDesc(ParamDesc):
2643101Sstever@eecs.umich.edu    # Convert assigned value to appropriate type.  If the RHS is not a
2653101Sstever@eecs.umich.edu    # list or tuple, it generates a single-element list.
2663101Sstever@eecs.umich.edu    def convert(self, value):
2673101Sstever@eecs.umich.edu        if isinstance(value, (list, tuple)):
2683101Sstever@eecs.umich.edu            # list: coerce each element into new list
2693101Sstever@eecs.umich.edu            tmp_list = [ ParamDesc.convert(self, v) for v in value ]
2703101Sstever@eecs.umich.edu        else:
2714762Snate@binkert.org            # singleton: coerce to a single-element list
2724762Snate@binkert.org            tmp_list = [ ParamDesc.convert(self, value) ]
2734762Snate@binkert.org
2744762Snate@binkert.org        if isSimObjectSequence(tmp_list):
2757528Ssteve.reinhardt@amd.com            return SimObjectVector(tmp_list)
2764762Snate@binkert.org        else:
2774762Snate@binkert.org            return VectorParamValue(tmp_list)
2784762Snate@binkert.org
2798596Ssteve.reinhardt@amd.com    def swig_module_name(self):
2808596Ssteve.reinhardt@amd.com        return "%s_vector" % self.ptype_str
2818596Ssteve.reinhardt@amd.com
2827673Snate@binkert.org    def swig_predecls(self, code):
2838596Ssteve.reinhardt@amd.com        code('%import "${{self.swig_module_name()}}.i"')
2844762Snate@binkert.org
2857673Snate@binkert.org    def swig_decl(self, code):
2868596Ssteve.reinhardt@amd.com        code('%module(package="m5.internal") ${{self.swig_module_name()}}')
2877675Snate@binkert.org        code('%{')
2887675Snate@binkert.org        self.ptype.cxx_predecls(code)
2897675Snate@binkert.org        code('%}')
2907675Snate@binkert.org        code()
2918656Sandreas.hansson@arm.com        # Make sure the SWIGPY_SLICE_ARG is defined through this inclusion
2928656Sandreas.hansson@arm.com        code('%include "std_container.i"')
2938656Sandreas.hansson@arm.com        code()
2947675Snate@binkert.org        self.ptype.swig_predecls(code)
2957675Snate@binkert.org        code()
2967673Snate@binkert.org        code('%include "std_vector.i"')
2977675Snate@binkert.org        code()
2987675Snate@binkert.org
2997675Snate@binkert.org        ptype = self.ptype_str
3007675Snate@binkert.org        cxx_type = self.ptype.cxx_type
3017675Snate@binkert.org
3027673Snate@binkert.org        code('''\
3037675Snate@binkert.org%typemap(in) std::vector< $cxx_type >::value_type {
3047675Snate@binkert.org    if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
3057675Snate@binkert.org        if (SWIG_ConvertPtr($$input, (void **)&$$1,
3067675Snate@binkert.org                            $$descriptor($cxx_type), 0) == -1) {
3077675Snate@binkert.org            return NULL;
3087675Snate@binkert.org        }
3097675Snate@binkert.org    }
3107675Snate@binkert.org}
3117675Snate@binkert.org
3127675Snate@binkert.org%typemap(in) std::vector< $cxx_type >::value_type * {
3137675Snate@binkert.org    if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
3147675Snate@binkert.org        if (SWIG_ConvertPtr($$input, (void **)&$$1,
3157675Snate@binkert.org                            $$descriptor($cxx_type *), 0) == -1) {
3167675Snate@binkert.org            return NULL;
3177675Snate@binkert.org        }
3187675Snate@binkert.org    }
3197673Snate@binkert.org}
3207673Snate@binkert.org''')
3213101Sstever@eecs.umich.edu
3227675Snate@binkert.org        code('%template(vector_$ptype) std::vector< $cxx_type >;')
3237675Snate@binkert.org
3247673Snate@binkert.org    def cxx_predecls(self, code):
3257673Snate@binkert.org        code('#include <vector>')
3267673Snate@binkert.org        self.ptype.cxx_predecls(code)
3273101Sstever@eecs.umich.edu
3287673Snate@binkert.org    def cxx_decl(self, code):
3297673Snate@binkert.org        code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
3303101Sstever@eecs.umich.edu
3313101Sstever@eecs.umich.educlass ParamFactory(object):
3323101Sstever@eecs.umich.edu    def __init__(self, param_desc_class, ptype_str = None):
3333101Sstever@eecs.umich.edu        self.param_desc_class = param_desc_class
3343101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
3353101Sstever@eecs.umich.edu
3363101Sstever@eecs.umich.edu    def __getattr__(self, attr):
3373101Sstever@eecs.umich.edu        if self.ptype_str:
3383101Sstever@eecs.umich.edu            attr = self.ptype_str + '.' + attr
3393101Sstever@eecs.umich.edu        return ParamFactory(self.param_desc_class, attr)
3403101Sstever@eecs.umich.edu
3413101Sstever@eecs.umich.edu    # E.g., Param.Int(5, "number of widgets")
3423101Sstever@eecs.umich.edu    def __call__(self, *args, **kwargs):
3433101Sstever@eecs.umich.edu        ptype = None
3443101Sstever@eecs.umich.edu        try:
3455033Smilesck@eecs.umich.edu            ptype = allParams[self.ptype_str]
3465033Smilesck@eecs.umich.edu        except KeyError:
3473101Sstever@eecs.umich.edu            # if name isn't defined yet, assume it's a SimObject, and
3483101Sstever@eecs.umich.edu            # try to resolve it later
3493101Sstever@eecs.umich.edu            pass
3503101Sstever@eecs.umich.edu        return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs)
3513101Sstever@eecs.umich.edu
3523101Sstever@eecs.umich.eduParam = ParamFactory(ParamDesc)
3533101Sstever@eecs.umich.eduVectorParam = ParamFactory(VectorParamDesc)
3543101Sstever@eecs.umich.edu
3553101Sstever@eecs.umich.edu#####################################################################
3563101Sstever@eecs.umich.edu#
3573101Sstever@eecs.umich.edu# Parameter Types
3583101Sstever@eecs.umich.edu#
3593101Sstever@eecs.umich.edu# Though native Python types could be used to specify parameter types
3603101Sstever@eecs.umich.edu# (the 'ptype' field of the Param and VectorParam classes), it's more
3613101Sstever@eecs.umich.edu# flexible to define our own set of types.  This gives us more control
3623101Sstever@eecs.umich.edu# over how Python expressions are converted to values (via the
3633101Sstever@eecs.umich.edu# __init__() constructor) and how these values are printed out (via
3643101Sstever@eecs.umich.edu# the __str__() conversion method).
3653101Sstever@eecs.umich.edu#
3663101Sstever@eecs.umich.edu#####################################################################
3673101Sstever@eecs.umich.edu
3683101Sstever@eecs.umich.edu# String-valued parameter.  Just mixin the ParamValue class with the
3693101Sstever@eecs.umich.edu# built-in str class.
3703101Sstever@eecs.umich.educlass String(ParamValue,str):
3713101Sstever@eecs.umich.edu    cxx_type = 'std::string'
3727673Snate@binkert.org
3737673Snate@binkert.org    @classmethod
3747673Snate@binkert.org    def cxx_predecls(self, code):
3757673Snate@binkert.org        code('#include <string>')
3767673Snate@binkert.org
3777673Snate@binkert.org    @classmethod
3787673Snate@binkert.org    def swig_predecls(cls, code):
3797673Snate@binkert.org        code('%include "std_string.i"')
3804762Snate@binkert.org
3814762Snate@binkert.org    def getValue(self):
3824762Snate@binkert.org        return self
3833101Sstever@eecs.umich.edu
3843101Sstever@eecs.umich.edu# superclass for "numeric" parameter values, to emulate math
3853101Sstever@eecs.umich.edu# operations in a type-safe way.  e.g., a Latency times an int returns
3863101Sstever@eecs.umich.edu# a new Latency object.
3873101Sstever@eecs.umich.educlass NumericParamValue(ParamValue):
3883101Sstever@eecs.umich.edu    def __str__(self):
3893101Sstever@eecs.umich.edu        return str(self.value)
3903101Sstever@eecs.umich.edu
3913101Sstever@eecs.umich.edu    def __float__(self):
3923101Sstever@eecs.umich.edu        return float(self.value)
3933101Sstever@eecs.umich.edu
3943714Sstever@eecs.umich.edu    def __long__(self):
3953714Sstever@eecs.umich.edu        return long(self.value)
3963714Sstever@eecs.umich.edu
3973714Sstever@eecs.umich.edu    def __int__(self):
3983714Sstever@eecs.umich.edu        return int(self.value)
3993714Sstever@eecs.umich.edu
4003101Sstever@eecs.umich.edu    # hook for bounds checking
4013101Sstever@eecs.umich.edu    def _check(self):
4023101Sstever@eecs.umich.edu        return
4033101Sstever@eecs.umich.edu
4043101Sstever@eecs.umich.edu    def __mul__(self, other):
4053101Sstever@eecs.umich.edu        newobj = self.__class__(self)
4063101Sstever@eecs.umich.edu        newobj.value *= other
4073101Sstever@eecs.umich.edu        newobj._check()
4083101Sstever@eecs.umich.edu        return newobj
4093101Sstever@eecs.umich.edu
4103101Sstever@eecs.umich.edu    __rmul__ = __mul__
4113101Sstever@eecs.umich.edu
4123101Sstever@eecs.umich.edu    def __div__(self, other):
4133101Sstever@eecs.umich.edu        newobj = self.__class__(self)
4143101Sstever@eecs.umich.edu        newobj.value /= other
4153101Sstever@eecs.umich.edu        newobj._check()
4163101Sstever@eecs.umich.edu        return newobj
4173101Sstever@eecs.umich.edu
4183101Sstever@eecs.umich.edu    def __sub__(self, other):
4193101Sstever@eecs.umich.edu        newobj = self.__class__(self)
4203101Sstever@eecs.umich.edu        newobj.value -= other
4213101Sstever@eecs.umich.edu        newobj._check()
4223101Sstever@eecs.umich.edu        return newobj
4233101Sstever@eecs.umich.edu
4243101Sstever@eecs.umich.edu# Metaclass for bounds-checked integer parameters.  See CheckedInt.
4255033Smilesck@eecs.umich.educlass CheckedIntType(MetaParamValue):
4263101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
4273101Sstever@eecs.umich.edu        super(CheckedIntType, cls).__init__(name, bases, dict)
4283101Sstever@eecs.umich.edu
4293101Sstever@eecs.umich.edu        # CheckedInt is an abstract base class, so we actually don't
4303101Sstever@eecs.umich.edu        # want to do any processing on it... the rest of this code is
4313101Sstever@eecs.umich.edu        # just for classes that derive from CheckedInt.
4323101Sstever@eecs.umich.edu        if name == 'CheckedInt':
4333101Sstever@eecs.umich.edu            return
4343101Sstever@eecs.umich.edu
4353101Sstever@eecs.umich.edu        if not (hasattr(cls, 'min') and hasattr(cls, 'max')):
4363101Sstever@eecs.umich.edu            if not (hasattr(cls, 'size') and hasattr(cls, 'unsigned')):
4373101Sstever@eecs.umich.edu                panic("CheckedInt subclass %s must define either\n" \
4385822Ssaidi@eecs.umich.edu                      "    'min' and 'max' or 'size' and 'unsigned'\n",
4395822Ssaidi@eecs.umich.edu                      name);
4403101Sstever@eecs.umich.edu            if cls.unsigned:
4413101Sstever@eecs.umich.edu                cls.min = 0
4423101Sstever@eecs.umich.edu                cls.max = 2 ** cls.size - 1
4433101Sstever@eecs.umich.edu            else:
4443101Sstever@eecs.umich.edu                cls.min = -(2 ** (cls.size - 1))
4453101Sstever@eecs.umich.edu                cls.max = (2 ** (cls.size - 1)) - 1
4463101Sstever@eecs.umich.edu
4473101Sstever@eecs.umich.edu# Abstract superclass for bounds-checked integer parameters.  This
4483101Sstever@eecs.umich.edu# class is subclassed to generate parameter classes with specific
4493101Sstever@eecs.umich.edu# bounds.  Initialization of the min and max bounds is done in the
4503101Sstever@eecs.umich.edu# metaclass CheckedIntType.__init__.
4513101Sstever@eecs.umich.educlass CheckedInt(NumericParamValue):
4523101Sstever@eecs.umich.edu    __metaclass__ = CheckedIntType
4533101Sstever@eecs.umich.edu
4543101Sstever@eecs.umich.edu    def _check(self):
4553101Sstever@eecs.umich.edu        if not self.min <= self.value <= self.max:
4563101Sstever@eecs.umich.edu            raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
4573101Sstever@eecs.umich.edu                  (self.min, self.value, self.max)
4583101Sstever@eecs.umich.edu
4593101Sstever@eecs.umich.edu    def __init__(self, value):
4603101Sstever@eecs.umich.edu        if isinstance(value, str):
4613102Sstever@eecs.umich.edu            self.value = convert.toInteger(value)
4623714Sstever@eecs.umich.edu        elif isinstance(value, (int, long, float, NumericParamValue)):
4633101Sstever@eecs.umich.edu            self.value = long(value)
4643714Sstever@eecs.umich.edu        else:
4653714Sstever@eecs.umich.edu            raise TypeError, "Can't convert object of type %s to CheckedInt" \
4663714Sstever@eecs.umich.edu                  % type(value).__name__
4673101Sstever@eecs.umich.edu        self._check()
4683101Sstever@eecs.umich.edu
4697673Snate@binkert.org    @classmethod
4707673Snate@binkert.org    def cxx_predecls(cls, code):
4717673Snate@binkert.org        # most derived types require this, so we just do it here once
4727673Snate@binkert.org        code('#include "base/types.hh"')
4737673Snate@binkert.org
4747673Snate@binkert.org    @classmethod
4757673Snate@binkert.org    def swig_predecls(cls, code):
4767673Snate@binkert.org        # most derived types require this, so we just do it here once
4777673Snate@binkert.org        code('%import "stdint.i"')
4787673Snate@binkert.org        code('%import "base/types.hh"')
4797673Snate@binkert.org
4804762Snate@binkert.org    def getValue(self):
4814762Snate@binkert.org        return long(self.value)
4824762Snate@binkert.org
4833101Sstever@eecs.umich.educlass Int(CheckedInt):      cxx_type = 'int';      size = 32; unsigned = False
4843101Sstever@eecs.umich.educlass Unsigned(CheckedInt): cxx_type = 'unsigned'; size = 32; unsigned = True
4853101Sstever@eecs.umich.edu
4863101Sstever@eecs.umich.educlass Int8(CheckedInt):     cxx_type =   'int8_t'; size =  8; unsigned = False
4873101Sstever@eecs.umich.educlass UInt8(CheckedInt):    cxx_type =  'uint8_t'; size =  8; unsigned = True
4883101Sstever@eecs.umich.educlass Int16(CheckedInt):    cxx_type =  'int16_t'; size = 16; unsigned = False
4893101Sstever@eecs.umich.educlass UInt16(CheckedInt):   cxx_type = 'uint16_t'; size = 16; unsigned = True
4903101Sstever@eecs.umich.educlass Int32(CheckedInt):    cxx_type =  'int32_t'; size = 32; unsigned = False
4913101Sstever@eecs.umich.educlass UInt32(CheckedInt):   cxx_type = 'uint32_t'; size = 32; unsigned = True
4923101Sstever@eecs.umich.educlass Int64(CheckedInt):    cxx_type =  'int64_t'; size = 64; unsigned = False
4933101Sstever@eecs.umich.educlass UInt64(CheckedInt):   cxx_type = 'uint64_t'; size = 64; unsigned = True
4943101Sstever@eecs.umich.edu
4953101Sstever@eecs.umich.educlass Counter(CheckedInt):  cxx_type = 'Counter';  size = 64; unsigned = True
4963101Sstever@eecs.umich.educlass Tick(CheckedInt):     cxx_type = 'Tick';     size = 64; unsigned = True
4973101Sstever@eecs.umich.educlass TcpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
4983101Sstever@eecs.umich.educlass UdpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
4993101Sstever@eecs.umich.edu
5003101Sstever@eecs.umich.educlass Percent(CheckedInt):  cxx_type = 'int'; min = 0; max = 100
5013101Sstever@eecs.umich.edu
5029184Sandreas.hansson@arm.comclass Cycles(CheckedInt):
5039184Sandreas.hansson@arm.com    cxx_type = 'Cycles'
5049184Sandreas.hansson@arm.com    size = 64
5059184Sandreas.hansson@arm.com    unsigned = True
5069184Sandreas.hansson@arm.com
5079184Sandreas.hansson@arm.com    def getValue(self):
5089184Sandreas.hansson@arm.com        from m5.internal.core import Cycles
5099184Sandreas.hansson@arm.com        return Cycles(self.value)
5109184Sandreas.hansson@arm.com
5113101Sstever@eecs.umich.educlass Float(ParamValue, float):
5124446Sbinkertn@umich.edu    cxx_type = 'double'
5133101Sstever@eecs.umich.edu
5145468Snate@binkert.org    def __init__(self, value):
5155468Snate@binkert.org        if isinstance(value, (int, long, float, NumericParamValue, Float)):
5165468Snate@binkert.org            self.value = float(value)
5175468Snate@binkert.org        else:
5185468Snate@binkert.org            raise TypeError, "Can't convert object of type %s to Float" \
5195468Snate@binkert.org                  % type(value).__name__
5205468Snate@binkert.org
5214762Snate@binkert.org    def getValue(self):
5224762Snate@binkert.org        return float(self.value)
5234762Snate@binkert.org
5243101Sstever@eecs.umich.educlass MemorySize(CheckedInt):
5253101Sstever@eecs.umich.edu    cxx_type = 'uint64_t'
5263101Sstever@eecs.umich.edu    size = 64
5273101Sstever@eecs.umich.edu    unsigned = True
5283101Sstever@eecs.umich.edu    def __init__(self, value):
5293101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
5303101Sstever@eecs.umich.edu            self.value = value.value
5313101Sstever@eecs.umich.edu        else:
5323102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
5333101Sstever@eecs.umich.edu        self._check()
5343101Sstever@eecs.umich.edu
5353101Sstever@eecs.umich.educlass MemorySize32(CheckedInt):
5364168Sbinkertn@umich.edu    cxx_type = 'uint32_t'
5373101Sstever@eecs.umich.edu    size = 32
5383101Sstever@eecs.umich.edu    unsigned = True
5393101Sstever@eecs.umich.edu    def __init__(self, value):
5403101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
5413101Sstever@eecs.umich.edu            self.value = value.value
5423101Sstever@eecs.umich.edu        else:
5433102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
5443101Sstever@eecs.umich.edu        self._check()
5453101Sstever@eecs.umich.edu
5463101Sstever@eecs.umich.educlass Addr(CheckedInt):
5473101Sstever@eecs.umich.edu    cxx_type = 'Addr'
5483101Sstever@eecs.umich.edu    size = 64
5493101Sstever@eecs.umich.edu    unsigned = True
5503101Sstever@eecs.umich.edu    def __init__(self, value):
5513101Sstever@eecs.umich.edu        if isinstance(value, Addr):
5523101Sstever@eecs.umich.edu            self.value = value.value
5533101Sstever@eecs.umich.edu        else:
5543101Sstever@eecs.umich.edu            try:
5553102Sstever@eecs.umich.edu                self.value = convert.toMemorySize(value)
5563101Sstever@eecs.umich.edu            except TypeError:
5573101Sstever@eecs.umich.edu                self.value = long(value)
5583101Sstever@eecs.umich.edu        self._check()
5593584Ssaidi@eecs.umich.edu    def __add__(self, other):
5603584Ssaidi@eecs.umich.edu        if isinstance(other, Addr):
5613584Ssaidi@eecs.umich.edu            return self.value + other.value
5623584Ssaidi@eecs.umich.edu        else:
5633584Ssaidi@eecs.umich.edu            return self.value + other
5643101Sstever@eecs.umich.edu
5659232Sandreas.hansson@arm.comclass AddrRange(ParamValue):
5669235Sandreas.hansson@arm.com    cxx_type = 'AddrRange'
5673101Sstever@eecs.umich.edu
5683101Sstever@eecs.umich.edu    def __init__(self, *args, **kwargs):
5699411Sandreas.hansson@arm.com        # Disable interleaving by default
5709411Sandreas.hansson@arm.com        self.intlvHighBit = 0
5719411Sandreas.hansson@arm.com        self.intlvBits = 0
5729411Sandreas.hansson@arm.com        self.intlvMatch = 0
5739411Sandreas.hansson@arm.com
5743101Sstever@eecs.umich.edu        def handle_kwargs(self, kwargs):
5759411Sandreas.hansson@arm.com            # An address range needs to have an upper limit, specified
5769411Sandreas.hansson@arm.com            # either explicitly with an end, or as an offset using the
5779411Sandreas.hansson@arm.com            # size keyword.
5783101Sstever@eecs.umich.edu            if 'end' in kwargs:
5799232Sandreas.hansson@arm.com                self.end = Addr(kwargs.pop('end'))
5803101Sstever@eecs.umich.edu            elif 'size' in kwargs:
5819232Sandreas.hansson@arm.com                self.end = self.start + Addr(kwargs.pop('size')) - 1
5823101Sstever@eecs.umich.edu            else:
5833101Sstever@eecs.umich.edu                raise TypeError, "Either end or size must be specified"
5843101Sstever@eecs.umich.edu
5859411Sandreas.hansson@arm.com            # Now on to the optional bit
5869411Sandreas.hansson@arm.com            if 'intlvHighBit' in kwargs:
5879411Sandreas.hansson@arm.com                self.intlvHighBit = int(kwargs.pop('intlvHighBit'))
5889411Sandreas.hansson@arm.com            if 'intlvBits' in kwargs:
5899411Sandreas.hansson@arm.com                self.intlvBits = int(kwargs.pop('intlvBits'))
5909411Sandreas.hansson@arm.com            if 'intlvMatch' in kwargs:
5919411Sandreas.hansson@arm.com                self.intlvMatch = int(kwargs.pop('intlvMatch'))
5929411Sandreas.hansson@arm.com
5933101Sstever@eecs.umich.edu        if len(args) == 0:
5949232Sandreas.hansson@arm.com            self.start = Addr(kwargs.pop('start'))
5953101Sstever@eecs.umich.edu            handle_kwargs(self, kwargs)
5963101Sstever@eecs.umich.edu
5973101Sstever@eecs.umich.edu        elif len(args) == 1:
5983101Sstever@eecs.umich.edu            if kwargs:
5999232Sandreas.hansson@arm.com                self.start = Addr(args[0])
6003101Sstever@eecs.umich.edu                handle_kwargs(self, kwargs)
6015219Ssaidi@eecs.umich.edu            elif isinstance(args[0], (list, tuple)):
6029232Sandreas.hansson@arm.com                self.start = Addr(args[0][0])
6039232Sandreas.hansson@arm.com                self.end = Addr(args[0][1])
6043101Sstever@eecs.umich.edu            else:
6059232Sandreas.hansson@arm.com                self.start = Addr(0)
6069232Sandreas.hansson@arm.com                self.end = Addr(args[0]) - 1
6073101Sstever@eecs.umich.edu
6083101Sstever@eecs.umich.edu        elif len(args) == 2:
6099232Sandreas.hansson@arm.com            self.start = Addr(args[0])
6109232Sandreas.hansson@arm.com            self.end = Addr(args[1])
6113101Sstever@eecs.umich.edu        else:
6123101Sstever@eecs.umich.edu            raise TypeError, "Too many arguments specified"
6133101Sstever@eecs.umich.edu
6143101Sstever@eecs.umich.edu        if kwargs:
6159232Sandreas.hansson@arm.com            raise TypeError, "Too many keywords: %s" % kwargs.keys()
6163101Sstever@eecs.umich.edu
6173101Sstever@eecs.umich.edu    def __str__(self):
6189232Sandreas.hansson@arm.com        return '%s:%s' % (self.start, self.end)
6199232Sandreas.hansson@arm.com
6209232Sandreas.hansson@arm.com    def size(self):
6219411Sandreas.hansson@arm.com        # Divide the size by the size of the interleaving slice
6229411Sandreas.hansson@arm.com        return (long(self.end) - long(self.start) + 1) >> self.intlvBits
6233101Sstever@eecs.umich.edu
6247673Snate@binkert.org    @classmethod
6257673Snate@binkert.org    def cxx_predecls(cls, code):
6269232Sandreas.hansson@arm.com        Addr.cxx_predecls(code)
6279235Sandreas.hansson@arm.com        code('#include "base/addr_range.hh"')
6287675Snate@binkert.org
6297675Snate@binkert.org    @classmethod
6307675Snate@binkert.org    def swig_predecls(cls, code):
6319232Sandreas.hansson@arm.com        Addr.swig_predecls(code)
6327673Snate@binkert.org
6334762Snate@binkert.org    def getValue(self):
6349235Sandreas.hansson@arm.com        # Go from the Python class to the wrapped C++ class generated
6359235Sandreas.hansson@arm.com        # by swig
6367675Snate@binkert.org        from m5.internal.range import AddrRange
6374762Snate@binkert.org
6389411Sandreas.hansson@arm.com        return AddrRange(long(self.start), long(self.end),
6399411Sandreas.hansson@arm.com                         int(self.intlvHighBit), int(self.intlvBits),
6409411Sandreas.hansson@arm.com                         int(self.intlvMatch))
6413101Sstever@eecs.umich.edu
6423101Sstever@eecs.umich.edu# Boolean parameter type.  Python doesn't let you subclass bool, since
6433101Sstever@eecs.umich.edu# it doesn't want to let you create multiple instances of True and
6443101Sstever@eecs.umich.edu# False.  Thus this is a little more complicated than String.
6453101Sstever@eecs.umich.educlass Bool(ParamValue):
6463101Sstever@eecs.umich.edu    cxx_type = 'bool'
6473101Sstever@eecs.umich.edu    def __init__(self, value):
6483101Sstever@eecs.umich.edu        try:
6493102Sstever@eecs.umich.edu            self.value = convert.toBool(value)
6503101Sstever@eecs.umich.edu        except TypeError:
6513101Sstever@eecs.umich.edu            self.value = bool(value)
6523101Sstever@eecs.umich.edu
6534762Snate@binkert.org    def getValue(self):
6544762Snate@binkert.org        return bool(self.value)
6554762Snate@binkert.org
6563101Sstever@eecs.umich.edu    def __str__(self):
6573101Sstever@eecs.umich.edu        return str(self.value)
6583101Sstever@eecs.umich.edu
6598934SBrad.Beckmann@amd.com    # implement truth value testing for Bool parameters so that these params
6608934SBrad.Beckmann@amd.com    # evaluate correctly during the python configuration phase
6618934SBrad.Beckmann@amd.com    def __nonzero__(self):
6628934SBrad.Beckmann@amd.com        return bool(self.value)
6638934SBrad.Beckmann@amd.com
6643101Sstever@eecs.umich.edu    def ini_str(self):
6653101Sstever@eecs.umich.edu        if self.value:
6663101Sstever@eecs.umich.edu            return 'true'
6673101Sstever@eecs.umich.edu        return 'false'
6683101Sstever@eecs.umich.edu
6693101Sstever@eecs.umich.edudef IncEthernetAddr(addr, val = 1):
6703101Sstever@eecs.umich.edu    bytes = map(lambda x: int(x, 16), addr.split(':'))
6713101Sstever@eecs.umich.edu    bytes[5] += val
6723101Sstever@eecs.umich.edu    for i in (5, 4, 3, 2, 1):
6733101Sstever@eecs.umich.edu        val,rem = divmod(bytes[i], 256)
6743101Sstever@eecs.umich.edu        bytes[i] = rem
6753101Sstever@eecs.umich.edu        if val == 0:
6763101Sstever@eecs.umich.edu            break
6773101Sstever@eecs.umich.edu        bytes[i - 1] += val
6783101Sstever@eecs.umich.edu    assert(bytes[0] <= 255)
6793101Sstever@eecs.umich.edu    return ':'.join(map(lambda x: '%02x' % x, bytes))
6803101Sstever@eecs.umich.edu
6814380Sbinkertn@umich.edu_NextEthernetAddr = "00:90:00:00:00:01"
6824380Sbinkertn@umich.edudef NextEthernetAddr():
6834380Sbinkertn@umich.edu    global _NextEthernetAddr
6843101Sstever@eecs.umich.edu
6854380Sbinkertn@umich.edu    value = _NextEthernetAddr
6864380Sbinkertn@umich.edu    _NextEthernetAddr = IncEthernetAddr(_NextEthernetAddr, 1)
6874380Sbinkertn@umich.edu    return value
6883101Sstever@eecs.umich.edu
6893101Sstever@eecs.umich.educlass EthernetAddr(ParamValue):
6903101Sstever@eecs.umich.edu    cxx_type = 'Net::EthAddr'
6917673Snate@binkert.org
6927673Snate@binkert.org    @classmethod
6937673Snate@binkert.org    def cxx_predecls(cls, code):
6947673Snate@binkert.org        code('#include "base/inet.hh"')
6957673Snate@binkert.org
6967673Snate@binkert.org    @classmethod
6977673Snate@binkert.org    def swig_predecls(cls, code):
6987673Snate@binkert.org        code('%include "python/swig/inet.i"')
6997673Snate@binkert.org
7003101Sstever@eecs.umich.edu    def __init__(self, value):
7013101Sstever@eecs.umich.edu        if value == NextEthernetAddr:
7023101Sstever@eecs.umich.edu            self.value = value
7033101Sstever@eecs.umich.edu            return
7043101Sstever@eecs.umich.edu
7053101Sstever@eecs.umich.edu        if not isinstance(value, str):
7063101Sstever@eecs.umich.edu            raise TypeError, "expected an ethernet address and didn't get one"
7073101Sstever@eecs.umich.edu
7083101Sstever@eecs.umich.edu        bytes = value.split(':')
7093101Sstever@eecs.umich.edu        if len(bytes) != 6:
7103101Sstever@eecs.umich.edu            raise TypeError, 'invalid ethernet address %s' % value
7113101Sstever@eecs.umich.edu
7123101Sstever@eecs.umich.edu        for byte in bytes:
7139941SGeoffrey.Blake@arm.com            if not 0 <= int(byte, base=16) <= 0xff:
7143101Sstever@eecs.umich.edu                raise TypeError, 'invalid ethernet address %s' % value
7153101Sstever@eecs.umich.edu
7163101Sstever@eecs.umich.edu        self.value = value
7173101Sstever@eecs.umich.edu
7183101Sstever@eecs.umich.edu    def unproxy(self, base):
7193101Sstever@eecs.umich.edu        if self.value == NextEthernetAddr:
7204380Sbinkertn@umich.edu            return EthernetAddr(self.value())
7213101Sstever@eecs.umich.edu        return self
7223101Sstever@eecs.umich.edu
7234762Snate@binkert.org    def getValue(self):
7247677Snate@binkert.org        from m5.internal.params import EthAddr
7254762Snate@binkert.org        return EthAddr(self.value)
7264762Snate@binkert.org
7274380Sbinkertn@umich.edu    def ini_str(self):
7284380Sbinkertn@umich.edu        return self.value
7293101Sstever@eecs.umich.edu
7307777Sgblack@eecs.umich.edu# When initializing an IpAddress, pass in an existing IpAddress, a string of
7317777Sgblack@eecs.umich.edu# the form "a.b.c.d", or an integer representing an IP.
7327777Sgblack@eecs.umich.educlass IpAddress(ParamValue):
7337777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpAddress'
7347777Sgblack@eecs.umich.edu
7357777Sgblack@eecs.umich.edu    @classmethod
7367777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
7377777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
7387777Sgblack@eecs.umich.edu
7397777Sgblack@eecs.umich.edu    @classmethod
7407777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
7417777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
7427777Sgblack@eecs.umich.edu
7437777Sgblack@eecs.umich.edu    def __init__(self, value):
7447777Sgblack@eecs.umich.edu        if isinstance(value, IpAddress):
7457777Sgblack@eecs.umich.edu            self.ip = value.ip
7467777Sgblack@eecs.umich.edu        else:
7477777Sgblack@eecs.umich.edu            try:
7487777Sgblack@eecs.umich.edu                self.ip = convert.toIpAddress(value)
7497777Sgblack@eecs.umich.edu            except TypeError:
7507777Sgblack@eecs.umich.edu                self.ip = long(value)
7517777Sgblack@eecs.umich.edu        self.verifyIp()
7527777Sgblack@eecs.umich.edu
7538579Ssteve.reinhardt@amd.com    def __str__(self):
7548579Ssteve.reinhardt@amd.com        tup = [(self.ip >> i)  & 0xff for i in (24, 16, 8, 0)]
7558579Ssteve.reinhardt@amd.com        return '%d.%d.%d.%d' % tuple(tup)
7568579Ssteve.reinhardt@amd.com
7578579Ssteve.reinhardt@amd.com    def __eq__(self, other):
7588579Ssteve.reinhardt@amd.com        if isinstance(other, IpAddress):
7598579Ssteve.reinhardt@amd.com            return self.ip == other.ip
7608579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
7618579Ssteve.reinhardt@amd.com            try:
7628579Ssteve.reinhardt@amd.com                return self.ip == convert.toIpAddress(other)
7638579Ssteve.reinhardt@amd.com            except:
7648579Ssteve.reinhardt@amd.com                return False
7658579Ssteve.reinhardt@amd.com        else:
7668579Ssteve.reinhardt@amd.com            return self.ip == other
7678579Ssteve.reinhardt@amd.com
7688579Ssteve.reinhardt@amd.com    def __ne__(self, other):
7698579Ssteve.reinhardt@amd.com        return not (self == other)
7708579Ssteve.reinhardt@amd.com
7717777Sgblack@eecs.umich.edu    def verifyIp(self):
7727777Sgblack@eecs.umich.edu        if self.ip < 0 or self.ip >= (1 << 32):
7737798Sgblack@eecs.umich.edu            raise TypeError, "invalid ip address %#08x" % self.ip
7747777Sgblack@eecs.umich.edu
7757777Sgblack@eecs.umich.edu    def getValue(self):
7767777Sgblack@eecs.umich.edu        from m5.internal.params import IpAddress
7777777Sgblack@eecs.umich.edu        return IpAddress(self.ip)
7787777Sgblack@eecs.umich.edu
7797777Sgblack@eecs.umich.edu# When initializing an IpNetmask, pass in an existing IpNetmask, a string of
7807777Sgblack@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
7817777Sgblack@eecs.umich.edu# positional or keyword arguments.
7827777Sgblack@eecs.umich.educlass IpNetmask(IpAddress):
7837777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpNetmask'
7847777Sgblack@eecs.umich.edu
7857777Sgblack@eecs.umich.edu    @classmethod
7867777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
7877777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
7887777Sgblack@eecs.umich.edu
7897777Sgblack@eecs.umich.edu    @classmethod
7907777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
7917777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
7927777Sgblack@eecs.umich.edu
7937777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
7947777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
7957777Sgblack@eecs.umich.edu            if key in kwargs:
7967777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
7977777Sgblack@eecs.umich.edu            elif elseVal:
7987777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
7997777Sgblack@eecs.umich.edu            else:
8007777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
8017777Sgblack@eecs.umich.edu
8027777Sgblack@eecs.umich.edu        if len(args) == 0:
8037777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
8047777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'netmask')
8057777Sgblack@eecs.umich.edu
8067777Sgblack@eecs.umich.edu        elif len(args) == 1:
8077777Sgblack@eecs.umich.edu            if kwargs:
8087777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'netmask' in kwargs:
8097777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
8107777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
8117777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'netmask', args[0])
8127777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpNetmask):
8137777Sgblack@eecs.umich.edu                self.ip = args[0].ip
8147777Sgblack@eecs.umich.edu                self.netmask = args[0].netmask
8157777Sgblack@eecs.umich.edu            else:
8167777Sgblack@eecs.umich.edu                (self.ip, self.netmask) = convert.toIpNetmask(args[0])
8177777Sgblack@eecs.umich.edu
8187777Sgblack@eecs.umich.edu        elif len(args) == 2:
8197777Sgblack@eecs.umich.edu            self.ip = args[0]
8207777Sgblack@eecs.umich.edu            self.netmask = args[1]
8217777Sgblack@eecs.umich.edu        else:
8227777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
8237777Sgblack@eecs.umich.edu
8247777Sgblack@eecs.umich.edu        if kwargs:
8257777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
8267777Sgblack@eecs.umich.edu
8277777Sgblack@eecs.umich.edu        self.verify()
8287777Sgblack@eecs.umich.edu
8298579Ssteve.reinhardt@amd.com    def __str__(self):
8308579Ssteve.reinhardt@amd.com        return "%s/%d" % (super(IpNetmask, self).__str__(), self.netmask)
8318579Ssteve.reinhardt@amd.com
8328579Ssteve.reinhardt@amd.com    def __eq__(self, other):
8338579Ssteve.reinhardt@amd.com        if isinstance(other, IpNetmask):
8348579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.netmask == other.netmask
8358579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
8368579Ssteve.reinhardt@amd.com            try:
8378579Ssteve.reinhardt@amd.com                return (self.ip, self.netmask) == convert.toIpNetmask(other)
8388579Ssteve.reinhardt@amd.com            except:
8398579Ssteve.reinhardt@amd.com                return False
8408579Ssteve.reinhardt@amd.com        else:
8418579Ssteve.reinhardt@amd.com            return False
8428579Ssteve.reinhardt@amd.com
8437777Sgblack@eecs.umich.edu    def verify(self):
8447777Sgblack@eecs.umich.edu        self.verifyIp()
8457777Sgblack@eecs.umich.edu        if self.netmask < 0 or self.netmask > 32:
8467777Sgblack@eecs.umich.edu            raise TypeError, "invalid netmask %d" % netmask
8477777Sgblack@eecs.umich.edu
8487777Sgblack@eecs.umich.edu    def getValue(self):
8497777Sgblack@eecs.umich.edu        from m5.internal.params import IpNetmask
8507777Sgblack@eecs.umich.edu        return IpNetmask(self.ip, self.netmask)
8517777Sgblack@eecs.umich.edu
8527777Sgblack@eecs.umich.edu# When initializing an IpWithPort, pass in an existing IpWithPort, a string of
8537777Sgblack@eecs.umich.edu# the form "a.b.c.d:p", or an ip and port as positional or keyword arguments.
8547777Sgblack@eecs.umich.educlass IpWithPort(IpAddress):
8557777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpWithPort'
8567777Sgblack@eecs.umich.edu
8577777Sgblack@eecs.umich.edu    @classmethod
8587777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
8597777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
8607777Sgblack@eecs.umich.edu
8617777Sgblack@eecs.umich.edu    @classmethod
8627777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
8637777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
8647777Sgblack@eecs.umich.edu
8657777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
8667777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
8677777Sgblack@eecs.umich.edu            if key in kwargs:
8687777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
8697777Sgblack@eecs.umich.edu            elif elseVal:
8707777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
8717777Sgblack@eecs.umich.edu            else:
8727777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
8737777Sgblack@eecs.umich.edu
8747777Sgblack@eecs.umich.edu        if len(args) == 0:
8757777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
8767777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'port')
8777777Sgblack@eecs.umich.edu
8787777Sgblack@eecs.umich.edu        elif len(args) == 1:
8797777Sgblack@eecs.umich.edu            if kwargs:
8807777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'port' in kwargs:
8817777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
8827777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
8837777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'port', args[0])
8847777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpWithPort):
8857777Sgblack@eecs.umich.edu                self.ip = args[0].ip
8867777Sgblack@eecs.umich.edu                self.port = args[0].port
8877777Sgblack@eecs.umich.edu            else:
8887777Sgblack@eecs.umich.edu                (self.ip, self.port) = convert.toIpWithPort(args[0])
8897777Sgblack@eecs.umich.edu
8907777Sgblack@eecs.umich.edu        elif len(args) == 2:
8917777Sgblack@eecs.umich.edu            self.ip = args[0]
8927777Sgblack@eecs.umich.edu            self.port = args[1]
8937777Sgblack@eecs.umich.edu        else:
8947777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
8957777Sgblack@eecs.umich.edu
8967777Sgblack@eecs.umich.edu        if kwargs:
8977777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
8987777Sgblack@eecs.umich.edu
8997777Sgblack@eecs.umich.edu        self.verify()
9007777Sgblack@eecs.umich.edu
9018579Ssteve.reinhardt@amd.com    def __str__(self):
9028579Ssteve.reinhardt@amd.com        return "%s:%d" % (super(IpWithPort, self).__str__(), self.port)
9038579Ssteve.reinhardt@amd.com
9048579Ssteve.reinhardt@amd.com    def __eq__(self, other):
9058579Ssteve.reinhardt@amd.com        if isinstance(other, IpWithPort):
9068579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.port == other.port
9078579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
9088579Ssteve.reinhardt@amd.com            try:
9098579Ssteve.reinhardt@amd.com                return (self.ip, self.port) == convert.toIpWithPort(other)
9108579Ssteve.reinhardt@amd.com            except:
9118579Ssteve.reinhardt@amd.com                return False
9128579Ssteve.reinhardt@amd.com        else:
9138579Ssteve.reinhardt@amd.com            return False
9148579Ssteve.reinhardt@amd.com
9157777Sgblack@eecs.umich.edu    def verify(self):
9167777Sgblack@eecs.umich.edu        self.verifyIp()
9177777Sgblack@eecs.umich.edu        if self.port < 0 or self.port > 0xffff:
9187777Sgblack@eecs.umich.edu            raise TypeError, "invalid port %d" % self.port
9197777Sgblack@eecs.umich.edu
9207777Sgblack@eecs.umich.edu    def getValue(self):
9217777Sgblack@eecs.umich.edu        from m5.internal.params import IpWithPort
9227777Sgblack@eecs.umich.edu        return IpWithPort(self.ip, self.port)
9237777Sgblack@eecs.umich.edu
9243932Sbinkertn@umich.edutime_formats = [ "%a %b %d %H:%M:%S %Z %Y",
9253932Sbinkertn@umich.edu                 "%a %b %d %H:%M:%S %Z %Y",
9263932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M:%S",
9273932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M",
9283932Sbinkertn@umich.edu                 "%Y/%m/%d",
9293932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M:%S",
9303932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M",
9313932Sbinkertn@umich.edu                 "%m/%d/%Y",
9323932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M:%S",
9333932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M",
9343932Sbinkertn@umich.edu                 "%m/%d/%y"]
9353932Sbinkertn@umich.edu
9363932Sbinkertn@umich.edu
9373885Sbinkertn@umich.edudef parse_time(value):
9383932Sbinkertn@umich.edu    from time import gmtime, strptime, struct_time, time
9393932Sbinkertn@umich.edu    from datetime import datetime, date
9403885Sbinkertn@umich.edu
9413932Sbinkertn@umich.edu    if isinstance(value, struct_time):
9423932Sbinkertn@umich.edu        return value
9433932Sbinkertn@umich.edu
9443932Sbinkertn@umich.edu    if isinstance(value, (int, long)):
9453932Sbinkertn@umich.edu        return gmtime(value)
9463932Sbinkertn@umich.edu
9473932Sbinkertn@umich.edu    if isinstance(value, (datetime, date)):
9483932Sbinkertn@umich.edu        return value.timetuple()
9493932Sbinkertn@umich.edu
9503932Sbinkertn@umich.edu    if isinstance(value, str):
9513932Sbinkertn@umich.edu        if value in ('Now', 'Today'):
9523932Sbinkertn@umich.edu            return time.gmtime(time.time())
9533932Sbinkertn@umich.edu
9543932Sbinkertn@umich.edu        for format in time_formats:
9553932Sbinkertn@umich.edu            try:
9563932Sbinkertn@umich.edu                return strptime(value, format)
9573932Sbinkertn@umich.edu            except ValueError:
9583932Sbinkertn@umich.edu                pass
9593885Sbinkertn@umich.edu
9603885Sbinkertn@umich.edu    raise ValueError, "Could not parse '%s' as a time" % value
9613885Sbinkertn@umich.edu
9623885Sbinkertn@umich.educlass Time(ParamValue):
9634762Snate@binkert.org    cxx_type = 'tm'
9647673Snate@binkert.org
9657673Snate@binkert.org    @classmethod
9667673Snate@binkert.org    def cxx_predecls(cls, code):
9677673Snate@binkert.org        code('#include <time.h>')
9687673Snate@binkert.org
9697673Snate@binkert.org    @classmethod
9707673Snate@binkert.org    def swig_predecls(cls, code):
9717673Snate@binkert.org        code('%include "python/swig/time.i"')
9727673Snate@binkert.org
9733885Sbinkertn@umich.edu    def __init__(self, value):
9743932Sbinkertn@umich.edu        self.value = parse_time(value)
9753885Sbinkertn@umich.edu
9764762Snate@binkert.org    def getValue(self):
9777677Snate@binkert.org        from m5.internal.params import tm
9784762Snate@binkert.org
9794762Snate@binkert.org        c_time = tm()
9804762Snate@binkert.org        py_time = self.value
9814762Snate@binkert.org
9824762Snate@binkert.org        # UNIX is years since 1900
9834762Snate@binkert.org        c_time.tm_year = py_time.tm_year - 1900;
9844762Snate@binkert.org
9854762Snate@binkert.org        # Python starts at 1, UNIX starts at 0
9864762Snate@binkert.org        c_time.tm_mon =  py_time.tm_mon - 1;
9874762Snate@binkert.org        c_time.tm_mday = py_time.tm_mday;
9884762Snate@binkert.org        c_time.tm_hour = py_time.tm_hour;
9894762Snate@binkert.org        c_time.tm_min = py_time.tm_min;
9904762Snate@binkert.org        c_time.tm_sec = py_time.tm_sec;
9914762Snate@binkert.org
9924762Snate@binkert.org        # Python has 0 as Monday, UNIX is 0 as sunday
9934762Snate@binkert.org        c_time.tm_wday = py_time.tm_wday + 1
9944762Snate@binkert.org        if c_time.tm_wday > 6:
9954762Snate@binkert.org            c_time.tm_wday -= 7;
9964762Snate@binkert.org
9974762Snate@binkert.org        # Python starts at 1, Unix starts at 0
9984762Snate@binkert.org        c_time.tm_yday = py_time.tm_yday - 1;
9994762Snate@binkert.org
10004762Snate@binkert.org        return c_time
10014762Snate@binkert.org
10023885Sbinkertn@umich.edu    def __str__(self):
10034762Snate@binkert.org        return time.asctime(self.value)
10043885Sbinkertn@umich.edu
10053885Sbinkertn@umich.edu    def ini_str(self):
10063932Sbinkertn@umich.edu        return str(self)
10073885Sbinkertn@umich.edu
10088664SAli.Saidi@ARM.com    def get_config_as_dict(self):
10098664SAli.Saidi@ARM.com        return str(self)
10108664SAli.Saidi@ARM.com
10113101Sstever@eecs.umich.edu# Enumerated types are a little more complex.  The user specifies the
10123101Sstever@eecs.umich.edu# type as Enum(foo) where foo is either a list or dictionary of
10133101Sstever@eecs.umich.edu# alternatives (typically strings, but not necessarily so).  (In the
10143101Sstever@eecs.umich.edu# long run, the integer value of the parameter will be the list index
10153101Sstever@eecs.umich.edu# or the corresponding dictionary value.  For now, since we only check
10163101Sstever@eecs.umich.edu# that the alternative is valid and then spit it into a .ini file,
10173101Sstever@eecs.umich.edu# there's not much point in using the dictionary.)
10183101Sstever@eecs.umich.edu
10193101Sstever@eecs.umich.edu# What Enum() must do is generate a new type encapsulating the
10203101Sstever@eecs.umich.edu# provided list/dictionary so that specific values of the parameter
10213101Sstever@eecs.umich.edu# can be instances of that type.  We define two hidden internal
10223101Sstever@eecs.umich.edu# classes (_ListEnum and _DictEnum) to serve as base classes, then
10233101Sstever@eecs.umich.edu# derive the new type from the appropriate base class on the fly.
10243101Sstever@eecs.umich.edu
10254762Snate@binkert.orgallEnums = {}
10263101Sstever@eecs.umich.edu# Metaclass for Enum types
10275033Smilesck@eecs.umich.educlass MetaEnum(MetaParamValue):
10284762Snate@binkert.org    def __new__(mcls, name, bases, dict):
10294762Snate@binkert.org        assert name not in allEnums
10304762Snate@binkert.org
10314762Snate@binkert.org        cls = super(MetaEnum, mcls).__new__(mcls, name, bases, dict)
10324762Snate@binkert.org        allEnums[name] = cls
10334762Snate@binkert.org        return cls
10344762Snate@binkert.org
10353101Sstever@eecs.umich.edu    def __init__(cls, name, bases, init_dict):
10363101Sstever@eecs.umich.edu        if init_dict.has_key('map'):
10373101Sstever@eecs.umich.edu            if not isinstance(cls.map, dict):
10383101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'map' " \
10393101Sstever@eecs.umich.edu                      "must be of type dict"
10403101Sstever@eecs.umich.edu            # build list of value strings from map
10413101Sstever@eecs.umich.edu            cls.vals = cls.map.keys()
10423101Sstever@eecs.umich.edu            cls.vals.sort()
10433101Sstever@eecs.umich.edu        elif init_dict.has_key('vals'):
10443101Sstever@eecs.umich.edu            if not isinstance(cls.vals, list):
10453101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'vals' " \
10463101Sstever@eecs.umich.edu                      "must be of type list"
10473101Sstever@eecs.umich.edu            # build string->value map from vals sequence
10483101Sstever@eecs.umich.edu            cls.map = {}
10493101Sstever@eecs.umich.edu            for idx,val in enumerate(cls.vals):
10503101Sstever@eecs.umich.edu                cls.map[val] = idx
10513101Sstever@eecs.umich.edu        else:
10523101Sstever@eecs.umich.edu            raise TypeError, "Enum-derived class must define "\
10533101Sstever@eecs.umich.edu                  "attribute 'map' or 'vals'"
10543101Sstever@eecs.umich.edu
10554762Snate@binkert.org        cls.cxx_type = 'Enums::%s' % name
10563101Sstever@eecs.umich.edu
10573101Sstever@eecs.umich.edu        super(MetaEnum, cls).__init__(name, bases, init_dict)
10583101Sstever@eecs.umich.edu
10593101Sstever@eecs.umich.edu    # Generate C++ class declaration for this enum type.
10603101Sstever@eecs.umich.edu    # Note that we wrap the enum in a class/struct to act as a namespace,
10613101Sstever@eecs.umich.edu    # so that the enum strings can be brief w/o worrying about collisions.
10627673Snate@binkert.org    def cxx_decl(cls, code):
10636654Snate@binkert.org        name = cls.__name__
10647673Snate@binkert.org        code('''\
10657673Snate@binkert.org#ifndef __ENUM__${name}__
10667673Snate@binkert.org#define __ENUM__${name}__
10677673Snate@binkert.org
10687673Snate@binkert.orgnamespace Enums {
10697673Snate@binkert.org    enum $name {
10707673Snate@binkert.org''')
10717673Snate@binkert.org        code.indent(2)
10724762Snate@binkert.org        for val in cls.vals:
10737673Snate@binkert.org            code('$val = ${{cls.map[val]}},')
10748902Sandreas.hansson@arm.com        code('Num_$name = ${{len(cls.vals)}}')
10757673Snate@binkert.org        code.dedent(2)
10767673Snate@binkert.org        code('''\
10777673Snate@binkert.org    };
10787673Snate@binkert.orgextern const char *${name}Strings[Num_${name}];
10797673Snate@binkert.org}
10804762Snate@binkert.org
10817673Snate@binkert.org#endif // __ENUM__${name}__
10827673Snate@binkert.org''')
10837673Snate@binkert.org
10847673Snate@binkert.org    def cxx_def(cls, code):
10856654Snate@binkert.org        name = cls.__name__
10867673Snate@binkert.org        code('''\
10877677Snate@binkert.org#include "enums/$name.hh"
10887673Snate@binkert.orgnamespace Enums {
10897673Snate@binkert.org    const char *${name}Strings[Num_${name}] =
10907673Snate@binkert.org    {
10917673Snate@binkert.org''')
10927673Snate@binkert.org        code.indent(2)
10934762Snate@binkert.org        for val in cls.vals:
10947673Snate@binkert.org            code('"$val",')
10957673Snate@binkert.org        code.dedent(2)
10967673Snate@binkert.org        code('''
10977673Snate@binkert.org    };
10987811Ssteve.reinhardt@amd.com} // namespace Enums
10997673Snate@binkert.org''')
11003101Sstever@eecs.umich.edu
11018596Ssteve.reinhardt@amd.com    def swig_decl(cls, code):
11028596Ssteve.reinhardt@amd.com        name = cls.__name__
11038596Ssteve.reinhardt@amd.com        code('''\
11048596Ssteve.reinhardt@amd.com%module(package="m5.internal") enum_$name
11058596Ssteve.reinhardt@amd.com
11068596Ssteve.reinhardt@amd.com%{
11078596Ssteve.reinhardt@amd.com#include "enums/$name.hh"
11088596Ssteve.reinhardt@amd.com%}
11098596Ssteve.reinhardt@amd.com
11108596Ssteve.reinhardt@amd.com%include "enums/$name.hh"
11118596Ssteve.reinhardt@amd.com''')
11128596Ssteve.reinhardt@amd.com
11138596Ssteve.reinhardt@amd.com
11143101Sstever@eecs.umich.edu# Base class for enum types.
11153101Sstever@eecs.umich.educlass Enum(ParamValue):
11163101Sstever@eecs.umich.edu    __metaclass__ = MetaEnum
11173101Sstever@eecs.umich.edu    vals = []
11183101Sstever@eecs.umich.edu
11193101Sstever@eecs.umich.edu    def __init__(self, value):
11203101Sstever@eecs.umich.edu        if value not in self.map:
11213101Sstever@eecs.umich.edu            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
11223101Sstever@eecs.umich.edu                  % (value, self.vals)
11233101Sstever@eecs.umich.edu        self.value = value
11243101Sstever@eecs.umich.edu
11257675Snate@binkert.org    @classmethod
11267675Snate@binkert.org    def cxx_predecls(cls, code):
11277675Snate@binkert.org        code('#include "enums/$0.hh"', cls.__name__)
11287675Snate@binkert.org
11297675Snate@binkert.org    @classmethod
11307675Snate@binkert.org    def swig_predecls(cls, code):
11317677Snate@binkert.org        code('%import "python/m5/internal/enum_$0.i"', cls.__name__)
11327675Snate@binkert.org
11334762Snate@binkert.org    def getValue(self):
11344762Snate@binkert.org        return int(self.map[self.value])
11354762Snate@binkert.org
11363101Sstever@eecs.umich.edu    def __str__(self):
11373101Sstever@eecs.umich.edu        return self.value
11383101Sstever@eecs.umich.edu
11393101Sstever@eecs.umich.edu# how big does a rounding error need to be before we warn about it?
11403101Sstever@eecs.umich.edufrequency_tolerance = 0.001  # 0.1%
11413101Sstever@eecs.umich.edu
11424167Sbinkertn@umich.educlass TickParamValue(NumericParamValue):
11433101Sstever@eecs.umich.edu    cxx_type = 'Tick'
11447673Snate@binkert.org
11457673Snate@binkert.org    @classmethod
11467673Snate@binkert.org    def cxx_predecls(cls, code):
11477673Snate@binkert.org        code('#include "base/types.hh"')
11487673Snate@binkert.org
11497673Snate@binkert.org    @classmethod
11507673Snate@binkert.org    def swig_predecls(cls, code):
11517673Snate@binkert.org        code('%import "stdint.i"')
11527673Snate@binkert.org        code('%import "base/types.hh"')
11534167Sbinkertn@umich.edu
11544762Snate@binkert.org    def getValue(self):
11554762Snate@binkert.org        return long(self.value)
11564762Snate@binkert.org
11574167Sbinkertn@umich.educlass Latency(TickParamValue):
11583101Sstever@eecs.umich.edu    def __init__(self, value):
11594167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11604167Sbinkertn@umich.edu            self.ticks = value.ticks
11614167Sbinkertn@umich.edu            self.value = value.value
11624167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11634167Sbinkertn@umich.edu            self.ticks = value.ticks
11644167Sbinkertn@umich.edu            self.value = 1.0 / value.value
11654167Sbinkertn@umich.edu        elif value.endswith('t'):
11664167Sbinkertn@umich.edu            self.ticks = True
11674167Sbinkertn@umich.edu            self.value = int(value[:-1])
11684167Sbinkertn@umich.edu        else:
11694167Sbinkertn@umich.edu            self.ticks = False
11704167Sbinkertn@umich.edu            self.value = convert.toLatency(value)
11713101Sstever@eecs.umich.edu
11723101Sstever@eecs.umich.edu    def __getattr__(self, attr):
11733101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
11743101Sstever@eecs.umich.edu            return self
11753101Sstever@eecs.umich.edu        if attr == 'frequency':
11763101Sstever@eecs.umich.edu            return Frequency(self)
11773101Sstever@eecs.umich.edu        raise AttributeError, "Latency object has no attribute '%s'" % attr
11783101Sstever@eecs.umich.edu
11794762Snate@binkert.org    def getValue(self):
11804762Snate@binkert.org        if self.ticks or self.value == 0:
11814762Snate@binkert.org            value = self.value
11824762Snate@binkert.org        else:
11834762Snate@binkert.org            value = ticks.fromSeconds(self.value)
11844762Snate@binkert.org        return long(value)
11854762Snate@binkert.org
11863101Sstever@eecs.umich.edu    # convert latency to ticks
11873101Sstever@eecs.umich.edu    def ini_str(self):
11884762Snate@binkert.org        return '%d' % self.getValue()
11893101Sstever@eecs.umich.edu
11904167Sbinkertn@umich.educlass Frequency(TickParamValue):
11913101Sstever@eecs.umich.edu    def __init__(self, value):
11924167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11934167Sbinkertn@umich.edu            if value.value == 0:
11944167Sbinkertn@umich.edu                self.value = 0
11954167Sbinkertn@umich.edu            else:
11964167Sbinkertn@umich.edu                self.value = 1.0 / value.value
11974167Sbinkertn@umich.edu            self.ticks = value.ticks
11984167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11994167Sbinkertn@umich.edu            self.value = value.value
12004167Sbinkertn@umich.edu            self.ticks = value.ticks
12014167Sbinkertn@umich.edu        else:
12024167Sbinkertn@umich.edu            self.ticks = False
12034167Sbinkertn@umich.edu            self.value = convert.toFrequency(value)
12043101Sstever@eecs.umich.edu
12053101Sstever@eecs.umich.edu    def __getattr__(self, attr):
12063101Sstever@eecs.umich.edu        if attr == 'frequency':
12073101Sstever@eecs.umich.edu            return self
12083101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
12093101Sstever@eecs.umich.edu            return Latency(self)
12103101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
12113101Sstever@eecs.umich.edu
12124167Sbinkertn@umich.edu    # convert latency to ticks
12134762Snate@binkert.org    def getValue(self):
12144762Snate@binkert.org        if self.ticks or self.value == 0:
12154762Snate@binkert.org            value = self.value
12164762Snate@binkert.org        else:
12174762Snate@binkert.org            value = ticks.fromSeconds(1.0 / self.value)
12184762Snate@binkert.org        return long(value)
12194762Snate@binkert.org
12203101Sstever@eecs.umich.edu    def ini_str(self):
12214762Snate@binkert.org        return '%d' % self.getValue()
12223101Sstever@eecs.umich.edu
122310019Sandreas.hansson@arm.com# A generic Frequency and/or Latency value. Value is stored as a
122410019Sandreas.hansson@arm.com# latency, just like Latency and Frequency.
122510019Sandreas.hansson@arm.comclass Clock(TickParamValue):
12263101Sstever@eecs.umich.edu    def __init__(self, value):
12274167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
12284167Sbinkertn@umich.edu            self.ticks = value.ticks
12294167Sbinkertn@umich.edu            self.value = value.value
12304167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
12314167Sbinkertn@umich.edu            self.ticks = value.ticks
12324167Sbinkertn@umich.edu            self.value = 1.0 / value.value
12334167Sbinkertn@umich.edu        elif value.endswith('t'):
12344167Sbinkertn@umich.edu            self.ticks = True
12354167Sbinkertn@umich.edu            self.value = int(value[:-1])
12364167Sbinkertn@umich.edu        else:
12374167Sbinkertn@umich.edu            self.ticks = False
12384167Sbinkertn@umich.edu            self.value = convert.anyToLatency(value)
12393101Sstever@eecs.umich.edu
12403101Sstever@eecs.umich.edu    def __getattr__(self, attr):
12413101Sstever@eecs.umich.edu        if attr == 'frequency':
12423101Sstever@eecs.umich.edu            return Frequency(self)
12433101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
12443101Sstever@eecs.umich.edu            return Latency(self)
12453101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
12463101Sstever@eecs.umich.edu
12474762Snate@binkert.org    def getValue(self):
12484762Snate@binkert.org        return self.period.getValue()
12494762Snate@binkert.org
12503101Sstever@eecs.umich.edu    def ini_str(self):
12513101Sstever@eecs.umich.edu        return self.period.ini_str()
12523101Sstever@eecs.umich.edu
12539827Sakash.bagdia@arm.comclass Voltage(float,ParamValue):
12549827Sakash.bagdia@arm.com    cxx_type = 'double'
12559827Sakash.bagdia@arm.com    def __new__(cls, value):
12569827Sakash.bagdia@arm.com        # convert to voltage
12579827Sakash.bagdia@arm.com        val = convert.toVoltage(value)
12589827Sakash.bagdia@arm.com        return super(cls, Voltage).__new__(cls, val)
12599827Sakash.bagdia@arm.com
12609827Sakash.bagdia@arm.com    def __str__(self):
12619827Sakash.bagdia@arm.com        return str(self.val)
12629827Sakash.bagdia@arm.com
12639827Sakash.bagdia@arm.com    def getValue(self):
12649827Sakash.bagdia@arm.com        value = float(self)
12659827Sakash.bagdia@arm.com        return value
12669827Sakash.bagdia@arm.com
12679827Sakash.bagdia@arm.com    def ini_str(self):
12689827Sakash.bagdia@arm.com        return '%f' % self.getValue()
12699827Sakash.bagdia@arm.com
12703101Sstever@eecs.umich.educlass NetworkBandwidth(float,ParamValue):
12713101Sstever@eecs.umich.edu    cxx_type = 'float'
12723101Sstever@eecs.umich.edu    def __new__(cls, value):
12734167Sbinkertn@umich.edu        # convert to bits per second
12744167Sbinkertn@umich.edu        val = convert.toNetworkBandwidth(value)
12753101Sstever@eecs.umich.edu        return super(cls, NetworkBandwidth).__new__(cls, val)
12763101Sstever@eecs.umich.edu
12773101Sstever@eecs.umich.edu    def __str__(self):
12783101Sstever@eecs.umich.edu        return str(self.val)
12793101Sstever@eecs.umich.edu
12804762Snate@binkert.org    def getValue(self):
12814167Sbinkertn@umich.edu        # convert to seconds per byte
12824167Sbinkertn@umich.edu        value = 8.0 / float(self)
12834167Sbinkertn@umich.edu        # convert to ticks per byte
12844762Snate@binkert.org        value = ticks.fromSeconds(value)
12854762Snate@binkert.org        return float(value)
12864762Snate@binkert.org
12874762Snate@binkert.org    def ini_str(self):
12884762Snate@binkert.org        return '%f' % self.getValue()
12893101Sstever@eecs.umich.edu
12903101Sstever@eecs.umich.educlass MemoryBandwidth(float,ParamValue):
12913101Sstever@eecs.umich.edu    cxx_type = 'float'
12925469Snate@binkert.org    def __new__(cls, value):
12937743Sgblack@eecs.umich.edu        # convert to bytes per second
12943102Sstever@eecs.umich.edu        val = convert.toMemoryBandwidth(value)
12953101Sstever@eecs.umich.edu        return super(cls, MemoryBandwidth).__new__(cls, val)
12963101Sstever@eecs.umich.edu
12973101Sstever@eecs.umich.edu    def __str__(self):
12983101Sstever@eecs.umich.edu        return str(self.val)
12993101Sstever@eecs.umich.edu
13004762Snate@binkert.org    def getValue(self):
13014167Sbinkertn@umich.edu        # convert to seconds per byte
13025468Snate@binkert.org        value = float(self)
13035468Snate@binkert.org        if value:
13045468Snate@binkert.org            value = 1.0 / float(self)
13054167Sbinkertn@umich.edu        # convert to ticks per byte
13064762Snate@binkert.org        value = ticks.fromSeconds(value)
13074762Snate@binkert.org        return float(value)
13084762Snate@binkert.org
13094762Snate@binkert.org    def ini_str(self):
13104762Snate@binkert.org        return '%f' % self.getValue()
13113101Sstever@eecs.umich.edu
13123101Sstever@eecs.umich.edu#
13133101Sstever@eecs.umich.edu# "Constants"... handy aliases for various values.
13143101Sstever@eecs.umich.edu#
13153101Sstever@eecs.umich.edu
13163102Sstever@eecs.umich.edu# Special class for NULL pointers.  Note the special check in
13173102Sstever@eecs.umich.edu# make_param_value() above that lets these be assigned where a
13183102Sstever@eecs.umich.edu# SimObject is required.
13193102Sstever@eecs.umich.edu# only one copy of a particular node
13203102Sstever@eecs.umich.educlass NullSimObject(object):
13213102Sstever@eecs.umich.edu    __metaclass__ = Singleton
13223102Sstever@eecs.umich.edu
13233102Sstever@eecs.umich.edu    def __call__(cls):
13243102Sstever@eecs.umich.edu        return cls
13253102Sstever@eecs.umich.edu
13263102Sstever@eecs.umich.edu    def _instantiate(self, parent = None, path = ''):
13273102Sstever@eecs.umich.edu        pass
13283102Sstever@eecs.umich.edu
13293102Sstever@eecs.umich.edu    def ini_str(self):
13303102Sstever@eecs.umich.edu        return 'Null'
13313102Sstever@eecs.umich.edu
13323102Sstever@eecs.umich.edu    def unproxy(self, base):
13333102Sstever@eecs.umich.edu        return self
13343102Sstever@eecs.umich.edu
13353102Sstever@eecs.umich.edu    def set_path(self, parent, name):
13363102Sstever@eecs.umich.edu        pass
13374762Snate@binkert.org
13383102Sstever@eecs.umich.edu    def __str__(self):
13393102Sstever@eecs.umich.edu        return 'Null'
13403102Sstever@eecs.umich.edu
13414762Snate@binkert.org    def getValue(self):
13424762Snate@binkert.org        return None
13434762Snate@binkert.org
13443102Sstever@eecs.umich.edu# The only instance you'll ever need...
13453102Sstever@eecs.umich.eduNULL = NullSimObject()
13463102Sstever@eecs.umich.edu
13473102Sstever@eecs.umich.edudef isNullPointer(value):
13483102Sstever@eecs.umich.edu    return isinstance(value, NullSimObject)
13493102Sstever@eecs.umich.edu
13503101Sstever@eecs.umich.edu# Some memory range specifications use this as a default upper bound.
13513101Sstever@eecs.umich.eduMaxAddr = Addr.max
13523101Sstever@eecs.umich.eduMaxTick = Tick.max
13533101Sstever@eecs.umich.eduAllMemory = AddrRange(0, MaxAddr)
13543101Sstever@eecs.umich.edu
13553101Sstever@eecs.umich.edu
13563101Sstever@eecs.umich.edu#####################################################################
13573101Sstever@eecs.umich.edu#
13583101Sstever@eecs.umich.edu# Port objects
13593101Sstever@eecs.umich.edu#
13603101Sstever@eecs.umich.edu# Ports are used to interconnect objects in the memory system.
13613101Sstever@eecs.umich.edu#
13623101Sstever@eecs.umich.edu#####################################################################
13633101Sstever@eecs.umich.edu
13643101Sstever@eecs.umich.edu# Port reference: encapsulates a reference to a particular port on a
13653101Sstever@eecs.umich.edu# particular SimObject.
13663101Sstever@eecs.umich.educlass PortRef(object):
13678839Sandreas.hansson@arm.com    def __init__(self, simobj, name, role):
13683105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
13693101Sstever@eecs.umich.edu        self.simobj = simobj
13703101Sstever@eecs.umich.edu        self.name = name
13718839Sandreas.hansson@arm.com        self.role = role
13723101Sstever@eecs.umich.edu        self.peer = None   # not associated with another port yet
13733101Sstever@eecs.umich.edu        self.ccConnected = False # C++ port connection done?
13743105Sstever@eecs.umich.edu        self.index = -1  # always -1 for non-vector ports
13753101Sstever@eecs.umich.edu
13763103Sstever@eecs.umich.edu    def __str__(self):
13773105Sstever@eecs.umich.edu        return '%s.%s' % (self.simobj, self.name)
13783103Sstever@eecs.umich.edu
13798840Sandreas.hansson@arm.com    def __len__(self):
13808840Sandreas.hansson@arm.com        # Return the number of connected ports, i.e. 0 is we have no
13818840Sandreas.hansson@arm.com        # peer and 1 if we do.
13828840Sandreas.hansson@arm.com        return int(self.peer != None)
13838840Sandreas.hansson@arm.com
13843105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
13853105Sstever@eecs.umich.edu    def ini_str(self):
13863105Sstever@eecs.umich.edu        return str(self.peer)
13873105Sstever@eecs.umich.edu
13889017Sandreas.hansson@arm.com    # for config.json
13899017Sandreas.hansson@arm.com    def get_config_as_dict(self):
13909017Sandreas.hansson@arm.com        return {'role' : self.role, 'peer' : str(self.peer)}
13919017Sandreas.hansson@arm.com
13923105Sstever@eecs.umich.edu    def __getattr__(self, attr):
13933105Sstever@eecs.umich.edu        if attr == 'peerObj':
13943105Sstever@eecs.umich.edu            # shorthand for proxies
13953105Sstever@eecs.umich.edu            return self.peer.simobj
13963105Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
13973105Sstever@eecs.umich.edu              (self.__class__.__name__, attr)
13983105Sstever@eecs.umich.edu
13993105Sstever@eecs.umich.edu    # Full connection is symmetric (both ways).  Called via
14003105Sstever@eecs.umich.edu    # SimObject.__setattr__ as a result of a port assignment, e.g.,
14013109Sstever@eecs.umich.edu    # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,
14023105Sstever@eecs.umich.edu    # e.g., "obj1.portA[3] = obj2.portB".
14033105Sstever@eecs.umich.edu    def connect(self, other):
14043105Sstever@eecs.umich.edu        if isinstance(other, VectorPortRef):
14053105Sstever@eecs.umich.edu            # reference to plain VectorPort is implicit append
14063105Sstever@eecs.umich.edu            other = other._get_next()
14073105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
14089014Sandreas.hansson@arm.com            fatal("Port %s is already connected to %s, cannot connect %s\n",
14099014Sandreas.hansson@arm.com                  self, self.peer, other);
14103101Sstever@eecs.umich.edu        self.peer = other
14113109Sstever@eecs.umich.edu        if proxy.isproxy(other):
14123109Sstever@eecs.umich.edu            other.set_param_desc(PortParamDesc())
14133109Sstever@eecs.umich.edu        elif isinstance(other, PortRef):
14143109Sstever@eecs.umich.edu            if other.peer is not self:
14153109Sstever@eecs.umich.edu                other.connect(self)
14163109Sstever@eecs.umich.edu        else:
14173109Sstever@eecs.umich.edu            raise TypeError, \
14183109Sstever@eecs.umich.edu                  "assigning non-port reference '%s' to port '%s'" \
14193109Sstever@eecs.umich.edu                  % (other, self)
14203101Sstever@eecs.umich.edu
14213105Sstever@eecs.umich.edu    def clone(self, simobj, memo):
14223105Sstever@eecs.umich.edu        if memo.has_key(self):
14233105Sstever@eecs.umich.edu            return memo[self]
14243101Sstever@eecs.umich.edu        newRef = copy.copy(self)
14253105Sstever@eecs.umich.edu        memo[self] = newRef
14263105Sstever@eecs.umich.edu        newRef.simobj = simobj
14273101Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
14283105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
14293179Sstever@eecs.umich.edu            peerObj = self.peer.simobj(_memo=memo)
14303105Sstever@eecs.umich.edu            newRef.peer = self.peer.clone(peerObj, memo)
14313105Sstever@eecs.umich.edu            assert(not isinstance(newRef.peer, VectorPortRef))
14323101Sstever@eecs.umich.edu        return newRef
14333101Sstever@eecs.umich.edu
14343105Sstever@eecs.umich.edu    def unproxy(self, simobj):
14353105Sstever@eecs.umich.edu        assert(simobj is self.simobj)
14363105Sstever@eecs.umich.edu        if proxy.isproxy(self.peer):
14373105Sstever@eecs.umich.edu            try:
14383105Sstever@eecs.umich.edu                realPeer = self.peer.unproxy(self.simobj)
14393105Sstever@eecs.umich.edu            except:
14403105Sstever@eecs.umich.edu                print "Error in unproxying port '%s' of %s" % \
14413105Sstever@eecs.umich.edu                      (self.name, self.simobj.path())
14423105Sstever@eecs.umich.edu                raise
14433105Sstever@eecs.umich.edu            self.connect(realPeer)
14443105Sstever@eecs.umich.edu
14453101Sstever@eecs.umich.edu    # Call C++ to create corresponding port connection between C++ objects
14463101Sstever@eecs.umich.edu    def ccConnect(self):
14478597Ssteve.reinhardt@amd.com        from m5.internal.pyobject import connectPorts
14484762Snate@binkert.org
14498839Sandreas.hansson@arm.com        if self.role == 'SLAVE':
14508839Sandreas.hansson@arm.com            # do nothing and let the master take care of it
14518839Sandreas.hansson@arm.com            return
14528839Sandreas.hansson@arm.com
14533101Sstever@eecs.umich.edu        if self.ccConnected: # already done this
14543101Sstever@eecs.umich.edu            return
14553101Sstever@eecs.umich.edu        peer = self.peer
14565578SSteve.Reinhardt@amd.com        if not self.peer: # nothing to connect to
14575578SSteve.Reinhardt@amd.com            return
14588839Sandreas.hansson@arm.com
14598839Sandreas.hansson@arm.com        # check that we connect a master to a slave
14608839Sandreas.hansson@arm.com        if self.role == peer.role:
14618839Sandreas.hansson@arm.com            raise TypeError, \
14628839Sandreas.hansson@arm.com                "cannot connect '%s' and '%s' due to identical role '%s'" \
14638839Sandreas.hansson@arm.com                % (peer, self, self.role)
14648839Sandreas.hansson@arm.com
14657526Ssteve.reinhardt@amd.com        try:
14668839Sandreas.hansson@arm.com            # self is always the master and peer the slave
14677526Ssteve.reinhardt@amd.com            connectPorts(self.simobj.getCCObject(), self.name, self.index,
14687526Ssteve.reinhardt@amd.com                         peer.simobj.getCCObject(), peer.name, peer.index)
14697526Ssteve.reinhardt@amd.com        except:
14707526Ssteve.reinhardt@amd.com            print "Error connecting port %s.%s to %s.%s" % \
14717526Ssteve.reinhardt@amd.com                  (self.simobj.path(), self.name,
14727526Ssteve.reinhardt@amd.com                   peer.simobj.path(), peer.name)
14737526Ssteve.reinhardt@amd.com            raise
14743101Sstever@eecs.umich.edu        self.ccConnected = True
14753101Sstever@eecs.umich.edu        peer.ccConnected = True
14763101Sstever@eecs.umich.edu
14773105Sstever@eecs.umich.edu# A reference to an individual element of a VectorPort... much like a
14783105Sstever@eecs.umich.edu# PortRef, but has an index.
14793105Sstever@eecs.umich.educlass VectorPortElementRef(PortRef):
14808839Sandreas.hansson@arm.com    def __init__(self, simobj, name, role, index):
14818839Sandreas.hansson@arm.com        PortRef.__init__(self, simobj, name, role)
14823105Sstever@eecs.umich.edu        self.index = index
14833105Sstever@eecs.umich.edu
14843105Sstever@eecs.umich.edu    def __str__(self):
14853105Sstever@eecs.umich.edu        return '%s.%s[%d]' % (self.simobj, self.name, self.index)
14863105Sstever@eecs.umich.edu
14873105Sstever@eecs.umich.edu# A reference to a complete vector-valued port (not just a single element).
14883105Sstever@eecs.umich.edu# Can be indexed to retrieve individual VectorPortElementRef instances.
14893105Sstever@eecs.umich.educlass VectorPortRef(object):
14908839Sandreas.hansson@arm.com    def __init__(self, simobj, name, role):
14913105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
14923105Sstever@eecs.umich.edu        self.simobj = simobj
14933105Sstever@eecs.umich.edu        self.name = name
14948839Sandreas.hansson@arm.com        self.role = role
14953105Sstever@eecs.umich.edu        self.elements = []
14963105Sstever@eecs.umich.edu
14973109Sstever@eecs.umich.edu    def __str__(self):
14983109Sstever@eecs.umich.edu        return '%s.%s[:]' % (self.simobj, self.name)
14993109Sstever@eecs.umich.edu
15008840Sandreas.hansson@arm.com    def __len__(self):
15018840Sandreas.hansson@arm.com        # Return the number of connected peers, corresponding the the
15028840Sandreas.hansson@arm.com        # length of the elements.
15038840Sandreas.hansson@arm.com        return len(self.elements)
15048840Sandreas.hansson@arm.com
15053105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
15063105Sstever@eecs.umich.edu    def ini_str(self):
15073105Sstever@eecs.umich.edu        return ' '.join([el.ini_str() for el in self.elements])
15083105Sstever@eecs.umich.edu
15099017Sandreas.hansson@arm.com    # for config.json
15109017Sandreas.hansson@arm.com    def get_config_as_dict(self):
15119017Sandreas.hansson@arm.com        return {'role' : self.role,
15129017Sandreas.hansson@arm.com                'peer' : [el.ini_str() for el in self.elements]}
15139017Sandreas.hansson@arm.com
15143105Sstever@eecs.umich.edu    def __getitem__(self, key):
15153105Sstever@eecs.umich.edu        if not isinstance(key, int):
15163105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
15173105Sstever@eecs.umich.edu        if key >= len(self.elements):
15183105Sstever@eecs.umich.edu            # need to extend list
15198839Sandreas.hansson@arm.com            ext = [VectorPortElementRef(self.simobj, self.name, self.role, i)
15203105Sstever@eecs.umich.edu                   for i in range(len(self.elements), key+1)]
15213105Sstever@eecs.umich.edu            self.elements.extend(ext)
15223105Sstever@eecs.umich.edu        return self.elements[key]
15233105Sstever@eecs.umich.edu
15243105Sstever@eecs.umich.edu    def _get_next(self):
15253105Sstever@eecs.umich.edu        return self[len(self.elements)]
15263105Sstever@eecs.umich.edu
15273105Sstever@eecs.umich.edu    def __setitem__(self, key, value):
15283105Sstever@eecs.umich.edu        if not isinstance(key, int):
15293105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
15303105Sstever@eecs.umich.edu        self[key].connect(value)
15313105Sstever@eecs.umich.edu
15323105Sstever@eecs.umich.edu    def connect(self, other):
15333109Sstever@eecs.umich.edu        if isinstance(other, (list, tuple)):
15343109Sstever@eecs.umich.edu            # Assign list of port refs to vector port.
15353109Sstever@eecs.umich.edu            # For now, append them... not sure if that's the right semantics
15363109Sstever@eecs.umich.edu            # or if it should replace the current vector.
15373109Sstever@eecs.umich.edu            for ref in other:
15383109Sstever@eecs.umich.edu                self._get_next().connect(ref)
15393109Sstever@eecs.umich.edu        else:
15403109Sstever@eecs.umich.edu            # scalar assignment to plain VectorPort is implicit append
15413109Sstever@eecs.umich.edu            self._get_next().connect(other)
15423109Sstever@eecs.umich.edu
15433109Sstever@eecs.umich.edu    def clone(self, simobj, memo):
15443109Sstever@eecs.umich.edu        if memo.has_key(self):
15453109Sstever@eecs.umich.edu            return memo[self]
15463109Sstever@eecs.umich.edu        newRef = copy.copy(self)
15473109Sstever@eecs.umich.edu        memo[self] = newRef
15483109Sstever@eecs.umich.edu        newRef.simobj = simobj
15493109Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
15503109Sstever@eecs.umich.edu        newRef.elements = [el.clone(simobj, memo) for el in self.elements]
15513109Sstever@eecs.umich.edu        return newRef
15523105Sstever@eecs.umich.edu
15533105Sstever@eecs.umich.edu    def unproxy(self, simobj):
15543105Sstever@eecs.umich.edu        [el.unproxy(simobj) for el in self.elements]
15553105Sstever@eecs.umich.edu
15563105Sstever@eecs.umich.edu    def ccConnect(self):
15573105Sstever@eecs.umich.edu        [el.ccConnect() for el in self.elements]
15583105Sstever@eecs.umich.edu
15593101Sstever@eecs.umich.edu# Port description object.  Like a ParamDesc object, this represents a
15603101Sstever@eecs.umich.edu# logical port in the SimObject class, not a particular port on a
15613101Sstever@eecs.umich.edu# SimObject instance.  The latter are represented by PortRef objects.
15623101Sstever@eecs.umich.educlass Port(object):
15633101Sstever@eecs.umich.edu    # Generate a PortRef for this port on the given SimObject with the
15643101Sstever@eecs.umich.edu    # given name
15653105Sstever@eecs.umich.edu    def makeRef(self, simobj):
15668839Sandreas.hansson@arm.com        return PortRef(simobj, self.name, self.role)
15673101Sstever@eecs.umich.edu
15683101Sstever@eecs.umich.edu    # Connect an instance of this port (on the given SimObject with
15693101Sstever@eecs.umich.edu    # the given name) with the port described by the supplied PortRef
15703105Sstever@eecs.umich.edu    def connect(self, simobj, ref):
15713105Sstever@eecs.umich.edu        self.makeRef(simobj).connect(ref)
15723101Sstever@eecs.umich.edu
15738840Sandreas.hansson@arm.com    # No need for any pre-declarations at the moment as we merely rely
15748840Sandreas.hansson@arm.com    # on an unsigned int.
15758840Sandreas.hansson@arm.com    def cxx_predecls(self, code):
15768840Sandreas.hansson@arm.com        pass
15778840Sandreas.hansson@arm.com
15788840Sandreas.hansson@arm.com    # Declare an unsigned int with the same name as the port, that
15798840Sandreas.hansson@arm.com    # will eventually hold the number of connected ports (and thus the
15808840Sandreas.hansson@arm.com    # number of elements for a VectorPort).
15818840Sandreas.hansson@arm.com    def cxx_decl(self, code):
15828840Sandreas.hansson@arm.com        code('unsigned int port_${{self.name}}_connection_count;')
15838840Sandreas.hansson@arm.com
15848839Sandreas.hansson@arm.comclass MasterPort(Port):
15858839Sandreas.hansson@arm.com    # MasterPort("description")
15868839Sandreas.hansson@arm.com    def __init__(self, *args):
15878839Sandreas.hansson@arm.com        if len(args) == 1:
15888839Sandreas.hansson@arm.com            self.desc = args[0]
15898839Sandreas.hansson@arm.com            self.role = 'MASTER'
15908839Sandreas.hansson@arm.com        else:
15918839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
15928839Sandreas.hansson@arm.com
15938839Sandreas.hansson@arm.comclass SlavePort(Port):
15948839Sandreas.hansson@arm.com    # SlavePort("description")
15958839Sandreas.hansson@arm.com    def __init__(self, *args):
15968839Sandreas.hansson@arm.com        if len(args) == 1:
15978839Sandreas.hansson@arm.com            self.desc = args[0]
15988839Sandreas.hansson@arm.com            self.role = 'SLAVE'
15998839Sandreas.hansson@arm.com        else:
16008839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
16018839Sandreas.hansson@arm.com
16023101Sstever@eecs.umich.edu# VectorPort description object.  Like Port, but represents a vector
16033101Sstever@eecs.umich.edu# of connections (e.g., as on a Bus).
16043101Sstever@eecs.umich.educlass VectorPort(Port):
16053105Sstever@eecs.umich.edu    def __init__(self, *args):
16063101Sstever@eecs.umich.edu        self.isVec = True
16073101Sstever@eecs.umich.edu
16083105Sstever@eecs.umich.edu    def makeRef(self, simobj):
16098839Sandreas.hansson@arm.com        return VectorPortRef(simobj, self.name, self.role)
16108839Sandreas.hansson@arm.com
16118839Sandreas.hansson@arm.comclass VectorMasterPort(VectorPort):
16128839Sandreas.hansson@arm.com    # VectorMasterPort("description")
16138839Sandreas.hansson@arm.com    def __init__(self, *args):
16148839Sandreas.hansson@arm.com        if len(args) == 1:
16158839Sandreas.hansson@arm.com            self.desc = args[0]
16168839Sandreas.hansson@arm.com            self.role = 'MASTER'
16178839Sandreas.hansson@arm.com            VectorPort.__init__(self, *args)
16188839Sandreas.hansson@arm.com        else:
16198839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
16208839Sandreas.hansson@arm.com
16218839Sandreas.hansson@arm.comclass VectorSlavePort(VectorPort):
16228839Sandreas.hansson@arm.com    # VectorSlavePort("description")
16238839Sandreas.hansson@arm.com    def __init__(self, *args):
16248839Sandreas.hansson@arm.com        if len(args) == 1:
16258839Sandreas.hansson@arm.com            self.desc = args[0]
16268839Sandreas.hansson@arm.com            self.role = 'SLAVE'
16278839Sandreas.hansson@arm.com            VectorPort.__init__(self, *args)
16288839Sandreas.hansson@arm.com        else:
16298839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
16303105Sstever@eecs.umich.edu
16313109Sstever@eecs.umich.edu# 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
16323109Sstever@eecs.umich.edu# proxy objects (via set_param_desc()) so that proxy error messages
16333109Sstever@eecs.umich.edu# make sense.
16343109Sstever@eecs.umich.educlass PortParamDesc(object):
16353109Sstever@eecs.umich.edu    __metaclass__ = Singleton
16363109Sstever@eecs.umich.edu
16373109Sstever@eecs.umich.edu    ptype_str = 'Port'
16383109Sstever@eecs.umich.edu    ptype = Port
16393105Sstever@eecs.umich.edu
16406654Snate@binkert.orgbaseEnums = allEnums.copy()
16416654Snate@binkert.orgbaseParams = allParams.copy()
16426654Snate@binkert.org
16436654Snate@binkert.orgdef clear():
16446654Snate@binkert.org    global allEnums, allParams
16456654Snate@binkert.org
16466654Snate@binkert.org    allEnums = baseEnums.copy()
16476654Snate@binkert.org    allParams = baseParams.copy()
16486654Snate@binkert.org
16493101Sstever@eecs.umich.edu__all__ = ['Param', 'VectorParam',
16503101Sstever@eecs.umich.edu           'Enum', 'Bool', 'String', 'Float',
16513101Sstever@eecs.umich.edu           'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
16523101Sstever@eecs.umich.edu           'Int32', 'UInt32', 'Int64', 'UInt64',
16533101Sstever@eecs.umich.edu           'Counter', 'Addr', 'Tick', 'Percent',
16543101Sstever@eecs.umich.edu           'TcpPort', 'UdpPort', 'EthernetAddr',
16557777Sgblack@eecs.umich.edu           'IpAddress', 'IpNetmask', 'IpWithPort',
16563101Sstever@eecs.umich.edu           'MemorySize', 'MemorySize32',
16579827Sakash.bagdia@arm.com           'Latency', 'Frequency', 'Clock', 'Voltage',
16583101Sstever@eecs.umich.edu           'NetworkBandwidth', 'MemoryBandwidth',
16599232Sandreas.hansson@arm.com           'AddrRange',
16603101Sstever@eecs.umich.edu           'MaxAddr', 'MaxTick', 'AllMemory',
16613885Sbinkertn@umich.edu           'Time',
16623102Sstever@eecs.umich.edu           'NextEthernetAddr', 'NULL',
16638839Sandreas.hansson@arm.com           'MasterPort', 'SlavePort',
16648839Sandreas.hansson@arm.com           'VectorMasterPort', 'VectorSlavePort']
16656654Snate@binkert.org
16666654Snate@binkert.orgimport SimObject
1667