14167Sbinkertn@umich.edu# Copyright (c) 2007 The Regents of The University of Michigan
24167Sbinkertn@umich.edu# All rights reserved.
34167Sbinkertn@umich.edu#
44167Sbinkertn@umich.edu# Redistribution and use in source and binary forms, with or without
54167Sbinkertn@umich.edu# modification, are permitted provided that the following conditions are
64167Sbinkertn@umich.edu# met: redistributions of source code must retain the above copyright
74167Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer;
84167Sbinkertn@umich.edu# redistributions in binary form must reproduce the above copyright
94167Sbinkertn@umich.edu# notice, this list of conditions and the following disclaimer in the
104167Sbinkertn@umich.edu# documentation and/or other materials provided with the distribution;
114167Sbinkertn@umich.edu# neither the name of the copyright holders nor the names of its
124167Sbinkertn@umich.edu# contributors may be used to endorse or promote products derived from
134167Sbinkertn@umich.edu# this software without specific prior written permission.
144167Sbinkertn@umich.edu#
154167Sbinkertn@umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
164167Sbinkertn@umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
174167Sbinkertn@umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
184167Sbinkertn@umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
194167Sbinkertn@umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
204167Sbinkertn@umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
214167Sbinkertn@umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224167Sbinkertn@umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234167Sbinkertn@umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
244167Sbinkertn@umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
254167Sbinkertn@umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264167Sbinkertn@umich.edu#
274167Sbinkertn@umich.edu# Authors: Nathan Binkert
284167Sbinkertn@umich.edu
2912563Sgabeblack@google.comfrom __future__ import print_function
3013719Sandreas.sandberg@arm.comimport six
3113719Sandreas.sandberg@arm.comif six.PY3:
3213719Sandreas.sandberg@arm.com    long = int
3312563Sgabeblack@google.com
344167Sbinkertn@umich.eduimport sys
359528Ssascha.bischoff@arm.comfrom m5.util import warn
364167Sbinkertn@umich.edu
3713409Sgabeblack@google.com# fix the global frequency
384167Sbinkertn@umich.edudef fixGlobalFrequency():
3911802Sandreas.sandberg@arm.com    import _m5.core
4013409Sgabeblack@google.com    _m5.core.fixClockFrequency()
414167Sbinkertn@umich.edu
424167Sbinkertn@umich.edudef setGlobalFrequency(ticksPerSecond):
436654Snate@binkert.org    from m5.util import convert
4413409Sgabeblack@google.com    import _m5.core
454167Sbinkertn@umich.edu
464167Sbinkertn@umich.edu    if isinstance(ticksPerSecond, (int, long)):
474167Sbinkertn@umich.edu        tps = ticksPerSecond
484167Sbinkertn@umich.edu    elif isinstance(ticksPerSecond, float):
494167Sbinkertn@umich.edu        tps = ticksPerSecond
504167Sbinkertn@umich.edu    elif isinstance(ticksPerSecond, str):
514167Sbinkertn@umich.edu        tps = round(convert.anyToFrequency(ticksPerSecond))
524167Sbinkertn@umich.edu    else:
5313663Sandreas.sandberg@arm.com        raise TypeError(
5413663Sandreas.sandberg@arm.com            "wrong type '%s' for ticksPerSecond" % type(ticksPerSecond))
5513431Ssrikant.bharadwaj@amd.com    _m5.core.setClockFrequency(int(tps))
564167Sbinkertn@umich.edu
574167Sbinkertn@umich.edu# how big does a rounding error need to be before we warn about it?
584167Sbinkertn@umich.edufrequency_tolerance = 0.001  # 0.1%
594167Sbinkertn@umich.edu
604167Sbinkertn@umich.edudef fromSeconds(value):
6113409Sgabeblack@google.com    import _m5.core
6213409Sgabeblack@google.com
634167Sbinkertn@umich.edu    if not isinstance(value, float):
6413663Sandreas.sandberg@arm.com        raise TypeError("can't convert '%s' to type tick" % type(value))
654167Sbinkertn@umich.edu
664167Sbinkertn@umich.edu    # once someone needs to convert to seconds, the global frequency
674167Sbinkertn@umich.edu    # had better be fixed
6813409Sgabeblack@google.com    if not _m5.core.clockFrequencyFixed():
6913663Sandreas.sandberg@arm.com        raise AttributeError(
7013663Sandreas.sandberg@arm.com              "In order to do conversions, the global frequency must be fixed")
714167Sbinkertn@umich.edu
724167Sbinkertn@umich.edu    if value == 0:
734167Sbinkertn@umich.edu        return 0
744167Sbinkertn@umich.edu
754167Sbinkertn@umich.edu    # convert the value from time to ticks
7613409Sgabeblack@google.com    value *= _m5.core.getClockFrequency()
774167Sbinkertn@umich.edu
784167Sbinkertn@umich.edu    int_value = int(round(value))
794167Sbinkertn@umich.edu    err = (value - int_value) / value
804167Sbinkertn@umich.edu    if err > frequency_tolerance:
819528Ssascha.bischoff@arm.com        warn("rounding error > tolerance\n    %f rounded to %d", value,
829528Ssascha.bischoff@arm.com            int_value)
834167Sbinkertn@umich.edu    return int_value
844167Sbinkertn@umich.edu
854167Sbinkertn@umich.edu__all__ = [ 'setGlobalFrequency', 'fixGlobalFrequency', 'fromSeconds',
864167Sbinkertn@umich.edu            'frequency_tolerance' ]
87