LOADING
453 字
2 分钟
Verilog书写
2019-11-18
无标签

总结一些Verilog常用模块和语法。

—————————————————————————————————————

1 verilog常用模块

1.1 上升沿及下降沿检测

上升沿

wire variable_rise;
reg variable_r1;
reg variable_r2;
assign variable_rise = ~variable_r2 & variable_r1; //检测上升沿
always @(posedge clk_50m or negedge rst_n) begin
if(!rst_n) begin
variable_r1 <= 1'b1;
variable_r2 <= 1'b1;
end
else begin
variable_r1 <= variable;
variable_r2 <= variable_r1;
end
end

下降沿

wire variable_fall;
reg variable_r1;
reg variable_r2;
assign variable_fall = variable_r2 & ~variable_r1; //检测下降沿
always @(posedge clk_50m or negedge rst_n) begin
if(!rst_n) begin
variable_r1 <= 1'b0;
variable_r2 <= 1'b0;
end
else begin
variable_r1 <= variable;
variable_r2 <= variable_r1;
end
end

1.2 跑马灯

参数N设定灯的个数,参数TIMEOUT设定亮灯时间间隔。

如设定N为10,TIMEOUT为5M,系统时钟是DE1-SOC的50M时钟,则亮灯间隔是0.1s。

module led
#( parameter N = 10,
parameter TIMEOUT = 32'd5_000_000)
( input clk,
input rst_n,
output reg [N-1 : 0] led
);
reg [31:0] cnt; //确定亮灯的时间间隔
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
cnt <= 'h0;
else if(cnt == TIMEOUT - 1'b1)
cnt <= 'h0;
else
cnt <= cnt + 1'b1;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
led <= 'b1;
else if(cnt == TIMEOUT - 1'b1)
if(led[N-1] == 1'b1)
led <= 'b1;
else
led <= led << 1;
else
led <= led;
end
endmodule

1.3 4位转16位解码器

module decoder_4_16(
input enable,
input [3:0] in,
output [15:0] out
);
wire [3:0] high_d;
wire [3:0] low_d;
assign high_d[3] = ( in[3]) & ( in[2]) & enable;
assign high_d[2] = ( in[3]) & (~in[2]) & enable;
assign high_d[1] = (~in[3]) & ( in[2]) & enable;
assign high_d[0] = (~in[3]) & (~in[2]) & enable;
assign low_d[3] = ( in[1]) & ( in[0]);
assign low_d[2] = ( in[1]) & (~in[0]);
assign low_d[1] = (~in[1]) & ( in[0]);
assign low_d[0] = (~in[1]) & (~in[0]);
assign out[15] = high_d[3] & low_d[3];
assign out[14] = high_d[3] & low_d[2];
assign out[13] = high_d[3] & low_d[1];
assign out[12] = high_d[3] & low_d[0];
assign out[11] = high_d[2] & low_d[3];
assign out[10] = high_d[2] & low_d[2];
assign out[9] = high_d[2] & low_d[1];
assign out[8] = high_d[2] & low_d[0];
assign out[7] = high_d[1] & low_d[3];
assign out[6] = high_d[1] & low_d[2];
assign out[5] = high_d[1] & low_d[1];
assign out[4] = high_d[1] & low_d[0];
assign out[3] = high_d[0] & low_d[3];
assign out[2] = high_d[0] & low_d[2];
assign out[1] = high_d[0] & low_d[1];
assign out[0] = high_d[0] & low_d[0];
endmodule

—————————————————————————————————————

HOME

Verilog书写
/posts/2019-11-18-verilog/
作者
Eric
发布于
2019-11-18
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时