params.py revision 3624
13101Sstever@eecs.umich.edu# Copyright (c) 2004-2006 The Regents of The University of Michigan
23101Sstever@eecs.umich.edu# All rights reserved.
33101Sstever@eecs.umich.edu#
43101Sstever@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
53101Sstever@eecs.umich.edu# modification, are permitted provided that the following conditions are
63101Sstever@eecs.umich.edu# met: redistributions of source code must retain the above copyright
73101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
83101Sstever@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
93101Sstever@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
103101Sstever@eecs.umich.edu# documentation and/or other materials provided with the distribution;
113101Sstever@eecs.umich.edu# neither the name of the copyright holders nor the names of its
123101Sstever@eecs.umich.edu# contributors may be used to endorse or promote products derived from
133101Sstever@eecs.umich.edu# this software without specific prior written permission.
143101Sstever@eecs.umich.edu#
153101Sstever@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
163101Sstever@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
173101Sstever@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
183101Sstever@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
193101Sstever@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
203101Sstever@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
213101Sstever@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223101Sstever@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233101Sstever@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243101Sstever@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
253101Sstever@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263101Sstever@eecs.umich.edu#
273101Sstever@eecs.umich.edu# Authors: Steve Reinhardt
283101Sstever@eecs.umich.edu#          Nathan Binkert
293101Sstever@eecs.umich.edu
303101Sstever@eecs.umich.edu#####################################################################
313101Sstever@eecs.umich.edu#
323101Sstever@eecs.umich.edu# Parameter description classes
333101Sstever@eecs.umich.edu#
343101Sstever@eecs.umich.edu# The _params dictionary in each class maps parameter names to either
353101Sstever@eecs.umich.edu# a Param or a VectorParam object.  These objects contain the
363101Sstever@eecs.umich.edu# parameter description string, the parameter type, and the default
373101Sstever@eecs.umich.edu# value (if any).  The convert() method on these objects is used to
383101Sstever@eecs.umich.edu# force whatever value is assigned to the parameter to the appropriate
393101Sstever@eecs.umich.edu# type.
403101Sstever@eecs.umich.edu#
413101Sstever@eecs.umich.edu# Note that the default values are loaded into the class's attribute
423101Sstever@eecs.umich.edu# space when the parameter dictionary is initialized (in
433101Sstever@eecs.umich.edu# MetaSimObject._new_param()); after that point they aren't used.
443101Sstever@eecs.umich.edu#
453101Sstever@eecs.umich.edu#####################################################################
463101Sstever@eecs.umich.edu
473101Sstever@eecs.umich.eduimport sys, inspect, copy
483101Sstever@eecs.umich.eduimport convert
493102Sstever@eecs.umich.edufrom util import *
503101Sstever@eecs.umich.edu
513101Sstever@eecs.umich.edu# Dummy base class to identify types that are legitimate for SimObject
523101Sstever@eecs.umich.edu# parameters.
533101Sstever@eecs.umich.educlass ParamValue(object):
543101Sstever@eecs.umich.edu
553101Sstever@eecs.umich.edu    cxx_predecls = []
563101Sstever@eecs.umich.edu    swig_predecls = []
573101Sstever@eecs.umich.edu
583101Sstever@eecs.umich.edu    # default for printing to .ini file is regular string conversion.
593101Sstever@eecs.umich.edu    # will be overridden in some cases
603101Sstever@eecs.umich.edu    def ini_str(self):
613101Sstever@eecs.umich.edu        return str(self)
623101Sstever@eecs.umich.edu
633101Sstever@eecs.umich.edu    # allows us to blithely call unproxy() on things without checking
643101Sstever@eecs.umich.edu    # if they're really proxies or not
653101Sstever@eecs.umich.edu    def unproxy(self, base):
663101Sstever@eecs.umich.edu        return self
673101Sstever@eecs.umich.edu
683101Sstever@eecs.umich.edu# Regular parameter description.
693101Sstever@eecs.umich.educlass ParamDesc(object):
703101Sstever@eecs.umich.edu    def __init__(self, ptype_str, ptype, *args, **kwargs):
713101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
723101Sstever@eecs.umich.edu        # remember ptype only if it is provided
733101Sstever@eecs.umich.edu        if ptype != None:
743101Sstever@eecs.umich.edu            self.ptype = ptype
753101Sstever@eecs.umich.edu
763101Sstever@eecs.umich.edu        if args:
773101Sstever@eecs.umich.edu            if len(args) == 1:
783101Sstever@eecs.umich.edu                self.desc = args[0]
793101Sstever@eecs.umich.edu            elif len(args) == 2:
803101Sstever@eecs.umich.edu                self.default = args[0]
813101Sstever@eecs.umich.edu                self.desc = args[1]
823101Sstever@eecs.umich.edu            else:
833101Sstever@eecs.umich.edu                raise TypeError, 'too many arguments'
843101Sstever@eecs.umich.edu
853101Sstever@eecs.umich.edu        if kwargs.has_key('desc'):
863101Sstever@eecs.umich.edu            assert(not hasattr(self, 'desc'))
873101Sstever@eecs.umich.edu            self.desc = kwargs['desc']
883101Sstever@eecs.umich.edu            del kwargs['desc']
893101Sstever@eecs.umich.edu
903101Sstever@eecs.umich.edu        if kwargs.has_key('default'):
913101Sstever@eecs.umich.edu            assert(not hasattr(self, 'default'))
923101Sstever@eecs.umich.edu            self.default = kwargs['default']
933101Sstever@eecs.umich.edu            del kwargs['default']
943101Sstever@eecs.umich.edu
953101Sstever@eecs.umich.edu        if kwargs:
963101Sstever@eecs.umich.edu            raise TypeError, 'extra unknown kwargs %s' % kwargs
973101Sstever@eecs.umich.edu
983101Sstever@eecs.umich.edu        if not hasattr(self, 'desc'):
993101Sstever@eecs.umich.edu            raise TypeError, 'desc attribute missing'
1003101Sstever@eecs.umich.edu
1013101Sstever@eecs.umich.edu    def __getattr__(self, attr):
1023101Sstever@eecs.umich.edu        if attr == 'ptype':
1033101Sstever@eecs.umich.edu            try:
1043102Sstever@eecs.umich.edu                ptype = eval(self.ptype_str, objects.__dict__)
1053101Sstever@eecs.umich.edu                if not isinstance(ptype, type):
1063102Sstever@eecs.umich.edu                    raise NameError
1073101Sstever@eecs.umich.edu                self.ptype = ptype
1083101Sstever@eecs.umich.edu                return ptype
1093101Sstever@eecs.umich.edu            except NameError:
1103102Sstever@eecs.umich.edu                raise TypeError, \
1113102Sstever@eecs.umich.edu                      "Param qualifier '%s' is not a type" % self.ptype_str
1123101Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
1133101Sstever@eecs.umich.edu              (type(self).__name__, attr)
1143101Sstever@eecs.umich.edu
1153101Sstever@eecs.umich.edu    def convert(self, value):
1163101Sstever@eecs.umich.edu        if isinstance(value, proxy.BaseProxy):
1173101Sstever@eecs.umich.edu            value.set_param_desc(self)
1183101Sstever@eecs.umich.edu            return value
1193101Sstever@eecs.umich.edu        if not hasattr(self, 'ptype') and isNullPointer(value):
1203101Sstever@eecs.umich.edu            # deferred evaluation of SimObject; continue to defer if
1213101Sstever@eecs.umich.edu            # we're just assigning a null pointer
1223101Sstever@eecs.umich.edu            return value
1233101Sstever@eecs.umich.edu        if isinstance(value, self.ptype):
1243101Sstever@eecs.umich.edu            return value
1253102Sstever@eecs.umich.edu        if isNullPointer(value) and isSimObjectClass(self.ptype):
1263101Sstever@eecs.umich.edu            return value
1273101Sstever@eecs.umich.edu        return self.ptype(value)
1283101Sstever@eecs.umich.edu
1293101Sstever@eecs.umich.edu    def cxx_predecls(self):
1303101Sstever@eecs.umich.edu        return self.ptype.cxx_predecls
1313101Sstever@eecs.umich.edu
1323101Sstever@eecs.umich.edu    def swig_predecls(self):
1333101Sstever@eecs.umich.edu        return self.ptype.swig_predecls
1343101Sstever@eecs.umich.edu
1353101Sstever@eecs.umich.edu    def cxx_decl(self):
1363101Sstever@eecs.umich.edu        return '%s %s;' % (self.ptype.cxx_type, self.name)
1373101Sstever@eecs.umich.edu
1383101Sstever@eecs.umich.edu# Vector-valued parameter description.  Just like ParamDesc, except
1393101Sstever@eecs.umich.edu# that the value is a vector (list) of the specified type instead of a
1403101Sstever@eecs.umich.edu# single value.
1413101Sstever@eecs.umich.edu
1423101Sstever@eecs.umich.educlass VectorParamValue(list):
1433101Sstever@eecs.umich.edu    def ini_str(self):
1443101Sstever@eecs.umich.edu        return ' '.join([v.ini_str() for v in self])
1453101Sstever@eecs.umich.edu
1463101Sstever@eecs.umich.edu    def unproxy(self, base):
1473101Sstever@eecs.umich.edu        return [v.unproxy(base) for v in self]
1483101Sstever@eecs.umich.edu
1493101Sstever@eecs.umich.educlass SimObjVector(VectorParamValue):
1503101Sstever@eecs.umich.edu    def print_ini(self):
1513101Sstever@eecs.umich.edu        for v in self:
1523101Sstever@eecs.umich.edu            v.print_ini()
1533101Sstever@eecs.umich.edu
1543101Sstever@eecs.umich.educlass VectorParamDesc(ParamDesc):
1553101Sstever@eecs.umich.edu    # Convert assigned value to appropriate type.  If the RHS is not a
1563101Sstever@eecs.umich.edu    # list or tuple, it generates a single-element list.
1573101Sstever@eecs.umich.edu    def convert(self, value):
1583101Sstever@eecs.umich.edu        if isinstance(value, (list, tuple)):
1593101Sstever@eecs.umich.edu            # list: coerce each element into new list
1603101Sstever@eecs.umich.edu            tmp_list = [ ParamDesc.convert(self, v) for v in value ]
1613101Sstever@eecs.umich.edu            if isSimObjectSequence(tmp_list):
1623101Sstever@eecs.umich.edu                return SimObjVector(tmp_list)
1633101Sstever@eecs.umich.edu            else:
1643101Sstever@eecs.umich.edu                return VectorParamValue(tmp_list)
1653101Sstever@eecs.umich.edu        else:
1663101Sstever@eecs.umich.edu            # singleton: leave it be (could coerce to a single-element
1673101Sstever@eecs.umich.edu            # list here, but for some historical reason we don't...
1683101Sstever@eecs.umich.edu            return ParamDesc.convert(self, value)
1693101Sstever@eecs.umich.edu
1703101Sstever@eecs.umich.edu    def cxx_predecls(self):
1713101Sstever@eecs.umich.edu        return ['#include <vector>'] + self.ptype.cxx_predecls
1723101Sstever@eecs.umich.edu
1733101Sstever@eecs.umich.edu    def swig_predecls(self):
1743101Sstever@eecs.umich.edu        return ['%include "std_vector.i"'] + self.ptype.swig_predecls
1753101Sstever@eecs.umich.edu
1763101Sstever@eecs.umich.edu    def cxx_decl(self):
1773101Sstever@eecs.umich.edu        return 'std::vector< %s > %s;' % (self.ptype.cxx_type, self.name)
1783101Sstever@eecs.umich.edu
1793101Sstever@eecs.umich.educlass ParamFactory(object):
1803101Sstever@eecs.umich.edu    def __init__(self, param_desc_class, ptype_str = None):
1813101Sstever@eecs.umich.edu        self.param_desc_class = param_desc_class
1823101Sstever@eecs.umich.edu        self.ptype_str = ptype_str
1833101Sstever@eecs.umich.edu
1843101Sstever@eecs.umich.edu    def __getattr__(self, attr):
1853101Sstever@eecs.umich.edu        if self.ptype_str:
1863101Sstever@eecs.umich.edu            attr = self.ptype_str + '.' + attr
1873101Sstever@eecs.umich.edu        return ParamFactory(self.param_desc_class, attr)
1883101Sstever@eecs.umich.edu
1893101Sstever@eecs.umich.edu    # E.g., Param.Int(5, "number of widgets")
1903101Sstever@eecs.umich.edu    def __call__(self, *args, **kwargs):
1913101Sstever@eecs.umich.edu        caller_frame = inspect.currentframe().f_back
1923101Sstever@eecs.umich.edu        ptype = None
1933101Sstever@eecs.umich.edu        try:
1943101Sstever@eecs.umich.edu            ptype = eval(self.ptype_str,
1953101Sstever@eecs.umich.edu                         caller_frame.f_globals, caller_frame.f_locals)
1963101Sstever@eecs.umich.edu            if not isinstance(ptype, type):
1973101Sstever@eecs.umich.edu                raise TypeError, \
1983101Sstever@eecs.umich.edu                      "Param qualifier is not a type: %s" % ptype
1993101Sstever@eecs.umich.edu        except NameError:
2003101Sstever@eecs.umich.edu            # if name isn't defined yet, assume it's a SimObject, and
2013101Sstever@eecs.umich.edu            # try to resolve it later
2023101Sstever@eecs.umich.edu            pass
2033101Sstever@eecs.umich.edu        return self.param_desc_class(self.ptype_str, ptype, *args, **kwargs)
2043101Sstever@eecs.umich.edu
2053101Sstever@eecs.umich.eduParam = ParamFactory(ParamDesc)
2063101Sstever@eecs.umich.eduVectorParam = ParamFactory(VectorParamDesc)
2073101Sstever@eecs.umich.edu
2083101Sstever@eecs.umich.edu#####################################################################
2093101Sstever@eecs.umich.edu#
2103101Sstever@eecs.umich.edu# Parameter Types
2113101Sstever@eecs.umich.edu#
2123101Sstever@eecs.umich.edu# Though native Python types could be used to specify parameter types
2133101Sstever@eecs.umich.edu# (the 'ptype' field of the Param and VectorParam classes), it's more
2143101Sstever@eecs.umich.edu# flexible to define our own set of types.  This gives us more control
2153101Sstever@eecs.umich.edu# over how Python expressions are converted to values (via the
2163101Sstever@eecs.umich.edu# __init__() constructor) and how these values are printed out (via
2173101Sstever@eecs.umich.edu# the __str__() conversion method).
2183101Sstever@eecs.umich.edu#
2193101Sstever@eecs.umich.edu#####################################################################
2203101Sstever@eecs.umich.edu
2213101Sstever@eecs.umich.edu# String-valued parameter.  Just mixin the ParamValue class with the
2223101Sstever@eecs.umich.edu# built-in str class.
2233101Sstever@eecs.umich.educlass String(ParamValue,str):
2243101Sstever@eecs.umich.edu    cxx_type = 'std::string'
2253101Sstever@eecs.umich.edu    cxx_predecls = ['#include <string>']
2263101Sstever@eecs.umich.edu    swig_predecls = ['%include "std_string.i"\n' +
2273101Sstever@eecs.umich.edu                     '%apply const std::string& {std::string *};']
2283101Sstever@eecs.umich.edu    pass
2293101Sstever@eecs.umich.edu
2303101Sstever@eecs.umich.edu# superclass for "numeric" parameter values, to emulate math
2313101Sstever@eecs.umich.edu# operations in a type-safe way.  e.g., a Latency times an int returns
2323101Sstever@eecs.umich.edu# a new Latency object.
2333101Sstever@eecs.umich.educlass NumericParamValue(ParamValue):
2343101Sstever@eecs.umich.edu    def __str__(self):
2353101Sstever@eecs.umich.edu        return str(self.value)
2363101Sstever@eecs.umich.edu
2373101Sstever@eecs.umich.edu    def __float__(self):
2383101Sstever@eecs.umich.edu        return float(self.value)
2393101Sstever@eecs.umich.edu
2403101Sstever@eecs.umich.edu    # hook for bounds checking
2413101Sstever@eecs.umich.edu    def _check(self):
2423101Sstever@eecs.umich.edu        return
2433101Sstever@eecs.umich.edu
2443101Sstever@eecs.umich.edu    def __mul__(self, other):
2453101Sstever@eecs.umich.edu        newobj = self.__class__(self)
2463101Sstever@eecs.umich.edu        newobj.value *= other
2473101Sstever@eecs.umich.edu        newobj._check()
2483101Sstever@eecs.umich.edu        return newobj
2493101Sstever@eecs.umich.edu
2503101Sstever@eecs.umich.edu    __rmul__ = __mul__
2513101Sstever@eecs.umich.edu
2523101Sstever@eecs.umich.edu    def __div__(self, other):
2533101Sstever@eecs.umich.edu        newobj = self.__class__(self)
2543101Sstever@eecs.umich.edu        newobj.value /= other
2553101Sstever@eecs.umich.edu        newobj._check()
2563101Sstever@eecs.umich.edu        return newobj
2573101Sstever@eecs.umich.edu
2583101Sstever@eecs.umich.edu    def __sub__(self, other):
2593101Sstever@eecs.umich.edu        newobj = self.__class__(self)
2603101Sstever@eecs.umich.edu        newobj.value -= other
2613101Sstever@eecs.umich.edu        newobj._check()
2623101Sstever@eecs.umich.edu        return newobj
2633101Sstever@eecs.umich.edu
2643101Sstever@eecs.umich.edu# Metaclass for bounds-checked integer parameters.  See CheckedInt.
2653101Sstever@eecs.umich.educlass CheckedIntType(type):
2663101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
2673101Sstever@eecs.umich.edu        super(CheckedIntType, cls).__init__(name, bases, dict)
2683101Sstever@eecs.umich.edu
2693101Sstever@eecs.umich.edu        # CheckedInt is an abstract base class, so we actually don't
2703101Sstever@eecs.umich.edu        # want to do any processing on it... the rest of this code is
2713101Sstever@eecs.umich.edu        # just for classes that derive from CheckedInt.
2723101Sstever@eecs.umich.edu        if name == 'CheckedInt':
2733101Sstever@eecs.umich.edu            return
2743101Sstever@eecs.umich.edu
2753101Sstever@eecs.umich.edu        if not cls.cxx_predecls:
2763101Sstever@eecs.umich.edu            # most derived types require this, so we just do it here once
2773101Sstever@eecs.umich.edu            cls.cxx_predecls = ['#include "sim/host.hh"']
2783101Sstever@eecs.umich.edu
2793101Sstever@eecs.umich.edu        if not cls.swig_predecls:
2803101Sstever@eecs.umich.edu            # most derived types require this, so we just do it here once
2813101Sstever@eecs.umich.edu            cls.swig_predecls = ['%import "python/m5/swig/stdint.i"\n' +
2823101Sstever@eecs.umich.edu                                 '%import "sim/host.hh"']
2833101Sstever@eecs.umich.edu
2843101Sstever@eecs.umich.edu        if not (hasattr(cls, 'min') and hasattr(cls, 'max')):
2853101Sstever@eecs.umich.edu            if not (hasattr(cls, 'size') and hasattr(cls, 'unsigned')):
2863101Sstever@eecs.umich.edu                panic("CheckedInt subclass %s must define either\n" \
2873101Sstever@eecs.umich.edu                      "    'min' and 'max' or 'size' and 'unsigned'\n" \
2883101Sstever@eecs.umich.edu                      % name);
2893101Sstever@eecs.umich.edu            if cls.unsigned:
2903101Sstever@eecs.umich.edu                cls.min = 0
2913101Sstever@eecs.umich.edu                cls.max = 2 ** cls.size - 1
2923101Sstever@eecs.umich.edu            else:
2933101Sstever@eecs.umich.edu                cls.min = -(2 ** (cls.size - 1))
2943101Sstever@eecs.umich.edu                cls.max = (2 ** (cls.size - 1)) - 1
2953101Sstever@eecs.umich.edu
2963101Sstever@eecs.umich.edu# Abstract superclass for bounds-checked integer parameters.  This
2973101Sstever@eecs.umich.edu# class is subclassed to generate parameter classes with specific
2983101Sstever@eecs.umich.edu# bounds.  Initialization of the min and max bounds is done in the
2993101Sstever@eecs.umich.edu# metaclass CheckedIntType.__init__.
3003101Sstever@eecs.umich.educlass CheckedInt(NumericParamValue):
3013101Sstever@eecs.umich.edu    __metaclass__ = CheckedIntType
3023101Sstever@eecs.umich.edu
3033101Sstever@eecs.umich.edu    def _check(self):
3043101Sstever@eecs.umich.edu        if not self.min <= self.value <= self.max:
3053101Sstever@eecs.umich.edu            raise TypeError, 'Integer param out of bounds %d < %d < %d' % \
3063101Sstever@eecs.umich.edu                  (self.min, self.value, self.max)
3073101Sstever@eecs.umich.edu
3083101Sstever@eecs.umich.edu    def __init__(self, value):
3093101Sstever@eecs.umich.edu        if isinstance(value, str):
3103102Sstever@eecs.umich.edu            self.value = convert.toInteger(value)
3113101Sstever@eecs.umich.edu        elif isinstance(value, (int, long, float)):
3123101Sstever@eecs.umich.edu            self.value = long(value)
3133101Sstever@eecs.umich.edu        self._check()
3143101Sstever@eecs.umich.edu
3153101Sstever@eecs.umich.educlass Int(CheckedInt):      cxx_type = 'int';      size = 32; unsigned = False
3163101Sstever@eecs.umich.educlass Unsigned(CheckedInt): cxx_type = 'unsigned'; size = 32; unsigned = True
3173101Sstever@eecs.umich.edu
3183101Sstever@eecs.umich.educlass Int8(CheckedInt):     cxx_type =   'int8_t'; size =  8; unsigned = False
3193101Sstever@eecs.umich.educlass UInt8(CheckedInt):    cxx_type =  'uint8_t'; size =  8; unsigned = True
3203101Sstever@eecs.umich.educlass Int16(CheckedInt):    cxx_type =  'int16_t'; size = 16; unsigned = False
3213101Sstever@eecs.umich.educlass UInt16(CheckedInt):   cxx_type = 'uint16_t'; size = 16; unsigned = True
3223101Sstever@eecs.umich.educlass Int32(CheckedInt):    cxx_type =  'int32_t'; size = 32; unsigned = False
3233101Sstever@eecs.umich.educlass UInt32(CheckedInt):   cxx_type = 'uint32_t'; size = 32; unsigned = True
3243101Sstever@eecs.umich.educlass Int64(CheckedInt):    cxx_type =  'int64_t'; size = 64; unsigned = False
3253101Sstever@eecs.umich.educlass UInt64(CheckedInt):   cxx_type = 'uint64_t'; size = 64; unsigned = True
3263101Sstever@eecs.umich.edu
3273101Sstever@eecs.umich.educlass Counter(CheckedInt):  cxx_type = 'Counter';  size = 64; unsigned = True
3283101Sstever@eecs.umich.educlass Tick(CheckedInt):     cxx_type = 'Tick';     size = 64; unsigned = True
3293101Sstever@eecs.umich.educlass TcpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
3303101Sstever@eecs.umich.educlass UdpPort(CheckedInt):  cxx_type = 'uint16_t'; size = 16; unsigned = True
3313101Sstever@eecs.umich.edu
3323101Sstever@eecs.umich.educlass Percent(CheckedInt):  cxx_type = 'int'; min = 0; max = 100
3333101Sstever@eecs.umich.edu
3343101Sstever@eecs.umich.educlass Float(ParamValue, float):
3353101Sstever@eecs.umich.edu    pass
3363101Sstever@eecs.umich.edu
3373101Sstever@eecs.umich.educlass MemorySize(CheckedInt):
3383101Sstever@eecs.umich.edu    cxx_type = 'uint64_t'
3393101Sstever@eecs.umich.edu    size = 64
3403101Sstever@eecs.umich.edu    unsigned = True
3413101Sstever@eecs.umich.edu    def __init__(self, value):
3423101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
3433101Sstever@eecs.umich.edu            self.value = value.value
3443101Sstever@eecs.umich.edu        else:
3453102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
3463101Sstever@eecs.umich.edu        self._check()
3473101Sstever@eecs.umich.edu
3483101Sstever@eecs.umich.educlass MemorySize32(CheckedInt):
3493101Sstever@eecs.umich.edu    size = 32
3503101Sstever@eecs.umich.edu    unsigned = True
3513101Sstever@eecs.umich.edu    def __init__(self, value):
3523101Sstever@eecs.umich.edu        if isinstance(value, MemorySize):
3533101Sstever@eecs.umich.edu            self.value = value.value
3543101Sstever@eecs.umich.edu        else:
3553102Sstever@eecs.umich.edu            self.value = convert.toMemorySize(value)
3563101Sstever@eecs.umich.edu        self._check()
3573101Sstever@eecs.umich.edu
3583101Sstever@eecs.umich.educlass Addr(CheckedInt):
3593101Sstever@eecs.umich.edu    cxx_type = 'Addr'
3603101Sstever@eecs.umich.edu    cxx_predecls = ['#include "targetarch/isa_traits.hh"']
3613101Sstever@eecs.umich.edu    size = 64
3623101Sstever@eecs.umich.edu    unsigned = True
3633101Sstever@eecs.umich.edu    def __init__(self, value):
3643101Sstever@eecs.umich.edu        if isinstance(value, Addr):
3653101Sstever@eecs.umich.edu            self.value = value.value
3663101Sstever@eecs.umich.edu        else:
3673101Sstever@eecs.umich.edu            try:
3683102Sstever@eecs.umich.edu                self.value = convert.toMemorySize(value)
3693101Sstever@eecs.umich.edu            except TypeError:
3703101Sstever@eecs.umich.edu                self.value = long(value)
3713101Sstever@eecs.umich.edu        self._check()
3723584Ssaidi@eecs.umich.edu    def __add__(self, other):
3733584Ssaidi@eecs.umich.edu        if isinstance(other, Addr):
3743584Ssaidi@eecs.umich.edu            return self.value + other.value
3753584Ssaidi@eecs.umich.edu        else:
3763584Ssaidi@eecs.umich.edu            return self.value + other
3773101Sstever@eecs.umich.edu
3783101Sstever@eecs.umich.edu
3793101Sstever@eecs.umich.educlass MetaRange(type):
3803101Sstever@eecs.umich.edu    def __init__(cls, name, bases, dict):
3813101Sstever@eecs.umich.edu        super(MetaRange, cls).__init__(name, bases, dict)
3823101Sstever@eecs.umich.edu        if name == 'Range':
3833101Sstever@eecs.umich.edu            return
3843101Sstever@eecs.umich.edu        cls.cxx_type = 'Range< %s >' % cls.type.cxx_type
3853101Sstever@eecs.umich.edu        cls.cxx_predecls = \
3863101Sstever@eecs.umich.edu                       ['#include "base/range.hh"'] + cls.type.cxx_predecls
3873101Sstever@eecs.umich.edu
3883101Sstever@eecs.umich.educlass Range(ParamValue):
3893101Sstever@eecs.umich.edu    __metaclass__ = MetaRange
3903101Sstever@eecs.umich.edu    type = Int # default; can be overridden in subclasses
3913101Sstever@eecs.umich.edu    def __init__(self, *args, **kwargs):
3923101Sstever@eecs.umich.edu        def handle_kwargs(self, kwargs):
3933101Sstever@eecs.umich.edu            if 'end' in kwargs:
3943101Sstever@eecs.umich.edu                self.second = self.type(kwargs.pop('end'))
3953101Sstever@eecs.umich.edu            elif 'size' in kwargs:
3963101Sstever@eecs.umich.edu                self.second = self.first + self.type(kwargs.pop('size')) - 1
3973101Sstever@eecs.umich.edu            else:
3983101Sstever@eecs.umich.edu                raise TypeError, "Either end or size must be specified"
3993101Sstever@eecs.umich.edu
4003101Sstever@eecs.umich.edu        if len(args) == 0:
4013101Sstever@eecs.umich.edu            self.first = self.type(kwargs.pop('start'))
4023101Sstever@eecs.umich.edu            handle_kwargs(self, kwargs)
4033101Sstever@eecs.umich.edu
4043101Sstever@eecs.umich.edu        elif len(args) == 1:
4053101Sstever@eecs.umich.edu            if kwargs:
4063101Sstever@eecs.umich.edu                self.first = self.type(args[0])
4073101Sstever@eecs.umich.edu                handle_kwargs(self, kwargs)
4083101Sstever@eecs.umich.edu            elif isinstance(args[0], Range):
4093101Sstever@eecs.umich.edu                self.first = self.type(args[0].first)
4103101Sstever@eecs.umich.edu                self.second = self.type(args[0].second)
4113101Sstever@eecs.umich.edu            else:
4123101Sstever@eecs.umich.edu                self.first = self.type(0)
4133101Sstever@eecs.umich.edu                self.second = self.type(args[0]) - 1
4143101Sstever@eecs.umich.edu
4153101Sstever@eecs.umich.edu        elif len(args) == 2:
4163101Sstever@eecs.umich.edu            self.first = self.type(args[0])
4173101Sstever@eecs.umich.edu            self.second = self.type(args[1])
4183101Sstever@eecs.umich.edu        else:
4193101Sstever@eecs.umich.edu            raise TypeError, "Too many arguments specified"
4203101Sstever@eecs.umich.edu
4213101Sstever@eecs.umich.edu        if kwargs:
4223101Sstever@eecs.umich.edu            raise TypeError, "too many keywords: %s" % kwargs.keys()
4233101Sstever@eecs.umich.edu
4243101Sstever@eecs.umich.edu    def __str__(self):
4253101Sstever@eecs.umich.edu        return '%s:%s' % (self.first, self.second)
4263101Sstever@eecs.umich.edu
4273101Sstever@eecs.umich.educlass AddrRange(Range):
4283101Sstever@eecs.umich.edu    type = Addr
4293101Sstever@eecs.umich.edu
4303101Sstever@eecs.umich.educlass TickRange(Range):
4313101Sstever@eecs.umich.edu    type = Tick
4323101Sstever@eecs.umich.edu
4333101Sstever@eecs.umich.edu# Boolean parameter type.  Python doesn't let you subclass bool, since
4343101Sstever@eecs.umich.edu# it doesn't want to let you create multiple instances of True and
4353101Sstever@eecs.umich.edu# False.  Thus this is a little more complicated than String.
4363101Sstever@eecs.umich.educlass Bool(ParamValue):
4373101Sstever@eecs.umich.edu    cxx_type = 'bool'
4383101Sstever@eecs.umich.edu    def __init__(self, value):
4393101Sstever@eecs.umich.edu        try:
4403102Sstever@eecs.umich.edu            self.value = convert.toBool(value)
4413101Sstever@eecs.umich.edu        except TypeError:
4423101Sstever@eecs.umich.edu            self.value = bool(value)
4433101Sstever@eecs.umich.edu
4443101Sstever@eecs.umich.edu    def __str__(self):
4453101Sstever@eecs.umich.edu        return str(self.value)
4463101Sstever@eecs.umich.edu
4473101Sstever@eecs.umich.edu    def ini_str(self):
4483101Sstever@eecs.umich.edu        if self.value:
4493101Sstever@eecs.umich.edu            return 'true'
4503101Sstever@eecs.umich.edu        return 'false'
4513101Sstever@eecs.umich.edu
4523101Sstever@eecs.umich.edudef IncEthernetAddr(addr, val = 1):
4533101Sstever@eecs.umich.edu    bytes = map(lambda x: int(x, 16), addr.split(':'))
4543101Sstever@eecs.umich.edu    bytes[5] += val
4553101Sstever@eecs.umich.edu    for i in (5, 4, 3, 2, 1):
4563101Sstever@eecs.umich.edu        val,rem = divmod(bytes[i], 256)
4573101Sstever@eecs.umich.edu        bytes[i] = rem
4583101Sstever@eecs.umich.edu        if val == 0:
4593101Sstever@eecs.umich.edu            break
4603101Sstever@eecs.umich.edu        bytes[i - 1] += val
4613101Sstever@eecs.umich.edu    assert(bytes[0] <= 255)
4623101Sstever@eecs.umich.edu    return ':'.join(map(lambda x: '%02x' % x, bytes))
4633101Sstever@eecs.umich.edu
4643101Sstever@eecs.umich.educlass NextEthernetAddr(object):
4653101Sstever@eecs.umich.edu    addr = "00:90:00:00:00:01"
4663101Sstever@eecs.umich.edu
4673101Sstever@eecs.umich.edu    def __init__(self, inc = 1):
4683101Sstever@eecs.umich.edu        self.value = NextEthernetAddr.addr
4693101Sstever@eecs.umich.edu        NextEthernetAddr.addr = IncEthernetAddr(NextEthernetAddr.addr, inc)
4703101Sstever@eecs.umich.edu
4713101Sstever@eecs.umich.educlass EthernetAddr(ParamValue):
4723101Sstever@eecs.umich.edu    cxx_type = 'Net::EthAddr'
4733101Sstever@eecs.umich.edu    cxx_predecls = ['#include "base/inet.hh"']
4743101Sstever@eecs.umich.edu    swig_predecls = ['class Net::EthAddr;']
4753101Sstever@eecs.umich.edu    def __init__(self, value):
4763101Sstever@eecs.umich.edu        if value == NextEthernetAddr:
4773101Sstever@eecs.umich.edu            self.value = value
4783101Sstever@eecs.umich.edu            return
4793101Sstever@eecs.umich.edu
4803101Sstever@eecs.umich.edu        if not isinstance(value, str):
4813101Sstever@eecs.umich.edu            raise TypeError, "expected an ethernet address and didn't get one"
4823101Sstever@eecs.umich.edu
4833101Sstever@eecs.umich.edu        bytes = value.split(':')
4843101Sstever@eecs.umich.edu        if len(bytes) != 6:
4853101Sstever@eecs.umich.edu            raise TypeError, 'invalid ethernet address %s' % value
4863101Sstever@eecs.umich.edu
4873101Sstever@eecs.umich.edu        for byte in bytes:
4883101Sstever@eecs.umich.edu            if not 0 <= int(byte) <= 256:
4893101Sstever@eecs.umich.edu                raise TypeError, 'invalid ethernet address %s' % value
4903101Sstever@eecs.umich.edu
4913101Sstever@eecs.umich.edu        self.value = value
4923101Sstever@eecs.umich.edu
4933101Sstever@eecs.umich.edu    def unproxy(self, base):
4943101Sstever@eecs.umich.edu        if self.value == NextEthernetAddr:
4953101Sstever@eecs.umich.edu            self.addr = self.value().value
4963101Sstever@eecs.umich.edu        return self
4973101Sstever@eecs.umich.edu
4983101Sstever@eecs.umich.edu    def __str__(self):
4993101Sstever@eecs.umich.edu        if self.value == NextEthernetAddr:
5003101Sstever@eecs.umich.edu            if hasattr(self, 'addr'):
5013101Sstever@eecs.umich.edu                return self.addr
5023101Sstever@eecs.umich.edu            else:
5033101Sstever@eecs.umich.edu                return "NextEthernetAddr (unresolved)"
5043101Sstever@eecs.umich.edu        else:
5053101Sstever@eecs.umich.edu            return self.value
5063101Sstever@eecs.umich.edu
5073101Sstever@eecs.umich.edu# Enumerated types are a little more complex.  The user specifies the
5083101Sstever@eecs.umich.edu# type as Enum(foo) where foo is either a list or dictionary of
5093101Sstever@eecs.umich.edu# alternatives (typically strings, but not necessarily so).  (In the
5103101Sstever@eecs.umich.edu# long run, the integer value of the parameter will be the list index
5113101Sstever@eecs.umich.edu# or the corresponding dictionary value.  For now, since we only check
5123101Sstever@eecs.umich.edu# that the alternative is valid and then spit it into a .ini file,
5133101Sstever@eecs.umich.edu# there's not much point in using the dictionary.)
5143101Sstever@eecs.umich.edu
5153101Sstever@eecs.umich.edu# What Enum() must do is generate a new type encapsulating the
5163101Sstever@eecs.umich.edu# provided list/dictionary so that specific values of the parameter
5173101Sstever@eecs.umich.edu# can be instances of that type.  We define two hidden internal
5183101Sstever@eecs.umich.edu# classes (_ListEnum and _DictEnum) to serve as base classes, then
5193101Sstever@eecs.umich.edu# derive the new type from the appropriate base class on the fly.
5203101Sstever@eecs.umich.edu
5213101Sstever@eecs.umich.edu
5223101Sstever@eecs.umich.edu# Metaclass for Enum types
5233101Sstever@eecs.umich.educlass MetaEnum(type):
5243101Sstever@eecs.umich.edu    def __init__(cls, name, bases, init_dict):
5253101Sstever@eecs.umich.edu        if init_dict.has_key('map'):
5263101Sstever@eecs.umich.edu            if not isinstance(cls.map, dict):
5273101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'map' " \
5283101Sstever@eecs.umich.edu                      "must be of type dict"
5293101Sstever@eecs.umich.edu            # build list of value strings from map
5303101Sstever@eecs.umich.edu            cls.vals = cls.map.keys()
5313101Sstever@eecs.umich.edu            cls.vals.sort()
5323101Sstever@eecs.umich.edu        elif init_dict.has_key('vals'):
5333101Sstever@eecs.umich.edu            if not isinstance(cls.vals, list):
5343101Sstever@eecs.umich.edu                raise TypeError, "Enum-derived class attribute 'vals' " \
5353101Sstever@eecs.umich.edu                      "must be of type list"
5363101Sstever@eecs.umich.edu            # build string->value map from vals sequence
5373101Sstever@eecs.umich.edu            cls.map = {}
5383101Sstever@eecs.umich.edu            for idx,val in enumerate(cls.vals):
5393101Sstever@eecs.umich.edu                cls.map[val] = idx
5403101Sstever@eecs.umich.edu        else:
5413101Sstever@eecs.umich.edu            raise TypeError, "Enum-derived class must define "\
5423101Sstever@eecs.umich.edu                  "attribute 'map' or 'vals'"
5433101Sstever@eecs.umich.edu
5443101Sstever@eecs.umich.edu        cls.cxx_type = name + '::Enum'
5453101Sstever@eecs.umich.edu
5463101Sstever@eecs.umich.edu        super(MetaEnum, cls).__init__(name, bases, init_dict)
5473101Sstever@eecs.umich.edu
5483101Sstever@eecs.umich.edu    # Generate C++ class declaration for this enum type.
5493101Sstever@eecs.umich.edu    # Note that we wrap the enum in a class/struct to act as a namespace,
5503101Sstever@eecs.umich.edu    # so that the enum strings can be brief w/o worrying about collisions.
5513101Sstever@eecs.umich.edu    def cxx_decl(cls):
5523101Sstever@eecs.umich.edu        s = 'struct %s {\n  enum Enum {\n    ' % cls.__name__
5533101Sstever@eecs.umich.edu        s += ',\n    '.join(['%s = %d' % (v,cls.map[v]) for v in cls.vals])
5543101Sstever@eecs.umich.edu        s += '\n  };\n};\n'
5553101Sstever@eecs.umich.edu        return s
5563101Sstever@eecs.umich.edu
5573101Sstever@eecs.umich.edu# Base class for enum types.
5583101Sstever@eecs.umich.educlass Enum(ParamValue):
5593101Sstever@eecs.umich.edu    __metaclass__ = MetaEnum
5603101Sstever@eecs.umich.edu    vals = []
5613101Sstever@eecs.umich.edu
5623101Sstever@eecs.umich.edu    def __init__(self, value):
5633101Sstever@eecs.umich.edu        if value not in self.map:
5643101Sstever@eecs.umich.edu            raise TypeError, "Enum param got bad value '%s' (not in %s)" \
5653101Sstever@eecs.umich.edu                  % (value, self.vals)
5663101Sstever@eecs.umich.edu        self.value = value
5673101Sstever@eecs.umich.edu
5683101Sstever@eecs.umich.edu    def __str__(self):
5693101Sstever@eecs.umich.edu        return self.value
5703101Sstever@eecs.umich.edu
5713101Sstever@eecs.umich.eduticks_per_sec = None
5723101Sstever@eecs.umich.edu
5733101Sstever@eecs.umich.edu# how big does a rounding error need to be before we warn about it?
5743101Sstever@eecs.umich.edufrequency_tolerance = 0.001  # 0.1%
5753101Sstever@eecs.umich.edu
5763101Sstever@eecs.umich.edu# convert a floting-point # of ticks to integer, and warn if rounding
5773101Sstever@eecs.umich.edu# discards too much precision
5783101Sstever@eecs.umich.edudef tick_check(float_ticks):
5793101Sstever@eecs.umich.edu    if float_ticks == 0:
5803101Sstever@eecs.umich.edu        return 0
5813101Sstever@eecs.umich.edu    int_ticks = int(round(float_ticks))
5823101Sstever@eecs.umich.edu    err = (float_ticks - int_ticks) / float_ticks
5833101Sstever@eecs.umich.edu    if err > frequency_tolerance:
5843101Sstever@eecs.umich.edu        print >> sys.stderr, "Warning: rounding error > tolerance"
5853101Sstever@eecs.umich.edu        print >> sys.stderr, "    %f rounded to %d" % (float_ticks, int_ticks)
5863101Sstever@eecs.umich.edu        #raise ValueError
5873101Sstever@eecs.umich.edu    return int_ticks
5883101Sstever@eecs.umich.edu
5893101Sstever@eecs.umich.edudef getLatency(value):
5903101Sstever@eecs.umich.edu    if isinstance(value, Latency) or isinstance(value, Clock):
5913101Sstever@eecs.umich.edu        return value.value
5923101Sstever@eecs.umich.edu    elif isinstance(value, Frequency) or isinstance(value, RootClock):
5933101Sstever@eecs.umich.edu        return 1 / value.value
5943101Sstever@eecs.umich.edu    elif isinstance(value, str):
5953101Sstever@eecs.umich.edu        try:
5963102Sstever@eecs.umich.edu            return convert.toLatency(value)
5973101Sstever@eecs.umich.edu        except ValueError:
5983101Sstever@eecs.umich.edu            try:
5993102Sstever@eecs.umich.edu                return 1 / convert.toFrequency(value)
6003101Sstever@eecs.umich.edu            except ValueError:
6013101Sstever@eecs.umich.edu                pass # fall through
6023101Sstever@eecs.umich.edu    raise ValueError, "Invalid Frequency/Latency value '%s'" % value
6033101Sstever@eecs.umich.edu
6043101Sstever@eecs.umich.edu
6053101Sstever@eecs.umich.educlass Latency(NumericParamValue):
6063101Sstever@eecs.umich.edu    cxx_type = 'Tick'
6073101Sstever@eecs.umich.edu    cxx_predecls = ['#include "sim/host.hh"']
6083101Sstever@eecs.umich.edu    swig_predecls = ['%import "python/m5/swig/stdint.i"\n' +
6093101Sstever@eecs.umich.edu                     '%import "sim/host.hh"']
6103101Sstever@eecs.umich.edu    def __init__(self, value):
6113101Sstever@eecs.umich.edu        self.value = getLatency(value)
6123101Sstever@eecs.umich.edu
6133101Sstever@eecs.umich.edu    def __getattr__(self, attr):
6143101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
6153101Sstever@eecs.umich.edu            return self
6163101Sstever@eecs.umich.edu        if attr == 'frequency':
6173101Sstever@eecs.umich.edu            return Frequency(self)
6183101Sstever@eecs.umich.edu        raise AttributeError, "Latency object has no attribute '%s'" % attr
6193101Sstever@eecs.umich.edu
6203101Sstever@eecs.umich.edu    # convert latency to ticks
6213101Sstever@eecs.umich.edu    def ini_str(self):
6223101Sstever@eecs.umich.edu        return str(tick_check(self.value * ticks_per_sec))
6233101Sstever@eecs.umich.edu
6243101Sstever@eecs.umich.educlass Frequency(NumericParamValue):
6253101Sstever@eecs.umich.edu    cxx_type = 'Tick'
6263101Sstever@eecs.umich.edu    cxx_predecls = ['#include "sim/host.hh"']
6273101Sstever@eecs.umich.edu    swig_predecls = ['%import "python/m5/swig/stdint.i"\n' +
6283101Sstever@eecs.umich.edu                     '%import "sim/host.hh"']
6293101Sstever@eecs.umich.edu    def __init__(self, value):
6303101Sstever@eecs.umich.edu        self.value = 1 / getLatency(value)
6313101Sstever@eecs.umich.edu
6323101Sstever@eecs.umich.edu    def __getattr__(self, attr):
6333101Sstever@eecs.umich.edu        if attr == 'frequency':
6343101Sstever@eecs.umich.edu            return self
6353101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
6363101Sstever@eecs.umich.edu            return Latency(self)
6373101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
6383101Sstever@eecs.umich.edu
6393101Sstever@eecs.umich.edu    # convert frequency to ticks per period
6403101Sstever@eecs.umich.edu    def ini_str(self):
6413101Sstever@eecs.umich.edu        return self.period.ini_str()
6423101Sstever@eecs.umich.edu
6433101Sstever@eecs.umich.edu# Just like Frequency, except ini_str() is absolute # of ticks per sec (Hz).
6443101Sstever@eecs.umich.edu# We can't inherit from Frequency because we don't want it to be directly
6453101Sstever@eecs.umich.edu# assignable to a regular Frequency parameter.
6463101Sstever@eecs.umich.educlass RootClock(ParamValue):
6473101Sstever@eecs.umich.edu    cxx_type = 'Tick'
6483101Sstever@eecs.umich.edu    cxx_predecls = ['#include "sim/host.hh"']
6493101Sstever@eecs.umich.edu    swig_predecls = ['%import "python/m5/swig/stdint.i"\n' +
6503101Sstever@eecs.umich.edu                     '%import "sim/host.hh"']
6513101Sstever@eecs.umich.edu    def __init__(self, value):
6523101Sstever@eecs.umich.edu        self.value = 1 / getLatency(value)
6533101Sstever@eecs.umich.edu
6543101Sstever@eecs.umich.edu    def __getattr__(self, attr):
6553101Sstever@eecs.umich.edu        if attr == 'frequency':
6563101Sstever@eecs.umich.edu            return Frequency(self)
6573101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
6583101Sstever@eecs.umich.edu            return Latency(self)
6593101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
6603101Sstever@eecs.umich.edu
6613101Sstever@eecs.umich.edu    def ini_str(self):
6623101Sstever@eecs.umich.edu        return str(tick_check(self.value))
6633101Sstever@eecs.umich.edu
6643101Sstever@eecs.umich.edu# A generic frequency and/or Latency value.  Value is stored as a latency,
6653101Sstever@eecs.umich.edu# but to avoid ambiguity this object does not support numeric ops (* or /).
6663101Sstever@eecs.umich.edu# An explicit conversion to a Latency or Frequency must be made first.
6673101Sstever@eecs.umich.educlass Clock(ParamValue):
6683101Sstever@eecs.umich.edu    cxx_type = 'Tick'
6693101Sstever@eecs.umich.edu    cxx_predecls = ['#include "sim/host.hh"']
6703101Sstever@eecs.umich.edu    swig_predecls = ['%import "python/m5/swig/stdint.i"\n' +
6713101Sstever@eecs.umich.edu                     '%import "sim/host.hh"']
6723101Sstever@eecs.umich.edu    def __init__(self, value):
6733101Sstever@eecs.umich.edu        self.value = getLatency(value)
6743101Sstever@eecs.umich.edu
6753101Sstever@eecs.umich.edu    def __getattr__(self, attr):
6763101Sstever@eecs.umich.edu        if attr == 'frequency':
6773101Sstever@eecs.umich.edu            return Frequency(self)
6783101Sstever@eecs.umich.edu        if attr in ('latency', 'period'):
6793101Sstever@eecs.umich.edu            return Latency(self)
6803101Sstever@eecs.umich.edu        raise AttributeError, "Frequency object has no attribute '%s'" % attr
6813101Sstever@eecs.umich.edu
6823101Sstever@eecs.umich.edu    def ini_str(self):
6833101Sstever@eecs.umich.edu        return self.period.ini_str()
6843101Sstever@eecs.umich.edu
6853101Sstever@eecs.umich.educlass NetworkBandwidth(float,ParamValue):
6863101Sstever@eecs.umich.edu    cxx_type = 'float'
6873101Sstever@eecs.umich.edu    def __new__(cls, value):
6883102Sstever@eecs.umich.edu        val = convert.toNetworkBandwidth(value) / 8.0
6893101Sstever@eecs.umich.edu        return super(cls, NetworkBandwidth).__new__(cls, val)
6903101Sstever@eecs.umich.edu
6913101Sstever@eecs.umich.edu    def __str__(self):
6923101Sstever@eecs.umich.edu        return str(self.val)
6933101Sstever@eecs.umich.edu
6943101Sstever@eecs.umich.edu    def ini_str(self):
6953101Sstever@eecs.umich.edu        return '%f' % (ticks_per_sec / float(self))
6963101Sstever@eecs.umich.edu
6973101Sstever@eecs.umich.educlass MemoryBandwidth(float,ParamValue):
6983101Sstever@eecs.umich.edu    cxx_type = 'float'
6993101Sstever@eecs.umich.edu    def __new__(self, value):
7003102Sstever@eecs.umich.edu        val = convert.toMemoryBandwidth(value)
7013101Sstever@eecs.umich.edu        return super(cls, MemoryBandwidth).__new__(cls, val)
7023101Sstever@eecs.umich.edu
7033101Sstever@eecs.umich.edu    def __str__(self):
7043101Sstever@eecs.umich.edu        return str(self.val)
7053101Sstever@eecs.umich.edu
7063101Sstever@eecs.umich.edu    def ini_str(self):
7073101Sstever@eecs.umich.edu        return '%f' % (ticks_per_sec / float(self))
7083101Sstever@eecs.umich.edu
7093101Sstever@eecs.umich.edu#
7103101Sstever@eecs.umich.edu# "Constants"... handy aliases for various values.
7113101Sstever@eecs.umich.edu#
7123101Sstever@eecs.umich.edu
7133102Sstever@eecs.umich.edu# Special class for NULL pointers.  Note the special check in
7143102Sstever@eecs.umich.edu# make_param_value() above that lets these be assigned where a
7153102Sstever@eecs.umich.edu# SimObject is required.
7163102Sstever@eecs.umich.edu# only one copy of a particular node
7173102Sstever@eecs.umich.educlass NullSimObject(object):
7183102Sstever@eecs.umich.edu    __metaclass__ = Singleton
7193102Sstever@eecs.umich.edu
7203102Sstever@eecs.umich.edu    def __call__(cls):
7213102Sstever@eecs.umich.edu        return cls
7223102Sstever@eecs.umich.edu
7233102Sstever@eecs.umich.edu    def _instantiate(self, parent = None, path = ''):
7243102Sstever@eecs.umich.edu        pass
7253102Sstever@eecs.umich.edu
7263102Sstever@eecs.umich.edu    def ini_str(self):
7273102Sstever@eecs.umich.edu        return 'Null'
7283102Sstever@eecs.umich.edu
7293102Sstever@eecs.umich.edu    def unproxy(self, base):
7303102Sstever@eecs.umich.edu        return self
7313102Sstever@eecs.umich.edu
7323102Sstever@eecs.umich.edu    def set_path(self, parent, name):
7333102Sstever@eecs.umich.edu        pass
7343102Sstever@eecs.umich.edu    def __str__(self):
7353102Sstever@eecs.umich.edu        return 'Null'
7363102Sstever@eecs.umich.edu
7373102Sstever@eecs.umich.edu# The only instance you'll ever need...
7383102Sstever@eecs.umich.eduNULL = NullSimObject()
7393102Sstever@eecs.umich.edu
7403102Sstever@eecs.umich.edudef isNullPointer(value):
7413102Sstever@eecs.umich.edu    return isinstance(value, NullSimObject)
7423102Sstever@eecs.umich.edu
7433101Sstever@eecs.umich.edu# Some memory range specifications use this as a default upper bound.
7443101Sstever@eecs.umich.eduMaxAddr = Addr.max
7453101Sstever@eecs.umich.eduMaxTick = Tick.max
7463101Sstever@eecs.umich.eduAllMemory = AddrRange(0, MaxAddr)
7473101Sstever@eecs.umich.edu
7483101Sstever@eecs.umich.edu
7493101Sstever@eecs.umich.edu#####################################################################
7503101Sstever@eecs.umich.edu#
7513101Sstever@eecs.umich.edu# Port objects
7523101Sstever@eecs.umich.edu#
7533101Sstever@eecs.umich.edu# Ports are used to interconnect objects in the memory system.
7543101Sstever@eecs.umich.edu#
7553101Sstever@eecs.umich.edu#####################################################################
7563101Sstever@eecs.umich.edu
7573101Sstever@eecs.umich.edu# Port reference: encapsulates a reference to a particular port on a
7583101Sstever@eecs.umich.edu# particular SimObject.
7593101Sstever@eecs.umich.educlass PortRef(object):
7603105Sstever@eecs.umich.edu    def __init__(self, simobj, name):
7613105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
7623101Sstever@eecs.umich.edu        self.simobj = simobj
7633101Sstever@eecs.umich.edu        self.name = name
7643101Sstever@eecs.umich.edu        self.peer = None   # not associated with another port yet
7653101Sstever@eecs.umich.edu        self.ccConnected = False # C++ port connection done?
7663105Sstever@eecs.umich.edu        self.index = -1  # always -1 for non-vector ports
7673101Sstever@eecs.umich.edu
7683103Sstever@eecs.umich.edu    def __str__(self):
7693105Sstever@eecs.umich.edu        return '%s.%s' % (self.simobj, self.name)
7703103Sstever@eecs.umich.edu
7713105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
7723105Sstever@eecs.umich.edu    def ini_str(self):
7733105Sstever@eecs.umich.edu        return str(self.peer)
7743105Sstever@eecs.umich.edu
7753105Sstever@eecs.umich.edu    def __getattr__(self, attr):
7763105Sstever@eecs.umich.edu        if attr == 'peerObj':
7773105Sstever@eecs.umich.edu            # shorthand for proxies
7783105Sstever@eecs.umich.edu            return self.peer.simobj
7793105Sstever@eecs.umich.edu        raise AttributeError, "'%s' object has no attribute '%s'" % \
7803105Sstever@eecs.umich.edu              (self.__class__.__name__, attr)
7813105Sstever@eecs.umich.edu
7823105Sstever@eecs.umich.edu    # Full connection is symmetric (both ways).  Called via
7833105Sstever@eecs.umich.edu    # SimObject.__setattr__ as a result of a port assignment, e.g.,
7843109Sstever@eecs.umich.edu    # "obj1.portA = obj2.portB", or via VectorPortElementRef.__setitem__,
7853105Sstever@eecs.umich.edu    # e.g., "obj1.portA[3] = obj2.portB".
7863105Sstever@eecs.umich.edu    def connect(self, other):
7873105Sstever@eecs.umich.edu        if isinstance(other, VectorPortRef):
7883105Sstever@eecs.umich.edu            # reference to plain VectorPort is implicit append
7893105Sstever@eecs.umich.edu            other = other._get_next()
7903105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
7913105Sstever@eecs.umich.edu            print "warning: overwriting port", self, \
7923105Sstever@eecs.umich.edu                  "value", self.peer, "with", other
7933101Sstever@eecs.umich.edu        self.peer = other
7943109Sstever@eecs.umich.edu        if proxy.isproxy(other):
7953109Sstever@eecs.umich.edu            other.set_param_desc(PortParamDesc())
7963109Sstever@eecs.umich.edu        elif isinstance(other, PortRef):
7973109Sstever@eecs.umich.edu            if other.peer is not self:
7983109Sstever@eecs.umich.edu                other.connect(self)
7993109Sstever@eecs.umich.edu        else:
8003109Sstever@eecs.umich.edu            raise TypeError, \
8013109Sstever@eecs.umich.edu                  "assigning non-port reference '%s' to port '%s'" \
8023109Sstever@eecs.umich.edu                  % (other, self)
8033101Sstever@eecs.umich.edu
8043105Sstever@eecs.umich.edu    def clone(self, simobj, memo):
8053105Sstever@eecs.umich.edu        if memo.has_key(self):
8063105Sstever@eecs.umich.edu            return memo[self]
8073101Sstever@eecs.umich.edu        newRef = copy.copy(self)
8083105Sstever@eecs.umich.edu        memo[self] = newRef
8093105Sstever@eecs.umich.edu        newRef.simobj = simobj
8103101Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
8113105Sstever@eecs.umich.edu        if self.peer and not proxy.isproxy(self.peer):
8123179Sstever@eecs.umich.edu            peerObj = self.peer.simobj(_memo=memo)
8133105Sstever@eecs.umich.edu            newRef.peer = self.peer.clone(peerObj, memo)
8143105Sstever@eecs.umich.edu            assert(not isinstance(newRef.peer, VectorPortRef))
8153101Sstever@eecs.umich.edu        return newRef
8163101Sstever@eecs.umich.edu
8173105Sstever@eecs.umich.edu    def unproxy(self, simobj):
8183105Sstever@eecs.umich.edu        assert(simobj is self.simobj)
8193105Sstever@eecs.umich.edu        if proxy.isproxy(self.peer):
8203105Sstever@eecs.umich.edu            try:
8213105Sstever@eecs.umich.edu                realPeer = self.peer.unproxy(self.simobj)
8223105Sstever@eecs.umich.edu            except:
8233105Sstever@eecs.umich.edu                print "Error in unproxying port '%s' of %s" % \
8243105Sstever@eecs.umich.edu                      (self.name, self.simobj.path())
8253105Sstever@eecs.umich.edu                raise
8263105Sstever@eecs.umich.edu            self.connect(realPeer)
8273105Sstever@eecs.umich.edu
8283101Sstever@eecs.umich.edu    # Call C++ to create corresponding port connection between C++ objects
8293101Sstever@eecs.umich.edu    def ccConnect(self):
8303101Sstever@eecs.umich.edu        if self.ccConnected: # already done this
8313101Sstever@eecs.umich.edu            return
8323101Sstever@eecs.umich.edu        peer = self.peer
8333624Sbinkertn@umich.edu        internal.main.connectPorts(self.simobj.getCCObject(), self.name,
8343624Sbinkertn@umich.edu                                   self.index, peer.simobj.getCCObject(),
8353624Sbinkertn@umich.edu                                   peer.name, peer.index)
8363101Sstever@eecs.umich.edu        self.ccConnected = True
8373101Sstever@eecs.umich.edu        peer.ccConnected = True
8383101Sstever@eecs.umich.edu
8393105Sstever@eecs.umich.edu# A reference to an individual element of a VectorPort... much like a
8403105Sstever@eecs.umich.edu# PortRef, but has an index.
8413105Sstever@eecs.umich.educlass VectorPortElementRef(PortRef):
8423105Sstever@eecs.umich.edu    def __init__(self, simobj, name, index):
8433105Sstever@eecs.umich.edu        PortRef.__init__(self, simobj, name)
8443105Sstever@eecs.umich.edu        self.index = index
8453105Sstever@eecs.umich.edu
8463105Sstever@eecs.umich.edu    def __str__(self):
8473105Sstever@eecs.umich.edu        return '%s.%s[%d]' % (self.simobj, self.name, self.index)
8483105Sstever@eecs.umich.edu
8493105Sstever@eecs.umich.edu# A reference to a complete vector-valued port (not just a single element).
8503105Sstever@eecs.umich.edu# Can be indexed to retrieve individual VectorPortElementRef instances.
8513105Sstever@eecs.umich.educlass VectorPortRef(object):
8523105Sstever@eecs.umich.edu    def __init__(self, simobj, name):
8533105Sstever@eecs.umich.edu        assert(isSimObject(simobj) or isSimObjectClass(simobj))
8543105Sstever@eecs.umich.edu        self.simobj = simobj
8553105Sstever@eecs.umich.edu        self.name = name
8563105Sstever@eecs.umich.edu        self.elements = []
8573105Sstever@eecs.umich.edu
8583109Sstever@eecs.umich.edu    def __str__(self):
8593109Sstever@eecs.umich.edu        return '%s.%s[:]' % (self.simobj, self.name)
8603109Sstever@eecs.umich.edu
8613105Sstever@eecs.umich.edu    # for config.ini, print peer's name (not ours)
8623105Sstever@eecs.umich.edu    def ini_str(self):
8633105Sstever@eecs.umich.edu        return ' '.join([el.ini_str() for el in self.elements])
8643105Sstever@eecs.umich.edu
8653105Sstever@eecs.umich.edu    def __getitem__(self, key):
8663105Sstever@eecs.umich.edu        if not isinstance(key, int):
8673105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
8683105Sstever@eecs.umich.edu        if key >= len(self.elements):
8693105Sstever@eecs.umich.edu            # need to extend list
8703105Sstever@eecs.umich.edu            ext = [VectorPortElementRef(self.simobj, self.name, i)
8713105Sstever@eecs.umich.edu                   for i in range(len(self.elements), key+1)]
8723105Sstever@eecs.umich.edu            self.elements.extend(ext)
8733105Sstever@eecs.umich.edu        return self.elements[key]
8743105Sstever@eecs.umich.edu
8753105Sstever@eecs.umich.edu    def _get_next(self):
8763105Sstever@eecs.umich.edu        return self[len(self.elements)]
8773105Sstever@eecs.umich.edu
8783105Sstever@eecs.umich.edu    def __setitem__(self, key, value):
8793105Sstever@eecs.umich.edu        if not isinstance(key, int):
8803105Sstever@eecs.umich.edu            raise TypeError, "VectorPort index must be integer"
8813105Sstever@eecs.umich.edu        self[key].connect(value)
8823105Sstever@eecs.umich.edu
8833105Sstever@eecs.umich.edu    def connect(self, other):
8843109Sstever@eecs.umich.edu        if isinstance(other, (list, tuple)):
8853109Sstever@eecs.umich.edu            # Assign list of port refs to vector port.
8863109Sstever@eecs.umich.edu            # For now, append them... not sure if that's the right semantics
8873109Sstever@eecs.umich.edu            # or if it should replace the current vector.
8883109Sstever@eecs.umich.edu            for ref in other:
8893109Sstever@eecs.umich.edu                self._get_next().connect(ref)
8903109Sstever@eecs.umich.edu        else:
8913109Sstever@eecs.umich.edu            # scalar assignment to plain VectorPort is implicit append
8923109Sstever@eecs.umich.edu            self._get_next().connect(other)
8933109Sstever@eecs.umich.edu
8943109Sstever@eecs.umich.edu    def clone(self, simobj, memo):
8953109Sstever@eecs.umich.edu        if memo.has_key(self):
8963109Sstever@eecs.umich.edu            return memo[self]
8973109Sstever@eecs.umich.edu        newRef = copy.copy(self)
8983109Sstever@eecs.umich.edu        memo[self] = newRef
8993109Sstever@eecs.umich.edu        newRef.simobj = simobj
9003109Sstever@eecs.umich.edu        assert(isSimObject(newRef.simobj))
9013109Sstever@eecs.umich.edu        newRef.elements = [el.clone(simobj, memo) for el in self.elements]
9023109Sstever@eecs.umich.edu        return newRef
9033105Sstever@eecs.umich.edu
9043105Sstever@eecs.umich.edu    def unproxy(self, simobj):
9053105Sstever@eecs.umich.edu        [el.unproxy(simobj) for el in self.elements]
9063105Sstever@eecs.umich.edu
9073105Sstever@eecs.umich.edu    def ccConnect(self):
9083105Sstever@eecs.umich.edu        [el.ccConnect() for el in self.elements]
9093105Sstever@eecs.umich.edu
9103101Sstever@eecs.umich.edu# Port description object.  Like a ParamDesc object, this represents a
9113101Sstever@eecs.umich.edu# logical port in the SimObject class, not a particular port on a
9123101Sstever@eecs.umich.edu# SimObject instance.  The latter are represented by PortRef objects.
9133101Sstever@eecs.umich.educlass Port(object):
9143105Sstever@eecs.umich.edu    # Port("description") or Port(default, "description")
9153105Sstever@eecs.umich.edu    def __init__(self, *args):
9163105Sstever@eecs.umich.edu        if len(args) == 1:
9173105Sstever@eecs.umich.edu            self.desc = args[0]
9183105Sstever@eecs.umich.edu        elif len(args) == 2:
9193105Sstever@eecs.umich.edu            self.default = args[0]
9203105Sstever@eecs.umich.edu            self.desc = args[1]
9213105Sstever@eecs.umich.edu        else:
9223105Sstever@eecs.umich.edu            raise TypeError, 'wrong number of arguments'
9233105Sstever@eecs.umich.edu        # self.name is set by SimObject class on assignment
9243105Sstever@eecs.umich.edu        # e.g., pio_port = Port("blah") sets self.name to 'pio_port'
9253101Sstever@eecs.umich.edu
9263101Sstever@eecs.umich.edu    # Generate a PortRef for this port on the given SimObject with the
9273101Sstever@eecs.umich.edu    # given name
9283105Sstever@eecs.umich.edu    def makeRef(self, simobj):
9293105Sstever@eecs.umich.edu        return PortRef(simobj, self.name)
9303101Sstever@eecs.umich.edu
9313101Sstever@eecs.umich.edu    # Connect an instance of this port (on the given SimObject with
9323101Sstever@eecs.umich.edu    # the given name) with the port described by the supplied PortRef
9333105Sstever@eecs.umich.edu    def connect(self, simobj, ref):
9343105Sstever@eecs.umich.edu        self.makeRef(simobj).connect(ref)
9353101Sstever@eecs.umich.edu
9363101Sstever@eecs.umich.edu# VectorPort description object.  Like Port, but represents a vector
9373101Sstever@eecs.umich.edu# of connections (e.g., as on a Bus).
9383101Sstever@eecs.umich.educlass VectorPort(Port):
9393105Sstever@eecs.umich.edu    def __init__(self, *args):
9403105Sstever@eecs.umich.edu        Port.__init__(self, *args)
9413101Sstever@eecs.umich.edu        self.isVec = True
9423101Sstever@eecs.umich.edu
9433105Sstever@eecs.umich.edu    def makeRef(self, simobj):
9443105Sstever@eecs.umich.edu        return VectorPortRef(simobj, self.name)
9453105Sstever@eecs.umich.edu
9463109Sstever@eecs.umich.edu# 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
9473109Sstever@eecs.umich.edu# proxy objects (via set_param_desc()) so that proxy error messages
9483109Sstever@eecs.umich.edu# make sense.
9493109Sstever@eecs.umich.educlass PortParamDesc(object):
9503109Sstever@eecs.umich.edu    __metaclass__ = Singleton
9513109Sstever@eecs.umich.edu
9523109Sstever@eecs.umich.edu    ptype_str = 'Port'
9533109Sstever@eecs.umich.edu    ptype = Port
9543105Sstever@eecs.umich.edu
9553101Sstever@eecs.umich.edu
9563101Sstever@eecs.umich.edu__all__ = ['Param', 'VectorParam',
9573101Sstever@eecs.umich.edu           'Enum', 'Bool', 'String', 'Float',
9583101Sstever@eecs.umich.edu           'Int', 'Unsigned', 'Int8', 'UInt8', 'Int16', 'UInt16',
9593101Sstever@eecs.umich.edu           'Int32', 'UInt32', 'Int64', 'UInt64',
9603101Sstever@eecs.umich.edu           'Counter', 'Addr', 'Tick', 'Percent',
9613101Sstever@eecs.umich.edu           'TcpPort', 'UdpPort', 'EthernetAddr',
9623101Sstever@eecs.umich.edu           'MemorySize', 'MemorySize32',
9633101Sstever@eecs.umich.edu           'Latency', 'Frequency', 'RootClock', 'Clock',
9643101Sstever@eecs.umich.edu           'NetworkBandwidth', 'MemoryBandwidth',
9653101Sstever@eecs.umich.edu           'Range', 'AddrRange', 'TickRange',
9663101Sstever@eecs.umich.edu           'MaxAddr', 'MaxTick', 'AllMemory',
9673102Sstever@eecs.umich.edu           'NextEthernetAddr', 'NULL',
9683101Sstever@eecs.umich.edu           'Port', 'VectorPort']
9693101Sstever@eecs.umich.edu
9703101Sstever@eecs.umich.edu# see comment on imports at end of __init__.py.
9713102Sstever@eecs.umich.edufrom SimObject import isSimObject, isSimObjectSequence, isSimObjectClass
9723101Sstever@eecs.umich.eduimport proxy
9733102Sstever@eecs.umich.eduimport objects
9743624Sbinkertn@umich.eduimport internal
975