题解 | #编写乘法器求解算法表达式#

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

https://www.nowcoder.com/practice/c414335a34b842aeb9960acfe5fc879f

`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] c1,c2;
	reg [7:0] c_r;

	mul4 inst0(
		.clk		(clk	),
		.rst_n		(rst_n	),
		.aa			(a		),
		.bb			(12		),
		.cc			(c1		)
);

	mul4 inst1(
		.clk		(clk	),
		.rst_n		(rst_n	),
		.aa			(b		),
		.bb			(5		),
		.cc			(c2		)
);

	assign c = c1 + c2;

endmodule

module mul4(
	input clk,
	input rst_n,
	input [3:0] aa,
	input [3:0] bb,
	output [7:0] cc
);

	wire [7:0] mid_mul [3:0];
	reg [7:0] add0,add1;
	genvar i;
	generate 
		for(i=0;i<4;i=i+1)begin:mul4_loop
			assign mid_mul[i] = aa[i] ? bb<<i : 'd0;
		end
	endgenerate

	always @(posedge clk or negedge rst_n) begin
		if(rst_n == 1'b0)
			add0 <= 8'd0;
		else
			add0 <= mid_mul[0] + mid_mul[1];
	end

	always @(posedge clk or negedge rst_n) begin
		if(rst_n == 1'b0)
			add1 <= 8'd0;
		else
			add1 <= mid_mul[2] + mid_mul[3];
	end

	assign cc = add0 + add1;

endmodule

全部评论

相关推荐

评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务