cxxrtl: track aliases in VCD writer.
[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) {
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 variables.emplace_back(variable { variables.size(), width, curr, cache.size() });
114 cache.insert(cache.end(), &curr[0], &curr[chunks]);
115 return variables.back();
116 }
117 }
118
119 bool test_variable(const variable &var) {
120 const size_t chunks = (var.width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
121 if (std::equal(&var.curr[0], &var.curr[chunks], &cache[var.prev_off])) {
122 return false;
123 } else {
124 std::copy(&var.curr[0], &var.curr[chunks], &cache[var.prev_off]);
125 return true;
126 }
127 }
128
129 static std::vector<std::string> split_hierarchy(const std::string &hier_name) {
130 std::vector<std::string> hierarchy;
131 size_t prev = 0;
132 while (true) {
133 size_t curr = hier_name.find_first_of(' ', prev + 1);
134 if (curr > hier_name.size())
135 curr = hier_name.size();
136 if (curr > prev + 1)
137 hierarchy.push_back(hier_name.substr(prev, curr - prev));
138 if (curr == hier_name.size())
139 break;
140 prev = curr + 1;
141 }
142 return hierarchy;
143 }
144
145 public:
146 std::string buffer;
147
148 void timescale(unsigned number, const std::string &unit) {
149 emit_timescale(number, unit);
150 }
151
152 void add(const std::string &hier_name, const debug_item &item) {
153 std::vector<std::string> scope = split_hierarchy(hier_name);
154 std::string name = scope.back();
155 scope.pop_back();
156
157 emit_scope(scope);
158 switch (item.type) {
159 // Not the best naming but oh well...
160 case debug_item::VALUE:
161 emit_var(register_variable(item.width, item.curr), "wire", name);
162 break;
163 case debug_item::WIRE:
164 emit_var(register_variable(item.width, item.curr), "reg", name);
165 break;
166 case debug_item::MEMORY: {
167 const size_t stride = (item.width + (sizeof(chunk_t) * 8 - 1)) / (sizeof(chunk_t) * 8);
168 for (size_t index = 0; index < item.depth; index++) {
169 chunk_t *nth_curr = &item.curr[stride * index];
170 std::string nth_name = name + '[' + std::to_string(index) + ']';
171 emit_var(register_variable(item.width, nth_curr), "reg", nth_name);
172 }
173 break;
174 }
175 }
176 }
177
178 template<class Filter>
179 void add(const debug_items &items, const Filter &filter) {
180 // `debug_items` is a map, so the items are already sorted in an order optimal for emitting
181 // VCD scope sections.
182 for (auto &it : items)
183 if (filter(it.first, it.second))
184 add(it.first, it.second);
185 }
186
187 void add(const debug_items &items) {
188 this->template add(items, [](const std::string &, const debug_item &) {
189 return true;
190 });
191 }
192
193 void add_without_memories(const debug_items &items) {
194 this->template add(items, [](const std::string &, const debug_item &item) {
195 return item.type == debug_item::VALUE || item.type == debug_item::WIRE;
196 });
197 }
198
199 void sample(uint64_t timestamp) {
200 bool first_sample = !streaming;
201 if (first_sample) {
202 emit_scope({});
203 emit_enddefinitions();
204 }
205 emit_time(timestamp);
206 for (auto var : variables)
207 if (test_variable(var) || first_sample) {
208 if (var.width == 1)
209 emit_scalar(var);
210 else
211 emit_vector(var);
212 }
213 }
214 };
215
216 }
217
218 #endif