2019-09-11 02:56:38 -05:00
|
|
|
pattern dffmux
|
|
|
|
|
2019-09-11 15:36:37 -05:00
|
|
|
state <IdString> cemuxAB rstmuxBA
|
|
|
|
state <SigSpec> sigD
|
2019-09-11 02:56:38 -05:00
|
|
|
|
|
|
|
match dff
|
|
|
|
select dff->type == $dff
|
|
|
|
select GetSize(port(dff, \D)) > 1
|
|
|
|
endmatch
|
|
|
|
|
2019-10-02 19:53:42 -05:00
|
|
|
code sigD
|
|
|
|
sigD = port(dff, \D);
|
|
|
|
endcode
|
|
|
|
|
2019-09-11 15:36:37 -05:00
|
|
|
match rstmux
|
|
|
|
select rstmux->type == $mux
|
|
|
|
select GetSize(port(rstmux, \Y)) > 1
|
2019-10-02 19:53:42 -05:00
|
|
|
index <SigSpec> port(rstmux, \Y) === sigD
|
2019-09-11 15:36:37 -05:00
|
|
|
choice <IdString> BA {\B, \A}
|
|
|
|
select port(rstmux, BA).is_fully_const()
|
|
|
|
set rstmuxBA BA
|
2019-10-02 19:53:42 -05:00
|
|
|
semioptional
|
2019-09-11 15:36:37 -05:00
|
|
|
endmatch
|
|
|
|
|
|
|
|
code sigD
|
|
|
|
if (rstmux)
|
|
|
|
sigD = port(rstmux, rstmuxBA == \B ? \A : \B);
|
|
|
|
endcode
|
|
|
|
|
2019-09-11 15:22:52 -05:00
|
|
|
match cemux
|
|
|
|
select cemux->type == $mux
|
|
|
|
select GetSize(port(cemux, \Y)) > 1
|
2019-09-11 15:36:37 -05:00
|
|
|
index <SigSpec> port(cemux, \Y) === sigD
|
2019-09-11 02:56:38 -05:00
|
|
|
choice <IdString> AB {\A, \B}
|
2019-09-11 15:22:52 -05:00
|
|
|
index <SigSpec> port(cemux, AB) === port(dff, \Q)
|
|
|
|
set cemuxAB AB
|
2019-10-02 19:53:42 -05:00
|
|
|
semioptional
|
2019-09-11 02:56:38 -05:00
|
|
|
endmatch
|
|
|
|
|
|
|
|
code
|
2019-10-02 19:53:42 -05:00
|
|
|
if (!cemux && !rstmux)
|
|
|
|
reject;
|
|
|
|
endcode
|
|
|
|
|
|
|
|
code
|
2019-09-11 15:36:37 -05:00
|
|
|
Const rst;
|
2019-10-02 19:53:42 -05:00
|
|
|
SigSpec D;
|
|
|
|
if (cemux) {
|
|
|
|
D = port(cemux, cemuxAB == \A ? \B : \A);
|
|
|
|
if (rstmux)
|
|
|
|
rst = port(rstmux, rstmuxBA).as_const();
|
|
|
|
else
|
|
|
|
rst = Const(State::Sx, GetSize(D));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_assert(rstmux);
|
|
|
|
D = port(rstmux, rstmuxBA == \B ? \A : \B);
|
2019-09-11 15:36:37 -05:00
|
|
|
rst = port(rstmux, rstmuxBA).as_const();
|
2019-10-02 19:53:42 -05:00
|
|
|
}
|
|
|
|
SigSpec Q = port(dff, \Q);
|
2019-09-11 02:56:38 -05:00
|
|
|
int width = GetSize(D);
|
|
|
|
|
2019-09-11 16:20:49 -05:00
|
|
|
SigSpec &dffD = dff->connections_.at(\D);
|
|
|
|
SigSpec &dffQ = dff->connections_.at(\Q);
|
2019-10-02 19:53:42 -05:00
|
|
|
Const init;
|
|
|
|
for (const auto &b : Q) {
|
|
|
|
auto it = b.wire->attributes.find(\init);
|
|
|
|
init.bits.push_back(it == b.wire->attributes.end() ? State::Sx : it->second[b.offset]);
|
|
|
|
}
|
2019-09-11 16:20:49 -05:00
|
|
|
|
2019-10-02 19:53:42 -05:00
|
|
|
auto cmpx = [=](State lhs, State rhs) {
|
|
|
|
if (lhs == State::Sx || rhs == State::Sx)
|
|
|
|
return true;
|
|
|
|
return lhs == rhs;
|
|
|
|
};
|
2019-09-11 02:56:38 -05:00
|
|
|
|
2019-10-02 23:26:26 -05:00
|
|
|
int i = width-1;
|
|
|
|
while (i > 1) {
|
2019-10-16 03:43:47 -05:00
|
|
|
// log_dump(i, D[i], D[i-1], rst[i], rst[i-1], init[i], init[i-1]);
|
2019-10-02 19:53:42 -05:00
|
|
|
if (D[i] != D[i-1])
|
|
|
|
break;
|
|
|
|
if (!cmpx(rst[i], rst[i-1]))
|
|
|
|
break;
|
|
|
|
if (!cmpx(init[i], init[i-1]))
|
|
|
|
break;
|
|
|
|
if (!cmpx(rst[i], init[i]))
|
|
|
|
break;
|
|
|
|
module->connect(Q[i], Q[i-1]);
|
2019-10-02 23:26:26 -05:00
|
|
|
i--;
|
2019-10-02 19:53:42 -05:00
|
|
|
}
|
|
|
|
if (i < width-1) {
|
2019-10-02 23:26:26 -05:00
|
|
|
did_something = true;
|
2019-10-02 19:53:42 -05:00
|
|
|
if (cemux) {
|
|
|
|
SigSpec &ceA = cemux->connections_.at(\A);
|
|
|
|
SigSpec &ceB = cemux->connections_.at(\B);
|
|
|
|
SigSpec &ceY = cemux->connections_.at(\Y);
|
2019-10-02 23:26:26 -05:00
|
|
|
ceA.remove(i, width-1-i);
|
|
|
|
ceB.remove(i, width-1-i);
|
|
|
|
ceY.remove(i, width-1-i);
|
2019-10-02 19:53:42 -05:00
|
|
|
cemux->fixup_parameters();
|
2019-09-11 02:56:38 -05:00
|
|
|
}
|
2019-10-02 23:26:26 -05:00
|
|
|
if (rstmux) {
|
|
|
|
SigSpec &rstA = rstmux->connections_.at(\A);
|
|
|
|
SigSpec &rstB = rstmux->connections_.at(\B);
|
|
|
|
SigSpec &rstY = rstmux->connections_.at(\Y);
|
|
|
|
rstA.remove(i, width-1-i);
|
|
|
|
rstB.remove(i, width-1-i);
|
|
|
|
rstY.remove(i, width-1-i);
|
|
|
|
rstmux->fixup_parameters();
|
|
|
|
}
|
|
|
|
dffD.remove(i, width-1-i);
|
|
|
|
dffQ.remove(i, width-1-i);
|
2019-09-11 02:56:38 -05:00
|
|
|
dff->fixup_parameters();
|
|
|
|
|
2019-10-02 23:26:26 -05:00
|
|
|
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);
|
|
|
|
width = i+1;
|
2019-09-11 02:56:38 -05:00
|
|
|
}
|
2019-10-02 23:26:26 -05:00
|
|
|
if (cemux) {
|
2019-10-02 19:53:42 -05:00
|
|
|
SigSpec &ceA = cemux->connections_.at(\A);
|
|
|
|
SigSpec &ceB = cemux->connections_.at(\B);
|
|
|
|
SigSpec &ceY = cemux->connections_.at(\Y);
|
|
|
|
|
2019-09-11 02:56:38 -05:00
|
|
|
int count = 0;
|
|
|
|
for (int i = width-1; i >= 0; i--) {
|
2019-09-11 15:22:52 -05:00
|
|
|
if (D[i].wire)
|
2019-09-11 02:56:38 -05:00
|
|
|
continue;
|
2019-10-02 23:26:26 -05:00
|
|
|
if (cmpx(rst[i], D[i].data) && cmpx(init[i], D[i].data)) {
|
2019-09-11 02:56:38 -05:00
|
|
|
count++;
|
2019-09-11 15:22:52 -05:00
|
|
|
module->connect(Q[i], D[i]);
|
2019-09-11 16:20:49 -05:00
|
|
|
ceA.remove(i);
|
|
|
|
ceB.remove(i);
|
|
|
|
ceY.remove(i);
|
|
|
|
dffD.remove(i);
|
|
|
|
dffQ.remove(i);
|
2019-09-11 02:56:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count > 0) {
|
|
|
|
did_something = true;
|
2019-09-11 15:22:52 -05:00
|
|
|
cemux->fixup_parameters();
|
2019-09-11 02:56:38 -05:00
|
|
|
dff->fixup_parameters();
|
2019-10-02 23:26:26 -05:00
|
|
|
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);
|
2019-09-11 02:56:38 -05:00
|
|
|
}
|
2019-10-02 23:26:26 -05:00
|
|
|
}
|
2019-09-11 02:56:38 -05:00
|
|
|
|
2019-10-02 23:26:26 -05:00
|
|
|
if (did_something)
|
2019-09-11 02:56:38 -05:00
|
|
|
accept;
|
|
|
|
endcode
|