bitwise

Modules

ToOneHot #(int SIZE)

module ToOneHot #(int SIZE) {
    interface ToOneHot : int#(FROM: 0, TO: SIZE - 1) selection'0 -> bool[SIZE] bits'0
}
ToOneHot default clock clk selection bits

Creates a One-Hot encoding of the given integer.

Examples:

ToOneHot#(SIZE: 5)(3) == [false, false, false, true, false]
ToOneHot#(SIZE: 2)(0) == [true, false]

FirstSetBit #(int SIZE)

module FirstSetBit #(int SIZE) {
    trigger is_nonzero : int#(FROM: 0, TO: SIZE) first_nonzero'0
    action find : bool[SIZE] bits'0
}
FirstSetBit default clock clk find bits is_nonzero first_nonzero

This module returns the index of the first true bit in the given bitstring. If the whole bitstring is false, then is_nonzero is not fired. If there is at least one set (‘1’) bit, is_nonzero triggers with the index of the first ‘1’ bit.

Usage:

FirstSetBit find_first_bit
find_first_bit.find([false, false, true, false, true])
when find_first_bit.is_nonzero : int first_nonzero {
    // Fires with `2`
}

PopCount #(int SIZE)

module PopCount #(int SIZE) {
    interface PopCount : bool[SIZE] bits'0 -> int#(FROM: 0, TO: SIZE+1) popcount
}
PopCount default clock clk bits popcount

Counts the number of set (‘1’) bits in the bitstring.

Examples:

PopCount([true, false, true, true, true]) == 4
PopCount(8'b00111011) == 5

BitwiseIntSplit #(int TO, int LOWER_BITS)

module BitwiseIntSplit #(int TO, int LOWER_BITS) {
    interface BitwiseIntSplit : int#(FROM: 0, TO) v'0 -> int#(FROM: 0, TO: UPPER_TO) upper'0, int#(FROM: 0, TO: pow2#(E: LOWER_BITS)) lower'0
}
BitwiseIntSplit default clock clk v upper lower

Splits the given integer. The upper [LOWER_BITS:] bits are returned as upper, the lower [:LOWER_BITS] bits are returned in lower See also BitwiseIntConcat.

BitwiseIntConcat #(int UPPER_TO, int LOWER_BITS)

module BitwiseIntConcat #(int UPPER_TO, int LOWER_BITS) {
    interface BitwiseIntConcat : int#(FROM: 0, TO: UPPER_TO) upper'0, int#(FROM: 0, TO: pow2#(E: LOWER_BITS)) lower'0 -> int#(FROM: 0, TO: UPPER_TO * pow2#(E: LOWER_BITS)) v'0
}
BitwiseIntConcat default clock clk upper lower v

Recombines a lower and upper integer parts. See also BitwiseIntSplit.

AlignToPow2 #(int FROM, int TO, int LOWER_BITS)

module AlignToPow2 #(int FROM, int TO, int LOWER_BITS) {
    interface AlignToPow2 : int#(FROM, TO) i'0 -> int#(FROM: ALIGNED_FROM, TO: ALIGNED_TO) o'0
}
AlignToPow2 default clock clk i o

Sets the lower [:LOWER_BITS] bits to false, thus aligning the value Works for unsigned and signed integers alike.