forked from SJTU-YONGFU-RESEARCH-GRP/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurable_priority_encoder.v
More file actions
executable file
·123 lines (116 loc) · 5.22 KB
/
configurable_priority_encoder.v
File metadata and controls
executable file
·123 lines (116 loc) · 5.22 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Configurable Priority Encoder
*
* This module implements a priority encoder that converts a one-hot or
* multi-hot input vector into a binary-encoded output representing the
* highest priority active input. Priority is determined by bit position,
* with MSB having highest priority.
*
* Key Features:
* - Priority encoding: Converts input vector to binary index
* - MSB priority: Highest bit position has highest priority
* - Valid output: Indicates when at least one input is active
* - Configurable width: Any input width via INPUT_WIDTH parameter
*
* Priority Encoding:
* - Input: One-hot or multi-hot vector (e.g., 0b00101000)
* - Output: Binary index of highest priority active bit (e.g., 5 for bit 5)
* - Priority: MSB (bit N-1) has highest priority, LSB (bit 0) has lowest
*
* Operation:
* - Scans from MSB to LSB
* - Finds first active bit (highest priority)
* - Outputs binary index of that bit
* - Sets valid=1 if any bit is active, valid=0 if all bits are zero
*
* Use Cases:
* - Interrupt controllers
* - Request arbitration
* - Address decoding
* - Priority-based selection
*
* @param INPUT_WIDTH Width of input vector (default: 8 bits)
* Input range: 0 to 2^INPUT_WIDTH - 1
* @param OUTPUT_WIDTH Width of output (default: $clog2(INPUT_WIDTH))
* Automatically calculated from INPUT_WIDTH
*
* @input request[INPUT_WIDTH-1:0] Input request vector
* Each bit represents a request, MSB has highest priority
* @output grant_index[OUTPUT_WIDTH-1:0] Binary-encoded index of highest priority active bit
* @output valid High when at least one input is active, low when all inputs are zero
*/
module configurable_priority_encoder #(
parameter INPUT_WIDTH = 8, // Width of the input vector
parameter OUTPUT_WIDTH = $clog2(INPUT_WIDTH) // Width of the output (calculated)
) (
input wire [INPUT_WIDTH-1:0] request, // Input request vector
output reg [OUTPUT_WIDTH-1:0] grant_index, // Binary encoded output
output reg valid // Valid output signal
);
integer i;
// ============================================================================
// Priority Encoding Logic (Combinational)
// ============================================================================
// Scans input vector from MSB to LSB, finds highest priority active bit
always @(*) begin
// ========================================================================
// Default Values
// ========================================================================
// Default: no valid output, index is zero
valid = 1'b0;
grant_index = {OUTPUT_WIDTH{1'b0}};
// ========================================================================
// Priority Encoding (MSB has Highest Priority)
// ========================================================================
// Scan from MSB to LSB to find highest priority active bit
for (i = INPUT_WIDTH-1; i >= 0; i = i - 1) begin
if (request[i]) begin
// ================================================================
// Highest Priority Active Bit Found
// ================================================================
// Assign the index of this bit as the output
// This is the highest priority because we scan from MSB
grant_index = i;
// Set valid flag to indicate at least one input is active
valid = 1'b1;
// ================================================================
// Force Loop Exit
// ================================================================
// Break out of the loop using a manual approach
// Set i to -1 to force loop exit (simulator-compatible alternative to 'break')
i = -1;
end
end
// ========================================================================
// No Active Input
// ========================================================================
// If no input is active, valid remains 0 and grant_index remains 0
// (default values are already set above)
end
// Alternative implementation without using break (compatible with all simulators)
/*
always @(*) begin
valid = 1'b0;
grant_index = {OUTPUT_WIDTH{1'b0}};
// Priority encoding logic using casez
casez (request)
{INPUT_WIDTH{1'b0}}: begin
// All zeros case
valid = 1'b0;
grant_index = {OUTPUT_WIDTH{1'b0}};
end
default: begin
// Find the highest priority bit (MSB)
valid = 1'b1;
// Priority encoding logic
for (i = INPUT_WIDTH-1; i >= 0; i = i - 1) begin
if (request[i]) begin
grant_index = i[OUTPUT_WIDTH-1:0];
i = -1; // Force exit from the loop
end
end
end
endcase
end
*/
endmodule