一,模块结构。
一个模块定义了一个基本单元,它允许你定义数字电路的行为和结构,格式如下:
module module_name(
port_list
);
Declarations_and_Statements
endmodule
port_list:端口列表,如果一个模块相当于一个芯片的话,端口就相当芯片引脚。
Declarations_and_Statements:变量的声明;
流水灯模块的代码如下:
module flow_led(
input sys_clk , //系统时钟
input sys_rst_n, //系统复位,低电平有效
output reg [3:0] led //4个LED灯
);
//reg define
reg [23:0] counter;
//*****************************************************
//** main code
//*****************************************************
//计数器对系统时钟计数,计时0.2秒
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n)
counter <= 24'd0;
else if (counter < 24'd999_9999)
counter <= counter + 1'b1;
else
counter <= 24'd0;
end
//通过移位寄存器控制IO口的高低电平,从而改变LED的显示状态
always @(posedge sys_clk or negedge sys_rst_n) begin
if (!sys_rst_n)
led <= 4'b0001;
else if(counter == 24'd999_9999)
led[3:0] <= {led[2:0],led[3]};
else
led <= led;
end
endmodule
二,port_list:端口列表。
端口就好比芯片的引脚,用来对外通信连接,在电路当中就好比硬线连接,在RTL图中可以查看端口引脚。
在Verilog中,port_list
是一种用于模块实例化的语法结构,它定义了模块端口与实例端口之间的连接。当你创建一个模块实例时,你需要指定每个模块端口连接到哪个信号或者另一个模块的端口。这通过端口列表(port list)来实现。
以下是一个简单的例子来说明如何在Verilog中使用端口列表进行模块实例化:
1. 定义模块
首先,定义一个简单的模块,比如一个AND门:
verilog复制代码
module AND_gate ( | |
input wire a, | |
input wire b, | |
output wire y | |
); | |
assign y = a & b; | |
endmodule |
2. 使用端口列表实例化模块
然后,在主模块(或顶层模块)中实例化这个AND门模块,并使用端口列表来连接信号:
verilog复制代码
module top_module ( | |
input wire in1, | |
input wire in2, | |
output wire out | |
); | |
// 实例化AND_gate模块,并使用端口列表连接信号 | |
AND_gate and1 ( | |
.a(in1), | |
.b(in2), | |
.y(out) | |
); | |
endmodule |
在这个例子中:
.a(in1)
表示将AND_gate模块的a
端口连接到top_module
的in1
信号。.b(in2)
表示将AND_gate模块的b
端口连接到top_module
的in2
信号。.y(out)
表示将AND_gate模块的y
端口连接到top_module
的out
信号。
端口列表的灵活性
端口列表不仅使得模块实例化更加清晰,而且允许端口映射的灵活性。例如,你可以按照任何顺序连接端口,或者甚至不按照模块定义的顺序:
verilog复制代码
AND_gate and2 ( | |
.y(some_other_signal), | |
.b(another_signal), | |
.a(yet_another_signal) | |
); |
这种方式使得设计更加灵活,特别是在复杂的系统中,当模块端口较多且需要特定的连接顺序时。
注意事项
- 端口列表中的每个端口连接都必须明确指定,不能遗漏。
- 端口名称和信号名称之间用圆点(
.
)分隔。 - 如果端口列表中的端口顺序与模块定义中的顺序不同,或者使用了不同的端口名称(例如,在顶层模块中重命名端口),则必须使用这种明确的映射方式。
通过这些规则,Verilog的端口列表提供了一种强大而灵活的方式来连接模块和信号,使得硬件描述语言(HDL)设计更加直观和易于管理。