forked from SJTU-YONGFU-RESEARCH-GRP/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriority_encoder.v
More file actions
executable file
·82 lines (78 loc) · 3.18 KB
/
priority_encoder.v
File metadata and controls
executable file
·82 lines (78 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Priority Encoder
*
* This module implements a priority encoder that finds the highest-priority
* active bit in the input vector. Higher bit indices have higher priority.
*
* Key Features:
* - Priority encoding: Highest active bit index is encoded
* - Valid flag: Indicates if any input bit is active
* - Configurable width: Any input width via WIDTH parameter
* - Combinational: Outputs available in same cycle as inputs
*
* Operation:
* - Scans input from MSB (highest index) to LSB (lowest index)
* - Encodes the index of the first (highest priority) active bit found
* - Sets valid flag if any bit is active
* - If no bits active: out = 0, valid = 0
*
* Priority Order:
* - Bit [WIDTH-1] has highest priority
* - Bit [0] has lowest priority
* - If multiple bits are active, highest index wins
*
* Use Cases:
* - Interrupt controllers (highest priority interrupt)
* - Arbitration (highest priority request)
* - One-hot to binary conversion (with priority)
* - Address encoding in CAMs
*
* @param WIDTH Width of input vector (default: 8 bits)
* @param OUT_WIDTH Width of output (calculated as log2(WIDTH))
*
* @input in[WIDTH-1:0] Input vector (one-hot or sparse)
* @output out[OUT_WIDTH-1:0] Encoded index of highest priority active bit
* @output valid High when at least one input bit is active
*/
module priority_encoder #(
parameter WIDTH = 8,
parameter OUT_WIDTH = $clog2(WIDTH)
) (
input wire [WIDTH-1:0] in,
output reg [OUT_WIDTH-1:0] out,
output reg valid
);
// ============================================================================
// Priority Encoding Logic
// ============================================================================
// Combinational logic: outputs available immediately based on inputs
integer i; // Loop counter
always @(*) begin
// ========================================================================
// Initialize Outputs
// ========================================================================
// Default: no active bit found
valid = 0;
out = {OUT_WIDTH{1'b0}};
// ========================================================================
// Priority Search
// ========================================================================
// Search from MSB (highest index) to LSB (lowest index)
// First active bit found has highest priority
for (i = WIDTH-1; i >= 0; i = i - 1) begin
if (in[i]) begin
// ================================================================
// Active Bit Found
// ================================================================
// Encode the index of this bit
out = i[OUT_WIDTH-1:0];
// Set valid flag to indicate an active bit was found
valid = 1;
// Break the loop by setting i to -1
// This ensures we only encode the highest priority (first found) bit
i = -1;
end
end
// Note: If loop completes without finding active bit, outputs remain at defaults
end
endmodule