Priority Encoder

The Priority Encoder is a parameterized combinatorial implementation that takes input data, i_data, and outputs a binary priority-encoded output, o_data, where the least significant bit (bit 0) is the highest priority bit and the most significant bit (bit DATA_WIDTH-1) is the lowest priority bit. For example, the input 0b1001 would result in the output 0b00 with the output valid signal being 1. Because the input can be all zeros, it’s possible the output is not valid which is provided by the o_valid signal.

Parameters

  • DATA_WIDTH : data width in bits

Ports

  • i_data : input data

  • o_data : binary priority-encoded output data

  • o_valid : valid output

Source Code

priority_encoder.sv
 1`ifndef LIBSV_CODERS_PRIORITY_ENCODER
 2`define LIBSV_CODERS_PRIORITY_ENCODER
 3
 4module priority_encoder #(
 5    parameter integer DATA_WIDTH  /* verilator public_flat_rd */ = 4
 6) (
 7    input  logic [        DATA_WIDTH-1:0] i_data,
 8    output logic [$clog2(DATA_WIDTH)-1:0] o_data,
 9    output logic                          o_valid
10);
11
12    always_comb begin
13        bit stop;
14        o_data  = '0;
15        o_valid = 1'b0;
16        stop    = 1'b0;
17
18        for (int i = 0; i < DATA_WIDTH; ++i) begin
19            if (i_data[i] == 1'b1 && !stop) begin
20                o_data  = $clog2(DATA_WIDTH)'(i);
21                o_valid = 1'b1;
22                stop    = 1'b1;
23            end
24        end
25    end
26
27endmodule
28
29`endif  /* LIBSV_CODERS_PRIORITY_ENCODER */