3 state <IdString> cemuxAB rstmuxBA
7 select dff->type == $dff
8 select GetSize(port(dff, \D)) > 1
16 select rstmux->type == $mux
17 select GetSize(port(rstmux, \Y)) > 1
18 index <SigSpec> port(rstmux, \Y) === sigD
19 choice <IdString> BA {\B, \A}
20 select port(rstmux, BA).is_fully_const()
27 sigD = port(rstmux, rstmuxBA == \B ? \A : \B);
31 select cemux->type == $mux
32 select GetSize(port(cemux, \Y)) > 1
33 index <SigSpec> port(cemux, \Y) === sigD
34 choice <IdString> AB {\A, \B}
35 index <SigSpec> port(cemux, AB) === port(dff, \Q)
41 if (!cemux && !rstmux)
49 D = port(cemux, cemuxAB == \A ? \B : \A);
51 rst = port(rstmux, rstmuxBA).as_const();
53 rst = Const(State::Sx, GetSize(D));
57 D = port(rstmux, rstmuxBA == \B ? \A : \B);
58 rst = port(rstmux, rstmuxBA).as_const();
60 SigSpec Q = port(dff, \Q);
61 int width = GetSize(D);
63 SigSpec &dffD = dff->connections_.at(\D);
64 SigSpec &dffQ = dff->connections_.at(\Q);
66 for (const auto &b : Q) {
67 auto it = b.wire->attributes.find(\init);
68 init.bits.push_back(it == b.wire->attributes.end() ? State::Sx : it->second[b.offset]);
71 auto cmpx = [=](State lhs, State rhs) {
72 if (lhs == State::Sx || rhs == State::Sx)
79 log_dump(i, D[i], D[i-1], rst[i], rst[i-1], init[i], init[i-1]);
82 if (!cmpx(rst[i], rst[i-1]))
84 if (!cmpx(init[i], init[i-1]))
86 if (!cmpx(rst[i], init[i]))
88 module->connect(Q[i], Q[i-1]);
94 SigSpec &ceA = cemux->connections_.at(\A);
95 SigSpec &ceB = cemux->connections_.at(\B);
96 SigSpec &ceY = cemux->connections_.at(\Y);
97 ceA.remove(i, width-1-i);
98 ceB.remove(i, width-1-i);
99 ceY.remove(i, width-1-i);
100 cemux->fixup_parameters();
103 SigSpec &rstA = rstmux->connections_.at(\A);
104 SigSpec &rstB = rstmux->connections_.at(\B);
105 SigSpec &rstY = rstmux->connections_.at(\Y);
106 rstA.remove(i, width-1-i);
107 rstB.remove(i, width-1-i);
108 rstY.remove(i, width-1-i);
109 rstmux->fixup_parameters();
111 dffD.remove(i, width-1-i);
112 dffQ.remove(i, width-1-i);
113 dff->fixup_parameters();
115 log("dffcemux pattern in %s: dff=%s, cemux=%s, rstmux=%s; removed top %d bits.\n", log_id(module), log_id(dff), log_id(cemux, "n/a"), log_id(rstmux, "n/a"), width-1-i);
119 SigSpec &ceA = cemux->connections_.at(\A);
120 SigSpec &ceB = cemux->connections_.at(\B);
121 SigSpec &ceY = cemux->connections_.at(\Y);
124 for (int i = width-1; i >= 0; i--) {
127 if (cmpx(rst[i], D[i].data) && cmpx(init[i], D[i].data)) {
129 module->connect(Q[i], D[i]);
138 did_something = true;
139 cemux->fixup_parameters();
140 dff->fixup_parameters();
141 log("dffcemux pattern in %s: dff=%s, cemux=%s, rstmux=%s; removed %d constant bits.\n", log_id(module), log_id(dff), log_id(cemux), log_id(rstmux, "n/a"), count);