跳轉到

Basics

Simple wire

原題:Simple wire

只需要把 outin 連接起來。

Verilog
module top_module( input in, output out );
    assign out = in;
endmodule

Four wires

原題:Four wires

依照圖示接線:waxybzc

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,結果不會受影響,但這個差異值得先記起來。

Verilog
module top_module( input in, output out );
    assign out = ~in;
endmodule

AND gate

原題:AND gate

需要實作 AND gate,和前一題類似,要注意 bitwise AND 的符號是 &,logical AND 的符號是 &&

Verilog
module top_module( 
    input a, 
    input b, 
    output out );
    assign out = a & b;
endmodule

NOR gate

原題:NOR gate

需要實作 NOR gate,和前一題類似,要注意 bitwise OR 的符號是 |,logical OR 的符號是 ||

Verilog
module top_module( 
    input a, 
    input b, 
    output out );
    assign out = ~(a | b);
endmodule

XNOR gate

原題:XNOR gate

需要實作 XNOR gate,和前一題類似,要注意 bitwise XOR 的符號是 ^,Verilog 沒有 logical XOR 運算子。

Verilog
module top_module( 
    input a, 
    input b, 
    output out );
    assign out = ~(a ^ b);
endmodule

Declaring wires

原題:Declaring wires

引入 wire 的概念。當線路較複雜時,可以用 wire 建立中間節點,讓程式更容易閱讀。補充說明,inputoutput 本身就是 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 提升可讀性。

Verilog
module top_module ( 
    input p1a, p1b, p1c, p1d, p1e, p1f,
    output p1y,
    input p2a, p2b, p2c, p2d,
    output p2y );
    assign p1y = (p1a & p1b & p1c) | (p1d & p1e & p1f);
    assign p2y = (p2a & p2b) | (p2c & p2d);
endmodule