Correct incorrect output bit size
[soc.git] / TLB / src / AddressEncoder.py
1 from nmigen import Module, Signal
2 from nmigen.lib.coding import Encoder, PriorityEncoder
3
4 class AddressEncoder():
5 def __init__(self, width):
6 # Internal
7 self.encoder = Encoder(width)
8 self.p_encoder = PriorityEncoder(width)
9
10 # Input
11 self.i = Signal(width)
12
13 # Output
14 self.single_match = Signal(1)
15 self.multiple_match = Signal(1)
16 self.o = Signal(max=width)
17
18 def elaborate(self, platform=None):
19 m = Module()
20
21 # Add internal submodules
22 m.submodules.encoder = self.encoder
23 m.submodules.p_encoder = self.p_encoder
24
25 m.d.comb += [
26 self.encoder.i.eq(self.i),
27 self.p_encoder.i.eq(self.i)
28 ]
29
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):
33 m.d.comb += [
34 self.single_match.eq(0),
35 self.multiple_match.eq(0),
36 self.o.eq(0)
37 ]
38 # If the priority encoder recieves an input > 0
39 with m.Else():
40 # Multiple Match if encoder n is invalid
41 with m.If(self.encoder.n):
42 m.d.comb += [
43 self.single_match.eq(0),
44 self.multiple_match.eq(1)
45 ]
46 # Single Match if encoder n is valid
47 with m.Else():
48 m.d.comb += [
49 self.single_match.eq(1),
50 self.multiple_match.eq(0)
51 ]
52 # Always set output based on priority encoder output
53 m.d.comb += self.o.eq(self.p_encoder.o)
54 return m