当前位置:
文档之家› Verilog数字系统设计4
Verilog数字系统设计4
2)移位相加乘法器
在不使用嵌入式硬件乘法器的情况下,移位 相加乘法器相比于并行乘法器更节省资源, 这随操作数位数的增加而越发明显。而其缺 点则在于,由于需要进行逐步移位,因而需 要一定的时间来完成两数相乘操作。
深圳大学信息工程学院
3)查找表乘法器
module Mult3(a,b,clk,outcome); input [3:0] a,b; input clk; output [7:0] outcome; wire [7:0] din; assign din[7:4]=a; assign din[3:0]=b; LMP_ROM u1(.address(din),.clock(clk),.q(outcome)); Endmodule
各位的进位输出信号只 与两个相加数有关,而与 低位进位信号无关。
S1
S0
S2
C
si c i c i 1
进位 电路
si c i c i 1
进位 电路
si c i c i 1
si c i c i 1
进位 电路
A3
B3
A 2 B2
A1
B1
A 0 B0
1-1-1-1 MASH实 现电路
深圳大学信息工程学院
深圳大学信息工程学院
加法树乘法器结合了移位相加乘法器 和查找表乘法器的有点,能在一个时钟周 期内完成两数相乘,提高了运算速度。但 是加法器乘法器需要增加若干个寄存器以 暂存数据,增加了芯片资源耗用。
深圳大学信息工程学院
3)查找表乘法器
LMP_ROM模块是使用Quartus II自带宏模块自动生 成的,其中包含了一个256字节的ROM存储器。 查找表乘法器将乘积结果直接存放在存储器中,将操 作数作为地址访问存储器,得到的输出数据就是乘法的结 果。查找表乘法器速度只局限于是使用的存储器的存取速 度,查找表的规模随操作数位数的增加而迅速增大。如上 述例子所示,实现四位操作数乘法需要ROM大小为256字 节;若用查找表方式实现八位操作数乘法,则需要存储器 大小须增至256K字节。因此,查找表法只适合于操作数 位数比较小的情况。
加法器
module add_4(x,y,sum,c); input [3:0] x,y; output[3:0] sum; 拼接 output c; assign {c,sum}=x+y; endmodule
6
加法器
多位加法器
串行进位加法器 超前进位加法器 pipelined加法器
S2
S1
S0
用一位全加器组成四位全加器
module ADDER4BIT ( Ain, Bin, SUM, OVF); input [3:0] Ain, Bin; output [3:0] SUM; wire [2:0] CY; outputOVF; FullAdder U0 (Ain[0], Bin[0], 0, SUM[0], CY[0]); FullAdder U1 (Ain[1], Bin[1], CY[0], SUM[1], CY[1]); FullAdder U2 (Ain[2], Bin[2], CY[1], SUM[2], CY[2]); FullAdder U3 (Ain[3], Bin[3], CY[2], SUM[3], OVF); endmodule
25
case(addr) 3’b000:mout=in1; 3’b001:mout=in2; 3’b010:mout=in3; 3’b011:mout=in4; 3’b100:mout=in5; 3’b101:mout=in6; 3’b110:mout=in7; 3’b111:mout=in8; endcase else mout=0; end endmodule
数字系统设计 (Verilog)
——简单的纯组合逻辑模块
深圳大学信息工程学院
本章提要
加法器 乘法器 比较器 多路器 总线和总线操作 流水线设计技术
深圳大学信息工程学院
加法器
Ai Bi C i-1 S i 0 0 0 0 Si =1 Ci1 真 0 0 1 1 & 0 1 0 1 Ci 值 0 1 1 0 & ≥1 1 0 0 1 & 表 1 0 1 0 1 1 0 0 表达式: 1 1 1 1 Si Ai BiCi1 Ai BiCi1 Ai BiCi1 Ai BiCi1 Ai Bi Ci1
并行乘法器可以看作是纯 组合逻辑电路,依靠组合 逻辑实现两数相乘,这种 方法能在输入数据改变时 立即得到相乘结果,延时 很短,但是耗用的资源随 操作数位数的增加而迅速 变多。并行乘法器实现代 码非常简短,适用于器件 内有嵌入式硬件乘法器的 情况。
13
2)移位相加乘法器
14
Top Module
module Mult2(DataA,LA,DataB,LB,clk,reset,start,p,Done); input [7:0] DataA,DataB; input LA,LB,clk,reset,start; output [15:0] p; output Done; wire EA,EB,EP,ER,psel,qb,zb; wire [15:0] qa,sum; multshift_cntrl f0(.clock(clk),.reset(reset),.s(start),.z(zb),.b0(qb),.ea(EA),.eb(EB), .ep(EP),.psel(psel),.done(Done)); shifta f1(.r(DataA),.l(LA),.e(EA),.clk(clk),.q(qa)); shiftb f2(.r(DataB),.l(LB),.e(EB),.clk(clk),.q0(qb),.z(zb)); sum f3(.a(qa),.p(p),.psel(psel),.sum(sum)); reg16 f4(.r(sum),.clk(clk),.rst(reset),.e(EP),.q(p)); endmodule 深圳大学信息工程学院
Ci Ai BiCi1 Ai BiCi1 Ai BiCi1 Ai BiCi1
Ai Bi
=1
Ci 0 0 0 1 0 1 1 1
Ai Bi Bi Ci-1 Ai Ci-1
深圳大学信息工程学院
用一位全加器组成四位全加器
module FullAdder (A, B, Cin, SUM, Cout); input A, B, Cin; output SUM, Cout; assign SUM = A ^ B ^ Cin; assign Cout = (A & B) | (A & Cin) | (B & Cin); endmodule
17
4)加法树乘法器
module Mult4(outcome,a,b,clk); input [7:0] a,b; input clk; output wire [15:0]outcome; wire [14:0] out1,c1; wire [12:0] out2; wire [10:0] out3,c2; wire [8:0] out4; reg [14:0] temp0; reg [13:0] temp1; reg [12:0] temp2; reg [11:0] temp3; reg [10:0] temp4; reg [9:0] temp5; reg [8:0] temp6; reg [7:0] temp7; function [7:0] mult8x1; input [7:0] operand; input sel; begin mult8x1=(sel)?(operand):8'b0; end endfunction always@(posedge clk) begin temp7<=mult8x1(a,b[0]); temp6<=((mult8x1(a,b[1]))<<1); temp5<=((mult8x1(a,b[2]))<<2); 深圳大学信息工程学院
temp4<=((mult8x1(a,b[3]))<<3); temp3<=((mult8x1(a,b[4]))<<4); temp2<=((mult8x1(a,b[5]))<<5); temp1<=((mult8x1(a,b[6]))<<6); temp0<=((mult8x1(a,b[7]))<<7); end assign out1=temp0+temp1; assign out2=temp2+temp3; assign out3=temp4+temp5; assign out4=temp6+temp7; assign c1=out1+out2; assign c2=out3+out4; assign outcome=c1+c2; 深圳大学信息工程学院 Endmodule
深圳大学信息工程学院
比较器
比较x、y的大小,位数由参数决定。 module compare_n(x,y,xgy,xsy,xey) ; parameter width = 8; input[width-1:0] x , y ; output xgy,xsy,xey; reg xgy,xsy,xey; always @(x or y) begin if(x= = y) xey=1; else xey=0; if(x>y) begin xgy=1;xsy=0;end else if(x<y) begin xgy=0;xsy=1;end end endmodule 23
Pipelined 加法器
乘法器
分类: 并行乘法器 移位相加乘法器 查找表乘法器 加法树乘法器
11
深圳大学信息工程学院