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 値論理システム、ネット、レジスタ、メモリ、パラメータと演算子 — シミュレーションとテストベンチの自学ガイド
0, 1, x (unknown / ambiguous), z (high impedance / open circuit).訊號的值:0、1、x(未知/不確定)、z(高阻抗/開路)。信号の値:0、1、x(不定/あいまい)、z(ハイインピーダンス/開放)。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。
| Time時間時刻 | Input event輸入事件入力イベント | Zero delay零延遲遅延なし | With delay (AND 3, NOT 2)有延遲(AND 3、NOT 2)遅延あり(AND 3、NOT 2) |
|---|---|---|---|
| 10 | A = 1, B = 0 | C = 0, D = 1 at 1010 時 C = 0、D = 110 で C = 0、D = 1 | C = 0 at 13, D = 1 at 1513 時 C = 0、15 時 D = 113 で C = 0、15 で D = 1 |
| 20 | B = 1 | C = 1, D = 0 at 2020 時 C = 1、D = 020 で C = 1、D = 0 | C = 1 at 23, D = 0 at 2523 時 C = 1、25 時 D = 023 で C = 1、25 で D = 0 |
| 30 | A = 0 | C = 0, D = 1 at 3030 時 C = 0、D = 130 で C = 0、D = 1 | C = 0 at 33, D = 1 at 3533 時 C = 0、35 時 D = 133 で C = 0、35 で D = 1 |
| 40 | B = 0 | no output change (C already 0)輸出沒有變化(C 本來就是 0)出力に変化なし(C はすでに 0) | no event scheduled沒有排定任何事件イベントはスケジュールされない |
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 の波形を示せ。
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 のままである。
| Construct語法構文 | Purpose用途用途 |
|---|---|
initial | Declares a one-shot behavior宣告一段只執行一次的行為一度だけ実行される動作を宣言する |
$monitor | Prints its arguments whenever any of them changes只要任一引數改變就印出所有引數引数のいずれかが変化するたびに引数を表示する |
$time | Returns the simulation time (64-bit integer)回傳模擬時間(64 位元整數)シミュレーション時刻を返す(64 ビット整数) |
$stop | Suspends simulation and waits for interactive input暫停模擬並等待互動輸入シミュレーションを中断し対話入力を待つ |
$finish | Ends the simulation and returns to the operating system結束模擬並回到作業系統シミュレーションを終了し OS に戻る |
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 アクティブ)である。
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時間時刻 | preset | clear | q | qbar | Why原因理由 |
|---|---|---|---|---|---|
| 0 | x | x | x | x | Nothing assigned yet還沒有任何賦值まだ何も代入されていない |
| 10 | 0 | 1 | x | x | Preset assertedpreset 生效preset がアサートされる |
| 11 | 0 | 1 | 1 | x | G1: NAND with a 0 input → 1 after 1G1:NAND 有一個 0 輸入 → 1 個時間單位後為 1G1:NAND の入力に 0 があるので 1 時間単位後に 1 |
| 12 | 0 | 1 | 1 | 0 | G2: NAND(1, 1) → 0 |
| 20 | 1 | 1 | 1 | 0 | Hold保持保持 |
| 30 | 1 | 0 | 1 | 0 | Clear assertedclear 生效clear がアサートされる |
| 31 | 1 | 0 | 1 | 1 | G2 → 1 |
| 32 | 1 | 0 | 0 | 1 | G1: NAND(1, 1) → 0 |
| 40 | 1 | 1 | 0 | 1 | Hold保持保持 |
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 を一つずつ置くというものである。
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 で終わるのはなぜか。
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 を呼ぶ。
| Value值値 | Meaning意義意味 |
|---|---|
0 | Logical 0, FALSE邏輯 0,FALSE論理 0、FALSE |
1 | Logical 1, TRUE邏輯 1,TRUE論理 1、TRUE |
x | Unknown未知不定 |
z | High impedance高阻抗ハイインピーダンス |
| Port埠ポート | Parent module (actual)父模組(actual)親モジュール(実引数) | Child module (formal)子模組(formal)子モジュール(仮引数) |
|---|---|---|
input | net or registernet 或 registerネットまたはレジスタ | net |
output | net | net or registernet 或 registerネットまたはレジスタ |
inout | net | net |
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 ブロックである。そのスコープ内では識別子で直接参照できる。
| Net typeNet 型別ネットの型 | Meaning意義意味 |
|---|---|
wire (default)wire(預設)wire(既定) | Connectivity連線接続 |
tri | Same 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, wor | Wired AND / OR with multiple drivers多重驅動時的線接 AND/OR複数ドライバによるワイヤード AND/OR |
triand, trior | Tri-stated wired AND / OR三態的線接 AND/OR三状態のワイヤード AND/OR |
supply0, supply1 | Connected 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
assign), or implicitly by connecting to the output of a primitive or a module port.net 的值可以由連續賦值(assign)明確給定,也可以藉由接到基本元件或模組埠的輸出而隱含決定。ネットの値は連続代入(assign)によって明示的に与えられるか、プリミティブやモジュールポートの出力に接続することで暗黙的に決まる。x; nets with no driver are z.在 $t_{sim} = 0$ 時,有驅動的 net 取得驅動源的值,預設為 x;沒有驅動的 net 則為 z。$t_{sim} = 0$ では、駆動されているネットはドライバの値(既定は x)をとり、ドライバのないネットは z となる。wire.在連接中使用未宣告的識別字,會隱含地成為 1 位元的 wire。接続に使われた未宣言の識別子は、暗黙の 1 ビット wire になる。| Type型別型 | Stores儲存內容格納する値 |
|---|---|
reg | Logic value (unsigned)邏輯值(無號)論理値(符号なし) |
integer | Signed integer in two's complement (32 bits)二補數表示的有號整數(32 位元)2 の補数表現の符号付き整数(32 ビット) |
real | Double-precision real倍精度實數倍精度実数 |
time | 64-bit unsigned simulation time64 位元無號模擬時間64 ビット符号なしのシミュレーション時刻 |
realtime | Time as a real number以實數表示的時間実数で表した時刻 |
reg a, b; reg [15:0] counter; reg [7:0] shift_reg [0:15]; integer c;
x in simulation.register 在模擬開始時為 x。レジスタはシミュレーション開始時に x である。reg may never be the output of a primitive gate or the target of a continuous assignment.reg 絕不能當作基本閘的輸出,也不能當作連續賦值的目標。reg をプリミティブゲートの出力や連続代入の対象にすることはできない。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];
parameter size = 16; reg [size-1:0] a; parameter num_b = 2'b01; parameter av_delay = (min_delay + max_delay) / 2;
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
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
#(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) は宣言順にパラメータを上書きする。モジュールインスタンスに遅延を付けることはできないため、モジュールに付いた #(...) は常にパラメータを意味する。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)) のような名前付き上書きを使うこと。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;
(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) 合法 — 出力は子モジュール内部でレジスタにしてよい。
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 はどう読み出すか。
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] も使える。)
| 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縮減運算リダクション | 1 | Bit位元ビット | & ~& | ~| ^ ~^ ^~ |
| Logical邏輯論理 | 2 (! takes 1)2(! 為 1)2(! は 1) | Boolean value布林值ブール値 | ! && || == != === !== |
| Relational關係関係 | 2 | Boolean value布林值ブール値 | < <= > >= |
| Shift移位シフト | 1 (plus shift amount)1(再加上移位量)1(+シフト量) | Binary word二進位字組2 進ワード | << >> |
| Conditional條件条件 | 3 | Expression運算式式 | ? : |
assign sum = A + B; assign neg = -A;
| 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 つあれば | の結果が決まる。
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 を返す。
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 は使えない。| ?: | 0 | 1 | x |
|---|---|---|---|
| 0 | 0 | x | x |
| 1 | x | 1 | x |
| x | x | x | x |
| 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。
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 ビット)。
(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 ビットの文脈なので和の桁上がりが保たれる)。
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 をそれぞれ求めよ。
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。
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 を観測できる — デバッグ用の手法であり、合成はできない。
#( ) 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 で子モジュール内部の信号にアクセスできる。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 はいつ何に変化するか。
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 に変わる。
$monitor, $time, $stop, $finish.#(4, 5) in declaration order.#(4, 5) 依宣告順序。#(4, 5) は宣言順。defparam with hierarchical names.defparam 搭配階層式名稱。defparam は階層名を使う。?: with x select merges bitwise.?: 的選擇端為 x 時逐位元合併。選択が x の ?: はビットごとに合成する。