Merge pull request #2480 from YosysHQ/dave/nexus-lram
[yosys.git] / backends / cxxrtl / cxxrtl_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_capi.h`.
20
21 #include <backends/cxxrtl/cxxrtl.h>
22 #include <backends/cxxrtl/cxxrtl_capi.h>
23
24 struct _cxxrtl_handle {
25 std::unique_ptr<cxxrtl::module> module;
26 cxxrtl::debug_items objects;
27 };
28
29 // Private function for use by other units of the C API.
30 const cxxrtl::debug_items &cxxrtl_debug_items_from_handle(cxxrtl_handle handle) {
31 return handle->objects;
32 }
33
34 cxxrtl_handle cxxrtl_create(cxxrtl_toplevel design) {
35 return cxxrtl_create_at(design, "");
36 }
37
38 cxxrtl_handle cxxrtl_create_at(cxxrtl_toplevel design, const char *root) {
39 std::string path = root;
40 if (!path.empty()) {
41 // module::debug_info() accepts either an empty path, or a path ending in space to simplify
42 // the logic in generated code. While this is sketchy at best to expose in the C++ API, this
43 // would be a lot worse in the C API, so don't expose it here.
44 assert(path.back() != ' ');
45 path += ' ';
46 }
47
48 cxxrtl_handle handle = new _cxxrtl_handle;
49 handle->module = std::move(design->module);
50 handle->module->debug_info(handle->objects, path);
51 delete design;
52 return handle;
53 }
54
55 void cxxrtl_destroy(cxxrtl_handle handle) {
56 delete handle;
57 }
58
59 void cxxrtl_reset(cxxrtl_handle handle) {
60 handle->module->reset();
61 }
62
63 int cxxrtl_eval(cxxrtl_handle handle) {
64 return handle->module->eval();
65 }
66
67 int cxxrtl_commit(cxxrtl_handle handle) {
68 return handle->module->commit();
69 }
70
71 size_t cxxrtl_step(cxxrtl_handle handle) {
72 return handle->module->step();
73 }
74
75 struct cxxrtl_object *cxxrtl_get_parts(cxxrtl_handle handle, const char *name, size_t *parts) {
76 auto it = handle->objects.table.find(name);
77 if (it == handle->objects.table.end())
78 return nullptr;
79 *parts = it->second.size();
80 return static_cast<cxxrtl_object*>(&it->second[0]);
81 }
82
83 void cxxrtl_enum(cxxrtl_handle handle, void *data,
84 void (*callback)(void *data, const char *name,
85 cxxrtl_object *object, size_t parts)) {
86 for (auto &it : handle->objects.table)
87 callback(data, it.first.c_str(), static_cast<cxxrtl_object*>(&it.second[0]), it.second.size());
88 }
89
90 void cxxrtl_outline_eval(cxxrtl_outline outline) {
91 outline->eval();
92 }