HDL Bits Solution: Always_case2

In this quiz, we are going to create a priority encoder circuit. This circuit scans the input bits starting from bit 0 (LSB) to the highest bit (MSB). When it finds the first bit that is set to 1, it outputs the index of that bit in decimal form.

For example, if the input is 100100, the circuit will output 2 in decimal, or we can write it as 6'd2, because index [2] contains the value 1. If none of the input bits are high, the circuit will output 0.

This is my code for this quiz:

 
module top_module (
    input [3:0] in,
    output reg [1:0] pos  );
    
    always@(*) begin
        if(in[0]==1)
            pos = 4’d0;
        else if(in[1]==1)
            pos = 4’d1;
        else if(in[2]==1)
            pos = 4’d2;      
        else if(in[3]==1)
            pos = 4’d3;  
        else 
            pos = 4’d0;       
    end    
 
endmodule
Reference: https://hdlbits.01xz.net/wiki/Always_case2

Leave a Reply

Your email address will not be published. Required fields are marked *