verilog入门学习---D8---2020/3/26

一、向量点积乘法器
算法:向量a = (a1, a2, a3, a4); b = (b1, b2, b3, b4),
a·b = a1b1+a2b2+a3b3+a4b4
原理图:
在这里插入图片描述
代码实现:

module vector(a1,a2,a3,a4,b1,b2,b3,b4,out);
	input [3:0] a1,a2,a3,a4,b1,b2,b3,b4;
	output [9:0] out;
	wire [7:0] out1,out2,out3,out4;
	wire [8:0] out5,out6;
	wire [9:0] out;
//四个乘法器
	mul_addtree U1(.a(a1), .b(b1), .out(out1));
	mul_addtree U2(.a(a2), .b(b2), .out(out2));
	mul_addtree U3(.a(a3), .b(b3), .out(out3));
	mul_addtree U4(.a(a4), .b(b4), .out(out4));
//前面的两个加法器
	add U5(.a(out1),.b(out2),.out(out5));
	add U6(.a(out3),.b(out4),.out(out6));
//后面的加法器
	addx U7(.a(out5),.b(out6),.out(out));
endmodule

//adder加法器实现
module add(a,b,out);
	input [7:0]a,b;
	output[8:0]out;
	assign out=a+b;
endmodule

module addx(a,b,out);
	input [8:0]a,b;
	output[9:0]out;
	assign out=a+b;
endmodule

//Mulyiplier乘法器
module mul_addtree(a,b,out);
	input [3:0]a,b;
	output [7:0]out;
	wire [7:0]out;
	wire [7:0]stored0,stored1,stored2,stored3;
	wire [7:0]add01,add23;
//内部延迟编译不过,原因需要后续解答

	assign stored3=b[3]?{1'b0,a,3'b0}:8'b0;
	assign stored2=b[2]?{2'b0,a,2'b0}:8'b0;
	assign stored1=b[1]?{3'b0,a,1'b0}:8'b0;
	assign stored0=b[0]?{4'b0,a}:8'b0;

	assign add01=stored1+stored0;
	assign add23=stored2+stored3;
	assign out=add01+add23;
endmodule
	

简单乘法器算法:乘法竖式的原理,一位一位的乘,之后相加
原理:

         1011
×        1010
---------------
         0000
        1011
       0000
      1011
等价于:
         1011
×        1010
---------------
     00000000
     00010110
     00000000
+    01011000 
---------------

	assign stored0=b[0]?{4'b0,a}:8'b0;
	assign stored1=b[1]?{3'b0,a,1'b0}:8'b0;
	assign stored2=b[2]?{2'b0,a,2'b0}:8'b0;
	assign stored3=b[3]?{1'b0,a,3'b0}:8'b0;

仿真代码:a1=0001,a2=0010,a3=0100,a4=1000,b1=0001,b2=0010,b3=0100,b4=1000
在这里插入图片描述

发布了19 篇原创文章 · 获赞 0 · 访问量 292

猜你喜欢

转载自blog.csdn.net/m0_46493315/article/details/105129340