params.py revision 9941
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
2503101Sstever@eecs.umich.educlass VectorParamDesc(ParamDesc):
2513101Sstever@eecs.umich.edu    # Convert assigned value to appropriate type.  If the RHS is not a
2523101Sstever@eecs.umich.edu    # list or tuple, it generates a single-element list.
2533101Sstever@eecs.umich.edu    def convert(self, value):
2543101Sstever@eecs.umich.edu        if isinstance(value, (list, tuple)):
2553101Sstever@eecs.umich.edu            # list: coerce each element into new list
2563101Sstever@eecs.umich.edu            tmp_list = [ ParamDesc.convert(self, v) for v in value ]
2573101Sstever@eecs.umich.edu        else:
2584762Snate@binkert.org            # singleton: coerce to a single-element list
2594762Snate@binkert.org            tmp_list = [ ParamDesc.convert(self, value) ]
2604762Snate@binkert.org
2614762Snate@binkert.org        if isSimObjectSequence(tmp_list):
2627528Ssteve.reinhardt@amd.com            return SimObjectVector(tmp_list)
2634762Snate@binkert.org        else:
2644762Snate@binkert.org            return VectorParamValue(tmp_list)
2654762Snate@binkert.org
2668596Ssteve.reinhardt@amd.com    def swig_module_name(self):
2678596Ssteve.reinhardt@amd.com        return "%s_vector" % self.ptype_str
2688596Ssteve.reinhardt@amd.com
2697673Snate@binkert.org    def swig_predecls(self, code):
2708596Ssteve.reinhardt@amd.com        code('%import "${{self.swig_module_name()}}.i"')
2714762Snate@binkert.org
2727673Snate@binkert.org    def swig_decl(self, code):
2738596Ssteve.reinhardt@amd.com        code('%module(package="m5.internal") ${{self.swig_module_name()}}')
2747675Snate@binkert.org        code('%{')
2757675Snate@binkert.org        self.ptype.cxx_predecls(code)
2767675Snate@binkert.org        code('%}')
2777675Snate@binkert.org        code()
2788656Sandreas.hansson@arm.com        # Make sure the SWIGPY_SLICE_ARG is defined through this inclusion
2798656Sandreas.hansson@arm.com        code('%include "std_container.i"')
2808656Sandreas.hansson@arm.com        code()
2817675Snate@binkert.org        self.ptype.swig_predecls(code)
2827675Snate@binkert.org        code()
2837673Snate@binkert.org        code('%include "std_vector.i"')
2847675Snate@binkert.org        code()
2857675Snate@binkert.org
2867675Snate@binkert.org        ptype = self.ptype_str
2877675Snate@binkert.org        cxx_type = self.ptype.cxx_type
2887675Snate@binkert.org
2897673Snate@binkert.org        code('''\
2907675Snate@binkert.org%typemap(in) std::vector< $cxx_type >::value_type {
2917675Snate@binkert.org    if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
2927675Snate@binkert.org        if (SWIG_ConvertPtr($$input, (void **)&$$1,
2937675Snate@binkert.org                            $$descriptor($cxx_type), 0) == -1) {
2947675Snate@binkert.org            return NULL;
2957675Snate@binkert.org        }
2967675Snate@binkert.org    }
2977675Snate@binkert.org}
2987675Snate@binkert.org
2997675Snate@binkert.org%typemap(in) std::vector< $cxx_type >::value_type * {
3007675Snate@binkert.org    if (SWIG_ConvertPtr($$input, (void **)&$$1, $$1_descriptor, 0) == -1) {
3017675Snate@binkert.org        if (SWIG_ConvertPtr($$input, (void **)&$$1,
3027675Snate@binkert.org                            $$descriptor($cxx_type *), 0) == -1) {
3037675Snate@binkert.org            return NULL;
3047675Snate@binkert.org        }
3057675Snate@binkert.org    }
3067673Snate@binkert.org}
3077673Snate@binkert.org''')
3083101Sstever@eecs.umich.edu
3097675Snate@binkert.org        code('%template(vector_$ptype) std::vector< $cxx_type >;')
3107675Snate@binkert.org
3117673Snate@binkert.org    def cxx_predecls(self, code):
3127673Snate@binkert.org        code('#include <vector>')
3137673Snate@binkert.org        self.ptype.cxx_predecls(code)
3143101Sstever@eecs.umich.edu
3157673Snate@binkert.org    def cxx_decl(self, code):
3167673Snate@binkert.org        code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
3173101Sstever@eecs.umich.edu
3183101Sstever@eecs.umich.educlass ParamFactory(object):
3193101Sstever@eecs.umich.edu    def __init__(self, param_desc_class, ptype_str = None):
3203101Sstever@eecs.umich.edu        self.param_desc_class = param_desc_class
3213101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
3223101Sstever@eecs.umich.edu
3233101Sstever@eecs.umich.edu    def __getattr__(self, attr):
3243101Sstever@eecs.umich.edu        if self.ptype_str:
3253101Sstever@eecs.umich.edu            attr = self.ptype_str + '.' + attr
3263101Sstever@eecs.umich.edu        return ParamFactory(self.param_desc_class, attr)
3273101Sstever@eecs.umich.edu
3283101Sstever@eecs.umich.edu    # E.g., Param.Int(5, "number of widgets")
3293101Sstever@eecs.umich.edu    def __call__(self, *args, **kwargs):
3303101Sstever@eecs.umich.edu        ptype = None
3313101Sstever@eecs.umich.edu        try:
3325033Smilesck@eecs.umich.edu            ptype = allParams[self.ptype_str]
3335033Smilesck@eecs.umich.edu        except KeyError:
3343101Sstever@eecs.umich.edu            # if name isn't defined yet, assume it's a SimObject, and
3353101Sstever@eecs.umich.edu            # try to resolve it later
3363101Sstever@eecs.umich.edu            pass
3373101Sstever@eecs.umich.edu        return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs)
3383101Sstever@eecs.umich.edu
3393101Sstever@eecs.umich.eduParam = ParamFactory(ParamDesc)
3403101Sstever@eecs.umich.eduVectorParam = ParamFactory(VectorParamDesc)
3413101Sstever@eecs.umich.edu
3423101Sstever@eecs.umich.edu#####################################################################
3433101Sstever@eecs.umich.edu#
3443101Sstever@eecs.umich.edu# Parameter Types
3453101Sstever@eecs.umich.edu#
3463101Sstever@eecs.umich.edu# Though native Python types could be used to specify parameter types
3473101Sstever@eecs.umich.edu# (the 'ptype' field of the Param and VectorParam classes), it's more
3483101Sstever@eecs.umich.edu# flexible to define our own set of types.  This gives us more control
3493101Sstever@eecs.umich.edu# over how Python expressions are converted to values (via the
3503101Sstever@eecs.umich.edu# __init__() constructor) and how these values are printed out (via
3513101Sstever@eecs.umich.edu# the __str__() conversion method).
3523101Sstever@eecs.umich.edu#
3533101Sstever@eecs.umich.edu#####################################################################
3543101Sstever@eecs.umich.edu
3553101Sstever@eecs.umich.edu# String-valued parameter.  Just mixin the ParamValue class with the
3563101Sstever@eecs.umich.edu# built-in str class.
3573101Sstever@eecs.umich.educlass String(ParamValue,str):
3583101Sstever@eecs.umich.edu    cxx_type = 'std::string'
3597673Snate@binkert.org
3607673Snate@binkert.org    @classmethod
3617673Snate@binkert.org    def cxx_predecls(self, code):
3627673Snate@binkert.org        code('#include <string>')
3637673Snate@binkert.org
3647673Snate@binkert.org    @classmethod
3657673Snate@binkert.org    def swig_predecls(cls, code):
3667673Snate@binkert.org        code('%include "std_string.i"')
3674762Snate@binkert.org
3684762Snate@binkert.org    def getValue(self):
3694762Snate@binkert.org        return self
3703101Sstever@eecs.umich.edu
3713101Sstever@eecs.umich.edu# superclass for "numeric" parameter values, to emulate math
3723101Sstever@eecs.umich.edu# operations in a type-safe way.  e.g., a Latency times an int returns
3733101Sstever@eecs.umich.edu# a new Latency object.
3743101Sstever@eecs.umich.educlass NumericParamValue(ParamValue):
3753101Sstever@eecs.umich.edu    def __str__(self):
3763101Sstever@eecs.umich.edu        return str(self.value)
3773101Sstever@eecs.umich.edu
3783101Sstever@eecs.umich.edu    def __float__(self):
3793101Sstever@eecs.umich.edu        return float(self.value)
3803101Sstever@eecs.umich.edu
3813714Sstever@eecs.umich.edu    def __long__(self):
3823714Sstever@eecs.umich.edu        return long(self.value)
3833714Sstever@eecs.umich.edu
3843714Sstever@eecs.umich.edu    def __int__(self):
3853714Sstever@eecs.umich.edu        return int(self.value)
3863714Sstever@eecs.umich.edu
3873101Sstever@eecs.umich.edu    # hook for bounds checking
3883101Sstever@eecs.umich.edu    def _check(self):
3893101Sstever@eecs.umich.edu        return
3903101Sstever@eecs.umich.edu
3913101Sstever@eecs.umich.edu    def __mul__(self, other):
3923101Sstever@eecs.umich.edu        newobj = self.__class__(self)
3933101Sstever@eecs.umich.edu        newobj.value *= other
3943101Sstever@eecs.umich.edu        newobj._check()
3953101Sstever@eecs.umich.edu        return newobj
3963101Sstever@eecs.umich.edu
3973101Sstever@eecs.umich.edu    __rmul__ = __mul__
3983101Sstever@eecs.umich.edu
3993101Sstever@eecs.umich.edu    def __div__(self, other):
4003101Sstever@eecs.umich.edu        newobj = self.__class__(self)
4013101Sstever@eecs.umich.edu        newobj.value /= other
4023101Sstever@eecs.umich.edu        newobj._check()
4033101Sstever@eecs.umich.edu        return newobj
4043101Sstever@eecs.umich.edu
4053101Sstever@eecs.umich.edu    def __sub__(self, other):
4063101Sstever@eecs.umich.edu        newobj = self.__class__(self)
4073101Sstever@eecs.umich.edu        newobj.value -= other
4083101Sstever@eecs.umich.edu        newobj._check()
4093101Sstever@eecs.umich.edu        return newobj
4103101Sstever@eecs.umich.edu
4113101Sstever@eecs.umich.edu# Metaclass for bounds-checked integer parameters.  See CheckedInt.
4125033Smilesck@eecs.umich.educlass CheckedIntType(MetaParamValue):
4133101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
4143101Sstever@eecs.umich.edu        super(CheckedIntType, cls).__init__(name, bases, dict)
4153101Sstever@eecs.umich.edu
4163101Sstever@eecs.umich.edu        # CheckedInt is an abstract base class, so we actually don't
4173101Sstever@eecs.umich.edu        # want to do any processing on it... the rest of this code is
4183101Sstever@eecs.umich.edu        # just for classes that derive from CheckedInt.
4193101Sstever@eecs.umich.edu        if name == 'CheckedInt':
4203101Sstever@eecs.umich.edu            return
4213101Sstever@eecs.umich.edu
4223101Sstever@eecs.umich.edu        if not (hasattr(cls, 'min') and hasattr(cls, 'max')):
4233101Sstever@eecs.umich.edu            if not (hasattr(cls, 'size') and hasattr(cls, 'unsigned')):
4243101Sstever@eecs.umich.edu                panic("CheckedInt subclass %s must define either\n" \
4255822Ssaidi@eecs.umich.edu                      "    'min' and 'max' or 'size' and 'unsigned'\n",
4265822Ssaidi@eecs.umich.edu                      name);
4273101Sstever@eecs.umich.edu            if cls.unsigned:
4283101Sstever@eecs.umich.edu                cls.min = 0
4293101Sstever@eecs.umich.edu                cls.max = 2 ** cls.size - 1
4303101Sstever@eecs.umich.edu            else:
4313101Sstever@eecs.umich.edu                cls.min = -(2 ** (cls.size - 1))
4323101Sstever@eecs.umich.edu                cls.max = (2 ** (cls.size - 1)) - 1
4333101Sstever@eecs.umich.edu
4343101Sstever@eecs.umich.edu# Abstract superclass for bounds-checked integer parameters.  This
4353101Sstever@eecs.umich.edu# class is subclassed to generate parameter classes with specific
4363101Sstever@eecs.umich.edu# bounds.  Initialization of the min and max bounds is done in the
4373101Sstever@eecs.umich.edu# metaclass CheckedIntType.__init__.
4383101Sstever@eecs.umich.educlass CheckedInt(NumericParamValue):
4393101Sstever@eecs.umich.edu    __metaclass__ = CheckedIntType
4403101Sstever@eecs.umich.edu
4413101Sstever@eecs.umich.edu    def _check(self):
4423101Sstever@eecs.umich.edu        if not self.min <= self.value <= self.max:
4433101Sstever@eecs.umich.edu            raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
4443101Sstever@eecs.umich.edu                  (self.min, self.value, self.max)
4453101Sstever@eecs.umich.edu
4463101Sstever@eecs.umich.edu    def __init__(self, value):
4473101Sstever@eecs.umich.edu        if isinstance(value, str):
4483102Sstever@eecs.umich.edu            self.value = convert.toInteger(value)
4493714Sstever@eecs.umich.edu        elif isinstance(value, (int, long, float, NumericParamValue)):
4503101Sstever@eecs.umich.edu            self.value = long(value)
4513714Sstever@eecs.umich.edu        else:
4523714Sstever@eecs.umich.edu            raise TypeError, "Can't convert object of type %s to CheckedInt" \
4533714Sstever@eecs.umich.edu                  % type(value).__name__
4543101Sstever@eecs.umich.edu        self._check()
4553101Sstever@eecs.umich.edu
4567673Snate@binkert.org    @classmethod
4577673Snate@binkert.org    def cxx_predecls(cls, code):
4587673Snate@binkert.org        # most derived types require this, so we just do it here once
4597673Snate@binkert.org        code('#include "base/types.hh"')
4607673Snate@binkert.org
4617673Snate@binkert.org    @classmethod
4627673Snate@binkert.org    def swig_predecls(cls, code):
4637673Snate@binkert.org        # most derived types require this, so we just do it here once
4647673Snate@binkert.org        code('%import "stdint.i"')
4657673Snate@binkert.org        code('%import "base/types.hh"')
4667673Snate@binkert.org
4674762Snate@binkert.org    def getValue(self):
4684762Snate@binkert.org        return long(self.value)
4694762Snate@binkert.org
4703101Sstever@eecs.umich.educlass Int(CheckedInt):      cxx_type = 'int';      size = 32; unsigned = False
4713101Sstever@eecs.umich.educlass Unsigned(CheckedInt): cxx_type = 'unsigned'; size = 32; unsigned = True
4723101Sstever@eecs.umich.edu
4733101Sstever@eecs.umich.educlass Int8(CheckedInt):     cxx_type =   'int8_t'; size =  8; unsigned = False
4743101Sstever@eecs.umich.educlass UInt8(CheckedInt):    cxx_type =  'uint8_t'; size =  8; unsigned = True
4753101Sstever@eecs.umich.educlass Int16(CheckedInt):    cxx_type =  'int16_t'; size = 16; unsigned = False
4763101Sstever@eecs.umich.educlass UInt16(CheckedInt):   cxx_type = 'uint16_t'; size = 16; unsigned = True
4773101Sstever@eecs.umich.educlass Int32(CheckedInt):    cxx_type =  'int32_t'; size = 32; unsigned = False
4783101Sstever@eecs.umich.educlass UInt32(CheckedInt):   cxx_type = 'uint32_t'; size = 32; unsigned = True
4793101Sstever@eecs.umich.educlass Int64(CheckedInt):    cxx_type =  'int64_t'; size = 64; unsigned = False
4803101Sstever@eecs.umich.educlass UInt64(CheckedInt):   cxx_type = 'uint64_t'; size = 64; unsigned = True
4813101Sstever@eecs.umich.edu
4823101Sstever@eecs.umich.educlass Counter(CheckedInt):  cxx_type = 'Counter';  size = 64; unsigned = True
4833101Sstever@eecs.umich.educlass Tick(CheckedInt):     cxx_type = 'Tick';     size = 64; unsigned = True
4843101Sstever@eecs.umich.educlass TcpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
4853101Sstever@eecs.umich.educlass UdpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
4863101Sstever@eecs.umich.edu
4873101Sstever@eecs.umich.educlass Percent(CheckedInt):  cxx_type = 'int'; min = 0; max = 100
4883101Sstever@eecs.umich.edu
4899184Sandreas.hansson@arm.comclass Cycles(CheckedInt):
4909184Sandreas.hansson@arm.com    cxx_type = 'Cycles'
4919184Sandreas.hansson@arm.com    size = 64
4929184Sandreas.hansson@arm.com    unsigned = True
4939184Sandreas.hansson@arm.com
4949184Sandreas.hansson@arm.com    def getValue(self):
4959184Sandreas.hansson@arm.com        from m5.internal.core import Cycles
4969184Sandreas.hansson@arm.com        return Cycles(self.value)
4979184Sandreas.hansson@arm.com
4983101Sstever@eecs.umich.educlass Float(ParamValue, float):
4994446Sbinkertn@umich.edu    cxx_type = 'double'
5003101Sstever@eecs.umich.edu
5015468Snate@binkert.org    def __init__(self, value):
5025468Snate@binkert.org        if isinstance(value, (int, long, float, NumericParamValue, Float)):
5035468Snate@binkert.org            self.value = float(value)
5045468Snate@binkert.org        else:
5055468Snate@binkert.org            raise TypeError, "Can't convert object of type %s to Float" \
5065468Snate@binkert.org                  % type(value).__name__
5075468Snate@binkert.org
5084762Snate@binkert.org    def getValue(self):
5094762Snate@binkert.org        return float(self.value)
5104762Snate@binkert.org
5113101Sstever@eecs.umich.educlass MemorySize(CheckedInt):
5123101Sstever@eecs.umich.edu    cxx_type = 'uint64_t'
5133101Sstever@eecs.umich.edu    size = 64
5143101Sstever@eecs.umich.edu    unsigned = True
5153101Sstever@eecs.umich.edu    def __init__(self, value):
5163101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
5173101Sstever@eecs.umich.edu            self.value = value.value
5183101Sstever@eecs.umich.edu        else:
5193102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
5203101Sstever@eecs.umich.edu        self._check()
5213101Sstever@eecs.umich.edu
5223101Sstever@eecs.umich.educlass MemorySize32(CheckedInt):
5234168Sbinkertn@umich.edu    cxx_type = 'uint32_t'
5243101Sstever@eecs.umich.edu    size = 32
5253101Sstever@eecs.umich.edu    unsigned = True
5263101Sstever@eecs.umich.edu    def __init__(self, value):
5273101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
5283101Sstever@eecs.umich.edu            self.value = value.value
5293101Sstever@eecs.umich.edu        else:
5303102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
5313101Sstever@eecs.umich.edu        self._check()
5323101Sstever@eecs.umich.edu
5333101Sstever@eecs.umich.educlass Addr(CheckedInt):
5343101Sstever@eecs.umich.edu    cxx_type = 'Addr'
5353101Sstever@eecs.umich.edu    size = 64
5363101Sstever@eecs.umich.edu    unsigned = True
5373101Sstever@eecs.umich.edu    def __init__(self, value):
5383101Sstever@eecs.umich.edu        if isinstance(value, Addr):
5393101Sstever@eecs.umich.edu            self.value = value.value
5403101Sstever@eecs.umich.edu        else:
5413101Sstever@eecs.umich.edu            try:
5423102Sstever@eecs.umich.edu                self.value = convert.toMemorySize(value)
5433101Sstever@eecs.umich.edu            except TypeError:
5443101Sstever@eecs.umich.edu                self.value = long(value)
5453101Sstever@eecs.umich.edu        self._check()
5463584Ssaidi@eecs.umich.edu    def __add__(self, other):
5473584Ssaidi@eecs.umich.edu        if isinstance(other, Addr):
5483584Ssaidi@eecs.umich.edu            return self.value + other.value
5493584Ssaidi@eecs.umich.edu        else:
5503584Ssaidi@eecs.umich.edu            return self.value + other
5513101Sstever@eecs.umich.edu
5529232Sandreas.hansson@arm.comclass AddrRange(ParamValue):
5539235Sandreas.hansson@arm.com    cxx_type = 'AddrRange'
5543101Sstever@eecs.umich.edu
5553101Sstever@eecs.umich.edu    def __init__(self, *args, **kwargs):
5569411Sandreas.hansson@arm.com        # Disable interleaving by default
5579411Sandreas.hansson@arm.com        self.intlvHighBit = 0
5589411Sandreas.hansson@arm.com        self.intlvBits = 0
5599411Sandreas.hansson@arm.com        self.intlvMatch = 0
5609411Sandreas.hansson@arm.com
5613101Sstever@eecs.umich.edu        def handle_kwargs(self, kwargs):
5629411Sandreas.hansson@arm.com            # An address range needs to have an upper limit, specified
5639411Sandreas.hansson@arm.com            # either explicitly with an end, or as an offset using the
5649411Sandreas.hansson@arm.com            # size keyword.
5653101Sstever@eecs.umich.edu            if 'end' in kwargs:
5669232Sandreas.hansson@arm.com                self.end = Addr(kwargs.pop('end'))
5673101Sstever@eecs.umich.edu            elif 'size' in kwargs:
5689232Sandreas.hansson@arm.com                self.end = self.start + Addr(kwargs.pop('size')) - 1
5693101Sstever@eecs.umich.edu            else:
5703101Sstever@eecs.umich.edu                raise TypeError, "Either end or size must be specified"
5713101Sstever@eecs.umich.edu
5729411Sandreas.hansson@arm.com            # Now on to the optional bit
5739411Sandreas.hansson@arm.com            if 'intlvHighBit' in kwargs:
5749411Sandreas.hansson@arm.com                self.intlvHighBit = int(kwargs.pop('intlvHighBit'))
5759411Sandreas.hansson@arm.com            if 'intlvBits' in kwargs:
5769411Sandreas.hansson@arm.com                self.intlvBits = int(kwargs.pop('intlvBits'))
5779411Sandreas.hansson@arm.com            if 'intlvMatch' in kwargs:
5789411Sandreas.hansson@arm.com                self.intlvMatch = int(kwargs.pop('intlvMatch'))
5799411Sandreas.hansson@arm.com
5803101Sstever@eecs.umich.edu        if len(args) == 0:
5819232Sandreas.hansson@arm.com            self.start = Addr(kwargs.pop('start'))
5823101Sstever@eecs.umich.edu            handle_kwargs(self, kwargs)
5833101Sstever@eecs.umich.edu
5843101Sstever@eecs.umich.edu        elif len(args) == 1:
5853101Sstever@eecs.umich.edu            if kwargs:
5869232Sandreas.hansson@arm.com                self.start = Addr(args[0])
5873101Sstever@eecs.umich.edu                handle_kwargs(self, kwargs)
5885219Ssaidi@eecs.umich.edu            elif isinstance(args[0], (list, tuple)):
5899232Sandreas.hansson@arm.com                self.start = Addr(args[0][0])
5909232Sandreas.hansson@arm.com                self.end = Addr(args[0][1])
5913101Sstever@eecs.umich.edu            else:
5929232Sandreas.hansson@arm.com                self.start = Addr(0)
5939232Sandreas.hansson@arm.com                self.end = Addr(args[0]) - 1
5943101Sstever@eecs.umich.edu
5953101Sstever@eecs.umich.edu        elif len(args) == 2:
5969232Sandreas.hansson@arm.com            self.start = Addr(args[0])
5979232Sandreas.hansson@arm.com            self.end = Addr(args[1])
5983101Sstever@eecs.umich.edu        else:
5993101Sstever@eecs.umich.edu            raise TypeError, "Too many arguments specified"
6003101Sstever@eecs.umich.edu
6013101Sstever@eecs.umich.edu        if kwargs:
6029232Sandreas.hansson@arm.com            raise TypeError, "Too many keywords: %s" % kwargs.keys()
6033101Sstever@eecs.umich.edu
6043101Sstever@eecs.umich.edu    def __str__(self):
6059232Sandreas.hansson@arm.com        return '%s:%s' % (self.start, self.end)
6069232Sandreas.hansson@arm.com
6079232Sandreas.hansson@arm.com    def size(self):
6089411Sandreas.hansson@arm.com        # Divide the size by the size of the interleaving slice
6099411Sandreas.hansson@arm.com        return (long(self.end) - long(self.start) + 1) >> self.intlvBits
6103101Sstever@eecs.umich.edu
6117673Snate@binkert.org    @classmethod
6127673Snate@binkert.org    def cxx_predecls(cls, code):
6139232Sandreas.hansson@arm.com        Addr.cxx_predecls(code)
6149235Sandreas.hansson@arm.com        code('#include "base/addr_range.hh"')
6157675Snate@binkert.org
6167675Snate@binkert.org    @classmethod
6177675Snate@binkert.org    def swig_predecls(cls, code):
6189232Sandreas.hansson@arm.com        Addr.swig_predecls(code)
6197673Snate@binkert.org
6204762Snate@binkert.org    def getValue(self):
6219235Sandreas.hansson@arm.com        # Go from the Python class to the wrapped C++ class generated
6229235Sandreas.hansson@arm.com        # by swig
6237675Snate@binkert.org        from m5.internal.range import AddrRange
6244762Snate@binkert.org
6259411Sandreas.hansson@arm.com        return AddrRange(long(self.start), long(self.end),
6269411Sandreas.hansson@arm.com                         int(self.intlvHighBit), int(self.intlvBits),
6279411Sandreas.hansson@arm.com                         int(self.intlvMatch))
6283101Sstever@eecs.umich.edu
6293101Sstever@eecs.umich.edu# Boolean parameter type.  Python doesn't let you subclass bool, since
6303101Sstever@eecs.umich.edu# it doesn't want to let you create multiple instances of True and
6313101Sstever@eecs.umich.edu# False.  Thus this is a little more complicated than String.
6323101Sstever@eecs.umich.educlass Bool(ParamValue):
6333101Sstever@eecs.umich.edu    cxx_type = 'bool'
6343101Sstever@eecs.umich.edu    def __init__(self, value):
6353101Sstever@eecs.umich.edu        try:
6363102Sstever@eecs.umich.edu            self.value = convert.toBool(value)
6373101Sstever@eecs.umich.edu        except TypeError:
6383101Sstever@eecs.umich.edu            self.value = bool(value)
6393101Sstever@eecs.umich.edu
6404762Snate@binkert.org    def getValue(self):
6414762Snate@binkert.org        return bool(self.value)
6424762Snate@binkert.org
6433101Sstever@eecs.umich.edu    def __str__(self):
6443101Sstever@eecs.umich.edu        return str(self.value)
6453101Sstever@eecs.umich.edu
6468934SBrad.Beckmann@amd.com    # implement truth value testing for Bool parameters so that these params
6478934SBrad.Beckmann@amd.com    # evaluate correctly during the python configuration phase
6488934SBrad.Beckmann@amd.com    def __nonzero__(self):
6498934SBrad.Beckmann@amd.com        return bool(self.value)
6508934SBrad.Beckmann@amd.com
6513101Sstever@eecs.umich.edu    def ini_str(self):
6523101Sstever@eecs.umich.edu        if self.value:
6533101Sstever@eecs.umich.edu            return 'true'
6543101Sstever@eecs.umich.edu        return 'false'
6553101Sstever@eecs.umich.edu
6563101Sstever@eecs.umich.edudef IncEthernetAddr(addr, val = 1):
6573101Sstever@eecs.umich.edu    bytes = map(lambda x: int(x, 16), addr.split(':'))
6583101Sstever@eecs.umich.edu    bytes[5] += val
6593101Sstever@eecs.umich.edu    for i in (5, 4, 3, 2, 1):
6603101Sstever@eecs.umich.edu        val,rem = divmod(bytes[i], 256)
6613101Sstever@eecs.umich.edu        bytes[i] = rem
6623101Sstever@eecs.umich.edu        if val == 0:
6633101Sstever@eecs.umich.edu            break
6643101Sstever@eecs.umich.edu        bytes[i - 1] += val
6653101Sstever@eecs.umich.edu    assert(bytes[0] <= 255)
6663101Sstever@eecs.umich.edu    return ':'.join(map(lambda x: '%02x' % x, bytes))
6673101Sstever@eecs.umich.edu
6684380Sbinkertn@umich.edu_NextEthernetAddr = "00:90:00:00:00:01"
6694380Sbinkertn@umich.edudef NextEthernetAddr():
6704380Sbinkertn@umich.edu    global _NextEthernetAddr
6713101Sstever@eecs.umich.edu
6724380Sbinkertn@umich.edu    value = _NextEthernetAddr
6734380Sbinkertn@umich.edu    _NextEthernetAddr = IncEthernetAddr(_NextEthernetAddr, 1)
6744380Sbinkertn@umich.edu    return value
6753101Sstever@eecs.umich.edu
6763101Sstever@eecs.umich.educlass EthernetAddr(ParamValue):
6773101Sstever@eecs.umich.edu    cxx_type = 'Net::EthAddr'
6787673Snate@binkert.org
6797673Snate@binkert.org    @classmethod
6807673Snate@binkert.org    def cxx_predecls(cls, code):
6817673Snate@binkert.org        code('#include "base/inet.hh"')
6827673Snate@binkert.org
6837673Snate@binkert.org    @classmethod
6847673Snate@binkert.org    def swig_predecls(cls, code):
6857673Snate@binkert.org        code('%include "python/swig/inet.i"')
6867673Snate@binkert.org
6873101Sstever@eecs.umich.edu    def __init__(self, value):
6883101Sstever@eecs.umich.edu        if value == NextEthernetAddr:
6893101Sstever@eecs.umich.edu            self.value = value
6903101Sstever@eecs.umich.edu            return
6913101Sstever@eecs.umich.edu
6923101Sstever@eecs.umich.edu        if not isinstance(value, str):
6933101Sstever@eecs.umich.edu            raise TypeError, "expected an ethernet address and didn't get one"
6943101Sstever@eecs.umich.edu
6953101Sstever@eecs.umich.edu        bytes = value.split(':')
6963101Sstever@eecs.umich.edu        if len(bytes) != 6:
6973101Sstever@eecs.umich.edu            raise TypeError, 'invalid ethernet address %s' % value
6983101Sstever@eecs.umich.edu
6993101Sstever@eecs.umich.edu        for byte in bytes:
7009941SGeoffrey.Blake@arm.com            if not 0 <= int(byte, base=16) <= 0xff:
7013101Sstever@eecs.umich.edu                raise TypeError, 'invalid ethernet address %s' % value
7023101Sstever@eecs.umich.edu
7033101Sstever@eecs.umich.edu        self.value = value
7043101Sstever@eecs.umich.edu
7053101Sstever@eecs.umich.edu    def unproxy(self, base):
7063101Sstever@eecs.umich.edu        if self.value == NextEthernetAddr:
7074380Sbinkertn@umich.edu            return EthernetAddr(self.value())
7083101Sstever@eecs.umich.edu        return self
7093101Sstever@eecs.umich.edu
7104762Snate@binkert.org    def getValue(self):
7117677Snate@binkert.org        from m5.internal.params import EthAddr
7124762Snate@binkert.org        return EthAddr(self.value)
7134762Snate@binkert.org
7144380Sbinkertn@umich.edu    def ini_str(self):
7154380Sbinkertn@umich.edu        return self.value
7163101Sstever@eecs.umich.edu
7177777Sgblack@eecs.umich.edu# When initializing an IpAddress, pass in an existing IpAddress, a string of
7187777Sgblack@eecs.umich.edu# the form "a.b.c.d", or an integer representing an IP.
7197777Sgblack@eecs.umich.educlass IpAddress(ParamValue):
7207777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpAddress'
7217777Sgblack@eecs.umich.edu
7227777Sgblack@eecs.umich.edu    @classmethod
7237777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
7247777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
7257777Sgblack@eecs.umich.edu
7267777Sgblack@eecs.umich.edu    @classmethod
7277777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
7287777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
7297777Sgblack@eecs.umich.edu
7307777Sgblack@eecs.umich.edu    def __init__(self, value):
7317777Sgblack@eecs.umich.edu        if isinstance(value, IpAddress):
7327777Sgblack@eecs.umich.edu            self.ip = value.ip
7337777Sgblack@eecs.umich.edu        else:
7347777Sgblack@eecs.umich.edu            try:
7357777Sgblack@eecs.umich.edu                self.ip = convert.toIpAddress(value)
7367777Sgblack@eecs.umich.edu            except TypeError:
7377777Sgblack@eecs.umich.edu                self.ip = long(value)
7387777Sgblack@eecs.umich.edu        self.verifyIp()
7397777Sgblack@eecs.umich.edu
7408579Ssteve.reinhardt@amd.com    def __str__(self):
7418579Ssteve.reinhardt@amd.com        tup = [(self.ip >> i)  & 0xff for i in (24, 16, 8, 0)]
7428579Ssteve.reinhardt@amd.com        return '%d.%d.%d.%d' % tuple(tup)
7438579Ssteve.reinhardt@amd.com
7448579Ssteve.reinhardt@amd.com    def __eq__(self, other):
7458579Ssteve.reinhardt@amd.com        if isinstance(other, IpAddress):
7468579Ssteve.reinhardt@amd.com            return self.ip == other.ip
7478579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
7488579Ssteve.reinhardt@amd.com            try:
7498579Ssteve.reinhardt@amd.com                return self.ip == convert.toIpAddress(other)
7508579Ssteve.reinhardt@amd.com            except:
7518579Ssteve.reinhardt@amd.com                return False
7528579Ssteve.reinhardt@amd.com        else:
7538579Ssteve.reinhardt@amd.com            return self.ip == other
7548579Ssteve.reinhardt@amd.com
7558579Ssteve.reinhardt@amd.com    def __ne__(self, other):
7568579Ssteve.reinhardt@amd.com        return not (self == other)
7578579Ssteve.reinhardt@amd.com
7587777Sgblack@eecs.umich.edu    def verifyIp(self):
7597777Sgblack@eecs.umich.edu        if self.ip < 0 or self.ip >= (1 << 32):
7607798Sgblack@eecs.umich.edu            raise TypeError, "invalid ip address %#08x" % self.ip
7617777Sgblack@eecs.umich.edu
7627777Sgblack@eecs.umich.edu    def getValue(self):
7637777Sgblack@eecs.umich.edu        from m5.internal.params import IpAddress
7647777Sgblack@eecs.umich.edu        return IpAddress(self.ip)
7657777Sgblack@eecs.umich.edu
7667777Sgblack@eecs.umich.edu# When initializing an IpNetmask, pass in an existing IpNetmask, a string of
7677777Sgblack@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
7687777Sgblack@eecs.umich.edu# positional or keyword arguments.
7697777Sgblack@eecs.umich.educlass IpNetmask(IpAddress):
7707777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpNetmask'
7717777Sgblack@eecs.umich.edu
7727777Sgblack@eecs.umich.edu    @classmethod
7737777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
7747777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
7757777Sgblack@eecs.umich.edu
7767777Sgblack@eecs.umich.edu    @classmethod
7777777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
7787777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
7797777Sgblack@eecs.umich.edu
7807777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
7817777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
7827777Sgblack@eecs.umich.edu            if key in kwargs:
7837777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
7847777Sgblack@eecs.umich.edu            elif elseVal:
7857777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
7867777Sgblack@eecs.umich.edu            else:
7877777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
7887777Sgblack@eecs.umich.edu
7897777Sgblack@eecs.umich.edu        if len(args) == 0:
7907777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
7917777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'netmask')
7927777Sgblack@eecs.umich.edu
7937777Sgblack@eecs.umich.edu        elif len(args) == 1:
7947777Sgblack@eecs.umich.edu            if kwargs:
7957777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'netmask' in kwargs:
7967777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
7977777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
7987777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'netmask', args[0])
7997777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpNetmask):
8007777Sgblack@eecs.umich.edu                self.ip = args[0].ip
8017777Sgblack@eecs.umich.edu                self.netmask = args[0].netmask
8027777Sgblack@eecs.umich.edu            else:
8037777Sgblack@eecs.umich.edu                (self.ip, self.netmask) = convert.toIpNetmask(args[0])
8047777Sgblack@eecs.umich.edu
8057777Sgblack@eecs.umich.edu        elif len(args) == 2:
8067777Sgblack@eecs.umich.edu            self.ip = args[0]
8077777Sgblack@eecs.umich.edu            self.netmask = args[1]
8087777Sgblack@eecs.umich.edu        else:
8097777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
8107777Sgblack@eecs.umich.edu
8117777Sgblack@eecs.umich.edu        if kwargs:
8127777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
8137777Sgblack@eecs.umich.edu
8147777Sgblack@eecs.umich.edu        self.verify()
8157777Sgblack@eecs.umich.edu
8168579Ssteve.reinhardt@amd.com    def __str__(self):
8178579Ssteve.reinhardt@amd.com        return "%s/%d" % (super(IpNetmask, self).__str__(), self.netmask)
8188579Ssteve.reinhardt@amd.com
8198579Ssteve.reinhardt@amd.com    def __eq__(self, other):
8208579Ssteve.reinhardt@amd.com        if isinstance(other, IpNetmask):
8218579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.netmask == other.netmask
8228579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
8238579Ssteve.reinhardt@amd.com            try:
8248579Ssteve.reinhardt@amd.com                return (self.ip, self.netmask) == convert.toIpNetmask(other)
8258579Ssteve.reinhardt@amd.com            except:
8268579Ssteve.reinhardt@amd.com                return False
8278579Ssteve.reinhardt@amd.com        else:
8288579Ssteve.reinhardt@amd.com            return False
8298579Ssteve.reinhardt@amd.com
8307777Sgblack@eecs.umich.edu    def verify(self):
8317777Sgblack@eecs.umich.edu        self.verifyIp()
8327777Sgblack@eecs.umich.edu        if self.netmask < 0 or self.netmask > 32:
8337777Sgblack@eecs.umich.edu            raise TypeError, "invalid netmask %d" % netmask
8347777Sgblack@eecs.umich.edu
8357777Sgblack@eecs.umich.edu    def getValue(self):
8367777Sgblack@eecs.umich.edu        from m5.internal.params import IpNetmask
8377777Sgblack@eecs.umich.edu        return IpNetmask(self.ip, self.netmask)
8387777Sgblack@eecs.umich.edu
8397777Sgblack@eecs.umich.edu# When initializing an IpWithPort, pass in an existing IpWithPort, a string of
8407777Sgblack@eecs.umich.edu# the form "a.b.c.d:p", or an ip and port as positional or keyword arguments.
8417777Sgblack@eecs.umich.educlass IpWithPort(IpAddress):
8427777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpWithPort'
8437777Sgblack@eecs.umich.edu
8447777Sgblack@eecs.umich.edu    @classmethod
8457777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
8467777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
8477777Sgblack@eecs.umich.edu
8487777Sgblack@eecs.umich.edu    @classmethod
8497777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
8507777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
8517777Sgblack@eecs.umich.edu
8527777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
8537777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
8547777Sgblack@eecs.umich.edu            if key in kwargs:
8557777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
8567777Sgblack@eecs.umich.edu            elif elseVal:
8577777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
8587777Sgblack@eecs.umich.edu            else:
8597777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
8607777Sgblack@eecs.umich.edu
8617777Sgblack@eecs.umich.edu        if len(args) == 0:
8627777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
8637777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'port')
8647777Sgblack@eecs.umich.edu
8657777Sgblack@eecs.umich.edu        elif len(args) == 1:
8667777Sgblack@eecs.umich.edu            if kwargs:
8677777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'port' in kwargs:
8687777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
8697777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
8707777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'port', args[0])
8717777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpWithPort):
8727777Sgblack@eecs.umich.edu                self.ip = args[0].ip
8737777Sgblack@eecs.umich.edu                self.port = args[0].port
8747777Sgblack@eecs.umich.edu            else:
8757777Sgblack@eecs.umich.edu                (self.ip, self.port) = convert.toIpWithPort(args[0])
8767777Sgblack@eecs.umich.edu
8777777Sgblack@eecs.umich.edu        elif len(args) == 2:
8787777Sgblack@eecs.umich.edu            self.ip = args[0]
8797777Sgblack@eecs.umich.edu            self.port = args[1]
8807777Sgblack@eecs.umich.edu        else:
8817777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
8827777Sgblack@eecs.umich.edu
8837777Sgblack@eecs.umich.edu        if kwargs:
8847777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
8857777Sgblack@eecs.umich.edu
8867777Sgblack@eecs.umich.edu        self.verify()
8877777Sgblack@eecs.umich.edu
8888579Ssteve.reinhardt@amd.com    def __str__(self):
8898579Ssteve.reinhardt@amd.com        return "%s:%d" % (super(IpWithPort, self).__str__(), self.port)
8908579Ssteve.reinhardt@amd.com
8918579Ssteve.reinhardt@amd.com    def __eq__(self, other):
8928579Ssteve.reinhardt@amd.com        if isinstance(other, IpWithPort):
8938579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.port == other.port
8948579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
8958579Ssteve.reinhardt@amd.com            try:
8968579Ssteve.reinhardt@amd.com                return (self.ip, self.port) == convert.toIpWithPort(other)
8978579Ssteve.reinhardt@amd.com            except:
8988579Ssteve.reinhardt@amd.com                return False
8998579Ssteve.reinhardt@amd.com        else:
9008579Ssteve.reinhardt@amd.com            return False
9018579Ssteve.reinhardt@amd.com
9027777Sgblack@eecs.umich.edu    def verify(self):
9037777Sgblack@eecs.umich.edu        self.verifyIp()
9047777Sgblack@eecs.umich.edu        if self.port < 0 or self.port > 0xffff:
9057777Sgblack@eecs.umich.edu            raise TypeError, "invalid port %d" % self.port
9067777Sgblack@eecs.umich.edu
9077777Sgblack@eecs.umich.edu    def getValue(self):
9087777Sgblack@eecs.umich.edu        from m5.internal.params import IpWithPort
9097777Sgblack@eecs.umich.edu        return IpWithPort(self.ip, self.port)
9107777Sgblack@eecs.umich.edu
9113932Sbinkertn@umich.edutime_formats = [ "%a %b %d %H:%M:%S %Z %Y",
9123932Sbinkertn@umich.edu                 "%a %b %d %H:%M:%S %Z %Y",
9133932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M:%S",
9143932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M",
9153932Sbinkertn@umich.edu                 "%Y/%m/%d",
9163932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M:%S",
9173932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M",
9183932Sbinkertn@umich.edu                 "%m/%d/%Y",
9193932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M:%S",
9203932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M",
9213932Sbinkertn@umich.edu                 "%m/%d/%y"]
9223932Sbinkertn@umich.edu
9233932Sbinkertn@umich.edu
9243885Sbinkertn@umich.edudef parse_time(value):
9253932Sbinkertn@umich.edu    from time import gmtime, strptime, struct_time, time
9263932Sbinkertn@umich.edu    from datetime import datetime, date
9273885Sbinkertn@umich.edu
9283932Sbinkertn@umich.edu    if isinstance(value, struct_time):
9293932Sbinkertn@umich.edu        return value
9303932Sbinkertn@umich.edu
9313932Sbinkertn@umich.edu    if isinstance(value, (int, long)):
9323932Sbinkertn@umich.edu        return gmtime(value)
9333932Sbinkertn@umich.edu
9343932Sbinkertn@umich.edu    if isinstance(value, (datetime, date)):
9353932Sbinkertn@umich.edu        return value.timetuple()
9363932Sbinkertn@umich.edu
9373932Sbinkertn@umich.edu    if isinstance(value, str):
9383932Sbinkertn@umich.edu        if value in ('Now', 'Today'):
9393932Sbinkertn@umich.edu            return time.gmtime(time.time())
9403932Sbinkertn@umich.edu
9413932Sbinkertn@umich.edu        for format in time_formats:
9423932Sbinkertn@umich.edu            try:
9433932Sbinkertn@umich.edu                return strptime(value, format)
9443932Sbinkertn@umich.edu            except ValueError:
9453932Sbinkertn@umich.edu                pass
9463885Sbinkertn@umich.edu
9473885Sbinkertn@umich.edu    raise ValueError, "Could not parse '%s' as a time" % value
9483885Sbinkertn@umich.edu
9493885Sbinkertn@umich.educlass Time(ParamValue):
9504762Snate@binkert.org    cxx_type = 'tm'
9517673Snate@binkert.org
9527673Snate@binkert.org    @classmethod
9537673Snate@binkert.org    def cxx_predecls(cls, code):
9547673Snate@binkert.org        code('#include <time.h>')
9557673Snate@binkert.org
9567673Snate@binkert.org    @classmethod
9577673Snate@binkert.org    def swig_predecls(cls, code):
9587673Snate@binkert.org        code('%include "python/swig/time.i"')
9597673Snate@binkert.org
9603885Sbinkertn@umich.edu    def __init__(self, value):
9613932Sbinkertn@umich.edu        self.value = parse_time(value)
9623885Sbinkertn@umich.edu
9634762Snate@binkert.org    def getValue(self):
9647677Snate@binkert.org        from m5.internal.params import tm
9654762Snate@binkert.org
9664762Snate@binkert.org        c_time = tm()
9674762Snate@binkert.org        py_time = self.value
9684762Snate@binkert.org
9694762Snate@binkert.org        # UNIX is years since 1900
9704762Snate@binkert.org        c_time.tm_year = py_time.tm_year - 1900;
9714762Snate@binkert.org
9724762Snate@binkert.org        # Python starts at 1, UNIX starts at 0
9734762Snate@binkert.org        c_time.tm_mon =  py_time.tm_mon - 1;
9744762Snate@binkert.org        c_time.tm_mday = py_time.tm_mday;
9754762Snate@binkert.org        c_time.tm_hour = py_time.tm_hour;
9764762Snate@binkert.org        c_time.tm_min = py_time.tm_min;
9774762Snate@binkert.org        c_time.tm_sec = py_time.tm_sec;
9784762Snate@binkert.org
9794762Snate@binkert.org        # Python has 0 as Monday, UNIX is 0 as sunday
9804762Snate@binkert.org        c_time.tm_wday = py_time.tm_wday + 1
9814762Snate@binkert.org        if c_time.tm_wday > 6:
9824762Snate@binkert.org            c_time.tm_wday -= 7;
9834762Snate@binkert.org
9844762Snate@binkert.org        # Python starts at 1, Unix starts at 0
9854762Snate@binkert.org        c_time.tm_yday = py_time.tm_yday - 1;
9864762Snate@binkert.org
9874762Snate@binkert.org        return c_time
9884762Snate@binkert.org
9893885Sbinkertn@umich.edu    def __str__(self):
9904762Snate@binkert.org        return time.asctime(self.value)
9913885Sbinkertn@umich.edu
9923885Sbinkertn@umich.edu    def ini_str(self):
9933932Sbinkertn@umich.edu        return str(self)
9943885Sbinkertn@umich.edu
9958664SAli.Saidi@ARM.com    def get_config_as_dict(self):
9968664SAli.Saidi@ARM.com        return str(self)
9978664SAli.Saidi@ARM.com
9983101Sstever@eecs.umich.edu# Enumerated types are a little more complex.  The user specifies the
9993101Sstever@eecs.umich.edu# type as Enum(foo) where foo is either a list or dictionary of
10003101Sstever@eecs.umich.edu# alternatives (typically strings, but not necessarily so).  (In the
10013101Sstever@eecs.umich.edu# long run, the integer value of the parameter will be the list index
10023101Sstever@eecs.umich.edu# or the corresponding dictionary value.  For now, since we only check
10033101Sstever@eecs.umich.edu# that the alternative is valid and then spit it into a .ini file,
10043101Sstever@eecs.umich.edu# there's not much point in using the dictionary.)
10053101Sstever@eecs.umich.edu
10063101Sstever@eecs.umich.edu# What Enum() must do is generate a new type encapsulating the
10073101Sstever@eecs.umich.edu# provided list/dictionary so that specific values of the parameter
10083101Sstever@eecs.umich.edu# can be instances of that type.  We define two hidden internal
10093101Sstever@eecs.umich.edu# classes (_ListEnum and _DictEnum) to serve as base classes, then
10103101Sstever@eecs.umich.edu# derive the new type from the appropriate base class on the fly.
10113101Sstever@eecs.umich.edu
10124762Snate@binkert.orgallEnums = {}
10133101Sstever@eecs.umich.edu# Metaclass for Enum types
10145033Smilesck@eecs.umich.educlass MetaEnum(MetaParamValue):
10154762Snate@binkert.org    def __new__(mcls, name, bases, dict):
10164762Snate@binkert.org        assert name not in allEnums
10174762Snate@binkert.org
10184762Snate@binkert.org        cls = super(MetaEnum, mcls).__new__(mcls, name, bases, dict)
10194762Snate@binkert.org        allEnums[name] = cls
10204762Snate@binkert.org        return cls
10214762Snate@binkert.org
10223101Sstever@eecs.umich.edu    def __init__(cls, name, bases, init_dict):
10233101Sstever@eecs.umich.edu        if init_dict.has_key('map'):
10243101Sstever@eecs.umich.edu            if not isinstance(cls.map, dict):
10253101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'map' " \
10263101Sstever@eecs.umich.edu                      "must be of type dict"
10273101Sstever@eecs.umich.edu            # build list of value strings from map
10283101Sstever@eecs.umich.edu            cls.vals = cls.map.keys()
10293101Sstever@eecs.umich.edu            cls.vals.sort()
10303101Sstever@eecs.umich.edu        elif init_dict.has_key('vals'):
10313101Sstever@eecs.umich.edu            if not isinstance(cls.vals, list):
10323101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'vals' " \
10333101Sstever@eecs.umich.edu                      "must be of type list"
10343101Sstever@eecs.umich.edu            # build string->value map from vals sequence
10353101Sstever@eecs.umich.edu            cls.map = {}
10363101Sstever@eecs.umich.edu            for idx,val in enumerate(cls.vals):
10373101Sstever@eecs.umich.edu                cls.map[val] = idx
10383101Sstever@eecs.umich.edu        else:
10393101Sstever@eecs.umich.edu            raise TypeError, "Enum-derived class must define "\
10403101Sstever@eecs.umich.edu                  "attribute 'map' or 'vals'"
10413101Sstever@eecs.umich.edu
10424762Snate@binkert.org        cls.cxx_type = 'Enums::%s' % name
10433101Sstever@eecs.umich.edu
10443101Sstever@eecs.umich.edu        super(MetaEnum, cls).__init__(name, bases, init_dict)
10453101Sstever@eecs.umich.edu
10463101Sstever@eecs.umich.edu    # Generate C++ class declaration for this enum type.
10473101Sstever@eecs.umich.edu    # Note that we wrap the enum in a class/struct to act as a namespace,
10483101Sstever@eecs.umich.edu    # so that the enum strings can be brief w/o worrying about collisions.
10497673Snate@binkert.org    def cxx_decl(cls, code):
10506654Snate@binkert.org        name = cls.__name__
10517673Snate@binkert.org        code('''\
10527673Snate@binkert.org#ifndef __ENUM__${name}__
10537673Snate@binkert.org#define __ENUM__${name}__
10547673Snate@binkert.org
10557673Snate@binkert.orgnamespace Enums {
10567673Snate@binkert.org    enum $name {
10577673Snate@binkert.org''')
10587673Snate@binkert.org        code.indent(2)
10594762Snate@binkert.org        for val in cls.vals:
10607673Snate@binkert.org            code('$val = ${{cls.map[val]}},')
10618902Sandreas.hansson@arm.com        code('Num_$name = ${{len(cls.vals)}}')
10627673Snate@binkert.org        code.dedent(2)
10637673Snate@binkert.org        code('''\
10647673Snate@binkert.org    };
10657673Snate@binkert.orgextern const char *${name}Strings[Num_${name}];
10667673Snate@binkert.org}
10674762Snate@binkert.org
10687673Snate@binkert.org#endif // __ENUM__${name}__
10697673Snate@binkert.org''')
10707673Snate@binkert.org
10717673Snate@binkert.org    def cxx_def(cls, code):
10726654Snate@binkert.org        name = cls.__name__
10737673Snate@binkert.org        code('''\
10747677Snate@binkert.org#include "enums/$name.hh"
10757673Snate@binkert.orgnamespace Enums {
10767673Snate@binkert.org    const char *${name}Strings[Num_${name}] =
10777673Snate@binkert.org    {
10787673Snate@binkert.org''')
10797673Snate@binkert.org        code.indent(2)
10804762Snate@binkert.org        for val in cls.vals:
10817673Snate@binkert.org            code('"$val",')
10827673Snate@binkert.org        code.dedent(2)
10837673Snate@binkert.org        code('''
10847673Snate@binkert.org    };
10857811Ssteve.reinhardt@amd.com} // namespace Enums
10867673Snate@binkert.org''')
10873101Sstever@eecs.umich.edu
10888596Ssteve.reinhardt@amd.com    def swig_decl(cls, code):
10898596Ssteve.reinhardt@amd.com        name = cls.__name__
10908596Ssteve.reinhardt@amd.com        code('''\
10918596Ssteve.reinhardt@amd.com%module(package="m5.internal") enum_$name
10928596Ssteve.reinhardt@amd.com
10938596Ssteve.reinhardt@amd.com%{
10948596Ssteve.reinhardt@amd.com#include "enums/$name.hh"
10958596Ssteve.reinhardt@amd.com%}
10968596Ssteve.reinhardt@amd.com
10978596Ssteve.reinhardt@amd.com%include "enums/$name.hh"
10988596Ssteve.reinhardt@amd.com''')
10998596Ssteve.reinhardt@amd.com
11008596Ssteve.reinhardt@amd.com
11013101Sstever@eecs.umich.edu# Base class for enum types.
11023101Sstever@eecs.umich.educlass Enum(ParamValue):
11033101Sstever@eecs.umich.edu    __metaclass__ = MetaEnum
11043101Sstever@eecs.umich.edu    vals = []
11053101Sstever@eecs.umich.edu
11063101Sstever@eecs.umich.edu    def __init__(self, value):
11073101Sstever@eecs.umich.edu        if value not in self.map:
11083101Sstever@eecs.umich.edu            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
11093101Sstever@eecs.umich.edu                  % (value, self.vals)
11103101Sstever@eecs.umich.edu        self.value = value
11113101Sstever@eecs.umich.edu
11127675Snate@binkert.org    @classmethod
11137675Snate@binkert.org    def cxx_predecls(cls, code):
11147675Snate@binkert.org        code('#include "enums/$0.hh"', cls.__name__)
11157675Snate@binkert.org
11167675Snate@binkert.org    @classmethod
11177675Snate@binkert.org    def swig_predecls(cls, code):
11187677Snate@binkert.org        code('%import "python/m5/internal/enum_$0.i"', cls.__name__)
11197675Snate@binkert.org
11204762Snate@binkert.org    def getValue(self):
11214762Snate@binkert.org        return int(self.map[self.value])
11224762Snate@binkert.org
11233101Sstever@eecs.umich.edu    def __str__(self):
11243101Sstever@eecs.umich.edu        return self.value
11253101Sstever@eecs.umich.edu
11263101Sstever@eecs.umich.edu# how big does a rounding error need to be before we warn about it?
11273101Sstever@eecs.umich.edufrequency_tolerance = 0.001  # 0.1%
11283101Sstever@eecs.umich.edu
11294167Sbinkertn@umich.educlass TickParamValue(NumericParamValue):
11303101Sstever@eecs.umich.edu    cxx_type = 'Tick'
11317673Snate@binkert.org
11327673Snate@binkert.org    @classmethod
11337673Snate@binkert.org    def cxx_predecls(cls, code):
11347673Snate@binkert.org        code('#include "base/types.hh"')
11357673Snate@binkert.org
11367673Snate@binkert.org    @classmethod
11377673Snate@binkert.org    def swig_predecls(cls, code):
11387673Snate@binkert.org        code('%import "stdint.i"')
11397673Snate@binkert.org        code('%import "base/types.hh"')
11404167Sbinkertn@umich.edu
11414762Snate@binkert.org    def getValue(self):
11424762Snate@binkert.org        return long(self.value)
11434762Snate@binkert.org
11444167Sbinkertn@umich.educlass Latency(TickParamValue):
11453101Sstever@eecs.umich.edu    def __init__(self, value):
11464167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11474167Sbinkertn@umich.edu            self.ticks = value.ticks
11484167Sbinkertn@umich.edu            self.value = value.value
11494167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11504167Sbinkertn@umich.edu            self.ticks = value.ticks
11514167Sbinkertn@umich.edu            self.value = 1.0 / value.value
11524167Sbinkertn@umich.edu        elif value.endswith('t'):
11534167Sbinkertn@umich.edu            self.ticks = True
11544167Sbinkertn@umich.edu            self.value = int(value[:-1])
11554167Sbinkertn@umich.edu        else:
11564167Sbinkertn@umich.edu            self.ticks = False
11574167Sbinkertn@umich.edu            self.value = convert.toLatency(value)
11583101Sstever@eecs.umich.edu
11593101Sstever@eecs.umich.edu    def __getattr__(self, attr):
11603101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
11613101Sstever@eecs.umich.edu            return self
11623101Sstever@eecs.umich.edu        if attr == 'frequency':
11633101Sstever@eecs.umich.edu            return Frequency(self)
11643101Sstever@eecs.umich.edu        raise AttributeError, "Latency object has no attribute '%s'" % attr
11653101Sstever@eecs.umich.edu
11664762Snate@binkert.org    def getValue(self):
11674762Snate@binkert.org        if self.ticks or self.value == 0:
11684762Snate@binkert.org            value = self.value
11694762Snate@binkert.org        else:
11704762Snate@binkert.org            value = ticks.fromSeconds(self.value)
11714762Snate@binkert.org        return long(value)
11724762Snate@binkert.org
11733101Sstever@eecs.umich.edu    # convert latency to ticks
11743101Sstever@eecs.umich.edu    def ini_str(self):
11754762Snate@binkert.org        return '%d' % self.getValue()
11763101Sstever@eecs.umich.edu
11774167Sbinkertn@umich.educlass Frequency(TickParamValue):
11783101Sstever@eecs.umich.edu    def __init__(self, value):
11794167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
11804167Sbinkertn@umich.edu            if value.value == 0:
11814167Sbinkertn@umich.edu                self.value = 0
11824167Sbinkertn@umich.edu            else:
11834167Sbinkertn@umich.edu                self.value = 1.0 / value.value
11844167Sbinkertn@umich.edu            self.ticks = value.ticks
11854167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
11864167Sbinkertn@umich.edu            self.value = value.value
11874167Sbinkertn@umich.edu            self.ticks = value.ticks
11884167Sbinkertn@umich.edu        else:
11894167Sbinkertn@umich.edu            self.ticks = False
11904167Sbinkertn@umich.edu            self.value = convert.toFrequency(value)
11913101Sstever@eecs.umich.edu
11923101Sstever@eecs.umich.edu    def __getattr__(self, attr):
11933101Sstever@eecs.umich.edu        if attr == 'frequency':
11943101Sstever@eecs.umich.edu            return self
11953101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
11963101Sstever@eecs.umich.edu            return Latency(self)
11973101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
11983101Sstever@eecs.umich.edu
11994167Sbinkertn@umich.edu    # convert latency to ticks
12004762Snate@binkert.org    def getValue(self):
12014762Snate@binkert.org        if self.ticks or self.value == 0:
12024762Snate@binkert.org            value = self.value
12034762Snate@binkert.org        else:
12044762Snate@binkert.org            value = ticks.fromSeconds(1.0 / self.value)
12054762Snate@binkert.org        return long(value)
12064762Snate@binkert.org
12073101Sstever@eecs.umich.edu    def ini_str(self):
12084762Snate@binkert.org        return '%d' % self.getValue()
12093101Sstever@eecs.umich.edu
12109544Sandreas.hansson@arm.com# A generic frequency and/or Latency value. Value is stored as a
12119544Sandreas.hansson@arm.com# latency, and any manipulation using a multiplier thus scales the
12129544Sandreas.hansson@arm.com# clock period, i.e. a 2x multiplier doubles the clock period and thus
12139544Sandreas.hansson@arm.com# halves the clock frequency.
12143101Sstever@eecs.umich.educlass Clock(ParamValue):
12153101Sstever@eecs.umich.edu    cxx_type = 'Tick'
12167673Snate@binkert.org
12177673Snate@binkert.org    @classmethod
12187673Snate@binkert.org    def cxx_predecls(cls, code):
12197673Snate@binkert.org        code('#include "base/types.hh"')
12207673Snate@binkert.org
12217673Snate@binkert.org    @classmethod
12227673Snate@binkert.org    def swig_predecls(cls, code):
12237673Snate@binkert.org        code('%import "stdint.i"')
12247673Snate@binkert.org        code('%import "base/types.hh"')
12257673Snate@binkert.org
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