For signed multipliers, compute sign bit separately...
authorEddie Hung <eddie@fpgeh.com>
Wed, 31 Jul 2019 22:45:41 +0000 (15:45 -0700)
committerEddie Hung <eddie@fpgeh.com>
Wed, 31 Jul 2019 22:45:41 +0000 (15:45 -0700)
techlibs/common/mul2dsp.v

index 678de37967570f435ad2409c09a1ea286263dc9e..b745547a8385e77f16162e87fe0680955c211f6f 100644 (file)
@@ -54,8 +54,22 @@ module \$mul (A, B, Y);
        generate\r
        if (A_SIGNED != B_SIGNED || A_WIDTH <= 1 || B_WIDTH <= 1)\r
                wire _TECHMAP_FAIL_ = 1;\r
-       // NB: A_SIGNED == B_SIGNED == 0 from here\r
-       else if (A_WIDTH >= B_WIDTH)\r
+       // NB: A_SIGNED == B_SIGNED from here\r
+       else if (A_WIDTH < B_WIDTH)\r
+               \$mul #(\r
+                       .A_SIGNED(B_SIGNED),\r
+                       .B_SIGNED(A_SIGNED),\r
+                       .A_WIDTH(B_WIDTH),\r
+                       .B_WIDTH(A_WIDTH),\r
+                       .Y_WIDTH(Y_WIDTH)\r
+               ) _TECHMAP_REPLACE_ (\r
+                       .A(B),\r
+                       .B(A),\r
+                       .Y(Y)\r
+               );\r
+       else if (A_SIGNED && (A_WIDTH > `DSP_A_MAXWIDTH || B_WIDTH > `DSP_B_MAXWIDTH)) begin\r
+               wire _;\r
+               assign Y[Y_WIDTH-1] = A[A_WIDTH-1] ^ B[B_WIDTH-1];\r
                \$__mul #(\r
                        .A_SIGNED(A_SIGNED),\r
                        .B_SIGNED(B_SIGNED),\r
@@ -65,18 +79,19 @@ module \$mul (A, B, Y);
                ) _TECHMAP_REPLACE_ (\r
                        .A(A),\r
                        .B(B),\r
-                       .Y(Y)\r
+                       .Y({_,Y[Y_WIDTH-2:0]})\r
                );\r
+       end\r
        else\r
                \$__mul #(\r
-                       .A_SIGNED(B_SIGNED),\r
-                       .B_SIGNED(A_SIGNED),\r
-                       .A_WIDTH(B_WIDTH),\r
-                       .B_WIDTH(A_WIDTH),\r
+                       .A_SIGNED(A_SIGNED),\r
+                       .B_SIGNED(B_SIGNED),\r
+                       .A_WIDTH(A_WIDTH),\r
+                       .B_WIDTH(B_WIDTH),\r
                        .Y_WIDTH(Y_WIDTH)\r
                ) _TECHMAP_REPLACE_ (\r
-                       .A(B),\r
-                       .B(A),\r
+                       .A(A),\r
+                       .B(B),\r
                        .Y(Y)\r
                );\r
        endgenerate\r
@@ -209,23 +224,27 @@ module \$__mul (A, B, Y);
                                        .B({{sign_headroom{1'b0}}, B[i*(`DSP_B_MAXWIDTH-sign_headroom) +: `DSP_B_MAXWIDTH-sign_headroom]}),\r
                                        .Y(partial[i])\r
                                );\r
-                // TODO: Currently a 'cascade' approach to summing the partial \r
-                //       products is taken here, but a more efficient 'binary\r
-                //       reduction' approach also exists...\r
+                               // TODO: Currently a 'cascade' approach to summing the partial \r
+                               //       products is taken here, but a more efficient 'binary\r
+                               //       reduction' approach also exists...\r
                                assign partial_sum[i] = (partial[i] << i*(`DSP_B_MAXWIDTH-sign_headroom)) + partial_sum[i-1];\r
                        end\r
 \r
-                       \$__mul #(\r
-                               .A_SIGNED(A_SIGNED),\r
-                               .B_SIGNED(B_SIGNED),\r
-                               .A_WIDTH(A_WIDTH),\r
-                               .B_WIDTH(B_WIDTH-(n-1)*(`DSP_B_MAXWIDTH-sign_headroom)),\r
-                               .Y_WIDTH(last_Y_WIDTH)\r
-                       ) mul_last (\r
-                               .A(A),\r
-                               .B(B[B_WIDTH-1 : (n-1)*(`DSP_B_MAXWIDTH-sign_headroom)]),\r
-                               .Y(last_partial)\r
-                       );\r
+                       localparam last_B_WIDTH = B_WIDTH-(n-1)*(`DSP_B_MAXWIDTH-sign_headroom);\r
+                       if (A_SIGNED && B_SIGNED && last_B_WIDTH == 1)\r
+                               assign last_partial = 0;\r
+                       else\r
+                               \$__mul #(\r
+                                       .A_SIGNED(A_SIGNED),\r
+                                       .B_SIGNED(B_SIGNED),\r
+                                       .A_WIDTH(A_WIDTH),\r
+                                       .B_WIDTH(last_B_WIDTH),\r
+                                       .Y_WIDTH(last_Y_WIDTH)\r
+                               ) mul_last (\r
+                                       .A(A),\r
+                                       .B(B[B_WIDTH-1 -: last_B_WIDTH]),\r
+                                       .Y(last_partial)\r
+                               );\r
                        assign partial_sum[n-1] = (last_partial << (n-1)*(`DSP_B_MAXWIDTH-sign_headroom)) + partial_sum[n-2];\r
                        assign Y = partial_sum[n-1];\r
                end\r