---
tags: verilog
---
# verilog_入門
## Final Status (可能會遇到的狀況)
1. Compile Error : Circuit did not compile.
(編譯錯誤:電路未編譯)
3. Simulation Error : Circuit compiled successfully, but simulation did not complete.
(模擬錯誤:電路編譯成功,但模擬尚未完成)
5. Incorrect : Circuit compiled and simulated, but the outputs did not match the reference.
(不正確:電路與模擬的輸出與參數對不匹配)
7. success! : Circuit was correct
(成功:電路正確)
### 題目一:
Build a circuit with no inputs and one output.
That output should always drive 1 (or logic high).
(建立一個沒有輸入和一個輸出的電路,輸出始終為1)
### verilog程式碼:
```verilog=
module top_module(output one);
assign one=1'b1;
endmodule
//1. module 檔名 (輸出,輸入)
//2.assign 賦予參數一個值,one = 1'b1; (等號右邊的值放進左邊的參數中)
// 1'b1 = 1個bit 'b1 :二進制的1
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/QxuJsyF.jpg)
### 題目二:
Build a circuit with no inputs and one output that outputs a constant 0
(建立一個沒有輸入和一個輸出的電路,輸出為0)
### verilog程式碼:
```verilog=
module top_module(output zero);
assign zero=1'b0;
endmodule
//1. module 檔名 (輸出,輸入)
//2.assign 賦予參數一個值 , zero = 1'b0; (等號右邊的值放進左邊的參數中)
// 1'b0 = 1個bit 'b0 :二進制的0
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/5X4OPky.jpg)
### 題目三:
Create a module with one input and one output that behaves like a wire.
(建立一個輸入和一個輸出的電路)
![](https://i.imgur.com/6vSzx7V.jpg)
### verilog程式碼:
```verilog=
module top_module( input in, output out );
assign out = in;
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值 (也可以賦予參數另一個參數的值)
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/1rd0V7x.jpg)
### 題目四:
Create a module with 3 inputs and 4 outputs that behaves like wires that makes these connections:
a -> w
b -> x
b -> y
c -> z
(創造一個3輸入4輸出如下圖之電路)
![](https://i.imgur.com/6vSzx7V.jpg)
### verilog程式碼:
```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
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值 (也可以賦予參數另一個參數的值)
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/ktiwXhi.jpg)
### 題目五:
Create a module that implements a NOT gate.
(創造一個 NOT邏輯閘)
![](https://i.imgur.com/XMb7kaD.jpg)
### verilog程式碼:
```verilog=
module top_module( input in, output out );
assign out = ~in;
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值 (也可以賦予參數另一個參數的值)
// 反向可以使用( ~ , ! )符號來代替。
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/oFJZ1C1.jpg)
### 題目六:
Create a module that implements an AND gate.
(創造一個 AND邏輯閘)
![](https://i.imgur.com/l9QY0V5.jpg)
### verilog程式碼:
```verilog=
module top_module( input a,b, output out );
assign out = a&b;
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值,(也可以賦予參數另一個參數的值)
// (也可以賦予運算結果值)
// 及閘可以使用(&) 符號來代替。
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/LgLO3cg.jpg)
### 題目七:
Create a module that implements a NOR gate.
(創造一個 AND邏輯閘)
![](https://i.imgur.com/lq6P8Mw.jpg)
### verilog程式碼:
```verilog=
module top_module( input a,b, output out );
assign out = ~(a|b);
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值,(也可以賦予參數另一個參數的值)
// (也可以賦予運算結果值)
// 或閘可以使用(|) 符號來代替。
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/UJGjn5g.jpg)
### 題目八:
Create a module that implements an XNOR gate.
(創造一個 XNOR邏輯閘)
![](https://i.imgur.com/xiQufZJ.jpg)
### verilog程式碼:
```verilog=
module top_module( input a,b, output out );
assign out = ~(a^b);
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值,(也可以賦予參數另一個參數的值)
// (也可以賦予運算結果值)
// 互斥或閘可以使用(^) 符號來代替。 XOR=互斥或閘 XNOR = 反互斥或閘
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/3t1sStH.jpg)
### 題目九:
Declaring wires.
(wire的使用)
![](https://i.imgur.com/vIXgMdB.jpg)
### verilog程式碼:
```verilog=
module top_module(input a,b,c,d,output out,out_n);
wire w1,w2;
assign w1 = a&b;
assign w2 = c&d;
assign out = w1|w2;
assign out_n = ~out;
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值,(也可以賦予參數另一個參數的值)
// (也可以賦予運算結果值)
// 把module想像成一個黑盒子,我們只看的到輸入及輸出,其餘內部的線皆使用wire表示
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/7WQ48H8.jpg)
### 題目十:
The 7458 is a chip with four AND gates and two OR gates.
(7458晶片含有4個AND閘2個OR閘)
![](https://i.imgur.com/vIXgMdB.jpg)
### verilog程式碼:
```verilog=
module top_module ( input p1a, p1b, p1c, p1d, p1e, p1f,p2a, p2b, p2c, p2d, output p1y,p2y);
wire w1,w2,w3,w4;
assign w1 = p2a & p2b;
assign w2 = p2c & p2d;
assign p2y = w1|w2;
assign w3 = p1a & p1c & p1b;
assign w4 = p1f & p1e & p1d;
assign p1y = w3|w4;
endmodule
//1. module 檔名 (輸出,輸入) or (直接在定義 input in output out)
//2.assign 賦予參數一個值,(也可以賦予參數另一個參數的值)
// (也可以賦予運算結果值)
// 把module想像成一個黑盒子,我們只看的到輸入及輸出,其餘內部的線皆使用wire表示
//endmodule 結束此區域程式
```
### Waveform (波形圖)
![](https://i.imgur.com/RXpJ94s.jpg)