Outline — click to expand大綱 — 點擊展開アウトライン — クリックで展開
1. Testbench and Simulation1. 測試平台與模擬1. テストベンチとシミュレーション
2. Writing a Testbench2. 撰寫測試平台2. テストベンチの書き方
3. Data Types3. 資料型態3. データ型
4. Operators4. 運算子4. 演算子
5. Hierarchical Names5. 階層式名稱5. 階層名
6. Summary6. 重點整理6. まとめ
← DCIC Study Guides← DCIC 自學指南← DCIC 自学ガイド
EE5733 Digital Communication IC Design · Lecture 2EE5733 數位通訊積體電路設計 · 第 2 講EE5733 デジタル通信 IC 設計 · 第 2 講

Simulation & Testbench模擬與測試平台シミュレーションとテストベンチ

Event-driven simulation, gate and inertial delay, writing a testbench, the four-value logic system, nets, registers, memories, parameters and operators — a self-study guide to simulation and testbenches事件驅動模擬、閘延遲與慣性延遲、撰寫測試平台、四值邏輯系統、net、register、記憶體、參數與運算子 — 模擬與測試平台自學指南イベント駆動シミュレーション、ゲート遅延と慣性遅延、テストベンチの記述、4 値論理システム、ネット、レジスタ、メモリ、パラメータと演算子 — シミュレーションとテストベンチの自学ガイド

Contents目錄目次

  1. Testbench and Event-Driven Simulation測試平台與事件驅動模擬テストベンチとイベント駆動シミュレーション
  2. Writing a Testbench撰寫測試平台テストベンチの書き方
  3. Logic System and Data Types邏輯系統與資料型態論理システムとデータ型
  4. Operators運算子演算子
  5. Hierarchical De-Referencing階層式參照階層参照
  6. Summary and Exam Checklist重點整理與考前檢查表まとめと試験前チェックリスト

1. Testbench and Event-Driven Simulation1. 測試平台與事件驅動模擬1. テストベンチとイベント駆動シミュレーション

1.1 Testbench and design under test1.1 測試平台與待測電路(DUT)1.1 テストベンチと検証対象回路(DUT)

Testbench測試平台テストベンチ Input pattern輸入樣本入力パターンDesign Under Test待測電路検証対象回路(DUT) Golden pattern標準樣本ゴールデンパターンCompare比對比較(equal?)(相同?)(一致?)output輸出出力
The testbench drives input patterns into the DUT and checks its outputs against golden patterns (typically produced by C/C++ or MATLAB).測試平台把輸入樣本送進 DUT,再把它的輸出與 golden pattern(通常由 C/C++ 或 MATLAB 產生)比對。テストベンチは入力パターンを DUT に与え、その出力をゴールデンパターン(通常は C/C++ や MATLAB で生成する)と照合する。

1.2 Event-driven simulation1.2 事件驅動模擬1.2 イベント駆動シミュレーション

1.3 Simulation without and with delay1.3 無延遲與有延遲的模擬1.3 遅延なし・遅延ありのシミュレーション

Circuit: C = A & B, D = ~C. Stimulus: at t = 10, A = 1 and B = 0; at 20, B = 1; at 30, A = 0; at 40, B = 0.電路:C = A & B、D = ~C。激勵:t = 10 時 A = 1、B = 0;20 時 B = 1;30 時 A = 0;40 時 B = 0。回路:C = A & B、D = ~C。刺激:t = 10 で A = 1、B = 0、20 で B = 1、30 で A = 0、40 で B = 0。

ABCDC'D' 01020304050 131523253335
C, D: zero-delay simulation — outputs change at the same instant as the inputs. C', D': with AND = 3 ns and inverter = 2 ns each output event is scheduled later, and C, D stay x until their first scheduled update (13 and 15).C、D:零延遲模擬 — 輸出與輸入在同一瞬間改變。C'、D':AND 為 3 ns、反相器為 2 ns 時,每個輸出事件都被排到較晚的時間,而 C、D 在第一次排定的更新(13 與 15)之前都維持 x。C、D:遅延なしのシミュレーション — 出力は入力と同じ瞬間に変化する。C'、D':AND を 3 ns、インバータを 2 ns とすると各出力イベントは後の時刻にスケジュールされ、C と D は最初の更新時刻(13 と 15)まで x のままである。
Time時間時刻Input event輸入事件入力イベントZero delay零延遲遅延なしWith delay (AND 3, NOT 2)有延遲(AND 3、NOT 2)遅延あり(AND 3、NOT 2)
10A = 1, B = 0C = 0, D = 1 at 1010 時 C = 0、D = 110 で C = 0、D = 1C = 0 at 13, D = 1 at 1513 時 C = 0、15 時 D = 113 で C = 0、15 で D = 1
20B = 1C = 1, D = 0 at 2020 時 C = 1、D = 020 で C = 1、D = 0C = 1 at 23, D = 0 at 2523 時 C = 1、25 時 D = 023 で C = 1、25 で D = 0
30A = 0C = 0, D = 1 at 3030 時 C = 0、D = 130 で C = 0、D = 1C = 0 at 33, D = 1 at 3533 時 C = 0、35 時 D = 133 で C = 0、35 で D = 1
40B = 0no output change (C already 0)輸出沒有變化(C 本來就是 0)出力に変化なし(C はすでに 0)no event scheduled沒有排定任何事件イベントはスケジュールされない

1.4 Inertial delay1.4 慣性延遲1.4 慣性遅延

ABOO 1 ns overlap1 ns 重疊1 ns の重なりwithout inertial delay (1 ns pulse, delayed 2 ns)無慣性延遲(1 ns 脈衝,延遲 2 ns)慣性遅延なし(1 ns パルスが 2 ns 遅延)with inertial delay: no output pulse有慣性延遲:輸出沒有脈衝慣性遅延あり:出力パルスなし
AND gate with 2 ns delay. A rises 1 ns before B falls, so A·B is high for only 1 ns. Since 1 ns < 2 ns, the pulse is filtered and O stays 0.2 ns 延遲的 AND 閘。A 比 B 下降早 1 ns 上升,因此 A·B 只維持 1 ns 的高電位。由於 1 ns < 2 ns,這個脈衝被濾掉,O 維持 0。遅延 2 ns の AND ゲート。A は B が下降する 1 ns 前に立ち上がるため、A·B が High なのは 1 ns だけである。1 ns < 2 ns なのでこのパルスは除去され、O は 0 のままとなる。
Practice 1 — Event list練習 1 — 事件表練習 1 — イベントリスト

Problem: Same AND–NOT circuit with AND = 3 and NOT = 2. Starting with A = 1, B = 1 (C = 1, D = 0 settled), B falls at t = 10 and rises at t = 12. List the events and the final D waveform.題目:同樣的 AND–NOT 電路,AND = 3、NOT = 2。初始 A = 1、B = 1(C = 1、D = 0 已穩定),B 在 t = 10 下降、t = 12 上升。列出所有事件並畫出最後的 D 波形。問題:同じ AND–NOT 回路で AND = 3、NOT = 2 とする。初期状態は A = 1、B = 1(C = 1、D = 0 で安定)。B は t = 10 で下降し、t = 12 で上昇する。発生するイベントと最終的な D の波形を示せ。

Click to reveal solution點擊展開解答クリックで解答を表示

At 10, B = 0 schedules C = 0 at 13. At 12, B = 1 schedules C = 1 at 15 — the 2 ns low pulse is shorter than the 3 ns inertial delay, so the pending C = 0 event is cancelled and C stays 1. No event on C → no event on D. D stays 0.在 10 時 B = 0 排定 13 時 C = 0。在 12 時 B = 1 排定 15 時 C = 1 — 這個 2 ns 的低電位脈衝比 3 ns 的慣性延遲短,因此待處理的 C = 0 事件被取消,C 維持 1。C 沒有事件 → D 也沒有事件。D 維持 0。10 で B = 0 となり 13 での C = 0 がスケジュールされる。12 で B = 1 となり 15 での C = 1 がスケジュールされる — この 2 ns の Low パルスは 3 ns の慣性遅延より短いため、保留中の C = 0 イベントは取り消され、C は 1 のままとなる。C にイベントがなければ D にもイベントはない。D は 0 のままである。

2. Writing a Testbench2. 撰寫測試平台2. テストベンチの書き方

2.1 Common system tasks2.1 常用的系統任務2.1 よく使うシステムタスク

Construct語法構文Purpose用途用途
initialDeclares a one-shot behavior宣告一段只執行一次的行為一度だけ実行される動作を宣言する
$monitorPrints its arguments whenever any of them changes只要任一引數改變就印出所有引數引数のいずれかが変化するたびに引数を表示する
$timeReturns the simulation time (64-bit integer)回傳模擬時間(64 位元整數)シミュレーション時刻を返す(64 ビット整数)
$stopSuspends simulation and waits for interactive input暫停模擬並等待互動輸入シミュレーションを中断し対話入力を待つ
$finishEnds the simulation and returns to the operating system結束模擬並回到作業系統シミュレーションを終了し OS に戻る

2.2 Example: NAND latch testbench2.2 範例:NAND 閘鎖測試平台2.2 例:NAND ラッチのテストベンチ

Design設計設計
module Nand_Latch_1 (q, qbar, preset, clear);
  output q, qbar;
  input  preset, clear;

  nand #1 G1 (q, preset, qbar),
          G2 (qbar, clear, q);
endmodule

Cross-coupled NANDs with 1 time-unit delay; preset and clear are active low.交叉耦合的 NAND 閘,延遲為 1 個時間單位;preset 與 clear 為低電位有效。1 時間単位の遅延をもつ交差結合 NAND。preset と clear は負論理(Low アクティブ)である。

Testbench測試平台テストベンチ
module test_Nand_Latch_1;        // design unit testbench
  reg  preset, clear;
  wire q, qbar;

  Nand_Latch_1 M1 (q, qbar, preset, clear);  // instantiate UUT

  initial                        // response monitor
    begin
      $monitor ($time, "preset = %b clear = %b q = %b qbar = %b",
                preset, clear, q, qbar);
    end

  initial                        // stimulus generator
    begin
      #10 preset = 0; clear = 1;
      #10 preset = 1; $stop;     // enter . to proceed
      #10 clear  = 0;
      #10 clear  = 1;
      #10 preset = 0;
    end

  initial
    #60 $finish;                 // stop watch
endmodule
Time時間時刻presetclearqqbarWhy原因理由
0xxxxNothing assigned yet還沒有任何賦值まだ何も代入されていない
1001xxPreset assertedpreset 生效preset がアサートされる
11011xG1: NAND with a 0 input → 1 after 1G1:NAND 有一個 0 輸入 → 1 個時間單位後為 1G1:NAND の入力に 0 があるので 1 時間単位後に 1
120110G2: NAND(1, 1) → 0
201110Hold保持保持
301010Clear assertedclear 生效clear がアサートされる
311011G2 → 1
321001G1: NAND(1, 1) → 0
401101Hold保持保持
Caution注意注意

There is no firm rule for testbench editing. A common layout: declare regs for DUT inputs and wires for outputs, instantiate the DUT, one initial for monitoring, one for stimulus and one to end the run.撰寫測試平台沒有硬性規定。常見的寫法是:為 DUT 的輸入宣告 reg、為輸出宣告 wire,實體化 DUT,再用一個 initial 做監看、一個產生激勵、一個結束模擬。テストベンチの書き方に決まった規則はない。よくある構成は、DUT の入力に reg、出力に wire を宣言し、DUT をインスタンス化し、監視用・刺激生成用・実行終了用の initial を一つずつ置くというものである。

2.3 Design vs testbench2.3 設計與測試平台2.3 設計とテストベンチ

Design設計設計
  • Real hardware: module instances exist all the time.真實硬體:模組實體一直存在。実際のハードウェア:モジュールのインスタンスは常に存在する。
  • Each module has an architecture figure — draw it before writing Verilog.每個模組都有對應的架構圖 — 先畫圖再寫 Verilog。各モジュールにはアーキテクチャ図がある — Verilog を書く前に描くこと。
Testbench測試平台テストベンチ
  • Not real hardware; written like C/C++.不是真實硬體;寫法類似 C/C++。実際のハードウェアではなく、C/C++ のように記述する。
  • Usually behavior level, not synthesized.通常是行為層級,不做合成。通常は動作レベルで、合成はしない。
  • Feeds input patterns and compares outputs versus time against golden patterns.送入輸入樣本,並把輸出隨時間的變化與 golden pattern 比對。入力パターンを与え、時間に対する出力をゴールデンパターンと比較する。
Practice 2 — Latch printout練習 2 — 閘鎖的列印結果練習 2 — ラッチの出力表示

Problem: Continue the table after t = 40: what does $monitor print at 50, 51 and 52? Why does the slide's waveform end at 60?題目:把表格延伸到 t = 40 之後:$monitor 在 50、51、52 各會印出什麼?投影片上的波形為什麼在 60 結束?問題:t = 40 以降も表を続けよ。$monitor は 50、51、52 で何を表示するか。スライドの波形が 60 で終わるのはなぜか。

Click to reveal solution點擊展開解答クリックで解答を表示

50: preset = 0 clear = 1 q = 0 qbar = 1. 51: q = 1 (G1 sees preset = 0). 52: qbar = 0 (G2 = NAND(1, 1)). The third initial calls $finish at 60.50:preset = 0 clear = 1 q = 0 qbar = 1。51:q = 1(G1 看到 preset = 0)。52:qbar = 0(G2 = NAND(1, 1))。第三個 initial 在 60 呼叫 $finish。50:preset = 0 clear = 1 q = 0 qbar = 1。51:q = 1(G1 が preset = 0 を見る)。52:qbar = 0(G2 = NAND(1, 1))。3 番目の initial が 60 で $finish を呼ぶ。

3. Logic System and Data Types3. 邏輯系統與資料型態3. 論理システムとデータ型

3.1 Four values, nets and registers3.1 四值邏輯、net 與 register3.1 4 値論理、ネットとレジスタ

Value值値Meaning意義意味
0Logical 0, FALSE邏輯 0,FALSE論理 0、FALSE
1Logical 1, TRUE邏輯 1,TRUE論理 1、TRUE
xUnknown未知不定
zHigh impedance高阻抗ハイインピーダンス

3.2 Port rules and scope3.2 埠的規則與作用範圍3.2 ポートの規則とスコープ

Port埠ポートParent module (actual)父模組(actual)親モジュール(実引数)Child module (formal)子模組(formal)子モジュール(仮引数)
inputnet or registernet 或 registerネットまたはレジスタnet
outputnetnet or registernet 或 registerネットまたはレジスタ
inoutnetnet

The scope of a variable is the module, task, function or named begin ... end block in which it is declared. Inside that scope it is referenced by its identifier directly.變數的作用範圍(scope)是宣告它的那個 module、task、function 或具名的 begin ... end 區塊。在該範圍內可以直接用識別字引用它。変数のスコープとは、それを宣言したモジュール、タスク、関数、または名前付きの begin ... end ブロックである。そのスコープ内では識別子で直接参照できる。

3.3 Data type I: nets3.3 資料型態一:net3.3 データ型 I:ネット

Net typeNet 型別ネットの型Meaning意義意味
wire (default)wire(預設)wire(既定)Connectivity連線接続
triSame as wire; documents that it is tri-stated (e.g. O = A if E = 1, otherwise z)與 wire 相同;只是標示它是三態的(例如 E = 1 時 O = A,否則為 z)wire と同じ。三状態であることを示すための記述(例:E = 1 のとき O = A、そうでなければ z)
wand, worWired AND / OR with multiple drivers多重驅動時的線接 AND/OR複数ドライバによるワイヤード AND/OR
triand, triorTri-stated wired AND / OR三態的線接 AND/OR三状態のワイヤード AND/OR
supply0, supply1Connected to GND / VDD接到 GND/VDDGND/VDD に接続
wire        x;
wand        y;
wire [15:0] data;              // data[5], data[5:3]
wire [0:7]  array_a;           // array_a[0:3]
wire [7:0]  array_2d_p [0:31]; // array_2d_p[2] is an 8-bit vector
wire        a = b + c;         // declaration with implicit assign

3.4 Data type II: registers3.4 資料型態二:register3.4 データ型 II:レジスタ

Type型別型Stores儲存內容格納する値
regLogic value (unsigned)邏輯值(無號)論理値(符号なし)
integerSigned integer in two's complement (32 bits)二補數表示的有號整數(32 位元)2 の補数表現の符号付き整数(32 ビット)
realDouble-precision real倍精度實數倍精度実数
time64-bit unsigned simulation time64 位元無號模擬時間64 ビット符号なしのシミュレーション時刻
realtimeTime as a real number以實數表示的時間実数で表した時刻
reg        a, b;
reg [15:0] counter;
reg [7:0]  shift_reg [0:15];
integer    c;

3.5 Bit / part select and memories3.5 位元選取、部分選取與記憶體3.5 ビット選択・部分選択とメモリ

The MSB of a part select is the left index; the LSB is the right index. An out-of-range index returns x — avoid it.部分選取的 MSB 是左邊的索引,LSB 是右邊的索引。超出範圍的索引會得到 x,應避免。部分選択の MSB は左側の添字、LSB は右側の添字である。範囲外の添字は x を返すので避けること。

Expression (word[7:0] = 8'b1100_0100)運算式(word[7:0] = 8'b1100_0100)式(word[7:0] = 8'b1100_0100)Value值値
word[3:0]4'b0100
word[5:1]5'b00010 (five bits; the slide writes 4'b0010)5'b00010(五個位元;投影片寫成 4'b0010)5'b00010(5 ビット。スライドでは 4'b0010 と書かれている)
word[7:5]3'b110
word[9:4]illegal select不合法的選取不正な選択

A memory is a 2-D array of registers. Bits of a memory word cannot be addressed directly in Verilog-1995: read the word first.記憶體是 register 的二維陣列。在 Verilog-1995 中不能直接定址記憶體字組中的位元:必須先讀出整個字組。メモリはレジスタの 2 次元配列である。Verilog-1995 ではメモリワード内のビットを直接指定できないため、まずワードを読み出す。

reg [31:0] cache [0:1023];   // 1K memory of 32-bit words
reg [31:0] one_word;
reg        one_bit;

one_word = cache[1022];
one_bit  = one_word[31];

3.6 Constants: parameters3.6 常數:參數3.6 定数:パラメータ

parameter size     = 16;
reg [size-1:0] a;
parameter num_b    = 2'b01;
parameter av_delay = (min_delay + max_delay) / 2;
Approach I — direct substitution方法一 — 直接替換方法 I — 直接置換
module modXnor (y_out, a, b);
  parameter size = 8, delay = 15;
  output [size-1:0] y_out;
  input  [size-1:0] a, b;
  wire   [size-1:0] #delay y_out = a ~^ b;  // bitwise xnor
endmodule

module Param;
  wire [7:0] y1_out;
  wire [3:0] y2_out;
  reg  [7:0] b1, c1;
  reg  [3:0] b2, c2;

  modXnor        G1 (y1_out, b1, c1);   // default parameters
  modXnor #(4, 5) G2 (y2_out, b2, c2);  // override: size 4, delay 5
endmodule
Approach II — indirect substitution方法二 — 間接替換方法 II — 間接置換
module hdref_Param;            // top level
  wire [7:0] y1_out;
  wire [3:0] y2_out;
  reg  [7:0] b1, c1;
  reg  [3:0] b2, c2;

  modXnor G1 (y1_out, b1, c1),
          G2 (y2_out, b2, c2);
endmodule

module annotate;               // separate annotation module
  defparam
    hdref_Param.G2.size  = 4,  // hierarchical reference
    hdref_Param.G2.delay = 5;
endmodule
Notes注意事項注意
  • #(4, 5) overrides parameters in declaration order. A module instance may not have a delay associated with it, so #(...) on a module always means parameters.#(4, 5) 會依宣告順序覆寫參數。模組實體不能附加延遲,因此模組上的 #(...) 一定是指參數。#(4, 5) は宣言順にパラメータを上書きする。モジュールインスタンスに遅延を付けることはできないため、モジュールに付いた #(...) は常にパラメータを意味する。
  • A UDP may not contain parameter declarations, and parameters cannot be associated with a primitive gate.UDP 內不能有參數宣告,基本閘也不能附加參數。UDP にパラメータ宣言を置くことはできず、プリミティブゲートにパラメータを付けることもできない。
  • defparam is not supported by synthesis (Lecture 5) and is deprecated; prefer #(.size(4), .delay(5)) named overrides.defparam 不被合成工具支援(第 5 講),而且已被棄用;建議改用 #(.size(4), .delay(5)) 這種具名覆寫。defparam は合成でサポートされず(第 5 講)、非推奨である。#(.size(4), .delay(5)) のような名前付き上書きを使うこと。
Practice 3 — Legal assignments練習 3 — 合法的賦值練習 3 — 合法な代入

Problem: Which are legal? (a) reg r; assign r = a & b; (b) wire w; always @(a) w = a; (c) reg r; and (r, a, b); (d) reg r; always @(a or b) r = a & b; (e) testbench: reg x; DUT u1 (.in(x)); (f) inside DUT: output y; reg y;題目:下列哪些是合法的?(a) reg r; assign r = a & b; (b) wire w; always @(a) w = a; (c) reg r; and (r, a, b); (d) reg r; always @(a or b) r = a & b; (e) 測試平台中:reg x; DUT u1 (.in(x)); (f) DUT 內部:output y; reg y;問題:次のうち合法なものはどれか。(a) reg r; assign r = a & b; (b) wire w; always @(a) w = a; (c) reg r; and (r, a, b); (d) reg r; always @(a or b) r = a & b; (e) テストベンチ内:reg x; DUT u1 (.in(x)); (f) DUT 内部:output y; reg y;

Click to reveal solution點擊展開解答クリックで解答を表示

(a) Illegal — reg as continuous-assign target. (b) Illegal — wire as procedural target. (c) Illegal — reg as primitive output. (d) Legal. (e) Legal — an input port may be driven by a parent register. (f) Legal — an output may be a register inside the child.(a) 不合法 — reg 不能當連續賦值的目標。(b) 不合法 — wire 不能當程序式賦值的目標。(c) 不合法 — reg 不能當基本閘的輸出。(d) 合法。(e) 合法 — 輸入埠可以由父模組的 register 驅動。(f) 合法 — 輸出在子模組內部可以是 register。(a) 不正 — reg を連続代入の対象にしている。(b) 不正 — wire を手続き代入の対象にしている。(c) 不正 — reg をプリミティブの出力にしている。(d) 合法。(e) 合法 — 入力ポートは親のレジスタで駆動してよい。(f) 合法 — 出力は子モジュール内部でレジスタにしてよい。

Practice 4 — Part selects練習 4 — 部分選取練習 4 — 部分選択

Problem: reg [0:7] v = 8'b1010_0011; (note ascending range). Give (a) v[0] (b) v[0:3] (c) v[6:7]. (d) With reg [7:0] mem [0:3]; how do you read bit 2 of word 1?題目:reg [0:7] v = 8'b1010_0011;(注意是遞增範圍)。求 (a) v[0] (b) v[0:3] (c) v[6:7]。(d) 若有 reg [7:0] mem [0:3];,要怎麼讀出第 1 個字組的第 2 位元?問題:reg [0:7] v = 8'b1010_0011;(昇順の範囲指定であることに注意)。(a) v[0] (b) v[0:3] (c) v[6:7] を求めよ。(d) reg [7:0] mem [0:3]; のとき、ワード 1 のビット 2 はどう読み出すか。

Click to reveal solution點擊展開解答クリックで解答を表示

With [0:7], index 0 is the MSB (leftmost). (a) 1. (b) 4'b1010. (c) 2'b11. (d) tmp = mem[1]; bit = tmp[2]; (Verilog-2001 also allows mem[1][2].)在 [0:7] 的宣告下,索引 0 是 MSB(最左邊)。(a) 1。(b) 4'b1010。(c) 2'b11。(d) tmp = mem[1]; bit = tmp[2];(Verilog-2001 也允許 mem[1][2]。)[0:7] の場合、添字 0 が MSB(最左)である。(a) 1。(b) 4'b1010。(c) 2'b11。(d) tmp = mem[1]; bit = tmp[2];(Verilog-2001 では mem[1][2] も使える。)

4. Operators4. 運算子4. 演算子

4.1 Operator classes4.1 運算子的分類4.1 演算子の分類

Operator運算子演算子Operands運算元數オペランド数Result結果結果Symbols符號記号
Arithmetic算術算術2 (unary +/− take 1)2(單元 +/− 為 1)2(単項 +/− は 1)Binary word二進位字組2 進ワード+ - * / %
Bitwise位元運算ビット演算2 (~ takes 1)2(~ 為 1)2(~ は 1)Binary word二進位字組2 進ワード~ & | ^ ~^ ^~
Reduction縮減運算リダクション1Bit位元ビット& ~& | ~| ^ ~^ ^~
Logical邏輯論理2 (! takes 1)2(! 為 1)2(! は 1)Boolean value布林值ブール値! && || == != === !==
Relational關係関係2Boolean value布林值ブール値< <= > >=
Shift移位シフト1 (plus shift amount)1(再加上移位量)1(+シフト量)Binary word二進位字組2 進ワード<< >>
Conditional條件条件3Expression運算式式? :
assign sum = A + B;
assign neg = -A;

4.2 Bitwise and reduction4.2 位元運算與縮減運算4.2 ビット演算とリダクション

Bitwise位元運算ビット演算Result結果結果
~(1010)0101
(01) & (11)01
(01) | (11)11
(01) ^ (11)10
(01) ~^ (11)01

The shorter operand is zero-padded to the longer one's width.較短的運算元會補 0 到較長者的寬度。短い方のオペランドは長い方の幅までゼロ拡張される。

Reduction縮減運算リダクションResult結果結果
&(0101)0
|(0101)1
&(01xx)0
|(01xx)1

Reduction folds all bits of one operand into one bit. A single 0 decides & and a single 1 decides |, even with x bits present.縮減運算把單一運算元的所有位元摺成一個位元。即使存在 x 位元,只要有一個 0 就決定 & 的結果,只要有一個 1 就決定 | 的結果。リダクションは 1 つのオペランドの全ビットを 1 ビットに畳み込む。x ビットがあっても、0 が 1 つあれば & の結果が、1 が 1 つあれば | の結果が決まる。

4.3 Logical, relational and shift4.3 邏輯、關係與移位運算4.3 論理・関係・シフト演算

if ((b != c) && (index == 0)) ...
if ((inword == 1) || (a != d)) ...
if ((a < size - 1) || (b >= 3)) ...
result = (a << 3);

== / != return x if either operand has an x or z bit; === / !== compare x and z literally and always return 0 or 1.若任一運算元含有 x 或 z 位元,== / != 會回傳 x;=== / !== 則把 x 與 z 當作字面值比較,結果一定是 0 或 1。どちらかのオペランドに x または z のビットがあると == / != は x を返す。=== / !== は x と z をそのまま比較し、必ず 0 か 1 を返す。

4.4 Conditional and concatenation4.4 條件運算與串接4.4 条件演算子と連結

y = (a == b) ? a : b;
{a, b}                     // concatenation
{4{a}}                     // = {a, a, a, a}  (replication)
{3{a}, 2{b}, 4{c}}         // slide shorthand for {a, a, a, b, b, c, c, c, c}

Strict Verilog requires each replication in its own braces: {{3{a}}, {2{b}}, {4{c}}}.嚴格的 Verilog 要求每個重複都要有自己的大括號:{{3{a}}, {2{b}}, {4{c}}}。厳密な Verilog では各繰り返しをそれぞれ波括弧で囲む必要がある:{{3{a}}, {2{b}}, {4{c}}}。

wire [1:0]  select;
wire [15:0] D1, D2, D3, D4;
wire [15:0] bus = (select == 2'b00) ? D1 :
                  (select == 2'b01) ? D2 :
                  (select == 2'b10) ? D3 :
                  (select == 2'b11) ? D4 : 16'bx;
  • Signal = (cond) ? true_expr : false_expr;
  • z is not allowed in the condition.條件端不允許出現 z。条件部に z は使えない。
  • If the condition is ambiguous (x), both expressions are evaluated and combined bit by bit with the table on the right.若條件不確定(x),兩個運算式都會被求值,再依右表逐位元合併。条件があいまい(x)な場合、両方の式が評価され、右の表に従ってビットごとに合成される。
?:01x
00xx
1x1x
xxxx

4.5 Precedence and operands4.5 優先順序與運算元4.5 優先順位とオペランド

Precedence優先順序優先順位Operators運算子演算子
Highest最高最高Unary單元運算単項演算
Multiplication, division, modulus乘、除、取餘乗算・除算・剰余
Add, subtract加、減加算・減算
Shift移位シフト
Relational關係関係
Lowest最低最低Conditional條件条件

If unsure, use parentheses. (Between relational and conditional the full standard also places equality, bitwise and logical operators.)不確定時就加括號。(在關係與條件之間,完整的標準還排了相等、位元與邏輯運算子。)迷ったら括弧を使うこと。(関係演算子と条件演算子の間には、規格上さらに等価・ビット・論理演算子が入る。)

An operand may be a net, register, constant, number, bit-select, part-select, memory element, function call, or a concatenation of these: assign THIS_SIG = A_SIG ^ B_SIG;, @(SET or RESET) begin ... end.運算元可以是 net、register、常數、數值、位元選取、部分選取、記憶體元素、函式呼叫,或這些的串接:assign THIS_SIG = A_SIG ^ B_SIG;、@(SET or RESET) begin ... end。オペランドにはネット、レジスタ、定数、数値、ビット選択、部分選択、メモリ要素、関数呼び出し、またはそれらの連結を指定できる:assign THIS_SIG = A_SIG ^ B_SIG;、@(SET or RESET) begin ... end。

Practice 5 — Evaluate練習 5 — 求值練習 5 — 式の評価

Problem: a = 4'b1100, b = 4'b1010, c = 4'b10x0. Evaluate: (a) a & b (b) a ^ b (c) ^a (d) &c (e) |c (f) a == c (g) a >> 1 (h) {a[1:0], b[3:2]} (i) a + b >> 1 with a 5-bit target.題目:a = 4'b1100、b = 4'b1010、c = 4'b10x0。求:(a) a & b (b) a ^ b (c) ^a (d) &c (e) |c (f) a == c (g) a >> 1 (h) {a[1:0], b[3:2]} (i) a + b >> 1,目標為 5 位元。問題:a = 4'b1100、b = 4'b1010、c = 4'b10x0 とする。次を評価せよ:(a) a & b (b) a ^ b (c) ^a (d) &c (e) |c (f) a == c (g) a >> 1 (h) {a[1:0], b[3:2]} (i) a + b >> 1(代入先は 5 ビット)。

Click to reveal solution點擊展開解答クリックで解答を表示

(a) 1000. (b) 0110. (c) 0 (two ones). (d) 0 (bit 0 is 0). (e) 1. (f) x — compares an x bit. (g) 0110. (h) 0010. (i) + binds tighter than >>: (12 + 10) >> 1 = 22 >> 1 = 11 = 5'b01011 (the 5-bit context keeps the carry of the sum).(a) 1000。(b) 0110。(c) 0(有兩個 1)。(d) 0(第 0 位元是 0)。(e) 1。(f) x — 比較到一個 x 位元。(g) 0110。(h) 0010。(i) + 的優先順序高於 >>:(12 + 10) >> 1 = 22 >> 1 = 11 = 5'b01011(5 位元的環境保留了加法的進位)。(a) 1000。(b) 0110。(c) 0(1 が 2 個)。(d) 0(ビット 0 が 0)。(e) 1。(f) x — x ビットを比較するため。(g) 0110。(h) 0010。(i) + は >> より結合が強い:(12 + 10) >> 1 = 22 >> 1 = 11 = 5'b01011(5 ビットの文脈なので和の桁上がりが保たれる)。

Practice 6 — ?: with an unknown select練習 6 — 選擇端為未知的 ?:練習 6 — 選択が不定の ?:

Problem: y = s ? 4'b1100 : 4'b1010; Give y for s = 1, s = 0 and s = x.題目:y = s ? 4'b1100 : 4'b1010; 分別求 s = 1、s = 0、s = x 時的 y。問題:y = s ? 4'b1100 : 4'b1010; s = 1、s = 0、s = x のときの y をそれぞれ求めよ。

Click to reveal solution點擊展開解答クリックで解答を表示

s = 1: 1100. s = 0: 1010. s = x: compare bit by bit — equal bits keep their value, different bits become x: 1xx0.s = 1:1100。s = 0:1010。s = x:逐位元比較 — 相同的位元保留原值,不同的位元變成 x:1xx0。s = 1:1100。s = 0:1010。s = x:ビットごとに比較し、一致するビットは値を保ち、異なるビットは x になる:1xx0。

5. Hierarchical De-Referencing5. 階層式參照5. 階層参照

A variable defined inside an instantiated module is referenced by its hierarchical path name: X.w is w in instance X; X.Y.Z.w is w inside instance Z inside Y inside X.定義在被實體化模組內部的變數,可以用階層式路徑名稱來引用:X.w 是實體 X 中的 w;X.Y.Z.w 則是 X 裡的 Y 裡的 Z 裡的 w。インスタンス化されたモジュール内部で定義された変数は、階層パス名で参照する:X.w はインスタンス X の中の w、X.Y.Z.w は X の中の Y の中の Z の中の w である。

module test_Add_rca_4 ();
  reg  [3:0] a, b;
  reg        c_in;
  wire [3:0] sum;
  wire       c_out;

  initial
    begin
      $monitor ($time, "c_out = %b c_in4 = %b c_in3 = %b c_in2 = %b c_in = %b",
                c_out, M1.c_in4, M1.c_in3, M1.c_in2, c_in);
    end
  initial
    begin
      // stimulus patterns go here
    end

  Add_rca_4 M1 (sum, c_out, a, b, c_in);
endmodule

module Add_rca_4 (sum, c_out, a, b, c_in);
  output [3:0] sum;
  output       c_out;
  input  [3:0] a, b;
  input        c_in;
  wire         c_in4, c_in3, c_in2;

  Add_full G1 (sum[0], c_in2, a[0], b[0], c_in);
  Add_full G2 (sum[1], c_in3, a[1], b[1], c_in2);
  Add_full G3 (sum[2], c_in4, a[2], b[2], c_in3);
  Add_full G4 (sum[3], c_out, a[3], b[3], c_in4);
endmodule

The testbench watches the internal carries M1.c_in2 … M1.c_in4 without adding ports to the adder — a debugging aid (not synthesizable).測試平台不必替加法器增加埠,就能觀察內部進位 M1.c_in2 … M1.c_in4 — 這是除錯用的手法(不可合成)。テストベンチは加算器にポートを追加することなく内部キャリー M1.c_in2 … M1.c_in4 を観測できる — デバッグ用の手法であり、合成はできない。

Key points (click to expand)重點整理(點擊展開)要点(クリックで展開)
  • A testbench cannot be synthesized; it only generates stimulus, observes the outputs and compares them with golden patterns.Testbench 不可合成,只用來產生激勵、觀察輸出並與 golden pattern 比對。テストベンチは合成できず、刺激を生成し、出力を観測してゴールデンパターンと比較するためだけに使う。
  • Event-driven simulation: values are computed only when a signal changes and only for the signals affected; the run ends when the event list is empty.事件驅動模擬:只在訊號改變時、只對受影響的訊號計算;事件表清空即結束。イベント駆動シミュレーション:信号が変化したときに、影響を受ける信号についてのみ計算する。イベントリストが空になれば終了する。
  • Verilog uses the propagation delay as the inertial delay: a pulse shorter than the gate delay is filtered out.Verilog 以傳播延遲作為慣性延遲:比閘延遲短的脈衝會被濾掉。Verilog は伝搬遅延を慣性遅延として用いる:ゲート遅延より短いパルスは除去される。
  • A net is a connection (driven by assign or by an output); a register is the target of a procedural assignment and is not necessarily a flip-flop. A reg starts at x and an undriven net is z.Net 是連線(assign 或接到輸出),Register 是程序式賦值的目標,不一定是正反器;reg 初值為 x,無驅動的 net 為 z。ネットは接続(assign または出力への接続で駆動される)、レジスタは手続き代入の対象であり、必ずしもフリップフロップではない。reg の初期値は x、駆動されていないネットは z である。
  • Parameters can be overridden directly with #( ) or indirectly with defparam; a hierarchical name X.Y.w reaches a signal inside a child module.參數可用 #( ) 直接覆寫或 defparam 間接覆寫;階層式名稱 X.Y.w 可存取子模組內部訊號。パラメータは #( ) で直接、または defparam で間接的に上書きできる。階層名 X.Y.w で子モジュール内部の信号にアクセスできる。
Practice 7 — Parameter override練習 7 — 參數覆寫練習 7 — パラメータの上書き

Problem: In Param, what are the width and delay of G1 and G2? If b2 = 4'b0110, c2 = 4'b0101 change at t = 0, when and to what does y2_out change?題目:在 Param 中,G1 與 G2 的位元寬度與延遲各是多少?若 b2 = 4'b0110、c2 = 4'b0101 在 t = 0 改變,y2_out 會在什麼時候變成什麼?問題:Param において G1 と G2 のビット幅と遅延はいくつか。b2 = 4'b0110、c2 = 4'b0101 が t = 0 で変化するとき、y2_out はいつ何に変化するか。

Click to reveal solution點擊展開解答クリックで解答を表示

G1: 8 bits, delay 15. G2: 4 bits, delay 5. 0110 ~^ 0101 = 1100, so y2_out goes from x to 4'b1100 at t = 5.G1:8 位元,延遲 15。G2:4 位元,延遲 5。0110 ~^ 0101 = 1100,因此 y2_out 在 t = 5 由 x 變成 4'b1100。G1:8 ビット、遅延 15。G2:4 ビット、遅延 5。0110 ~^ 0101 = 1100 なので、y2_out は t = 5 で x から 4'b1100 に変わる。

6. Summary and Exam Checklist6. 重點整理與考前檢查表6. まとめと試験前チェックリスト

Simulation模擬シミュレーション
  • Event = value change.事件 = 值的改變。イベント=値の変化。
  • Time-ordered event list.依時間排序的事件表。時刻順のイベントリスト。
  • Gate delay = inertial delay.閘延遲 = 慣性延遲。ゲート遅延=慣性遅延。
Testbench測試平台テストベンチ
  • reg drives DUT inputs, wire for outputs.reg 驅動 DUT 的輸入,wire 接輸出。reg が DUT の入力を駆動し、出力には wire を使う。
  • $monitor, $time, $stop, $finish.
  • Not synthesized.不做合成。合成しない。
NetsNetネット
  • wire, tri, wand/wor, supply0/1.
  • Undriven = z; undeclared = wire.沒有驅動 = z;未宣告 = wire。駆動なし=z、未宣言=wire。
RegistersRegisterレジスタ
  • reg, integer, real, time, realtime.
  • Initial x; only procedural targets.初值為 x;只能是程序式賦值的目標。初期値は x。手続き代入の対象にのみなれる。
  • Memory: read the word, then the bit.記憶體:先讀字組,再取位元。メモリ:まずワードを読み、次にビットを取る。
Parameters參數パラメータ
  • #(4, 5) in declaration order.#(4, 5) 依宣告順序。#(4, 5) は宣言順。
  • defparam with hierarchical names.defparam 搭配階層式名稱。defparam は階層名を使う。
Operators運算子演算子
  • Reduction → 1 bit; bitwise → word.縮減運算 → 1 個位元;位元運算 → 整個字組。リダクション→1 ビット、ビット演算→ワード。
  • ?: with x select merges bitwise.?: 的選擇端為 x 時逐位元合併。選択が x の ?: はビットごとに合成する。
  • Use parentheses.多用括號。括弧を使うこと。