kernel.cc (13067:3d6ef32002ef) kernel.cc (13077:0037323cb4dd)
1/*
2 * Copyright 2018 Google, Inc.
3 *
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.
14 *
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.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/kernel.hh"
31
32#include "base/logging.hh"
33#include "systemc/core/channel.hh"
34#include "systemc/core/module.hh"
35#include "systemc/core/scheduler.hh"
36
37namespace sc_gem5
38{
39
40namespace
41{
42
1/*
2 * Copyright 2018 Google, Inc.
3 *
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.
14 *
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.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/kernel.hh"
31
32#include "base/logging.hh"
33#include "systemc/core/channel.hh"
34#include "systemc/core/module.hh"
35#include "systemc/core/scheduler.hh"
36
37namespace sc_gem5
38{
39
40namespace
41{
42
43bool scMainDone = false;
43bool stopAfterCallbacks = false;
44bool startComplete = false;
45bool endComplete = false;
46
47sc_core::sc_status _status = sc_core::SC_ELABORATION;
48
49} // anonymous namespace
50
51bool Kernel::startOfSimulationComplete() { return startComplete; }
52bool Kernel::endOfSimulationComplete() { return endComplete; }
53
44bool stopAfterCallbacks = false;
45bool startComplete = false;
46bool endComplete = false;
47
48sc_core::sc_status _status = sc_core::SC_ELABORATION;
49
50} // anonymous namespace
51
52bool Kernel::startOfSimulationComplete() { return startComplete; }
53bool Kernel::endOfSimulationComplete() { return endComplete; }
54
55bool Kernel::scMainFinished() { return scMainDone; }
56void Kernel::scMainFinished(bool finished) { scMainDone = finished; }
57
54sc_core::sc_status Kernel::status() { return _status; }
55void Kernel::status(sc_core::sc_status s) { _status = s; }
56
57Kernel::Kernel(Params *params) :
58 SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
59{
60 // Install ourselves as the scheduler's event manager.
61 ::sc_gem5::scheduler.setEventQueue(eventQueue());
62}
63
64void
65Kernel::init()
66{
58sc_core::sc_status Kernel::status() { return _status; }
59void Kernel::status(sc_core::sc_status s) { _status = s; }
60
61Kernel::Kernel(Params *params) :
62 SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
63{
64 // Install ourselves as the scheduler's event manager.
65 ::sc_gem5::scheduler.setEventQueue(eventQueue());
66}
67
68void
69Kernel::init()
70{
71 if (scMainDone)
72 return;
73
67 status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
68 for (auto m: sc_gem5::allModules) {
69 callbackModule(m);
70 m->sc_mod()->before_end_of_elaboration();
71 for (auto p: m->ports)
72 p->before_end_of_elaboration();
73 for (auto e: m->exports)
74 e->before_end_of_elaboration();
75 }
76 callbackModule(nullptr);
77 for (auto c: sc_gem5::allChannels)
78 c->sc_chan()->before_end_of_elaboration();
79
80 if (stopAfterCallbacks)
81 stopWork();
82}
83
84void
85Kernel::regStats()
86{
74 status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
75 for (auto m: sc_gem5::allModules) {
76 callbackModule(m);
77 m->sc_mod()->before_end_of_elaboration();
78 for (auto p: m->ports)
79 p->before_end_of_elaboration();
80 for (auto e: m->exports)
81 e->before_end_of_elaboration();
82 }
83 callbackModule(nullptr);
84 for (auto c: sc_gem5::allChannels)
85 c->sc_chan()->before_end_of_elaboration();
86
87 if (stopAfterCallbacks)
88 stopWork();
89}
90
91void
92Kernel::regStats()
93{
94 if (scMainDone)
95 return;
96
87 for (auto m: sc_gem5::allModules)
88 for (auto p: m->ports)
89 p->_gem5Finalize();
90
91 status(::sc_core::SC_END_OF_ELABORATION);
92 for (auto m: sc_gem5::allModules) {
93 m->sc_mod()->end_of_elaboration();
94 for (auto p: m->ports)
95 p->end_of_elaboration();
96 for (auto e: m->exports)
97 e->end_of_elaboration();
98 }
99 for (auto c: sc_gem5::allChannels)
100 c->sc_chan()->end_of_elaboration();
101
102 if (stopAfterCallbacks)
103 stopWork();
104}
105
106void
107Kernel::startup()
108{
97 for (auto m: sc_gem5::allModules)
98 for (auto p: m->ports)
99 p->_gem5Finalize();
100
101 status(::sc_core::SC_END_OF_ELABORATION);
102 for (auto m: sc_gem5::allModules) {
103 m->sc_mod()->end_of_elaboration();
104 for (auto p: m->ports)
105 p->end_of_elaboration();
106 for (auto e: m->exports)
107 e->end_of_elaboration();
108 }
109 for (auto c: sc_gem5::allChannels)
110 c->sc_chan()->end_of_elaboration();
111
112 if (stopAfterCallbacks)
113 stopWork();
114}
115
116void
117Kernel::startup()
118{
119 if (scMainDone)
120 return;
121
109 status(::sc_core::SC_START_OF_SIMULATION);
110 for (auto m: sc_gem5::allModules) {
111 m->sc_mod()->start_of_simulation();
112 for (auto p: m->ports)
113 p->start_of_simulation();
114 for (auto e: m->exports)
115 e->start_of_simulation();
116 }
117 for (auto c: sc_gem5::allChannels)
118 c->sc_chan()->start_of_simulation();
119
120 startComplete = true;
121
122 if (stopAfterCallbacks)
123 stopWork();
124
125 kernel->status(::sc_core::SC_RUNNING);
126
127 schedule(t0Event, curTick());
128 // Run update once before the event queue starts.
129 ::sc_gem5::scheduler.update();
130}
131
132void
133Kernel::stop()
134{
135 if (status() < ::sc_core::SC_RUNNING)
136 stopAfterCallbacks = true;
137 else
138 stopWork();
139}
140
141void
142Kernel::stopWork()
143{
144 status(::sc_core::SC_END_OF_SIMULATION);
145 for (auto m: sc_gem5::allModules) {
146 m->sc_mod()->end_of_simulation();
147 for (auto p: m->ports)
148 p->end_of_simulation();
149 for (auto e: m->exports)
150 e->end_of_simulation();
151 }
152 for (auto c: sc_gem5::allChannels)
153 c->sc_chan()->end_of_simulation();
154
155 endComplete = true;
156
157 status(::sc_core::SC_STOPPED);
158
159 if (stopAfterCallbacks)
160 fatal("Simulation called sc_stop during elaboration.\n");
161}
162
163void
164Kernel::t0Handler()
165{
166 ::sc_gem5::scheduler.initPhase();
167
168 status(::sc_core::SC_RUNNING);
169}
170
171Kernel *kernel;
172
173} // namespace sc_gem5
174
175sc_gem5::Kernel *
176SystemC_KernelParams::create()
177{
178 panic_if(sc_gem5::kernel,
179 "Only one systemc kernel object may be defined.\n");
180 sc_gem5::kernel = new sc_gem5::Kernel(this);
181 return sc_gem5::kernel;
182}
122 status(::sc_core::SC_START_OF_SIMULATION);
123 for (auto m: sc_gem5::allModules) {
124 m->sc_mod()->start_of_simulation();
125 for (auto p: m->ports)
126 p->start_of_simulation();
127 for (auto e: m->exports)
128 e->start_of_simulation();
129 }
130 for (auto c: sc_gem5::allChannels)
131 c->sc_chan()->start_of_simulation();
132
133 startComplete = true;
134
135 if (stopAfterCallbacks)
136 stopWork();
137
138 kernel->status(::sc_core::SC_RUNNING);
139
140 schedule(t0Event, curTick());
141 // Run update once before the event queue starts.
142 ::sc_gem5::scheduler.update();
143}
144
145void
146Kernel::stop()
147{
148 if (status() < ::sc_core::SC_RUNNING)
149 stopAfterCallbacks = true;
150 else
151 stopWork();
152}
153
154void
155Kernel::stopWork()
156{
157 status(::sc_core::SC_END_OF_SIMULATION);
158 for (auto m: sc_gem5::allModules) {
159 m->sc_mod()->end_of_simulation();
160 for (auto p: m->ports)
161 p->end_of_simulation();
162 for (auto e: m->exports)
163 e->end_of_simulation();
164 }
165 for (auto c: sc_gem5::allChannels)
166 c->sc_chan()->end_of_simulation();
167
168 endComplete = true;
169
170 status(::sc_core::SC_STOPPED);
171
172 if (stopAfterCallbacks)
173 fatal("Simulation called sc_stop during elaboration.\n");
174}
175
176void
177Kernel::t0Handler()
178{
179 ::sc_gem5::scheduler.initPhase();
180
181 status(::sc_core::SC_RUNNING);
182}
183
184Kernel *kernel;
185
186} // namespace sc_gem5
187
188sc_gem5::Kernel *
189SystemC_KernelParams::create()
190{
191 panic_if(sc_gem5::kernel,
192 "Only one systemc kernel object may be defined.\n");
193 sc_gem5::kernel = new sc_gem5::Kernel(this);
194 return sc_gem5::kernel;
195}