4位正数乘法器
- 格式:docx
- 大小:19.45 KB
- 文档页数:2
module mult4(ain,bin,clk,rst_n,cout);
input clk;
input rst_n;
input [3:0] ain, bin;
output [7:0] cout;
reg [7:0] cout;
reg [7:0] stored0;
reg [7:0] stored1;
reg [7:0] stored2;
reg [7:0] stored3;
reg [7:0] add01;
reg [7:0] add23;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
cout <= 0;
stored0 <= 0;
stored1 <= 0;
stored2 <= 0;
stored3 <= 0;
add01 <= 0;
add23 <= 0;
end
else
begin
stored0 <= bin[0]? {4'b0, ain} : 8'b0;
stored1 <= bin[1]? {3'b0, ain, 1'b0} : 8'b0;
stored2 <= bin[2]? {2'b0, ain, 2'b0} : 8'b0;
stored3 <= bin[3]? {1'b0, ain, 3'b0} : 8'b0;
add01 <= stored1 + stored0;
add23 <= stored3 + stored2;
cout <= add01 + add23;
end
end
endmodule
测试模块:
`timescale 1ns / 1ps
module mult4_test;
reg clk;
reg rst_n;
reg [3:0] ain;
reg [3:0] bin;
wire [7:0] cout;
mult4 mult (.ain(ain),.bin(bin),.clk(clk),.rst_n(rst_n),.cout(cout));
always #50 clk=~clk;
initial
begin
clk = 0;
rst_n = 0;
ain = 0;
bin = 0;
#100 rst_n=1;
#100 ain=4'b0011;
bin=4'b0100;
#2000 $stop;
end
endmodule