1 // -*- mode: C++; c-file-style: "cc-mode" -*-
2 //=============================================================================
3 //
4 // Code available from: https://verilator.org
5 //
6 // Copyright 2001-2021 by Wilson Snyder. This program is free software; you
7 // can redistribute it and/or modify it under the terms of either the GNU
8 // Lesser General Public License Version 3 or the Perl Artistic License
9 // Version 2.0.
10 // SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
11 //
12 //=============================================================================
13 ///
14 /// \file
15 /// \brief Verilated C++ tracing in FST format implementation code
16 ///
17 /// This file must be compiled and linked against all Verilated objects
18 /// that use --trace-fst.
19 ///
20 /// Use "verilator --trace-fst" to add this to the Makefile for the linker.
21 ///
22 //=============================================================================
23
24 // clang-format off
25
26 #define __STDC_LIMIT_MACROS // UINT64_MAX
27 #include "verilated.h"
28 #include "verilated_fst_c.h"
29
30 // GTKWave configuration
31 #ifdef VL_TRACE_FST_WRITER_THREAD
32 # define HAVE_LIBPTHREAD
33 # define FST_WRITER_PARALLEL
34 #endif
35
36 // Include the GTKWave implementation directly
37 #define FST_CONFIG_INCLUDE "fst_config.h"
38 #include "gtkwave/fastlz.c"
39 #include "gtkwave/fstapi.c"
40 #include "gtkwave/lz4.c"
41
42 #include <algorithm>
43 #include <iterator>
44 #include <sstream>
45
46 #if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
47 # include <io.h>
48 #else
49 # include <unistd.h>
50 #endif
51
52 // clang-format on
53
54 //=============================================================================
55 // Check that vltscope_t matches fstScopeType
56
57 static_assert(static_cast<int>(FST_ST_VCD_MODULE) == static_cast<int>(VLT_TRACE_SCOPE_MODULE),
58 "VLT_TRACE_SCOPE_MODULE mismatches");
59 static_assert(static_cast<int>(FST_ST_VCD_TASK) == static_cast<int>(VLT_TRACE_SCOPE_TASK),
60 "VLT_TRACE_SCOPE_TASK mismatches");
61 static_assert(static_cast<int>(FST_ST_VCD_FUNCTION) == static_cast<int>(VLT_TRACE_SCOPE_FUNCTION),
62 "VLT_TRACE_SCOPE_FUNCTION mismatches");
63 static_assert(static_cast<int>(FST_ST_VCD_BEGIN) == static_cast<int>(VLT_TRACE_SCOPE_BEGIN),
64 "VLT_TRACE_SCOPE_BEGIN mismatches");
65 static_assert(static_cast<int>(FST_ST_VCD_FORK) == static_cast<int>(VLT_TRACE_SCOPE_FORK),
66 "VLT_TRACE_SCOPE_FORK mismatches");
67 static_assert(static_cast<int>(FST_ST_VCD_GENERATE) == static_cast<int>(VLT_TRACE_SCOPE_GENERATE),
68 "VLT_TRACE_SCOPE_GENERATE mismatches");
69 static_assert(static_cast<int>(FST_ST_VCD_STRUCT) == static_cast<int>(VLT_TRACE_SCOPE_STRUCT),
70 "VLT_TRACE_SCOPE_STRUCT mismatches");
71 static_assert(static_cast<int>(FST_ST_VCD_UNION) == static_cast<int>(VLT_TRACE_SCOPE_UNION),
72 "VLT_TRACE_SCOPE_UNION mismatches");
73 static_assert(static_cast<int>(FST_ST_VCD_CLASS) == static_cast<int>(VLT_TRACE_SCOPE_CLASS),
74 "VLT_TRACE_SCOPE_CLASS mismatches");
75 static_assert(static_cast<int>(FST_ST_VCD_INTERFACE)
76 == static_cast<int>(VLT_TRACE_SCOPE_INTERFACE),
77 "VLT_TRACE_SCOPE_INTERFACE mismatches");
78 static_assert(static_cast<int>(FST_ST_VCD_PACKAGE) == static_cast<int>(VLT_TRACE_SCOPE_PACKAGE),
79 "VLT_TRACE_SCOPE_PACKAGE mismatches");
80 static_assert(static_cast<int>(FST_ST_VCD_PROGRAM) == static_cast<int>(VLT_TRACE_SCOPE_PROGRAM),
81 "VLT_TRACE_SCOPE_PROGRAM mismatches");
82
83 //=============================================================================
84 // Specialization of the generics for this trace format
85
86 #define VL_DERIVED_T VerilatedFst
87 #include "verilated_trace_imp.cpp"
88 #undef VL_DERIVED_T
89
90 //=============================================================================
91 // VerilatedFst
92
VerilatedFst(void * fst)93 VerilatedFst::VerilatedFst(void* fst)
94 : m_fst{fst} {}
95
~VerilatedFst()96 VerilatedFst::~VerilatedFst() {
97 if (m_fst) fstWriterClose(m_fst);
98 if (m_symbolp) VL_DO_CLEAR(delete[] m_symbolp, m_symbolp = nullptr);
99 if (m_strbuf) VL_DO_CLEAR(delete[] m_strbuf, m_strbuf = nullptr);
100 }
101
open(const char * filename)102 void VerilatedFst::open(const char* filename) VL_MT_SAFE_EXCLUDES(m_mutex) {
103 const VerilatedLockGuard lock{m_mutex};
104 m_fst = fstWriterCreate(filename, 1);
105 fstWriterSetPackType(m_fst, FST_WR_PT_LZ4);
106 fstWriterSetTimescaleFromString(m_fst, timeResStr().c_str()); // lintok-begin-on-ref
107 #ifdef VL_TRACE_FST_WRITER_THREAD
108 fstWriterSetParallelMode(m_fst, 1);
109 #endif
110 fullDump(true); // First dump must be full for fst
111
112 m_curScope.clear();
113
114 VerilatedTrace<VerilatedFst>::traceInit();
115
116 // Clear the scope stack
117 auto it = m_curScope.begin();
118 while (it != m_curScope.end()) {
119 fstWriterSetUpscope(m_fst);
120 it = m_curScope.erase(it);
121 }
122
123 // convert m_code2symbol into an array for fast lookup
124 if (!m_symbolp) {
125 m_symbolp = new fstHandle[nextCode()];
126 for (const auto& i : m_code2symbol) m_symbolp[i.first] = i.second;
127 }
128 m_code2symbol.clear();
129
130 // Allocate string buffer for arrays
131 if (!m_strbuf) m_strbuf = new char[maxBits() + 32];
132 }
133
close()134 void VerilatedFst::close() VL_MT_SAFE_EXCLUDES(m_mutex) {
135 const VerilatedLockGuard lock{m_mutex};
136 VerilatedTrace<VerilatedFst>::closeBase();
137 fstWriterClose(m_fst);
138 m_fst = nullptr;
139 }
140
flush()141 void VerilatedFst::flush() VL_MT_SAFE_EXCLUDES(m_mutex) {
142 const VerilatedLockGuard lock{m_mutex};
143 VerilatedTrace<VerilatedFst>::flushBase();
144 fstWriterFlushContext(m_fst);
145 }
146
emitTimeChange(vluint64_t timeui)147 void VerilatedFst::emitTimeChange(vluint64_t timeui) { fstWriterEmitTimeChange(m_fst, timeui); }
148
149 //=============================================================================
150 // Decl
151
declDTypeEnum(int dtypenum,const char * name,vluint32_t elements,unsigned int minValbits,const char ** itemNamesp,const char ** itemValuesp)152 void VerilatedFst::declDTypeEnum(int dtypenum, const char* name, vluint32_t elements,
153 unsigned int minValbits, const char** itemNamesp,
154 const char** itemValuesp) {
155 const fstEnumHandle enumNum
156 = fstWriterCreateEnumTable(m_fst, name, elements, minValbits, itemNamesp, itemValuesp);
157 m_local2fstdtype[dtypenum] = enumNum;
158 }
159
declare(vluint32_t code,const char * name,int dtypenum,fstVarDir vardir,fstVarType vartype,bool array,int arraynum,int msb,int lsb)160 void VerilatedFst::declare(vluint32_t code, const char* name, int dtypenum, fstVarDir vardir,
161 fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
162 const int bits = ((msb > lsb) ? (msb - lsb) : (lsb - msb)) + 1;
163
164 VerilatedTrace<VerilatedFst>::declCode(code, bits, false);
165
166 std::istringstream nameiss{name};
167 std::istream_iterator<std::string> beg(nameiss);
168 std::istream_iterator<std::string> end;
169 std::list<std::string> tokens(beg, end); // Split name
170 std::string symbol_name{tokens.back()};
171 tokens.pop_back(); // Remove symbol name from hierarchy
172 tokens.insert(tokens.begin(), moduleName()); // Add current module to the hierarchy
173 std::string tmpModName;
174
175 // Find point where current and new scope diverge
176 auto cur_it = m_curScope.begin();
177 auto new_it = tokens.begin();
178 while (cur_it != m_curScope.end() && new_it != tokens.end()) {
179 if (*cur_it != *new_it) break;
180 ++cur_it;
181 ++new_it;
182 }
183
184 // Go back to the common point
185 while (cur_it != m_curScope.end()) {
186 fstWriterSetUpscope(m_fst);
187 cur_it = m_curScope.erase(cur_it);
188 }
189
190 // Follow the hierarchy of the new variable from the common scope point
191 while (new_it != tokens.end()) {
192 if ((new_it->back() & 0x80)) {
193 tmpModName = *new_it;
194 tmpModName.pop_back();
195 // If the scope ends with a non-ascii character, it will be 0x80 + fstScopeType
196 fstWriterSetScope(m_fst, static_cast<fstScopeType>(new_it->back() & 0x7f),
197 tmpModName.c_str(), nullptr);
198 } else
199 fstWriterSetScope(m_fst, FST_ST_VCD_SCOPE, new_it->c_str(), nullptr);
200
201 m_curScope.push_back(*new_it);
202 new_it = tokens.erase(new_it);
203 }
204
205 std::stringstream name_ss;
206 name_ss << symbol_name;
207 if (array) name_ss << "[" << arraynum << "]";
208 std::string name_str = name_ss.str();
209
210 if (dtypenum > 0) {
211 const fstEnumHandle enumNum = m_local2fstdtype[dtypenum];
212 fstWriterEmitEnumTableRef(m_fst, enumNum);
213 }
214
215 const auto it = vlstd::as_const(m_code2symbol).find(code);
216 if (it == m_code2symbol.end()) { // New
217 m_code2symbol[code]
218 = fstWriterCreateVar(m_fst, vartype, vardir, bits, name_str.c_str(), 0);
219 } else { // Alias
220 fstWriterCreateVar(m_fst, vartype, vardir, bits, name_str.c_str(), it->second);
221 }
222 }
223
declBit(vluint32_t code,const char * name,int dtypenum,fstVarDir vardir,fstVarType vartype,bool array,int arraynum)224 void VerilatedFst::declBit(vluint32_t code, const char* name, int dtypenum, fstVarDir vardir,
225 fstVarType vartype, bool array, int arraynum) {
226 declare(code, name, dtypenum, vardir, vartype, array, arraynum, 0, 0);
227 }
declBus(vluint32_t code,const char * name,int dtypenum,fstVarDir vardir,fstVarType vartype,bool array,int arraynum,int msb,int lsb)228 void VerilatedFst::declBus(vluint32_t code, const char* name, int dtypenum, fstVarDir vardir,
229 fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
230 declare(code, name, dtypenum, vardir, vartype, array, arraynum, msb, lsb);
231 }
declQuad(vluint32_t code,const char * name,int dtypenum,fstVarDir vardir,fstVarType vartype,bool array,int arraynum,int msb,int lsb)232 void VerilatedFst::declQuad(vluint32_t code, const char* name, int dtypenum, fstVarDir vardir,
233 fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
234 declare(code, name, dtypenum, vardir, vartype, array, arraynum, msb, lsb);
235 }
declArray(vluint32_t code,const char * name,int dtypenum,fstVarDir vardir,fstVarType vartype,bool array,int arraynum,int msb,int lsb)236 void VerilatedFst::declArray(vluint32_t code, const char* name, int dtypenum, fstVarDir vardir,
237 fstVarType vartype, bool array, int arraynum, int msb, int lsb) {
238 declare(code, name, dtypenum, vardir, vartype, array, arraynum, msb, lsb);
239 }
declDouble(vluint32_t code,const char * name,int dtypenum,fstVarDir vardir,fstVarType vartype,bool array,int arraynum)240 void VerilatedFst::declDouble(vluint32_t code, const char* name, int dtypenum, fstVarDir vardir,
241 fstVarType vartype, bool array, int arraynum) {
242 declare(code, name, dtypenum, vardir, vartype, array, arraynum, 63, 0);
243 }
244
245 // Note: emit* are only ever called from one place (full* in
246 // verilated_trace_imp.cpp, which is included in this file at the top),
247 // so always inline them.
248
249 VL_ATTR_ALWINLINE
emitBit(vluint32_t code,CData newval)250 void VerilatedFst::emitBit(vluint32_t code, CData newval) {
251 fstWriterEmitValueChange(m_fst, m_symbolp[code], newval ? "1" : "0");
252 }
253
254 VL_ATTR_ALWINLINE
emitCData(vluint32_t code,CData newval,int bits)255 void VerilatedFst::emitCData(vluint32_t code, CData newval, int bits) {
256 char buf[VL_BYTESIZE];
257 cvtCDataToStr(buf, newval << (VL_BYTESIZE - bits));
258 fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
259 }
260
261 VL_ATTR_ALWINLINE
emitSData(vluint32_t code,SData newval,int bits)262 void VerilatedFst::emitSData(vluint32_t code, SData newval, int bits) {
263 char buf[VL_SHORTSIZE];
264 cvtSDataToStr(buf, newval << (VL_SHORTSIZE - bits));
265 fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
266 }
267
268 VL_ATTR_ALWINLINE
emitIData(vluint32_t code,IData newval,int bits)269 void VerilatedFst::emitIData(vluint32_t code, IData newval, int bits) {
270 char buf[VL_IDATASIZE];
271 cvtIDataToStr(buf, newval << (VL_IDATASIZE - bits));
272 fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
273 }
274
275 VL_ATTR_ALWINLINE
emitQData(vluint32_t code,QData newval,int bits)276 void VerilatedFst::emitQData(vluint32_t code, QData newval, int bits) {
277 char buf[VL_QUADSIZE];
278 cvtQDataToStr(buf, newval << (VL_QUADSIZE - bits));
279 fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
280 }
281
282 VL_ATTR_ALWINLINE
emitWData(vluint32_t code,const WData * newvalp,int bits)283 void VerilatedFst::emitWData(vluint32_t code, const WData* newvalp, int bits) {
284 int words = VL_WORDS_I(bits);
285 char* wp = m_strbuf;
286 // Convert the most significant word
287 const int bitsInMSW = VL_BITBIT_E(bits) ? VL_BITBIT_E(bits) : VL_EDATASIZE;
288 cvtEDataToStr(wp, newvalp[--words] << (VL_EDATASIZE - bitsInMSW));
289 wp += bitsInMSW;
290 // Convert the remaining words
291 while (words > 0) {
292 cvtEDataToStr(wp, newvalp[--words]);
293 wp += VL_EDATASIZE;
294 }
295 fstWriterEmitValueChange(m_fst, m_symbolp[code], m_strbuf);
296 }
297
298 VL_ATTR_ALWINLINE
emitDouble(vluint32_t code,double newval)299 void VerilatedFst::emitDouble(vluint32_t code, double newval) {
300 fstWriterEmitValueChange(m_fst, m_symbolp[code], &newval);
301 }
302