From 2b3a148fc41f77e6933a97c964fb610d9dc313ab Mon Sep 17 00:00:00 2001 From: Anhijkt Date: Sat, 5 Apr 2025 13:46:38 +0300 Subject: [PATCH 1/4] ice40_dsp: fix const handling --- techlibs/ice40/ice40_dsp.pmg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/techlibs/ice40/ice40_dsp.pmg b/techlibs/ice40/ice40_dsp.pmg index 63bc8de4b..2cfb97b2a 100644 --- a/techlibs/ice40/ice40_dsp.pmg +++ b/techlibs/ice40/ice40_dsp.pmg @@ -32,8 +32,8 @@ code sigA sigB sigH ++i; return sig.extract(0, i); }; - sigA = unextend(port(mul, \A)); - sigB = unextend(port(mul, \B)); + sigA = port(mul, \A).is_fully_const() ? port(mul, \A) : unextend(port(mul, \A)); + sigB = port(mul, \B).is_fully_const() ? port(mul, \B) : unextend(port(mul, \B)); SigSpec O; if (mul->type == $mul) From 41a7d4bb819c715cc428a482b938315f68c6b358 Mon Sep 17 00:00:00 2001 From: Anhijkt Date: Wed, 9 Apr 2025 21:21:46 +0300 Subject: [PATCH 2/4] ice40_dsp: add test --- tests/arch/ice40/ice40_dsp_const.ys | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 tests/arch/ice40/ice40_dsp_const.ys diff --git a/tests/arch/ice40/ice40_dsp_const.ys b/tests/arch/ice40/ice40_dsp_const.ys new file mode 100644 index 000000000..c9c76a1ac --- /dev/null +++ b/tests/arch/ice40/ice40_dsp_const.ys @@ -0,0 +1,80 @@ +read_verilog << EOT +module top(input wire [14:0] a, output wire [18:0] b); +assign b = a*$unsigned(5'b01111); +endmodule +EOT + +prep +ice40_dsp + +read_verilog << EOT +module ref(a, b); + wire _0_; + wire _1_; + wire _2_; + wire [12:0] _3_; + (* src = "< Date: Thu, 10 Apr 2025 17:42:39 +0300 Subject: [PATCH 3/4] ice40_dsp: change unextend call condition --- techlibs/ice40/ice40_dsp.pmg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/techlibs/ice40/ice40_dsp.pmg b/techlibs/ice40/ice40_dsp.pmg index 2cfb97b2a..ff7c5cb65 100644 --- a/techlibs/ice40/ice40_dsp.pmg +++ b/techlibs/ice40/ice40_dsp.pmg @@ -32,8 +32,8 @@ code sigA sigB sigH ++i; return sig.extract(0, i); }; - sigA = port(mul, \A).is_fully_const() ? port(mul, \A) : unextend(port(mul, \A)); - sigB = port(mul, \B).is_fully_const() ? port(mul, \B) : unextend(port(mul, \B)); + sigA = param(mul, \A_SIGNED).as_bool() ? unextend(port(mul, \A)) : port(mul, \A); + sigB = param(mul, \B_SIGNED).as_bool() ? unextend(port(mul, \B)) : port(mul, \B); SigSpec O; if (mul->type == $mul) From 163e339c692494133a4648fb157fabed8da9753b Mon Sep 17 00:00:00 2001 From: Anhijkt Date: Fri, 11 Apr 2025 19:41:35 +0300 Subject: [PATCH 4/4] ice40_dsp: add unextend_unsigned function --- techlibs/ice40/ice40_dsp.pmg | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/techlibs/ice40/ice40_dsp.pmg b/techlibs/ice40/ice40_dsp.pmg index ff7c5cb65..7e4c3ace2 100644 --- a/techlibs/ice40/ice40_dsp.pmg +++ b/techlibs/ice40/ice40_dsp.pmg @@ -23,7 +23,7 @@ match mul endmatch code sigA sigB sigH - auto unextend = [](const SigSpec &sig) { + auto unextend_signed = [](const SigSpec &sig) { int i; for (i = GetSize(sig)-1; i > 0; i--) if (sig[i] != sig[i-1]) @@ -32,8 +32,16 @@ code sigA sigB sigH ++i; return sig.extract(0, i); }; - sigA = param(mul, \A_SIGNED).as_bool() ? unextend(port(mul, \A)) : port(mul, \A); - sigB = param(mul, \B_SIGNED).as_bool() ? unextend(port(mul, \B)) : port(mul, \B); + auto unextend_unsigned = [](const SigSpec &sig) { + int i; + for (i = GetSize(sig)-1; i > 0; i--) + if (sig[i] != SigBit(State::S0)) + break; + ++i; + return sig.extract(0, i); + }; + sigA = param(mul, \A_SIGNED).as_bool() ? unextend_signed(port(mul, \A)) : unextend_unsigned(port(mul, \A)); + sigB = param(mul, \B_SIGNED).as_bool() ? unextend_signed(port(mul, \B)) : unextend_unsigned(port(mul, \B)); SigSpec O; if (mul->type == $mul)