# Resource Reuse
###### 參考資料:[Data reuse](https://hackmd.io/I7IW5xABSruvTWsEImCLDA)
## 1. 甚麼是 Resource Reuse
> [!Note] **Resource Reuse**
> - **一種 ==減少電路面積== 的技巧**
> ---
> - **在合成時,程式碼中每次 ==宣告 module==、==輸入運算符號== ==(`+`,`*`,`if`,`case`)==**
> **都會產生一個 ==運算電路==**
> - **這些 ==運算電路== 有大有小,若 ==複雜的電路== 被 ==產生很多次==,電路的 ==面積== 就會 ==變大==**
> ---
> - **所以,減少==複雜電路== 的宣告次數,改變 ==複雜電路== 的使用方式**
> - **讓 ==複雜電路== 變成可以被 ==重複利用== 的模組**
## 2. 如何實施 Resource Reuse
> [!Tip] **方法**
> 1. **找出哪些 ==模組== 被 ==重複使用== 且 ==模組複雜(面積大)==**
> 2. **通常會透過 ==調整模組輸入訊號== 達成 ==重複使用模組== 的目的**
> - **在 ==輸入訊號前== 加入 ==多工器==**
> - **可以在 ==某些狀態== 中 ==切換== 不同的訊號源**
## 3. ex:多工器選擇乘法運算
> [!Warning] **提示**
> - **面積大小:==乘法器 >> 多工器==**
> - **在原始程式中 ==乘法器== 被宣告 ==2次==**
> [!Important] **原始電路**
> ```css
> #sel
> #a ─┐ ┌────┐ ┌─────┐
> |─────►│MULT│─────►│ │
> #b ─┘ └────┘ │ │
> │M U X│─────►#result
> #c ─┐ ┌────┐ │ │
> |─────►│MULT│─────►│ │
> #d ─┘ └────┘ └─────┘
>
> ```
>
> ```verilog=
> module example(sel, a, b, c, d, result);
> input [3:0] a, b, c, d;
> input sel;
> output reg [7:0] result;
>
> always@(*) begin
> if (sel) result = a * b;
> else result = c * d;
> end
> endmodule
> ```
> [!Important] **改良電路**
> ```css
> #sel
> #a ─┐ ┌───┐ ┌──────┐
> |─────►│MUX│─────►│ │
> #c ─┘ └───┘ │ │
> #sel │ MULT │─────►#result
> #b ─┐ ┌───┐ │ │
> |─────►│MUX│─────►│ │
> #d ─┘ └───┘ └──────┘
>
> ```
>
> ```verilog=
> module example(sel, a, b, c, d, result);
> input [3:0] a, b, c, d;
> input sel;
> output reg [7:0] result;
>
> wire [3:0] tmp_0, tmp_1;
> assign tmp_0 = (sel)? a:c;
> assign tmp_1 = (sel)? b:d;
>
> always@(*) begin
> result = tmp_0 * tmp_1;
> end
> endmodule
> ```
## 4. 基本運算電路的面積比較
---