add addrgen comment
[soc.git] / src / scoreboard / addr_match.py
1 """ Load / Store partial address matcher
2
3 Loads and Stores do not need a full match (CAM), they need "good enough"
4 avoidance. Around 11 bits on a 64-bit address is "good enough".
5
6 The simplest way to use this module is to ignore not only the top bits,
7 but also the bottom bits as well: in this case (this RV64 processor),
8 enough to cover a DWORD (64-bit). that means ignore the bottom 4 bits,
9 due to the possibility of 64-bit LD/ST being misaligned.
10
11 To reiterate: the use of this module is an *optimisation*. All it has
12 to do is cover the cases that are *definitely* matches (by checking 11
13 bits or so), and if a few opportunities for parallel LD/STs are missed
14 because the top (or bottom) bits weren't checked, so what: all that
15 happens is: the mis-matched addresses are LD/STd on single-cycles. Big Deal.
16
17 However, if we wanted to enhance this algorithm (without using a CAM and
18 without using expensive comparators) probably the best way to do so would
19 be to turn the last 16 bits into a byte-level bitmap. LD/ST on a byte
20 would have 1 of the 16 bits set. LD/ST on a DWORD would have 8 of the 16
21 bits set (offset if the LD/ST was misaligned). TODO.
22
23 Notes:
24
25 > I have used bits <11:6> as they are not translated (4KB pages)
26 > and larger than a cache line (64 bytes).
27 > I have used bits <11:4> when the L1 cache was QuadW sized and
28 > the L2 cache was Line sized.
29 """
30
31 from nmigen.compat.sim import run_simulation
32 from nmigen.cli import verilog, rtlil
33 from nmigen import Module, Signal, Const, Array, Cat, Elaboratable
34
35
36 class PartialAddrMatch(Elaboratable):
37 """A partial address matcher
38 """
39 def __init__(self, n_adr, bitwid):
40 self.n_adr = n_adr
41 self.bitwid = bitwid
42 # inputs
43 self.addrs_i = Array(Signal(bitwid, name="addr") for i in range(n_adr))
44 self.addr_we_i = Signal(n_adr) # write-enable for incoming address
45 self.addr_en_i = Signal(n_adr) # address activated (0 == ignore)
46
47 # output
48 self.addr_match_o = Signal(n_adr)
49
50 def elaborate(self, platform):
51 m = Module()
52 comb = m.d.comb
53 sync = m.d.sync
54
55 addrs_r = Array(Signal(self.bitwid, "a_r") for i in range(self.n_adr))
56 addr_en_r = Signal(self.n_adr)
57
58 # copy in addresses (and "enable" signals)
59 for i in range(self.n_adr):
60 with m.If(self.addr_we_i[i]):
61 sync += addrs_r[i].eq(self.addrs_i[i])
62 sync += addr_en_r[i].eq(self.addr_en_i[i])
63
64 # is there a clash, yes/no
65 for i in range(self.n_adr):
66 match = []
67 for j in range(self.n_adr):
68 if i == j:
69 match.append(Const(0)) # don't match against self!
70 else:
71 match.append(addrs_r[i] == addrs_r[j])
72 comb += self.addr_match_o.eq(Cat(*match).bool() & addr_en_r)
73
74 return m
75
76 def __iter__(self):
77 yield from self.addrs_i
78 yield self.addr_we_i
79 yield self.addr_en_i
80 yield self.addr_match_o
81
82 def ports(self):
83 return list(self)
84
85
86 def part_addr_sim(dut):
87 yield dut.dest_i.eq(1)
88 yield dut.issue_i.eq(1)
89 yield
90 yield dut.issue_i.eq(0)
91 yield
92 yield dut.src1_i.eq(1)
93 yield dut.issue_i.eq(1)
94 yield
95 yield dut.issue_i.eq(0)
96 yield
97 yield dut.go_rd_i.eq(1)
98 yield
99 yield dut.go_rd_i.eq(0)
100 yield
101 yield dut.go_wr_i.eq(1)
102 yield
103 yield dut.go_wr_i.eq(0)
104 yield
105
106 def test_part_addr():
107 dut = PartialAddrMatch(3, 10)
108 vl = rtlil.convert(dut, ports=dut.ports())
109 with open("test_part_addr.il", "w") as f:
110 f.write(vl)
111
112 run_simulation(dut, part_addr_sim(dut), vcd_name='test_part_addr.vcd')
113
114 if __name__ == '__main__':
115 test_part_addr()