1 from nmigen
import Module
, Signal
2 from nmigen
.lib
.coding
import Encoder
, PriorityEncoder
4 class AddressEncoder():
5 def __init__(self
, width
):
7 self
.encoder
= Encoder(width
)
8 self
.p_encoder
= PriorityEncoder(width
)
11 self
.i
= Signal(width
)
14 self
.single_match
= Signal(1)
15 self
.multiple_match
= Signal(1)
16 self
.o
= Signal(max=width
)
18 def elaborate(self
, platform
=None):
21 # Add internal submodules
22 m
.submodules
.encoder
= self
.encoder
23 m
.submodules
.p_encoder
= self
.p_encoder
26 self
.encoder
.i
.eq(self
.i
),
27 self
.p_encoder
.i
.eq(self
.i
)
30 # If the priority encoder recieves an input of 0
31 # If n is 1 then the output is not valid
32 with m
.If(self
.p_encoder
.n
):
34 self
.single_match
.eq(0),
35 self
.multiple_match
.eq(0),
38 # If the priority encoder recieves an input > 0
40 # Multiple Match if encoder n is invalid
41 with m
.If(self
.encoder
.n
):
43 self
.single_match
.eq(0),
44 self
.multiple_match
.eq(1)
46 # Single Match if encoder n is valid
49 self
.single_match
.eq(1),
50 self
.multiple_match
.eq(0)
52 # Always set output based on priority encoder output
53 m
.d
.comb
+= self
.o
.eq(self
.p_encoder
.o
)