simulate.cc revision 7822
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2006 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com *
2812855Sgabeblack@google.com * Authors: Nathan Binkert
2912855Sgabeblack@google.com *          Steve Reinhardt
3012855Sgabeblack@google.com */
3112855Sgabeblack@google.com
3212855Sgabeblack@google.com#include "base/misc.hh"
3312855Sgabeblack@google.com#include "base/pollevent.hh"
3412855Sgabeblack@google.com#include "base/types.hh"
3512855Sgabeblack@google.com#include "sim/async.hh"
3612855Sgabeblack@google.com#include "sim/eventq.hh"
3712855Sgabeblack@google.com#include "sim/sim_events.hh"
3812855Sgabeblack@google.com#include "sim/sim_exit.hh"
3912855Sgabeblack@google.com#include "sim/simulate.hh"
4012855Sgabeblack@google.com#include "sim/stat_control.hh"
4112855Sgabeblack@google.com
4212855Sgabeblack@google.com/** Simulate for num_cycles additional cycles.  If num_cycles is -1
4312855Sgabeblack@google.com * (the default), do not limit simulation; some other event must
4412855Sgabeblack@google.com * terminate the loop.  Exported to Python via SWIG.
4512855Sgabeblack@google.com * @return The SimLoopExitEvent that caused the loop to exit.
4612855Sgabeblack@google.com */
4712855Sgabeblack@google.comSimLoopExitEvent *
4812855Sgabeblack@google.comsimulate(Tick num_cycles)
4912855Sgabeblack@google.com{
5012855Sgabeblack@google.com    inform("Entering event queue @ %d.  Starting simulation...\n", curTick);
5112855Sgabeblack@google.com
5212855Sgabeblack@google.com    if (num_cycles < 0)
5312855Sgabeblack@google.com        fatal("simulate: num_cycles must be >= 0 (was %d)\n", num_cycles);
5412855Sgabeblack@google.com    else if (curTick + num_cycles < 0)  //Overflow
5512855Sgabeblack@google.com        num_cycles = MaxTick;
56    else
57        num_cycles = curTick + num_cycles;
58
59    Event *limit_event =
60        new SimLoopExitEvent("simulate() limit reached", 0);
61    mainEventQueue.schedule(limit_event, num_cycles);
62
63    while (1) {
64        // there should always be at least one event (the SimLoopExitEvent
65        // we just scheduled) in the queue
66        assert(!mainEventQueue.empty());
67        assert(curTick <= mainEventQueue.nextTick() &&
68               "event scheduled in the past");
69
70        // forward current cycle to the time of the first event on the
71        // queue
72        curTick = mainEventQueue.nextTick();
73        Event *exit_event = mainEventQueue.serviceOne();
74        if (exit_event != NULL) {
75            // hit some kind of exit event; return to Python
76            // event must be subclass of SimLoopExitEvent...
77            SimLoopExitEvent *se_event;
78            se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
79
80            if (se_event == NULL)
81                panic("Bogus exit event class!");
82
83            // if we didn't hit limit_event, delete it
84            if (se_event != limit_event) {
85                assert(limit_event->scheduled());
86                limit_event->squash();
87                hack_once("be nice to actually delete the event here");
88            }
89
90            return se_event;
91        }
92
93        if (async_event) {
94            async_event = false;
95            if (async_statdump || async_statreset) {
96                Stats::schedStatEvent(async_statdump, async_statreset);
97                async_statdump = false;
98                async_statreset = false;
99            }
100
101            if (async_exit) {
102                async_exit = false;
103                exitSimLoop("user interrupt received");
104            }
105
106            if (async_io || async_alarm) {
107                async_io = false;
108                async_alarm = false;
109                pollQueue.service();
110            }
111
112            if (async_exception) {
113                async_exception = false;
114                return NULL;
115            }
116        }
117    }
118
119    // not reached... only exit is return on SimLoopExitEvent
120}
121
122