Added ice40_ffinit pass
[yosys.git] / techlibs / ice40 / ice40_ffinit.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #include "kernel/yosys.h"
21 #include "kernel/sigtools.h"
22
23 USING_YOSYS_NAMESPACE
24 PRIVATE_NAMESPACE_BEGIN
25
26 struct Ice40FfinitPass : public Pass {
27 Ice40FfinitPass() : Pass("ice40_ffinit", "iCE40: handle FF init values") { }
28 virtual void help()
29 {
30 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
31 log("\n");
32 log(" ice40_ffinit [options] [selection]\n");
33 log("\n");
34 log("Remove zero init values for FF output signals. Add inverters to implement\n");
35 log("nonzero init values.\n");
36 log("\n");
37 }
38 virtual void execute(std::vector<std::string> args, RTLIL::Design *design)
39 {
40 log_header("Executing ICE40_FFINIT pass (implement FF init values).\n");
41
42 size_t argidx;
43 for (argidx = 1; argidx < args.size(); argidx++)
44 {
45 // if (args[argidx] == "-singleton") {
46 // singleton_mode = true;
47 // continue;
48 // }
49 break;
50 }
51 extra_args(args, argidx, design);
52
53 for (auto module : design->selected_modules())
54 {
55 log("Handling FF init values in %s.\n", log_id(module));
56
57 SigMap sigmap(module);
58 pool<Wire*> init_wires;
59 dict<SigBit, State> initbits;
60 pool<SigBit> handled_initbits;
61
62 for (auto wire : module->selected_wires())
63 {
64 if (wire->attributes.count("\\init") == 0)
65 continue;
66
67 SigSpec wirebits = sigmap(wire);
68 Const initval = wire->attributes.at("\\init");
69 init_wires.insert(wire);
70
71 for (int i = 0; i < GetSize(wirebits) && i < GetSize(initval); i++)
72 {
73 SigBit bit = wirebits[i];
74 State val = initval[i];
75
76 if (val != State::S0 && val != State::S1)
77 continue;
78
79 if (initbits.count(bit)) {
80 if (initbits.at(bit) != val)
81 log_error("Conflicting init values for signal %s.\n", log_signal(bit));
82 continue;
83 }
84
85 initbits[bit] = val;
86 }
87 }
88
89 for (auto cell : module->selected_cells())
90 {
91 if (!cell->type.in("\\SB_DFF", "\\SB_DFFE", "\\SB_DFFN", "\\SB_DFFNE"))
92 continue;
93
94 SigBit sig_d = sigmap(cell->getPort("\\D"));
95 SigBit sig_q = sigmap(cell->getPort("\\Q"));
96
97 if (!initbits.count(sig_q))
98 continue;
99
100 State val = initbits.at(sig_q);
101 handled_initbits.insert(sig_q);
102
103 log("FF init value for cell %s (%s): %s = %c\n", log_id(cell), log_id(cell->type),
104 log_signal(sig_q), val != State::S0 ? '1' : '0');
105
106 if (val == State::S0)
107 continue;
108
109 Wire *new_sig_d = module->addWire(NEW_ID);
110 Wire *new_sig_q = module->addWire(NEW_ID);
111
112 module->addNotGate(NEW_ID, sig_d, new_sig_d);
113 module->addNotGate(NEW_ID, new_sig_q, sig_q);
114
115 cell->setPort("\\D", new_sig_d);
116 cell->setPort("\\Q", new_sig_q);
117 }
118
119 for (auto wire : init_wires)
120 {
121 if (wire->attributes.count("\\init") == 0)
122 continue;
123
124 SigSpec wirebits = sigmap(wire);
125 Const &initval = wire->attributes.at("\\init");
126 bool remove_attribute = true;
127
128 for (int i = 0; i < GetSize(wirebits) && i < GetSize(initval); i++) {
129 if (handled_initbits.count(wirebits[i]))
130 wirebits[i] = State::Sx;
131 else
132 remove_attribute = false;
133 }
134
135 if (remove_attribute)
136 wire->attributes.erase("\\init");
137 }
138 }
139 }
140 } Ice40FfinitPass;
141
142 PRIVATE_NAMESPACE_END