為確保系統(tǒng)上電后有一個(gè)明確、穩(wěn)定的初始狀態(tài),或系統(tǒng)運(yùn)行狀態(tài)紊亂時(shí)可以恢復(fù)到正常的初始狀態(tài),數(shù)字系統(tǒng)設(shè)計(jì)中一定要有復(fù)位電路模塊。復(fù)位電路異??赡軙?huì)導(dǎo)致整個(gè)系統(tǒng)的功能異常,所以在一定程度上,復(fù)位電路的重要性也不亞于時(shí)鐘電路。
復(fù)位電路可分類為同步復(fù)位和異步復(fù)位。
同步復(fù)位是指復(fù)位信號(hào)在時(shí)鐘有效邊沿到來時(shí)有效。如果沒有時(shí)鐘,無論復(fù)位信號(hào)怎樣變化,電路也不執(zhí)行復(fù)位操作。
同步復(fù)位的典型代碼描述如下:
module sync_reset(
input rstn, //同步復(fù)位信號(hào)
input clk, //時(shí)鐘
input din, //輸入數(shù)據(jù)
output reg dout //輸出數(shù)據(jù)
);
always @(posedge clk) begin //復(fù)位信號(hào)不要加入到敏感列表中
if(!rstn) dout <= 1'b0 ; //rstn 信號(hào)與時(shí)鐘 clk 同步
else dout <= din ;
end
endmodule
該描述代碼常常會(huì)被綜合成如下電路:
同步復(fù)位的優(yōu)點(diǎn):信號(hào)間是同步的,能濾除復(fù)位信號(hào)中的毛刺,有利于時(shí)序分析。
同步復(fù)位的缺點(diǎn):大多數(shù)觸發(fā)器單元是沒有同步復(fù)位端的,采用同步復(fù)位會(huì)多消耗部分邏輯資源。且復(fù)位信號(hào)的寬度必須大于一個(gè)時(shí)鐘周期,否則可能會(huì)漏掉復(fù)位信號(hào)。
異步復(fù)位是指無論時(shí)鐘到來與否,只要復(fù)位信號(hào)有效,電路就會(huì)執(zhí)行復(fù)位操作。
異步復(fù)位的典型代碼描述如下:
module async_reset(
input rstn, //異步復(fù)位信號(hào)
input clk, //時(shí)鐘
input din, //輸入數(shù)據(jù)
output reg dout //輸出數(shù)據(jù)
);
//復(fù)位信號(hào)要加到敏感列表中
always @(posedge clk or negedge rstn) begin
if(!rstn) dout <= 1'b0 ; //rstn 信號(hào)與時(shí)鐘 clk 異步
else dout <= din ;
end
endmodule
該代碼常常會(huì)被綜合成如下電路:
異步復(fù)位的優(yōu)點(diǎn):大多數(shù)觸發(fā)器單元有異步復(fù)位端,不會(huì)占用額外的邏輯資源。且異步復(fù)位信號(hào)不經(jīng)過處理直接引用,設(shè)計(jì)相對(duì)簡(jiǎn)單,信號(hào)識(shí)別快速方便。
異步復(fù)位的缺點(diǎn):復(fù)位信號(hào)與時(shí)鐘信號(hào)無確定的時(shí)序關(guān)系,異步復(fù)位很容易引起時(shí)序上 removal 和 recovery 的不滿足。且異步復(fù)位容易受到毛刺的干擾,產(chǎn)生意外的復(fù)位操作。
綜合設(shè)計(jì)與資源等方面的考慮,一般數(shù)字系統(tǒng)設(shè)計(jì)時(shí)都會(huì)使用異步復(fù)位。
為消除異步復(fù)位的缺陷,復(fù)位電路往往會(huì)采用"異步復(fù)位、同步釋放"的設(shè)計(jì)方法。即復(fù)位信號(hào)到來時(shí)不受時(shí)鐘信號(hào)的同步,復(fù)位信號(hào)釋放時(shí)需要進(jìn)行時(shí)鐘信號(hào)的同步。
異步復(fù)位、同步釋放的典型代碼描述如下:
module areset_srelease(
input rstn, //異步復(fù)位信號(hào)
input clk, //時(shí)鐘
input din, //輸入數(shù)據(jù)
output reg dout //輸出數(shù)據(jù)
);
reg rstn_r1, rstn_r2;
always @ (posedge clk or negedge rstn) begin
if (!rstn) begin
rstn_r1 <= 1'b0; //異步復(fù)位
rstn_r2 <= 1'b0;
end
else begin
rstn_r1 <= 1'b1; //同步釋放
rstn_r2 <= rstn_r1; //同步打拍,時(shí)序差可以多延遲幾拍
end
end
//使用 rstn_r2 做同步復(fù)位,復(fù)位信號(hào)可以加到敏感列表中
always @ (posedge clk or negedge rstn_r2) begin
if (!rstn_r2) dout <= 1'b0; //同步復(fù)位
else dout <= din;
end
endmodule
該代碼描述常常會(huì)被綜合成如下電路:
需要說明的是,復(fù)位電路會(huì)消耗更多的硬件邏輯和面積資源,增加系統(tǒng)設(shè)計(jì)的復(fù)雜性。不帶復(fù)位端的觸發(fā)器也具有相對(duì)較高的性能。所以在一些初始值不影響邏輯正確性的數(shù)字設(shè)計(jì)中,例如數(shù)據(jù)通路中一些數(shù)據(jù)處理的部分,高速流水線中的一些寄存器,可以考慮去掉復(fù)位以達(dá)到最佳性能。
為方便、快速的仿真非復(fù)位邏輯的其他功能,教程所有數(shù)字設(shè)計(jì)中的復(fù)位都是從 testbench 中引入異步復(fù)位,沒有考慮復(fù)位電路的時(shí)序問題。實(shí)際設(shè)計(jì)數(shù)字系統(tǒng)時(shí),一定要對(duì)復(fù)位電路進(jìn)行單獨(dú)、仔細(xì)、慎重的設(shè)計(jì)。
更多建議: