3 This first version is intended for prototyping and test purposes:
4 it has "direct" access to Memory.
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.
13 * https://bugs.libre-soc.org/show_bug.cgi?id=216
14 * https://libre-soc.org/3d_gpu/architecture/memory_and_cache/
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
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
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 from soc
.scoreboard
.addr_split
import LDSTSplitter
37 # for testing purposes
38 from soc
.experiment
.testmem
import TestMemory
41 class PortInterface(RecordObject
):
44 defines the interface - the API - that the LDSTCompUnit connects
45 to. note that this is NOT a "fire-and-forget" interface. the
46 LDSTCompUnit *must* be kept appraised that the request is in
47 progress, and only when it has a 100% successful completion rate
48 can the notification be given (busy dropped).
50 The interface FSM rules are as follows:
52 * if busy_o is asserted, a LD/ST is in progress. further
53 requests may not be made until busy_o is deasserted.
55 * only one of is_ld_i or is_st_i may be asserted. busy_o
56 will immediately be asserted and remain asserted.
58 * addr.ok is to be asserted when the LD/ST address is known.
59 addr.data is to be valid on the same cycle.
61 addr.ok and addr.data must REMAIN asserted until busy_o
62 is de-asserted. this ensures that there is no need
63 for the L0 Cache/Buffer to have an additional address latch
64 (because the LDSTCompUnit already has it)
66 * addr_ok_o (or addr_exc_o) must be waited for. these will
67 be asserted *only* for one cycle and one cycle only.
69 * addr_exc_o will be asserted if there is no chance that the
70 memory request may be fulfilled.
72 busy_o is deasserted on the same cycle as addr_exc_o is asserted.
74 * conversely: addr_ok_o must *ONLY* be asserted if there is a
75 HUNDRED PERCENT guarantee that the memory request will be
78 * for a LD, ld.ok will be asserted - for only one clock cycle -
79 at any point in the future that is acceptable to the underlying
80 Memory subsystem. the recipient MUST latch ld.data on that cycle.
82 busy_o is deasserted on the same cycle as ld.ok is asserted.
84 * for a ST, st.ok may be asserted only after addr_ok_o had been
85 asserted, alongside valid st.data at the same time. st.ok
86 must only be asserted for one cycle.
88 the underlying Memory is REQUIRED to pick up that data and
89 guarantee its delivery. no back-acknowledgement is required.
91 busy_o is deasserted on the cycle AFTER st.ok is asserted.
94 def __init__(self
, name
=None, regwid
=64, addrwid
=48):
97 self
._addrwid
= addrwid
99 RecordObject
.__init
__(self
, name
=name
)
101 # distinguish op type (ld/st)
102 self
.is_ld_i
= Signal(reset_less
=True)
103 self
.is_st_i
= Signal(reset_less
=True)
104 self
.op
= CompLDSTOpSubset() # hm insn_type ld/st duplicates here
107 self
.busy_o
= Signal(reset_less
=True) # do not use if busy
108 self
.go_die_i
= Signal(reset_less
=True) # back to reset
109 self
.addr
= Data(addrwid
, "addr_i") # addr/addr-ok
110 # addr is valid (TLB, L1 etc.)
111 self
.addr_ok_o
= Signal(reset_less
=True)
112 self
.addr_exc_o
= Signal(reset_less
=True) # TODO, "type" of exception
115 self
.ld
= Data(regwid
, "ld_data_o") # ok to be set by L0 Cache/Buf
116 self
.st
= Data(regwid
, "st_data_i") # ok to be set by CompUnit
118 # TODO: elaborate function
121 class DualPortSplitter(Elaboratable
):
124 * one incoming PortInterface
125 * two *OUTGOING* PortInterfaces
126 * uses LDSTSplitter to do it
128 (actually, thinking about it LDSTSplitter could simply be
129 modified to conform to PortInterface: one in, two out)
131 once that is done each pair of ports may be wired directly
132 to the dual ports of L0CacheBuffer
134 The split is carried out so that, regardless of alignment or
135 mis-alignment, outgoing PortInterface[0] takes bit 4 == 0
136 of the address, whilst outgoing PortInterface[1] takes
139 PortInterface *may* need to be changed so that the length is
140 a binary number (accepting values 1-16).
143 self
.outp
= [PortInterface(name
="outp_0"),
144 PortInterface(name
="outp_1")]
145 self
.inp
= PortInterface(name
="inp")
148 def elaborate(self
, platform
):
151 m
.submodules
.splitter
= splitter
= LDSTSplitter(64, 48, 4)
152 comb
+= splitter
.addr_i
.eq(self
.inp
.addr
) #XXX
153 #comb += splitter.len_i.eq()
154 #comb += splitter.valid_i.eq()
155 comb
+= splitter
.is_ld_i
.eq(self
.inp
.is_ld_i
)
156 comb
+= splitter
.is_st_i
.eq(self
.inp
.is_st_i
)
157 #comb += splitter.st_data_i.eq()
158 #comb += splitter.sld_valid_i.eq()
159 #comb += splitter.sld_data_i.eq()
160 #comb += splitter.sst_valid_i.eq()
163 class DataMergerRecord(Record
):
165 {data: 128 bit, byte_enable: 16 bit}
168 def __init__(self
, name
=None):
169 layout
= (('data', 128),
173 Record
.__init
__(self
, Layout(layout
), name
=name
)
175 #FIXME: make resetless
177 # TODO: formal verification
179 class DataMerger(Elaboratable
):
182 Merges data based on an address-match matrix.
183 Identifies (picks) one (any) row, then uses that row,
184 based on matching address bits, to merge (OR) all data
185 rows into the output.
187 Basically, by the time DataMerger is used, all of its incoming data is
188 determined not to conflict. The last step before actually submitting
189 the request to the Memory Subsystem is to work out which requests,
190 on the same 128-bit cache line, can be "merged" due to them being:
191 (A) on the same address (bits 4 and above) (B) having byte-enable
192 lines that (as previously mentioned) do not conflict.
194 Therefore, put simply, this module will:
195 (1) pick a row (any row) and identify it by an index labelled "idx"
196 (2) merge all byte-enable lines which are on that same address, as
197 indicated by addr_match_i[idx], onto the output
200 def __init__(self
, array_size
):
202 :addr_array_i: an NxN Array of Signals with bits set indicating address
203 match. bits across the diagonal (addr_array_i[x][x])
204 will always be set, to indicate "active".
205 :data_i: an Nx Array of Records {data: 128 bit, byte_enable: 16 bit}
206 :data_o: an Output Record of same type
207 {data: 128 bit, byte_enable: 16 bit}
209 self
.array_size
= array_size
211 for i
in range(array_size
):
212 ul
.append(Signal(array_size
,
214 name
="addr_match_%d" % i
))
215 self
.addr_array_i
= Array(ul
)
218 for i
in range(array_size
):
219 ul
.append(DataMergerRecord())
220 self
.data_i
= Array(ul
)
221 self
.data_o
= DataMergerRecord()
223 def elaborate(self
, platform
):
227 m
.submodules
.pick
= pick
= PriorityEncoder(self
.array_size
)
228 for j
in range(self
.array_size
):
229 comb
+= pick
.i
[j
].eq(self
.addr_array_i
[j
].bool())
235 for j
in range(self
.array_size
):
236 select
= self
.addr_array_i
[idx
][j
]
237 r
= DataMergerRecord()
239 comb
+= r
.eq(self
.data_i
[j
])
241 comb
+= self
.data_o
.data
.eq(ortreereduce(l
,"data"))
242 comb
+= self
.data_o
.en
.eq(ortreereduce(l
,"en"))
247 class LDSTPort(Elaboratable
):
248 def __init__(self
, idx
, regwid
=64, addrwid
=48):
249 self
.pi
= PortInterface("ldst_port%d" % idx
, regwid
, addrwid
)
251 def elaborate(self
, platform
):
253 comb
, sync
= m
.d
.comb
, m
.d
.sync
256 m
.submodules
.busy_l
= busy_l
= SRLatch(False, name
="busy")
257 m
.submodules
.cyc_l
= cyc_l
= SRLatch(True, name
="cyc")
258 comb
+= cyc_l
.s
.eq(0)
259 comb
+= cyc_l
.r
.eq(0)
261 # this is a little weird: we let the L0Cache/Buffer set
262 # the outputs: this module just monitors "state".
264 # LD/ST requested activates "busy"
265 with m
.If(self
.pi
.is_ld_i | self
.pi
.is_st_i
):
266 comb
+= busy_l
.s
.eq(1)
268 # monitor for an exception or the completion of LD.
269 with m
.If(self
.pi
.addr_exc_o
):
270 comb
+= busy_l
.r
.eq(1)
272 # however ST needs one cycle before busy is reset
273 with m
.If(self
.pi
.st
.ok | self
.pi
.ld
.ok
):
274 comb
+= cyc_l
.s
.eq(1)
277 comb
+= cyc_l
.r
.eq(1)
278 comb
+= busy_l
.r
.eq(1)
280 # busy latch outputs to interface
281 comb
+= self
.pi
.busy_o
.eq(busy_l
.q
)
286 yield self
.pi
.is_ld_i
287 yield self
.pi
.is_st_i
288 yield from self
.pi
.op
.ports()
290 yield self
.pi
.go_die_i
291 yield from self
.pi
.addr
.ports()
292 yield self
.pi
.addr_ok_o
293 yield self
.pi
.addr_exc_o
295 yield from self
.pi
.ld
.ports()
296 yield from self
.pi
.st
.ports()
302 class L0CacheBuffer(Elaboratable
):
305 Note that the final version will have *two* interfaces per LDSTCompUnit,
306 to cover mis-aligned requests, as well as *two* 128-bit L1 Cache
307 interfaces: one for odd (addr[4] == 1) and one for even (addr[4] == 1).
309 This version is to be used for test purposes (and actively maintained
310 for such, rather than "replaced")
312 There are much better ways to implement this. However it's only
313 a "demo" / "test" class, and one important aspect: it responds
314 combinatorially, where a nmigen FSM's state-changes only activate
315 on clock-sync boundaries.
318 def __init__(self
, n_units
, mem
, regwid
=64, addrwid
=48):
319 self
.n_units
= n_units
322 for i
in range(n_units
):
323 ul
.append(LDSTPort(i
, regwid
, addrwid
))
324 self
.dports
= Array(ul
)
326 def elaborate(self
, platform
):
328 comb
, sync
= m
.d
.comb
, m
.d
.sync
330 # connect the ports as modules
331 for i
in range(self
.n_units
):
332 setattr(m
.submodules
, "port%d" % i
, self
.dports
[i
])
334 # state-machine latches
335 m
.submodules
.st_active
= st_active
= SRLatch(False, name
="st_active")
336 m
.submodules
.ld_active
= ld_active
= SRLatch(False, name
="ld_active")
337 m
.submodules
.reset_l
= reset_l
= SRLatch(True, name
="reset")
338 m
.submodules
.idx_l
= idx_l
= SRLatch(False, name
="idx_l")
339 m
.submodules
.adrok_l
= adrok_l
= SRLatch(False, name
="addr_acked")
341 # find one LD (or ST) and do it. only one per cycle.
342 # TODO: in the "live" (production) L0Cache/Buffer, merge multiple
343 # LD/STs using mask-expansion - see LenExpand class
345 m
.submodules
.ldpick
= ldpick
= PriorityEncoder(self
.n_units
)
346 m
.submodules
.stpick
= stpick
= PriorityEncoder(self
.n_units
)
348 lds
= Signal(self
.n_units
, reset_less
=True)
349 sts
= Signal(self
.n_units
, reset_less
=True)
352 for i
in range(self
.n_units
):
353 pi
= self
.dports
[i
].pi
354 ldi
.append(pi
.is_ld_i
& pi
.busy_o
) # accumulate ld-req signals
355 sti
.append(pi
.is_st_i
& pi
.busy_o
) # accumulate st-req signals
356 # put the requests into the priority-pickers
357 comb
+= ldpick
.i
.eq(Cat(*ldi
))
358 comb
+= stpick
.i
.eq(Cat(*sti
))
360 # hmm, have to select (record) the right port index
361 nbits
= log2_int(self
.n_units
, False)
362 ld_idx
= Signal(nbits
, reset_less
=False)
363 st_idx
= Signal(nbits
, reset_less
=False)
364 # use these because of the sync-and-comb pass-through capability
365 latchregister(m
, ldpick
.o
, ld_idx
, idx_l
.qn
, name
="ld_idx_l")
366 latchregister(m
, stpick
.o
, st_idx
, idx_l
.qn
, name
="st_idx_l")
368 # convenience variables to reference the "picked" port
369 ldport
= self
.dports
[ld_idx
].pi
370 stport
= self
.dports
[st_idx
].pi
371 # and the memory ports
372 rdport
= self
.mem
.rdport
373 wrport
= self
.mem
.wrport
375 # Priority-Pickers pick one and only one request, capture its index.
376 # from that point on this code *only* "listens" to that port.
378 sync
+= adrok_l
.s
.eq(0)
379 comb
+= adrok_l
.r
.eq(0)
380 with m
.If(~ldpick
.n
):
381 comb
+= ld_active
.s
.eq(1) # activate LD mode
382 comb
+= idx_l
.r
.eq(1) # pick (and capture) the port index
383 with m
.Elif(~stpick
.n
):
384 comb
+= st_active
.s
.eq(1) # activate ST mode
385 comb
+= idx_l
.r
.eq(1) # pick (and capture) the port index
387 # from this point onwards, with the port "picked", it stays picked
388 # until ld_active (or st_active) are de-asserted.
390 # if now in "LD" mode: wait for addr_ok, then send the address out
391 # to memory, acknowledge address, and send out LD data
392 with m
.If(ld_active
.q
):
393 with m
.If(ldport
.addr
.ok
& adrok_l
.qn
):
394 comb
+= rdport
.addr
.eq(ldport
.addr
.data
) # addr ok, send thru
395 comb
+= ldport
.addr_ok_o
.eq(1) # acknowledge addr ok
396 sync
+= adrok_l
.s
.eq(1) # and pull "ack" latch
398 # if now in "ST" mode: likewise do the same but with "ST"
399 # to memory, acknowledge address, and send out LD data
400 with m
.If(st_active
.q
):
401 with m
.If(stport
.addr
.ok
):
402 comb
+= wrport
.addr
.eq(stport
.addr
.data
) # addr ok, send thru
403 with m
.If(adrok_l
.qn
):
404 comb
+= stport
.addr_ok_o
.eq(1) # acknowledge addr ok
405 sync
+= adrok_l
.s
.eq(1) # and pull "ack" latch
407 # NOTE: in both these, below, the port itself takes care
408 # of de-asserting its "busy_o" signal, based on either ld.ok going
409 # high (by us, here) or by st.ok going high (by the LDSTCompUnit).
411 # for LD mode, when addr has been "ok'd", assume that (because this
412 # is a "Memory" test-class) the memory read data is valid.
413 comb
+= reset_l
.s
.eq(0)
414 comb
+= reset_l
.r
.eq(0)
415 with m
.If(ld_active
.q
& adrok_l
.q
):
416 comb
+= ldport
.ld
.data
.eq(rdport
.data
) # put data out
417 comb
+= ldport
.ld
.ok
.eq(1) # indicate data valid
418 comb
+= reset_l
.s
.eq(1) # reset mode after 1 cycle
420 # for ST mode, when addr has been "ok'd", wait for incoming "ST ok"
421 with m
.If(st_active
.q
& stport
.st
.ok
):
422 comb
+= wrport
.data
.eq(stport
.st
.data
) # write st to mem
423 comb
+= wrport
.en
.eq(1) # enable write
424 comb
+= reset_l
.s
.eq(1) # reset mode after 1 cycle
426 # after waiting one cycle (reset_l is "sync" mode), reset the port
427 with m
.If(reset_l
.q
):
428 comb
+= idx_l
.s
.eq(1) # deactivate port-index selector
429 comb
+= ld_active
.r
.eq(1) # leave the ST active for 1 cycle
430 comb
+= st_active
.r
.eq(1) # leave the ST active for 1 cycle
431 comb
+= reset_l
.r
.eq(1) # clear reset
432 comb
+= adrok_l
.r
.eq(1) # address reset
437 for p
in self
.dports
:
441 class TstL0CacheBuffer(Elaboratable
):
442 def __init__(self
, n_units
=3, regwid
=16, addrwid
=4):
443 self
.mem
= TestMemory(regwid
, addrwid
)
444 self
.l0
= L0CacheBuffer(n_units
, self
.mem
, regwid
, addrwid
)
446 def elaborate(self
, platform
):
448 m
.submodules
.mem
= self
.mem
449 m
.submodules
.l0
= self
.l0
454 yield from self
.l0
.ports()
455 yield self
.mem
.rdport
.addr
456 yield self
.mem
.rdport
.data
457 yield self
.mem
.wrport
.addr
458 yield self
.mem
.wrport
.data
462 def wait_busy(port
, no
=False):
464 busy
= yield port
.pi
.busy_o
465 print("busy", no
, busy
)
473 addr_ok
= yield port
.pi
.addr_ok_o
474 print("addrok", addr_ok
)
482 ldok
= yield port
.pi
.ld
.ok
489 def l0_cache_st(dut
, addr
, data
):
495 # have to wait until not busy
496 yield from wait_busy(port1
, no
=False) # wait until not busy
498 # set up a ST on the port. address first:
499 yield port1
.pi
.is_st_i
.eq(1) # indicate LD
501 yield port1
.pi
.addr
.data
.eq(addr
) # set address
502 yield port1
.pi
.addr
.ok
.eq(1) # set ok
503 yield from wait_addr(port1
) # wait until addr ok
504 # yield # not needed, just for checking
505 # yield # not needed, just for checking
506 # assert "ST" for one cycle (required by the API)
507 yield port1
.pi
.st
.data
.eq(data
)
508 yield port1
.pi
.st
.ok
.eq(1)
510 yield port1
.pi
.st
.ok
.eq(0)
512 # can go straight to reset.
513 yield port1
.pi
.is_st_i
.eq(0) # end
514 yield port1
.pi
.addr
.ok
.eq(0) # set !ok
515 # yield from wait_busy(port1, False) # wait until not busy
518 def l0_cache_ld(dut
, addr
, expected
):
525 # have to wait until not busy
526 yield from wait_busy(port1
, no
=False) # wait until not busy
528 # set up a LD on the port. address first:
529 yield port1
.pi
.is_ld_i
.eq(1) # indicate LD
531 yield port1
.pi
.addr
.data
.eq(addr
) # set address
532 yield port1
.pi
.addr
.ok
.eq(1) # set ok
533 yield from wait_addr(port1
) # wait until addr ok
535 yield from wait_ldok(port1
) # wait until ld ok
536 data
= yield port1
.pi
.ld
.data
539 yield port1
.pi
.is_ld_i
.eq(0) # end
540 yield port1
.pi
.addr
.ok
.eq(0) # set !ok
541 # yield from wait_busy(port1, no=False) # wait until not busy
546 def l0_cache_ldst(dut
):
552 yield from l0_cache_st(dut
, 0x2, data
)
553 yield from l0_cache_st(dut
, 0x3, data2
)
554 result
= yield from l0_cache_ld(dut
, 0x2, data
)
555 result2
= yield from l0_cache_ld(dut
, 0x3, data2
)
557 assert data
== result
, "data %x != %x" % (result
, data
)
558 assert data2
== result2
, "data2 %x != %x" % (result2
, data2
)
560 def data_merger_merge(dut
):
562 #starting with all inputs zero
564 en
= yield dut
.data_o
.en
565 data
= yield dut
.data_o
.data
566 assert en
== 0, "en must be zero"
567 assert data
== 0, "data must be zero"
570 yield dut
.addr_array_i
[0].eq(0xFF)
571 for j
in range(dut
.array_size
):
572 yield dut
.data_i
[j
].en
.eq(1 << j
)
573 yield dut
.data_i
[j
].data
.eq(0xFF << (16*j
))
576 en
= yield dut
.data_o
.en
577 data
= yield dut
.data_o
.data
578 assert data
== 0xff00ff00ff00ff00ff00ff00ff00ff
584 dut
= TstL0CacheBuffer(regwid
=64)
585 #vl = rtlil.convert(dut, ports=dut.ports())
586 #with open("test_basic_l0_cache.il", "w") as f:
589 run_simulation(dut
, l0_cache_ldst(dut
),
590 vcd_name
='test_l0_cache_basic.vcd')
592 def test_data_merger():
595 #vl = rtlil.convert(dut, ports=dut.ports())
596 #with open("test_data_merger.il", "w") as f:
599 run_simulation(dut
, data_merger_merge(dut
),
600 vcd_name
='test_data_merger.vcd')
602 def test_dual_port_splitter():
604 dut
= DualPortSplitter()
605 #vl = rtlil.convert(dut, ports=dut.ports())
606 #with open("test_data_merger.il", "w") as f:
609 #run_simulation(dut, data_merger_merge(dut),
610 # vcd_name='test_dual_port_splitter.vcd')
612 if __name__
== '__main__':
615 #test_dual_port_splitter()