(筆記) 如何設計D Latch與D Flip-Flop? (SOC) (Verilog)
Abstract
記憶元件的基礎:D Latch與D Flip-Flop。
Introduction
使用環境:Quartus II 7.2 SP3
D Latch
Method 1:
使用continuous assignment:
d_latch.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_latch.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d latch
7 Release : 08/09/2008 1.0
8 */
9
10 module d_latch (
11 input rst_n,
12 input en,
13 input d,
14 output q
15 );
16
17 assign q = (!rst_n) ? 0 :
18 (en) ? d : q;
19 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_latch.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d latch
7 Release : 08/09/2008 1.0
8 */
9
10 module d_latch (
11 input rst_n,
12 input en,
13 input d,
14 output q
15 );
16
17 assign q = (!rst_n) ? 0 :
18 (en) ? d : q;
19 endmodule
Method 2:
使用always block:
d_latch2.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_latch2.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d latch
7 Release : 08/09/2008 1.0
8 */
9
10 module d_latch2 (
11 input rst_n,
12 input en,
13 input d,
14 output reg q
15 );
16
17 always@(rst_n, en, d, q) begin
18 if (!rst_n)
19 q = 0;
20 else if (en)
21 q = d;
22 end
23
24 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_latch2.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d latch
7 Release : 08/09/2008 1.0
8 */
9
10 module d_latch2 (
11 input rst_n,
12 input en,
13 input d,
14 output reg q
15 );
16
17 always@(rst_n, en, d, q) begin
18 if (!rst_n)
19 q = 0;
20 else if (en)
21 q = d;
22 end
23
24 endmodule
D Flip-Flop
Method 1:
使用always block
d_ff.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d flip-flop
7 Release : 08/09/2008 1.0
8 */
9
10 module d_ff (
11 input clk,
12 input rst_n,
13 input en,
14 input d,
15 output reg q
16 );
17
18 always@(posedge clk or negedge rst_n)
19 if (!rst_n)
20 q <= 0;
21 else if (en)
22 q <= d;
23
24 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d flip-flop
7 Release : 08/09/2008 1.0
8 */
9
10 module d_ff (
11 input clk,
12 input rst_n,
13 input en,
14 input d,
15 output reg q
16 );
17
18 always@(posedge clk or negedge rst_n)
19 if (!rst_n)
20 q <= 0;
21 else if (en)
22 q <= d;
23
24 endmodule
Method 2:
使用Mega function
d_ff_mf.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_mf.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d flip-flop with Mega function
7 Release : 08/11/2008 1.0
8 */
9
10 module d_ff_mf (
11 input clk,
12 input rst_n,
13 input en,
14 input d,
15 output q
16 );
17
18 lpm_ff # (.lpm_width(1))
19 df (
20 .clock(clk),
21 .aclr(!rst_n),
22 .enable(en),
23 .data(d),
24 .q(q)
25 );
26
27 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : d_ff_mf.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write d flip-flop with Mega function
7 Release : 08/11/2008 1.0
8 */
9
10 module d_ff_mf (
11 input clk,
12 input rst_n,
13 input en,
14 input d,
15 output q
16 );
17
18 lpm_ff # (.lpm_width(1))
19 df (
20 .clock(clk),
21 .aclr(!rst_n),
22 .enable(en),
23 .data(d),
24 .q(q)
25 );
26
27 endmodule
完整程式碼下載
d_latch.7z
d_latch2.7z
d_ff.7z
d_ff_mf.7z
See Also
(筆記) 如何設計8位元暫存器? (SOC) (Verilog)
Reference
(原創) Verilog入門書推薦2:數位系統實習 Quartus II (SOC) (Verilog)