[笔记]再笔记--边干边学Verilog HDL --001

缘起

   

    偶然,借到块DE2,开始学习Verilog HDL,也是从语法看起,照着书上敲代码,仿真,做lab,下载,貌似入门了,但总是不确定,总是觉得不能把握什么。偶然看到akuei2的《Verilog哪些事儿--建模篇》V4,觉得很好,遂想记录“打谱”的点滴--是为再笔记。

Lab 1 永远的流水灯

    这名字是直接拿来的,起的太好了,不多言。

    image

    这个实验想阐述并行的思想,用4个类似的模块建立1个并行操作的流水灯模块。每隔100ms流动一下。

代码:

led0_module.v

复制代码
1 /**
2 * File name: led0_mocule.v
3 *
4 */
5
6  module led0_module
7 (
8 clk, rst_n, led_out
9 );
10
11 input clk;
12 input rst_n;
13 output led_out;
14
15 parameter T10MS = 25'd20_000_000;
16  
17 reg [24:0] count;
18
19 always @(posedge clk or negedge rst_n)
20 if (!rst_n)
21 count <= 25'd0;
22   else if (count == T10MS)
23 count <= 25'd0;
24   else
25 count <= count + 1'b1;
26  
27 reg rled_out;
28
29 always @(posedge clk or negedge rst_n)
30 if (!rst_n)
31 rled_out <= 1'b0;
32   else if (count >= 25'd0 && count < 25'd5_000_000)
33 rled_out <= 1'b1;
34   else
35 rled_out <= 1'b0;
36
37 assign led_out = rled_out;
38
39 endmodule
40
复制代码

led1_module.v

复制代码
1 /**
2 * File name: led1_mocule.v
3 *
4 */
5
6 module led1_module
7 (
8 clk, rst_n, led_out
9 );
10
11 input clk;
12 input rst_n;
13 output led_out;
14
15 parameter T10MS = 25'd20_000_000;
16
17 reg [24:0] count;
18
19 always @(posedge clk or negedge rst_n)
20 if (!rst_n)
21 count <= 25'd0;
22 else if (count == T10MS)
23 count <= 25'd0;
24 else
25 count <= count + 1'b1;
26
27 reg rled_out;
28
29 always @(posedge clk or negedge rst_n)
30 if (!rst_n)
31 rled_out <= 1'b0;
32 else if (count >= 25'd5_000_000 && count < 25'd10_000_000)
33 rled_out <= 1'b1;
34 else
35 rled_out <= 1'b0;
36
37 assign led_out = rled_out;
38
39 endmodule
40
复制代码

led2_module.v

复制代码
1 /**
2 * File name: led2_mocule.v
3 *
4 */
5
6 module led2_module
7 (
8 clk, rst_n, led_out
9 );
10
11 input clk;
12 input rst_n;
13 output led_out;
14
15 parameter T10MS = 25'd20_000_000;
16
17 reg [24:0] count;
18
19 always @(posedge clk or negedge rst_n)
20 if (!rst_n)
21 count <= 25'd0;
22 else if (count == T10MS)
23 count <= 25'd0;
24 else
25 count <= count + 1'b1;
26
27 reg rled_out;
28
29 always @(posedge clk or negedge rst_n)
30 if (!rst_n)
31 rled_out <= 1'b0;
32 else if (count >= 25'd10_000_000 && count < 25'd15_000_000)
33 rled_out <= 1'b1;
34 else
35 rled_out <= 1'b0;
36
37 assign led_out = rled_out;
38
39 endmodule
40
复制代码

led3_module.v

复制代码
1 /**
2 * File name: led3_mocule.v
3 *
4 */
5
6 module led3_module
7 (
8 clk, rst_n, led_out
9 );
10
11 input clk;
12 input rst_n;
13 output led_out;
14
15 parameter T10MS = 25'd20_000_000;
16
17 reg [24:0] count;
18
19 always @(posedge clk or negedge rst_n)
20 if (!rst_n)
21 count <= 25'd0;
22 else if (count == T10MS)
23 count <= 25'd0;
24 else
25 count <= count + 1'b1;
26
27 reg rled_out;
28
29 always @(posedge clk or negedge rst_n)
30 if (!rst_n)
31 rled_out <= 1'b0;
32 else if (count >= 25'd15_000_000 && count < 25'd20_000_000)
33 rled_out <= 1'b1;
34 else
35 rled_out <= 1'b0;
36
37 assign led_out = rled_out;
38
39 endmodule
40
复制代码

top_module.v

复制代码
1 /**
2 * File name: top_module.v
3 * Function: moving lamp, time interval = 0.1s.
4 * Pins: KEY0-rst_n, LEDG3-0
5 * Target board: DE2.
6 * Software: Quartus II 9.1 sp1
7 * ----------------------------
8 * yf.x
9 * 7-11-2011
10 *
11 */
12
13 module top_module
14 (
15 CLOCK_50, KEY, LEDG
16 );
17
18 input CLOCK_50;
19 input [0:0] KEY;
20 output [3:0] LEDG;
21
22 wire led0_out;
23
24 led0_module U0
25 (
26 .clk (CLOCK_50),
27 .rst_n (KEY[0]),
28 .led_out (led0_out)
29 );
30
31 wire led1_out;
32
33 led1_module U1
34 (
35 .clk (CLOCK_50),
36 .rst_n (KEY[0]),
37 .led_out (led1_out)
38 );
39
40 wire led2_out;
41
42 led2_module U2
43 (
44 .clk (CLOCK_50),
45 .rst_n (KEY[0]),
46 .led_out (led2_out)
47 );
48
49 wire led3_out;
50
51 led3_module U3
52 (
53 .clk (CLOCK_50),
54 .rst_n (KEY[0]),
55 .led_out (led3_out)
56 );
57
58 assign LEDG[3:0] = {led3_out, led2_out, led1_out, led0_out};
59
60 endmodule
复制代码

RTL图

image

小结

    并行思维.

posted on   yf.x  阅读(1761)  评论(4编辑  收藏  举报

编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 2025成都.NET开发者Connect圆满结束
< 2011年7月 >
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

导航

统计

点击右上角即可分享
微信分享提示