Merge pull request #2485 from whitequark/cxxrtl-cell-input-buffering
[yosys.git] / backends / cxxrtl / cxxrtl_vcd_capi.cc
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 // This file is a part of the CXXRTL C API. It should be used together with `cxxrtl_vcd_capi.h`.
20
21 #include <backends/cxxrtl/cxxrtl_vcd.h>
22 #include <backends/cxxrtl/cxxrtl_vcd_capi.h>
23
24 extern const cxxrtl::debug_items &cxxrtl_debug_items_from_handle(cxxrtl_handle handle);
25
26 struct _cxxrtl_vcd {
27 cxxrtl::vcd_writer writer;
28 bool flush = false;
29 };
30
31 cxxrtl_vcd cxxrtl_vcd_create() {
32 return new _cxxrtl_vcd;
33 }
34
35 void cxxrtl_vcd_destroy(cxxrtl_vcd vcd) {
36 delete vcd;
37 }
38
39 void cxxrtl_vcd_timescale(cxxrtl_vcd vcd, int number, const char *unit) {
40 vcd->writer.timescale(number, unit);
41 }
42
43 void cxxrtl_vcd_add(cxxrtl_vcd vcd, const char *name, cxxrtl_object *object) {
44 // Note the copy. We don't know whether `object` came from a design (in which case it is
45 // an instance of `debug_item`), or from user code (in which case it is an instance of
46 // `cxxrtl_object`), so casting the pointer wouldn't be safe.
47 vcd->writer.add(name, cxxrtl::debug_item(*object));
48 }
49
50 void cxxrtl_vcd_add_from(cxxrtl_vcd vcd, cxxrtl_handle handle) {
51 vcd->writer.add(cxxrtl_debug_items_from_handle(handle));
52 }
53
54 void cxxrtl_vcd_add_from_if(cxxrtl_vcd vcd, cxxrtl_handle handle, void *data,
55 int (*filter)(void *data, const char *name,
56 const cxxrtl_object *object)) {
57 vcd->writer.add(cxxrtl_debug_items_from_handle(handle),
58 [=](const std::string &name, const cxxrtl::debug_item &item) {
59 return filter(data, name.c_str(), static_cast<const cxxrtl_object*>(&item));
60 });
61 }
62
63 void cxxrtl_vcd_add_from_without_memories(cxxrtl_vcd vcd, cxxrtl_handle handle) {
64 vcd->writer.add_without_memories(cxxrtl_debug_items_from_handle(handle));
65 }
66
67 void cxxrtl_vcd_sample(cxxrtl_vcd vcd, uint64_t time) {
68 if (vcd->flush) {
69 vcd->writer.buffer.clear();
70 vcd->flush = false;
71 }
72 vcd->writer.sample(time);
73 }
74
75 void cxxrtl_vcd_read(cxxrtl_vcd vcd, const char **data, size_t *size) {
76 if (vcd->flush) {
77 vcd->writer.buffer.clear();
78 vcd->flush = false;
79 }
80 *data = vcd->writer.buffer.c_str();
81 *size = vcd->writer.buffer.size();
82 vcd->flush = true;
83 }