常用关键字
module endmodule
input output inout
unsigned signed
reg wire
always @(posedge clk or negedge rst_n) 时序逻辑:非阻塞赋值,语句并行执行,等到一个时钟完成后才完成赋值。
always块中被赋值的必须是reg类型的。
- 非阻塞赋值综合出来的电路是
触发器:在时序逻辑中使用非阻塞赋值都会生成触发器(有几条赋值语句就会生成几个触发器)。
posedge negedge
"posedge"和"negedge"是数字电路中用来描述信号从低状态到高状态(上升沿)或从高状态到低状态(下降沿)转换的术语。
- 这些术语通常用于
Verilog和VHDL编程语言中,用于描述数字电路中信号的行为。
- 例如,在
Verilog中,类似"always @(posedge clk)"的语句意味着以下代码块将在"clk"信号从低到高转换时执行。同样,"always @(negedge rst_n)"意味着代码块将在"rst_n"信号从高到低转换时执行。
assign 组合逻辑:阻塞赋值,语句顺序执行,下一条赋值语句要等到上一条赋值语句完成后才能赋值,并且阻塞赋值是立即完成的。
begin end
if else
parameter定义常量
case endcase default
task endtask
function endfunction
initial(testbench文件中初始化使用)
repeat
etc.
复位
同步复位,与时钟沿有关:复位信号在时钟上升沿或下降沿生效,复位和数据变化都是在时钟的边沿进行的,因此称为同步复位。
always @(posedge clk) begin
if (~rst_n) begin // 同步复位
q <= 1'b0;
end
else begin
q <= d;
end
end
异步复位,与时钟沿无关:复位信号在任何时候都可以立即生效,不需要等待时钟的边沿,因此称为异步复位。【推荐使用】
always @(posedge clk or negedge rst_n) begin
if (~rst_n) begin // 异步复位
q <= 1'b0;
end
else begin
q <= d;
end
end
简单Counter计数器举例
TOP顶层文件
module counter (
input clk, // 时钟信号
input rst_n, // 复位信号
input en, // 使能信号
output reg [1:0] count // 计数器输出
);
always @(posedge clk or negedge rst_n) begin
if (~rst_n) begin // 复位信号为低电平时,计数器清零
count <= 2'd0;
end
else if (en) begin // 否则计数器加1
count <= count + 2'd1;
end
end
endmodule
TestBench仿真文件
`timescale 1ns / 1ps
module counter_tb;
reg clk;
reg rst_n;
reg en;
wire [1:0] count;
counter dut (
.clk(clk),
.rst_n(rst_n),
.en(en),
.count(count)
);
initial begin
clk = 0;
rst_n = 0;
en = 0;
#10 rst_n = 1;
#10 en = 1;
#100 $finish;
end
always #5 clk = ~clk;
endmodule