Verilog 制御ブロック
ハードウェアの動作は、条件文やロジックの流れを制御するその他の方法なしでは実装できません。 Verilog には、同じことを実現する一連の制御フロー ブロックとメカニズムがあります。
if-else-if
この条件文 特定のステートメントを実行するかどうかを決定するために使用されます。これは if-else-if
と非常によく似ています 式が true と評価された場合、最初のステートメントが実行されます。式が false と評価され、else
の場合 部分が存在し、else
部分が実行されます。
// if statement without else part
if (expression)
[statement]
// if statment with an else part
if (expression)
[statement]
else
[statement]
// if else for multiple statements should be
// enclosed within "begin" and "end"
if (expression) begin
[multiple statements]
end else begin
[multiple statements]
end
// if-else-if statement
if (expression)
[statement]
else if (expression)
[statement]
else
[statement]
else
if-else の一部はオプションであり、else
の場合は混乱を招く可能性があります ネストされた if シーケンスでは省略されます。この混乱を避けるために、else がない場合は常に else を前のものに関連付ける方が簡単です。もう 1 つの方法は、ステートメントを begin-end
で囲むことです。 ブロック。最後の else
上記以外のケース、または他の条件が満たされていないデフォルトのケースを処理します。
if-else-if の詳細については、ここをクリックしてください
ループは、ブロック内の単一または複数のステートメントを 1 回以上実行する方法を提供します。 Verilog には 4 種類のループ ステートメントがあります。
永久ループ
これにより、ブロック内のステートメントが継続的に実行されます。
forever
[statement]
forever begin
[multiple statements]
end
例
module my_design;
initial begin
forever begin
$display ("This will be printed forever, simulation can hang ...");
end
end
endmodule
シミュレーションログ ncsim> run This will be printed forever, simulation can hang ... This will be printed forever, simulation can hang ... ... ... This will be printed forever, simulation can hang ... This will be printed forever, simulation can hang ... This will be printed forever, simulation can hang ... This will be printed forever, simulation can hang ... Result reached the maximum of 5000 lines. Killing process.
ループを繰り返す
これにより、ステートメントが一定回数実行されます。式が X または Z に評価される場合、それはゼロとして扱われ、まったく実行されません。
repeat ([num_of_times]) begin
[statements]
end
repeat ([num_of_times]) @ ([some_event]) begin
[statements]
end
例
module my_design;
initial begin
repeat(4) begin
$display("This is a new iteration ...");
end
end
endmodule
シミュレーションログ ncsim> run This is a new iteration ... This is a new iteration ... This is a new iteration ... This is a new iteration ... ncsim: *W,RNQUIE: Simulation is complete.
while ループ
これは、式が真である限りステートメントを実行し、条件が偽になると終了します。条件が最初から false の場合、ステートメントはまったく実行されません。
while (expression) begin
[statements]
end
例
module my_design;
integer i = 5;
initial begin
while (i > 0) begin
$display ("Iteration#%0d", i);
i = i - 1;
end
end
endmodule
シミュレーションログ ncsim> run Iteration#5 Iteration#4 Iteration#3 Iteration#2 Iteration#1 ncsim: *W,RNQUIE: Simulation is complete.
for ループ
for ( initial_assignment; condition; increment_variable) begin
[statements]
end
これは、3 段階のプロセスを使用してステートメントを制御します:
- ループ カウンター変数を初期化する
- 通常はループ カウンタ変数を含む式を評価します
- 後で式が false になり、ループが終了するように、ループ カウンター変数をインクリメントします。
例
module my_design;
integer i = 5;
initial begin
for (i = 0; i < 5; i = i + 1) begin
$display ("Loop #%0d", i);
end
end
endmodule
シミュレーションログ ncsim> run Loop #0 Loop #1 Loop #2 Loop #3 Loop #4 ncsim: *W,RNQUIE: Simulation is complete.
for ループの詳細については、ここをクリックしてください。
Verilog