4-Bit Priority Encoder with Valid Signal
Design a synthesizable 4-bit priority encoder where the highest-index active bit takes priority. Outputs a 2-bit encoded grant and an active-high valid flag.
Signal Interface Specification
| Port | Direction | Engineering Description |
|---|---|---|
req[3:0] | input | Active-high request lines from 4 clients |
grant[1:0] | output | 2-bit binary index of the highest priority active request |
valid | output | High when at least one request line is asserted |
โฑ๏ธ Timing & Latency Constraint
Pure combinational logic. Maximum propagation delay should not exceed 2 gate delays.
โ ๏ธ Pitfalls & Edge Cases
Failing to prioritize req[3] over req[2:0]; asserting valid when all inputs are 0; incomplete case statements causing latch inference.
๐ก View Engineering Hint
Use a priority if-else ladder or synthesizable casez (with 4'b1??? -> 2'b11). Note that casez requires careful syntax to avoid simulation-synthesis mismatches.
๐ View Verified Reference Solution & Trade-off Analysis
module priority_encoder4 (
input wire [3:0] req,
output reg [1:0] grant,
output wire valid
);
assign valid = |req; // Reduction OR across all requests
always @(*) begin
if (req[3]) grant = 2'b11;
else if (req[2]) grant = 2'b10;
else if (req[1]) grant = 2'b01;
else grant = 2'b00;
end
endmodule
Hardware Trade-off: Priority if-else infers a cascaded mux structure with $O(N)$ delay. For large $N$ (e.g. 64 bits), a parallel tree lookahead encoder is preferred.