eric2013 发表于 2013-1-26 14:03:49

组合逻辑电路(5)----- 算数运算(乘法器)

【例5.16】用for 语句实现2个8位数相乘
module mult_for(outcome,a,b);
parameter size=8;
input a,b;                   //两个操作数
output outcome;      //结果
reg outcome;
integer i;
always @(a or b)
    begin
outcome=0;
for(i=1; i<=size; i=i+1)      //for语句
if(b)outcome=outcome +(a << (i-1));
    end
endmodule
【例5.17】用repeat实现8位二进制数的乘法
module mult_repeat(outcome,a,b);
parameter size=8;
input a,b;
output outcome;
reg temp_a,outcome;
reg temp_b;
always @(a or b)
begin
outcome=0;
temp_a=a;
temp_b=b;
repeat(size)         //repeat语句,size为循环次数
begin
if(temp_b)      //如果temp_b的最低位为1,就执行下面的加法
outcome=outcome+temp_a;
temp_a=temp_a<<1;      //操作数a左移一位
【例6.6】阶乘运算函数
module funct(clk,n,result,reset);
output result;
input n;
input reset,clk;
reg result;
always @(posedge clk)          //在clk的上升沿时执行运算
begin
if(!reset)result<=0;      //复位
elsebegin
result <= 2 * factorial(n); //调用factorial函数
end
end

function factorial;      //阶乘运算函数定义(注意无端口列表)
input opa;               //函数只能定义输入端,输出端口为函数名本身
reg i;
begin
factorial = opa ? 1 : 0;
for(i= 2; i <= opa; i = i+1)   //该句若要综合通过,opa应赋具体的数值
factorial = i* factorial;       //阶乘运算
end

function factorial;      //阶乘运算函数定义(注意无端口列表)
input opa;               //函数只能定义输入端,输出端口为函数名本身
reg i;
begin
factorial = opa ? 1 : 0;
for(i= 2; i <= opa; i = i+1)   //该句若要综合通过,opa应赋具体的数值
factorial = i* factorial;       //阶乘运算
end
endfunction
endmodule

eric2013 发表于 2013-1-26 14:09:27

【例9.30】乘累加器(MAC)代码
module MAC(out,opa,opb,clk,clr);
output out;
input opa,opb;
input clk,clr;
wire sum;
reg out;

function mult;       //函数定义,mult函数完成乘法操作
input opa,opb;      //函数只能定义输入端,输出端口为函数名本身
reg result;
integer i;

begin
result = opa? opb : 0;
for(i= 1; i <= 7; i = i+1)
begin
    if(opa==1)result=result+(opb<<(i-1));
end
mult=result;
end
endfunction

assign sum=mult(opa,opb)+out;

always @(posedge clk or posedge clr)
    begin
if(clr)out<=0;
else    out<=sum;
    end
endmodule

eric2013 发表于 2013-1-26 14:25:41

【例12.4】8位并行乘法器
module mult(outcome,a,b);
parameter size=8;
input a,b;          //两个操作数
output outcome;      //结果
assign outcome=a*b;               //乘法运算符
endmodul
【例12.5】4×4查找表乘法器
module mult4x4(out,a,b,clk);
output out;
input a,b;
input clk;
reg out;
reg firsta,firstb;
reg seconda,secondb;
wire outa,outb,outc,outd;
always @(posedge clk)
begin
firsta = a;seconda = a;
firstb = b;secondb = b;
end

lookupm1(outa,firsta,firstb,clk),
   m2(outb,firsta,secondb,clk),
   m3(outc,seconda,firstb,clk),
         m4(outd,seconda,secondb,clk);    //模块调用

always @(posedge clk)
begin
   out = (outa << 4) + (outb << 2) + (outc << 2) + outd;
end
endmodule

module lookup(out,a,b,clk);             //用查找表方式实现2×2乘法
output out;
input a,b;
input clk;
reg out;
reg address;
always @(posedge clk)
begin
   address = {a,b};
   case(address)
   4'h0 : out = 4 'b0000;
   4'h1 : out = 4'b0000;
   4'h2 : out = 4'b0000;
   4'h3 : out = 4'b0000;
   4'h4 : out = 4'b0000;
   4'h5 : out = 4'b0001;
   4'h6 : out = 4'b0010;
   4'h7 : out = 4'b0011;
   4'h8 : out = 4'b0000;
   4'h9 : out = 4'b0010;
   4'ha : out = 4'b0100;
   4'hb : out = 4'b0110;
   4'hc : out = 4'b0000;
   4'hd : out = 4'b0011;
   4'he : out = 4'b0110;
   4'hf : out = 4'b1001;
   default : out='bx;
   endcase
end
endmodule

eric2013 发表于 2013-1-26 14:53:17

【例12.6】8位加法树乘法器
module add_tree(out,a,b,clk);
output out;
input a,b;
input clk;
wire out;
wire out1,c1;
wire out2;
wire out3,c2;
wire out4;
reg temp0;
reg temp1;
reg temp2;
reg temp3;
reg temp4;
reg temp5;
reg temp6;
reg temp7;

function mult8x1;      //该函数实现8×1乘法
input operand;
input sel;
begin
mult8x1= (sel) ? (operand) : 8'b00000000;
例12.6】8位加法树乘法器
module add_tree(out,a,b,clk);
output out;
input a,b;
input clk;
wire out;
wire out1,c1;
wire out2;
wire out3,c2;
wire out4;
reg temp0;
reg temp1;
reg temp2;
reg temp3;
reg temp4;
reg temp5;
reg temp6;
reg temp7;

function mult8x1;      //该函数实现8×1乘法
input operand;
input sel;
begin
mult8x1= (sel) ? (operand) : 8'b00000000;
程序文本
- 76 -
end
endfunction

always @(posedge clk)      //调用函数实现操作数b 各位与操作数a的相乘
begin
temp7<=mult8x1(a,b);
temp6<=((mult8x1(a,b))<<1);
temp5<=((mult8x1(a,b))<<2);
temp4<=((mult8x1(a,b))<<3);
temp3<=((mult8x1(a,b))<<4);
temp2<=((mult8x1(a,b))<<5);
temp1<=((mult8x1(a,b))<<6);
temp0<=((mult8x1(a,b))<<7);
end

assignout1 = temp0 + temp1;    //加法器树运算
assignout2 = temp2 + temp3;
assignout3 = temp4 + temp5;
assignout4 = temp6 + temp7;
assignc1 = out1 + out2;
assignc2 = out3 + out4;
assignout = c1 + c2;

endmodule
页: [1]
查看完整版本: 组合逻辑电路(5)----- 算数运算(乘法器)