可综合风格的Verilog HDL模块实例

可综合风格的Verilog HDL模块实例:

1. 组合逻辑电路设计实例

[例1] 八位带进位端的加法器的设计实例(利用简单的算法描述)

module adder_8(cout,sum,a,b,cin);
	output cout;
	output [7:0] sum;
	input cin;
	input[7:0] a,b;
	assign {cout,sum}=a+b+cin;//位拼接
endmodule

[例2]指令译码电路的设计实例
(利用电平敏感的always块来设计组合逻辑)

//操作码的宏定义
`define plus 3'd0
`define minus 3'd1
`define band 3'd2
`define bor 3'd3
`define unegate 3'd4
module alu(out,opcode,a,b);
	output [7:0] out;
	input [2:0] opcode;
	input [7:0] a,b;
	reg [7:0] out;
		always @(opcode or a or b)
		//用电平敏感的always块描述组合逻辑
			begin
			case(opcode)
				//算术运算
				`plus: out=a+b;
				`minus: out=a-b;
				//位运算
				`band: out=a&b;
				`bor: out=a|b;
				//单目运算
				`unegate: out=~a;
			default:out=8'hx;
		endcase
	end
endmodule

[例3].利用task和电平敏感的always块设计比较后重组信号的组合逻辑.

module sort4(ra,rb,rc,rd,a,b,c,d);
	parameter t=3;
	output [t:0] ra, rb, rc, rd;
	input [t:0] a, b, c, d;
	reg [t:0] ra, rb, rc, rd;
		always @(a or b or c or d)
//用电平敏感的always块描述组合逻辑
			begin
				reg [t:0] va, vb, vc, vd;
				{va,vb,vc,vd}={a,b,c,d};
				sort2(va,vc);
				sort2(vb,vd);
				sort2(va,vb);
				sort2(vc,vd);
				sort2(vb,vc);
				{ra,rb,rc,rd}={va,vb,vc,vd};
			end
			
	task sort2;
		inout [t:0] x, y;
		reg [t:0] tmp;
			if( x > y )
				begin
					tmp = x;
					x = y;
					y = tmp;
				end
		endtask
endmodule

[例4]. 比较器的设计实例(利用赋值语句设计组合逻辑)

module compare(equal,a,b);
parameter size=1;
output equal;
input [size-1:0] a, b;
	assign equal =(a==b)? 1 : 0;
endmodule

[例5]. 3-8译码器设计实例(利用赋值语句设计组合逻辑)

module decoder(out,in);
output [7:0] out;
input [2:0] in;
	assign out = 1'b1<<in;
/**** 把最低位的1左移 in(根据从in口输入的值)位,
并赋予out ****/
endmodule

[例6]. 8-3编码器的设计实例
编码器设计方案之一:

module encoder1(none_on,out,in);
	output none_on;
	output [2:0] out;
	input [7:0] in;
	reg [2:0] out;
	reg none_on;
	always @(in)
		begin: local
			integer i;
			out = 0;
			none_on = 1;
/*returns the value of the highest bit
number turned on*/
		for( i=0; i<8; i=i+1 )
			begin
				if( in[i] )
					begin
						out = i;
						none_on = 0;
					end
			end
		end
endmodule

编码器设计方案之二:

module encoder2 ( none_on, out2, out1, out0, h, g, f,
	e, d, c, b, a);
	input h, g, f, e, d, c, b, a;
	output none_on, out2, out1, out0;
	wire [3:0] outvec;
	assign outvec= h? 4'b0111 : g? 4'b0110 : f? 4'b0101:
	e? 4'b0100 : d? 4'b0011 :c? 4'b0010 : b? 4'b0001:
	a? 4'b0000 : 4'b1000;
	assign none_on = outvec[3];
	assign out2 = outvec[2];
	assign out1 = outvec[1];
	assign out0 = outvec[0];
endmodule

编码器设计方案之三:

module encoder3 (none_on, out2, out1, out0, h, g,
						f, e, d, c, b, a);
		input h, g, f, e, d, c, b, a;
		output out2, out1, out0;
		output none_on;
		reg [3:0] outvec;
		assign {none_on,out2,out1,out0} = outvec;
	always @( a or b or c or d or e or f or g or h)
		begin
			if(h) outvec=4'b0111;
			else if(g) outvec=4'b0110;
			else if(f) outvec=4'b0101;
			else if(e) outvec=4'b0100;
			else if(d) outvec=4'b0011;
			else if(c) outvec=4'b0010;
			else if(b) outvec=4'b0001;
			else if(a) outvec=4'b0000;
			else outvec=4'b1000;
		end
endmodule

[例7]. 多路器的设计实例。
使用连续赋值、 case语句或if-else语句可以生成多路器电路, 如果条件语句(case或if-else)中分支条件是互斥的话,综合器能自动地生成并行的多路器。
多路器设计方案之一:

modul emux1(out, a, b, sel);
	output out;
	input a, b, sel;
		assign out = sel? A : b;
endmodule

多路器设计方案之二:

module mux2( out, a, b, sel);
	output out;
	input a, b, sel;
	reg out;
//用电平触发的always块来设计多路器的组合逻辑
		always @( a or b or sel )
		begin
/*检查输入信号sel的值,如为1,输出out为a,如为0,
输出out为b.*/
		case( sel )
			1'b1: out = a;
			1'b0: out = b;
		default: out = 'bx;
	endcase
	end
endmodule

多路器设计方案之三:

module mux3( out, a, b, sel);
	output out;
	input a, b, sel;
	reg out;
	always @( a or b or sel )
		begin
			if( sel )
			out = a;
			else
			out = b;
		end
	endmodule

[例8]. 奇偶校验位生成器设计实例

module parity( even_numbits,odd_numbits,input_bus);
	output even_numbits, odd_numbits;
	input [7:0] input_bus;
		assign odd_numbits = ^input_bus;
		assign even_numbits = ~odd_numbits;
	endmodule

[例9]. 三态输出驱动器设计实例
(用连续赋值语句建立三态门模型)
三态输出驱动器设计方案之一:

module trist1( out, in, enable);
output out;
input in, enable;
	assign out = enable? in: 'bz;
endmodule

三态输出驱动器设计方案之二:

module trist2( out, in, enable );
output out;
input in, enable;
	//bufif1是 一个 Verilog门级原语(primitive)
	bufif1 mybuf1(out, in, enable);
endmodule

[例10]. 三态双向驱动器设计实例

module bidir(tri_inout, out, in, en, b);
inout tri_inout;
output out;
input in, en, b;
	assign tri_inout = en? In : 'bz;
	assign out = tri_inout ^ b;
endmodule
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值