simulate.cc (9174:2171e04a2ee5) simulate.cc (9356:b279bad40aa3)
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"
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.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 */
47SimLoopExitEvent *
48simulate(Tick num_cycles)
49{
50 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
51
52 if (num_cycles < MaxTick - curTick())
53 num_cycles = curTick() + num_cycles;
54 else // counter would roll over or be set to MaxTick anyhow
55 num_cycles = MaxTick;
56
57 Event *limit_event =
58 new SimLoopExitEvent("simulate() limit reached", 0);
59 mainEventQueue.schedule(limit_event, num_cycles);
60
61 while (1) {
62 // there should always be at least one event (the SimLoopExitEvent
63 // we just scheduled) in the queue
64 assert(!mainEventQueue.empty());
65 assert(curTick() <= mainEventQueue.nextTick() &&
66 "event scheduled in the past");
67
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 */
47SimLoopExitEvent *
48simulate(Tick num_cycles)
49{
50 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
51
52 if (num_cycles < MaxTick - curTick())
53 num_cycles = curTick() + num_cycles;
54 else // counter would roll over or be set to MaxTick anyhow
55 num_cycles = MaxTick;
56
57 Event *limit_event =
58 new SimLoopExitEvent("simulate() limit reached", 0);
59 mainEventQueue.schedule(limit_event, num_cycles);
60
61 while (1) {
62 // there should always be at least one event (the SimLoopExitEvent
63 // we just scheduled) in the queue
64 assert(!mainEventQueue.empty());
65 assert(curTick() <= mainEventQueue.nextTick() &&
66 "event scheduled in the past");
67
68 // forward current cycle to the time of the first event on the
69 // queue
70 curTick(mainEventQueue.nextTick());
71 Event *exit_event = mainEventQueue.serviceOne();
72 if (exit_event != NULL) {
73 // hit some kind of exit event; return to Python
74 // event must be subclass of SimLoopExitEvent...
75 SimLoopExitEvent *se_event;
76 se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
77
78 if (se_event == NULL)
79 panic("Bogus exit event class!");
80
81 // if we didn't hit limit_event, delete it
82 if (se_event != limit_event) {
83 assert(limit_event->scheduled());
84 limit_event->squash();
85 hack_once("be nice to actually delete the event here");
86 }
87
88 return se_event;
89 }
90
91 if (async_event) {
92 async_event = false;
93 if (async_statdump || async_statreset) {
94 Stats::schedStatEvent(async_statdump, async_statreset);
95 async_statdump = false;
96 async_statreset = false;
97 }
98
99 if (async_exit) {
100 async_exit = false;
101 exitSimLoop("user interrupt received");
102 }
103
104 if (async_io || async_alarm) {
105 async_io = false;
106 async_alarm = false;
107 pollQueue.service();
108 }
109
110 if (async_exception) {
111 async_exception = false;
112 return NULL;
113 }
114 }
115 }
116
117 // not reached... only exit is return on SimLoopExitEvent
118}
119
68 Event *exit_event = mainEventQueue.serviceOne();
69 if (exit_event != NULL) {
70 // hit some kind of exit event; return to Python
71 // event must be subclass of SimLoopExitEvent...
72 SimLoopExitEvent *se_event;
73 se_event = dynamic_cast<SimLoopExitEvent *>(exit_event);
74
75 if (se_event == NULL)
76 panic("Bogus exit event class!");
77
78 // if we didn't hit limit_event, delete it
79 if (se_event != limit_event) {
80 assert(limit_event->scheduled());
81 limit_event->squash();
82 hack_once("be nice to actually delete the event here");
83 }
84
85 return se_event;
86 }
87
88 if (async_event) {
89 async_event = false;
90 if (async_statdump || async_statreset) {
91 Stats::schedStatEvent(async_statdump, async_statreset);
92 async_statdump = false;
93 async_statreset = false;
94 }
95
96 if (async_exit) {
97 async_exit = false;
98 exitSimLoop("user interrupt received");
99 }
100
101 if (async_io || async_alarm) {
102 async_io = false;
103 async_alarm = false;
104 pollQueue.service();
105 }
106
107 if (async_exception) {
108 async_exception = false;
109 return NULL;
110 }
111 }
112 }
113
114 // not reached... only exit is return on SimLoopExitEvent
115}
116