kernel.cc (13042:8c1fb556547e) kernel.cc (13045:ccedccd0d93d)
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/module.hh"
34#include "systemc/core/scheduler.hh"
35
36namespace sc_gem5
37{
38
39namespace
40{
41
42bool stopAfterCallbacks = false;
43bool startComplete = false;
44bool endComplete = false;
45
46sc_core::sc_status _status = sc_core::SC_ELABORATION;
47
48} // anonymous namespace
49
50bool Kernel::startOfSimulationComplete() { return startComplete; }
51bool Kernel::endOfSimulationComplete() { return endComplete; }
52
53sc_core::sc_status Kernel::status() { return _status; }
54void Kernel::status(sc_core::sc_status s) { _status = s; }
55
56Kernel::Kernel(Params *params) :
57 SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
58{
59 // Install ourselves as the scheduler's event manager.
60 ::sc_gem5::scheduler.setEventQueue(eventQueue());
61}
62
63void
64Kernel::init()
65{
66 status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
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/module.hh"
34#include "systemc/core/scheduler.hh"
35
36namespace sc_gem5
37{
38
39namespace
40{
41
42bool stopAfterCallbacks = false;
43bool startComplete = false;
44bool endComplete = false;
45
46sc_core::sc_status _status = sc_core::SC_ELABORATION;
47
48} // anonymous namespace
49
50bool Kernel::startOfSimulationComplete() { return startComplete; }
51bool Kernel::endOfSimulationComplete() { return endComplete; }
52
53sc_core::sc_status Kernel::status() { return _status; }
54void Kernel::status(sc_core::sc_status s) { _status = s; }
55
56Kernel::Kernel(Params *params) :
57 SimObject(params), t0Event(this, false, EventBase::Default_Pri - 1)
58{
59 // Install ourselves as the scheduler's event manager.
60 ::sc_gem5::scheduler.setEventQueue(eventQueue());
61}
62
63void
64Kernel::init()
65{
66 status(::sc_core::SC_BEFORE_END_OF_ELABORATION);
67 for (auto m: sc_gem5::allModules)
67 for (auto m: sc_gem5::allModules) {
68 callbackModule(m);
68 m->sc_mod()->before_end_of_elaboration();
69 m->sc_mod()->before_end_of_elaboration();
70 }
71 callbackModule(nullptr);
69
70 if (stopAfterCallbacks)
71 stopWork();
72}
73
74void
75Kernel::regStats()
76{
77 status(::sc_core::SC_END_OF_ELABORATION);
78 for (auto m: sc_gem5::allModules)
79 m->sc_mod()->end_of_elaboration();
80
81 if (stopAfterCallbacks)
82 stopWork();
83}
84
85void
86Kernel::startup()
87{
88 status(::sc_core::SC_START_OF_SIMULATION);
89 for (auto m: sc_gem5::allModules)
90 m->sc_mod()->start_of_simulation();
91
92 startComplete = true;
93
94 if (stopAfterCallbacks)
95 stopWork();
96
97 kernel->status(::sc_core::SC_RUNNING);
98
99 schedule(t0Event, curTick());
100 // Run update once before the event queue starts.
101 ::sc_gem5::scheduler.update();
102}
103
104void
105Kernel::stop()
106{
107 if (status() < ::sc_core::SC_RUNNING)
108 stopAfterCallbacks = true;
109 else
110 stopWork();
111}
112
113void
114Kernel::stopWork()
115{
116 status(::sc_core::SC_END_OF_SIMULATION);
117 for (auto m: sc_gem5::allModules)
118 m->sc_mod()->end_of_simulation();
119
120 endComplete = true;
121
122 status(::sc_core::SC_STOPPED);
123
124 if (stopAfterCallbacks)
125 fatal("Simulation called sc_stop during elaboration.\n");
126}
127
128void
129Kernel::t0Handler()
130{
131 // Now that the event queue has started, mark all the processes that
132 // need to be initialized as ready to run.
133 //
134 // This event has greater priority than delta notifications and so will
135 // happen before them, honoring the ordering for the initialization phase
136 // in the spec. The delta phase will happen at normal priority, and then
137 // the event which runs the processes which is at a lower priority.
138 ::sc_gem5::scheduler.prepareForInit();
139
140 status(::sc_core::SC_RUNNING);
141}
142
143Kernel *kernel;
144
145} // namespace sc_gem5
146
147sc_gem5::Kernel *
148SystemC_KernelParams::create()
149{
150 panic_if(sc_gem5::kernel,
151 "Only one systemc kernel object may be defined.\n");
152 sc_gem5::kernel = new sc_gem5::Kernel(this);
153 return sc_gem5::kernel;
154}
72
73 if (stopAfterCallbacks)
74 stopWork();
75}
76
77void
78Kernel::regStats()
79{
80 status(::sc_core::SC_END_OF_ELABORATION);
81 for (auto m: sc_gem5::allModules)
82 m->sc_mod()->end_of_elaboration();
83
84 if (stopAfterCallbacks)
85 stopWork();
86}
87
88void
89Kernel::startup()
90{
91 status(::sc_core::SC_START_OF_SIMULATION);
92 for (auto m: sc_gem5::allModules)
93 m->sc_mod()->start_of_simulation();
94
95 startComplete = true;
96
97 if (stopAfterCallbacks)
98 stopWork();
99
100 kernel->status(::sc_core::SC_RUNNING);
101
102 schedule(t0Event, curTick());
103 // Run update once before the event queue starts.
104 ::sc_gem5::scheduler.update();
105}
106
107void
108Kernel::stop()
109{
110 if (status() < ::sc_core::SC_RUNNING)
111 stopAfterCallbacks = true;
112 else
113 stopWork();
114}
115
116void
117Kernel::stopWork()
118{
119 status(::sc_core::SC_END_OF_SIMULATION);
120 for (auto m: sc_gem5::allModules)
121 m->sc_mod()->end_of_simulation();
122
123 endComplete = true;
124
125 status(::sc_core::SC_STOPPED);
126
127 if (stopAfterCallbacks)
128 fatal("Simulation called sc_stop during elaboration.\n");
129}
130
131void
132Kernel::t0Handler()
133{
134 // Now that the event queue has started, mark all the processes that
135 // need to be initialized as ready to run.
136 //
137 // This event has greater priority than delta notifications and so will
138 // happen before them, honoring the ordering for the initialization phase
139 // in the spec. The delta phase will happen at normal priority, and then
140 // the event which runs the processes which is at a lower priority.
141 ::sc_gem5::scheduler.prepareForInit();
142
143 status(::sc_core::SC_RUNNING);
144}
145
146Kernel *kernel;
147
148} // namespace sc_gem5
149
150sc_gem5::Kernel *
151SystemC_KernelParams::create()
152{
153 panic_if(sc_gem5::kernel,
154 "Only one systemc kernel object may be defined.\n");
155 sc_gem5::kernel = new sc_gem5::Kernel(this);
156 return sc_gem5::kernel;
157}