simulate.cc revision 9456:34e3295b0e39
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Steve Reinhardt
30 */
31
32#include "base/misc.hh"
33#include "base/pollevent.hh"
34#include "base/types.hh"
35#include "sim/async.hh"
36#include "sim/eventq_impl.hh"
37#include "sim/sim_events.hh"
38#include "sim/sim_exit.hh"
39#include "sim/simulate.hh"
40#include "sim/stat_control.hh"
41
42/** Simulate for num_cycles additional cycles.  If num_cycles is -1
43 * (the default), do not limit simulation; some other event must
44 * terminate the loop.  Exported to Python via SWIG.
45 * @return The SimLoopExitEvent that caused the loop to exit.
46 */
47
48// record the clock cycle for last exit event
49Tick lastExitTick = 0;
50
51SimLoopExitEvent *
52simulate(Tick num_cycles)
53{
54    inform("Entering event queue @ %d.  Starting simulation...\n", curTick());
55
56    if (num_cycles < MaxTick - curTick())
57        num_cycles = curTick() + num_cycles;
58    else // counter would roll over or be set to MaxTick anyhow
59        num_cycles = MaxTick;
60
61    Event *limit_event =
62        new SimLoopExitEvent("simulate() limit reached", 0);
63    mainEventQueue.schedule(limit_event, num_cycles);
64
65    while (1) {
66        // there should always be at least one event (the SimLoopExitEvent
67        // we just scheduled) in the queue
68        assert(!mainEventQueue.empty());
69        assert(curTick() <= mainEventQueue.nextTick() &&
70               "event scheduled in the past");
71
72        Event *exit_event = mainEventQueue.serviceOne();
73        if (exit_event != NULL) {
74            /*
75             * if there are multiple exit events in the same cycle, drain the
76             * following exit events since gem5 only allows one * exit event in
77             * a cycle
78             */
79            if (lastExitTick == curTick())
80                continue;
81            else
82                lastExitTick = curTick();
83
84            // hit some kind of exit event; return to Python
85            // event must be subclass of SimLoopExitEvent...
86            SimLoopExitEvent *se_event;
87            se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
88
89            if (se_event == NULL)
90                panic("Bogus exit event class!");
91
92            // if we didn't hit limit_event, delete it
93            if (se_event != limit_event) {
94                assert(limit_event->scheduled());
95                limit_event->squash();
96                hack_once("be nice to actually delete the event here");
97            }
98
99            return se_event;
100        }
101
102        if (async_event) {
103            async_event = false;
104            if (async_statdump || async_statreset) {
105                Stats::schedStatEvent(async_statdump, async_statreset);
106                async_statdump = false;
107                async_statreset = false;
108            }
109
110            if (async_exit) {
111                async_exit = false;
112                exitSimLoop("user interrupt received");
113            }
114
115            if (async_io || async_alarm) {
116                async_io = false;
117                async_alarm = false;
118                pollQueue.service();
119            }
120
121            if (async_exception) {
122                async_exception = false;
123                return NULL;
124            }
125        }
126    }
127
128    // not reached... only exit is return on SimLoopExitEvent
129}
130
131