(筆記) d-ff的幾種寫法 (SOC) (Verilog)
Abstract
Verilog除了synthesizable RTL外,也提供non synthesizable的寫法,用來寫testbench。
Introduction
使用環境:NC-Verilog 5.4 + Debussy 5.4
Method 1:
Synthesizable RTL
這是大家最熟悉的寫法,使用always block配合non blocking。
d_ff_rtl.v / Verilog
1 /*
2 (C) OOMusou 2009 http://oomusou.cnblogs.com
3
4 Filename : d_ff_rtl.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with synthesizable rtl
7 Release : 07/12/2009 1.0
8 */
9 `timescale 1ns/1ns
10
11 module d_ff_rtl (
12 clk,
13 rst_n,
14 d_i,
15 q_o
16 );
17
18 input clk;
19 input rst_n;
20 input d_i;
21 output reg q_o;
22
23 always@(posedge clk, negedge rst_n) begin
24 if (~rst_n)
25 q_o <= 1'b0;
26 else
27 q_o <= d_i;
28 end
29
30 endmodule
2 (C) OOMusou 2009 http://oomusou.cnblogs.com
3
4 Filename : d_ff_rtl.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with synthesizable rtl
7 Release : 07/12/2009 1.0
8 */
9 `timescale 1ns/1ns
10
11 module d_ff_rtl (
12 clk,
13 rst_n,
14 d_i,
15 q_o
16 );
17
18 input clk;
19 input rst_n;
20 input d_i;
21 output reg q_o;
22
23 always@(posedge clk, negedge rst_n) begin
24 if (~rst_n)
25 q_o <= 1'b0;
26 else
27 q_o <= d_i;
28 end
29
30 endmodule
Testbench
d_ff_rtl_tb.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_rtl_tb.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with synthesizable rtl
7 Release : 07/12/2009 1.0
8 */
9
10 `include "d_ff_rtl.v"
11 `timescale 1ns/1ns
12
13 module d_ff_rtl_tb;
14
15 reg clk;
16 reg rst_n;
17 reg d_i;
18 wire q_o;
19
20 d_ff_rtl d_ff_rtl0 (
21 .clk(clk),
22 .rst_n(rst_n),
23 .d_i(d_i),
24 .q_o(q_o)
25 );
26
27 initial begin
28 $fsdbDumpfile("d_ff_rtl.fsdb");
29 $fsdbDumpvars(0, d_ff_rtl_tb);
30 #100;
31 $finish;
32 end
33
34 initial clk = 1'b0;
35 always #10 clk = ~clk;
36
37 initial begin
38 rst_n = 1'b1;
39 d_i = 1'b0;
40 #5 rst_n = 1'b0;
41 #1 rst_n = 1'b1;
42 #4 d_i = 1'b1;
43 #10 d_i = 1'b0;
44 end
45
46 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_rtl_tb.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with synthesizable rtl
7 Release : 07/12/2009 1.0
8 */
9
10 `include "d_ff_rtl.v"
11 `timescale 1ns/1ns
12
13 module d_ff_rtl_tb;
14
15 reg clk;
16 reg rst_n;
17 reg d_i;
18 wire q_o;
19
20 d_ff_rtl d_ff_rtl0 (
21 .clk(clk),
22 .rst_n(rst_n),
23 .d_i(d_i),
24 .q_o(q_o)
25 );
26
27 initial begin
28 $fsdbDumpfile("d_ff_rtl.fsdb");
29 $fsdbDumpvars(0, d_ff_rtl_tb);
30 #100;
31 $finish;
32 end
33
34 initial clk = 1'b0;
35 always #10 clk = ~clk;
36
37 initial begin
38 rst_n = 1'b1;
39 d_i = 1'b0;
40 #5 rst_n = 1'b0;
41 #1 rst_n = 1'b1;
42 #4 d_i = 1'b1;
43 #10 d_i = 1'b0;
44 end
45
46 endmodule
Waveform
Method 2:
使用wait (non synthesizable)
d_ff_beh1.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh1.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `timescale 1ns/1ns
11
12 module d_ff_beh1 (
13 clk,
14 rst_n,
15 d_i,
16 q_o,
17 );
18
19 input clk;
20 input rst_n;
21 input d_i;
22 output reg q_o;
23
24 always begin
25 wait(~rst_n);
26 q_o = 1'b0;
27 wait(rst_n);
28 end
29
30 always@(posedge clk) begin
31 if (rst_n)
32 q_o <= d_i;
33 end
34
35 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh1.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `timescale 1ns/1ns
11
12 module d_ff_beh1 (
13 clk,
14 rst_n,
15 d_i,
16 q_o,
17 );
18
19 input clk;
20 input rst_n;
21 input d_i;
22 output reg q_o;
23
24 always begin
25 wait(~rst_n);
26 q_o = 1'b0;
27 wait(rst_n);
28 end
29
30 always@(posedge clk) begin
31 if (rst_n)
32 q_o <= d_i;
33 end
34
35 endmodule
Testbench
d_ff_beh1_tb.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh1_tb.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `include "d_ff_beh1.v"
11 `timescale 1ns/1ns
12
13 module d_ff_beh1_tb;
14
15 reg clk;
16 reg rst_n;
17 reg d_i;
18 wire q_o;
19
20 d_ff_beh1 d_ff_beh10 (
21 .clk(clk),
22 .rst_n(rst_n),
23 .d_i(d_i),
24 .q_o(q_o)
25 );
26
27 initial begin
28 $fsdbDumpfile("d_ff_beh1.fsdb");
29 $fsdbDumpvars(0, d_ff_beh1_tb);
30 #100;
31 $finish;
32 end
33
34 initial clk = 1'b0;
35 always #10 clk = ~clk;
36
37 initial begin
38 rst_n = 1'b1;
39 d_i = 1'b0;
40 #5 rst_n = 1'b0;
41 #1 rst_n = 1'b1;
42 #4 d_i = 1'b1;
43 #10 d_i = 1'b0;
44 end
45
46 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh1_tb.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `include "d_ff_beh1.v"
11 `timescale 1ns/1ns
12
13 module d_ff_beh1_tb;
14
15 reg clk;
16 reg rst_n;
17 reg d_i;
18 wire q_o;
19
20 d_ff_beh1 d_ff_beh10 (
21 .clk(clk),
22 .rst_n(rst_n),
23 .d_i(d_i),
24 .q_o(q_o)
25 );
26
27 initial begin
28 $fsdbDumpfile("d_ff_beh1.fsdb");
29 $fsdbDumpvars(0, d_ff_beh1_tb);
30 #100;
31 $finish;
32 end
33
34 initial clk = 1'b0;
35 always #10 clk = ~clk;
36
37 initial begin
38 rst_n = 1'b1;
39 d_i = 1'b0;
40 #5 rst_n = 1'b0;
41 #1 rst_n = 1'b1;
42 #4 d_i = 1'b1;
43 #10 d_i = 1'b0;
44 end
45
46 endmodule
Waveform
Method 3:
使用assign / deassign (non synthesizable)
d_ff_beh2.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh2.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `timescale 1ns/1ns
11
12 module d_ff_beh2 (
13 clk,
14 rst_n,
15 d_i,
16 q_o,
17 );
18
19 input clk;
20 input rst_n;
21 input d_i;
22 output reg q_o;
23
24 always@(rst_n) begin
25 if (~rst_n)
26 assign q_o = 1'b0;
27 else
28 deassign q_o;
29 end
30
31 always@(posedge clk) begin
32 q_o <= d_i;
33 end
34
35 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh2.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `timescale 1ns/1ns
11
12 module d_ff_beh2 (
13 clk,
14 rst_n,
15 d_i,
16 q_o,
17 );
18
19 input clk;
20 input rst_n;
21 input d_i;
22 output reg q_o;
23
24 always@(rst_n) begin
25 if (~rst_n)
26 assign q_o = 1'b0;
27 else
28 deassign q_o;
29 end
30
31 always@(posedge clk) begin
32 q_o <= d_i;
33 end
34
35 endmodule
Testbench
d_ff_beh2_tb.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh2_tb.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `include "d_ff_beh2.v"
11 `timescale 1ns/1ns
12
13 module d_ff_beh2_tb;
14
15 reg clk;
16 reg rst_n;
17 reg d_i;
18 wire q_o;
19
20 d_ff_beh2 d_ff_beh20 (
21 .clk(clk),
22 .rst_n(rst_n),
23 .d_i(d_i),
24 .q_o(q_o)
25 );
26
27 initial begin
28 $fsdbDumpfile("d_ff_beh2.fsdb");
29 $fsdbDumpvars(0, d_ff_beh2_tb);
30 #100;
31 $finish;
32 end
33
34 initial clk = 1'b0;
35 always #10 clk = ~clk;
36
37 initial begin
38 rst_n = 1'b1;
39 d_i = 1'b0;
40 #5 rst_n = 1'b0;
41 #1 rst_n = 1'b1;
42 #4 d_i = 1'b1;
43 #10 d_i = 1'b0;
44 end
45
46 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_beh2_tb.v
5 Compiler : NC-Verilog 5.4
6 Description : d-ff with non synthesizable behavior model 1
7 Release : 07/12/2009 1.0
8 */
9
10 `include "d_ff_beh2.v"
11 `timescale 1ns/1ns
12
13 module d_ff_beh2_tb;
14
15 reg clk;
16 reg rst_n;
17 reg d_i;
18 wire q_o;
19
20 d_ff_beh2 d_ff_beh20 (
21 .clk(clk),
22 .rst_n(rst_n),
23 .d_i(d_i),
24 .q_o(q_o)
25 );
26
27 initial begin
28 $fsdbDumpfile("d_ff_beh2.fsdb");
29 $fsdbDumpvars(0, d_ff_beh2_tb);
30 #100;
31 $finish;
32 end
33
34 initial clk = 1'b0;
35 always #10 clk = ~clk;
36
37 initial begin
38 rst_n = 1'b1;
39 d_i = 1'b0;
40 #5 rst_n = 1'b0;
41 #1 rst_n = 1'b1;
42 #4 d_i = 1'b1;
43 #10 d_i = 1'b0;
44 end
45
46 endmodule
Waveform