cxxrtl: only write VCD values that were actually updated.
[yosys.git] / backends / cxxrtl / cxxrtl_vcd.h
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2020 whitequark <whitequark@whitequark.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 */
18
19 #ifndef CXXRTL_VCD_H
20 #define CXXRTL_VCD_H
21
22 #include <backends/cxxrtl/cxxrtl.h>
23
24 namespace cxxrtl {
25
26 class vcd_writer {
27 struct variable {
28 size_t ident;
29 size_t width;
30 chunk_t *curr;
31 size_t prev_off;
32 };
33
34 std::vector<std::string> current_scope;
35 std::vector<variable> variables;
36 std::vector<chunk_t> cache;
37 bool streaming = false;
38
39 void emit_timescale(unsigned number, const std::string &unit) {
40 assert(!streaming);
41 assert(number == 1 || number == 10 || number == 100);
42 assert(unit == "s" || unit == "ms" || unit == "us" ||
43 unit == "ns" || unit == "ps" || unit == "fs");
44 buffer += "$timescale " + std::to_string(number) + " " + unit + " $end\n";
45 }
46
47 void emit_scope(const std::vector<std::string> &scope) {
48 assert(!streaming);
49 while (current_scope.size() > scope.size() ||
50 (current_scope.size() > 0 &&
51 current_scope[current_scope.size() - 1] != scope[current_scope.size() - 1])) {
52 buffer += "$upscope $end\n";
53 current_scope.pop_back();
54 }
55 while (current_scope.size() < scope.size()) {
56 buffer += "$scope module " + scope[current_scope.size()] + " $end\n";
57 current_scope.push_back(scope[current_scope.size()]);
58 }
59 }
60
61 void emit_ident(size_t ident) {
62 do {
63 buffer += '!' + ident % 94; // "base94"
64 ident /= 94;
65 } while (ident != 0);
66 }
67
68 void emit_var(const variable &var, const std::string &type, const std::string &name) {
69 assert(!streaming);
70 buffer += "$var " + type + " " + std::to_string(var.width) + " ";
71 emit_ident(var.ident);
72 buffer += " " + name + " $end\n";
73 }
74
75 void emit_enddefinitions() {
76 assert(!streaming);
77 buffer += "$enddefinitions $end\n";
78 streaming = true;
79 }
80
81 void emit_time(uint64_t timestamp) {
82 assert(streaming);
83 buffer += "#" + std::to_string(timestamp) + "\n";
84 }
85
86 void emit_scalar(const variable &var) {
87 assert(streaming);
88 assert(var.width == 1);
89 buffer += (*var.curr ? '1' : '0');
90 emit_ident(var.ident);
91 buffer += '\n';
92 }
93
94 void emit_vector(const variable &var) {
95 assert(streaming);
96 buffer += 'b';
97 for (size_t bit = var.width - 1; bit != (size_t)-1; bit--) {
98 bool bit_curr = var.curr[bit / (8 * sizeof(chunk_t))] & (1 << (bit % (8 * sizeof(chunk_t))));
99 buffer += (bit_curr ? '1' : '0');
100 }
101 buffer += ' ';
102 emit_ident(var.ident);
103 buffer += '\n';
104 }
105
106 void append_variable(size_t width, chunk_t *curr) {
107 const size_t chunks = (width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
108 variables.emplace_back(variable { variables.size(), width, curr, cache.size() });
109 cache.insert(cache.end(), &curr[0], &curr[chunks]);
110 }
111
112 bool test_variable(const variable &var) {
113 const size_t chunks = (var.width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
114 if (std::equal(&var.curr[0], &var.curr[chunks], &cache[var.prev_off])) {
115 return false;
116 } else {
117 std::copy(&var.curr[0], &var.curr[chunks], &cache[var.prev_off]);
118 return true;
119 }
120 }
121
122 static std::vector<std::string> split_hierarchy(const std::string &hier_name) {
123 std::vector<std::string> hierarchy;
124 size_t prev = 0;
125 while (true) {
126 size_t curr = hier_name.find_first_of(' ', prev + 1);
127 if (curr > hier_name.size())
128 curr = hier_name.size();
129 if (curr > prev + 1)
130 hierarchy.push_back(hier_name.substr(prev, curr - prev));
131 if (curr == hier_name.size())
132 break;
133 prev = curr + 1;
134 }
135 return hierarchy;
136 }
137
138 public:
139 std::string buffer;
140
141 void timescale(unsigned number, const std::string &unit) {
142 emit_timescale(number, unit);
143 }
144
145 void add(const std::string &hier_name, const debug_item &item) {
146 std::vector<std::string> scope = split_hierarchy(hier_name);
147 std::string name = scope.back();
148 scope.pop_back();
149
150 emit_scope(scope);
151 switch (item.type) {
152 // Not the best naming but oh well...
153 case debug_item::VALUE:
154 append_variable(item.width, item.curr);
155 emit_var(variables.back(), "wire", name);
156 break;
157 case debug_item::WIRE:
158 append_variable(item.width, item.curr);
159 emit_var(variables.back(), "reg", name);
160 break;
161 case debug_item::MEMORY: {
162 const size_t stride = (item.width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
163 for (size_t index = 0; index < item.depth; index++) {
164 chunk_t *nth_curr = &item.curr[stride * index];
165 std::string nth_name = name + '[' + std::to_string(index) + ']';
166 append_variable(item.width, nth_curr);
167 emit_var(variables.back(), "reg", nth_name);
168 }
169 break;
170 }
171 }
172 }
173
174 template<class Filter>
175 void add(const debug_items &items, const Filter &filter) {
176 // `debug_items` is a map, so the items are already sorted in an order optimal for emitting
177 // VCD scope sections.
178 for (auto &it : items)
179 if (filter(it.first, it.second))
180 add(it.first, it.second);
181 }
182
183 void add(const debug_items &items) {
184 this->template add(items, [](const std::string &, const debug_item &) {
185 return true;
186 });
187 }
188
189 void add_without_memories(const debug_items &items) {
190 this->template add(items, [](const std::string &, const debug_item &item) {
191 return item.type == debug_item::VALUE || item.type == debug_item::WIRE;
192 });
193 }
194
195 void sample(uint64_t timestamp) {
196 bool first_sample = !streaming;
197 if (first_sample) {
198 emit_scope({});
199 emit_enddefinitions();
200 }
201 emit_time(timestamp);
202 for (auto var : variables)
203 if (test_variable(var) || first_sample) {
204 if (var.width == 1)
205 emit_scalar(var);
206 else
207 emit_vector(var);
208 }
209 }
210 };
211
212 }
213
214 #endif