2013-06-27 14:56:57 -04:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
|
|
|
|
"""
|
|
|
|
Encoders and decoders between binary and one-hot representation
|
|
|
|
"""
|
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-06-27 14:56:57 -04:00
|
|
|
class Encoder(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
"""Encode one-hot to binary
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
If `n` is low, the `o` th bit in `i` is asserted, else none or
|
|
|
|
multiple bits are asserted.
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
width : int
|
|
|
|
Bit width of the input
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
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):
|
2015-04-13 15:21:30 -04:00
|
|
|
self.i = Signal(width) # one-hot
|
|
|
|
self.o = Signal(max=max(2, width)) # binary
|
|
|
|
self.n = Signal() # invalid: none or multiple
|
2015-04-13 14:07:07 -04:00
|
|
|
act = dict((1<<j, self.o.eq(j)) for j in range(width))
|
|
|
|
act["default"] = self.n.eq(1)
|
|
|
|
self.comb += Case(self.i, act)
|
2013-06-27 14:56:57 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-06-27 14:56:57 -04:00
|
|
|
class PriorityEncoder(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
"""Priority encode requests to binary
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
If `n` is low, the `o` th bit in `i` is asserted and the bits below
|
|
|
|
`o` are unasserted, else `o == 0`. The LSB has priority.
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
width : int
|
|
|
|
Bit width of the input
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
i : Signal(width), in
|
|
|
|
Input requests
|
|
|
|
o : Signal(max=width), out
|
|
|
|
Encoded binary
|
|
|
|
n : Signal(1), out
|
|
|
|
Invalid, no input bits are asserted
|
|
|
|
"""
|
|
|
|
def __init__(self, width):
|
2015-04-13 15:21:30 -04:00
|
|
|
self.i = Signal(width) # one-hot, lsb has priority
|
|
|
|
self.o = Signal(max=max(2, width)) # binary
|
|
|
|
self.n = Signal() # none
|
|
|
|
for j in range(width)[::-1]: # last has priority
|
2015-04-13 14:07:07 -04:00
|
|
|
self.comb += If(self.i[j], self.o.eq(j))
|
|
|
|
self.comb += self.n.eq(self.i == 0)
|
2013-06-27 14:56:57 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-06-27 14:56:57 -04:00
|
|
|
class Decoder(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
"""Decode binary to one-hot
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
If `n` is low, the `i` th bit in `o` is asserted, the others are
|
|
|
|
not, else `o == 0`.
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
width : int
|
|
|
|
Bit width of the output
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
Attributes
|
|
|
|
----------
|
|
|
|
i : Signal(max=width), in
|
|
|
|
Input binary
|
|
|
|
o : Signal(width), out
|
|
|
|
Decoded one-hot
|
|
|
|
n : Signal(1), in
|
|
|
|
Invalid, no output bits are to be asserted
|
|
|
|
"""
|
2013-11-29 04:36:48 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, width):
|
2015-04-13 15:21:30 -04:00
|
|
|
self.i = Signal(max=max(2, width)) # binary
|
|
|
|
self.n = Signal() # none/invalid
|
|
|
|
self.o = Signal(width) # one-hot
|
2015-04-13 14:07:07 -04:00
|
|
|
act = dict((j, self.o.eq(1<<j)) for j in range(width))
|
|
|
|
self.comb += Case(self.i, act)
|
|
|
|
self.comb += If(self.n, self.o.eq(0))
|
2013-06-27 14:56:57 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-06-27 14:56:57 -04:00
|
|
|
class PriorityDecoder(Decoder):
|
2015-04-13 15:21:30 -04:00
|
|
|
pass # same
|