本文主要是介绍【【二进制 无符号乘 有符号 乘法器 的设计verilog代码+testbench 】】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
二进制 无符号乘 有符号 乘法器 的设计verilog代码+testbench
usmultiplier.v
module usmultiplier #(parameter NUMBER1 = 8 ,parameter NUMBER2 = 8 )(input [NUMBER1-1 : 0] input1 ,input signed [NUMBER2-1 : 0] input2 ,input clk ,input rst_n ,input begin_en ,output reg finish_en ,output reg [NUMBER1+NUMBER2-2 : 0] out);//======================================================================================\// define parameter and internal signal \//======================================================================================\reg [NUMBER1+NUMBER2-2 : 0] out1 ;//==========================================================================================\// next is main code \\//===========================================================================================\\
always@(posedge clk or negedge rst_n)beginif(rst_n == 0)beginout <= 0 ;endelse if(begin_en && finish_en )beginout[NUMBER1+NUMBER2-3:0] <= input1 * input2[NUMBER2-2 : 0] ;out[NUMBER1+NUMBER2-2] <= input2[NUMBER2-1] ;endelseout <= out ;endalways@(posedge clk or negedge rst_n )beginif(rst_n == 0)beginout1 <= 0 ;endelseout1 <= out ;endalways@(posedge clk or negedge rst_n)beginif(rst_n == 0)beginfinish_en <= 1'b1 ;endelse if(out != out1)beginfinish_en <= 1'b1 ;endelsefinish_en <= 1'b0 ;endendmodule
usmultiplier_tb.v
module uumultiplier_tb #(parameter NUMBER1 = 4 ,parameter NUMBER2 = 4 );reg [NUMBER1-1 : 0] input1 ;
reg [NUMBER2-1 : 0] input2 ;
reg clk ;
reg rst_n ;
reg begin_en ;
wire finish_en ;
wire [(NUMBER1)+(NUMBER2)-2 : 0] out ;uumultiplier#(.NUMBER1 ( NUMBER1 ),.NUMBER2 ( NUMBER2 )
)u_uumultiplier(.input1 ( input1 ),.input2 ( input2 ),.clk ( clk ),.rst_n ( rst_n ),.begin_en ( begin_en ),.finish_en ( finish_en ),.out ( out )
);always #5 clk = ~clk ;initial begin clk = 0 ;rst_n = 0 ;input1 = 2'b00 ;input2 = 2'b00 ;begin_en = 1 ;#20rst_n = 1 ;input1 = 4'b0001 ; input2 = 4'b1100 ;#20input1 = 4'b0010; input2 = 4'b1111 ;#20 input1 = 4'b0011 ; input2 = 4'b1100 ;#20input1 = 4'b0100 ; input2 = 4'b0100 ;end
endmodule
这篇关于【【二进制 无符号乘 有符号 乘法器 的设计verilog代码+testbench 】】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!