params.py revision 11988
111988Sandreas.sandberg@arm.com# Copyright (c) 2012-2014, 2017 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
9610267SGeoffrey.Blake@arm.com    cmd_line_settable = False
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
10511988Sandreas.sandberg@arm.com    @classmethod
10611988Sandreas.sandberg@arm.com    def pybind_predecls(cls, code):
10711988Sandreas.sandberg@arm.com        cls.cxx_predecls(code)
10811988Sandreas.sandberg@arm.com
1098596Ssteve.reinhardt@amd.com    # Generate the code needed as a prerequisite for including a
1108596Ssteve.reinhardt@amd.com    # reference to a C++ object of this type in a SWIG .i file.
1118596Ssteve.reinhardt@amd.com    # Typically generates one or more %import or %include statements.
1127673Snate@binkert.org    @classmethod
1137673Snate@binkert.org    def swig_predecls(cls, code):
1147673Snate@binkert.org        pass
1153101Sstever@eecs.umich.edu
1163101Sstever@eecs.umich.edu    # default for printing to .ini file is regular string conversion.
1173101Sstever@eecs.umich.edu    # will be overridden in some cases
1183101Sstever@eecs.umich.edu    def ini_str(self):
1193101Sstever@eecs.umich.edu        return str(self)
1203101Sstever@eecs.umich.edu
12110380SAndrew.Bardsley@arm.com    # default for printing to .json file is regular string conversion.
12210380SAndrew.Bardsley@arm.com    # will be overridden in some cases, mostly to use native Python
12310380SAndrew.Bardsley@arm.com    # types where there are similar JSON types
12410380SAndrew.Bardsley@arm.com    def config_value(self):
12510380SAndrew.Bardsley@arm.com        return str(self)
12610380SAndrew.Bardsley@arm.com
12710458Sandreas.hansson@arm.com    # Prerequisites for .ini parsing with cxx_ini_parse
12810458Sandreas.hansson@arm.com    @classmethod
12910458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
13010458Sandreas.hansson@arm.com        pass
13110458Sandreas.hansson@arm.com
13210458Sandreas.hansson@arm.com    # parse a .ini file entry for this param from string expression
13310458Sandreas.hansson@arm.com    # src into lvalue dest (of the param's C++ type)
13410458Sandreas.hansson@arm.com    @classmethod
13510458Sandreas.hansson@arm.com    def cxx_ini_parse(cls, code, src, dest, ret):
13610458Sandreas.hansson@arm.com        code('// Unhandled param type: %s' % cls.__name__)
13710458Sandreas.hansson@arm.com        code('%s false;' % ret)
13810458Sandreas.hansson@arm.com
1393101Sstever@eecs.umich.edu    # allows us to blithely call unproxy() on things without checking
1403101Sstever@eecs.umich.edu    # if they're really proxies or not
1413101Sstever@eecs.umich.edu    def unproxy(self, base):
1423101Sstever@eecs.umich.edu        return self
1433101Sstever@eecs.umich.edu
14410267SGeoffrey.Blake@arm.com    # Produce a human readable version of the stored value
14510267SGeoffrey.Blake@arm.com    def pretty_print(self, value):
14610267SGeoffrey.Blake@arm.com        return str(value)
14710267SGeoffrey.Blake@arm.com
1483101Sstever@eecs.umich.edu# Regular parameter description.
1493101Sstever@eecs.umich.educlass ParamDesc(object):
1503101Sstever@eecs.umich.edu    def __init__(self, ptype_str, ptype, *args, **kwargs):
1513101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
1523101Sstever@eecs.umich.edu        # remember ptype only if it is provided
1533101Sstever@eecs.umich.edu        if ptype != None:
1543101Sstever@eecs.umich.edu            self.ptype = ptype
1553101Sstever@eecs.umich.edu
1563101Sstever@eecs.umich.edu        if args:
1573101Sstever@eecs.umich.edu            if len(args) == 1:
1583101Sstever@eecs.umich.edu                self.desc = args[0]
1593101Sstever@eecs.umich.edu            elif len(args) == 2:
1603101Sstever@eecs.umich.edu                self.default = args[0]
1613101Sstever@eecs.umich.edu                self.desc = args[1]
1623101Sstever@eecs.umich.edu            else:
1633101Sstever@eecs.umich.edu                raise TypeError, 'too many arguments'
1643101Sstever@eecs.umich.edu
1653101Sstever@eecs.umich.edu        if kwargs.has_key('desc'):
1663101Sstever@eecs.umich.edu            assert(not hasattr(self, 'desc'))
1673101Sstever@eecs.umich.edu            self.desc = kwargs['desc']
1683101Sstever@eecs.umich.edu            del kwargs['desc']
1693101Sstever@eecs.umich.edu
1703101Sstever@eecs.umich.edu        if kwargs.has_key('default'):
1713101Sstever@eecs.umich.edu            assert(not hasattr(self, 'default'))
1723101Sstever@eecs.umich.edu            self.default = kwargs['default']
1733101Sstever@eecs.umich.edu            del kwargs['default']
1743101Sstever@eecs.umich.edu
1753101Sstever@eecs.umich.edu        if kwargs:
1763101Sstever@eecs.umich.edu            raise TypeError, 'extra unknown kwargs %s' % kwargs
1773101Sstever@eecs.umich.edu
1783101Sstever@eecs.umich.edu        if not hasattr(self, 'desc'):
1793101Sstever@eecs.umich.edu            raise TypeError, 'desc attribute missing'
1803101Sstever@eecs.umich.edu
1813101Sstever@eecs.umich.edu    def __getattr__(self, attr):
1823101Sstever@eecs.umich.edu        if attr == 'ptype':
1835033Smilesck@eecs.umich.edu            ptype = SimObject.allClasses[self.ptype_str]
1846656Snate@binkert.org            assert isSimObjectClass(ptype)
1855033Smilesck@eecs.umich.edu            self.ptype = ptype
1865033Smilesck@eecs.umich.edu            return ptype
1875033Smilesck@eecs.umich.edu
1883101Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
1893101Sstever@eecs.umich.edu              (type(self).__name__, attr)
1903101Sstever@eecs.umich.edu
19110267SGeoffrey.Blake@arm.com    def example_str(self):
19210267SGeoffrey.Blake@arm.com        if hasattr(self.ptype, "ex_str"):
19310267SGeoffrey.Blake@arm.com            return self.ptype.ex_str
19410267SGeoffrey.Blake@arm.com        else:
19510267SGeoffrey.Blake@arm.com            return self.ptype_str
19610267SGeoffrey.Blake@arm.com
19710267SGeoffrey.Blake@arm.com    # Is the param available to be exposed on the command line
19810267SGeoffrey.Blake@arm.com    def isCmdLineSettable(self):
19910267SGeoffrey.Blake@arm.com        if hasattr(self.ptype, "cmd_line_settable"):
20010267SGeoffrey.Blake@arm.com            return self.ptype.cmd_line_settable
20110267SGeoffrey.Blake@arm.com        else:
20210267SGeoffrey.Blake@arm.com            return False
20310267SGeoffrey.Blake@arm.com
2043101Sstever@eecs.umich.edu    def convert(self, value):
2053101Sstever@eecs.umich.edu        if isinstance(value, proxy.BaseProxy):
2063101Sstever@eecs.umich.edu            value.set_param_desc(self)
2073101Sstever@eecs.umich.edu            return value
2083101Sstever@eecs.umich.edu        if not hasattr(self, 'ptype') and isNullPointer(value):
2093101Sstever@eecs.umich.edu            # deferred evaluation of SimObject; continue to defer if
2103101Sstever@eecs.umich.edu            # we're just assigning a null pointer
2113101Sstever@eecs.umich.edu            return value
2123101Sstever@eecs.umich.edu        if isinstance(value, self.ptype):
2133101Sstever@eecs.umich.edu            return value
2143102Sstever@eecs.umich.edu        if isNullPointer(value) and isSimObjectClass(self.ptype):
2153101Sstever@eecs.umich.edu            return value
2163101Sstever@eecs.umich.edu        return self.ptype(value)
2173101Sstever@eecs.umich.edu
21810267SGeoffrey.Blake@arm.com    def pretty_print(self, value):
21910267SGeoffrey.Blake@arm.com        if isinstance(value, proxy.BaseProxy):
22010267SGeoffrey.Blake@arm.com           return str(value)
22110267SGeoffrey.Blake@arm.com        if isNullPointer(value):
22210267SGeoffrey.Blake@arm.com           return NULL
22310267SGeoffrey.Blake@arm.com        return self.ptype(value).pretty_print(value)
22410267SGeoffrey.Blake@arm.com
2257673Snate@binkert.org    def cxx_predecls(self, code):
2268607Sgblack@eecs.umich.edu        code('#include <cstddef>')
2277673Snate@binkert.org        self.ptype.cxx_predecls(code)
2283101Sstever@eecs.umich.edu
22911988Sandreas.sandberg@arm.com    def pybind_predecls(self, code):
23011988Sandreas.sandberg@arm.com        self.ptype.pybind_predecls(code)
23111988Sandreas.sandberg@arm.com
2327673Snate@binkert.org    def swig_predecls(self, code):
2337673Snate@binkert.org        self.ptype.swig_predecls(code)
2343101Sstever@eecs.umich.edu
2357673Snate@binkert.org    def cxx_decl(self, code):
2367673Snate@binkert.org        code('${{self.ptype.cxx_type}} ${{self.name}};')
2373101Sstever@eecs.umich.edu
2383101Sstever@eecs.umich.edu# Vector-valued parameter description.  Just like ParamDesc, except
2393101Sstever@eecs.umich.edu# that the value is a vector (list) of the specified type instead of a
2403101Sstever@eecs.umich.edu# single value.
2413101Sstever@eecs.umich.edu
2423101Sstever@eecs.umich.educlass VectorParamValue(list):
2435033Smilesck@eecs.umich.edu    __metaclass__ = MetaParamValue
2445475Snate@binkert.org    def __setattr__(self, attr, value):
2455475Snate@binkert.org        raise AttributeError, \
2465475Snate@binkert.org              "Not allowed to set %s on '%s'" % (attr, type(self).__name__)
2475475Snate@binkert.org
24810380SAndrew.Bardsley@arm.com    def config_value(self):
24910380SAndrew.Bardsley@arm.com        return [v.config_value() for v in self]
25010380SAndrew.Bardsley@arm.com
2513101Sstever@eecs.umich.edu    def ini_str(self):
2523101Sstever@eecs.umich.edu        return ' '.join([v.ini_str() for v in self])
2533101Sstever@eecs.umich.edu
2544762Snate@binkert.org    def getValue(self):
2554762Snate@binkert.org        return [ v.getValue() for v in self ]
2564762Snate@binkert.org
2573101Sstever@eecs.umich.edu    def unproxy(self, base):
2588460SAli.Saidi@ARM.com        if len(self) == 1 and isinstance(self[0], proxy.AllProxy):
2598459SAli.Saidi@ARM.com            return self[0].unproxy(base)
2608459SAli.Saidi@ARM.com        else:
2618459SAli.Saidi@ARM.com             return [v.unproxy(base) for v in self]
2623101Sstever@eecs.umich.edu
2637528Ssteve.reinhardt@amd.comclass SimObjectVector(VectorParamValue):
2647528Ssteve.reinhardt@amd.com    # support clone operation
2657528Ssteve.reinhardt@amd.com    def __call__(self, **kwargs):
2667528Ssteve.reinhardt@amd.com        return SimObjectVector([v(**kwargs) for v in self])
2677528Ssteve.reinhardt@amd.com
2687528Ssteve.reinhardt@amd.com    def clear_parent(self, old_parent):
2693101Sstever@eecs.umich.edu        for v in self:
2707528Ssteve.reinhardt@amd.com            v.clear_parent(old_parent)
2717528Ssteve.reinhardt@amd.com
2727528Ssteve.reinhardt@amd.com    def set_parent(self, parent, name):
2737528Ssteve.reinhardt@amd.com        if len(self) == 1:
2747528Ssteve.reinhardt@amd.com            self[0].set_parent(parent, name)
2757528Ssteve.reinhardt@amd.com        else:
2767528Ssteve.reinhardt@amd.com            width = int(math.ceil(math.log(len(self))/math.log(10)))
2777528Ssteve.reinhardt@amd.com            for i,v in enumerate(self):
2787528Ssteve.reinhardt@amd.com                v.set_parent(parent, "%s%0*d" % (name, width, i))
2797528Ssteve.reinhardt@amd.com
2808321Ssteve.reinhardt@amd.com    def has_parent(self):
2818321Ssteve.reinhardt@amd.com        return reduce(lambda x,y: x and y, [v.has_parent() for v in self])
2827528Ssteve.reinhardt@amd.com
2837528Ssteve.reinhardt@amd.com    # return 'cpu0 cpu1' etc. for print_ini()
2847528Ssteve.reinhardt@amd.com    def get_name(self):
2857528Ssteve.reinhardt@amd.com        return ' '.join([v._name for v in self])
2867528Ssteve.reinhardt@amd.com
2877528Ssteve.reinhardt@amd.com    # By iterating through the constituent members of the vector here
2887528Ssteve.reinhardt@amd.com    # we can nicely handle iterating over all a SimObject's children
2897528Ssteve.reinhardt@amd.com    # without having to provide lots of special functions on
2907528Ssteve.reinhardt@amd.com    # SimObjectVector directly.
2917528Ssteve.reinhardt@amd.com    def descendants(self):
2927528Ssteve.reinhardt@amd.com        for v in self:
2937528Ssteve.reinhardt@amd.com            for obj in v.descendants():
2947528Ssteve.reinhardt@amd.com                yield obj
2953101Sstever@eecs.umich.edu
2968664SAli.Saidi@ARM.com    def get_config_as_dict(self):
2978664SAli.Saidi@ARM.com        a = []
2988664SAli.Saidi@ARM.com        for v in self:
2998664SAli.Saidi@ARM.com            a.append(v.get_config_as_dict())
3008664SAli.Saidi@ARM.com        return a
3018664SAli.Saidi@ARM.com
3029953Sgeoffrey.blake@arm.com    # If we are replacing an item in the vector, make sure to set the
3039953Sgeoffrey.blake@arm.com    # parent reference of the new SimObject to be the same as the parent
3049953Sgeoffrey.blake@arm.com    # of the SimObject being replaced. Useful to have if we created
3059953Sgeoffrey.blake@arm.com    # a SimObjectVector of temporary objects that will be modified later in
3069953Sgeoffrey.blake@arm.com    # configuration scripts.
3079953Sgeoffrey.blake@arm.com    def __setitem__(self, key, value):
3089953Sgeoffrey.blake@arm.com        val = self[key]
3099953Sgeoffrey.blake@arm.com        if value.has_parent():
3109953Sgeoffrey.blake@arm.com            warn("SimObject %s already has a parent" % value.get_name() +\
3119953Sgeoffrey.blake@arm.com                 " that is being overwritten by a SimObjectVector")
3129953Sgeoffrey.blake@arm.com        value.set_parent(val.get_parent(), val._name)
3139953Sgeoffrey.blake@arm.com        super(SimObjectVector, self).__setitem__(key, value)
3149953Sgeoffrey.blake@arm.com
31510267SGeoffrey.Blake@arm.com    # Enumerate the params of each member of the SimObject vector. Creates
31610267SGeoffrey.Blake@arm.com    # strings that will allow indexing into the vector by the python code and
31710267SGeoffrey.Blake@arm.com    # allow it to be specified on the command line.
31810267SGeoffrey.Blake@arm.com    def enumerateParams(self, flags_dict = {},
31910267SGeoffrey.Blake@arm.com                        cmd_line_str = "",
32010267SGeoffrey.Blake@arm.com                        access_str = ""):
32110267SGeoffrey.Blake@arm.com        if hasattr(self, "_paramEnumed"):
32210267SGeoffrey.Blake@arm.com            print "Cycle detected enumerating params at %s?!" % (cmd_line_str)
32310267SGeoffrey.Blake@arm.com        else:
32410267SGeoffrey.Blake@arm.com            x = 0
32510267SGeoffrey.Blake@arm.com            for vals in self:
32610267SGeoffrey.Blake@arm.com                # Each entry in the SimObjectVector should be an
32710267SGeoffrey.Blake@arm.com                # instance of a SimObject
32810267SGeoffrey.Blake@arm.com                flags_dict = vals.enumerateParams(flags_dict,
32910267SGeoffrey.Blake@arm.com                                                  cmd_line_str + "%d." % x,
33010267SGeoffrey.Blake@arm.com                                                  access_str + "[%d]." % x)
33110267SGeoffrey.Blake@arm.com                x = x + 1
33210267SGeoffrey.Blake@arm.com
33310267SGeoffrey.Blake@arm.com        return flags_dict
33410267SGeoffrey.Blake@arm.com
3353101Sstever@eecs.umich.educlass VectorParamDesc(ParamDesc):
3363101Sstever@eecs.umich.edu    # Convert assigned value to appropriate type.  If the RHS is not a
3373101Sstever@eecs.umich.edu    # list or tuple, it generates a single-element list.
3383101Sstever@eecs.umich.edu    def convert(self, value):
3393101Sstever@eecs.umich.edu        if isinstance(value, (list, tuple)):
3403101Sstever@eecs.umich.edu            # list: coerce each element into new list
3413101Sstever@eecs.umich.edu            tmp_list = [ ParamDesc.convert(self, v) for v in value ]
34210364SGeoffrey.Blake@arm.com        elif isinstance(value, str):
34310364SGeoffrey.Blake@arm.com            # If input is a csv string
34410364SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.convert(self, v) \
34510364SGeoffrey.Blake@arm.com                         for v in value.strip('[').strip(']').split(',') ]
3463101Sstever@eecs.umich.edu        else:
3474762Snate@binkert.org            # singleton: coerce to a single-element list
3484762Snate@binkert.org            tmp_list = [ ParamDesc.convert(self, value) ]
3494762Snate@binkert.org
3504762Snate@binkert.org        if isSimObjectSequence(tmp_list):
3517528Ssteve.reinhardt@amd.com            return SimObjectVector(tmp_list)
3524762Snate@binkert.org        else:
3534762Snate@binkert.org            return VectorParamValue(tmp_list)
3544762Snate@binkert.org
35510267SGeoffrey.Blake@arm.com    # Produce a human readable example string that describes
35610267SGeoffrey.Blake@arm.com    # how to set this vector parameter in the absence of a default
35710267SGeoffrey.Blake@arm.com    # value.
35810267SGeoffrey.Blake@arm.com    def example_str(self):
35910267SGeoffrey.Blake@arm.com        s = super(VectorParamDesc, self).example_str()
36010267SGeoffrey.Blake@arm.com        help_str = "[" + s + "," + s + ", ...]"
36110267SGeoffrey.Blake@arm.com        return help_str
36210267SGeoffrey.Blake@arm.com
36310267SGeoffrey.Blake@arm.com    # Produce a human readable representation of the value of this vector param.
36410267SGeoffrey.Blake@arm.com    def pretty_print(self, value):
36510267SGeoffrey.Blake@arm.com        if isinstance(value, (list, tuple)):
36610267SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.pretty_print(self, v) for v in value ]
36710267SGeoffrey.Blake@arm.com        elif isinstance(value, str):
36810267SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.pretty_print(self, v) for v in value.split(',') ]
36910267SGeoffrey.Blake@arm.com        else:
37010267SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.pretty_print(self, value) ]
37110267SGeoffrey.Blake@arm.com
37210267SGeoffrey.Blake@arm.com        return tmp_list
37310267SGeoffrey.Blake@arm.com
37410267SGeoffrey.Blake@arm.com    # This is a helper function for the new config system
37510267SGeoffrey.Blake@arm.com    def __call__(self, value):
37610267SGeoffrey.Blake@arm.com        if isinstance(value, (list, tuple)):
37710267SGeoffrey.Blake@arm.com            # list: coerce each element into new list
37810267SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.convert(self, v) for v in value ]
37910267SGeoffrey.Blake@arm.com        elif isinstance(value, str):
38010267SGeoffrey.Blake@arm.com            # If input is a csv string
38110364SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.convert(self, v) \
38210364SGeoffrey.Blake@arm.com                         for v in value.strip('[').strip(']').split(',') ]
38310267SGeoffrey.Blake@arm.com        else:
38410267SGeoffrey.Blake@arm.com            # singleton: coerce to a single-element list
38510267SGeoffrey.Blake@arm.com            tmp_list = [ ParamDesc.convert(self, value) ]
38610267SGeoffrey.Blake@arm.com
38710267SGeoffrey.Blake@arm.com        return VectorParamValue(tmp_list)
38810267SGeoffrey.Blake@arm.com
3898596Ssteve.reinhardt@amd.com    def swig_module_name(self):
3908596Ssteve.reinhardt@amd.com        return "%s_vector" % self.ptype_str
3918596Ssteve.reinhardt@amd.com
3927673Snate@binkert.org    def swig_predecls(self, code):
3938596Ssteve.reinhardt@amd.com        code('%import "${{self.swig_module_name()}}.i"')
3944762Snate@binkert.org
3957673Snate@binkert.org    def swig_decl(self, code):
39611802Sandreas.sandberg@arm.com        code('%module(package="_m5") ${{self.swig_module_name()}}')
3977675Snate@binkert.org        code('%{')
3987675Snate@binkert.org        self.ptype.cxx_predecls(code)
3997675Snate@binkert.org        code('%}')
4007675Snate@binkert.org        code()
4018656Sandreas.hansson@arm.com        # Make sure the SWIGPY_SLICE_ARG is defined through this inclusion
4028656Sandreas.hansson@arm.com        code('%include "std_container.i"')
4038656Sandreas.hansson@arm.com        code()
4047675Snate@binkert.org        self.ptype.swig_predecls(code)
4057675Snate@binkert.org        code()
4067673Snate@binkert.org        code('%include "std_vector.i"')
4077675Snate@binkert.org        code()
4087675Snate@binkert.org
4097675Snate@binkert.org        ptype = self.ptype_str
4107675Snate@binkert.org        cxx_type = self.ptype.cxx_type
4117675Snate@binkert.org
4127675Snate@binkert.org        code('%template(vector_$ptype) std::vector< $cxx_type >;')
4137675Snate@binkert.org
4147673Snate@binkert.org    def cxx_predecls(self, code):
4157673Snate@binkert.org        code('#include <vector>')
4167673Snate@binkert.org        self.ptype.cxx_predecls(code)
4173101Sstever@eecs.umich.edu
41811988Sandreas.sandberg@arm.com    def pybind_predecls(self, code):
41911988Sandreas.sandberg@arm.com        code('#include <vector>')
42011988Sandreas.sandberg@arm.com        self.ptype.pybind_predecls(code)
42111988Sandreas.sandberg@arm.com
4227673Snate@binkert.org    def cxx_decl(self, code):
4237673Snate@binkert.org        code('std::vector< ${{self.ptype.cxx_type}} > ${{self.name}};')
4243101Sstever@eecs.umich.edu
4253101Sstever@eecs.umich.educlass ParamFactory(object):
4263101Sstever@eecs.umich.edu    def __init__(self, param_desc_class, ptype_str = None):
4273101Sstever@eecs.umich.edu        self.param_desc_class = param_desc_class
4283101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
4293101Sstever@eecs.umich.edu
4303101Sstever@eecs.umich.edu    def __getattr__(self, attr):
4313101Sstever@eecs.umich.edu        if self.ptype_str:
4323101Sstever@eecs.umich.edu            attr = self.ptype_str + '.' + attr
4333101Sstever@eecs.umich.edu        return ParamFactory(self.param_desc_class, attr)
4343101Sstever@eecs.umich.edu
4353101Sstever@eecs.umich.edu    # E.g., Param.Int(5, "number of widgets")
4363101Sstever@eecs.umich.edu    def __call__(self, *args, **kwargs):
4373101Sstever@eecs.umich.edu        ptype = None
4383101Sstever@eecs.umich.edu        try:
4395033Smilesck@eecs.umich.edu            ptype = allParams[self.ptype_str]
4405033Smilesck@eecs.umich.edu        except KeyError:
4413101Sstever@eecs.umich.edu            # if name isn't defined yet, assume it's a SimObject, and
4423101Sstever@eecs.umich.edu            # try to resolve it later
4433101Sstever@eecs.umich.edu            pass
4443101Sstever@eecs.umich.edu        return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs)
4453101Sstever@eecs.umich.edu
4463101Sstever@eecs.umich.eduParam = ParamFactory(ParamDesc)
4473101Sstever@eecs.umich.eduVectorParam = ParamFactory(VectorParamDesc)
4483101Sstever@eecs.umich.edu
4493101Sstever@eecs.umich.edu#####################################################################
4503101Sstever@eecs.umich.edu#
4513101Sstever@eecs.umich.edu# Parameter Types
4523101Sstever@eecs.umich.edu#
4533101Sstever@eecs.umich.edu# Though native Python types could be used to specify parameter types
4543101Sstever@eecs.umich.edu# (the 'ptype' field of the Param and VectorParam classes), it's more
4553101Sstever@eecs.umich.edu# flexible to define our own set of types.  This gives us more control
4563101Sstever@eecs.umich.edu# over how Python expressions are converted to values (via the
4573101Sstever@eecs.umich.edu# __init__() constructor) and how these values are printed out (via
4583101Sstever@eecs.umich.edu# the __str__() conversion method).
4593101Sstever@eecs.umich.edu#
4603101Sstever@eecs.umich.edu#####################################################################
4613101Sstever@eecs.umich.edu
4623101Sstever@eecs.umich.edu# String-valued parameter.  Just mixin the ParamValue class with the
4633101Sstever@eecs.umich.edu# built-in str class.
4643101Sstever@eecs.umich.educlass String(ParamValue,str):
4653101Sstever@eecs.umich.edu    cxx_type = 'std::string'
46610267SGeoffrey.Blake@arm.com    cmd_line_settable = True
4677673Snate@binkert.org
4687673Snate@binkert.org    @classmethod
4697673Snate@binkert.org    def cxx_predecls(self, code):
4707673Snate@binkert.org        code('#include <string>')
4717673Snate@binkert.org
4727673Snate@binkert.org    @classmethod
4737673Snate@binkert.org    def swig_predecls(cls, code):
4747673Snate@binkert.org        code('%include "std_string.i"')
4754762Snate@binkert.org
47610267SGeoffrey.Blake@arm.com    def __call__(self, value):
47710267SGeoffrey.Blake@arm.com        self = value
47810267SGeoffrey.Blake@arm.com        return value
47910267SGeoffrey.Blake@arm.com
48010458Sandreas.hansson@arm.com    @classmethod
48110458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
48210458Sandreas.hansson@arm.com        code('%s = %s;' % (dest, src))
48310458Sandreas.hansson@arm.com        code('%s true;' % ret)
48410458Sandreas.hansson@arm.com
4854762Snate@binkert.org    def getValue(self):
4864762Snate@binkert.org        return self
4873101Sstever@eecs.umich.edu
4883101Sstever@eecs.umich.edu# superclass for "numeric" parameter values, to emulate math
4893101Sstever@eecs.umich.edu# operations in a type-safe way.  e.g., a Latency times an int returns
4903101Sstever@eecs.umich.edu# a new Latency object.
4913101Sstever@eecs.umich.educlass NumericParamValue(ParamValue):
4923101Sstever@eecs.umich.edu    def __str__(self):
4933101Sstever@eecs.umich.edu        return str(self.value)
4943101Sstever@eecs.umich.edu
4953101Sstever@eecs.umich.edu    def __float__(self):
4963101Sstever@eecs.umich.edu        return float(self.value)
4973101Sstever@eecs.umich.edu
4983714Sstever@eecs.umich.edu    def __long__(self):
4993714Sstever@eecs.umich.edu        return long(self.value)
5003714Sstever@eecs.umich.edu
5013714Sstever@eecs.umich.edu    def __int__(self):
5023714Sstever@eecs.umich.edu        return int(self.value)
5033714Sstever@eecs.umich.edu
5043101Sstever@eecs.umich.edu    # hook for bounds checking
5053101Sstever@eecs.umich.edu    def _check(self):
5063101Sstever@eecs.umich.edu        return
5073101Sstever@eecs.umich.edu
5083101Sstever@eecs.umich.edu    def __mul__(self, other):
5093101Sstever@eecs.umich.edu        newobj = self.__class__(self)
5103101Sstever@eecs.umich.edu        newobj.value *= other
5113101Sstever@eecs.umich.edu        newobj._check()
5123101Sstever@eecs.umich.edu        return newobj
5133101Sstever@eecs.umich.edu
5143101Sstever@eecs.umich.edu    __rmul__ = __mul__
5153101Sstever@eecs.umich.edu
5163101Sstever@eecs.umich.edu    def __div__(self, other):
5173101Sstever@eecs.umich.edu        newobj = self.__class__(self)
5183101Sstever@eecs.umich.edu        newobj.value /= other
5193101Sstever@eecs.umich.edu        newobj._check()
5203101Sstever@eecs.umich.edu        return newobj
5213101Sstever@eecs.umich.edu
5223101Sstever@eecs.umich.edu    def __sub__(self, other):
5233101Sstever@eecs.umich.edu        newobj = self.__class__(self)
5243101Sstever@eecs.umich.edu        newobj.value -= other
5253101Sstever@eecs.umich.edu        newobj._check()
5263101Sstever@eecs.umich.edu        return newobj
5273101Sstever@eecs.umich.edu
52810380SAndrew.Bardsley@arm.com    def config_value(self):
52910380SAndrew.Bardsley@arm.com        return self.value
53010380SAndrew.Bardsley@arm.com
53110458Sandreas.hansson@arm.com    @classmethod
53210458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
53310458Sandreas.hansson@arm.com        # Assume that base/str.hh will be included anyway
53410458Sandreas.hansson@arm.com        # code('#include "base/str.hh"')
53510458Sandreas.hansson@arm.com        pass
53610458Sandreas.hansson@arm.com
53710458Sandreas.hansson@arm.com    # The default for parsing PODs from an .ini entry is to extract from an
53810458Sandreas.hansson@arm.com    # istringstream and let overloading choose the right type according to
53910458Sandreas.hansson@arm.com    # the dest type.
54010458Sandreas.hansson@arm.com    @classmethod
54110458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
54210458Sandreas.hansson@arm.com        code('%s to_number(%s, %s);' % (ret, src, dest))
54310458Sandreas.hansson@arm.com
5443101Sstever@eecs.umich.edu# Metaclass for bounds-checked integer parameters.  See CheckedInt.
5455033Smilesck@eecs.umich.educlass CheckedIntType(MetaParamValue):
5463101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
5473101Sstever@eecs.umich.edu        super(CheckedIntType, cls).__init__(name, bases, dict)
5483101Sstever@eecs.umich.edu
5493101Sstever@eecs.umich.edu        # CheckedInt is an abstract base class, so we actually don't
5503101Sstever@eecs.umich.edu        # want to do any processing on it... the rest of this code is
5513101Sstever@eecs.umich.edu        # just for classes that derive from CheckedInt.
5523101Sstever@eecs.umich.edu        if name == 'CheckedInt':
5533101Sstever@eecs.umich.edu            return
5543101Sstever@eecs.umich.edu
5553101Sstever@eecs.umich.edu        if not (hasattr(cls, 'min') and hasattr(cls, 'max')):
5563101Sstever@eecs.umich.edu            if not (hasattr(cls, 'size') and hasattr(cls, 'unsigned')):
5573101Sstever@eecs.umich.edu                panic("CheckedInt subclass %s must define either\n" \
5585822Ssaidi@eecs.umich.edu                      "    'min' and 'max' or 'size' and 'unsigned'\n",
5595822Ssaidi@eecs.umich.edu                      name);
5603101Sstever@eecs.umich.edu            if cls.unsigned:
5613101Sstever@eecs.umich.edu                cls.min = 0
5623101Sstever@eecs.umich.edu                cls.max = 2 ** cls.size - 1
5633101Sstever@eecs.umich.edu            else:
5643101Sstever@eecs.umich.edu                cls.min = -(2 ** (cls.size - 1))
5653101Sstever@eecs.umich.edu                cls.max = (2 ** (cls.size - 1)) - 1
5663101Sstever@eecs.umich.edu
5673101Sstever@eecs.umich.edu# Abstract superclass for bounds-checked integer parameters.  This
5683101Sstever@eecs.umich.edu# class is subclassed to generate parameter classes with specific
5693101Sstever@eecs.umich.edu# bounds.  Initialization of the min and max bounds is done in the
5703101Sstever@eecs.umich.edu# metaclass CheckedIntType.__init__.
5713101Sstever@eecs.umich.educlass CheckedInt(NumericParamValue):
5723101Sstever@eecs.umich.edu    __metaclass__ = CheckedIntType
57310267SGeoffrey.Blake@arm.com    cmd_line_settable = True
5743101Sstever@eecs.umich.edu
5753101Sstever@eecs.umich.edu    def _check(self):
5763101Sstever@eecs.umich.edu        if not self.min <= self.value <= self.max:
5773101Sstever@eecs.umich.edu            raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
5783101Sstever@eecs.umich.edu                  (self.min, self.value, self.max)
5793101Sstever@eecs.umich.edu
5803101Sstever@eecs.umich.edu    def __init__(self, value):
5813101Sstever@eecs.umich.edu        if isinstance(value, str):
5823102Sstever@eecs.umich.edu            self.value = convert.toInteger(value)
5833714Sstever@eecs.umich.edu        elif isinstance(value, (int, long, float, NumericParamValue)):
5843101Sstever@eecs.umich.edu            self.value = long(value)
5853714Sstever@eecs.umich.edu        else:
5863714Sstever@eecs.umich.edu            raise TypeError, "Can't convert object of type %s to CheckedInt" \
5873714Sstever@eecs.umich.edu                  % type(value).__name__
5883101Sstever@eecs.umich.edu        self._check()
5893101Sstever@eecs.umich.edu
59010267SGeoffrey.Blake@arm.com    def __call__(self, value):
59110267SGeoffrey.Blake@arm.com        self.__init__(value)
59210267SGeoffrey.Blake@arm.com        return value
59310267SGeoffrey.Blake@arm.com
5947673Snate@binkert.org    @classmethod
5957673Snate@binkert.org    def cxx_predecls(cls, code):
5967673Snate@binkert.org        # most derived types require this, so we just do it here once
5977673Snate@binkert.org        code('#include "base/types.hh"')
5987673Snate@binkert.org
5997673Snate@binkert.org    @classmethod
6007673Snate@binkert.org    def swig_predecls(cls, code):
6017673Snate@binkert.org        # most derived types require this, so we just do it here once
6027673Snate@binkert.org        code('%import "stdint.i"')
6037673Snate@binkert.org        code('%import "base/types.hh"')
6047673Snate@binkert.org
6054762Snate@binkert.org    def getValue(self):
6064762Snate@binkert.org        return long(self.value)
6074762Snate@binkert.org
6083101Sstever@eecs.umich.educlass Int(CheckedInt):      cxx_type = 'int';      size = 32; unsigned = False
6093101Sstever@eecs.umich.educlass Unsigned(CheckedInt): cxx_type = 'unsigned'; size = 32; unsigned = True
6103101Sstever@eecs.umich.edu
6113101Sstever@eecs.umich.educlass Int8(CheckedInt):     cxx_type =   'int8_t'; size =  8; unsigned = False
6123101Sstever@eecs.umich.educlass UInt8(CheckedInt):    cxx_type =  'uint8_t'; size =  8; unsigned = True
6133101Sstever@eecs.umich.educlass Int16(CheckedInt):    cxx_type =  'int16_t'; size = 16; unsigned = False
6143101Sstever@eecs.umich.educlass UInt16(CheckedInt):   cxx_type = 'uint16_t'; size = 16; unsigned = True
6153101Sstever@eecs.umich.educlass Int32(CheckedInt):    cxx_type =  'int32_t'; size = 32; unsigned = False
6163101Sstever@eecs.umich.educlass UInt32(CheckedInt):   cxx_type = 'uint32_t'; size = 32; unsigned = True
6173101Sstever@eecs.umich.educlass Int64(CheckedInt):    cxx_type =  'int64_t'; size = 64; unsigned = False
6183101Sstever@eecs.umich.educlass UInt64(CheckedInt):   cxx_type = 'uint64_t'; size = 64; unsigned = True
6193101Sstever@eecs.umich.edu
6203101Sstever@eecs.umich.educlass Counter(CheckedInt):  cxx_type = 'Counter';  size = 64; unsigned = True
6213101Sstever@eecs.umich.educlass Tick(CheckedInt):     cxx_type = 'Tick';     size = 64; unsigned = True
6223101Sstever@eecs.umich.educlass TcpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
6233101Sstever@eecs.umich.educlass UdpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
6243101Sstever@eecs.umich.edu
6253101Sstever@eecs.umich.educlass Percent(CheckedInt):  cxx_type = 'int'; min = 0; max = 100
6263101Sstever@eecs.umich.edu
6279184Sandreas.hansson@arm.comclass Cycles(CheckedInt):
6289184Sandreas.hansson@arm.com    cxx_type = 'Cycles'
6299184Sandreas.hansson@arm.com    size = 64
6309184Sandreas.hansson@arm.com    unsigned = True
6319184Sandreas.hansson@arm.com
6329184Sandreas.hansson@arm.com    def getValue(self):
63311802Sandreas.sandberg@arm.com        from _m5.core import Cycles
6349184Sandreas.hansson@arm.com        return Cycles(self.value)
6359184Sandreas.hansson@arm.com
63610458Sandreas.hansson@arm.com    @classmethod
63710458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
63810458Sandreas.hansson@arm.com        # Assume that base/str.hh will be included anyway
63910458Sandreas.hansson@arm.com        # code('#include "base/str.hh"')
64010458Sandreas.hansson@arm.com        pass
64110458Sandreas.hansson@arm.com
64210458Sandreas.hansson@arm.com    @classmethod
64310458Sandreas.hansson@arm.com    def cxx_ini_parse(cls, code, src, dest, ret):
64410458Sandreas.hansson@arm.com        code('uint64_t _temp;')
64510458Sandreas.hansson@arm.com        code('bool _ret = to_number(%s, _temp);' % src)
64610458Sandreas.hansson@arm.com        code('if (_ret)')
64710458Sandreas.hansson@arm.com        code('    %s = Cycles(_temp);' % dest)
64810458Sandreas.hansson@arm.com        code('%s _ret;' % ret)
64910458Sandreas.hansson@arm.com
6503101Sstever@eecs.umich.educlass Float(ParamValue, float):
6514446Sbinkertn@umich.edu    cxx_type = 'double'
65210668SGeoffrey.Blake@arm.com    cmd_line_settable = True
6533101Sstever@eecs.umich.edu
6545468Snate@binkert.org    def __init__(self, value):
65510267SGeoffrey.Blake@arm.com        if isinstance(value, (int, long, float, NumericParamValue, Float, str)):
6565468Snate@binkert.org            self.value = float(value)
6575468Snate@binkert.org        else:
6585468Snate@binkert.org            raise TypeError, "Can't convert object of type %s to Float" \
6595468Snate@binkert.org                  % type(value).__name__
6605468Snate@binkert.org
66110267SGeoffrey.Blake@arm.com    def __call__(self, value):
66210267SGeoffrey.Blake@arm.com        self.__init__(value)
66310267SGeoffrey.Blake@arm.com        return value
66410267SGeoffrey.Blake@arm.com
6654762Snate@binkert.org    def getValue(self):
6664762Snate@binkert.org        return float(self.value)
6674762Snate@binkert.org
66810380SAndrew.Bardsley@arm.com    def config_value(self):
66910380SAndrew.Bardsley@arm.com        return self
67010380SAndrew.Bardsley@arm.com
67110458Sandreas.hansson@arm.com    @classmethod
67210458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
67310458Sandreas.hansson@arm.com        code('#include <sstream>')
67410458Sandreas.hansson@arm.com
67510458Sandreas.hansson@arm.com    @classmethod
67610458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
67710458Sandreas.hansson@arm.com        code('%s (std::istringstream(%s) >> %s).eof();' % (ret, src, dest))
67810458Sandreas.hansson@arm.com
6793101Sstever@eecs.umich.educlass MemorySize(CheckedInt):
6803101Sstever@eecs.umich.edu    cxx_type = 'uint64_t'
68110267SGeoffrey.Blake@arm.com    ex_str = '512MB'
6823101Sstever@eecs.umich.edu    size = 64
6833101Sstever@eecs.umich.edu    unsigned = True
6843101Sstever@eecs.umich.edu    def __init__(self, value):
6853101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
6863101Sstever@eecs.umich.edu            self.value = value.value
6873101Sstever@eecs.umich.edu        else:
6883102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
6893101Sstever@eecs.umich.edu        self._check()
6903101Sstever@eecs.umich.edu
6913101Sstever@eecs.umich.educlass MemorySize32(CheckedInt):
6924168Sbinkertn@umich.edu    cxx_type = 'uint32_t'
69310267SGeoffrey.Blake@arm.com    ex_str = '512MB'
6943101Sstever@eecs.umich.edu    size = 32
6953101Sstever@eecs.umich.edu    unsigned = True
6963101Sstever@eecs.umich.edu    def __init__(self, value):
6973101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
6983101Sstever@eecs.umich.edu            self.value = value.value
6993101Sstever@eecs.umich.edu        else:
7003102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
7013101Sstever@eecs.umich.edu        self._check()
7023101Sstever@eecs.umich.edu
7033101Sstever@eecs.umich.educlass Addr(CheckedInt):
7043101Sstever@eecs.umich.edu    cxx_type = 'Addr'
7053101Sstever@eecs.umich.edu    size = 64
7063101Sstever@eecs.umich.edu    unsigned = True
7073101Sstever@eecs.umich.edu    def __init__(self, value):
7083101Sstever@eecs.umich.edu        if isinstance(value, Addr):
7093101Sstever@eecs.umich.edu            self.value = value.value
7103101Sstever@eecs.umich.edu        else:
7113101Sstever@eecs.umich.edu            try:
71210317Smitch.hayenga@arm.com                # Often addresses are referred to with sizes. Ex: A device
71310317Smitch.hayenga@arm.com                # base address is at "512MB".  Use toMemorySize() to convert
71410317Smitch.hayenga@arm.com                # these into addresses. If the address is not specified with a
71510317Smitch.hayenga@arm.com                # "size", an exception will occur and numeric translation will
71610317Smitch.hayenga@arm.com                # proceed below.
7173102Sstever@eecs.umich.edu                self.value = convert.toMemorySize(value)
71810317Smitch.hayenga@arm.com            except (TypeError, ValueError):
71910317Smitch.hayenga@arm.com                # Convert number to string and use long() to do automatic
72010317Smitch.hayenga@arm.com                # base conversion (requires base=0 for auto-conversion)
72110317Smitch.hayenga@arm.com                self.value = long(str(value), base=0)
72210317Smitch.hayenga@arm.com
7233101Sstever@eecs.umich.edu        self._check()
7243584Ssaidi@eecs.umich.edu    def __add__(self, other):
7253584Ssaidi@eecs.umich.edu        if isinstance(other, Addr):
7263584Ssaidi@eecs.umich.edu            return self.value + other.value
7273584Ssaidi@eecs.umich.edu        else:
7283584Ssaidi@eecs.umich.edu            return self.value + other
72910267SGeoffrey.Blake@arm.com    def pretty_print(self, value):
73010267SGeoffrey.Blake@arm.com        try:
73110267SGeoffrey.Blake@arm.com            val = convert.toMemorySize(value)
73210267SGeoffrey.Blake@arm.com        except TypeError:
73310267SGeoffrey.Blake@arm.com            val = long(value)
73410267SGeoffrey.Blake@arm.com        return "0x%x" % long(val)
7353101Sstever@eecs.umich.edu
7369232Sandreas.hansson@arm.comclass AddrRange(ParamValue):
7379235Sandreas.hansson@arm.com    cxx_type = 'AddrRange'
7383101Sstever@eecs.umich.edu
7393101Sstever@eecs.umich.edu    def __init__(self, *args, **kwargs):
74010676Sandreas.hansson@arm.com        # Disable interleaving and hashing by default
7419411Sandreas.hansson@arm.com        self.intlvHighBit = 0
74210676Sandreas.hansson@arm.com        self.xorHighBit = 0
7439411Sandreas.hansson@arm.com        self.intlvBits = 0
7449411Sandreas.hansson@arm.com        self.intlvMatch = 0
7459411Sandreas.hansson@arm.com
7463101Sstever@eecs.umich.edu        def handle_kwargs(self, kwargs):
7479411Sandreas.hansson@arm.com            # An address range needs to have an upper limit, specified
7489411Sandreas.hansson@arm.com            # either explicitly with an end, or as an offset using the
7499411Sandreas.hansson@arm.com            # size keyword.
7503101Sstever@eecs.umich.edu            if 'end' in kwargs:
7519232Sandreas.hansson@arm.com                self.end = Addr(kwargs.pop('end'))
7523101Sstever@eecs.umich.edu            elif 'size' in kwargs:
7539232Sandreas.hansson@arm.com                self.end = self.start + Addr(kwargs.pop('size')) - 1
7543101Sstever@eecs.umich.edu            else:
7553101Sstever@eecs.umich.edu                raise TypeError, "Either end or size must be specified"
7563101Sstever@eecs.umich.edu
7579411Sandreas.hansson@arm.com            # Now on to the optional bit
7589411Sandreas.hansson@arm.com            if 'intlvHighBit' in kwargs:
7599411Sandreas.hansson@arm.com                self.intlvHighBit = int(kwargs.pop('intlvHighBit'))
76010676Sandreas.hansson@arm.com            if 'xorHighBit' in kwargs:
76110676Sandreas.hansson@arm.com                self.xorHighBit = int(kwargs.pop('xorHighBit'))
7629411Sandreas.hansson@arm.com            if 'intlvBits' in kwargs:
7639411Sandreas.hansson@arm.com                self.intlvBits = int(kwargs.pop('intlvBits'))
7649411Sandreas.hansson@arm.com            if 'intlvMatch' in kwargs:
7659411Sandreas.hansson@arm.com                self.intlvMatch = int(kwargs.pop('intlvMatch'))
7669411Sandreas.hansson@arm.com
7673101Sstever@eecs.umich.edu        if len(args) == 0:
7689232Sandreas.hansson@arm.com            self.start = Addr(kwargs.pop('start'))
7693101Sstever@eecs.umich.edu            handle_kwargs(self, kwargs)
7703101Sstever@eecs.umich.edu
7713101Sstever@eecs.umich.edu        elif len(args) == 1:
7723101Sstever@eecs.umich.edu            if kwargs:
7739232Sandreas.hansson@arm.com                self.start = Addr(args[0])
7743101Sstever@eecs.umich.edu                handle_kwargs(self, kwargs)
7755219Ssaidi@eecs.umich.edu            elif isinstance(args[0], (list, tuple)):
7769232Sandreas.hansson@arm.com                self.start = Addr(args[0][0])
7779232Sandreas.hansson@arm.com                self.end = Addr(args[0][1])
7783101Sstever@eecs.umich.edu            else:
7799232Sandreas.hansson@arm.com                self.start = Addr(0)
7809232Sandreas.hansson@arm.com                self.end = Addr(args[0]) - 1
7813101Sstever@eecs.umich.edu
7823101Sstever@eecs.umich.edu        elif len(args) == 2:
7839232Sandreas.hansson@arm.com            self.start = Addr(args[0])
7849232Sandreas.hansson@arm.com            self.end = Addr(args[1])
7853101Sstever@eecs.umich.edu        else:
7863101Sstever@eecs.umich.edu            raise TypeError, "Too many arguments specified"
7873101Sstever@eecs.umich.edu
7883101Sstever@eecs.umich.edu        if kwargs:
7899232Sandreas.hansson@arm.com            raise TypeError, "Too many keywords: %s" % kwargs.keys()
7903101Sstever@eecs.umich.edu
7913101Sstever@eecs.umich.edu    def __str__(self):
79211620SMatthew.Poremba@amd.com        return '%s:%s:%s:%s:%s:%s' \
79311620SMatthew.Poremba@amd.com            % (self.start, self.end, self.intlvHighBit, self.xorHighBit,\
79411620SMatthew.Poremba@amd.com               self.intlvBits, self.intlvMatch)
7959232Sandreas.hansson@arm.com
7969232Sandreas.hansson@arm.com    def size(self):
7979411Sandreas.hansson@arm.com        # Divide the size by the size of the interleaving slice
7989411Sandreas.hansson@arm.com        return (long(self.end) - long(self.start) + 1) >> self.intlvBits
7993101Sstever@eecs.umich.edu
8007673Snate@binkert.org    @classmethod
8017673Snate@binkert.org    def cxx_predecls(cls, code):
8029232Sandreas.hansson@arm.com        Addr.cxx_predecls(code)
8039235Sandreas.hansson@arm.com        code('#include "base/addr_range.hh"')
8047675Snate@binkert.org
8057675Snate@binkert.org    @classmethod
80611988Sandreas.sandberg@arm.com    def pybind_predecls(cls, code):
80711988Sandreas.sandberg@arm.com        Addr.pybind_predecls(code)
80811988Sandreas.sandberg@arm.com        code('#include "base/addr_range.hh"')
80911988Sandreas.sandberg@arm.com
81011988Sandreas.sandberg@arm.com    @classmethod
8117675Snate@binkert.org    def swig_predecls(cls, code):
8129232Sandreas.hansson@arm.com        Addr.swig_predecls(code)
8137673Snate@binkert.org
81410458Sandreas.hansson@arm.com    @classmethod
81510458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
81610458Sandreas.hansson@arm.com        code('#include <sstream>')
81710458Sandreas.hansson@arm.com
81810458Sandreas.hansson@arm.com    @classmethod
81910458Sandreas.hansson@arm.com    def cxx_ini_parse(cls, code, src, dest, ret):
82011620SMatthew.Poremba@amd.com        code('uint64_t _start, _end, _intlvHighBit = 0, _xorHighBit = 0;')
82111620SMatthew.Poremba@amd.com        code('uint64_t _intlvBits = 0, _intlvMatch = 0;')
82210458Sandreas.hansson@arm.com        code('char _sep;')
82310458Sandreas.hansson@arm.com        code('std::istringstream _stream(${src});')
82410458Sandreas.hansson@arm.com        code('_stream >> _start;')
82510458Sandreas.hansson@arm.com        code('_stream.get(_sep);')
82610458Sandreas.hansson@arm.com        code('_stream >> _end;')
82711620SMatthew.Poremba@amd.com        code('if (!_stream.fail() && !_stream.eof()) {')
82811620SMatthew.Poremba@amd.com        code('    _stream.get(_sep);')
82911620SMatthew.Poremba@amd.com        code('    _stream >> _intlvHighBit;')
83011620SMatthew.Poremba@amd.com        code('    _stream.get(_sep);')
83111620SMatthew.Poremba@amd.com        code('    _stream >> _xorHighBit;')
83211620SMatthew.Poremba@amd.com        code('    _stream.get(_sep);')
83311620SMatthew.Poremba@amd.com        code('    _stream >> _intlvBits;')
83411620SMatthew.Poremba@amd.com        code('    _stream.get(_sep);')
83511620SMatthew.Poremba@amd.com        code('    _stream >> _intlvMatch;')
83611620SMatthew.Poremba@amd.com        code('}')
83710458Sandreas.hansson@arm.com        code('bool _ret = !_stream.fail() &&'
83810458Sandreas.hansson@arm.com            '_stream.eof() && _sep == \':\';')
83910458Sandreas.hansson@arm.com        code('if (_ret)')
84011620SMatthew.Poremba@amd.com        code('   ${dest} = AddrRange(_start, _end, _intlvHighBit, \
84111620SMatthew.Poremba@amd.com                _xorHighBit, _intlvBits, _intlvMatch);')
84210458Sandreas.hansson@arm.com        code('${ret} _ret;')
84310458Sandreas.hansson@arm.com
8444762Snate@binkert.org    def getValue(self):
8459235Sandreas.hansson@arm.com        # Go from the Python class to the wrapped C++ class generated
8469235Sandreas.hansson@arm.com        # by swig
84711802Sandreas.sandberg@arm.com        from _m5.range import AddrRange
8484762Snate@binkert.org
8499411Sandreas.hansson@arm.com        return AddrRange(long(self.start), long(self.end),
85010676Sandreas.hansson@arm.com                         int(self.intlvHighBit), int(self.xorHighBit),
85110676Sandreas.hansson@arm.com                         int(self.intlvBits), int(self.intlvMatch))
8523101Sstever@eecs.umich.edu
8533101Sstever@eecs.umich.edu# Boolean parameter type.  Python doesn't let you subclass bool, since
8543101Sstever@eecs.umich.edu# it doesn't want to let you create multiple instances of True and
8553101Sstever@eecs.umich.edu# False.  Thus this is a little more complicated than String.
8563101Sstever@eecs.umich.educlass Bool(ParamValue):
8573101Sstever@eecs.umich.edu    cxx_type = 'bool'
85810267SGeoffrey.Blake@arm.com    cmd_line_settable = True
85910267SGeoffrey.Blake@arm.com
8603101Sstever@eecs.umich.edu    def __init__(self, value):
8613101Sstever@eecs.umich.edu        try:
8623102Sstever@eecs.umich.edu            self.value = convert.toBool(value)
8633101Sstever@eecs.umich.edu        except TypeError:
8643101Sstever@eecs.umich.edu            self.value = bool(value)
8653101Sstever@eecs.umich.edu
86610267SGeoffrey.Blake@arm.com    def __call__(self, value):
86710267SGeoffrey.Blake@arm.com        self.__init__(value)
86810267SGeoffrey.Blake@arm.com        return value
86910267SGeoffrey.Blake@arm.com
8704762Snate@binkert.org    def getValue(self):
8714762Snate@binkert.org        return bool(self.value)
8724762Snate@binkert.org
8733101Sstever@eecs.umich.edu    def __str__(self):
8743101Sstever@eecs.umich.edu        return str(self.value)
8753101Sstever@eecs.umich.edu
8768934SBrad.Beckmann@amd.com    # implement truth value testing for Bool parameters so that these params
8778934SBrad.Beckmann@amd.com    # evaluate correctly during the python configuration phase
8788934SBrad.Beckmann@amd.com    def __nonzero__(self):
8798934SBrad.Beckmann@amd.com        return bool(self.value)
8808934SBrad.Beckmann@amd.com
8813101Sstever@eecs.umich.edu    def ini_str(self):
8823101Sstever@eecs.umich.edu        if self.value:
8833101Sstever@eecs.umich.edu            return 'true'
8843101Sstever@eecs.umich.edu        return 'false'
8853101Sstever@eecs.umich.edu
88610380SAndrew.Bardsley@arm.com    def config_value(self):
88710380SAndrew.Bardsley@arm.com        return self.value
88810380SAndrew.Bardsley@arm.com
88910458Sandreas.hansson@arm.com    @classmethod
89010458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
89110458Sandreas.hansson@arm.com        # Assume that base/str.hh will be included anyway
89210458Sandreas.hansson@arm.com        # code('#include "base/str.hh"')
89310458Sandreas.hansson@arm.com        pass
89410458Sandreas.hansson@arm.com
89510458Sandreas.hansson@arm.com    @classmethod
89610458Sandreas.hansson@arm.com    def cxx_ini_parse(cls, code, src, dest, ret):
89710458Sandreas.hansson@arm.com        code('%s to_bool(%s, %s);' % (ret, src, dest))
89810458Sandreas.hansson@arm.com
8993101Sstever@eecs.umich.edudef IncEthernetAddr(addr, val = 1):
9003101Sstever@eecs.umich.edu    bytes = map(lambda x: int(x, 16), addr.split(':'))
9013101Sstever@eecs.umich.edu    bytes[5] += val
9023101Sstever@eecs.umich.edu    for i in (5, 4, 3, 2, 1):
9033101Sstever@eecs.umich.edu        val,rem = divmod(bytes[i], 256)
9043101Sstever@eecs.umich.edu        bytes[i] = rem
9053101Sstever@eecs.umich.edu        if val == 0:
9063101Sstever@eecs.umich.edu            break
9073101Sstever@eecs.umich.edu        bytes[i - 1] += val
9083101Sstever@eecs.umich.edu    assert(bytes[0] <= 255)
9093101Sstever@eecs.umich.edu    return ':'.join(map(lambda x: '%02x' % x, bytes))
9103101Sstever@eecs.umich.edu
9114380Sbinkertn@umich.edu_NextEthernetAddr = "00:90:00:00:00:01"
9124380Sbinkertn@umich.edudef NextEthernetAddr():
9134380Sbinkertn@umich.edu    global _NextEthernetAddr
9143101Sstever@eecs.umich.edu
9154380Sbinkertn@umich.edu    value = _NextEthernetAddr
9164380Sbinkertn@umich.edu    _NextEthernetAddr = IncEthernetAddr(_NextEthernetAddr, 1)
9174380Sbinkertn@umich.edu    return value
9183101Sstever@eecs.umich.edu
9193101Sstever@eecs.umich.educlass EthernetAddr(ParamValue):
9203101Sstever@eecs.umich.edu    cxx_type = 'Net::EthAddr'
92110267SGeoffrey.Blake@arm.com    ex_str = "00:90:00:00:00:01"
92210267SGeoffrey.Blake@arm.com    cmd_line_settable = True
9237673Snate@binkert.org
9247673Snate@binkert.org    @classmethod
9257673Snate@binkert.org    def cxx_predecls(cls, code):
9267673Snate@binkert.org        code('#include "base/inet.hh"')
9277673Snate@binkert.org
9287673Snate@binkert.org    @classmethod
9297673Snate@binkert.org    def swig_predecls(cls, code):
9307673Snate@binkert.org        code('%include "python/swig/inet.i"')
9317673Snate@binkert.org
9323101Sstever@eecs.umich.edu    def __init__(self, value):
9333101Sstever@eecs.umich.edu        if value == NextEthernetAddr:
9343101Sstever@eecs.umich.edu            self.value = value
9353101Sstever@eecs.umich.edu            return
9363101Sstever@eecs.umich.edu
9373101Sstever@eecs.umich.edu        if not isinstance(value, str):
9383101Sstever@eecs.umich.edu            raise TypeError, "expected an ethernet address and didn't get one"
9393101Sstever@eecs.umich.edu
9403101Sstever@eecs.umich.edu        bytes = value.split(':')
9413101Sstever@eecs.umich.edu        if len(bytes) != 6:
9423101Sstever@eecs.umich.edu            raise TypeError, 'invalid ethernet address %s' % value
9433101Sstever@eecs.umich.edu
9443101Sstever@eecs.umich.edu        for byte in bytes:
9459941SGeoffrey.Blake@arm.com            if not 0 <= int(byte, base=16) <= 0xff:
9463101Sstever@eecs.umich.edu                raise TypeError, 'invalid ethernet address %s' % value
9473101Sstever@eecs.umich.edu
9483101Sstever@eecs.umich.edu        self.value = value
9493101Sstever@eecs.umich.edu
95010267SGeoffrey.Blake@arm.com    def __call__(self, value):
95110267SGeoffrey.Blake@arm.com        self.__init__(value)
95210267SGeoffrey.Blake@arm.com        return value
95310267SGeoffrey.Blake@arm.com
9543101Sstever@eecs.umich.edu    def unproxy(self, base):
9553101Sstever@eecs.umich.edu        if self.value == NextEthernetAddr:
9564380Sbinkertn@umich.edu            return EthernetAddr(self.value())
9573101Sstever@eecs.umich.edu        return self
9583101Sstever@eecs.umich.edu
9594762Snate@binkert.org    def getValue(self):
96011988Sandreas.sandberg@arm.com        from _m5.net import EthAddr
9614762Snate@binkert.org        return EthAddr(self.value)
9624762Snate@binkert.org
96311228SAndrew.Bardsley@arm.com    def __str__(self):
96411228SAndrew.Bardsley@arm.com        return self.value
96511228SAndrew.Bardsley@arm.com
9664380Sbinkertn@umich.edu    def ini_str(self):
9674380Sbinkertn@umich.edu        return self.value
9683101Sstever@eecs.umich.edu
96910458Sandreas.hansson@arm.com    @classmethod
97010458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
97110458Sandreas.hansson@arm.com        code('%s = Net::EthAddr(%s);' % (dest, src))
97210458Sandreas.hansson@arm.com        code('%s true;' % ret)
97310458Sandreas.hansson@arm.com
9747777Sgblack@eecs.umich.edu# When initializing an IpAddress, pass in an existing IpAddress, a string of
9757777Sgblack@eecs.umich.edu# the form "a.b.c.d", or an integer representing an IP.
9767777Sgblack@eecs.umich.educlass IpAddress(ParamValue):
9777777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpAddress'
97810267SGeoffrey.Blake@arm.com    ex_str = "127.0.0.1"
97910267SGeoffrey.Blake@arm.com    cmd_line_settable = True
9807777Sgblack@eecs.umich.edu
9817777Sgblack@eecs.umich.edu    @classmethod
9827777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
9837777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
9847777Sgblack@eecs.umich.edu
9857777Sgblack@eecs.umich.edu    @classmethod
9867777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
9877777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
9887777Sgblack@eecs.umich.edu
9897777Sgblack@eecs.umich.edu    def __init__(self, value):
9907777Sgblack@eecs.umich.edu        if isinstance(value, IpAddress):
9917777Sgblack@eecs.umich.edu            self.ip = value.ip
9927777Sgblack@eecs.umich.edu        else:
9937777Sgblack@eecs.umich.edu            try:
9947777Sgblack@eecs.umich.edu                self.ip = convert.toIpAddress(value)
9957777Sgblack@eecs.umich.edu            except TypeError:
9967777Sgblack@eecs.umich.edu                self.ip = long(value)
9977777Sgblack@eecs.umich.edu        self.verifyIp()
9987777Sgblack@eecs.umich.edu
99910267SGeoffrey.Blake@arm.com    def __call__(self, value):
100010267SGeoffrey.Blake@arm.com        self.__init__(value)
100110267SGeoffrey.Blake@arm.com        return value
100210267SGeoffrey.Blake@arm.com
10038579Ssteve.reinhardt@amd.com    def __str__(self):
10048579Ssteve.reinhardt@amd.com        tup = [(self.ip >> i)  & 0xff for i in (24, 16, 8, 0)]
10058579Ssteve.reinhardt@amd.com        return '%d.%d.%d.%d' % tuple(tup)
10068579Ssteve.reinhardt@amd.com
10078579Ssteve.reinhardt@amd.com    def __eq__(self, other):
10088579Ssteve.reinhardt@amd.com        if isinstance(other, IpAddress):
10098579Ssteve.reinhardt@amd.com            return self.ip == other.ip
10108579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
10118579Ssteve.reinhardt@amd.com            try:
10128579Ssteve.reinhardt@amd.com                return self.ip == convert.toIpAddress(other)
10138579Ssteve.reinhardt@amd.com            except:
10148579Ssteve.reinhardt@amd.com                return False
10158579Ssteve.reinhardt@amd.com        else:
10168579Ssteve.reinhardt@amd.com            return self.ip == other
10178579Ssteve.reinhardt@amd.com
10188579Ssteve.reinhardt@amd.com    def __ne__(self, other):
10198579Ssteve.reinhardt@amd.com        return not (self == other)
10208579Ssteve.reinhardt@amd.com
10217777Sgblack@eecs.umich.edu    def verifyIp(self):
10227777Sgblack@eecs.umich.edu        if self.ip < 0 or self.ip >= (1 << 32):
10237798Sgblack@eecs.umich.edu            raise TypeError, "invalid ip address %#08x" % self.ip
10247777Sgblack@eecs.umich.edu
10257777Sgblack@eecs.umich.edu    def getValue(self):
102611988Sandreas.sandberg@arm.com        from _m5.net import IpAddress
10277777Sgblack@eecs.umich.edu        return IpAddress(self.ip)
10287777Sgblack@eecs.umich.edu
10297777Sgblack@eecs.umich.edu# When initializing an IpNetmask, pass in an existing IpNetmask, a string of
10307777Sgblack@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
10317777Sgblack@eecs.umich.edu# positional or keyword arguments.
10327777Sgblack@eecs.umich.educlass IpNetmask(IpAddress):
10337777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpNetmask'
103410267SGeoffrey.Blake@arm.com    ex_str = "127.0.0.0/24"
103510267SGeoffrey.Blake@arm.com    cmd_line_settable = True
10367777Sgblack@eecs.umich.edu
10377777Sgblack@eecs.umich.edu    @classmethod
10387777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
10397777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
10407777Sgblack@eecs.umich.edu
10417777Sgblack@eecs.umich.edu    @classmethod
10427777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
10437777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
10447777Sgblack@eecs.umich.edu
10457777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
10467777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
10477777Sgblack@eecs.umich.edu            if key in kwargs:
10487777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
10497777Sgblack@eecs.umich.edu            elif elseVal:
10507777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
10517777Sgblack@eecs.umich.edu            else:
10527777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
10537777Sgblack@eecs.umich.edu
10547777Sgblack@eecs.umich.edu        if len(args) == 0:
10557777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
10567777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'netmask')
10577777Sgblack@eecs.umich.edu
10587777Sgblack@eecs.umich.edu        elif len(args) == 1:
10597777Sgblack@eecs.umich.edu            if kwargs:
10607777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'netmask' in kwargs:
10617777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
10627777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
10637777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'netmask', args[0])
10647777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpNetmask):
10657777Sgblack@eecs.umich.edu                self.ip = args[0].ip
10667777Sgblack@eecs.umich.edu                self.netmask = args[0].netmask
10677777Sgblack@eecs.umich.edu            else:
10687777Sgblack@eecs.umich.edu                (self.ip, self.netmask) = convert.toIpNetmask(args[0])
10697777Sgblack@eecs.umich.edu
10707777Sgblack@eecs.umich.edu        elif len(args) == 2:
10717777Sgblack@eecs.umich.edu            self.ip = args[0]
10727777Sgblack@eecs.umich.edu            self.netmask = args[1]
10737777Sgblack@eecs.umich.edu        else:
10747777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
10757777Sgblack@eecs.umich.edu
10767777Sgblack@eecs.umich.edu        if kwargs:
10777777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
10787777Sgblack@eecs.umich.edu
10797777Sgblack@eecs.umich.edu        self.verify()
10807777Sgblack@eecs.umich.edu
108110267SGeoffrey.Blake@arm.com    def __call__(self, value):
108210267SGeoffrey.Blake@arm.com        self.__init__(value)
108310267SGeoffrey.Blake@arm.com        return value
108410267SGeoffrey.Blake@arm.com
10858579Ssteve.reinhardt@amd.com    def __str__(self):
10868579Ssteve.reinhardt@amd.com        return "%s/%d" % (super(IpNetmask, self).__str__(), self.netmask)
10878579Ssteve.reinhardt@amd.com
10888579Ssteve.reinhardt@amd.com    def __eq__(self, other):
10898579Ssteve.reinhardt@amd.com        if isinstance(other, IpNetmask):
10908579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.netmask == other.netmask
10918579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
10928579Ssteve.reinhardt@amd.com            try:
10938579Ssteve.reinhardt@amd.com                return (self.ip, self.netmask) == convert.toIpNetmask(other)
10948579Ssteve.reinhardt@amd.com            except:
10958579Ssteve.reinhardt@amd.com                return False
10968579Ssteve.reinhardt@amd.com        else:
10978579Ssteve.reinhardt@amd.com            return False
10988579Ssteve.reinhardt@amd.com
10997777Sgblack@eecs.umich.edu    def verify(self):
11007777Sgblack@eecs.umich.edu        self.verifyIp()
11017777Sgblack@eecs.umich.edu        if self.netmask < 0 or self.netmask > 32:
11027777Sgblack@eecs.umich.edu            raise TypeError, "invalid netmask %d" % netmask
11037777Sgblack@eecs.umich.edu
11047777Sgblack@eecs.umich.edu    def getValue(self):
110511988Sandreas.sandberg@arm.com        from _m5.net import IpNetmask
11067777Sgblack@eecs.umich.edu        return IpNetmask(self.ip, self.netmask)
11077777Sgblack@eecs.umich.edu
11087777Sgblack@eecs.umich.edu# When initializing an IpWithPort, pass in an existing IpWithPort, a string of
11097777Sgblack@eecs.umich.edu# the form "a.b.c.d:p", or an ip and port as positional or keyword arguments.
11107777Sgblack@eecs.umich.educlass IpWithPort(IpAddress):
11117777Sgblack@eecs.umich.edu    cxx_type = 'Net::IpWithPort'
111210267SGeoffrey.Blake@arm.com    ex_str = "127.0.0.1:80"
111310267SGeoffrey.Blake@arm.com    cmd_line_settable = True
11147777Sgblack@eecs.umich.edu
11157777Sgblack@eecs.umich.edu    @classmethod
11167777Sgblack@eecs.umich.edu    def cxx_predecls(cls, code):
11177777Sgblack@eecs.umich.edu        code('#include "base/inet.hh"')
11187777Sgblack@eecs.umich.edu
11197777Sgblack@eecs.umich.edu    @classmethod
11207777Sgblack@eecs.umich.edu    def swig_predecls(cls, code):
11217777Sgblack@eecs.umich.edu        code('%include "python/swig/inet.i"')
11227777Sgblack@eecs.umich.edu
11237777Sgblack@eecs.umich.edu    def __init__(self, *args, **kwargs):
11247777Sgblack@eecs.umich.edu        def handle_kwarg(self, kwargs, key, elseVal = None):
11257777Sgblack@eecs.umich.edu            if key in kwargs:
11267777Sgblack@eecs.umich.edu                setattr(self, key, kwargs.pop(key))
11277777Sgblack@eecs.umich.edu            elif elseVal:
11287777Sgblack@eecs.umich.edu                setattr(self, key, elseVal)
11297777Sgblack@eecs.umich.edu            else:
11307777Sgblack@eecs.umich.edu                raise TypeError, "No value set for %s" % key
11317777Sgblack@eecs.umich.edu
11327777Sgblack@eecs.umich.edu        if len(args) == 0:
11337777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'ip')
11347777Sgblack@eecs.umich.edu            handle_kwarg(self, kwargs, 'port')
11357777Sgblack@eecs.umich.edu
11367777Sgblack@eecs.umich.edu        elif len(args) == 1:
11377777Sgblack@eecs.umich.edu            if kwargs:
11387777Sgblack@eecs.umich.edu                if not 'ip' in kwargs and not 'port' in kwargs:
11397777Sgblack@eecs.umich.edu                    raise TypeError, "Invalid arguments"
11407777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'ip', args[0])
11417777Sgblack@eecs.umich.edu                handle_kwarg(self, kwargs, 'port', args[0])
11427777Sgblack@eecs.umich.edu            elif isinstance(args[0], IpWithPort):
11437777Sgblack@eecs.umich.edu                self.ip = args[0].ip
11447777Sgblack@eecs.umich.edu                self.port = args[0].port
11457777Sgblack@eecs.umich.edu            else:
11467777Sgblack@eecs.umich.edu                (self.ip, self.port) = convert.toIpWithPort(args[0])
11477777Sgblack@eecs.umich.edu
11487777Sgblack@eecs.umich.edu        elif len(args) == 2:
11497777Sgblack@eecs.umich.edu            self.ip = args[0]
11507777Sgblack@eecs.umich.edu            self.port = args[1]
11517777Sgblack@eecs.umich.edu        else:
11527777Sgblack@eecs.umich.edu            raise TypeError, "Too many arguments specified"
11537777Sgblack@eecs.umich.edu
11547777Sgblack@eecs.umich.edu        if kwargs:
11557777Sgblack@eecs.umich.edu            raise TypeError, "Too many keywords: %s" % kwargs.keys()
11567777Sgblack@eecs.umich.edu
11577777Sgblack@eecs.umich.edu        self.verify()
11587777Sgblack@eecs.umich.edu
115910267SGeoffrey.Blake@arm.com    def __call__(self, value):
116010267SGeoffrey.Blake@arm.com        self.__init__(value)
116110267SGeoffrey.Blake@arm.com        return value
116210267SGeoffrey.Blake@arm.com
11638579Ssteve.reinhardt@amd.com    def __str__(self):
11648579Ssteve.reinhardt@amd.com        return "%s:%d" % (super(IpWithPort, self).__str__(), self.port)
11658579Ssteve.reinhardt@amd.com
11668579Ssteve.reinhardt@amd.com    def __eq__(self, other):
11678579Ssteve.reinhardt@amd.com        if isinstance(other, IpWithPort):
11688579Ssteve.reinhardt@amd.com            return self.ip == other.ip and self.port == other.port
11698579Ssteve.reinhardt@amd.com        elif isinstance(other, str):
11708579Ssteve.reinhardt@amd.com            try:
11718579Ssteve.reinhardt@amd.com                return (self.ip, self.port) == convert.toIpWithPort(other)
11728579Ssteve.reinhardt@amd.com            except:
11738579Ssteve.reinhardt@amd.com                return False
11748579Ssteve.reinhardt@amd.com        else:
11758579Ssteve.reinhardt@amd.com            return False
11768579Ssteve.reinhardt@amd.com
11777777Sgblack@eecs.umich.edu    def verify(self):
11787777Sgblack@eecs.umich.edu        self.verifyIp()
11797777Sgblack@eecs.umich.edu        if self.port < 0 or self.port > 0xffff:
11807777Sgblack@eecs.umich.edu            raise TypeError, "invalid port %d" % self.port
11817777Sgblack@eecs.umich.edu
11827777Sgblack@eecs.umich.edu    def getValue(self):
118311988Sandreas.sandberg@arm.com        from _m5.net import IpWithPort
11847777Sgblack@eecs.umich.edu        return IpWithPort(self.ip, self.port)
11857777Sgblack@eecs.umich.edu
11863932Sbinkertn@umich.edutime_formats = [ "%a %b %d %H:%M:%S %Z %Y",
118710380SAndrew.Bardsley@arm.com                 "%a %b %d %H:%M:%S %Y",
11883932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M:%S",
11893932Sbinkertn@umich.edu                 "%Y/%m/%d %H:%M",
11903932Sbinkertn@umich.edu                 "%Y/%m/%d",
11913932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M:%S",
11923932Sbinkertn@umich.edu                 "%m/%d/%Y %H:%M",
11933932Sbinkertn@umich.edu                 "%m/%d/%Y",
11943932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M:%S",
11953932Sbinkertn@umich.edu                 "%m/%d/%y %H:%M",
11963932Sbinkertn@umich.edu                 "%m/%d/%y"]
11973932Sbinkertn@umich.edu
11983932Sbinkertn@umich.edu
11993885Sbinkertn@umich.edudef parse_time(value):
12003932Sbinkertn@umich.edu    from time import gmtime, strptime, struct_time, time
12013932Sbinkertn@umich.edu    from datetime import datetime, date
12023885Sbinkertn@umich.edu
12033932Sbinkertn@umich.edu    if isinstance(value, struct_time):
12043932Sbinkertn@umich.edu        return value
12053932Sbinkertn@umich.edu
12063932Sbinkertn@umich.edu    if isinstance(value, (int, long)):
12073932Sbinkertn@umich.edu        return gmtime(value)
12083932Sbinkertn@umich.edu
12093932Sbinkertn@umich.edu    if isinstance(value, (datetime, date)):
12103932Sbinkertn@umich.edu        return value.timetuple()
12113932Sbinkertn@umich.edu
12123932Sbinkertn@umich.edu    if isinstance(value, str):
12133932Sbinkertn@umich.edu        if value in ('Now', 'Today'):
12143932Sbinkertn@umich.edu            return time.gmtime(time.time())
12153932Sbinkertn@umich.edu
12163932Sbinkertn@umich.edu        for format in time_formats:
12173932Sbinkertn@umich.edu            try:
12183932Sbinkertn@umich.edu                return strptime(value, format)
12193932Sbinkertn@umich.edu            except ValueError:
12203932Sbinkertn@umich.edu                pass
12213885Sbinkertn@umich.edu
12223885Sbinkertn@umich.edu    raise ValueError, "Could not parse '%s' as a time" % value
12233885Sbinkertn@umich.edu
12243885Sbinkertn@umich.educlass Time(ParamValue):
12254762Snate@binkert.org    cxx_type = 'tm'
12267673Snate@binkert.org
12277673Snate@binkert.org    @classmethod
12287673Snate@binkert.org    def cxx_predecls(cls, code):
12297673Snate@binkert.org        code('#include <time.h>')
12307673Snate@binkert.org
12317673Snate@binkert.org    @classmethod
12327673Snate@binkert.org    def swig_predecls(cls, code):
12337673Snate@binkert.org        code('%include "python/swig/time.i"')
12347673Snate@binkert.org
12353885Sbinkertn@umich.edu    def __init__(self, value):
12363932Sbinkertn@umich.edu        self.value = parse_time(value)
12373885Sbinkertn@umich.edu
123810267SGeoffrey.Blake@arm.com    def __call__(self, value):
123910267SGeoffrey.Blake@arm.com        self.__init__(value)
124010267SGeoffrey.Blake@arm.com        return value
124110267SGeoffrey.Blake@arm.com
12424762Snate@binkert.org    def getValue(self):
124311988Sandreas.sandberg@arm.com        from _m5.core import tm
124411988Sandreas.sandberg@arm.com        import calendar
12454762Snate@binkert.org
124611988Sandreas.sandberg@arm.com        return tm.gmtime(calendar.timegm(self.value))
12474762Snate@binkert.org
12483885Sbinkertn@umich.edu    def __str__(self):
12494762Snate@binkert.org        return time.asctime(self.value)
12503885Sbinkertn@umich.edu
12513885Sbinkertn@umich.edu    def ini_str(self):
12523932Sbinkertn@umich.edu        return str(self)
12533885Sbinkertn@umich.edu
12548664SAli.Saidi@ARM.com    def get_config_as_dict(self):
125510380SAndrew.Bardsley@arm.com        assert false
12568664SAli.Saidi@ARM.com        return str(self)
12578664SAli.Saidi@ARM.com
125810458Sandreas.hansson@arm.com    @classmethod
125910458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
126010458Sandreas.hansson@arm.com        code('#include <time.h>')
126110458Sandreas.hansson@arm.com
126210458Sandreas.hansson@arm.com    @classmethod
126310458Sandreas.hansson@arm.com    def cxx_ini_parse(cls, code, src, dest, ret):
126410458Sandreas.hansson@arm.com        code('char *_parse_ret = strptime((${src}).c_str(),')
126510458Sandreas.hansson@arm.com        code('    "%a %b %d %H:%M:%S %Y", &(${dest}));')
126610458Sandreas.hansson@arm.com        code('${ret} _parse_ret && *_parse_ret == \'\\0\';');
126710458Sandreas.hansson@arm.com
12683101Sstever@eecs.umich.edu# Enumerated types are a little more complex.  The user specifies the
12693101Sstever@eecs.umich.edu# type as Enum(foo) where foo is either a list or dictionary of
12703101Sstever@eecs.umich.edu# alternatives (typically strings, but not necessarily so).  (In the
12713101Sstever@eecs.umich.edu# long run, the integer value of the parameter will be the list index
12723101Sstever@eecs.umich.edu# or the corresponding dictionary value.  For now, since we only check
12733101Sstever@eecs.umich.edu# that the alternative is valid and then spit it into a .ini file,
12743101Sstever@eecs.umich.edu# there's not much point in using the dictionary.)
12753101Sstever@eecs.umich.edu
12763101Sstever@eecs.umich.edu# What Enum() must do is generate a new type encapsulating the
12773101Sstever@eecs.umich.edu# provided list/dictionary so that specific values of the parameter
12783101Sstever@eecs.umich.edu# can be instances of that type.  We define two hidden internal
12793101Sstever@eecs.umich.edu# classes (_ListEnum and _DictEnum) to serve as base classes, then
12803101Sstever@eecs.umich.edu# derive the new type from the appropriate base class on the fly.
12813101Sstever@eecs.umich.edu
12824762Snate@binkert.orgallEnums = {}
12833101Sstever@eecs.umich.edu# Metaclass for Enum types
12845033Smilesck@eecs.umich.educlass MetaEnum(MetaParamValue):
12854762Snate@binkert.org    def __new__(mcls, name, bases, dict):
12864762Snate@binkert.org        assert name not in allEnums
12874762Snate@binkert.org
12884762Snate@binkert.org        cls = super(MetaEnum, mcls).__new__(mcls, name, bases, dict)
12894762Snate@binkert.org        allEnums[name] = cls
12904762Snate@binkert.org        return cls
12914762Snate@binkert.org
12923101Sstever@eecs.umich.edu    def __init__(cls, name, bases, init_dict):
12933101Sstever@eecs.umich.edu        if init_dict.has_key('map'):
12943101Sstever@eecs.umich.edu            if not isinstance(cls.map, dict):
12953101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'map' " \
12963101Sstever@eecs.umich.edu                      "must be of type dict"
12973101Sstever@eecs.umich.edu            # build list of value strings from map
12983101Sstever@eecs.umich.edu            cls.vals = cls.map.keys()
12993101Sstever@eecs.umich.edu            cls.vals.sort()
13003101Sstever@eecs.umich.edu        elif init_dict.has_key('vals'):
13013101Sstever@eecs.umich.edu            if not isinstance(cls.vals, list):
13023101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'vals' " \
13033101Sstever@eecs.umich.edu                      "must be of type list"
13043101Sstever@eecs.umich.edu            # build string->value map from vals sequence
13053101Sstever@eecs.umich.edu            cls.map = {}
13063101Sstever@eecs.umich.edu            for idx,val in enumerate(cls.vals):
13073101Sstever@eecs.umich.edu                cls.map[val] = idx
13083101Sstever@eecs.umich.edu        else:
13093101Sstever@eecs.umich.edu            raise TypeError, "Enum-derived class must define "\
13103101Sstever@eecs.umich.edu                  "attribute 'map' or 'vals'"
13113101Sstever@eecs.umich.edu
13124762Snate@binkert.org        cls.cxx_type = 'Enums::%s' % name
13133101Sstever@eecs.umich.edu
13143101Sstever@eecs.umich.edu        super(MetaEnum, cls).__init__(name, bases, init_dict)
13153101Sstever@eecs.umich.edu
13163101Sstever@eecs.umich.edu    # Generate C++ class declaration for this enum type.
13173101Sstever@eecs.umich.edu    # Note that we wrap the enum in a class/struct to act as a namespace,
13183101Sstever@eecs.umich.edu    # so that the enum strings can be brief w/o worrying about collisions.
13197673Snate@binkert.org    def cxx_decl(cls, code):
132010201SAndrew.Bardsley@arm.com        wrapper_name = cls.wrapper_name
132110201SAndrew.Bardsley@arm.com        wrapper = 'struct' if cls.wrapper_is_struct else 'namespace'
132210201SAndrew.Bardsley@arm.com        name = cls.__name__ if cls.enum_name is None else cls.enum_name
132310201SAndrew.Bardsley@arm.com        idem_macro = '__ENUM__%s__%s__' % (wrapper_name, name)
132410201SAndrew.Bardsley@arm.com
13257673Snate@binkert.org        code('''\
132610201SAndrew.Bardsley@arm.com#ifndef $idem_macro
132710201SAndrew.Bardsley@arm.com#define $idem_macro
13287673Snate@binkert.org
132910201SAndrew.Bardsley@arm.com$wrapper $wrapper_name {
13307673Snate@binkert.org    enum $name {
13317673Snate@binkert.org''')
13327673Snate@binkert.org        code.indent(2)
13334762Snate@binkert.org        for val in cls.vals:
13347673Snate@binkert.org            code('$val = ${{cls.map[val]}},')
13358902Sandreas.hansson@arm.com        code('Num_$name = ${{len(cls.vals)}}')
13367673Snate@binkert.org        code.dedent(2)
133710201SAndrew.Bardsley@arm.com        code('    };')
13384762Snate@binkert.org
133910201SAndrew.Bardsley@arm.com        if cls.wrapper_is_struct:
134010201SAndrew.Bardsley@arm.com            code('    static const char *${name}Strings[Num_${name}];')
134110201SAndrew.Bardsley@arm.com            code('};')
134210201SAndrew.Bardsley@arm.com        else:
134310201SAndrew.Bardsley@arm.com            code('extern const char *${name}Strings[Num_${name}];')
134410201SAndrew.Bardsley@arm.com            code('}')
134510201SAndrew.Bardsley@arm.com
134610201SAndrew.Bardsley@arm.com        code()
134710201SAndrew.Bardsley@arm.com        code('#endif // $idem_macro')
13487673Snate@binkert.org
13497673Snate@binkert.org    def cxx_def(cls, code):
135010201SAndrew.Bardsley@arm.com        wrapper_name = cls.wrapper_name
135110201SAndrew.Bardsley@arm.com        file_name = cls.__name__
135210201SAndrew.Bardsley@arm.com        name = cls.__name__ if cls.enum_name is None else cls.enum_name
135310201SAndrew.Bardsley@arm.com
135410201SAndrew.Bardsley@arm.com        code('#include "enums/$file_name.hh"')
135510201SAndrew.Bardsley@arm.com        if cls.wrapper_is_struct:
135610201SAndrew.Bardsley@arm.com            code('const char *${wrapper_name}::${name}Strings'
135710201SAndrew.Bardsley@arm.com                '[Num_${name}] =')
135810201SAndrew.Bardsley@arm.com        else:
135910201SAndrew.Bardsley@arm.com            code('namespace Enums {')
136010201SAndrew.Bardsley@arm.com            code.indent(1)
136110201SAndrew.Bardsley@arm.com            code(' const char *${name}Strings[Num_${name}] =')
136210201SAndrew.Bardsley@arm.com
136310201SAndrew.Bardsley@arm.com        code('{')
136410201SAndrew.Bardsley@arm.com        code.indent(1)
13654762Snate@binkert.org        for val in cls.vals:
13667673Snate@binkert.org            code('"$val",')
136710201SAndrew.Bardsley@arm.com        code.dedent(1)
136810201SAndrew.Bardsley@arm.com        code('};')
136910201SAndrew.Bardsley@arm.com
137010201SAndrew.Bardsley@arm.com        if not cls.wrapper_is_struct:
137110201SAndrew.Bardsley@arm.com            code('} // namespace $wrapper_name')
137210201SAndrew.Bardsley@arm.com            code.dedent(1)
13733101Sstever@eecs.umich.edu
137411988Sandreas.sandberg@arm.com    def pybind_def(cls, code):
137511988Sandreas.sandberg@arm.com        name = cls.__name__
137611988Sandreas.sandberg@arm.com        wrapper_name = cls.wrapper_name
137711988Sandreas.sandberg@arm.com        enum_name = cls.__name__ if cls.enum_name is None else cls.enum_name
137811988Sandreas.sandberg@arm.com
137911988Sandreas.sandberg@arm.com        code('''#include "pybind11/pybind11.h"
138011988Sandreas.sandberg@arm.com#include "pybind11/stl.h"
138111988Sandreas.sandberg@arm.com
138211988Sandreas.sandberg@arm.com#include <sim/init.hh>
138311988Sandreas.sandberg@arm.com
138411988Sandreas.sandberg@arm.comnamespace py = pybind11;
138511988Sandreas.sandberg@arm.com
138611988Sandreas.sandberg@arm.comstatic void
138711988Sandreas.sandberg@arm.commodule_init(py::module &m_internal)
138811988Sandreas.sandberg@arm.com{
138911988Sandreas.sandberg@arm.com    py::module m = m_internal.def_submodule("enum_${name}");
139011988Sandreas.sandberg@arm.com
139111988Sandreas.sandberg@arm.com    py::enum_<${wrapper_name}::${enum_name}>(m, "enum_${name}")
139211988Sandreas.sandberg@arm.com''')
139311988Sandreas.sandberg@arm.com
139411988Sandreas.sandberg@arm.com        code.indent()
139511988Sandreas.sandberg@arm.com        code.indent()
139611988Sandreas.sandberg@arm.com        for val in cls.vals:
139711988Sandreas.sandberg@arm.com            code('.value("${val}", ${wrapper_name}::${val})')
139811988Sandreas.sandberg@arm.com        code('.value("Num_${name}", ${wrapper_name}::Num_${enum_name})')
139911988Sandreas.sandberg@arm.com        code('.export_values()')
140011988Sandreas.sandberg@arm.com        code(';')
140111988Sandreas.sandberg@arm.com        code.dedent()
140211988Sandreas.sandberg@arm.com
140311988Sandreas.sandberg@arm.com        code('}')
140411988Sandreas.sandberg@arm.com        code.dedent()
140511988Sandreas.sandberg@arm.com        code()
140611988Sandreas.sandberg@arm.com        code('static EmbeddedPyBind embed_enum("enum_${name}", module_init);')
140711988Sandreas.sandberg@arm.com
14088596Ssteve.reinhardt@amd.com    def swig_decl(cls, code):
14098596Ssteve.reinhardt@amd.com        name = cls.__name__
14108596Ssteve.reinhardt@amd.com        code('''\
141111802Sandreas.sandberg@arm.com%module(package="_m5") enum_$name
14128596Ssteve.reinhardt@amd.com
14138596Ssteve.reinhardt@amd.com%{
14148596Ssteve.reinhardt@amd.com#include "enums/$name.hh"
14158596Ssteve.reinhardt@amd.com%}
14168596Ssteve.reinhardt@amd.com
14178596Ssteve.reinhardt@amd.com%include "enums/$name.hh"
14188596Ssteve.reinhardt@amd.com''')
14198596Ssteve.reinhardt@amd.com
14208596Ssteve.reinhardt@amd.com
14213101Sstever@eecs.umich.edu# Base class for enum types.
14223101Sstever@eecs.umich.educlass Enum(ParamValue):
14233101Sstever@eecs.umich.edu    __metaclass__ = MetaEnum
14243101Sstever@eecs.umich.edu    vals = []
142510267SGeoffrey.Blake@arm.com    cmd_line_settable = True
14263101Sstever@eecs.umich.edu
142710201SAndrew.Bardsley@arm.com    # The name of the wrapping namespace or struct
142810201SAndrew.Bardsley@arm.com    wrapper_name = 'Enums'
142910201SAndrew.Bardsley@arm.com
143010201SAndrew.Bardsley@arm.com    # If true, the enum is wrapped in a struct rather than a namespace
143110201SAndrew.Bardsley@arm.com    wrapper_is_struct = False
143210201SAndrew.Bardsley@arm.com
143310201SAndrew.Bardsley@arm.com    # If not None, use this as the enum name rather than this class name
143410201SAndrew.Bardsley@arm.com    enum_name = None
143510201SAndrew.Bardsley@arm.com
14363101Sstever@eecs.umich.edu    def __init__(self, value):
14373101Sstever@eecs.umich.edu        if value not in self.map:
14383101Sstever@eecs.umich.edu            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
14393101Sstever@eecs.umich.edu                  % (value, self.vals)
14403101Sstever@eecs.umich.edu        self.value = value
14413101Sstever@eecs.umich.edu
144210267SGeoffrey.Blake@arm.com    def __call__(self, value):
144310267SGeoffrey.Blake@arm.com        self.__init__(value)
144410267SGeoffrey.Blake@arm.com        return value
144510267SGeoffrey.Blake@arm.com
14467675Snate@binkert.org    @classmethod
14477675Snate@binkert.org    def cxx_predecls(cls, code):
14487675Snate@binkert.org        code('#include "enums/$0.hh"', cls.__name__)
14497675Snate@binkert.org
14507675Snate@binkert.org    @classmethod
14517675Snate@binkert.org    def swig_predecls(cls, code):
145211802Sandreas.sandberg@arm.com        code('%import "python/_m5/enum_$0.i"', cls.__name__)
14537675Snate@binkert.org
145410458Sandreas.hansson@arm.com    @classmethod
145510458Sandreas.hansson@arm.com    def cxx_ini_parse(cls, code, src, dest, ret):
145610458Sandreas.hansson@arm.com        code('if (false) {')
145710458Sandreas.hansson@arm.com        for elem_name in cls.map.iterkeys():
145810458Sandreas.hansson@arm.com            code('} else if (%s == "%s") {' % (src, elem_name))
145910458Sandreas.hansson@arm.com            code.indent()
146010458Sandreas.hansson@arm.com            code('%s = Enums::%s;' % (dest, elem_name))
146110458Sandreas.hansson@arm.com            code('%s true;' % ret)
146210458Sandreas.hansson@arm.com            code.dedent()
146310458Sandreas.hansson@arm.com        code('} else {')
146410458Sandreas.hansson@arm.com        code('    %s false;' % ret)
146510458Sandreas.hansson@arm.com        code('}')
146610458Sandreas.hansson@arm.com
14674762Snate@binkert.org    def getValue(self):
146811988Sandreas.sandberg@arm.com        import m5.internal.params
146911988Sandreas.sandberg@arm.com        e = getattr(m5.internal.params, "enum_%s" % self.__class__.__name__)
147011988Sandreas.sandberg@arm.com        return e(self.map[self.value])
14714762Snate@binkert.org
14723101Sstever@eecs.umich.edu    def __str__(self):
14733101Sstever@eecs.umich.edu        return self.value
14743101Sstever@eecs.umich.edu
14753101Sstever@eecs.umich.edu# how big does a rounding error need to be before we warn about it?
14763101Sstever@eecs.umich.edufrequency_tolerance = 0.001  # 0.1%
14773101Sstever@eecs.umich.edu
14784167Sbinkertn@umich.educlass TickParamValue(NumericParamValue):
14793101Sstever@eecs.umich.edu    cxx_type = 'Tick'
148010267SGeoffrey.Blake@arm.com    ex_str = "1MHz"
148110267SGeoffrey.Blake@arm.com    cmd_line_settable = True
14827673Snate@binkert.org
14837673Snate@binkert.org    @classmethod
14847673Snate@binkert.org    def cxx_predecls(cls, code):
14857673Snate@binkert.org        code('#include "base/types.hh"')
14867673Snate@binkert.org
14877673Snate@binkert.org    @classmethod
14887673Snate@binkert.org    def swig_predecls(cls, code):
14897673Snate@binkert.org        code('%import "stdint.i"')
14907673Snate@binkert.org        code('%import "base/types.hh"')
14914167Sbinkertn@umich.edu
149210267SGeoffrey.Blake@arm.com    def __call__(self, value):
149310267SGeoffrey.Blake@arm.com        self.__init__(value)
149410267SGeoffrey.Blake@arm.com        return value
149510267SGeoffrey.Blake@arm.com
14964762Snate@binkert.org    def getValue(self):
14974762Snate@binkert.org        return long(self.value)
14984762Snate@binkert.org
149910458Sandreas.hansson@arm.com    @classmethod
150010458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
150110458Sandreas.hansson@arm.com        code('#include <sstream>')
150210458Sandreas.hansson@arm.com
150310458Sandreas.hansson@arm.com    # Ticks are expressed in seconds in JSON files and in plain
150410458Sandreas.hansson@arm.com    # Ticks in .ini files.  Switch based on a config flag
150510458Sandreas.hansson@arm.com    @classmethod
150610458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
150710458Sandreas.hansson@arm.com        code('${ret} to_number(${src}, ${dest});')
150810458Sandreas.hansson@arm.com
15094167Sbinkertn@umich.educlass Latency(TickParamValue):
151010267SGeoffrey.Blake@arm.com    ex_str = "100ns"
151110267SGeoffrey.Blake@arm.com
15123101Sstever@eecs.umich.edu    def __init__(self, value):
15134167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
15144167Sbinkertn@umich.edu            self.ticks = value.ticks
15154167Sbinkertn@umich.edu            self.value = value.value
15164167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
15174167Sbinkertn@umich.edu            self.ticks = value.ticks
15184167Sbinkertn@umich.edu            self.value = 1.0 / value.value
15194167Sbinkertn@umich.edu        elif value.endswith('t'):
15204167Sbinkertn@umich.edu            self.ticks = True
15214167Sbinkertn@umich.edu            self.value = int(value[:-1])
15224167Sbinkertn@umich.edu        else:
15234167Sbinkertn@umich.edu            self.ticks = False
15244167Sbinkertn@umich.edu            self.value = convert.toLatency(value)
15253101Sstever@eecs.umich.edu
152610267SGeoffrey.Blake@arm.com    def __call__(self, value):
152710267SGeoffrey.Blake@arm.com        self.__init__(value)
152810267SGeoffrey.Blake@arm.com        return value
152910267SGeoffrey.Blake@arm.com
15303101Sstever@eecs.umich.edu    def __getattr__(self, attr):
15313101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
15323101Sstever@eecs.umich.edu            return self
15333101Sstever@eecs.umich.edu        if attr == 'frequency':
15343101Sstever@eecs.umich.edu            return Frequency(self)
15353101Sstever@eecs.umich.edu        raise AttributeError, "Latency object has no attribute '%s'" % attr
15363101Sstever@eecs.umich.edu
15374762Snate@binkert.org    def getValue(self):
15384762Snate@binkert.org        if self.ticks or self.value == 0:
15394762Snate@binkert.org            value = self.value
15404762Snate@binkert.org        else:
15414762Snate@binkert.org            value = ticks.fromSeconds(self.value)
15424762Snate@binkert.org        return long(value)
15434762Snate@binkert.org
154410380SAndrew.Bardsley@arm.com    def config_value(self):
154510380SAndrew.Bardsley@arm.com        return self.getValue()
154610380SAndrew.Bardsley@arm.com
15473101Sstever@eecs.umich.edu    # convert latency to ticks
15483101Sstever@eecs.umich.edu    def ini_str(self):
15494762Snate@binkert.org        return '%d' % self.getValue()
15503101Sstever@eecs.umich.edu
15514167Sbinkertn@umich.educlass Frequency(TickParamValue):
155210267SGeoffrey.Blake@arm.com    ex_str = "1GHz"
155310267SGeoffrey.Blake@arm.com
15543101Sstever@eecs.umich.edu    def __init__(self, value):
15554167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
15564167Sbinkertn@umich.edu            if value.value == 0:
15574167Sbinkertn@umich.edu                self.value = 0
15584167Sbinkertn@umich.edu            else:
15594167Sbinkertn@umich.edu                self.value = 1.0 / value.value
15604167Sbinkertn@umich.edu            self.ticks = value.ticks
15614167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
15624167Sbinkertn@umich.edu            self.value = value.value
15634167Sbinkertn@umich.edu            self.ticks = value.ticks
15644167Sbinkertn@umich.edu        else:
15654167Sbinkertn@umich.edu            self.ticks = False
15664167Sbinkertn@umich.edu            self.value = convert.toFrequency(value)
15673101Sstever@eecs.umich.edu
156810267SGeoffrey.Blake@arm.com    def __call__(self, value):
156910267SGeoffrey.Blake@arm.com        self.__init__(value)
157010267SGeoffrey.Blake@arm.com        return value
157110267SGeoffrey.Blake@arm.com
15723101Sstever@eecs.umich.edu    def __getattr__(self, attr):
15733101Sstever@eecs.umich.edu        if attr == 'frequency':
15743101Sstever@eecs.umich.edu            return self
15753101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
15763101Sstever@eecs.umich.edu            return Latency(self)
15773101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
15783101Sstever@eecs.umich.edu
15794167Sbinkertn@umich.edu    # convert latency to ticks
15804762Snate@binkert.org    def getValue(self):
15814762Snate@binkert.org        if self.ticks or self.value == 0:
15824762Snate@binkert.org            value = self.value
15834762Snate@binkert.org        else:
15844762Snate@binkert.org            value = ticks.fromSeconds(1.0 / self.value)
15854762Snate@binkert.org        return long(value)
15864762Snate@binkert.org
158710380SAndrew.Bardsley@arm.com    def config_value(self):
158810380SAndrew.Bardsley@arm.com        return self.getValue()
158910380SAndrew.Bardsley@arm.com
15903101Sstever@eecs.umich.edu    def ini_str(self):
15914762Snate@binkert.org        return '%d' % self.getValue()
15923101Sstever@eecs.umich.edu
159310019Sandreas.hansson@arm.com# A generic Frequency and/or Latency value. Value is stored as a
159410019Sandreas.hansson@arm.com# latency, just like Latency and Frequency.
159510019Sandreas.hansson@arm.comclass Clock(TickParamValue):
15963101Sstever@eecs.umich.edu    def __init__(self, value):
15974167Sbinkertn@umich.edu        if isinstance(value, (Latency, Clock)):
15984167Sbinkertn@umich.edu            self.ticks = value.ticks
15994167Sbinkertn@umich.edu            self.value = value.value
16004167Sbinkertn@umich.edu        elif isinstance(value, Frequency):
16014167Sbinkertn@umich.edu            self.ticks = value.ticks
16024167Sbinkertn@umich.edu            self.value = 1.0 / value.value
16034167Sbinkertn@umich.edu        elif value.endswith('t'):
16044167Sbinkertn@umich.edu            self.ticks = True
16054167Sbinkertn@umich.edu            self.value = int(value[:-1])
16064167Sbinkertn@umich.edu        else:
16074167Sbinkertn@umich.edu            self.ticks = False
16084167Sbinkertn@umich.edu            self.value = convert.anyToLatency(value)
16093101Sstever@eecs.umich.edu
161010267SGeoffrey.Blake@arm.com    def __call__(self, value):
161110267SGeoffrey.Blake@arm.com        self.__init__(value)
161210267SGeoffrey.Blake@arm.com        return value
161310267SGeoffrey.Blake@arm.com
161410267SGeoffrey.Blake@arm.com    def __str__(self):
161510267SGeoffrey.Blake@arm.com        return "%s" % Latency(self)
161610267SGeoffrey.Blake@arm.com
16173101Sstever@eecs.umich.edu    def __getattr__(self, attr):
16183101Sstever@eecs.umich.edu        if attr == 'frequency':
16193101Sstever@eecs.umich.edu            return Frequency(self)
16203101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
16213101Sstever@eecs.umich.edu            return Latency(self)
16223101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
16233101Sstever@eecs.umich.edu
16244762Snate@binkert.org    def getValue(self):
16254762Snate@binkert.org        return self.period.getValue()
16264762Snate@binkert.org
162710380SAndrew.Bardsley@arm.com    def config_value(self):
162810380SAndrew.Bardsley@arm.com        return self.period.config_value()
162910380SAndrew.Bardsley@arm.com
16303101Sstever@eecs.umich.edu    def ini_str(self):
16313101Sstever@eecs.umich.edu        return self.period.ini_str()
16323101Sstever@eecs.umich.edu
16339827Sakash.bagdia@arm.comclass Voltage(float,ParamValue):
16349827Sakash.bagdia@arm.com    cxx_type = 'double'
163510267SGeoffrey.Blake@arm.com    ex_str = "1V"
163611498Sakash.bagdia@ARM.com    cmd_line_settable = True
163710267SGeoffrey.Blake@arm.com
16389827Sakash.bagdia@arm.com    def __new__(cls, value):
16399827Sakash.bagdia@arm.com        # convert to voltage
16409827Sakash.bagdia@arm.com        val = convert.toVoltage(value)
16419827Sakash.bagdia@arm.com        return super(cls, Voltage).__new__(cls, val)
16429827Sakash.bagdia@arm.com
164310267SGeoffrey.Blake@arm.com    def __call__(self, value):
164410267SGeoffrey.Blake@arm.com        val = convert.toVoltage(value)
164510267SGeoffrey.Blake@arm.com        self.__init__(val)
164610267SGeoffrey.Blake@arm.com        return value
164710267SGeoffrey.Blake@arm.com
16489827Sakash.bagdia@arm.com    def __str__(self):
164910267SGeoffrey.Blake@arm.com        return str(self.getValue())
16509827Sakash.bagdia@arm.com
16519827Sakash.bagdia@arm.com    def getValue(self):
16529827Sakash.bagdia@arm.com        value = float(self)
16539827Sakash.bagdia@arm.com        return value
16549827Sakash.bagdia@arm.com
16559827Sakash.bagdia@arm.com    def ini_str(self):
16569827Sakash.bagdia@arm.com        return '%f' % self.getValue()
16579827Sakash.bagdia@arm.com
165810458Sandreas.hansson@arm.com    @classmethod
165910458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
166010458Sandreas.hansson@arm.com        code('#include <sstream>')
166110458Sandreas.hansson@arm.com
166210458Sandreas.hansson@arm.com    @classmethod
166310458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
166410458Sandreas.hansson@arm.com        code('%s (std::istringstream(%s) >> %s).eof();' % (ret, src, dest))
166510458Sandreas.hansson@arm.com
166610427Sandreas.hansson@arm.comclass Current(float, ParamValue):
166710427Sandreas.hansson@arm.com    cxx_type = 'double'
166810427Sandreas.hansson@arm.com    ex_str = "1mA"
166910427Sandreas.hansson@arm.com    cmd_line_settable = False
167010427Sandreas.hansson@arm.com
167110427Sandreas.hansson@arm.com    def __new__(cls, value):
167210427Sandreas.hansson@arm.com        # convert to current
167310427Sandreas.hansson@arm.com        val = convert.toCurrent(value)
167410427Sandreas.hansson@arm.com        return super(cls, Current).__new__(cls, val)
167510427Sandreas.hansson@arm.com
167610427Sandreas.hansson@arm.com    def __call__(self, value):
167710427Sandreas.hansson@arm.com        val = convert.toCurrent(value)
167810427Sandreas.hansson@arm.com        self.__init__(val)
167910427Sandreas.hansson@arm.com        return value
168010427Sandreas.hansson@arm.com
168110427Sandreas.hansson@arm.com    def __str__(self):
168210427Sandreas.hansson@arm.com        return str(self.getValue())
168310427Sandreas.hansson@arm.com
168410427Sandreas.hansson@arm.com    def getValue(self):
168510427Sandreas.hansson@arm.com        value = float(self)
168610427Sandreas.hansson@arm.com        return value
168710427Sandreas.hansson@arm.com
168810427Sandreas.hansson@arm.com    def ini_str(self):
168910427Sandreas.hansson@arm.com        return '%f' % self.getValue()
169010427Sandreas.hansson@arm.com
169110458Sandreas.hansson@arm.com    @classmethod
169210458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
169310458Sandreas.hansson@arm.com        code('#include <sstream>')
169410458Sandreas.hansson@arm.com
169510458Sandreas.hansson@arm.com    @classmethod
169610458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
169710458Sandreas.hansson@arm.com        code('%s (std::istringstream(%s) >> %s).eof();' % (ret, src, dest))
169810458Sandreas.hansson@arm.com
16993101Sstever@eecs.umich.educlass NetworkBandwidth(float,ParamValue):
17003101Sstever@eecs.umich.edu    cxx_type = 'float'
170110267SGeoffrey.Blake@arm.com    ex_str = "1Gbps"
170210267SGeoffrey.Blake@arm.com    cmd_line_settable = True
170310267SGeoffrey.Blake@arm.com
17043101Sstever@eecs.umich.edu    def __new__(cls, value):
17054167Sbinkertn@umich.edu        # convert to bits per second
17064167Sbinkertn@umich.edu        val = convert.toNetworkBandwidth(value)
17073101Sstever@eecs.umich.edu        return super(cls, NetworkBandwidth).__new__(cls, val)
17083101Sstever@eecs.umich.edu
17093101Sstever@eecs.umich.edu    def __str__(self):
17103101Sstever@eecs.umich.edu        return str(self.val)
17113101Sstever@eecs.umich.edu
171210267SGeoffrey.Blake@arm.com    def __call__(self, value):
171310267SGeoffrey.Blake@arm.com        val = convert.toNetworkBandwidth(value)
171410267SGeoffrey.Blake@arm.com        self.__init__(val)
171510267SGeoffrey.Blake@arm.com        return value
171610267SGeoffrey.Blake@arm.com
17174762Snate@binkert.org    def getValue(self):
17184167Sbinkertn@umich.edu        # convert to seconds per byte
17194167Sbinkertn@umich.edu        value = 8.0 / float(self)
17204167Sbinkertn@umich.edu        # convert to ticks per byte
17214762Snate@binkert.org        value = ticks.fromSeconds(value)
17224762Snate@binkert.org        return float(value)
17234762Snate@binkert.org
17244762Snate@binkert.org    def ini_str(self):
17254762Snate@binkert.org        return '%f' % self.getValue()
17263101Sstever@eecs.umich.edu
172710380SAndrew.Bardsley@arm.com    def config_value(self):
172810380SAndrew.Bardsley@arm.com        return '%f' % self.getValue()
172910380SAndrew.Bardsley@arm.com
173010458Sandreas.hansson@arm.com    @classmethod
173110458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
173210458Sandreas.hansson@arm.com        code('#include <sstream>')
173310458Sandreas.hansson@arm.com
173410458Sandreas.hansson@arm.com    @classmethod
173510458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
173610458Sandreas.hansson@arm.com        code('%s (std::istringstream(%s) >> %s).eof();' % (ret, src, dest))
173710458Sandreas.hansson@arm.com
17383101Sstever@eecs.umich.educlass MemoryBandwidth(float,ParamValue):
17393101Sstever@eecs.umich.edu    cxx_type = 'float'
174010267SGeoffrey.Blake@arm.com    ex_str = "1GB/s"
174110267SGeoffrey.Blake@arm.com    cmd_line_settable = True
174210267SGeoffrey.Blake@arm.com
17435469Snate@binkert.org    def __new__(cls, value):
17447743Sgblack@eecs.umich.edu        # convert to bytes per second
17453102Sstever@eecs.umich.edu        val = convert.toMemoryBandwidth(value)
17463101Sstever@eecs.umich.edu        return super(cls, MemoryBandwidth).__new__(cls, val)
17473101Sstever@eecs.umich.edu
174810267SGeoffrey.Blake@arm.com    def __call__(self, value):
174910267SGeoffrey.Blake@arm.com        val = convert.toMemoryBandwidth(value)
175010267SGeoffrey.Blake@arm.com        self.__init__(val)
175110267SGeoffrey.Blake@arm.com        return value
17523101Sstever@eecs.umich.edu
17534762Snate@binkert.org    def getValue(self):
17544167Sbinkertn@umich.edu        # convert to seconds per byte
17555468Snate@binkert.org        value = float(self)
17565468Snate@binkert.org        if value:
17575468Snate@binkert.org            value = 1.0 / float(self)
17584167Sbinkertn@umich.edu        # convert to ticks per byte
17594762Snate@binkert.org        value = ticks.fromSeconds(value)
17604762Snate@binkert.org        return float(value)
17614762Snate@binkert.org
17624762Snate@binkert.org    def ini_str(self):
17634762Snate@binkert.org        return '%f' % self.getValue()
17643101Sstever@eecs.umich.edu
176510380SAndrew.Bardsley@arm.com    def config_value(self):
176610380SAndrew.Bardsley@arm.com        return '%f' % self.getValue()
176710380SAndrew.Bardsley@arm.com
176810458Sandreas.hansson@arm.com    @classmethod
176910458Sandreas.hansson@arm.com    def cxx_ini_predecls(cls, code):
177010458Sandreas.hansson@arm.com        code('#include <sstream>')
177110458Sandreas.hansson@arm.com
177210458Sandreas.hansson@arm.com    @classmethod
177310458Sandreas.hansson@arm.com    def cxx_ini_parse(self, code, src, dest, ret):
177410458Sandreas.hansson@arm.com        code('%s (std::istringstream(%s) >> %s).eof();' % (ret, src, dest))
177510458Sandreas.hansson@arm.com
17763101Sstever@eecs.umich.edu#
17773101Sstever@eecs.umich.edu# "Constants"... handy aliases for various values.
17783101Sstever@eecs.umich.edu#
17793101Sstever@eecs.umich.edu
17803102Sstever@eecs.umich.edu# Special class for NULL pointers.  Note the special check in
17813102Sstever@eecs.umich.edu# make_param_value() above that lets these be assigned where a
17823102Sstever@eecs.umich.edu# SimObject is required.
17833102Sstever@eecs.umich.edu# only one copy of a particular node
17843102Sstever@eecs.umich.educlass NullSimObject(object):
17853102Sstever@eecs.umich.edu    __metaclass__ = Singleton
17863102Sstever@eecs.umich.edu
17873102Sstever@eecs.umich.edu    def __call__(cls):
17883102Sstever@eecs.umich.edu        return cls
17893102Sstever@eecs.umich.edu
17903102Sstever@eecs.umich.edu    def _instantiate(self, parent = None, path = ''):
17913102Sstever@eecs.umich.edu        pass
17923102Sstever@eecs.umich.edu
17933102Sstever@eecs.umich.edu    def ini_str(self):
17943102Sstever@eecs.umich.edu        return 'Null'
17953102Sstever@eecs.umich.edu
17963102Sstever@eecs.umich.edu    def unproxy(self, base):
17973102Sstever@eecs.umich.edu        return self
17983102Sstever@eecs.umich.edu
17993102Sstever@eecs.umich.edu    def set_path(self, parent, name):
18003102Sstever@eecs.umich.edu        pass
18014762Snate@binkert.org
18023102Sstever@eecs.umich.edu    def __str__(self):
18033102Sstever@eecs.umich.edu        return 'Null'
18043102Sstever@eecs.umich.edu
180510380SAndrew.Bardsley@arm.com    def config_value(self):
180610380SAndrew.Bardsley@arm.com        return None
180710380SAndrew.Bardsley@arm.com
18084762Snate@binkert.org    def getValue(self):
18094762Snate@binkert.org        return None
18104762Snate@binkert.org
18113102Sstever@eecs.umich.edu# The only instance you'll ever need...
18123102Sstever@eecs.umich.eduNULL = NullSimObject()
18133102Sstever@eecs.umich.edu
18143102Sstever@eecs.umich.edudef isNullPointer(value):
18153102Sstever@eecs.umich.edu    return isinstance(value, NullSimObject)
18163102Sstever@eecs.umich.edu
18173101Sstever@eecs.umich.edu# Some memory range specifications use this as a default upper bound.
18183101Sstever@eecs.umich.eduMaxAddr = Addr.max
18193101Sstever@eecs.umich.eduMaxTick = Tick.max
18203101Sstever@eecs.umich.eduAllMemory = AddrRange(0, MaxAddr)
18213101Sstever@eecs.umich.edu
18223101Sstever@eecs.umich.edu
18233101Sstever@eecs.umich.edu#####################################################################
18243101Sstever@eecs.umich.edu#
18253101Sstever@eecs.umich.edu# Port objects
18263101Sstever@eecs.umich.edu#
18273101Sstever@eecs.umich.edu# Ports are used to interconnect objects in the memory system.
18283101Sstever@eecs.umich.edu#
18293101Sstever@eecs.umich.edu#####################################################################
18303101Sstever@eecs.umich.edu
18313101Sstever@eecs.umich.edu# Port reference: encapsulates a reference to a particular port on a
18323101Sstever@eecs.umich.edu# particular SimObject.
18333101Sstever@eecs.umich.educlass PortRef(object):
18348839Sandreas.hansson@arm.com    def __init__(self, simobj, name, role):
18353105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
18363101Sstever@eecs.umich.edu        self.simobj = simobj
18373101Sstever@eecs.umich.edu        self.name = name
18388839Sandreas.hansson@arm.com        self.role = role
18393101Sstever@eecs.umich.edu        self.peer = None   # not associated with another port yet
18403101Sstever@eecs.umich.edu        self.ccConnected = False # C++ port connection done?
18413105Sstever@eecs.umich.edu        self.index = -1  # always -1 for non-vector ports
18423101Sstever@eecs.umich.edu
18433103Sstever@eecs.umich.edu    def __str__(self):
18443105Sstever@eecs.umich.edu        return '%s.%s' % (self.simobj, self.name)
18453103Sstever@eecs.umich.edu
18468840Sandreas.hansson@arm.com    def __len__(self):
18478840Sandreas.hansson@arm.com        # Return the number of connected ports, i.e. 0 is we have no
18488840Sandreas.hansson@arm.com        # peer and 1 if we do.
18498840Sandreas.hansson@arm.com        return int(self.peer != None)
18508840Sandreas.hansson@arm.com
18513105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
18523105Sstever@eecs.umich.edu    def ini_str(self):
18533105Sstever@eecs.umich.edu        return str(self.peer)
18543105Sstever@eecs.umich.edu
18559017Sandreas.hansson@arm.com    # for config.json
18569017Sandreas.hansson@arm.com    def get_config_as_dict(self):
18579017Sandreas.hansson@arm.com        return {'role' : self.role, 'peer' : str(self.peer)}
18589017Sandreas.hansson@arm.com
18593105Sstever@eecs.umich.edu    def __getattr__(self, attr):
18603105Sstever@eecs.umich.edu        if attr == 'peerObj':
18613105Sstever@eecs.umich.edu            # shorthand for proxies
18623105Sstever@eecs.umich.edu            return self.peer.simobj
18633105Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
18643105Sstever@eecs.umich.edu              (self.__class__.__name__, attr)
18653105Sstever@eecs.umich.edu
18663105Sstever@eecs.umich.edu    # Full connection is symmetric (both ways).  Called via
18673105Sstever@eecs.umich.edu    # SimObject.__setattr__ as a result of a port assignment, e.g.,
18683109Sstever@eecs.umich.edu    # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,
18693105Sstever@eecs.umich.edu    # e.g., "obj1.portA[3] = obj2.portB".
18703105Sstever@eecs.umich.edu    def connect(self, other):
18713105Sstever@eecs.umich.edu        if isinstance(other, VectorPortRef):
18723105Sstever@eecs.umich.edu            # reference to plain VectorPort is implicit append
18733105Sstever@eecs.umich.edu            other = other._get_next()
18743105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
18759014Sandreas.hansson@arm.com            fatal("Port %s is already connected to %s, cannot connect %s\n",
18769014Sandreas.hansson@arm.com                  self, self.peer, other);
18773101Sstever@eecs.umich.edu        self.peer = other
18783109Sstever@eecs.umich.edu        if proxy.isproxy(other):
18793109Sstever@eecs.umich.edu            other.set_param_desc(PortParamDesc())
18803109Sstever@eecs.umich.edu        elif isinstance(other, PortRef):
18813109Sstever@eecs.umich.edu            if other.peer is not self:
18823109Sstever@eecs.umich.edu                other.connect(self)
18833109Sstever@eecs.umich.edu        else:
18843109Sstever@eecs.umich.edu            raise TypeError, \
18853109Sstever@eecs.umich.edu                  "assigning non-port reference '%s' to port '%s'" \
18863109Sstever@eecs.umich.edu                  % (other, self)
18873101Sstever@eecs.umich.edu
188810355SGeoffrey.Blake@arm.com    # Allow a master/slave port pair to be spliced between
188910355SGeoffrey.Blake@arm.com    # a port and its connected peer. Useful operation for connecting
189010355SGeoffrey.Blake@arm.com    # instrumentation structures into a system when it is necessary
189110355SGeoffrey.Blake@arm.com    # to connect the instrumentation after the full system has been
189210355SGeoffrey.Blake@arm.com    # constructed.
189310355SGeoffrey.Blake@arm.com    def splice(self, new_master_peer, new_slave_peer):
189410355SGeoffrey.Blake@arm.com        if self.peer and not proxy.isproxy(self.peer):
189510355SGeoffrey.Blake@arm.com            if isinstance(new_master_peer, PortRef) and \
189610355SGeoffrey.Blake@arm.com               isinstance(new_slave_peer, PortRef):
189710355SGeoffrey.Blake@arm.com                 old_peer = self.peer
189810355SGeoffrey.Blake@arm.com                 if self.role == 'SLAVE':
189910355SGeoffrey.Blake@arm.com                     self.peer = new_master_peer
190010355SGeoffrey.Blake@arm.com                     old_peer.peer = new_slave_peer
190110355SGeoffrey.Blake@arm.com                     new_master_peer.connect(self)
190210355SGeoffrey.Blake@arm.com                     new_slave_peer.connect(old_peer)
190310355SGeoffrey.Blake@arm.com                 elif self.role == 'MASTER':
190410355SGeoffrey.Blake@arm.com                     self.peer = new_slave_peer
190510355SGeoffrey.Blake@arm.com                     old_peer.peer = new_master_peer
190610355SGeoffrey.Blake@arm.com                     new_slave_peer.connect(self)
190710355SGeoffrey.Blake@arm.com                     new_master_peer.connect(old_peer)
190810355SGeoffrey.Blake@arm.com                 else:
190910355SGeoffrey.Blake@arm.com                     panic("Port %s has unknown role, "+\
191010355SGeoffrey.Blake@arm.com                           "cannot splice in new peers\n", self)
191110355SGeoffrey.Blake@arm.com            else:
191210355SGeoffrey.Blake@arm.com                raise TypeError, \
191310355SGeoffrey.Blake@arm.com                      "Splicing non-port references '%s','%s' to port '%s'"\
191410355SGeoffrey.Blake@arm.com                      % (new_peer, peers_new_peer, self)
191510355SGeoffrey.Blake@arm.com        else:
191610355SGeoffrey.Blake@arm.com            fatal("Port %s not connected, cannot splice in new peers\n", self)
191710355SGeoffrey.Blake@arm.com
19183105Sstever@eecs.umich.edu    def clone(self, simobj, memo):
19193105Sstever@eecs.umich.edu        if memo.has_key(self):
19203105Sstever@eecs.umich.edu            return memo[self]
19213101Sstever@eecs.umich.edu        newRef = copy.copy(self)
19223105Sstever@eecs.umich.edu        memo[self] = newRef
19233105Sstever@eecs.umich.edu        newRef.simobj = simobj
19243101Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
19253105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
19263179Sstever@eecs.umich.edu            peerObj = self.peer.simobj(_memo=memo)
19273105Sstever@eecs.umich.edu            newRef.peer = self.peer.clone(peerObj, memo)
19283105Sstever@eecs.umich.edu            assert(not isinstance(newRef.peer, VectorPortRef))
19293101Sstever@eecs.umich.edu        return newRef
19303101Sstever@eecs.umich.edu
19313105Sstever@eecs.umich.edu    def unproxy(self, simobj):
19323105Sstever@eecs.umich.edu        assert(simobj is self.simobj)
19333105Sstever@eecs.umich.edu        if proxy.isproxy(self.peer):
19343105Sstever@eecs.umich.edu            try:
19353105Sstever@eecs.umich.edu                realPeer = self.peer.unproxy(self.simobj)
19363105Sstever@eecs.umich.edu            except:
19373105Sstever@eecs.umich.edu                print "Error in unproxying port '%s' of %s" % \
19383105Sstever@eecs.umich.edu                      (self.name, self.simobj.path())
19393105Sstever@eecs.umich.edu                raise
19403105Sstever@eecs.umich.edu            self.connect(realPeer)
19413105Sstever@eecs.umich.edu
19423101Sstever@eecs.umich.edu    # Call C++ to create corresponding port connection between C++ objects
19433101Sstever@eecs.umich.edu    def ccConnect(self):
194411802Sandreas.sandberg@arm.com        from _m5.pyobject import connectPorts
19454762Snate@binkert.org
19468839Sandreas.hansson@arm.com        if self.role == 'SLAVE':
19478839Sandreas.hansson@arm.com            # do nothing and let the master take care of it
19488839Sandreas.hansson@arm.com            return
19498839Sandreas.hansson@arm.com
19503101Sstever@eecs.umich.edu        if self.ccConnected: # already done this
19513101Sstever@eecs.umich.edu            return
19523101Sstever@eecs.umich.edu        peer = self.peer
19535578SSteve.Reinhardt@amd.com        if not self.peer: # nothing to connect to
19545578SSteve.Reinhardt@amd.com            return
19558839Sandreas.hansson@arm.com
19568839Sandreas.hansson@arm.com        # check that we connect a master to a slave
19578839Sandreas.hansson@arm.com        if self.role == peer.role:
19588839Sandreas.hansson@arm.com            raise TypeError, \
19598839Sandreas.hansson@arm.com                "cannot connect '%s' and '%s' due to identical role '%s'" \
19608839Sandreas.hansson@arm.com                % (peer, self, self.role)
19618839Sandreas.hansson@arm.com
19627526Ssteve.reinhardt@amd.com        try:
19638839Sandreas.hansson@arm.com            # self is always the master and peer the slave
19647526Ssteve.reinhardt@amd.com            connectPorts(self.simobj.getCCObject(), self.name, self.index,
19657526Ssteve.reinhardt@amd.com                         peer.simobj.getCCObject(), peer.name, peer.index)
19667526Ssteve.reinhardt@amd.com        except:
19677526Ssteve.reinhardt@amd.com            print "Error connecting port %s.%s to %s.%s" % \
19687526Ssteve.reinhardt@amd.com                  (self.simobj.path(), self.name,
19697526Ssteve.reinhardt@amd.com                   peer.simobj.path(), peer.name)
19707526Ssteve.reinhardt@amd.com            raise
19713101Sstever@eecs.umich.edu        self.ccConnected = True
19723101Sstever@eecs.umich.edu        peer.ccConnected = True
19733101Sstever@eecs.umich.edu
19743105Sstever@eecs.umich.edu# A reference to an individual element of a VectorPort... much like a
19753105Sstever@eecs.umich.edu# PortRef, but has an index.
19763105Sstever@eecs.umich.educlass VectorPortElementRef(PortRef):
19778839Sandreas.hansson@arm.com    def __init__(self, simobj, name, role, index):
19788839Sandreas.hansson@arm.com        PortRef.__init__(self, simobj, name, role)
19793105Sstever@eecs.umich.edu        self.index = index
19803105Sstever@eecs.umich.edu
19813105Sstever@eecs.umich.edu    def __str__(self):
19823105Sstever@eecs.umich.edu        return '%s.%s[%d]' % (self.simobj, self.name, self.index)
19833105Sstever@eecs.umich.edu
19843105Sstever@eecs.umich.edu# A reference to a complete vector-valued port (not just a single element).
19853105Sstever@eecs.umich.edu# Can be indexed to retrieve individual VectorPortElementRef instances.
19863105Sstever@eecs.umich.educlass VectorPortRef(object):
19878839Sandreas.hansson@arm.com    def __init__(self, simobj, name, role):
19883105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
19893105Sstever@eecs.umich.edu        self.simobj = simobj
19903105Sstever@eecs.umich.edu        self.name = name
19918839Sandreas.hansson@arm.com        self.role = role
19923105Sstever@eecs.umich.edu        self.elements = []
19933105Sstever@eecs.umich.edu
19943109Sstever@eecs.umich.edu    def __str__(self):
19953109Sstever@eecs.umich.edu        return '%s.%s[:]' % (self.simobj, self.name)
19963109Sstever@eecs.umich.edu
19978840Sandreas.hansson@arm.com    def __len__(self):
19988840Sandreas.hansson@arm.com        # Return the number of connected peers, corresponding the the
19998840Sandreas.hansson@arm.com        # length of the elements.
20008840Sandreas.hansson@arm.com        return len(self.elements)
20018840Sandreas.hansson@arm.com
20023105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
20033105Sstever@eecs.umich.edu    def ini_str(self):
20043105Sstever@eecs.umich.edu        return ' '.join([el.ini_str() for el in self.elements])
20053105Sstever@eecs.umich.edu
20069017Sandreas.hansson@arm.com    # for config.json
20079017Sandreas.hansson@arm.com    def get_config_as_dict(self):
20089017Sandreas.hansson@arm.com        return {'role' : self.role,
20099017Sandreas.hansson@arm.com                'peer' : [el.ini_str() for el in self.elements]}
20109017Sandreas.hansson@arm.com
20113105Sstever@eecs.umich.edu    def __getitem__(self, key):
20123105Sstever@eecs.umich.edu        if not isinstance(key, int):
20133105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
20143105Sstever@eecs.umich.edu        if key >= len(self.elements):
20153105Sstever@eecs.umich.edu            # need to extend list
20168839Sandreas.hansson@arm.com            ext = [VectorPortElementRef(self.simobj, self.name, self.role, i)
20173105Sstever@eecs.umich.edu                   for i in range(len(self.elements), key+1)]
20183105Sstever@eecs.umich.edu            self.elements.extend(ext)
20193105Sstever@eecs.umich.edu        return self.elements[key]
20203105Sstever@eecs.umich.edu
20213105Sstever@eecs.umich.edu    def _get_next(self):
20223105Sstever@eecs.umich.edu        return self[len(self.elements)]
20233105Sstever@eecs.umich.edu
20243105Sstever@eecs.umich.edu    def __setitem__(self, key, value):
20253105Sstever@eecs.umich.edu        if not isinstance(key, int):
20263105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
20273105Sstever@eecs.umich.edu        self[key].connect(value)
20283105Sstever@eecs.umich.edu
20293105Sstever@eecs.umich.edu    def connect(self, other):
20303109Sstever@eecs.umich.edu        if isinstance(other, (list, tuple)):
20313109Sstever@eecs.umich.edu            # Assign list of port refs to vector port.
20323109Sstever@eecs.umich.edu            # For now, append them... not sure if that's the right semantics
20333109Sstever@eecs.umich.edu            # or if it should replace the current vector.
20343109Sstever@eecs.umich.edu            for ref in other:
20353109Sstever@eecs.umich.edu                self._get_next().connect(ref)
20363109Sstever@eecs.umich.edu        else:
20373109Sstever@eecs.umich.edu            # scalar assignment to plain VectorPort is implicit append
20383109Sstever@eecs.umich.edu            self._get_next().connect(other)
20393109Sstever@eecs.umich.edu
20403109Sstever@eecs.umich.edu    def clone(self, simobj, memo):
20413109Sstever@eecs.umich.edu        if memo.has_key(self):
20423109Sstever@eecs.umich.edu            return memo[self]
20433109Sstever@eecs.umich.edu        newRef = copy.copy(self)
20443109Sstever@eecs.umich.edu        memo[self] = newRef
20453109Sstever@eecs.umich.edu        newRef.simobj = simobj
20463109Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
20473109Sstever@eecs.umich.edu        newRef.elements = [el.clone(simobj, memo) for el in self.elements]
20483109Sstever@eecs.umich.edu        return newRef
20493105Sstever@eecs.umich.edu
20503105Sstever@eecs.umich.edu    def unproxy(self, simobj):
20513105Sstever@eecs.umich.edu        [el.unproxy(simobj) for el in self.elements]
20523105Sstever@eecs.umich.edu
20533105Sstever@eecs.umich.edu    def ccConnect(self):
20543105Sstever@eecs.umich.edu        [el.ccConnect() for el in self.elements]
20553105Sstever@eecs.umich.edu
20563101Sstever@eecs.umich.edu# Port description object.  Like a ParamDesc object, this represents a
20573101Sstever@eecs.umich.edu# logical port in the SimObject class, not a particular port on a
20583101Sstever@eecs.umich.edu# SimObject instance.  The latter are represented by PortRef objects.
20593101Sstever@eecs.umich.educlass Port(object):
20603101Sstever@eecs.umich.edu    # Generate a PortRef for this port on the given SimObject with the
20613101Sstever@eecs.umich.edu    # given name
20623105Sstever@eecs.umich.edu    def makeRef(self, simobj):
20638839Sandreas.hansson@arm.com        return PortRef(simobj, self.name, self.role)
20643101Sstever@eecs.umich.edu
20653101Sstever@eecs.umich.edu    # Connect an instance of this port (on the given SimObject with
20663101Sstever@eecs.umich.edu    # the given name) with the port described by the supplied PortRef
20673105Sstever@eecs.umich.edu    def connect(self, simobj, ref):
20683105Sstever@eecs.umich.edu        self.makeRef(simobj).connect(ref)
20693101Sstever@eecs.umich.edu
20708840Sandreas.hansson@arm.com    # No need for any pre-declarations at the moment as we merely rely
20718840Sandreas.hansson@arm.com    # on an unsigned int.
20728840Sandreas.hansson@arm.com    def cxx_predecls(self, code):
20738840Sandreas.hansson@arm.com        pass
20748840Sandreas.hansson@arm.com
207511988Sandreas.sandberg@arm.com    def pybind_predecls(self, code):
207611988Sandreas.sandberg@arm.com        cls.cxx_predecls(self, code)
207711988Sandreas.sandberg@arm.com
20788840Sandreas.hansson@arm.com    # Declare an unsigned int with the same name as the port, that
20798840Sandreas.hansson@arm.com    # will eventually hold the number of connected ports (and thus the
20808840Sandreas.hansson@arm.com    # number of elements for a VectorPort).
20818840Sandreas.hansson@arm.com    def cxx_decl(self, code):
20828840Sandreas.hansson@arm.com        code('unsigned int port_${{self.name}}_connection_count;')
20838840Sandreas.hansson@arm.com
20848839Sandreas.hansson@arm.comclass MasterPort(Port):
20858839Sandreas.hansson@arm.com    # MasterPort("description")
20868839Sandreas.hansson@arm.com    def __init__(self, *args):
20878839Sandreas.hansson@arm.com        if len(args) == 1:
20888839Sandreas.hansson@arm.com            self.desc = args[0]
20898839Sandreas.hansson@arm.com            self.role = 'MASTER'
20908839Sandreas.hansson@arm.com        else:
20918839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
20928839Sandreas.hansson@arm.com
20938839Sandreas.hansson@arm.comclass SlavePort(Port):
20948839Sandreas.hansson@arm.com    # SlavePort("description")
20958839Sandreas.hansson@arm.com    def __init__(self, *args):
20968839Sandreas.hansson@arm.com        if len(args) == 1:
20978839Sandreas.hansson@arm.com            self.desc = args[0]
20988839Sandreas.hansson@arm.com            self.role = 'SLAVE'
20998839Sandreas.hansson@arm.com        else:
21008839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
21018839Sandreas.hansson@arm.com
21023101Sstever@eecs.umich.edu# VectorPort description object.  Like Port, but represents a vector
210310405Sandreas.hansson@arm.com# of connections (e.g., as on a XBar).
21043101Sstever@eecs.umich.educlass VectorPort(Port):
21053105Sstever@eecs.umich.edu    def __init__(self, *args):
21063101Sstever@eecs.umich.edu        self.isVec = True
21073101Sstever@eecs.umich.edu
21083105Sstever@eecs.umich.edu    def makeRef(self, simobj):
21098839Sandreas.hansson@arm.com        return VectorPortRef(simobj, self.name, self.role)
21108839Sandreas.hansson@arm.com
21118839Sandreas.hansson@arm.comclass VectorMasterPort(VectorPort):
21128839Sandreas.hansson@arm.com    # VectorMasterPort("description")
21138839Sandreas.hansson@arm.com    def __init__(self, *args):
21148839Sandreas.hansson@arm.com        if len(args) == 1:
21158839Sandreas.hansson@arm.com            self.desc = args[0]
21168839Sandreas.hansson@arm.com            self.role = 'MASTER'
21178839Sandreas.hansson@arm.com            VectorPort.__init__(self, *args)
21188839Sandreas.hansson@arm.com        else:
21198839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
21208839Sandreas.hansson@arm.com
21218839Sandreas.hansson@arm.comclass VectorSlavePort(VectorPort):
21228839Sandreas.hansson@arm.com    # VectorSlavePort("description")
21238839Sandreas.hansson@arm.com    def __init__(self, *args):
21248839Sandreas.hansson@arm.com        if len(args) == 1:
21258839Sandreas.hansson@arm.com            self.desc = args[0]
21268839Sandreas.hansson@arm.com            self.role = 'SLAVE'
21278839Sandreas.hansson@arm.com            VectorPort.__init__(self, *args)
21288839Sandreas.hansson@arm.com        else:
21298839Sandreas.hansson@arm.com            raise TypeError, 'wrong number of arguments'
21303105Sstever@eecs.umich.edu
21313109Sstever@eecs.umich.edu# 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
21323109Sstever@eecs.umich.edu# proxy objects (via set_param_desc()) so that proxy error messages
21333109Sstever@eecs.umich.edu# make sense.
21343109Sstever@eecs.umich.educlass PortParamDesc(object):
21353109Sstever@eecs.umich.edu    __metaclass__ = Singleton
21363109Sstever@eecs.umich.edu
21373109Sstever@eecs.umich.edu    ptype_str = 'Port'
21383109Sstever@eecs.umich.edu    ptype = Port
21393105Sstever@eecs.umich.edu
21406654Snate@binkert.orgbaseEnums = allEnums.copy()
21416654Snate@binkert.orgbaseParams = allParams.copy()
21426654Snate@binkert.org
21436654Snate@binkert.orgdef clear():
21446654Snate@binkert.org    global allEnums, allParams
21456654Snate@binkert.org
21466654Snate@binkert.org    allEnums = baseEnums.copy()
21476654Snate@binkert.org    allParams = baseParams.copy()
21486654Snate@binkert.org
21493101Sstever@eecs.umich.edu__all__ = ['Param', 'VectorParam',
21503101Sstever@eecs.umich.edu           'Enum', 'Bool', 'String', 'Float',
21513101Sstever@eecs.umich.edu           'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
21523101Sstever@eecs.umich.edu           'Int32', 'UInt32', 'Int64', 'UInt64',
21533101Sstever@eecs.umich.edu           'Counter', 'Addr', 'Tick', 'Percent',
21543101Sstever@eecs.umich.edu           'TcpPort', 'UdpPort', 'EthernetAddr',
21557777Sgblack@eecs.umich.edu           'IpAddress', 'IpNetmask', 'IpWithPort',
21563101Sstever@eecs.umich.edu           'MemorySize', 'MemorySize32',
21579827Sakash.bagdia@arm.com           'Latency', 'Frequency', 'Clock', 'Voltage',
21583101Sstever@eecs.umich.edu           'NetworkBandwidth', 'MemoryBandwidth',
21599232Sandreas.hansson@arm.com           'AddrRange',
21603101Sstever@eecs.umich.edu           'MaxAddr', 'MaxTick', 'AllMemory',
21613885Sbinkertn@umich.edu           'Time',
21623102Sstever@eecs.umich.edu           'NextEthernetAddr', 'NULL',
21638839Sandreas.hansson@arm.com           'MasterPort', 'SlavePort',
21648839Sandreas.hansson@arm.com           'VectorMasterPort', 'VectorSlavePort']
21656654Snate@binkert.org
21666654Snate@binkert.orgimport SimObject
2167