(筆記) 如何設計8位元暫存器? (SOC) (Verilog)
Abstract
暫存器,聽起來好像很高深的東西,其實只要多個D-FF,就可以組成暫存器了。
Introduction
Method 1:
使用always block
reg8.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : reg8.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write 8 bit register
7 Release : 08/11/2008 1.0
8 */
9
10 module reg8 (
11 input clk,
12 input rst_n,
13 input en,
14 input [7:0] d,
15 output reg [7:0] q
16 );
17
18 always@(posedge clk, negedge rst_n) begin
19 if (!rst_n)
20 q <= 0;
21 else if (en)
22 q <= d;
23 end
24
25 endmodule
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : reg8.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write 8 bit register
7 Release : 08/11/2008 1.0
8 */
9
10 module reg8 (
11 input clk,
12 input rst_n,
13 input en,
14 input [7:0] d,
15 output reg [7:0] q
16 );
17
18 always@(posedge clk, negedge rst_n) begin
19 if (!rst_n)
20 q <= 0;
21 else if (en)
22 q <= d;
23 end
24
25 endmodule
Method 2:
使用Mega function
reg8_mf.v / Verilog
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : reg8_mf.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write 8 bit register with Mega function
7 Release : 08/11/2008 1.0
8 */
9
10 module reg8_mf (
11 input clk,
12 input rst_n,
13 input en,
14 input [7:0] d,
15 output reg [7:0] q
16 );
17
18 lpm_ff # (.lpm_width(8))
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 : reg8_mf.v
5 Compiler : Quartus II 7.2 SP3
6 Description : Demo how to write 8 bit register with Mega function
7 Release : 08/11/2008 1.0
8 */
9
10 module reg8_mf (
11 input clk,
12 input rst_n,
13 input en,
14 input [7:0] d,
15 output reg [7:0] q
16 );
17
18 lpm_ff # (.lpm_width(8))
19 df (
20 .clock(clk),
21 .aclr(!rst_n),
22 .enable(en),
23 .data(d),
24 .q(q)
25 );
26
27 endmodule
完整程式碼下載
reg8.7z
reg8_mf.7z
See Also
(筆記) 如何設計D Latch與D Flip-Flop? (SOC) (Verilog)
Reference
(原創) Verilog入門書推薦2:數位系統實習 Quartus II (SOC) (Verilog)