Code conversion
The amaranth.lib.coding
module provides building blocks for conversion between different encodings of binary numbers.
One-hot coding
- class amaranth.lib.coding.Encoder
Encode one-hot to binary.
If one bit in
i
is asserted,n
is low ando
indicates the asserted bit. Otherwise,n
is high ando
is0
.- Parameters:
width (int) – Bit width of the input
- Attributes:
i (Signal(width), in) – One-hot input.
o (Signal(range(width)), out) – Encoded natural binary.
n (Signal, out) – Invalid: either none or multiple input bits are asserted.
- class amaranth.lib.coding.Decoder
Decode binary to one-hot.
If
n
is low, only thei
-th bit ino
is asserted. Ifn
is high,o
is0
.- Parameters:
width (int) – Bit width of the output.
- Attributes:
i (Signal(range(width)), in) – Input binary.
o (Signal(width), out) – Decoded one-hot.
n (Signal, in) – Invalid, no output bits are to be asserted.
Priority coding
- class amaranth.lib.coding.PriorityEncoder
Priority encode requests to binary.
If any bit in
i
is asserted,n
is low ando
indicates the least significant asserted bit. Otherwise,n
is high ando
is0
.- Parameters:
width (int) – Bit width of the input.
- Attributes:
i (Signal(width), in) – Input requests.
o (Signal(range(width)), out) – Encoded natural binary.
n (Signal, out) – Invalid: no input bits are asserted.