# Module Architecture
當我們將想法畫出來後,要如何用 Verilog 進行描述呢 ?
接下來將以 Comparator 為例,介紹畫出 Block Diagram 後,再從 Block Diagram 如何對應到 Verilog 描述,此篇著重於介紹 Verilog 中 Module Architecture。
## Comparator 比較器
需求說明:
* 1 bit Comparator 電路。
* Input port
* 1 bit A signal
* 1 bit B signal
* Output port
* 1 bit gt signal
* 1 bit lt signal
* 1 bit eq signal
* function 說明
* 當輸入兩個 1 bit 的 signal 時,能比較出兩個 signal 大小,並輸出 1 bit 的 gt, lt, eq signal。
| Signal| 說明 |
| --- | --- |
| gt | A>B 成立則設 1'b1, 反之為 1'b0。|
| lt | A<B 成立則設 1'b1, 反之為 1'b0|
| eq | A==B 成立則設 1'b1, 反之為 1'b0|
---
## Block Diagram
由上述需求,畫成 Block Diagram,如下圖所示:

---
## Module 概念
在 Verilog 中,會有一個 Top Module 如同一個大黑箱子,給予 Input / Output port 連接,內部可能放多個小的 Module(Module 連接部分請參考 [Module Connection](https://hackmd.io/s/ryk3FvkYg))。
* Module hierarchy
* 只有一個 Top Module,但內部可能包含其他的小 Module,如下圖所示。

* 上圖若以階層方式表達,則如下圖所示。

* 模組要如何切割 ?
* 視 Design 大小而定,若將系統模組切太細,會使之後使用合成工具上,電路內部最佳化的效果不佳,此部分之後將在 [Circuits 範例](https://hackmd.io/s/H1lFtSgYg) 中介紹。
---
## Module Architecture
### module / endmodule
* Verilog 中每個 module 都是由 module / endmodule 所包起來。
* 撰寫時請設簡單明瞭的名稱給 module。
* 依據 Block Diagram,將連接此 module 的 port(輸入及輸出),寫入括號內部 (portA, portB, portC)。
* 同一份 verilog code(.v檔)中,可以有多個 module / endmodule 。
* **請注意!!** module / endmodule **裡面不可以再包住另一個** module / endmodule。
:::success
<font color="black" size="5">module/endmodule</font>
```verilog=
module ModuleName(portA, portB, portC);
//請注意!! 最後要記得加 ';'
...
...
endmodule
```
:::

:::success
<font color="black" size="5">Example: Comparator</font>
```verilog=
module Comparator(A, B, gt, lt, eq);
//請注意!! 最後要記得加 ';'
...
...
endmodule
```
:::

### input / output port
在此介紹兩種宣告 input / output port 的方式,主要差異在於 Verilog HDL 新版與舊版改革而已,寫哪種方式,Tool 都是可以接受的,同學依自己喜好選擇即可。
#### **Port 宣告 舊版語法**
* module 依據括號內部,可知道目前連接的 port 有哪些,但它無法知道哪些是 input port 哪些又是 output port,所以需要另外宣告 module 的 input port 與 output port。
* 若沒有特別宣告bit數,預設皆為 1-bit。所以下面 Comparator 範例中 A、B 為 1 bits,gt、lt、 eq 則各為 1 bit。
* 在宣告 input/output 時,可同時宣告資料型態(reg/wire),default 的資料型態為 wire。
:::success
<font color="black" size="5">Example: Comparator</font>
```verilog=
module Comparator(A, B, gt, lt, eq); //有五個 port
input A, B; //1 bit signal
output gt; //1 bit signal
output lt;
output eq;
...
endmodule
```
:::

#### **Port 宣告 新版語法**
* module 的 input / output port 宣告**直接寫在括號中,不需另外宣告**。
* 若沒有特別宣告bit數,預設皆為 1-bit。所以下面 Comparator 範例中 A、B 為 1 bits,gt、lt、 eq 則各為 1 bit。
* 在宣告 input/output 時,可同時宣告資料型態(reg/wire),default 的資料型態為 wire。
:::success
<font color="black" size="5">Example: Comparator</font>
```verilog=
module Comparator(
input A, //1 bit signal
input B,
output gt; //1 bit signal
output lt;
output eq;
);
...
endmodule
```
:::

#### input / output port 的 data type
* module 內 input port 的 data type **只能是 wire**;output port 的 data type 則可為 **wire 或 reg**。如下圖所示:

* 常見錯誤
* input port 宣告成 **reg type** 造成 syntax error,請記得 input port 為 wire type。
#### **bit 宣告語法**
如果我們的 signal 不只 1 bit 呢 ?可以透過中括號 [] 宣告 signal 有幾個 bit。
:::success
<font color="black" size="5">Example: Comparator 3-bit</font>
```verilog=
module Comparator(A, B, gt, lt, eq);
input [2:0]A, B; //3 bits signal
output gt; //1 bit signal
output lt;
output eq;
...
endmodule
```
:::

>程式碼說明:
>其中 [2:0] 代表有 3 bits,index 從左至右為2、1、0。假設我們給予 A = 3'b010,則 A[2] 就是最左邊的位元,即為 0;A[1]代表從右邊數來第二個位元,即為 1。
>這種方法是最常用的表示法,即最左邊的 bit 權重最重,也就是 Most Significant Bit, MSB;最右邊的權重最小,也就是 Least Significant Bit, LSB。

>(不常用)另外一種寫法,則是[0:2],同樣代表 3 bits,但 index 則是左至右為0、1、2。假設我們給予 A = 3'b110,則 A[2]就是最右邊的位元,即為 0;A[1]代表從左邊數來第二個位元,即為 1。**此種方法不常用,建議同學皆用前面所介紹的表示法。**
---
## 總結
以上為介紹從 Block diagram 到如何使用 Verilog 描述電路,並說明 Verilog 中的 module 結構與語法。
# [Home Page:hatched_chick: ](https://hackmd.io/s/BkYeCF5Og)