(筆記) 如何設計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


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


d_latch00

d_latch01

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


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


d_latch02

d_latch03

完整程式碼下載
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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值