module t_ff(T, Preset, Clock, Q, Qbar); // dataflow style
input T,Preset,Clock;
output Q,Qbar;
reg Q;
wire Qn; // the next state Qn
always @(negedge Clock or posedge Preset) begin
// negative-edge triggered
if (Preset) // asynchronous preset , active high
Q <= 1'b1;
else
Q <= Qn; // assign the next state
end // always
assign Qn = T ^ Q; ; // characteristic equation of T flip-flop
assign Qbar = ~Q;
endmodule
//test bench//
`timescale 1 ns/1 ps
module t_test();
reg t,clk,preset;
wire q,qn;
// module t_ff(T, Preset, Clock, Q, Qbar);
t_ff f1(.Preset(preset),.
Q(q),
.Qbar(qn),
.T(t),
.Clock(clk));
initial
begin
$display("time,\t q,\t qn,\t t,\t clk,\t preset");
$monitor("%g,\t %b,\t %b,\t %b,\t %b,\t %b",$time,q,qn,t,clk,preset);
clk=1'b0;
preset=1'b1;
#5 preset=1'b0;
#10 t=1'b0;
#20 t=1'b1;
#30 t=1'b0;
#40 t=1'b1;
end
always #10 clk=~clk;
initial
#200 $stop;
endmodule
沒有留言:
張貼留言