[RTLLM-p015] Arithmetic/Comparator/comparator_4bit
4 位元減法式比較器
題目說明
請設計一個名為 comparator_4bit 的 Verilog 模組,使用組合邏輯比較兩個 4 位元無號二進位數 A 與 B。
模組須透過減法運算 A - B 判斷兩數的大小關係,並輸出三個互斥的比較結果:
A_greater:當A > B時為 1。A_equal:當A == B時為 1。A_less:當A < B時為 1。
三個輸出必須互斥,任一時刻只能有一個輸出為 1。
模組介面
| 埠 | 方向 | 位元寬度 | 說明 |
|---|---|---|---|
A |
input | 4 | 第一個 4 位元無號運算元 |
B |
input | 4 | 第二個 4 位元無號運算元 |
A_greater |
output | 1 | 當 A > B 時輸出 1 |
A_equal |
output | 1 | 當 A == B 時輸出 1 |
A_less |
output | 1 | 當 A < B 時輸出 1 |
設計要求
- 模組名稱必須為
comparator_4bit。 - 使用組合邏輯完成設計。
- 使用減法結果判斷
A與B的關係。 - 減法必須保留額外的借位資訊。
A_greater、A_equal與A_less必須互斥。- 輸入視為無號數。
比較規則
當 A - B 發生借位時:
A_less = 1
當減法結果為零時:
A_equal = 1
當沒有借位且減法結果不為零時:
A_greater = 1
程式設計模板
module comparator_4bit (
input [3:0] A,
input [3:0] B,
output A_greater,
output A_equal,
output A_less
);
// Write your code here
endmodule
評論