One-Hot State Decoder

Here is a quick way for you to decode a one hot state value into a binary representation. This code is built for the MIPS32 architecture.

/*************************************
* One-Hot Decoder
*      Usage: Convert the lower nibble of a 32bit
*            one hot active low to a binary number
*            representing the bit position.
*      Input: $a0 for the hot value (IE: 0b1101)
*     Output: $v0 is the decoded value (IE: 0x1)
*************************************/
.ent one_hot_decode
one_hot_decode:
        //Invert the incoming ( Remove if Active High )
        xori    $a0, $a0, 0xF
 
        //Count the number of leading zeros
        clz             $t1, $a0
 
        //v0 = 31 - #zeros
        li              $t0, 31
        subu    $v0, $t0, $t1
 
        jr              $ra
        nop
.end one_hot_decode

Leave a Reply