1 from nmigen
import Module
, Signal
, Elaboratable
2 from nmigen
.lib
.coding
import Encoder
, PriorityEncoder
4 class AddressEncoder(Elaboratable
):
7 The purpose of this module is to take in a vector and
8 encode the bits that are one hot into an address. This module
9 combines both nmigen's Encoder and PriorityEncoder and will state
10 whether the input line has a single bit hot, multiple bits hot,
11 or no bits hot. The output line will always have the lowest value
15 The output is valid when either single or multiple match is high.
16 Otherwise output is 0.
18 def __init__(self
, width
):
20 * width: The desired length of the input vector
23 self
.encoder
= Encoder(width
)
24 self
.p_encoder
= PriorityEncoder(width
)
27 self
.i
= Signal(width
)
30 self
.single_match
= Signal(1)
31 self
.multiple_match
= Signal(1)
32 self
.o
= Signal(max=width
)
34 def elaborate(self
, platform
=None):
37 # Add internal submodules
38 m
.submodules
.encoder
= self
.encoder
39 m
.submodules
.p_encoder
= self
.p_encoder
42 self
.encoder
.i
.eq(self
.i
),
43 self
.p_encoder
.i
.eq(self
.i
)
47 # 1. check if the input vector is non-zero
48 # 2. if non-zero, check if single match or multiple match
49 # 3. set output line to be lowest value address output
51 # If the priority encoder recieves an input of 0
52 # If n is 1 then the output is not valid
53 with m
.If(self
.p_encoder
.n
):
55 self
.single_match
.eq(0),
56 self
.multiple_match
.eq(0),
59 # If the priority encoder recieves an input > 0
61 # Multiple Match if encoder n is invalid
62 with m
.If(self
.encoder
.n
):
64 self
.single_match
.eq(0),
65 self
.multiple_match
.eq(1)
67 # Single Match if encoder n is valid
70 self
.single_match
.eq(1),
71 self
.multiple_match
.eq(0)
73 # Always set output based on priority encoder output
74 m
.d
.comb
+= self
.o
.eq(self
.p_encoder
.o
)