# Verilog notes ###### tags:`verilog` ###### contributed by <[YongDa Su](https://github.com/YongDaSu/2022ComputerArchitecture/tree/main/Lab02)> ## 電路設計方法 * 邏輯閘層次 Gate Level * 資料流層次 Dataflow level * 行為層次 Behavior Level * 暫存器轉移層次 Register-Transfer Level, RTL (綜合以上三種) ## Verilog基本語法 * 模組宣告 * 定義模組 : module name (); * 結束定義 : endmodule * 範例 ![](https://i.imgur.com/Soee5LD.png) * 腳位宣告 * 在module定義中加入腳位 : module name (PortA, PortB, PortC,...); * 類型定義 : input PortA; output PortC; * 範例 ![](https://i.imgur.com/wujgZjR.png) ![](https://i.imgur.com/n4cQJYt.png) * 電線宣告 * wire(NAME1, NAME2,...) ### Structural description 邏輯匝層次設計範例 * 邏輯匝類型 邏輯匝名稱(輸出, 輸入1, 輸入2...) * 範例 ![](https://i.imgur.com/o8P4dCF.png) ![](https://i.imgur.com/qLyEi5t.png) * 完整多工器程式碼 ![](https://i.imgur.com/1FRCTuK.png) :::info :bulb: 不適合用在實際IC設計中,因為邏輯匝數量會太多,導致程式碼過大。但在最上層整合端可能也會用到。 ::: ### Data flow description * ![](https://i.imgur.com/MzIVPEW.png) * ![](https://i.imgur.com/bGlzicH.png) :::info :bulb: [組合電路](https://zh.wikipedia.org/zh-tw/%E7%BB%84%E5%90%88%E9%80%BB%E8%BE%91%E7%94%B5%E8%B7%AF)常用assign。 ::: ### Behavioral description #### #1 * ![](https://i.imgur.com/8kb4aVu.png) :::info :bulb: begin end 類似C的{},只有一行可以不加。 IN訊號有變化時就會改變always。 always也是組合電路常用寫法。 ::: #### #2 ![](https://i.imgur.com/Er8qOrE.png) :::info :bulb: 直接用Truth table描述, case的選用很重要。 ::: ## Module Module分兩種宣告模式 : (1)Position (2)Name ### Position 直接依照宣告的腳位順序來寫 ![](https://i.imgur.com/IKUnH8U.png) ![](https://i.imgur.com/tkmjhFH.png) ### Name ![](https://i.imgur.com/IftblaU.png) ![](https://i.imgur.com/kadCL4l.png) :::info :bulb: 要注意.的位置, .內部名稱(外部輸出入名稱)。 ::: ### always block * Combinational : always@(*) * (*) : all inputs * can't contain continous assignments * have a richer set of statement than 'assign' * Clocked : always(posedge clk) * always vs assign * these two statement is equivalent: 1. assign out1 = a & b | c ^ d; 2. always @(*) out2 = a & b | c ^ d; * left side of = 1. assign左邊要wire 2. alwaysy左邊要reg * ## 遇到的問題 ### 1.reg wire 在always的情況 1. ![](https://i.imgur.com/JdOxxNy.png) `宣告reg,則always括號中不需要有out_01,out_23,因為reg會儲存狀態。` 3. ![](https://i.imgur.com/gzCw3so.png) `假如宣告wire,則always括號中則需要新增out_1,out_2,其改變的訊號才會被使用。` ### 2.input output 型態 1. input只能為 `wire`。 2. output可以為 `reg` 或 `wire`,可在宣告中直接使用。例如`output reg [7:0]name` ## Reference [youtube](https://www.youtube.com/watch?v=0qUIl3wI_I8) [HDLBits](https://hdlbits.01xz.net/wiki/Module_shift)