一.一位二选一的数字选择器
功能代码:
module mux22(in1,in2,sel,out);
input in1,in2;
input sel;
output out;
wire out;
assign out =sel?in1:in2;
endmodule
测试代码:
module mux22_td;
reg sel,a,b;
mux22 u(a,b,sel,out);
always #10 sel=~sel;
initial
begin
a=1; b=0;
sel=0;
#10 a=0;b=1;
#10 a=1;b=0;
#10 a=0;b=1;
#20 a=1;b=0;
#20 a=0;b=1;
end
endmodule
运行结果:
二.D触发器
功能代码:
module dff(din,clk,q);
output q;
input din,clk;
reg q;
always@ (posedge clk)
q<=din;
endmodule
测试代码:
dff u1(din,clk,q);
always
#5 clk=~clk;
initial
begin
clk=0;din=0;
#10 din=0;
#10 din=1; //此时为20s处
#10 din=0;
#10 din=1;
#10 din=0;
#10 din=1;
#10 din=0;
end
endmodule
运行结果: