Writing The Testbench
CoddyのVerilogジャーニー「基礎」セクションの一部。レッスン 82/90。
チャレンジ
このレッスンでは、traffic light コントローラーが正しく動作することを検証するためのtestbenchを作成します。
行うこと:
次の条件を満たすtestbenchを作成します。
- 信号を宣言する(clkとresetには
reg、red、yellow、greenにはwire) - 名前
uutでtraffic_lightmoduleをインスタンス化する - 1 time unitごとに切り替わるクロックを生成する
- 2 time unitsの間resetを適用し、その後解除する
- simulationを100 time units実行する
simulationは出力内容によってチェックされるため、initial blockでは次の行を正確に出力する必要もあります。
$displayを使って、clkとresetを設定する前にTraffic Light Testを出力する- 100 time unitsの後、
$displayを使ってTest completeを出力する - simulationを
$finishで終了する。これにより、出力に独自の行が追加されます
自分で試してみよう
module traffic_light (
input clk,
input reset,
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
reg [5:0] counter;
// 出力の割り当て
always @(*) begin
case (state)
0: begin green = 1; yellow = 0; red = 0; end
1: begin green = 0; yellow = 1; red = 0; end
2: begin green = 0; yellow = 0; red = 1; end
default: begin green = 0; yellow = 0; red = 1; end
endcase
end
// タイミング付きステートマシン
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= 2;
counter <= 0;
end else begin
if (counter == 0) begin
case (state)
0: begin
state <= 1;
counter <= 10;
end
1: begin
state <= 2;
counter <= 40;
end
2: begin
state <= 0;
counter <= 30;
end
endcase
end else begin
counter <= counter - 1;
end
end
end
endmodule
module testbench;
// TODO: clkとreset用のregを宣言する
// TODO: red、yellow、green用のwireを宣言する
// TODO: traffic_lightモジュールをuutという名前でインスタンス化する
// clk、reset、red、yellow、greenを接続する
// TODO: クロックを生成する(1時間単位ごとにトグル)
initial begin
$display("Traffic Light Test");
// TODO: clkを0に初期化する
// TODO: リセットを適用する(2時間単位の間reset=1、その後reset=0)
// TODO: 100時間単位のシミュレーションを実行する
$display("Test complete");
$finish;
end
endmodule基礎のすべてのレッスン
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic自分で練習してみよう: Verilogオンラインコンパイラ