2 * Copyright 2018 Google, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "systemc/core/kernel.hh"
32 #include "base/logging.hh"
33 #include "systemc/core/channel.hh"
34 #include "systemc/core/module.hh"
35 #include "systemc/core/port.hh"
36 #include "systemc/core/scheduler.hh"
44 bool scMainDone
= false;
45 bool stopAfterCallbacks
= false;
46 bool startComplete
= false;
47 bool endComplete
= false;
49 sc_core::sc_status _status
= sc_core::SC_ELABORATION
;
51 } // anonymous namespace
53 bool Kernel::startOfSimulationComplete() { return startComplete
; }
54 bool Kernel::endOfSimulationComplete() { return endComplete
; }
56 bool Kernel::scMainFinished() { return scMainDone
; }
57 void Kernel::scMainFinished(bool finished
) { scMainDone
= finished
; }
59 sc_core::sc_status
Kernel::status() { return _status
; }
60 void Kernel::status(sc_core::sc_status s
) { _status
= s
; }
62 Kernel::Kernel(Params
*params
) :
63 SimObject(params
), t0Event(this, false, EventBase::Default_Pri
- 1)
65 // Install ourselves as the scheduler's event manager.
66 ::sc_gem5::scheduler
.setEventQueue(eventQueue());
75 if (stopAfterCallbacks
)
76 fatal("Simulation called sc_stop during elaboration.\n");
78 status(::sc_core::SC_BEFORE_END_OF_ELABORATION
);
79 for (auto p
: allPorts
)
80 p
->sc_port_base()->before_end_of_elaboration();
81 for (auto m
: sc_gem5::allModules
)
82 m
->beforeEndOfElaboration();
83 for (auto c
: sc_gem5::allChannels
)
84 c
->sc_chan()->before_end_of_elaboration();
86 ::sc_gem5::scheduler
.elaborationDone(true);
92 if (scMainDone
|| stopAfterCallbacks
)
96 for (auto p
: allPorts
)
98 for (auto p
: allPorts
)
101 status(::sc_core::SC_END_OF_ELABORATION
);
102 for (auto p
: allPorts
)
103 p
->sc_port_base()->end_of_elaboration();
104 for (auto m
: sc_gem5::allModules
)
105 m
->endOfElaboration();
106 for (auto c
: sc_gem5::allChannels
)
107 c
->sc_chan()->end_of_elaboration();
109 ::sc_gem5::scheduler
.throwToScMain();
119 schedule(t0Event
, curTick());
121 if (stopAfterCallbacks
)
125 status(::sc_core::SC_START_OF_SIMULATION
);
126 for (auto p
: allPorts
)
127 p
->sc_port_base()->start_of_simulation();
128 for (auto m
: sc_gem5::allModules
)
129 m
->startOfSimulation();
130 for (auto c
: sc_gem5::allChannels
)
131 c
->sc_chan()->start_of_simulation();
133 ::sc_gem5::scheduler
.throwToScMain();
136 startComplete
= true;
138 if (stopAfterCallbacks
)
141 kernel
->status(::sc_core::SC_RUNNING
);
147 if (status() < ::sc_core::SC_RUNNING
)
148 stopAfterCallbacks
= true;
156 status(::sc_core::SC_END_OF_SIMULATION
);
158 for (auto p
: allPorts
)
159 p
->sc_port_base()->end_of_simulation();
160 for (auto m
: sc_gem5::allModules
)
161 m
->endOfSimulation();
162 for (auto c
: sc_gem5::allChannels
)
163 c
->sc_chan()->end_of_simulation();
165 ::sc_gem5::scheduler
.throwToScMain();
170 status(::sc_core::SC_STOPPED
);
176 if (stopAfterCallbacks
) {
178 ::sc_gem5::scheduler
.initPhase();
179 scheduler
.scheduleStop(false);
181 ::sc_gem5::scheduler
.initPhase();
182 status(::sc_core::SC_RUNNING
);
188 } // namespace sc_gem5
191 SystemC_KernelParams::create()
193 panic_if(sc_gem5::kernel
,
194 "Only one systemc kernel object may be defined.\n");
195 sc_gem5::kernel
= new sc_gem5::Kernel(this);
196 return sc_gem5::kernel
;