root.hh revision 12185
17861Sgblack@eecs.umich.edu/*
28332Snate@binkert.org * Copyright (c) 2011 Advanced Micro Devices, Inc.
37861Sgblack@eecs.umich.edu * All rights reserved.
47861Sgblack@eecs.umich.edu *
57861Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
67861Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
77861Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
87861Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
97861Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
107861Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
117861Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
127861Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
137861Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
147861Sgblack@eecs.umich.edu * this software without specific prior written permission.
157861Sgblack@eecs.umich.edu *
167861Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
177861Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
187861Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
197861Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
207861Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
217861Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
227861Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237861Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247861Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257861Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
267861Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277861Sgblack@eecs.umich.edu *
287861Sgblack@eecs.umich.edu * Authors: Gabe Black
297861Sgblack@eecs.umich.edu */
307861Sgblack@eecs.umich.edu
317861Sgblack@eecs.umich.edu/**
327861Sgblack@eecs.umich.edu * @file This file defines the Root simobject and the methods used to control
337861Sgblack@eecs.umich.edu * the time syncing mechanism provided through it.
347861Sgblack@eecs.umich.edu *
357861Sgblack@eecs.umich.edu * Time syncing prevents simulated time from passing faster than real time. It
367861Sgblack@eecs.umich.edu * works by scheduling a periodic event that checks to see if its simulated
377861Sgblack@eecs.umich.edu * period is shorter than its real period. If it is, it stalls the simulation
387861Sgblack@eecs.umich.edu * until they're equal.
397861Sgblack@eecs.umich.edu */
407861Sgblack@eecs.umich.edu
417861Sgblack@eecs.umich.edu#ifndef __SIM_ROOT_HH__
427861Sgblack@eecs.umich.edu#define __SIM_ROOT_HH__
437861Sgblack@eecs.umich.edu
447861Sgblack@eecs.umich.edu#include "base/time.hh"
457861Sgblack@eecs.umich.edu#include "params/Root.hh"
467861Sgblack@eecs.umich.edu#include "sim/eventq.hh"
477861Sgblack@eecs.umich.edu#include "sim/sim_object.hh"
487861Sgblack@eecs.umich.edu
497861Sgblack@eecs.umich.educlass Root : public SimObject
507861Sgblack@eecs.umich.edu{
517861Sgblack@eecs.umich.edu  private:
527861Sgblack@eecs.umich.edu    static Root *_root;
537861Sgblack@eecs.umich.edu
547861Sgblack@eecs.umich.edu  protected:
557861Sgblack@eecs.umich.edu    bool _enabled;
567861Sgblack@eecs.umich.edu    Time _period;
577861Sgblack@eecs.umich.edu    Tick _periodTick;
587861Sgblack@eecs.umich.edu    Time _spinThreshold;
597861Sgblack@eecs.umich.edu
607861Sgblack@eecs.umich.edu    Time lastTime;
617861Sgblack@eecs.umich.edu
627861Sgblack@eecs.umich.edu    void timeSync();
6312088Sspwilson2@wisc.edu    EventFunctionWrapper syncEvent;
647861Sgblack@eecs.umich.edu
657861Sgblack@eecs.umich.edu  public:
667861Sgblack@eecs.umich.edu    /**
677861Sgblack@eecs.umich.edu     * Use this function to get a pointer to the single Root object in the
687861Sgblack@eecs.umich.edu     * simulation. This function asserts that such an object has actual been
697861Sgblack@eecs.umich.edu     * constructed to avoid having to perform that check everywhere the root
707861Sgblack@eecs.umich.edu     * is used. This is to allow calling the functions below.
717861Sgblack@eecs.umich.edu     *
727861Sgblack@eecs.umich.edu     * @return Pointer to the single root object.
737861Sgblack@eecs.umich.edu     */
747861Sgblack@eecs.umich.edu    static Root *
757861Sgblack@eecs.umich.edu    root()
767861Sgblack@eecs.umich.edu    {
777861Sgblack@eecs.umich.edu        assert(_root);
787861Sgblack@eecs.umich.edu        return _root;
797861Sgblack@eecs.umich.edu    }
807861Sgblack@eecs.umich.edu
817861Sgblack@eecs.umich.edu  public:
827861Sgblack@eecs.umich.edu
837861Sgblack@eecs.umich.edu    /// Check whether time syncing is enabled.
847861Sgblack@eecs.umich.edu    bool timeSyncEnabled() const { return _enabled; }
857861Sgblack@eecs.umich.edu    /// Retrieve the period for the sync event.
867861Sgblack@eecs.umich.edu    const Time timeSyncPeriod() const { return _period; }
877861Sgblack@eecs.umich.edu    /// Retrieve the threshold for time remaining to spin wait.
887861Sgblack@eecs.umich.edu    const Time timeSyncSpinThreshold() const { return _spinThreshold; }
897861Sgblack@eecs.umich.edu
907861Sgblack@eecs.umich.edu    /// Enable or disable time syncing.
917861Sgblack@eecs.umich.edu    void timeSyncEnable(bool en);
927861Sgblack@eecs.umich.edu    /// Configure the period for time sync events.
937861Sgblack@eecs.umich.edu    void timeSyncPeriod(Time newPeriod);
947861Sgblack@eecs.umich.edu    /// Set the threshold for time remaining to spin wait.
957861Sgblack@eecs.umich.edu    void timeSyncSpinThreshold(Time newThreshold);
967861Sgblack@eecs.umich.edu
977942SAli.Saidi@ARM.com    typedef RootParams Params;
987942SAli.Saidi@ARM.com    const Params *
997942SAli.Saidi@ARM.com    params() const
1007942SAli.Saidi@ARM.com    {
1017942SAli.Saidi@ARM.com        return dynamic_cast<const Params *>(_params);
1027942SAli.Saidi@ARM.com    }
1037942SAli.Saidi@ARM.com
1047942SAli.Saidi@ARM.com    Root(Params *p);
1057942SAli.Saidi@ARM.com
10612185Sgabeblack@google.com    /** Schedule the timesync event at startup().
1077942SAli.Saidi@ARM.com     */
10812185Sgabeblack@google.com    void startup() override;
1099048SAli.Saidi@ARM.com
11011168Sandreas.hansson@arm.com    void serialize(CheckpointOut &cp) const override;
1117861Sgblack@eecs.umich.edu};
1127861Sgblack@eecs.umich.edu
1137861Sgblack@eecs.umich.edu#endif // __SIM_ROOT_HH__
114