背景
集成電路的設計經歷了從原理圖繪製(工程師在紙上繪製晶體管及其連接,以便對其設計,使其可以在硅上製造)到硬件描述語言的轉變,這是因爲大型的設計,如果使用原理圖的方式進行設計會耗費大量的人力、時間和資源等,這催生着硬件描述語言的誕生!
硬件描述語言最開始出現的VHDL,它是1983年,應美國國防部要求開發的,目的是記錄供應商公司將其包括在設備中的ASIC的行爲。
硬件描述語言允許工程師描述所需硬件的功能,並使得EDA工具將該行爲轉換爲組合邏輯以及時序邏輯之類的實際硬件單元,VHDL很快得到了發展!
Verilog的開發旨在簡化開發流程,並使得硬件描述語言(HDL)更加的健壯和靈活。如果,Verilog已經成爲全球使用地區最多的硬件描述語言。
如下圖,來自於參考資料2,顯示了使用Verilog以及VHDL的地域區別,
正文介紹
Verilog有什麼用途?
Verilog創建了一種抽象級別,以幫助隱藏電路實現及其細節,讓我們能關注於電路的行爲。
舉個例子:
module ctr (
input up_down,
input clk,
input rstn,
output reg [2:0] out
);
always @ (posedge clk)
if (!rstn)
out <= 0;
else begin
if (up_down)
out <= out + 1;
else
out <= out - 1;
end
endmodule
這段設計描述的電路是一種計數器,輸入信號up_down有效時候,計數遞增,否則遞減!
這很清晰地讓我們知道Verilog長什麼樣子,我們不需要描述電路的實現細節,而只需要描述電路的行爲或者功能即可,這大大提高了開發效率!
如何驗證Verilog設計的功能?
打個不恰當的比喻,也許能幫助理解,如果將Verilog描述的電路比作一個燈泡,那麼驗證的方式就是給燈泡通電,測試燈泡亮或不亮,以此達到驗證的目的。
通過不同的方法對設計進行檢查,統稱爲驗證。驗證的最普遍和廣泛實踐的方法是電路仿真。有一些軟件工具可以瞭解Verilog中描述的硬件應如何工作併爲設計模型提供各種輸入刺激。然後對照預期值檢查設計的輸出,以查看設計在功能上是否正確。
所有仿真均由EDA(電子設計自動化)軟件工具執行,並且Verilog設計RTL放置在稱爲testbench的平臺內。在測試平臺內,各種測試爲設計提供了不同的刺激。下圖顯示了這樣的測試平臺。
Verilog設計模板
module [design_name] ( [port_list] );
[list_of_input_ports]
[list_of_output_ports]
[declaration_of_other_signals]
[other_module_instantiations_if_required]
[behavioral_code_for_this_module]
endmodule
- 模塊定義和端口列表聲明
- 輸入和輸出端口列表
- 使用允許的Verilog數據類型聲明其他信號
- 設計可能依賴於其他Verilog模塊,因此它們的實例是通過模塊實例化創建的
- 描述該模塊行爲的此模塊的實際Verilog設計
例如下面的verilog設計顯示了一個D觸發器的行爲:
// "dff" is the name of this module
module dff (
input d, // Inputs to the design should start with "input"
input rstn,
input clk,
output reg q); // Outputs of the design should start with "output"
always @ (posedge clk) begin // This block is executed at the positive edge of clk 0->1
if (!rstn) // At the posedge, if rstn is 0 then q should get 0
q <= 0;
else
q <= d; // At the posedge, if rstn is 1 then q should get d
end
endmodule // End of module
對這段設計進行測試的仿真文件:
module tb;
// 1. Declare input/output variables to drive to the design
reg tb_clk;
reg tb_d;
reg tb_rstn;
wire tb_q;
// 2. Create an instance of the design
// This is called design instantiation
dff dff0 (
.clk (tb_clk), // Connect clock input with TB signal
.d (tb_d), // Connect data input with TB signal
.rstn (tb_rstn), // Connect reset input with TB signal
.q (tb_q)); // Connect output q with TB signal
// 3. The following is an example of a stimulus
// Here we drive the signals tb_* with certain values
// Since these tb_* signals are connected to the design inputs,
// the design will be driven with the values in tb_*
initial begin
tb_rsnt <= 1'b0;
tb_clk <= 1'b0;
tb_d <= 1'b0;
end
endmodule