看一下下面的MODULE
/*简单上电复位模块
当计数器到0xffff时复位信号为1
*/
module reset(
input clk,
output rst_n
);
/*对用户来说,clk, rst_n只是输入或输出,把它定义为wire*/
/*cnt,rst_n_reg需要在always语句中应用,应把它定义为reg*/
reg[15:0] cnt = 16'd0; // 初始化定义为0
reg rst_n_reg;
assign rst_n = rst_n_reg;
always@(posedge clk)
if(cnt != 16'hffff)
cnt <= cnt + 16'd1;
else
cnt <= cnt;
always@(posedge clk)
rst_n_reg <= (cnt == 16'hffff);
endmodule
这个模块产生复位信号,但在复位之前,在这里的CNT变量被赋值为0,不这样的话,有可能会是其它值。
注意:变量在上电复位初始化时一定要用“=” 阻塞式赋值
而在ALWAYS中的复位需要用非阻塞式赋值。
寄存器的值初始有2种:
1.fpga上电起来后,寄存器是有个值,这个值是通过 reg a = xx ,来赋值的。2.复位后,寄存器的值由代码的复位逻辑决定的,例如:
always@(posedge clk or negedge rstn)
if(!rstn)
reg <= xx;
in verilog you have the initial
block after the modul declaration
module mymodul ( myclk , myreg);
input myclk;
output myreg;
initial
begin
myreg <= myinitvalue;
end
reg myreg;
always @ ( posedge myclk )
myreg <= myreg + 1;
endmodule
but have a look at the quartus settings about initial values, i am currently unshure which one but i think the is one settings about that.