expand regwid to 64 in l0_cache test
[soc.git] / src / soc / experiment / l0_cache.py
1 """L0 Cache/Buffer
2
3 This first version is intended for prototyping and test purposes:
4 it has "direct" access to Memory.
5
6 The intention is that this version remains an integral part of the
7 test infrastructure, and, just as with minerva's memory arrangement,
8 a dynamic runtime config *selects* alternative memory arrangements
9 rather than *replaces and discards* this code.
10
11 Links:
12
13 * https://bugs.libre-soc.org/show_bug.cgi?id=216
14 * https://libre-soc.org/3d_gpu/architecture/memory_and_cache/
15
16 """
17
18 from nmigen.compat.sim import run_simulation, Settle
19 from nmigen.cli import verilog, rtlil
20 from nmigen import Module, Signal, Mux, Elaboratable, Array, Cat
21 from nmutil.iocontrol import RecordObject
22 from nmigen.utils import log2_int
23 from nmigen.hdl.rec import Record, Layout
24
25 from nmutil.latch import SRLatch, latchregister
26 from soc.decoder.power_decoder2 import Data
27 from soc.decoder.power_enums import InternalOp
28 from soc.regfile.regfile import ortreereduce
29 from nmutil.util import treereduce
30
31 from soc.experiment.compldst import CompLDSTOpSubset
32 from soc.decoder.power_decoder2 import Data
33 #from nmutil.picker import PriorityPicker
34 from nmigen.lib.coding import PriorityEncoder
35
36 # for testing purposes
37 from soc.experiment.testmem import TestMemory
38
39
40 class PortInterface(RecordObject):
41 """PortInterface
42
43 defines the interface - the API - that the LDSTCompUnit connects
44 to. note that this is NOT a "fire-and-forget" interface. the
45 LDSTCompUnit *must* be kept appraised that the request is in
46 progress, and only when it has a 100% successful completion rate
47 can the notification be given (busy dropped).
48
49 The interface FSM rules are as follows:
50
51 * if busy_o is asserted, a LD/ST is in progress. further
52 requests may not be made until busy_o is deasserted.
53
54 * only one of is_ld_i or is_st_i may be asserted. busy_o
55 will immediately be asserted and remain asserted.
56
57 * addr.ok is to be asserted when the LD/ST address is known.
58 addr.data is to be valid on the same cycle.
59
60 addr.ok and addr.data must REMAIN asserted until busy_o
61 is de-asserted. this ensures that there is no need
62 for the L0 Cache/Buffer to have an additional address latch
63 (because the LDSTCompUnit already has it)
64
65 * addr_ok_o (or addr_exc_o) must be waited for. these will
66 be asserted *only* for one cycle and one cycle only.
67
68 * addr_exc_o will be asserted if there is no chance that the
69 memory request may be fulfilled.
70
71 busy_o is deasserted on the same cycle as addr_exc_o is asserted.
72
73 * conversely: addr_ok_o must *ONLY* be asserted if there is a
74 HUNDRED PERCENT guarantee that the memory request will be
75 fulfilled.
76
77 * for a LD, ld.ok will be asserted - for only one clock cycle -
78 at any point in the future that is acceptable to the underlying
79 Memory subsystem. the recipient MUST latch ld.data on that cycle.
80
81 busy_o is deasserted on the same cycle as ld.ok is asserted.
82
83 * for a ST, st.ok may be asserted only after addr_ok_o had been
84 asserted, alongside valid st.data at the same time. st.ok
85 must only be asserted for one cycle.
86
87 the underlying Memory is REQUIRED to pick up that data and
88 guarantee its delivery. no back-acknowledgement is required.
89
90 busy_o is deasserted on the cycle AFTER st.ok is asserted.
91 """
92
93 def __init__(self, name=None, regwid=64, addrwid=48):
94
95 self._regwid = regwid
96 self._addrwid = addrwid
97
98 RecordObject.__init__(self, name=name)
99
100 # distinguish op type (ld/st)
101 self.is_ld_i = Signal(reset_less=True)
102 self.is_st_i = Signal(reset_less=True)
103 self.op = CompLDSTOpSubset() # hm insn_type ld/st duplicates here
104
105 # common signals
106 self.busy_o = Signal(reset_less=True) # do not use if busy
107 self.go_die_i = Signal(reset_less=True) # back to reset
108 self.addr = Data(addrwid, "addr_i") # addr/addr-ok
109 # addr is valid (TLB, L1 etc.)
110 self.addr_ok_o = Signal(reset_less=True)
111 self.addr_exc_o = Signal(reset_less=True) # TODO, "type" of exception
112
113 # LD/ST
114 self.ld = Data(regwid, "ld_data_o") # ok to be set by L0 Cache/Buf
115 self.st = Data(regwid, "st_data_i") # ok to be set by CompUnit
116
117 # TODO: elaborate function
118
119
120 class DualPortSplitter(Elaboratable):
121 """DualPortSplitter
122
123 * one incoming PortInterface
124 * two *OUTGOING* PortInterfaces
125 * uses LDSTSplitter to do it
126
127 (actually, thinking about it LDSTSplitter could simply be
128 modified to conform to PortInterface: one in, two out)
129
130 once that is done each pair of ports may be wired directly
131 to the dual ports of L0CacheBuffer
132
133 The split is carried out so that, regardless of alignment or
134 mis-alignment, outgoing PortInterface[0] takes bit 4 == 0
135 of the address, whilst outgoing PortInterface[1] takes
136 bit 4 == 1.
137
138 PortInterface *may* need to be changed so that the length is
139 a binary number (accepting values 1-16).
140 """
141 def __init__(self):
142 self.outp = []
143 self.outp[0] = PortInterface(name="outp_0")
144 self.outp[1] = PortInterface(name="outp_1")
145 self.inp = PortInterface(name="inp")
146
147 def elaborate(self, platform):
148 splitter = LDSTSplitter(64, 48, 4)
149
150
151 class DataMergerRecord(Record):
152 """
153 {data: 128 bit, byte_enable: 16 bit}
154 """
155
156 def __init__(self, name=None):
157 layout = (('data', 128),
158 ('en', 16)
159 )
160
161 Record.__init__(self, Layout(layout), name=name)
162
163 #FIXME: make resetless
164
165 # TODO: formal verification
166
167 class DataMerger(Elaboratable):
168 """DataMerger
169
170 Merges data based on an address-match matrix.
171 Identifies (picks) one (any) row, then uses that row,
172 based on matching address bits, to merge (OR) all data
173 rows into the output.
174
175 Basically, by the time DataMerger is used, all of its incoming data is
176 determined not to conflict. The last step before actually submitting
177 the request to the Memory Subsystem is to work out which requests,
178 on the same 128-bit cache line, can be "merged" due to them being:
179 (A) on the same address (bits 4 and above) (B) having byte-enable
180 lines that (as previously mentioned) do not conflict.
181
182 Therefore, put simply, this module will:
183 (1) pick a row (any row) and identify it by an index labelled "idx"
184 (2) merge all byte-enable lines which are on that same address, as
185 indicated by addr_match_i[idx], onto the output
186 """
187
188 def __init__(self, array_size):
189 """
190 :addr_array_i: an NxN Array of Signals with bits set indicating address
191 match. bits across the diagonal (addr_array_i[x][x])
192 will always be set, to indicate "active".
193 :data_i: an Nx Array of Records {data: 128 bit, byte_enable: 16 bit}
194 :data_o: an Output Record of same type
195 {data: 128 bit, byte_enable: 16 bit}
196 """
197 self.array_size = array_size
198 ul = []
199 for i in range(array_size):
200 ul.append(Signal(array_size,
201 reset_less=True,
202 name="addr_match_%d" % i))
203 self.addr_array_i = Array(ul)
204
205 ul = []
206 for i in range(array_size):
207 ul.append(DataMergerRecord())
208 self.data_i = Array(ul)
209 self.data_o = DataMergerRecord()
210
211 def elaborate(self, platform):
212 m = Module()
213 comb = m.d.comb
214 #(1) pick a row
215 m.submodules.pick = pick = PriorityEncoder(self.array_size)
216 for j in range(self.array_size):
217 comb += pick.i[j].eq(self.addr_array_i[j].bool())
218 valid = ~pick.n
219 idx = pick.o
220 #(2) merge
221 with m.If(valid):
222 l = []
223 for j in range(self.array_size):
224 select = self.addr_array_i[idx][j]
225 r = DataMergerRecord()
226 with m.If(select):
227 comb += r.eq(self.data_i[j])
228 l.append(r)
229 comb += self.data_o.data.eq(ortreereduce(l,"data"))
230 comb += self.data_o.en.eq(ortreereduce(l,"en"))
231
232 return m
233
234
235 class LDSTPort(Elaboratable):
236 def __init__(self, idx, regwid=64, addrwid=48):
237 self.pi = PortInterface("ldst_port%d" % idx, regwid, addrwid)
238
239 def elaborate(self, platform):
240 m = Module()
241 comb, sync = m.d.comb, m.d.sync
242
243 # latches
244 m.submodules.busy_l = busy_l = SRLatch(False, name="busy")
245 m.submodules.cyc_l = cyc_l = SRLatch(True, name="cyc")
246 comb += cyc_l.s.eq(0)
247 comb += cyc_l.r.eq(0)
248
249 # this is a little weird: we let the L0Cache/Buffer set
250 # the outputs: this module just monitors "state".
251
252 # LD/ST requested activates "busy"
253 with m.If(self.pi.is_ld_i | self.pi.is_st_i):
254 comb += busy_l.s.eq(1)
255
256 # monitor for an exception or the completion of LD.
257 with m.If(self.pi.addr_exc_o):
258 comb += busy_l.r.eq(1)
259
260 # however ST needs one cycle before busy is reset
261 with m.If(self.pi.st.ok | self.pi.ld.ok):
262 comb += cyc_l.s.eq(1)
263
264 with m.If(cyc_l.q):
265 comb += cyc_l.r.eq(1)
266 comb += busy_l.r.eq(1)
267
268 # busy latch outputs to interface
269 comb += self.pi.busy_o.eq(busy_l.q)
270
271 return m
272
273 def __iter__(self):
274 yield self.pi.is_ld_i
275 yield self.pi.is_st_i
276 yield from self.pi.op.ports()
277 yield self.pi.busy_o
278 yield self.pi.go_die_i
279 yield from self.pi.addr.ports()
280 yield self.pi.addr_ok_o
281 yield self.pi.addr_exc_o
282
283 yield from self.pi.ld.ports()
284 yield from self.pi.st.ports()
285
286 def ports(self):
287 return list(self)
288
289
290 class L0CacheBuffer(Elaboratable):
291 """L0 Cache / Buffer
292
293 Note that the final version will have *two* interfaces per LDSTCompUnit,
294 to cover mis-aligned requests, as well as *two* 128-bit L1 Cache
295 interfaces: one for odd (addr[4] == 1) and one for even (addr[4] == 1).
296
297 This version is to be used for test purposes (and actively maintained
298 for such, rather than "replaced")
299
300 There are much better ways to implement this. However it's only
301 a "demo" / "test" class, and one important aspect: it responds
302 combinatorially, where a nmigen FSM's state-changes only activate
303 on clock-sync boundaries.
304 """
305
306 def __init__(self, n_units, mem, regwid=64, addrwid=48):
307 self.n_units = n_units
308 self.mem = mem
309 ul = []
310 for i in range(n_units):
311 ul.append(LDSTPort(i, regwid, addrwid))
312 self.dports = Array(ul)
313
314 def elaborate(self, platform):
315 m = Module()
316 comb, sync = m.d.comb, m.d.sync
317
318 # connect the ports as modules
319 for i in range(self.n_units):
320 setattr(m.submodules, "port%d" % i, self.dports[i])
321
322 # state-machine latches
323 m.submodules.st_active = st_active = SRLatch(False, name="st_active")
324 m.submodules.ld_active = ld_active = SRLatch(False, name="ld_active")
325 m.submodules.reset_l = reset_l = SRLatch(True, name="reset")
326 m.submodules.idx_l = idx_l = SRLatch(False, name="idx_l")
327 m.submodules.adrok_l = adrok_l = SRLatch(False, name="addr_acked")
328
329 # find one LD (or ST) and do it. only one per cycle.
330 # TODO: in the "live" (production) L0Cache/Buffer, merge multiple
331 # LD/STs using mask-expansion - see LenExpand class
332
333 m.submodules.ldpick = ldpick = PriorityEncoder(self.n_units)
334 m.submodules.stpick = stpick = PriorityEncoder(self.n_units)
335
336 lds = Signal(self.n_units, reset_less=True)
337 sts = Signal(self.n_units, reset_less=True)
338 ldi = []
339 sti = []
340 for i in range(self.n_units):
341 pi = self.dports[i].pi
342 ldi.append(pi.is_ld_i & pi.busy_o) # accumulate ld-req signals
343 sti.append(pi.is_st_i & pi.busy_o) # accumulate st-req signals
344 # put the requests into the priority-pickers
345 comb += ldpick.i.eq(Cat(*ldi))
346 comb += stpick.i.eq(Cat(*sti))
347
348 # hmm, have to select (record) the right port index
349 nbits = log2_int(self.n_units, False)
350 ld_idx = Signal(nbits, reset_less=False)
351 st_idx = Signal(nbits, reset_less=False)
352 # use these because of the sync-and-comb pass-through capability
353 latchregister(m, ldpick.o, ld_idx, idx_l.qn, name="ld_idx_l")
354 latchregister(m, stpick.o, st_idx, idx_l.qn, name="st_idx_l")
355
356 # convenience variables to reference the "picked" port
357 ldport = self.dports[ld_idx].pi
358 stport = self.dports[st_idx].pi
359 # and the memory ports
360 rdport = self.mem.rdport
361 wrport = self.mem.wrport
362
363 # Priority-Pickers pick one and only one request, capture its index.
364 # from that point on this code *only* "listens" to that port.
365
366 sync += adrok_l.s.eq(0)
367 comb += adrok_l.r.eq(0)
368 with m.If(~ldpick.n):
369 comb += ld_active.s.eq(1) # activate LD mode
370 comb += idx_l.r.eq(1) # pick (and capture) the port index
371 with m.Elif(~stpick.n):
372 comb += st_active.s.eq(1) # activate ST mode
373 comb += idx_l.r.eq(1) # pick (and capture) the port index
374
375 # from this point onwards, with the port "picked", it stays picked
376 # until ld_active (or st_active) are de-asserted.
377
378 # if now in "LD" mode: wait for addr_ok, then send the address out
379 # to memory, acknowledge address, and send out LD data
380 with m.If(ld_active.q):
381 with m.If(ldport.addr.ok & adrok_l.qn):
382 comb += rdport.addr.eq(ldport.addr.data) # addr ok, send thru
383 comb += ldport.addr_ok_o.eq(1) # acknowledge addr ok
384 sync += adrok_l.s.eq(1) # and pull "ack" latch
385
386 # if now in "ST" mode: likewise do the same but with "ST"
387 # to memory, acknowledge address, and send out LD data
388 with m.If(st_active.q):
389 with m.If(stport.addr.ok):
390 comb += wrport.addr.eq(stport.addr.data) # addr ok, send thru
391 with m.If(adrok_l.qn):
392 comb += stport.addr_ok_o.eq(1) # acknowledge addr ok
393 sync += adrok_l.s.eq(1) # and pull "ack" latch
394
395 # NOTE: in both these, below, the port itself takes care
396 # of de-asserting its "busy_o" signal, based on either ld.ok going
397 # high (by us, here) or by st.ok going high (by the LDSTCompUnit).
398
399 # for LD mode, when addr has been "ok'd", assume that (because this
400 # is a "Memory" test-class) the memory read data is valid.
401 comb += reset_l.s.eq(0)
402 comb += reset_l.r.eq(0)
403 with m.If(ld_active.q & adrok_l.q):
404 comb += ldport.ld.data.eq(rdport.data) # put data out
405 comb += ldport.ld.ok.eq(1) # indicate data valid
406 comb += reset_l.s.eq(1) # reset mode after 1 cycle
407
408 # for ST mode, when addr has been "ok'd", wait for incoming "ST ok"
409 with m.If(st_active.q & stport.st.ok):
410 comb += wrport.data.eq(stport.st.data) # write st to mem
411 comb += wrport.en.eq(1) # enable write
412 comb += reset_l.s.eq(1) # reset mode after 1 cycle
413
414 # after waiting one cycle (reset_l is "sync" mode), reset the port
415 with m.If(reset_l.q):
416 comb += idx_l.s.eq(1) # deactivate port-index selector
417 comb += ld_active.r.eq(1) # leave the ST active for 1 cycle
418 comb += st_active.r.eq(1) # leave the ST active for 1 cycle
419 comb += reset_l.r.eq(1) # clear reset
420 comb += adrok_l.r.eq(1) # address reset
421
422 return m
423
424 def ports(self):
425 for p in self.dports:
426 yield from p.ports()
427
428
429 class TstL0CacheBuffer(Elaboratable):
430 def __init__(self, n_units=3, regwid=16, addrwid=4):
431 self.mem = TestMemory(regwid, addrwid)
432 self.l0 = L0CacheBuffer(n_units, self.mem, regwid, addrwid)
433
434 def elaborate(self, platform):
435 m = Module()
436 m.submodules.mem = self.mem
437 m.submodules.l0 = self.l0
438
439 return m
440
441 def ports(self):
442 yield from self.l0.ports()
443 yield self.mem.rdport.addr
444 yield self.mem.rdport.data
445 yield self.mem.wrport.addr
446 yield self.mem.wrport.data
447 # TODO: mem ports
448
449
450 def wait_busy(port, no=False):
451 while True:
452 busy = yield port.pi.busy_o
453 print("busy", no, busy)
454 if bool(busy) == no:
455 break
456 yield
457
458
459 def wait_addr(port):
460 while True:
461 addr_ok = yield port.pi.addr_ok_o
462 print("addrok", addr_ok)
463 if not addr_ok:
464 break
465 yield
466
467
468 def wait_ldok(port):
469 while True:
470 ldok = yield port.pi.ld.ok
471 print("ldok", ldok)
472 if ldok:
473 break
474 yield
475
476
477 def l0_cache_st(dut, addr, data):
478 l0 = dut.l0
479 mem = dut.mem
480 port0 = l0.dports[0]
481 port1 = l0.dports[1]
482
483 # have to wait until not busy
484 yield from wait_busy(port1, no=False) # wait until not busy
485
486 # set up a ST on the port. address first:
487 yield port1.pi.is_st_i.eq(1) # indicate LD
488
489 yield port1.pi.addr.data.eq(addr) # set address
490 yield port1.pi.addr.ok.eq(1) # set ok
491 yield from wait_addr(port1) # wait until addr ok
492 # yield # not needed, just for checking
493 # yield # not needed, just for checking
494 # assert "ST" for one cycle (required by the API)
495 yield port1.pi.st.data.eq(data)
496 yield port1.pi.st.ok.eq(1)
497 yield
498 yield port1.pi.st.ok.eq(0)
499
500 # can go straight to reset.
501 yield port1.pi.is_st_i.eq(0) # end
502 yield port1.pi.addr.ok.eq(0) # set !ok
503 # yield from wait_busy(port1, False) # wait until not busy
504
505
506 def l0_cache_ld(dut, addr, expected):
507
508 l0 = dut.l0
509 mem = dut.mem
510 port0 = l0.dports[0]
511 port1 = l0.dports[1]
512
513 # have to wait until not busy
514 yield from wait_busy(port1, no=False) # wait until not busy
515
516 # set up a LD on the port. address first:
517 yield port1.pi.is_ld_i.eq(1) # indicate LD
518
519 yield port1.pi.addr.data.eq(addr) # set address
520 yield port1.pi.addr.ok.eq(1) # set ok
521 yield from wait_addr(port1) # wait until addr ok
522
523 yield from wait_ldok(port1) # wait until ld ok
524 data = yield port1.pi.ld.data
525
526 # cleanup
527 yield port1.pi.is_ld_i.eq(0) # end
528 yield port1.pi.addr.ok.eq(0) # set !ok
529 # yield from wait_busy(port1, no=False) # wait until not busy
530
531 return data
532
533
534 def l0_cache_ldst(dut):
535 yield
536 addr = 0x2
537 data = 0xbeef
538 data2 = 0xf00f
539 #data = 0x4
540 yield from l0_cache_st(dut, 0x2, data)
541 yield from l0_cache_st(dut, 0x3, data2)
542 result = yield from l0_cache_ld(dut, 0x2, data)
543 result2 = yield from l0_cache_ld(dut, 0x3, data2)
544 yield
545 assert data == result, "data %x != %x" % (result, data)
546 assert data2 == result2, "data2 %x != %x" % (result2, data2)
547
548 def data_merger_merge(dut):
549 print("data_merger")
550 #starting with all inputs zero
551 yield Settle()
552 en = yield dut.data_o.en
553 data = yield dut.data_o.data
554 assert en == 0, "en must be zero"
555 assert data == 0, "data must be zero"
556 yield
557
558 yield dut.addr_array_i[0].eq(0xFF)
559 for j in range(dut.array_size):
560 yield dut.data_i[j].en.eq(1 << j)
561 yield dut.data_i[j].data.eq(0xFF << (16*j))
562 yield Settle()
563
564 en = yield dut.data_o.en
565 data = yield dut.data_o.data
566 assert data == 0xff00ff00ff00ff00ff00ff00ff00ff
567 assert en == 0xff
568 yield
569
570 def test_l0_cache():
571
572 dut = TstL0CacheBuffer(regwid=64)
573 #vl = rtlil.convert(dut, ports=dut.ports())
574 #with open("test_basic_l0_cache.il", "w") as f:
575 # f.write(vl)
576
577 run_simulation(dut, l0_cache_ldst(dut),
578 vcd_name='test_l0_cache_basic.vcd')
579
580 def test_data_merger():
581
582 dut = DataMerger(8)
583 #vl = rtlil.convert(dut, ports=dut.ports())
584 #with open("test_data_merger.il", "w") as f:
585 # f.write(vl)
586
587 run_simulation(dut, data_merger_merge(dut),
588 vcd_name='test_data_merger.vcd')
589
590
591 if __name__ == '__main__':
592 test_l0_cache()
593 test_data_merger()