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