One-hot Priority Encoder

The One-hot Priority Encoder is a parameterized combinatorial implementation that takes input data, i_data, and outputs a one-hot 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 0b0001.

Parameters

  • DATA_WIDTH : data width in bits

Ports

  • i_data : input data

  • o_data : one-hot priority-encoded output data

Source Code

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