2 Encoders and decoders between binary and one-hot representation
5 from litex
.gen
.fhdl
.structure
import *
6 from litex
.gen
.fhdl
.module
import Module
10 """Encode one-hot to binary
12 If `n` is low, the `o` th bit in `i` is asserted, else none or
13 multiple bits are asserted.
18 Bit width of the input
24 o : Signal(max=width), out
27 Invalid, either none or multiple input bits are asserted
29 def __init__(self
, width
):
30 self
.i
= Signal(width
) # one-hot
31 self
.o
= Signal(max=max(2, width
)) # binary
32 self
.n
= Signal() # invalid: none or multiple
33 act
= dict((1<<j
, self
.o
.eq(j
)) for j
in range(width
))
34 act
["default"] = self
.n
.eq(1)
35 self
.comb
+= Case(self
.i
, act
)
38 class PriorityEncoder(Module
):
39 """Priority encode requests to binary
41 If `n` is low, the `o` th bit in `i` is asserted and the bits below
42 `o` are unasserted, else `o == 0`. The LSB has priority.
47 Bit width of the input
53 o : Signal(max=width), out
56 Invalid, no input bits are asserted
58 def __init__(self
, width
):
59 self
.i
= Signal(width
) # one-hot, lsb has priority
60 self
.o
= Signal(max=max(2, width
)) # binary
61 self
.n
= Signal() # none
62 for j
in range(width
)[::-1]: # last has priority
63 self
.comb
+= If(self
.i
[j
], self
.o
.eq(j
))
64 self
.comb
+= self
.n
.eq(self
.i
== 0)
67 class Decoder(Module
):
68 """Decode binary to one-hot
70 If `n` is low, the `i` th bit in `o` is asserted, the others are
76 Bit width of the output
80 i : Signal(max=width), in
82 o : Signal(width), out
85 Invalid, no output bits are to be asserted
88 def __init__(self
, width
):
89 self
.i
= Signal(max=max(2, width
)) # binary
90 self
.n
= Signal() # none/invalid
91 self
.o
= Signal(width
) # one-hot
92 act
= dict((j
, self
.o
.eq(1<<j
)) for j
in range(width
))
93 self
.comb
+= Case(self
.i
, act
)
94 self
.comb
+= If(self
.n
, self
.o
.eq(0))
97 class PriorityDecoder(Decoder
):