""" Encoders and decoders between binary and one-hot representation """ from migen.fhdl.structure import * from migen.fhdl.module import Module class Encoder(Module): """Encode one-hot to binary If `n` is low, the `o` th bit in `i` is asserted, else none or multiple bits are asserted. Parameters ---------- width : int Bit width of the input Attributes ---------- i : Signal(width), in One-hot input o : Signal(max=width), out Encoded binary n : Signal(1), out Invalid, either none or multiple input bits are asserted """ def __init__(self, width): self.i = Signal(width) # one-hot self.o = Signal(max=max(2, width)) # binary self.n = Signal() # invalid: none or multiple act = dict((1<