scc 2025.09
SystemC components library
vcd_pull_trace.cpp
1/*******************************************************************************
2 * Copyright 2021 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17#include "vcd_pull_trace.hh"
18#include "trace/vcd_trace.hh"
19#include "utilities.h"
20
21#include <cmath>
22#include <cstdio>
23#include <cstdlib>
24#include <cstring>
25#include <deque>
26#include <map>
27#include <sstream>
28#include <string>
29#include <unordered_map>
30#include <vector>
31
32#define FPRINT(FP, FMTSTR) \
33 { \
34 auto buf = fmt::format(FMTSTR); \
35 std::fwrite(buf.c_str(), 1, buf.size(), FP); \
36 }
37#define FPRINTF(FP, FMTSTR, ...) \
38 { \
39 auto buf = fmt::format(FMTSTR, __VA_ARGS__); \
40 std::fwrite(buf.c_str(), 1, buf.size(), FP); \
41 }
42
43namespace scc {
44/*******************************************************************************************************
45 *
46 *******************************************************************************************************/
47vcd_pull_trace_file::vcd_pull_trace_file(const char* name, std::function<bool()>& enable)
48: name(name)
49, check_enabled(enable) {
50 vcd_out = fopen(fmt::format("{}.vcd", name).c_str(), "w");
51
52#if SC_VERSION_MAJOR < 3
53#if defined(WITH_SC_TRACING_PHASE_CALLBACKS)
54 // remove from hierarchy
55 sc_object::detach();
56 // register regular (non-delta) callbacks
57 sc_object::register_simulation_phase_callback(SC_BEFORE_TIMESTEP);
58#else // explicitly register with simcontext
59 sc_core::sc_get_curr_simcontext()->add_trace_file(this);
60#endif
61#endif
62}
63
64vcd_pull_trace_file::~vcd_pull_trace_file() {
65 if(vcd_out) {
66 FPRINTF(vcd_out, "#{}\n", sc_core::sc_time_stamp() / 1_ps);
67 fclose(vcd_out);
68 }
69 for(auto t : all_traces)
70 delete t.trc;
71}
72
73template <typename T, typename OT = T> bool changed(trace::vcd_trace* trace) {
74 if(reinterpret_cast<trace::vcd_trace_t<T, OT>*>(trace)->changed()) {
75 reinterpret_cast<trace::vcd_trace_t<T, OT>*>(trace)->update();
76 return true;
77 } else
78 return false;
79}
80#define DECL_TRACE_METHOD_A(tp) \
81 void vcd_pull_trace_file::trace(const tp& object, const std::string& name) { \
82 all_traces.emplace_back(&changed<tp>, new trace::vcd_trace_t<tp>(object, name)); \
83 }
84#define DECL_TRACE_METHOD_B(tp) \
85 void vcd_pull_trace_file::trace(const tp& object, const std::string& name, int width) { \
86 all_traces.emplace_back(&changed<tp>, new trace::vcd_trace_t<tp>(object, name)); \
87 }
88#define DECL_TRACE_METHOD_C(tp, tpo) \
89 void vcd_pull_trace_file::trace(const tp& object, const std::string& name) { \
90 all_traces.emplace_back(&changed<tp, tpo>, new trace::vcd_trace_t<tp, tpo>(object, name)); \
91 }
92
93#if(SYSTEMC_VERSION >= 20171012) || defined(NCSC)
94void vcd_pull_trace_file::trace(const sc_core::sc_event& object, const std::string& name) {}
95// void vcd_pull_trace_file::trace(const sc_core::sc_time& object, const std::string& name){}
96DECL_TRACE_METHOD_A(sc_core::sc_time)
97#endif
98DECL_TRACE_METHOD_A(bool)
99DECL_TRACE_METHOD_A(sc_dt::sc_bit)
100DECL_TRACE_METHOD_A(sc_dt::sc_logic)
101
102DECL_TRACE_METHOD_B(unsigned char)
103DECL_TRACE_METHOD_B(unsigned short)
104DECL_TRACE_METHOD_B(unsigned int)
105DECL_TRACE_METHOD_B(unsigned long)
106#ifdef SYSTEMC_64BIT_PATCHES
107DECL_TRACE_METHOD_B(unsigned long long)
108#endif
109DECL_TRACE_METHOD_B(char)
110DECL_TRACE_METHOD_B(short)
111DECL_TRACE_METHOD_B(int)
112DECL_TRACE_METHOD_B(long)
113DECL_TRACE_METHOD_B(sc_dt::int64)
114DECL_TRACE_METHOD_B(sc_dt::uint64)
115
116DECL_TRACE_METHOD_A(float)
117DECL_TRACE_METHOD_A(double)
118DECL_TRACE_METHOD_A(sc_dt::sc_int_base)
119DECL_TRACE_METHOD_A(sc_dt::sc_uint_base)
120DECL_TRACE_METHOD_A(sc_dt::sc_signed)
121DECL_TRACE_METHOD_A(sc_dt::sc_unsigned)
122
123DECL_TRACE_METHOD_A(sc_dt::sc_fxval)
124DECL_TRACE_METHOD_A(sc_dt::sc_fxval_fast)
125DECL_TRACE_METHOD_C(sc_dt::sc_fxnum, sc_dt::sc_fxval)
126DECL_TRACE_METHOD_C(sc_dt::sc_fxnum_fast, sc_dt::sc_fxval_fast)
127
128DECL_TRACE_METHOD_A(sc_dt::sc_bv_base)
129DECL_TRACE_METHOD_A(sc_dt::sc_lv_base)
130#undef DECL_TRACE_METHOD_A
131#undef DECL_TRACE_METHOD_B
132
133void vcd_pull_trace_file::trace(const unsigned int& object, const std::string& name, const char** enum_literals) {
134 all_traces.emplace_back(&changed<unsigned int>, new trace::vcd_trace_enum(object, name, enum_literals));
135}
136
137std::string vcd_pull_trace_file::obtain_name() {
138 const char first_type_used = 'a';
139 const int used_types_count = 'z' - 'a' + 1;
140 int result;
141
142 result = vcd_name_index;
143 char char6 = static_cast<char>(vcd_name_index % used_types_count);
144
145 result = result / used_types_count;
146 char char5 = static_cast<char>(result % used_types_count);
147
148 result = result / used_types_count;
149 char char4 = static_cast<char>(result % used_types_count);
150
151 result = result / used_types_count;
152 char char3 = static_cast<char>(result % used_types_count);
153
154 result = result / used_types_count;
155 char char2 = static_cast<char>(result % used_types_count);
156
157 char buf[20];
158 std::sprintf(buf, "%c%c%c%c%c", char2 + first_type_used, char3 + first_type_used, char4 + first_type_used, char5 + first_type_used,
159 char6 + first_type_used);
160 vcd_name_index++;
161 return std::string(buf);
162}
163
164void vcd_pull_trace_file::write_comment(const std::string& comment) { FPRINTF(vcd_out, "$comment\n{}\n$end\n\n", comment); }
165
166void vcd_pull_trace_file::init() {
167 std::sort(std::begin(all_traces), std::end(all_traces),
168 [](trace_entry const& a, trace_entry const& b) -> bool { return a.trc->name < b.trc->name; });
169 std::unordered_map<uintptr_t, std::string> alias_map;
170
171 trace::vcd_scope_stack<trace::vcd_trace> scope;
172 for(auto& e : all_traces) {
173 auto alias_it = alias_map.find(e.trc->get_hash());
174 e.trc->is_alias = alias_it != std::end(alias_map);
175 e.trc->trc_hndl = e.trc->is_alias ? alias_it->second : obtain_name();
176 if(!e.trc->is_alias)
177 alias_map.insert({e.trc->get_hash(), e.trc->trc_hndl});
178 scope.add_trace(e.trc);
179 }
180 std::copy_if(std::begin(all_traces), std::end(all_traces), std::back_inserter(active_traces),
181 [](trace_entry const& e) { return !e.trc->is_alias; });
182 changed_traces.reserve(active_traces.size());
183 // date:
184 char tbuf[200];
185 time_t long_time;
186 time(&long_time);
187 struct tm* p_tm = localtime(&long_time);
188 strftime(tbuf, 199, "%b %d, %Y %H:%M:%S", p_tm);
189 FPRINTF(vcd_out, "$date\n {}\n$end\n\n", tbuf);
190 // version:
191 FPRINTF(vcd_out, "$version\n {}\n$end\n\n", sc_core::sc_version());
192 // timescale:
193 FPRINTF(vcd_out, "$timescale\n {}\n$end\n\n", (1_ps).to_string());
194 std::stringstream ss;
195 ss << "tracing " << active_traces.size() << " distinct traces out of " << all_traces.size() << " traces";
196 write_comment(ss.str());
197 scope.print(vcd_out);
198}
199
200std::string vcd_pull_trace_file::prune_name(std::string const& orig_name) {
201 static bool warned = false;
202 bool braces_removed = false;
203 std::string hier_name = orig_name;
204 for(unsigned int i = 0; i < hier_name.length(); i++) {
205 if(hier_name[i] == '[') {
206 hier_name[i] = '(';
207 braces_removed = true;
208 } else if(hier_name[i] == ']') {
209 hier_name[i] = ')';
210 braces_removed = true;
211 }
212 }
213
214 if(braces_removed && !warned) {
215 std::stringstream ss;
216 ss << name
217 << ":\n"
218 "\tTraced objects found with name containing [], which may be\n"
219 "\tinterpreted by the waveform viewer in unexpected ways.\n"
220 "\tSo the [] is automatically replaced by ().";
221
222 SC_REPORT_WARNING(sc_core::SC_ID_TRACING_OBJECT_NAME_FILTERED_, ss.str().c_str());
223 }
224 return hier_name;
225}
226
227void vcd_pull_trace_file::cycle(bool delta_cycle) {
228 if(delta_cycle)
229 return;
230 if(!initialized) {
231 init();
232 initialized = true;
233 FPRINT(vcd_out, "$enddefinitions $end\n\n$dumpvars\n");
234 for(auto& e : active_traces) {
235 e.compare_and_update(e.trc);
236 e.trc->record(vcd_out);
237 }
238 FPRINT(vcd_out, "$end\n\n");
239 } else {
240 if(check_enabled && !check_enabled())
241 return;
242 changed_traces.clear();
243 for(auto& e : active_traces) {
244 if(e.compare_and_update(e.trc))
245 changed_traces.push_back(e.trc);
246 }
247 if(changed_traces.size()) {
248 FPRINTF(vcd_out, "#{}\n", sc_core::sc_time_stamp() / 1_ps);
249 for(auto& t : changed_traces)
250 t->record(vcd_out);
251 }
252 }
253}
254
255void vcd_pull_trace_file::set_time_unit(double v, sc_core::sc_time_unit tu) {}
256
257sc_core::sc_trace_file* create_vcd_pull_trace_file(const char* name, std::function<bool()> enable) {
258 return new vcd_pull_trace_file(name, enable);
259}
260
261void close_vcd_pull_trace_file(sc_core::sc_trace_file* tf) {
262 vcd_pull_trace_file* vcd_tf = static_cast<vcd_pull_trace_file*>(tf);
263 delete vcd_tf;
264}
265
266} // namespace scc
SCC TLM utilities.
sc_core::sc_trace_file * create_vcd_pull_trace_file(const char *name, std::function< bool()> enable=std::function< bool()>())
create VCD file which uses pull mechanism
void close_vcd_pull_trace_file(sc_core::sc_trace_file *tf)
close the VCD file