Basics
Simple wire
原題:Simple wire
只需要把 out 和 in 連接起來。
Four wires
原題:Four wires
依照圖示接線:w 接 a,x、y 接 b,z 接 c。
Verilog
module top_module(
input a,b,c,
output w,x,y,z );
assign w = a;
assign x = b;
assign y = b;
assign z = c;
endmodule
Inverter
原題:Inverter
需要實作 NOT gate。
值得注意的是,Verilog 的運算子可以分成 bitwise operator 和 logical operator,bitwise operator 會對 input 的每個 bit 分別運算,所以輸出的 bit 數會和 input 相同。而 logical operator 只做邏輯判斷,最後只會輸出 1 bit。
因此 bitwise NOT 的符號是 ~,logical NOT 的符號是 !。雖然本題的 input 只有 1 bit,結果不會受影響,但這個差異值得先記起來。
AND gate
原題:AND gate
需要實作 AND gate,和前一題類似,要注意 bitwise AND 的符號是 &,logical AND 的符號是 &&。
NOR gate
原題:NOR gate
需要實作 NOR gate,和前一題類似,要注意 bitwise OR 的符號是 |,logical OR 的符號是 ||。
XNOR gate
原題:XNOR gate
需要實作 XNOR gate,和前一題類似,要注意 bitwise XOR 的符號是 ^,Verilog 沒有 logical XOR 運算子。
Declaring wires
引入 wire 的概念。當線路較複雜時,可以用 wire 建立中間節點,讓程式更容易閱讀。補充說明,input 和 output 本身就是 wire。
Verilog
`default_nettype none
module top_module(
input a,
input b,
input c,
input d,
output out,
output out_n );
wire a_and_b, c_and_d;
assign a_and_b = a & b;
assign c_and_d = c & d;
assign out = a_and_b | c_and_d;
assign out_n = ~out;
endmodule
7458 chip
原題:7458 chip
和前一題類似,只是線路稍微複雜,可以直接寫出邏輯式,也可以使用 wire 提升可讀性。