# Three Types of Design Modeling
###### tags: `Digital IC Design`
[回到主頁面](https://hackmd.io/@derek8955/BkK2Nb5Jo/https%3A%2F%2Fhackmd.io%2FdpcBlBL8TlShpQ-wSi9Quw)
```verilog=
// verilog 的架構
module 模組名稱( 輸出入埠名稱 );
// 01 輸出入埠 敘述
// 02 資料型態 敘述
// 03 內部電路 敘述
endmodule
```
```verilog=
// Gate level Modeling
module example( a, b );
input a;
output b;
not U1 (b,a);
// b、a 只能被宣告成 wire
// 所以第四行不能打成 output reg b;
// 沒有宣告 data type,default 是 wire
// output wire b; 等同於 output b;
endmodule
----------------------------------------------------------------------------------
// Dataflow Modeling
module example( c, a, b );
input a,b;
output c;
assign c = a * b;
// 使用 assign 讓 c = a * b;
// 使用 dataflow modeling 時,等號左邊的 signal c 只能被宣告成 wire
// Input Signal 是從外部傳進來,所以沒辦法對其進行更改,自然只能寫在等號右邊
endmodule
----------------------------------------------------------------------------------
// Behavioral Modeling( Procedural Blocks )
// no clock
module example1( c, a, b );
input a,b;
output reg c;
// assume a = 1, b = 2
always@( * ) begin
// 1. 括號中的內容我們稱作 sensitivity list
// 2. sensitivity list 中只有一個 * ,代表電路裡所有的 signals 有變化( 0 - > 1 or 1 -> 0 )
// 則會執行 begin end 中的內容
// 3. 這邊同樣要注意,使用 behavioral modeling 時,等號左邊的 signal c 只能被宣告成 reg
c = a;
c = b;
// result : c = 2
// procedural blocks裡的電路敘述是有先後順序之分的,會先執行39行再執行40行
end
endmodule
// no clock
module example2( c, a, b );
input a,b;
output reg c;
always@( * ) begin
c = a * b;
end
// 想想看這樣的 code 描述出的電路會長怎樣,答案在下方
endmodule
// with clock
module example3( clk, c, a, b );
input clk, a,b;
output reg c;
always@( posedge clk ) begin
// posedge means positive edge trigger
// 所以如果現在想使用負緣觸發則更改成 negedge
c <= a * b;
// <= 將在後面說明
end
// 想想看這樣的 code 描述出的電路會長怎樣,答案在下方
endmodule
// with clock and asynchronous reset
module example4( clk, rst_n, c, a, b );
input clk, rst_n, a,b;
output reg c;
always@( posedge clk or negedge rst_n ) begin // clock 正緣觸發, reset 負緣觸發
if( !rst_n ) c <= 0;
else c <= a * b;
end
endmodule
// with clock and synchronous reset
module example5( clk, rst_n, c, a, b );
input clk, rst_n, a,b;
output reg c;
always@( posedge clk ) begin // sensitivity list 去掉 reset 就是 synchronous reset
if( !rst_n ) c <= 0;
else c <= a * b;
end
endmodule
```
:::warning
:heavy_exclamation_mark: 回顧之前的 [04 - Data type](https://hackmd.io/PUFV4TJWR46sFqgljeOOJA),就可知道為何 behavioral modeling 一定要用 reg, dataflow modeling 一定要用 wire
:::
|||
|:---:|:---:|
| example2 | example3 |
### <font color = "blue"> Apendix </font>
> Q : synchronous reset vs. asynchronous reset 哪個比較好 ?
> A : 各有好處
:::success
Syntax:
synchronous reset => <font color="red">always @( posedge clk )</font>
asynchronous reset => <font color="red">always @( posedge clk or negedge rst_n) </font>
:::
- synchronous reset:
- 優點
1. 防止 reset 訊號有 glitch : 在整個 circuit 運作過程中,訊號可能不穩定,導致突然有個 pulse,此時如果使用 synchronous reset 便可避免 glitch 產生
- 缺點
1. 一定要等到 clock 正緣/負緣 到來時才做 reset
2. Data path 會有額外的delay
3. 需要 pulse stretcher來保證reset有被sample到
```verilog=
always @( posedge clk ) begin
if (reset) b <= 0;
else b <= a;
end
```
||
|:---:|
| synchronous reset with MUX|
- asynchronous reset(較常使用)
- 優點
1. reset訊號即時
2. 如果有 gated clock 還是能被 reset
- 缺點
1. 可能會有 glitch 發生
2. [STA](https://hackmd.io/@derek8955/BkK2Nb5Jo/https%3A%2F%2Fhackmd.io%2FBOMX17vmRBumB39IhTR3Eg) 需要另外check removal and recovery
更多reset的相關知識可以看以下文章:
http://www.sunburst-design.com/papers/CummingsSNUG2003Boston_Resets.
http://www.sunburst-design.com/papers/CummingsSNUG2002SJ_Resets.pdf