(筆記) 如何設計D Latch與D Flip-Flop? (SOC) (Verilog)

來源:互聯網
上載者:User

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

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 */

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

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 */

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

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 */

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

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 */

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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.