-
Notifications
You must be signed in to change notification settings - Fork 210
/
coding.py
98 lines (79 loc) · 2.51 KB
/
coding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
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<<j, self.o.eq(j)) for j in range(width))
act["default"] = self.n.eq(1)
self.comb += Case(self.i, act)
class PriorityEncoder(Module):
"""Priority encode requests to binary
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.
Parameters
----------
width : int
Bit width of the input
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):
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
self.comb += If(self.i[j], self.o.eq(j))
self.comb += self.n.eq(self.i == 0)
class Decoder(Module):
"""Decode binary to one-hot
If `n` is low, the `i` th bit in `o` is asserted, the others are
not, else `o == 0`.
Parameters
----------
width : int
Bit width of the output
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
"""
def __init__(self, width):
self.i = Signal(max=max(2, width)) # binary
self.n = Signal() # none/invalid
self.o = Signal(width) # one-hot
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))
class PriorityDecoder(Decoder):
pass # same