编写乘法器求解算法表达式

作者 : admin 本文共1123个字,预计阅读时间需要3分钟 发布时间: 2024-06-16 共2人阅读

描述

编写一个4bit乘法器模块,并例化该乘法器求解c=12*a+5*b,其中输入信号a,b为4bit无符号数,c为输出。注意请不要直接使用*符号实现乘法功能。

模块的信号接口图如下:

   

编写乘法器求解算法表达式插图

    要求使用Verilog HDL语言实现以上功能,并编写testbench验证模块的功能。

输入描述:

clk:系统时钟信号

rst_n:复位信号,低电平有效

a:输入信号,位宽为4bit

b:输入信号,位宽为4bit

输出描述:

c:输出信号

参考答案

`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] product_1;
	wire [7:0] product_2;
	reg[3:0] mult_1 = 12;
	reg[3:0]  mult_2 = 5;
	mult multiplier_1(
	.clk(clk),
	.rst_n(rst_n),
	.multiplicand(a),
	.multiplier(mult_1),
	.product(product_1)	
	);
	mult multiplier_2(
	.clk(clk),
	.rst_n(rst_n),
	.multiplicand(b),
	.multiplier(mult_2),
	.product(product_2)	
	);		
	assign c =  product_1+product_2;
endmodule

module mult(
	input 				clk 	,   
	input 				rst_n	,
	input	[3:0]		multiplicand,
	input	[3:0]		multiplier	,
 	output	reg	[7:0]	product		
);

wire [7:0]	temp0 ;
wire [7:0]	temp1 ;
wire [7:0]	temp2 ;
wire [7:0]	temp3 ;

assign temp0 = multiplicand[0]? {4'b0, multiplier} : 1'd0;
assign temp1 = multiplicand[1]? {3'b0, multiplier, 1'b0} : 1'd0;
assign temp2 = multiplicand[2]? {2'b0, multiplier, 2'b0} : 1'd0;
assign temp3 = multiplicand[3]? {1'b0, multiplier, 3'b0} : 1'd0;

always @(posedge clk or negedge rst_n) begin 
	if(~rst_n) begin
		product <= 1'd0;
	end 
	else begin
		product <= temp0 + temp1 + temp2 + temp3;

	end
end

		
endmodule

本站无任何商业行为
个人在线分享 » 编写乘法器求解算法表达式
E-->