数字电路应用之Verilog HDL语言程序经典例题 (Quartus II软件编程)
一:2线4线译码器:
module tom(a,b,y); input a,b; output [3:0] y; wire na,nb; not (na,a); not (nb,b);
and (y[0],na,nb); and (y[1],na,b); and (y[2],a,nb); and (y[3],a,b); endmodule
二:三输入表决器:
module add(a2,a1,a0,y); input a2,a1,a0; output y;
assign y=((a1&a0)|(a2&a1)|(a2&a0));
endmodule
三:3线8线译码器:
module fulladd(a2,a1,a0,y);
input a2,a1,a0; output [7:0] y;
assign y[0]= ~( ~a2 & ~a1 & ~a0); assign y[1]= ~( ~a2 & ~a1 & a0); assign y[2]= ~( ~a2 & a1 & ~a0); assign y[3]= ~( ~a2 & a1 & a0); assign y[4]= ~( a2 & ~a1 & ~a0); assign y[5]= ~( a2 & ~a1 & a0); assign y[6]= ~( a2 & a1 & ~a0); assign y[7]= ~( a2 & a1 & a0);
endmodule
四:BIN2BCD码制转换:
module fulladd(y,d,e); input [6:0] y; output [3:0] d,e;
assign d=y/10; assign e=y;
endmodule
五:4位比较器:
module tom(y,x,d); input [3:0] y,x; output [2:0] d;
assign d[2]=(x>y)?1:0; assign d[1]=(x==y)?1:0; assign d[0]=(x endmodule 六:四位全加器: 法一:(调用程序法) module fulladd4(sum,c_in,c_out,a,b); output [3:0] sum; output c_out; input [3:0] a,b; input c_in; wire c1,c2,c3; fulladd fa0(sum[0],c1,a[0],b[0],c_in); fulladd fa1(sum[1],c2,a[1],b[1],c1); fulladd fa2(sum[2],c3,a[2],b[2],c2); fulladd fa3(sum[3],c_out,a[3],b[3],c3); endmodule //程序调用