Merge pull request #2132 from YosysHQ/eddie/verific_initial
[yosys.git] / backends / cxxrtl / cxxrtl_capi.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_CAPI_H
20 #define CXXRTL_CAPI_H
21
22 // This file is a part of the CXXRTL C API. It should be used together with `cxxrtl_capi.cc`.
23 //
24 // The CXXRTL C API makes it possible to drive CXXRTL designs using C or any other language that
25 // supports the C ABI, for example, Python. It does not provide a way to implement black boxes.
26
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <assert.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 // Opaque reference to a design toplevel.
36 //
37 // A design toplevel can only be used to create a design handle.
38 typedef struct _cxxrtl_toplevel *cxxrtl_toplevel;
39
40 // The constructor for a design toplevel is provided as a part of generated code for that design.
41 // Its prototype matches:
42 //
43 // cxxrtl_toplevel <design-name>_create();
44
45 // Opaque reference to a design handle.
46 //
47 // A design handle is required by all operations in the C API.
48 typedef struct _cxxrtl_handle *cxxrtl_handle;
49
50 // Create a design handle from a design toplevel.
51 //
52 // The `design` is consumed by this operation and cannot be used afterwards.
53 cxxrtl_handle cxxrtl_create(cxxrtl_toplevel design);
54
55 // Release all resources used by a design and its handle.
56 void cxxrtl_destroy(cxxrtl_handle handle);
57
58 // Simulate the design to a fixed point.
59 //
60 // Returns the number of delta cycles.
61 size_t cxxrtl_step(cxxrtl_handle handle);
62
63 // Type of a simulated object.
64 enum cxxrtl_type {
65 // Values correspond to singly buffered netlist nodes, i.e. nodes driven exclusively by
66 // combinatorial cells, or toplevel input nodes.
67 //
68 // Values can be inspected via the `curr` pointer. If the `next` pointer is NULL, the value is
69 // driven by a constant and can never be modified. Otherwise, the value can be modified through
70 // the `next` pointer (which is equal to `curr` if not NULL). Note that changes to the bits
71 // driven by combinatorial cells will be ignored.
72 //
73 // Values always have depth 1.
74 CXXRTL_VALUE = 0,
75
76 // Wires correspond to doubly buffered netlist nodes, i.e. nodes driven, at least in part, by
77 // storage cells, or by combinatorial cells that are a part of a feedback path.
78 //
79 // Wires can be inspected via the `curr` pointer and modified via the `next` pointer (which are
80 // distinct for wires). Note that changes to the bits driven by combinatorial cells will be
81 // ignored.
82 //
83 // Wires always have depth 1.
84 CXXRTL_WIRE = 1,
85
86 // Memories correspond to memory cells.
87 //
88 // Memories can be inspected and modified via the `curr` pointer. Due to a limitation of this
89 // API, memories cannot yet be modified in a guaranteed race-free way, and the `next` pointer is
90 // always NULL.
91 CXXRTL_MEMORY = 2,
92
93 // Aliases correspond to netlist nodes driven by another node such that their value is always
94 // exactly equal, or driven by a constant value.
95 //
96 // Aliases can be inspected via the `curr` pointer. They cannot be modified, and the `next`
97 // pointer is always NULL.
98 CXXRTL_ALIAS = 3,
99
100 // More object types may be added in the future, but the existing ones will never change.
101 };
102
103 // Description of a simulated object.
104 //
105 // The `data` array can be accessed directly to inspect and, if applicable, modify the bits
106 // stored in the object.
107 struct cxxrtl_object {
108 // Type of the object.
109 //
110 // All objects have the same memory layout determined by `width` and `depth`, but the type
111 // determines all other properties of the object.
112 uint32_t type; // actually `enum cxxrtl_type`
113
114 // Width of the object in bits.
115 size_t width;
116
117 // Index of the least significant bit.
118 size_t lsb_at;
119
120 // Depth of the object. Only meaningful for memories; for other objects, always 1.
121 size_t depth;
122
123 // Index of the first word. Only meaningful for memories; for other objects, always 0;
124 size_t zero_at;
125
126 // Bits stored in the object, as 32-bit chunks, least significant bits first.
127 //
128 // The width is rounded up to a multiple of 32; the padding bits are always set to 0 by
129 // the simulation code, and must be always written as 0 when modified by user code.
130 // In memories, every element is stored contiguously. Therefore, the total number of chunks
131 // in any object is `((width + 31) / 32) * depth`.
132 //
133 // To allow the simulation to be partitioned into multiple independent units communicating
134 // through wires, the bits are double buffered. To avoid race conditions, user code should
135 // always read from `curr` and write to `next`. The `curr` pointer is always valid; for objects
136 // that cannot be modified, or cannot be modified in a race-free way, `next` is NULL.
137 uint32_t *curr;
138 uint32_t *next;
139
140 // More description fields may be added in the future, but the existing ones will never change.
141 };
142
143 // Retrieve description of a simulated object.
144 //
145 // The `name` is the full hierarchical name of the object in the Yosys notation, where public names
146 // have a `\` prefix and hierarchy levels are separated by single spaces. For example, if
147 // the top-level module instantiates a module `foo`, which in turn contains a wire `bar`, the full
148 // hierarchical name is `\foo \bar`.
149 //
150 // The storage of a single abstract object may be split (usually with the `splitnets` pass) into
151 // many physical parts, all of which correspond to the same hierarchical name. To handle such cases,
152 // this function returns an array and writes its length to `parts`. The array is sorted by `lsb_at`.
153 //
154 // Returns the object parts if it was found, NULL otherwise. The returned parts are valid until
155 // the design is destroyed.
156 struct cxxrtl_object *cxxrtl_get_parts(cxxrtl_handle handle, const char *name, size_t *parts);
157
158 // Retrieve description of a single part simulated object.
159 //
160 // This function is a shortcut for the most common use of `cxxrtl_get_parts`. It asserts that,
161 // if the object exists, it consists of a single part. If assertions are disabled, it returns NULL
162 // for multi-part objects.
163 inline struct cxxrtl_object *cxxrtl_get(cxxrtl_handle handle, const char *name) {
164 size_t parts = 0;
165 struct cxxrtl_object *object = cxxrtl_get_parts(handle, name, &parts);
166 assert(object == NULL || parts == 1);
167 if (object == NULL || parts == 1)
168 return object;
169 return NULL;
170 }
171
172 // Enumerate simulated objects.
173 //
174 // For every object in the simulation, `callback` is called with the provided `data`, the full
175 // hierarchical name of the object (see `cxxrtl_get` for details), and the object parts.
176 // The provided `name` and `object` values are valid until the design is destroyed.
177 void cxxrtl_enum(cxxrtl_handle handle, void *data,
178 void (*callback)(void *data, const char *name,
179 struct cxxrtl_object *object, size_t parts));
180
181 #ifdef __cplusplus
182 }
183 #endif
184
185 #endif