sc_clock.cc (13297:bd7203a206f6) sc_clock.cc (13324:c8b709468e61)
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 "base/logging.hh"
31#include "base/types.hh"
32#include "sim/core.hh"
33#include "sim/eventq.hh"
34#include "systemc/core/kernel.hh"
35#include "systemc/core/process_types.hh"
36#include "systemc/core/sched_event.hh"
37#include "systemc/core/scheduler.hh"
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 "base/logging.hh"
31#include "base/types.hh"
32#include "sim/core.hh"
33#include "sim/eventq.hh"
34#include "systemc/core/kernel.hh"
35#include "systemc/core/process_types.hh"
36#include "systemc/core/sched_event.hh"
37#include "systemc/core/scheduler.hh"
38#include "systemc/ext/channel/messages.hh"
38#include "systemc/ext/channel/sc_clock.hh"
39#include "systemc/ext/core/sc_main.hh"
40#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
41#include "systemc/ext/utils/sc_report_handler.hh"
42
43namespace sc_gem5
44{
45
46class ClockTick : public ScEvent
47{
48 private:
49 ::sc_core::sc_clock *clock;
50 ::sc_core::sc_time _period;
51 std::string name;
52 Process *p;
53 ProcessMemberFuncWrapper<::sc_core::sc_clock> funcWrapper;
54
55 public:
56 ClockTick(::sc_core::sc_clock *clock, bool to,
57 ::sc_core::sc_time _period) :
58 ScEvent([this]() { tick(); }),
59 clock(clock), _period(_period), name(clock->basename()), p(nullptr),
60 funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp :
61 &::sc_core::sc_clock::tickDown)
62 {
63 name += std::string(to ? "_posedge_action" : "_negedge_action");
64 name = ::sc_core::sc_gen_unique_name(name.c_str());
65 }
66
67 void
68 createProcess()
69 {
70 p = new Method(name.c_str(), &funcWrapper, true);
71 p->dontInitialize(true);
72 scheduler.reg(p);
73 }
74
75 ~ClockTick()
76 {
77 if (scheduled())
78 scheduler.deschedule(this);
79 if (p)
80 p->popListNode();
81 }
82
83 void
84 tick()
85 {
86 scheduler.schedule(this, _period);
87 p->ready();
88 }
89};
90
91};
92
93namespace sc_core
94{
95
96sc_clock::sc_clock() :
97 sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS),
98 0.5, SC_ZERO_TIME, true)
99{}
100
101sc_clock::sc_clock(const char *name) :
102 sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true)
103{}
104
105sc_clock::sc_clock(const char *name, const sc_time &period,
106 double duty_cycle, const sc_time &start_time,
107 bool posedge_first) :
108 sc_interface(), sc_signal<bool>(name, posedge_first ? false : true),
109 _period(period), _dutyCycle(duty_cycle), _startTime(start_time),
110 _posedgeFirst(posedge_first)
111{
112 if (period == SC_ZERO_TIME) {
113 std::string msg =
114 "increase the period: clock '" +
115 std::string(name) + "'";
39#include "systemc/ext/channel/sc_clock.hh"
40#include "systemc/ext/core/sc_main.hh"
41#include "systemc/ext/core/sc_module.hh" // for sc_gen_unique_name
42#include "systemc/ext/utils/sc_report_handler.hh"
43
44namespace sc_gem5
45{
46
47class ClockTick : public ScEvent
48{
49 private:
50 ::sc_core::sc_clock *clock;
51 ::sc_core::sc_time _period;
52 std::string name;
53 Process *p;
54 ProcessMemberFuncWrapper<::sc_core::sc_clock> funcWrapper;
55
56 public:
57 ClockTick(::sc_core::sc_clock *clock, bool to,
58 ::sc_core::sc_time _period) :
59 ScEvent([this]() { tick(); }),
60 clock(clock), _period(_period), name(clock->basename()), p(nullptr),
61 funcWrapper(clock, to ? &::sc_core::sc_clock::tickUp :
62 &::sc_core::sc_clock::tickDown)
63 {
64 name += std::string(to ? "_posedge_action" : "_negedge_action");
65 name = ::sc_core::sc_gen_unique_name(name.c_str());
66 }
67
68 void
69 createProcess()
70 {
71 p = new Method(name.c_str(), &funcWrapper, true);
72 p->dontInitialize(true);
73 scheduler.reg(p);
74 }
75
76 ~ClockTick()
77 {
78 if (scheduled())
79 scheduler.deschedule(this);
80 if (p)
81 p->popListNode();
82 }
83
84 void
85 tick()
86 {
87 scheduler.schedule(this, _period);
88 p->ready();
89 }
90};
91
92};
93
94namespace sc_core
95{
96
97sc_clock::sc_clock() :
98 sc_clock(sc_gen_unique_name("clock"), sc_time(1.0, SC_NS),
99 0.5, SC_ZERO_TIME, true)
100{}
101
102sc_clock::sc_clock(const char *name) :
103 sc_clock(name, sc_time(1.0, SC_NS), 0.5, SC_ZERO_TIME, true)
104{}
105
106sc_clock::sc_clock(const char *name, const sc_time &period,
107 double duty_cycle, const sc_time &start_time,
108 bool posedge_first) :
109 sc_interface(), sc_signal<bool>(name, posedge_first ? false : true),
110 _period(period), _dutyCycle(duty_cycle), _startTime(start_time),
111 _posedgeFirst(posedge_first)
112{
113 if (period == SC_ZERO_TIME) {
114 std::string msg =
115 "increase the period: clock '" +
116 std::string(name) + "'";
116 SC_REPORT_ERROR("(E101) sc_clock period is zero", msg.c_str());
117 SC_REPORT_ERROR(SC_ID_CLOCK_PERIOD_ZERO_, msg.c_str());
117 }
118
119 if (duty_cycle * period == SC_ZERO_TIME) {
120 std::string msg =
121 "increase the period or increase the duty cycle: clock '" +
122 std::string(name) + "'";
118 }
119
120 if (duty_cycle * period == SC_ZERO_TIME) {
121 std::string msg =
122 "increase the period or increase the duty cycle: clock '" +
123 std::string(name) + "'";
123 SC_REPORT_ERROR("(E102) sc_clock high time is zero", msg.c_str());
124 SC_REPORT_ERROR(SC_ID_CLOCK_HIGH_TIME_ZERO_, msg.c_str());
124 }
125
126 if (duty_cycle * period == period) {
127 std::string msg =
128 "increase the period or decrease the duty cycle: clock '" +
129 std::string(name) + "'";
125 }
126
127 if (duty_cycle * period == period) {
128 std::string msg =
129 "increase the period or decrease the duty cycle: clock '" +
130 std::string(name) + "'";
130 SC_REPORT_ERROR("(E103) sc_clock low time is zero", msg.c_str());
131 SC_REPORT_ERROR(SC_ID_CLOCK_LOW_TIME_ZERO_, msg.c_str());
131 }
132
133 _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period);
134 _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period);
135}
136
137sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
138 double duty_cycle) :
139 sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME,
140 true)
141{}
142
143sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
144 double duty_cycle, double start_time_v,
145 sc_time_unit start_time_tu, bool posedge_first) :
146 sc_clock(name, sc_time(period_v, period_tu), duty_cycle,
147 sc_time(start_time_v, start_time_tu), posedge_first)
148{}
149
150sc_clock::sc_clock(const char *name, double period, double duty_cycle,
151 double start_time, bool posedge_first) :
152 sc_clock(name, sc_time(period, true), duty_cycle,
153 sc_time(start_time, true), posedge_first)
154{}
155
156sc_clock::~sc_clock()
157{
158 if (_gem5UpEdge->scheduled())
159 ::sc_gem5::scheduler.deschedule(_gem5UpEdge);
160 if (_gem5DownEdge->scheduled())
161 ::sc_gem5::scheduler.deschedule(_gem5DownEdge);
162 delete _gem5UpEdge;
163 delete _gem5DownEdge;
164}
165
166void
167sc_clock::write(const bool &)
168{
169 panic("write() called on sc_clock.");
170}
171
172const sc_time &sc_clock::period() const { return _period; }
173double sc_clock::duty_cycle() const { return _dutyCycle; }
174const sc_time &sc_clock::start_time() const { return _startTime; }
175bool sc_clock::posedge_first() const { return _posedgeFirst; }
176
177const sc_time &
178sc_clock::time_stamp()
179{
180 return sc_time_stamp();
181}
182
183void
184sc_clock::before_end_of_elaboration()
185{
186 _gem5UpEdge->createProcess();
187 _gem5DownEdge->createProcess();
188 if (_posedgeFirst) {
189 ::sc_gem5::scheduler.schedule(_gem5UpEdge, _startTime);
190 ::sc_gem5::scheduler.schedule(_gem5DownEdge,
191 _startTime + _period * _dutyCycle);
192 } else {
193 ::sc_gem5::scheduler.schedule(_gem5DownEdge, _startTime);
194 ::sc_gem5::scheduler.schedule(_gem5UpEdge,
195 _startTime + _period * (1.0 - _dutyCycle));
196 }
197}
198
199} // namespace sc_core
132 }
133
134 _gem5UpEdge = new ::sc_gem5::ClockTick(this, true, period);
135 _gem5DownEdge = new ::sc_gem5::ClockTick(this, false, period);
136}
137
138sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
139 double duty_cycle) :
140 sc_clock(name, sc_time(period_v, period_tu), duty_cycle, SC_ZERO_TIME,
141 true)
142{}
143
144sc_clock::sc_clock(const char *name, double period_v, sc_time_unit period_tu,
145 double duty_cycle, double start_time_v,
146 sc_time_unit start_time_tu, bool posedge_first) :
147 sc_clock(name, sc_time(period_v, period_tu), duty_cycle,
148 sc_time(start_time_v, start_time_tu), posedge_first)
149{}
150
151sc_clock::sc_clock(const char *name, double period, double duty_cycle,
152 double start_time, bool posedge_first) :
153 sc_clock(name, sc_time(period, true), duty_cycle,
154 sc_time(start_time, true), posedge_first)
155{}
156
157sc_clock::~sc_clock()
158{
159 if (_gem5UpEdge->scheduled())
160 ::sc_gem5::scheduler.deschedule(_gem5UpEdge);
161 if (_gem5DownEdge->scheduled())
162 ::sc_gem5::scheduler.deschedule(_gem5DownEdge);
163 delete _gem5UpEdge;
164 delete _gem5DownEdge;
165}
166
167void
168sc_clock::write(const bool &)
169{
170 panic("write() called on sc_clock.");
171}
172
173const sc_time &sc_clock::period() const { return _period; }
174double sc_clock::duty_cycle() const { return _dutyCycle; }
175const sc_time &sc_clock::start_time() const { return _startTime; }
176bool sc_clock::posedge_first() const { return _posedgeFirst; }
177
178const sc_time &
179sc_clock::time_stamp()
180{
181 return sc_time_stamp();
182}
183
184void
185sc_clock::before_end_of_elaboration()
186{
187 _gem5UpEdge->createProcess();
188 _gem5DownEdge->createProcess();
189 if (_posedgeFirst) {
190 ::sc_gem5::scheduler.schedule(_gem5UpEdge, _startTime);
191 ::sc_gem5::scheduler.schedule(_gem5DownEdge,
192 _startTime + _period * _dutyCycle);
193 } else {
194 ::sc_gem5::scheduler.schedule(_gem5DownEdge, _startTime);
195 ::sc_gem5::scheduler.schedule(_gem5UpEdge,
196 _startTime + _period * (1.0 - _dutyCycle));
197 }
198}
199
200} // namespace sc_core