编写一个4bit乘法器模块,并例化该乘法器求解c=12*a+5*b,其中输入信号a,b为4bit无符号数,c为输出。注意请不要直接使用*符号实现乘法功能。
模块的信号接口图如下:
要求使用Verilog HDL语言实现以上功能,并编写testbench验证模块的功能。
编写一个4bit乘法器模块,并例化该乘法器求解c=12*a+5*b,其中输入信号a,b为4bit无符号数,c为输出。注意请不要直接使用*符号实现乘法功能。
模块的信号接口图如下:
要求使用Verilog HDL语言实现以上功能,并编写testbench验证模块的功能。
clk:系统时钟信号rst_n:复位信号,低电平有效a:输入信号,位宽为4bitb:输入信号,位宽为4bit
c:输出信号
`timescale 1ns/1ns module calculation( input clk, input rst_n, input [3:0] a, input [3:0] b, output [8:0] c ); reg [7 : 0] r_a_result ; reg [7 : 0] r_b_result ; reg [3 : 0] r_a ; reg [3 : 0] r_b ; assign c = r_a_result + r_b_result; always @(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n)begin r_a <= 'd0; r_b <= 'd0; end else begin r_a <= a; r_b <= b; end end always @(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) r_a_result <= 'd0; else r_a_result <= (r_a << 2) + (r_a << 3); end always @(posedge clk&nbs***bsp;negedge rst_n) begin if(!rst_n) r_b_result <= 'd0; else r_b_result <= r_b + (r_b << 2); end endmodule
`timescale 1ns/1ns module calculation ( input clk , input rst_n , input [3:0] a , input [3:0] b , output [8:0] c ); wire [7:0] sum_1; wire [7:0] sum_2; mul_4 mul_4_inst_1( .clk (clk) , .rst_n (rst_n) , .data_1 (a) , .data_2 (4'd12) , .mul (sum_1) ); mul_4 mul_4_inst_2( .clk (clk) , .rst_n (rst_n) , .data_1 (b) , .data_2 (4'd5) , .mul (sum_2) ); assign c = sum_1 + sum_2; endmodule module mul_4( input clk , input rst_n , input [3:0] data_1 , input [3:0] data_2 , output reg [7:0] mul ); reg [7:0] sum [3:0] ; always @(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) sum[3] <= 0; else if(data_2[3]) sum[3] <= {data_1,3'b0}; else sum[3] <= 8'd0; always @(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) sum[2] <= 0; else if(data_2[2]) sum[2] <= {data_1,2'b0}; else sum[2] <= 8'd0; always @(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) sum[1] <= 0; else if(data_2[1]) sum[1] <= {data_1,1'b0}; else sum[1] <= 8'd0; always @(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) sum[0] <= 0; else if(data_2[0]) sum[0] <= data_1; else sum[0] <= 8'd0; always @(posedge clk&nbs***bsp;negedge rst_n) if(!rst_n) mul <= 0; else mul <= sum[3] + sum[2] + sum[1] + sum[0]; endmodule
`timescale 1ns/1ns module calculation( input clk, input rst_n, input [3:0] a, input [3:0] b, output [8:0] c ); reg [8:0] a_reg_shift8; reg [8:0] a_reg_shift4; reg [8:0] b_reg_shift4; reg [8:0] b_reg_shift1; reg [8:0] a_reg; reg [8:0] b_reg; //一级流水线 always @(posedge clk&nbs***bsp;negedge rst_n) begin if (!rst_n) begin a_reg_shift8 <= 0; a_reg_shift4 <= 0; b_reg_shift4 <= 0; b_reg_shift1 <= 0; end else begin a_reg_shift8 <= a << 3; a_reg_shift4 <= a << 2; b_reg_shift4 <= b << 2; b_reg_shift1 <= b; end end //二级流水线 always @(posedge clk&nbs***bsp;negedge rst_n) begin if (!rst_n) begin a_reg <= 0; b_reg <= 0; end else begin a_reg <= a_reg_shift8 + a_reg_shift4; b_reg <= b_reg_shift4 + b_reg_shift1; end end assign c = a_reg + b_reg; endmodule
`timescale 1ns/1ns module calculation( input clk, input rst_n, input [3:0] a, input [3:0] b, output reg [8:0] c ); wire [8:0] c1,c2; bit_4 b1( .clk(clk), .rst_n(rst_n), .a(a), .b(4'd12), .c(c1) ) ; bit_4 b2( .clk(clk), .rst_n(rst_n), .a(b), .b(4'd5), .c(c2) ) ; always@(posedge clk&nbs***bsp;negedge rst_n) if(~rst_n) c<=0; else c<=c1+c2; endmodule module bit_4( input clk, input rst_n, input [3:0] a, input [3:0] b, output reg [8:0] c ); wire [8:0] co[3:0]; assign co[0]=b[0]?a:0; assign co[1]=b[1]?{4'b0,a,1'b0}:0; assign co[2]=b[2]?{3'b0,a,2'b0}:0; assign co[3]=b[3]?{2'b0,a,3'b0}:0; //assign c=co[0]+co[1]+co[2]+co[3]; always@(posedge clk&nbs***bsp;negedge rst_n) if(~rst_n) c<=0; else c<=co[0]+co[1]+co[2]+co[3]; endmodule