]>
Witch of Git - nan-gate/blob - nangate.cc
1 #include "kernel/yosys.h"
2 #include "kernel/celltypes.h"
3 #include "erase_b2f_pm.h"
4 #include "dff_nan_pm.h"
5 #include "share_nan_pm.h"
8 PRIVATE_NAMESPACE_BEGIN
10 static bool is_width(const string
&arg
) {
11 return arg
== "-w" or arg
== "-width" or arg
== "--width";
14 struct NandToNaNWorker
16 int nand_count
= 0, not_count
= 0, b2f_count
= 0, f2b_count
= 0;
17 RTLIL::Design
*design
;
18 RTLIL::Module
*module
;
21 NandToNaNWorker(RTLIL::Design
*design
, RTLIL::Module
*module
, size_t width
) :
22 design(design
), module(module
), width(width
)
24 IdString
b2f_cell("\\bit_to_fp" + std::to_string(width
));
25 IdString
nan_cell("\\nan_fp" + std::to_string(width
));
26 IdString
f2b_cell("\\fp" + std::to_string(width
) + "_to_bit");
28 for (auto cell
: module
->selected_cells()) {
29 if (cell
->type
== "$_NAND_") {
33 RTLIL::Cell
*b2fA
= module
->addCell(NEW_ID
, b2f_cell
);
34 RTLIL::Cell
*b2fB
= module
->addCell(NEW_ID
, b2f_cell
);
35 RTLIL::Cell
*nan
= module
->addCell(NEW_ID
, nan_cell
);
36 RTLIL::Cell
*f2b
= module
->addCell(NEW_ID
, f2b_cell
);
37 b2fA
->attributes
[ID(nan_b2f
)] = width
;
38 b2fB
->attributes
[ID(nan_b2f
)] = width
;
39 nan
->attributes
[ID(nan_cell
)] = width
;
40 f2b
->attributes
[ID(nan_f2b
)] = width
;
41 b2fA
->setPort("\\A", cell
->getPort("\\A"));
42 b2fA
->setPort("\\Y", module
->addWire(NEW_ID
, width
));
43 b2fB
->setPort("\\A", cell
->getPort("\\B"));
44 b2fB
->setPort("\\Y", module
->addWire(NEW_ID
, width
));
45 f2b
->setPort("\\A", module
->addWire(NEW_ID
, width
));
46 f2b
->setPort("\\Y", cell
->getPort("\\Y"));
47 nan
->setPort("\\A", b2fA
->getPort("\\Y"));
48 nan
->setPort("\\B", b2fB
->getPort("\\Y"));
49 nan
->setPort("\\Y", f2b
->getPort("\\A"));
50 module
->swap_names(cell
, nan
);
52 } else if (cell
->type
== "$_NOT_") {
56 RTLIL::Cell
*b2f
= module
->addCell(NEW_ID
, b2f_cell
);
57 RTLIL::Cell
*nan
= module
->addCell(NEW_ID
, nan_cell
);
58 RTLIL::Cell
*f2b
= module
->addCell(NEW_ID
, f2b_cell
);
59 b2f
->attributes
[ID(nan_b2f
)] = width
;
60 nan
->attributes
[ID(nan_cell
)] = width
;
61 f2b
->attributes
[ID(nan_f2b
)] = width
;
62 b2f
->setPort("\\A", cell
->getPort("\\A"));
63 b2f
->setPort("\\Y", module
->addWire(NEW_ID
, width
));
64 f2b
->setPort("\\A", module
->addWire(NEW_ID
, width
));
65 f2b
->setPort("\\Y", cell
->getPort("\\Y"));
66 nan
->setPort("\\A", b2f
->getPort("\\Y"));
67 nan
->setPort("\\B", b2f
->getPort("\\Y"));
68 nan
->setPort("\\Y", f2b
->getPort("\\A"));
69 module
->swap_names(cell
, nan
);
76 struct NandToNaNPass
: public Pass
{
77 NandToNaNPass() : Pass("nand_to_nan") {}
78 void execute(vector
<string
> args
, Design
*design
) override
{
79 log_header(design
, "Executing NAND_TO_NaN pass (implementing tom7 logic)\n");
81 Pass::call(design
, "read_verilog -lib -sv +/plugins/nangate/techlib.sv");
86 for (; argidx
< args
.size(); ++argidx
) {
87 if (is_width(args
[argidx
]) and argidx
+ 1 < args
.size()) {
89 width
= std::stoi(args
[++argidx
]);
91 cmd_error(args
, argidx
, "Invalid number");
98 for (auto module
: design
->selected_modules()) {
99 log("Replacing NAND with NaN in module %s...\n", log_id(module
));
100 NandToNaNWorker
worker(design
, module
, width
);
101 log("Replaced %d NAND gates and %d NOT gates.\n",
102 worker
.nand_count
, worker
.not_count
);
103 log("Inserted:\n nan_fp#: %5d\n bit_to_fp#: %5d\n fp#_to_bit: %5d\n",
104 worker
.nand_count
+ worker
.not_count
,
105 worker
.b2f_count
, worker
.f2b_count
);
110 struct DffToFpPass
: public Pass
{
111 DffToFpPass() : Pass("dff_nan") {}
112 void execute(vector
<string
> args
, Design
*design
) override
{
113 log_header(design
, "Executing DFF_NaN pass (widening flipflops to hold floats)\n");
117 for (; argidx
< args
.size(); ++argidx
) {
118 if (is_width(args
[argidx
]) and argidx
+ 1 < args
.size()) {
120 width
= std::stoi(args
[++argidx
]);
122 cmd_error(args
, argidx
, "Invalid number");
126 extra_args(args
, argidx
, design
, false);
128 IdString
b2f_cell("\\bit_to_fp" + std::to_string(width
));
129 IdString
nan_cell("\\nan_fp" + std::to_string(width
));
130 IdString
f2b_cell("\\fp" + std::to_string(width
) + "_to_bit");
132 for (auto module
: design
->selected_modules()) {
133 log(" Module %s\n", log_id(module
));
134 dff_nan_pm
pm(module
, module
->selected_cells());
135 pool
<RTLIL::Cell
*> dffs
;
136 pm
.run([&]() { dffs
.insert(pm
.st
.dff
); });
137 for (auto &dff
: dffs
) {
138 RTLIL::Cell
*f2b
= module
->addCell(NEW_ID
, f2b_cell
);
139 f2b
->attributes
[ID(nan_f2b
)] = width
;
140 f2b
->setPort("\\A", module
->addWire(NEW_ID
, width
));
141 f2b
->setPort("\\Y", dff
->getPort("\\Q"));
142 RTLIL::Cell
*b2f
= module
->addCell(NEW_ID
, b2f_cell
);
143 b2f
->attributes
[ID(nan_b2f
)] = width
;
144 b2f
->setPort("\\A", dff
->getPort("\\D"));
145 b2f
->setPort("\\Y", module
->addWire(NEW_ID
, width
));
146 for (int i
= 0; i
< width
; i
++) {
147 // @TODO: Support more DFF types
148 assert(dff
->type
== "$_DFF_P_");
149 RTLIL::Cell
*new_ff
= module
->addCell(NEW_ID
, "$_DFF_P_");
150 new_ff
->setPort("\\C", dff
->getPort("\\C"));
151 new_ff
->setPort("\\D", b2f
->getPort("\\Y")[i
]);
152 new_ff
->setPort("\\Q", f2b
->getPort("\\A")[i
]);
156 log("Converted %d flip-flops to hold floats\n", GetSize(dffs
));
161 struct EraseFpBitPass
: public Pass
{
162 EraseFpBitPass() : Pass("simplify_nan") {}
163 void execute(vector
<string
> args
, Design
*design
) override
{
164 log_header(design
, "Executing SIMPLIFY_NaN pass (erasing useless conversion chains)\n");
166 for (auto module
: design
->selected_modules()) {
167 log("Simplifying NaN conversions in module %s\n", log_id(module
));
168 erase_b2f_pm
pm(module
, module
->selected_cells());
169 pool
<RTLIL::Cell
*> eraseCells
;
171 module
->connect(pm
.st
.base
->getPort("\\A"), pm
.st
.target
->getPort("\\Y"));
172 eraseCells
.insert(pm
.st
.target
);
174 for (auto cell
: eraseCells
) {
175 module
->remove(cell
);
177 log("Removed %d bit_to_fp# nodes\n", GetSize(eraseCells
));
182 struct ShareNaN
: public Pass
{
183 ShareNaN() : Pass("share_nan") {}
184 void execute(vector
<string
> args
, Design
*design
) override
{
185 log_header(design
, "Executing SHARE_NAN pass (merging conversion cells).\n");
187 for (auto module
: design
->selected_modules()) {
188 log("Module %s\n", log_id(module
));
189 share_nan_pm
pm(module
, module
->selected_cells());
190 mfp
<RTLIL::Cell
*> sharedCells
;
191 pm
.run([&]() { sharedCells
.merge(pm
.st
.cvt_a
, pm
.st
.cvt_b
); });
193 for (auto &entry
: sharedCells
) {
194 auto &main
= sharedCells
.find(entry
);
195 if (entry
== main
) continue;
197 module
->connect(main
->getPort("\\Y"), entry
->getPort("\\Y"));
198 module
->remove(entry
);
200 log("Merged %d conversion cells\n", merged
);
205 struct TechmapNaN
: public Pass
{
206 TechmapNaN() : Pass("techmap_nan", "techmap NaN gates") {}
207 void execute(vector
<string
>, Design
*design
) override
{
208 Pass::call(design
, "techmap -autoproc -extern -map +/plugins/nangate/techlib.sv");
212 struct SynthNaN
: public Pass
{
213 SynthNaN() : Pass("synth_nan", "synthesize to tom7 logic") {}
214 void help() override
{
216 log(" synth_nan [options]\n\n");
218 log("This command synthesizes a design into NaN gates.");
220 log(" -top <module>");
221 log(" use the specified module as top module (default='top')");
223 log(" -width <width>");
224 log(" synthesize with a given floating-point with (default=3)");
226 log("Runs the equivalent of the following script:\n\n");
227 log(" synth [-top <module>]\n");
228 log(" abc -g NAND\n");
229 log(" nand_to_nan [-width <width>]\n");
231 log(" dff_nan [-width <width>]\n");
232 log(" simplify_nan\n");
234 log(" techmap_nan\n");
236 void execute(vector
<string
> args
, Design
*design
) override
{
239 log_header(design
, "Executing SYNTH_NaN pass (synthesizing to tom7 logic).\n");
243 for (; argidx
< args
.size(); ++argidx
) {
244 if (args
[argidx
] == "-top" and argidx
+ 1 < args
.size()) {
245 synth_args
+= " -top ";
246 synth_args
+= args
[++argidx
];
249 if (is_width(args
[argidx
]) and argidx
+ 1 < args
.size()) {
250 width_args
+= " --width ";
251 width_args
+= args
[++argidx
];
256 extra_args(args
, argidx
, design
, false);
258 Pass::call(design
, "synth" + synth_args
);
259 Pass::call(design
, "abc -g NAND");
260 Pass::call(design
, "nand_to_nan" + width_args
);
261 Pass::call(design
, "share_nan");
262 Pass::call(design
, "dff_nan" + width_args
);
263 Pass::call(design
, "simplify_nan");
264 Pass::call(design
, "clean");
265 Pass::call(design
, "techmap_nan");
270 PRIVATE_NAMESPACE_END