1 """ Load / Store partial address matcher
4 * http://bugs.libre-riscv.org/show_bug.cgi?id=216
6 Loads and Stores do not need a full match (CAM), they need "good enough"
7 avoidance. Around 11 bits on a 64-bit address is "good enough".
9 The simplest way to use this module is to ignore not only the top bits,
10 but also the bottom bits as well: in this case (this RV64 processor),
11 enough to cover a DWORD (64-bit). that means ignore the bottom 4 bits,
12 due to the possibility of 64-bit LD/ST being misaligned.
14 To reiterate: the use of this module is an *optimisation*. All it has
15 to do is cover the cases that are *definitely* matches (by checking 11
16 bits or so), and if a few opportunities for parallel LD/STs are missed
17 because the top (or bottom) bits weren't checked, so what: all that
18 happens is: the mis-matched addresses are LD/STd on single-cycles. Big Deal.
20 However, if we wanted to enhance this algorithm (without using a CAM and
21 without using expensive comparators) probably the best way to do so would
22 be to turn the last 16 bits into a byte-level bitmap. LD/ST on a byte
23 would have 1 of the 16 bits set. LD/ST on a DWORD would have 8 of the 16
24 bits set (offset if the LD/ST was misaligned). TODO.
28 > I have used bits <11:6> as they are not translated (4KB pages)
29 > and larger than a cache line (64 bytes).
30 > I have used bits <11:4> when the L1 cache was QuadW sized and
31 > the L2 cache was Line sized.
34 from nmigen
.compat
.sim
import run_simulation
35 from nmigen
.cli
import verilog
, rtlil
36 from nmigen
import Module
, Signal
, Const
, Array
, Cat
, Elaboratable
37 from nmigen
.lib
.coding
import Decoder
39 from nmutil
.latch
import latchregister
, SRLatch
42 class PartialAddrMatch(Elaboratable
):
43 """A partial address matcher
45 def __init__(self
, n_adr
, bitwid
):
49 self
.addrs_i
= Array(Signal(bitwid
, name
="addr") for i
in range(n_adr
))
50 #self.addr_we_i = Signal(n_adr, reset_less=True) # write-enable
51 self
.addr_en_i
= Signal(n_adr
, reset_less
=True) # address latched in
52 self
.addr_rs_i
= Signal(n_adr
, reset_less
=True) # address deactivated
54 # output: a nomatch for each address plus individual nomatch signals
55 self
.addr_nomatch_o
= Signal(n_adr
, name
="nomatch_o", reset_less
=True)
56 self
.addr_nomatch_a_o
= Array(Signal(n_adr
, reset_less
=True,
57 name
="nomatch_array_o") \
58 for i
in range(n_adr
))
60 def elaborate(self
, platform
):
62 return self
._elaborate
(m
, platform
)
64 def _elaborate(self
, m
, platform
):
68 # array of address-latches
69 m
.submodules
.l
= self
.l
= l
= SRLatch(llen
=self
.n_adr
, sync
=False)
70 self
.addrs_r
= addrs_r
= Array(Signal(self
.bitwid
, reset_less
=True,
72 for i
in range(self
.n_adr
))
75 comb
+= l
.s
.eq(self
.addr_en_i
)
76 comb
+= l
.r
.eq(self
.addr_rs_i
)
78 # copy in addresses (and "enable" signals)
79 for i
in range(self
.n_adr
):
80 latchregister(m
, self
.addrs_i
[i
], addrs_r
[i
], l
.q
[i
])
82 # is there a clash, yes/no
84 for i
in range(self
.n_adr
):
86 for j
in range(self
.n_adr
):
87 match
.append(self
.is_match(i
, j
))
88 comb
+= self
.addr_nomatch_a_o
[i
].eq(~
Cat(*match
) & l
.q
)
89 matchgrp
.append(self
.addr_nomatch_a_o
[i
] == l
.q
)
90 comb
+= self
.addr_nomatch_o
.eq(Cat(*matchgrp
) & l
.q
)
94 def is_match(self
, i
, j
):
96 return Const(0) # don't match against self!
97 return self
.addrs_r
[i
] == self
.addrs_r
[j
]
100 yield from self
.addrs_i
101 #yield self.addr_we_i
103 yield from self
.addr_nomatch_a_o
104 yield self
.addr_nomatch_o
110 class LenExpand(Elaboratable
):
111 """LenExpand: expands binary length (and LSBs of an address) into unary
113 this basically produces a bitmap of which *bytes* are to be read (written)
116 (bit_len=4) len=4, addr=0b0011 => 0b1111 << addr
118 (bit_len=4) len=8, addr=0b0101 => 0b11111111 << addr
122 def __init__(self
, bit_len
):
123 self
.bit_len
= bit_len
124 self
.len_i
= Signal(bit_len
, reset_less
=True)
125 self
.addr_i
= Signal(bit_len
, reset_less
=True)
126 self
.explen_o
= Signal(1<<(bit_len
+1), reset_less
=True)
128 def elaborate(self
, platform
):
133 binlen
= Signal((1<<self
.bit_len
)+1, reset_less
=True)
134 comb
+= binlen
.eq((Const(1, self
.bit_len
+1) << (1+self
.len_i
)) - 1)
135 comb
+= self
.explen_o
.eq(binlen
<< self
.addr_i
)
140 return [self
.len_i
, self
.addr_i
, self
.explen_o
,]
143 class PartialAddrBitmap(PartialAddrMatch
):
146 makes two comparisons for each address, with each (addr,len)
147 being extended to an unary byte-map.
149 two comparisons are needed because when an address is misaligned,
150 the byte-map is split into two halves. example:
152 address = 0b1011011, len=8 => 0b101 and shift of 11 (0b1011)
153 len in unary is 0b0000 0000 1111 1111
154 when shifted becomes TWO addresses:
156 * 0b101 and a byte-map of 0b1111 1000 0000 0000 (len-mask shifted by 11)
157 * 0b101+1 and a byte-map of 0b0000 0000 0000 0111 (overlaps onto next 16)
159 therefore, because this now covers two addresses, we need *two*
160 comparisons per address *not* one.
162 def __init__(self
, n_adr
, lsbwid
, bitlen
):
163 self
.lsbwid
= lsbwid
# number of bits to turn into unary
164 self
.midlen
= bitlen
-lsbwid
165 PartialAddrMatch
.__init
__(self
, n_adr
, self
.midlen
)
167 # input: length of the LOAD/STORE
168 self
.len_i
= Array(Signal(lsbwid
, reset_less
=True,
169 name
="len") for i
in range(n_adr
))
170 # input: full address
171 self
.faddrs_i
= Array(Signal(bitlen
, reset_less
=True,
172 name
="fadr") for i
in range(n_adr
))
174 # intermediary: address + 1
175 self
.addr1s
= Array(Signal(self
.midlen
, reset_less
=True,
177 for i
in range(n_adr
))
179 # expanded lengths, needed in match
180 expwid
= 1+self
.lsbwid
# XXX assume LD/ST no greater than 8
181 self
.explen
= Array(Signal(1<<expwid
, reset_less
=True,
183 for i
in range(self
.n_adr
))
185 def elaborate(self
, platform
):
186 m
= PartialAddrMatch
.elaborate(self
, platform
)
190 addrs_r
, l
= self
.addrs_r
, self
.l
191 len_r
= Array(Signal(self
.lsbwid
, reset_less
=True,
193 for i
in range(self
.n_adr
))
195 for i
in range(self
.n_adr
):
196 # create a bit-expander for each address
197 be
= LenExpand(self
.lsbwid
)
198 setattr(m
.submodules
, "le%d" % i
, be
)
199 # copy the top lsbwid..(lsbwid-bit_len) of addresses to compare
200 comb
+= self
.addrs_i
[i
].eq(self
.faddrs_i
[i
][self
.lsbwid
:])
202 # copy in lengths and latch them
203 latchregister(m
, self
.len_i
[i
], len_r
[i
], l
.q
[i
])
205 # add one to intermediate addresses
206 comb
+= self
.addr1s
[i
].eq(self
.addrs_r
[i
]+1)
208 # put the bottom bits of each address into each LenExpander.
209 comb
+= be
.len_i
.eq(len_r
[i
])
210 comb
+= be
.addr_i
.eq(self
.faddrs_i
[i
][:self
.lsbwid
])
211 # connect expander output
212 comb
+= self
.explen
[i
].eq(be
.explen_o
)
216 def is_match(self
, i
, j
):
218 return Const(0) # don't match against self!
219 expwid
= 1<<self
.lsbwid
221 expwid2
= expwid
+ hexp
222 print (self
.lsbwid
, expwid
)
223 return ((self
.addrs_r
[i
] == self
.addrs_r
[j
]) & \
224 (self
.explen
[i
][:expwid
] & self
.explen
[j
][:expwid
]).bool() |
225 (self
.addr1s
[i
] == self
.addrs_r
[j
]) & \
226 (self
.explen
[i
][expwid
:expwid2
] & self
.explen
[j
][:hexp
]).bool())
229 yield from self
.faddrs_i
230 yield from self
.len_i
231 #yield self.addr_we_i
233 yield from self
.addr_nomatch_a_o
234 yield self
.addr_nomatch_o
239 def part_addr_sim(dut
):
240 yield dut
.dest_i
.eq(1)
241 yield dut
.issue_i
.eq(1)
243 yield dut
.issue_i
.eq(0)
245 yield dut
.src1_i
.eq(1)
246 yield dut
.issue_i
.eq(1)
248 yield dut
.issue_i
.eq(0)
250 yield dut
.go_rd_i
.eq(1)
252 yield dut
.go_rd_i
.eq(0)
254 yield dut
.go_wr_i
.eq(1)
256 yield dut
.go_wr_i
.eq(0)
259 def test_part_addr():
261 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
262 with
open("test_len_expand.il", "w") as f
:
265 dut
= PartialAddrBitmap(3, 4, 10)
266 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
267 with
open("test_part_bit.il", "w") as f
:
270 dut
= PartialAddrMatch(3, 10)
271 vl
= rtlil
.convert(dut
, ports
=dut
.ports())
272 with
open("test_part_addr.il", "w") as f
:
275 run_simulation(dut
, part_addr_sim(dut
), vcd_name
='test_part_addr.vcd')
277 if __name__
== '__main__':