Merge pull request #2140 from whitequark/cxxrtl-aliases
[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 std::map<chunk_t*, size_t> aliases;
38 bool streaming = false;
39
40 void emit_timescale(unsigned number, const std::string &unit) {
41 assert(!streaming);
42 assert(number == 1 || number == 10 || number == 100);
43 assert(unit == "s" || unit == "ms" || unit == "us" ||
44 unit == "ns" || unit == "ps" || unit == "fs");
45 buffer += "$timescale " + std::to_string(number) + " " + unit + " $end\n";
46 }
47
48 void emit_scope(const std::vector<std::string> &scope) {
49 assert(!streaming);
50 while (current_scope.size() > scope.size() ||
51 (current_scope.size() > 0 &&
52 current_scope[current_scope.size() - 1] != scope[current_scope.size() - 1])) {
53 buffer += "$upscope $end\n";
54 current_scope.pop_back();
55 }
56 while (current_scope.size() < scope.size()) {
57 buffer += "$scope module " + scope[current_scope.size()] + " $end\n";
58 current_scope.push_back(scope[current_scope.size()]);
59 }
60 }
61
62 void emit_ident(size_t ident) {
63 do {
64 buffer += '!' + ident % 94; // "base94"
65 ident /= 94;
66 } while (ident != 0);
67 }
68
69 void emit_var(const variable &var, const std::string &type, const std::string &name) {
70 assert(!streaming);
71 buffer += "$var " + type + " " + std::to_string(var.width) + " ";
72 emit_ident(var.ident);
73 buffer += " " + name + " $end\n";
74 }
75
76 void emit_enddefinitions() {
77 assert(!streaming);
78 buffer += "$enddefinitions $end\n";
79 streaming = true;
80 }
81
82 void emit_time(uint64_t timestamp) {
83 assert(streaming);
84 buffer += "#" + std::to_string(timestamp) + "\n";
85 }
86
87 void emit_scalar(const variable &var) {
88 assert(streaming);
89 assert(var.width == 1);
90 buffer += (*var.curr ? '1' : '0');
91 emit_ident(var.ident);
92 buffer += '\n';
93 }
94
95 void emit_vector(const variable &var) {
96 assert(streaming);
97 buffer += 'b';
98 for (size_t bit = var.width - 1; bit != (size_t)-1; bit--) {
99 bool bit_curr = var.curr[bit / (8 * sizeof(chunk_t))] & (1 << (bit % (8 * sizeof(chunk_t))));
100 buffer += (bit_curr ? '1' : '0');
101 }
102 buffer += ' ';
103 emit_ident(var.ident);
104 buffer += '\n';
105 }
106
107 const variable &register_variable(size_t width, chunk_t *curr, bool constant = false) {
108 if (aliases.count(curr)) {
109 return variables[aliases[curr]];
110 } else {
111 const size_t chunks = (width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
112 aliases[curr] = variables.size();
113 if (constant) {
114 variables.emplace_back(variable { variables.size(), width, curr, (size_t)-1 });
115 } else {
116 variables.emplace_back(variable { variables.size(), width, curr, cache.size() });
117 cache.insert(cache.end(), &curr[0], &curr[chunks]);
118 }
119 return variables.back();
120 }
121 }
122
123 bool test_variable(const variable &var) {
124 if (var.prev_off == (size_t)-1)
125 return false; // constant
126 const size_t chunks = (var.width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
127 if (std::equal(&var.curr[0], &var.curr[chunks], &cache[var.prev_off])) {
128 return false;
129 } else {
130 std::copy(&var.curr[0], &var.curr[chunks], &cache[var.prev_off]);
131 return true;
132 }
133 }
134
135 static std::vector<std::string> split_hierarchy(const std::string &hier_name) {
136 std::vector<std::string> hierarchy;
137 size_t prev = 0;
138 while (true) {
139 size_t curr = hier_name.find_first_of(' ', prev);
140 if (curr == std::string::npos) {
141 hierarchy.push_back(hier_name.substr(prev));
142 break;
143 } else {
144 hierarchy.push_back(hier_name.substr(prev, curr - prev));
145 prev = curr + 1;
146 }
147 }
148 return hierarchy;
149 }
150
151 public:
152 std::string buffer;
153
154 void timescale(unsigned number, const std::string &unit) {
155 emit_timescale(number, unit);
156 }
157
158 void add(const std::string &hier_name, const debug_item &item) {
159 std::vector<std::string> scope = split_hierarchy(hier_name);
160 std::string name = scope.back();
161 scope.pop_back();
162
163 emit_scope(scope);
164 switch (item.type) {
165 // Not the best naming but oh well...
166 case debug_item::VALUE:
167 emit_var(register_variable(item.width, item.curr, /*constant=*/item.next == nullptr), "wire", name);
168 break;
169 case debug_item::WIRE:
170 emit_var(register_variable(item.width, item.curr), "reg", name);
171 break;
172 case debug_item::MEMORY: {
173 const size_t stride = (item.width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
174 for (size_t index = 0; index < item.depth; index++) {
175 chunk_t *nth_curr = &item.curr[stride * index];
176 std::string nth_name = name + '[' + std::to_string(index) + ']';
177 emit_var(register_variable(item.width, nth_curr), "reg", nth_name);
178 }
179 break;
180 }
181 case debug_item::ALIAS:
182 // Like VALUE, but, even though `item.next == nullptr` always holds, the underlying value
183 // can actually change, and must be tracked. In most cases the VCD identifier will be
184 // unified with the aliased reg, but we should handle the case where only the alias is
185 // added to the VCD writer, too.
186 emit_var(register_variable(item.width, item.curr), "wire", name);
187 break;
188 }
189 }
190
191 template<class Filter>
192 void add(const debug_items &items, const Filter &filter) {
193 // `debug_items` is a map, so the items are already sorted in an order optimal for emitting
194 // VCD scope sections.
195 for (auto &it : items)
196 if (filter(it.first, it.second))
197 add(it.first, it.second);
198 }
199
200 void add(const debug_items &items) {
201 this->template add(items, [](const std::string &, const debug_item &) {
202 return true;
203 });
204 }
205
206 void add_without_memories(const debug_items &items) {
207 this->template add(items, [](const std::string &, const debug_item &item) {
208 return item.type != debug_item::MEMORY;
209 });
210 }
211
212 void sample(uint64_t timestamp) {
213 bool first_sample = !streaming;
214 if (first_sample) {
215 emit_scope({});
216 emit_enddefinitions();
217 }
218 emit_time(timestamp);
219 for (auto var : variables)
220 if (test_variable(var) || first_sample) {
221 if (var.width == 1)
222 emit_scalar(var);
223 else
224 emit_vector(var);
225 }
226 }
227 };
228
229 }
230
231 #endif