使用Quartus-II 9.1SP2 + ModelSim 6.5b-Aletra + Altera DE2-115 FPGA開發平台,設計3Bit Counter為例(TestBench平台)
//Test Bench for 3 bit Counter:
//module Counter_3bit(Q2, Q1, Q0, CLK, RESETn);
// output Q2;
// output Q1;
// output Q0;
// input CLK;
// input RESETn;
`timescale 10ns/10ps
module tb_counter3bit;
reg tCLK;
reg tRESETn;
wire tQ2, tQ1, tQ0;
Counter_3bit DUT(tQ2, tQ1, tQ0, tCLK, tRESETn);
//instantiate counter to be tested.
initial
begin
#0 tCLK=1'b0; //tCLK;
#0 tRESETn=1'b0; //tRESETn=1'b0
#25 tRESETn=1'b1; //tRESETn=1’b1;
#500 $stop;
end
always begin
#20 tCLK=~tCLK; //generate clock
end
endmodule
// output Q2;
// output Q1;
// output Q0;
// input CLK;
// input RESETn;
`timescale 10ns/10ps
module tb_counter3bit;
reg tCLK;
reg tRESETn;
wire tQ2, tQ1, tQ0;
Counter_3bit DUT(tQ2, tQ1, tQ0, tCLK, tRESETn);
//instantiate counter to be tested.
initial
begin
#0 tCLK=1'b0; //tCLK;
#0 tRESETn=1'b0; //tRESETn=1'b0
#25 tRESETn=1'b1; //tRESETn=1’b1;
#500 $stop;
end
always begin
#20 tCLK=~tCLK; //generate clock
end
endmodule
module Counter_3bit(Q2, Q1, Q0, nCLK, RESETn);
output Q2;
output Q1;
output Q0;
input nCLK;
input RESETn;
wire Q2n;
wire Q1n;
wire Q0n;
wire J0;
wire J1;
wire J2;
and(J2, Q1, Q0n);
and(J1, Q0, Q2n);
and(J0, Q1n, Q2n);
jk_flip_flop_edge_triggered jkff0(Q0, Q0n, nCLK, J0, 1'b1, RESETn);
jk_flip_flop_edge_triggered jkff1(Q1, Q1n, nCLK, J1, 1'b1, RESETn);
jk_flip_flop_edge_triggered jkff2(Q2, Q2n, nCLK, J2, 1'b1, RESETn);
endmodule // counter
module jk_flip_flop_edge_triggered(Q, Qn, C, J, K, RESETn);
output Q;
output Qn;
input C;
input J;
input K;
input RESETn;
wire Kn; // The complement of the K input.
wire D;
wire D1; // Data input to the D latch.
wire Cn; // Control input to the D latch.
wire Cnn; // Control input to the SR latch.
wire DQ; // Output from the D latch, inputs to the gated SR latch (S).
wire DQn; // Output from the D latch, inputs to the gated SR latch (R).
assign D1 = !RESETn ? 0 : D; // Upon reset force D1 = 0
not(Kn, K);
and(J1, J, Qn);
and(K1, Kn, Q);
or(D, J1, K1);
not(Cn, C);
not(Cnn, Cn);
d_latch dl(DQ, DQn, Cn, D1);
sr_latch_gated sr(Q, Qn, Cnn, DQ, DQn);
endmodule // jk_flip_flop_edge_triggered
module d_latch(Q, Qn, G, D);
output Q;
output Qn;
input G;
input D;
wire Dn;
wire D1;
wire Dn1;
not(Dn, D);
and(D1, G, D);
and(Dn1, G, Dn);
nor(Qn, D1, Q);
nor(Q, Dn1, Qn);
endmodule // d_latch
module sr_latch_gated(Q, Qn, G, S, R);
output Q;
output Qn;
input G;
input S;
input R;
wire S1;
wire R1;
and(S1, G, S);
and(R1, G, R);
nor(Qn, S1, Q);
nor(Q, R1, Qn);
endmodule // sr_latch_gated
沒有留言:
張貼留言