文章目錄
-
-
-
- 文法
- 靜态task的定義
- 靜态調用示例
- automatic示例
- 全局task
- task和function的差別
- 禁止任務
-
-
文法
function可以對輸入資料進行處理,并傳回一個值,而task更通用,可以計算出多個值,可以使用output或inout參數類型,task可以包含仿真時間控制,例如@,posedge等。
// Style 1
task [name];
input [port_list];
inout [port_list];
output [port_list];
begin
[statements]
end
endtask
// Style 2
task [name] (input [port_list], inout [port_list], output [port_list]);
begin
[statements]
end
endtask
// Empty port list
task [name] ();
begin
[statements]
end
endtask
Verilog中的Function函數和C中的函數非常類似,它可以根據你的輸入,傳回計算的結果,函數的實作隻能是組合邏輯,不能包括時間控制,例如#100,可以指定傳回值的類型,應該包含至少1個輸入,輸入隻能是input類型,不能是inout或output,隻能傳回一個值。
當task/function定義為automatic,其變量也是隐式automatic的。 是以,在多次調用task/function時,變量每次都會配置設定記憶體并不會覆寫。
靜态task的定義
如果task是靜态定義的,成員變量将會在不同調用之間共享。
task sum (input [7:0] a, b, output [7:0] c);
begin
c = a + b;
end
endtask
// or
task sum;
input [7:0] a, b;
output [7:0] c;
begin
c = a + b;
end
endtask
initial begin
reg [7:0] x, y , z;
sum (x, y, z);
end
靜态調用示例
仿真檔案:
module tb;
initial display();
initial display();
initial display();
initial display();
// This is a static task
task display();
integer i = 0;
i = i + 1;
$display("i=%0d", i);
endtask
endmodule
運作結果:
xcelium> run
i=1
i=2
i=3
i=4
xmsim: *W,RNQUIE: Simulation is complete.
automatic示例
如果任務被加上了automatic關鍵字,那麼每次調用任務時都會配置設定不同的空間。
仿真檔案:
module tb;
initial display();
initial display();
initial display();
initial display();
// Note that the task is now automatic
task automatic display();
integer i = 0;
i = i + 1;
$display("i=%0d", i);
endtask
endmodule
運作結果:
xcelium> run
i=1
i=1
i=1
i=1
xmsim: *W,RNQUIE: Simulation is complete.
全局task
如果task被聲明在Module的外部,它将是全局的,可以被目前檔案内的所有module調用。
例如:
// This task is outside all modules
task display();
$display("Hello World !");
endtask
module des;
initial begin
display();
end
endmodule
運作結果:
xcelium> run
Hello World !
xmsim: *W,RNQUIE: Simulation is complete.
如果task被定義在某個module範圍内,那麼它将隻能在目前module範圍内使用。
例如:
module tb;
des u0();
initial begin
u0.display(); // Task is not visible in the module 'tb'
end
endmodule
module des;
initial begin
display(); // Task definition is local to the module
end
task display();
$display("Hello World");
endtask
endmodule
運作結果:
xcelium> run
Hello World
Hello World
xmsim: *W,RNQUIE: Simulation is complete.
task和function的差別
Function | Task |
---|---|
不能包含時間控制相關的關鍵字 | 可以包含時間控制語句 |
不能啟動另一個task | 可以啟動另一個task或調用function |
應該包括至少1個輸入 | 能夠包括0個或多個輸入 |
隻能有1個輸出 |
當function包含時間控制語句時,例如:
module tb;
reg signal;
initial wait_for_1(signal);
function wait_for_1(reg signal);
#10;
endfunction
endmodule
報文法錯誤:
禁止任務
task可以通過disable,以下示例中display任務将會在50ns後被結束。
module tb;
initial display();
initial begin
// After 50 time units, disable a particular named
// block T_DISPLAY inside the task called 'display'
#50 disable display.T_DISPLAY;
end
task display();
begin : T_DISPLAY
$display("[%0t] T_Task started", $time);
#100;
$display("[%0t] T_Task ended", $time);
end
begin : S_DISPLAY
#10;
$display("[%0t] S_Task started", $time);
#20;
$display("[%0t] S_Task ended", $time);
end
endtask
endmodule
運作結果:
xcelium> run
[0] T_Task started
[60] S_Task started
[80] S_Task ended
xmsim: *W,RNQUIE: Simulation is complete.
FROM:verilog-task