# Verilog 介紹 [TOC] ## 前言 `Verilog`屬於硬體語言,在邏輯上會和`C/C++`不太相同。在這邊可以把寫`Verilog`的過程想作是拼積木,由小而大堆砌構成。而當中我們會用到一些基本的材料像是 AND、OR、NOT...等等(也可以自定義或參考課堂上學到的),藉由巧妙的設計去實現各種功能。以下會帶大家理解`Verilog`大致上的思路與架構。 ## 基本架構 一個大的架構通常包含數個較小的 module,如果把一個 module 想像是一塊積木,`input port`跟`output port`是它的表面,在設計的過程當中,就是要確保 module 內部這些小零件能正確地組在一起,讓 input 跟 output 有理想的結果。 **<font color=#bf2222> 語法:</font>** ```verilog= module module_name (輸入輸出 port 名稱); 輸入輸出 port 定義 資料型態定義 電路描述 endmodule ``` * `module_name`是 module 的名字,不能與其他 module 重複。 * `input port`跟`output port`要在`module name`後面的 () 中定義。 * 在 module 跟 endmodule 中間加入`資料型態定義`及`電路描述`。 --- **<font color=#bf2222>範例:</font>** 就讓我們用 majority function 的例子來了解`Verilog`要怎麼寫吧! | ![](https://i.imgur.com/CsNj2yr.png) | | -------- | 首先,畫出 majority function 的 Truth table,並且使用 Kmap 簡化得到`Out = XY + XZ + YZ`。我們會需要三個 AND gate 跟一個 OR gate(右側圖為 block diagram )。接著,我們以`Verilog`的方式實行,可參考以下程式碼: ```verilog= module MAJORITY(X, Y, Z, Out); input X, Y, Z; output Out; wire W0, W1, W2; and A_0(W0, X, Y); and A_1(W1, X, Z); and A_2(W2, Y, Z); or O_0(Out, W0, W1, W2); endmodule ``` 可以看到當中 MAJORITY 就是這個 module 的`module_name`,X、Y、Z 是`input port`,Out 是`output port`,中間有另外定義`wire`型態的 W0、W1、W2,並在下方加入 AND、OR 等`電路描述`,完成 majority function 的功能。 # [HOMEPAGE](https://hackmd.io/s/HJdaLPTQV)