使用门级图了解二进制乘法器

时间:2019-01-18 04:21:43

标签: binary verilog fpga multiplication synthesis

我在理解下面的code(bimpy.v)进行无符号2位乘法运算时遇到问题。

编辑:添加了我的一位朋友的评论:以下修改具有更少逻辑!

o_r <= (i_a[0] ? i_b : 2'b0) + ((i_a[1] ? i_b : 2'b0) << 1);

bimpy.v中的两个信号(w_r和c)的用途是什么?

assign  w_r =  { ((i_a[1])?i_b:{(BW){1'b0}}), 1'b0 }
            ^ { 1'b0, ((i_a[0])?i_b:{(BW){1'b0}}) };

assign  c = { ((i_a[1])?i_b[(BW-2):0]:{(BW-1){1'b0}}) }
        & ((i_a[0])?i_b[(BW-1):1]:{(BW-1){1'b0}});

代码与2位乘2位二进制乘法器gate-level diagram不匹配,如果有误,请纠正我

2-bit by 2-bit binary multiplier

我还附加了bimpy.v的工作波形,用于一个简单的2x2无符号乘法器。

bimpy.v waveform

我还为bimpy.v生成了门级表示图。

gate-level representation of bimpy.v

 ////////////////////////////////////////////////////////////////////////////////
//
// Filename:    bimpy
//
// Project: A multiply core generator
//
// Purpose: An unsigned 2-bit multiply based upon the fact that LUT's allow
//      6-bits of input, but a 2x2 bit multiply will never carry more
//  than one bit.  While this multiply is hardware independent, it is
//  really motivated by trying to optimize for a specific piece of
//  hardware (Xilinx-7 series ...) that has 4-input LUT's with carry
//  chains.
//
// Creator: Dan Gisselquist, Ph.D.
//      Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2015,2017-2019, Gisselquist Technology, LLC
//
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of  the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program.  If not, see <http://www.gnu.org/licenses/> for a
// copy.
//
// License: GPL, v3, as defined and found on www.gnu.org,
//      http://www.gnu.org/licenses/gpl.html
//
//
////////////////////////////////////////////////////////////////////////////////
module  bimpy(i_clk, i_reset, i_ce, i_a, i_b, o_r);
    parameter   BW=2, LUTB=2;
    input               i_clk, i_reset, i_ce;
    input       [(LUTB-1):0]    i_a;
    input       [(BW-1):0]  i_b;
    output  reg [(BW+LUTB-1):0] o_r;

    wire    [(BW+LUTB-2):0] w_r;
    wire    [(BW+LUTB-3):1] c;

    assign  w_r =  { ((i_a[1])?i_b:{(BW){1'b0}}), 1'b0 }
                ^ { 1'b0, ((i_a[0])?i_b:{(BW){1'b0}}) };
    assign  c = { ((i_a[1])?i_b[(BW-2):0]:{(BW-1){1'b0}}) }
            & ((i_a[0])?i_b[(BW-1):1]:{(BW-1){1'b0}});

    initial o_r = 0;
    always @(posedge i_clk)
        if (i_reset)
            o_r <= 0;
        else if (i_ce)
            o_r <= w_r + { c, 2'b0 };

endmodule

1 个答案:

答案 0 :(得分:1)

关于MUX的说明

回想一下?描述了一个多路复用器(MUX),因此声明:

out = sel ? x : y

在门级实现中等效于:

out = (sel & x) | (~sel & y)

(当sel=1out <= xsel=0out <= y时)

如果y=0,则MUX减少为xsel之间的AND:out = (sel & x) | (~sel & 0) = sel & x

派生w_r

假设BW=2LUTB=2 w_r是4位信号。让我们分解一下:

w_r = w_rL ^ x_rR

w_rL = { ((i_a[1])?i_b:{(BW){1'b0}}), 1'b0 }

w_rR = { 1'b0, ((i_a[0])?i_b:{(BW){1'b0}}) }

请注意,MUX的两个“ else”值如何都为零,因此将MUX简化为AND,如上面的“ Note”中所述:

w_rL = { BW{i_a[1]} & i_b, 1'b0 } = { A1 & B1, A1 & B0, 0 }

w_rR = { 1'b0, BW{i_a[0]} & i_b } = { 0, A0 & B1, A0 & B0}

为了简化表示,我替换了i_a = {A1, A0}i_b = {B1, B0}。最终,通过按位对它们进行XOR:

  • w_r[0] = 0 ^ (A0 & B0) = A0 & B0

  • w_r[1] = (A1 & B0) ^ (A0 & B1)

  • w_r[2] = (A1 & B1) ^ 0 = A1 & B1

  • w_r[3] = 0(隐式设置)

推导c

类似地,对于1位c信号:

c = cL & cR

cL = i_a[1] ? i_b[(BW-2):0]:{(BW-1){1'b0}} = {A1 & B0}

cR = i_a[0] ? i_b[(BW-1):1]:{(BW-1){1'b0}} = {A0 & B1)

最终:

  • c = {A1 & B0 & A0 & B1}

派生o_r

如果我们分解o_r位:

  • o_r[0] = 0 + w_r[0] = A0 & B0

  • o_r[1] = 0 + w_r[1] = (A1 & B0) ^ (A0 & B1)

  • o_r[2] = c + w_r[2] = (A1 & B0 & A0 & B1) + (A1 & B1)-如果我们将它们相加,则总和是它们的XOR,而进位是它们的AND,即:o_r[2] = (A1 & B0 & A0 & B1) ^ (A1 & B1)

  • o_r[3] = <carry from o_r[2] addition> = A1 & B0 & A0 & B1 & A1 & B1 = A1 & B0 & A0 & B1(请记住,与自己进行AND运算等于自己,即x & x = x

门级图输出

您的门级图描述了以下等式:

C0 = A0 & B0=o_r[0]

C1 = (A0 & B1) ^ (A1 & B0)=o_r[1]

C2 = (A0 & B1 & A1 & B0) ^ (A1 & B1)(总金额为=o_r[2]

C3 = (A0 & B1 & A1 & B0) & (A1 & B1) = A0 & B1 & A1 & B0=o_r[3]携带)

为什么实施如此奇怪?!

代码注释表明乘法器单元是为特定的FPGA架构构建的,看起来原始编码器的意图是将每个乘法器单元装入该架构的单个LUT中。所以我敢打赌,原来的编码器试图“引导”一个旧的,笨拙的工具,以FPGA有效的方式构建乘法器,而这通常不是门级有效的方式。我认为,这种“手动” RTL级优化对于当今的EDA工具毫无用处(希望如此!)。