more use of namedtuples in core.py for clarity
[soc.git] / src / soc / simple / core.py
1 """simple core
2
3 not in any way intended for production use. connects up FunctionUnits to
4 Register Files in a brain-dead fashion that only permits one and only one
5 Function Unit to be operational.
6
7 the principle here is to take the Function Units, analyse their regspecs,
8 and turn their requirements for access to register file read/write ports
9 into groupings by Register File and Register File Port name.
10
11 under each grouping - by regfile/port - a list of Function Units that
12 need to connect to that port is created. as these are a contended
13 resource a "Broadcast Bus" per read/write port is then also created,
14 with access to it managed by a PriorityPicker.
15
16 the brain-dead part of this module is that even though there is no
17 conflict of access, regfile read/write hazards are *not* analysed,
18 and consequently it is safer to wait for the Function Unit to complete
19 before allowing a new instruction to proceed.
20 """
21
22 from nmigen import (Elaboratable, Module, Signal, ResetSignal, Cat, Mux,
23 Const)
24 from nmigen.cli import rtlil
25
26 from openpower.decoder.power_decoder2 import PowerDecodeSubset
27 from openpower.decoder.power_regspec_map import regspec_decode_read
28 from openpower.decoder.power_regspec_map import regspec_decode_write
29 from openpower.sv.svp64 import SVP64Rec
30
31 from nmutil.picker import PriorityPicker
32 from nmutil.util import treereduce
33 from nmutil.singlepipe import ControlBase
34
35 from soc.fu.compunits.compunits import AllFunctionUnits, LDSTFunctionUnit
36 from soc.regfile.regfiles import RegFiles
37 from openpower.decoder.power_decoder2 import get_rdflags
38 from soc.experiment.l0_cache import TstL0CacheBuffer # test only
39 from soc.config.test.test_loadstore import TestMemPspec
40 from openpower.decoder.power_enums import MicrOp, Function
41 from soc.simple.core_data import CoreInput, CoreOutput
42
43 from collections import defaultdict, namedtuple
44 import operator
45
46 from nmutil.util import rising_edge
47
48 FUSpec = namedtuple("FUSpec", ["funame", "fu", "idx"])
49 ByRegSpec = namedtuple("ByRegSpec", ["rdport", "wrport", "read",
50 "write", "wid", "specs"])
51
52 # helper function for reducing a list of signals down to a parallel
53 # ORed single signal.
54 def ortreereduce(tree, attr="o_data"):
55 return treereduce(tree, operator.or_, lambda x: getattr(x, attr))
56
57
58 def ortreereduce_sig(tree):
59 return treereduce(tree, operator.or_, lambda x: x)
60
61
62 # helper function to place full regs declarations first
63 def sort_fuspecs(fuspecs):
64 res = []
65 for (regname, fspec) in fuspecs.items():
66 if regname.startswith("full"):
67 res.append((regname, fspec))
68 for (regname, fspec) in fuspecs.items():
69 if not regname.startswith("full"):
70 res.append((regname, fspec))
71 return res # enumerate(res)
72
73
74 # derive from ControlBase rather than have a separate Stage instance,
75 # this is simpler to do
76 class NonProductionCore(ControlBase):
77 def __init__(self, pspec):
78 self.pspec = pspec
79
80 # test is SVP64 is to be enabled
81 self.svp64_en = hasattr(pspec, "svp64") and (pspec.svp64 == True)
82
83 # test to see if regfile ports should be reduced
84 self.regreduce_en = (hasattr(pspec, "regreduce") and
85 (pspec.regreduce == True))
86
87 # test to see if overlapping of instructions is allowed
88 # (not normally enabled for TestIssuer FSM but useful for checking
89 # the bitvector hazard detection, before doing In-Order)
90 self.allow_overlap = (hasattr(pspec, "allow_overlap") and
91 (pspec.allow_overlap == True))
92
93 # test core type
94 self.make_hazard_vecs = True
95 self.core_type = "fsm"
96 if hasattr(pspec, "core_type"):
97 self.core_type = pspec.core_type
98
99 super().__init__(stage=self)
100
101 # single LD/ST funnel for memory access
102 self.l0 = l0 = TstL0CacheBuffer(pspec, n_units=1)
103 pi = l0.l0.dports[0]
104
105 # function units (only one each)
106 # only include mmu if enabled in pspec
107 self.fus = AllFunctionUnits(pspec, pilist=[pi])
108
109 # link LoadStore1 into MMU
110 mmu = self.fus.get_fu('mmu0')
111 print ("core pspec", pspec.ldst_ifacetype)
112 print ("core mmu", mmu)
113 if mmu is not None:
114 print ("core lsmem.lsi", l0.cmpi.lsmem.lsi)
115 mmu.alu.set_ldst_interface(l0.cmpi.lsmem.lsi)
116
117 # register files (yes plural)
118 self.regs = RegFiles(pspec, make_hazard_vecs=self.make_hazard_vecs)
119
120 # set up input and output: unusual requirement to set data directly
121 # (due to the way that the core is set up in a different domain,
122 # see TestIssuer.setup_peripherals
123 self.p.i_data, self.n.o_data = self.new_specs(None)
124 self.i, self.o = self.p.i_data, self.n.o_data
125
126 # actual internal input data used (captured)
127 self.ireg = self.ispec()
128
129 # create per-FU instruction decoders (subsetted). these "satellite"
130 # decoders reduce wire fan-out from the one (main) PowerDecoder2
131 # (used directly by the trap unit) to the *twelve* (or more)
132 # Function Units. we can either have 32 wires (the instruction)
133 # to each, or we can have well over a 200 wire fan-out (to 12
134 # ALUs). it's an easy choice to make.
135 self.decoders = {}
136 self.des = {}
137
138 for funame, fu in self.fus.fus.items():
139 f_name = fu.fnunit.name
140 fnunit = fu.fnunit.value
141 opkls = fu.opsubsetkls
142 if f_name == 'TRAP':
143 # TRAP decoder is the *main* decoder
144 self.trapunit = funame
145 continue
146 self.decoders[funame] = PowerDecodeSubset(None, opkls, f_name,
147 final=True,
148 state=self.ireg.state,
149 svp64_en=self.svp64_en,
150 regreduce_en=self.regreduce_en)
151 self.des[funame] = self.decoders[funame].do
152
153 # share the SPR decoder with the MMU if it exists
154 if "mmu0" in self.decoders:
155 self.decoders["mmu0"].mmu0_spr_dec = self.decoders["spr0"]
156
157 # next 3 functions are Stage API Compliance
158 def setup(self, m, i):
159 pass
160
161 def ispec(self):
162 return CoreInput(self.pspec, self.svp64_en, self.regreduce_en)
163
164 def ospec(self):
165 return CoreOutput()
166
167 # elaborate function to create HDL
168 def elaborate(self, platform):
169 m = super().elaborate(platform)
170
171 # for testing purposes, to cut down on build time in coriolis2
172 if hasattr(self.pspec, "nocore") and self.pspec.nocore == True:
173 x = Signal() # dummy signal
174 m.d.sync += x.eq(~x)
175 return m
176 comb = m.d.comb
177
178 m.submodules.fus = self.fus
179 m.submodules.l0 = l0 = self.l0
180 self.regs.elaborate_into(m, platform)
181 regs = self.regs
182 fus = self.fus.fus
183
184 # connect decoders
185 self.connect_satellite_decoders(m)
186
187 # ssh, cheat: trap uses the main decoder because of the rewriting
188 self.des[self.trapunit] = self.ireg.e.do
189
190 # connect up Function Units, then read/write ports, and hazard conflict
191 self.issue_conflict = Signal()
192 fu_bitdict, fu_selected = self.connect_instruction(m)
193 raw_hazard = self.connect_rdports(m, fu_bitdict, fu_selected)
194 self.connect_wrports(m, fu_bitdict, fu_selected)
195 if self.allow_overlap:
196 comb += self.issue_conflict.eq(raw_hazard)
197
198 # note if an exception happened. in a pipelined or OoO design
199 # this needs to be accompanied by "shadowing" (or stalling)
200 el = []
201 for exc in self.fus.excs.values():
202 el.append(exc.happened)
203 if len(el) > 0: # at least one exception
204 comb += self.o.exc_happened.eq(Cat(*el).bool())
205
206 return m
207
208 def connect_satellite_decoders(self, m):
209 comb = m.d.comb
210 for k, v in self.decoders.items():
211 # connect each satellite decoder and give it the instruction.
212 # as subset decoders this massively reduces wire fanout given
213 # the large number of ALUs
214 setattr(m.submodules, "dec_%s" % v.fn_name, v)
215 comb += v.dec.raw_opcode_in.eq(self.ireg.raw_insn_i)
216 comb += v.dec.bigendian.eq(self.ireg.bigendian_i)
217 # sigh due to SVP64 RA_OR_ZERO detection connect these too
218 comb += v.sv_a_nz.eq(self.ireg.sv_a_nz)
219 if self.svp64_en:
220 comb += v.pred_sm.eq(self.ireg.sv_pred_sm)
221 comb += v.pred_dm.eq(self.ireg.sv_pred_dm)
222 if k != self.trapunit:
223 comb += v.sv_rm.eq(self.ireg.sv_rm) # pass through SVP64 RM
224 comb += v.is_svp64_mode.eq(self.ireg.is_svp64_mode)
225 # only the LDST PowerDecodeSubset *actually* needs to
226 # know to use the alternative decoder. this is all
227 # a terrible hack
228 if k.lower().startswith("ldst"):
229 comb += v.use_svp64_ldst_dec.eq(
230 self.ireg.use_svp64_ldst_dec)
231
232 def connect_instruction(self, m):
233 """connect_instruction
234
235 uses decoded (from PowerOp) function unit information from CSV files
236 to ascertain which Function Unit should deal with the current
237 instruction.
238
239 some (such as OP_ATTN, OP_NOP) are dealt with here, including
240 ignoring it and halting the processor. OP_NOP is a bit annoying
241 because the issuer expects busy flag still to be raised then lowered.
242 (this requires a fake counter to be set).
243 """
244 comb, sync = m.d.comb, m.d.sync
245 fus = self.fus.fus
246
247 # indicate if core is busy
248 busy_o = self.o.busy_o
249 any_busy_o = self.o.any_busy_o
250
251 # connect up temporary copy of incoming instruction. the FSM will
252 # either blat the incoming instruction (if valid) into self.ireg
253 # or if the instruction could not be delivered, keep dropping the
254 # latched copy into ireg
255 ilatch = self.ispec()
256 self.instr_active = Signal()
257
258 # enable/busy-signals for each FU, get one bit for each FU (by name)
259 fu_enable = Signal(len(fus), reset_less=True)
260 fu_busy = Signal(len(fus), reset_less=True)
261 fu_bitdict = {}
262 fu_selected = {}
263 for i, funame in enumerate(fus.keys()):
264 fu_bitdict[funame] = fu_enable[i]
265 fu_selected[funame] = fu_busy[i]
266
267 # identify function units and create a list by fnunit so that
268 # PriorityPickers can be created for selecting one of them that
269 # isn't busy at the time the incoming instruction needs passing on
270 by_fnunit = defaultdict(list)
271 for fname, member in Function.__members__.items():
272 for funame, fu in fus.items():
273 fnunit = fu.fnunit.value
274 if member.value & fnunit: # this FU handles this type of op
275 by_fnunit[fname].append((funame, fu)) # add by Function
276
277 # ok now just print out the list of FUs by Function, because we can
278 for fname, fu_list in by_fnunit.items():
279 print ("FUs by type", fname, fu_list)
280
281 # now create a PriorityPicker per FU-type such that only one
282 # non-busy FU will be picked
283 issue_pps = {}
284 fu_found = Signal() # take a note if no Function Unit was available
285 for fname, fu_list in by_fnunit.items():
286 i_pp = PriorityPicker(len(fu_list))
287 m.submodules['i_pp_%s' % fname] = i_pp
288 i_l = []
289 for i, (funame, fu) in enumerate(fu_list):
290 # match the decoded instruction (e.do.fn_unit) against the
291 # "capability" of this FU, gate that by whether that FU is
292 # busy, and drop that into the PriorityPicker.
293 # this will give us an output of the first available *non-busy*
294 # Function Unit (Reservation Statio) capable of handling this
295 # instruction.
296 fnunit = fu.fnunit.value
297 en_req = Signal(name="issue_en_%s" % funame, reset_less=True)
298 fnmatch = (self.ireg.e.do.fn_unit & fnunit).bool()
299 comb += en_req.eq(fnmatch & ~fu.busy_o &
300 self.instr_active)
301 i_l.append(en_req) # store in list for doing the Cat-trick
302 # picker output, gated by enable: store in fu_bitdict
303 po = Signal(name="o_issue_pick_"+funame) # picker output
304 comb += po.eq(i_pp.o[i] & i_pp.en_o)
305 comb += fu_bitdict[funame].eq(po)
306 comb += fu_selected[funame].eq(fu.busy_o | po)
307 # if we don't do this, then when there are no FUs available,
308 # the "p.o_ready" signal will go back "ok we accepted this
309 # instruction" which of course isn't true.
310 with m.If(i_pp.en_o):
311 comb += fu_found.eq(1)
312 # for each input, Cat them together and drop them into the picker
313 comb += i_pp.i.eq(Cat(*i_l))
314
315 # rdmask, which is for registers needs to come from the *main* decoder
316 for funame, fu in fus.items():
317 rdmask = get_rdflags(self.ireg.e, fu)
318 comb += fu.rdmaskn.eq(~rdmask)
319
320 # sigh - need a NOP counter
321 counter = Signal(2)
322 with m.If(counter != 0):
323 sync += counter.eq(counter - 1)
324 comb += busy_o.eq(1)
325
326 # default to reading from incoming instruction: may be overridden
327 # by copy from latch when "waiting"
328 comb += self.ireg.eq(self.i)
329 # always say "ready" except if overridden
330 comb += self.p.o_ready.eq(1)
331
332 with m.FSM():
333 with m.State("READY"):
334 with m.If(self.p.i_valid): # run only when valid
335 with m.Switch(self.ireg.e.do.insn_type):
336 # check for ATTN: halt if true
337 with m.Case(MicrOp.OP_ATTN):
338 m.d.sync += self.o.core_terminate_o.eq(1)
339
340 # fake NOP - this isn't really used (Issuer detects NOP)
341 with m.Case(MicrOp.OP_NOP):
342 sync += counter.eq(2)
343 comb += busy_o.eq(1)
344
345 with m.Default():
346 comb += self.instr_active.eq(1)
347 comb += self.p.o_ready.eq(0)
348 # connect instructions. only one enabled at a time
349 for funame, fu in fus.items():
350 do = self.des[funame]
351 enable = fu_bitdict[funame]
352
353 # run this FunctionUnit if enabled route op,
354 # issue, busy, read flags and mask to FU
355 with m.If(enable):
356 # operand comes from the *local* decoder
357 comb += fu.oper_i.eq_from(do)
358 comb += fu.issue_i.eq(1) # issue when valid
359 # instruction ok, indicate ready
360 comb += self.p.o_ready.eq(1)
361
362 if self.allow_overlap:
363 with m.If(~fu_found):
364 # latch copy of instruction
365 sync += ilatch.eq(self.i)
366 comb += self.p.o_ready.eq(1) # accept
367 comb += busy_o.eq(1)
368 m.next = "WAITING"
369
370 with m.State("WAITING"):
371 comb += self.instr_active.eq(1)
372 comb += self.p.o_ready.eq(0)
373 comb += busy_o.eq(1)
374 # using copy of instruction, keep waiting until an FU is free
375 comb += self.ireg.eq(ilatch)
376 with m.If(fu_found): # wait for conflict to clear
377 # connect instructions. only one enabled at a time
378 for funame, fu in fus.items():
379 do = self.des[funame]
380 enable = fu_bitdict[funame]
381
382 # run this FunctionUnit if enabled route op,
383 # issue, busy, read flags and mask to FU
384 with m.If(enable):
385 # operand comes from the *local* decoder
386 comb += fu.oper_i.eq_from(do)
387 comb += fu.issue_i.eq(1) # issue when valid
388 comb += self.p.o_ready.eq(1)
389 comb += busy_o.eq(0)
390 m.next = "READY"
391
392 print ("core: overlap allowed", self.allow_overlap)
393 busys = map(lambda fu: fu.busy_o, fus.values())
394 comb += any_busy_o.eq(Cat(*busys).bool())
395 if not self.allow_overlap:
396 # for simple non-overlap, if any instruction is busy, set
397 # busy output for core.
398 comb += busy_o.eq(any_busy_o)
399 else:
400 # sigh deal with a fun situation that needs to be investigated
401 # and resolved
402 with m.If(self.issue_conflict):
403 comb += busy_o.eq(1)
404
405 # return both the function unit "enable" dict as well as the "busy".
406 # the "busy-or-issued" can be passed in to the Read/Write port
407 # connecters to give them permission to request access to regfiles
408 return fu_bitdict, fu_selected
409
410 def connect_rdport(self, m, fu_bitdict, fu_selected,
411 rdpickers, regfile, regname, fspec):
412 comb, sync = m.d.comb, m.d.sync
413 fus = self.fus.fus
414 regs = self.regs
415
416 rpidx = regname
417
418 # select the required read port. these are pre-defined sizes
419 rfile = regs.rf[regfile.lower()]
420 rport = rfile.r_ports[rpidx]
421 print("read regfile", rpidx, regfile, regs.rf.keys(),
422 rfile, rfile.unary)
423
424 # for checking if the read port has an outstanding write
425 if self.make_hazard_vecs:
426 wv = regs.wv[regfile.lower()]
427 wvchk = wv.r_ports["issue"] # write-vec bit-level hazard check
428
429 # if a hazard is detected on this read port, simply blithely block
430 # every FU from reading on it. this is complete overkill but very
431 # simple for now.
432 hazard_detected = Signal(name="raw_%s_%s" % (regfile, rpidx))
433
434 fspecs = fspec
435 if not isinstance(fspecs, list):
436 fspecs = [fspecs]
437
438 rdflags = []
439 pplen = 0
440 ppoffs = []
441 for i, fspec in enumerate(fspecs):
442 # get the regfile specs for this regfile port
443 (rf, wf, _read, _write, wid, fuspecs) = \
444 (fspec.rdport, fspec.wrport, fspec.read, fspec.write,
445 fspec.wid, fspec.specs)
446 print ("fpsec", i, fspec, len(fuspecs))
447 ppoffs.append(pplen) # record offset for picker
448 pplen += len(fspec.specs)
449 name = "rdflag_%s_%s_%d" % (regfile, regname, i)
450 rdflag = Signal(name=name, reset_less=True)
451 comb += rdflag.eq(fspec.rdport)
452 rdflags.append(rdflag)
453
454 print ("pplen", pplen)
455
456 # create a priority picker to manage this port
457 rdpickers[regfile][rpidx] = rdpick = PriorityPicker(pplen)
458 setattr(m.submodules, "rdpick_%s_%s" % (regfile, rpidx), rdpick)
459
460 rens = []
461 addrs = []
462 wvens = []
463
464 for i, fspec in enumerate(fspecs):
465 (rf, wf, _read, _write, wid, fuspecs) = \
466 (fspec.rdport, fspec.wrport, fspec.read, fspec.write,
467 fspec.wid, fspec.specs)
468 # connect up the FU req/go signals, and the reg-read to the FU
469 # and create a Read Broadcast Bus
470 for pi, fuspec in enumerate(fspec.specs):
471 (funame, fu, idx) = (fuspec.funame, fuspec.fu, fuspec.idx)
472 pi += ppoffs[i]
473 name = "%s_%s_%s_%i" % (regfile, rpidx, funame, pi)
474 fu_active = fu_selected[funame]
475 fu_issued = fu_bitdict[funame]
476
477 # get (or set up) a latched copy of read register number
478 rname = "%s_%s_%s_%d" % (funame, regfile, regname, pi)
479 read = Signal.like(_read, name="read_"+name)
480 if rname not in fu.rd_latches:
481 rdl = Signal.like(_read, name="rdlatch_"+rname)
482 fu.rd_latches[rname] = rdl
483 with m.If(fu.issue_i):
484 sync += rdl.eq(_read)
485 else:
486 rdl = fu.rd_latches[rname]
487 # latch to make the read immediately available on issue cycle
488 # after the read cycle, use the latched copy
489 with m.If(fu.issue_i):
490 comb += read.eq(_read)
491 with m.Else():
492 comb += read.eq(rdl)
493
494 # connect request-read to picker input, and output to go-rd
495 addr_en = Signal.like(read, name="addr_en_"+name)
496 pick = Signal(name="pick_"+name) # picker input
497 rp = Signal(name="rp_"+name) # picker output
498 delay_pick = Signal(name="dp_"+name) # read-enable "underway"
499 rhazard = Signal(name="rhaz_"+name)
500
501 # exclude any currently-enabled read-request (mask out active)
502 # entirely block anything hazarded from being picked
503 comb += pick.eq(fu.rd_rel_o[idx] & fu_active & rdflags[i] &
504 ~delay_pick & ~rhazard)
505 comb += rdpick.i[pi].eq(pick)
506 comb += fu.go_rd_i[idx].eq(delay_pick) # pass in *delayed* pick
507
508 # if picked, select read-port "reg select" number to port
509 comb += rp.eq(rdpick.o[pi] & rdpick.en_o)
510 sync += delay_pick.eq(rp) # delayed "pick"
511 comb += addr_en.eq(Mux(rp, read, 0))
512
513 # the read-enable happens combinatorially (see mux-bus below)
514 # but it results in the data coming out on a one-cycle delay.
515 if rfile.unary:
516 rens.append(addr_en)
517 else:
518 addrs.append(addr_en)
519 rens.append(rp)
520
521 # use the *delayed* pick signal to put requested data onto bus
522 with m.If(delay_pick):
523 # connect regfile port to input, creating fan-out Bus
524 src = fu.src_i[idx]
525 print("reg connect widths",
526 regfile, regname, pi, funame,
527 src.shape(), rport.o_data.shape())
528 # all FUs connect to same port
529 comb += src.eq(rport.o_data)
530
531 if not self.make_hazard_vecs:
532 continue
533
534 # read the write-hazard bitvector (wv) for any bit that is
535 wvchk_en = Signal(len(wvchk.ren), name="wv_chk_addr_en_"+name)
536 issue_active = Signal(name="rd_iactive_"+name)
537 # XXX combinatorial loop here
538 comb += issue_active.eq(fu_active & rf)
539 with m.If(issue_active):
540 if rfile.unary:
541 comb += wvchk_en.eq(read)
542 else:
543 comb += wvchk_en.eq(1<<read)
544 # if FU is busy (which doesn't get set at the same time as
545 # issue) and no hazard was detected, clear wvchk_en (i.e.
546 # stop checking for hazards). there is a loop here, but it's
547 # via a DFF, so is ok. some linters may complain, but hey.
548 with m.If(fu.busy_o & ~rhazard):
549 comb += wvchk_en.eq(0)
550
551 # read-hazard is ANDed with (filtered by) what is actually
552 # being requested.
553 comb += rhazard.eq((wvchk.o_data & wvchk_en).bool())
554
555 wvens.append(wvchk_en)
556
557 # or-reduce the muxed read signals
558 if rfile.unary:
559 # for unary-addressed
560 comb += rport.ren.eq(ortreereduce_sig(rens))
561 else:
562 # for binary-addressed
563 comb += rport.addr.eq(ortreereduce_sig(addrs))
564 comb += rport.ren.eq(Cat(*rens).bool())
565 print ("binary", regfile, rpidx, rport, rport.ren, rens, addrs)
566
567 if not self.make_hazard_vecs:
568 return Const(0) # declare "no hazards"
569
570 # enable the read bitvectors for this issued instruction
571 # and return whether any write-hazard bit is set
572 comb += wvchk.ren.eq(ortreereduce_sig(wvens))
573 comb += hazard_detected.eq(wvchk.o_data.bool())
574 return hazard_detected
575
576 def connect_rdports(self, m, fu_bitdict, fu_selected):
577 """connect read ports
578
579 orders the read regspecs into a dict-of-dicts, by regfile, by
580 regport name, then connects all FUs that want that regport by
581 way of a PriorityPicker.
582 """
583 comb, sync = m.d.comb, m.d.sync
584 fus = self.fus.fus
585 regs = self.regs
586 rd_hazard = []
587
588 # dictionary of lists of regfile read ports
589 byregfiles_rd, byregfiles_rdspec = self.get_byregfiles(True)
590
591 # okaay, now we need a PriorityPicker per regfile per regfile port
592 # loootta pickers... peter piper picked a pack of pickled peppers...
593 rdpickers = {}
594 for regfile, spec in byregfiles_rd.items():
595 fuspecs = byregfiles_rdspec[regfile]
596 rdpickers[regfile] = {}
597
598 # argh. an experiment to merge RA and RB in the INT regfile
599 # (we have too many read/write ports)
600 if self.regreduce_en:
601 if regfile == 'INT':
602 fuspecs['rabc'] = [fuspecs.pop('rb')]
603 fuspecs['rabc'].append(fuspecs.pop('rc'))
604 fuspecs['rabc'].append(fuspecs.pop('ra'))
605 if regfile == 'FAST':
606 fuspecs['fast1'] = [fuspecs.pop('fast1')]
607 if 'fast2' in fuspecs:
608 fuspecs['fast1'].append(fuspecs.pop('fast2'))
609 if 'fast3' in fuspecs:
610 fuspecs['fast1'].append(fuspecs.pop('fast3'))
611
612 # for each named regfile port, connect up all FUs to that port
613 # also return (and collate) hazard detection)
614 for (regname, fspec) in sort_fuspecs(fuspecs):
615 print("connect rd", regname, fspec)
616 rh = self.connect_rdport(m, fu_bitdict, fu_selected,
617 rdpickers, regfile,
618 regname, fspec)
619 rd_hazard.append(rh)
620
621 return Cat(*rd_hazard).bool()
622
623 def make_hazards(self, m, regfile, rfile, wvclr, wvset,
624 funame, regname, idx,
625 addr_en, wp, fu, fu_active, wrflag, write,
626 fu_wrok):
627 """make_hazards: a setter and a clearer for the regfile write ports
628
629 setter is at issue time (using PowerDecoder2 regfile write numbers)
630 clearer is at regfile write time (when FU has said what to write to)
631
632 there is *one* unusual case here which has to be dealt with:
633 when the Function Unit does *NOT* request a write to the regfile
634 (has its data.ok bit CLEARED). this is perfectly legitimate.
635 and a royal pain.
636 """
637 comb, sync = m.d.comb, m.d.sync
638 name = "%s_%s_%d" % (funame, regname, idx)
639
640 # connect up the bitvector write hazard. unlike the
641 # regfile writeports, a ONE must be written to the corresponding
642 # bit of the hazard bitvector (to indicate the existence of
643 # the hazard)
644
645 # the detection of what shall be written to is based
646 # on *issue*
647 print ("write vector (for regread)", regfile, wvset)
648 wviaddr_en = Signal(len(wvset.wen), name="wv_issue_addr_en_"+name)
649 issue_active = Signal(name="iactive_"+name)
650 comb += issue_active.eq(fu.issue_i & fu_active & wrflag)
651 with m.If(issue_active):
652 if rfile.unary:
653 comb += wviaddr_en.eq(write)
654 else:
655 comb += wviaddr_en.eq(1<<write)
656
657 # deal with write vector clear: this kicks in when the regfile
658 # is written to, and clears the corresponding bitvector entry
659 print ("write vector", regfile, wvclr)
660 wvaddr_en = Signal(len(wvclr.wen), name="wvaddr_en_"+name)
661 if rfile.unary:
662 comb += wvaddr_en.eq(addr_en)
663 else:
664 with m.If(wp):
665 comb += wvaddr_en.eq(1<<addr_en)
666
667 # XXX ASSUME that LDSTFunctionUnit always sets the data it intends to
668 # this may NOT be the case when an exception occurs
669 if isinstance(fu, LDSTFunctionUnit):
670 return wvaddr_en, wviaddr_en
671
672 # okaaay, this is preparation for the awkward case.
673 # * latch a copy of wrflag when issue goes high.
674 # * when the fu_wrok (data.ok) flag is NOT set,
675 # but the FU is done, the FU is NEVER going to write
676 # so the bitvector has to be cleared.
677 latch_wrflag = Signal(name="latch_wrflag_"+name)
678 with m.If(~fu.busy_o):
679 sync += latch_wrflag.eq(0)
680 with m.If(fu.issue_i & fu_active):
681 sync += latch_wrflag.eq(wrflag)
682 with m.If(fu.alu_done_o & latch_wrflag & ~fu_wrok):
683 if rfile.unary:
684 comb += wvaddr_en.eq(write) # addr_en gated with wp, don't use
685 else:
686 comb += wvaddr_en.eq(1<<addr_en) # binary addr_en not gated
687
688 return wvaddr_en, wviaddr_en
689
690 def connect_wrport(self, m, fu_bitdict, fu_selected,
691 wrpickers, regfile, regname, fspec):
692 comb, sync = m.d.comb, m.d.sync
693 fus = self.fus.fus
694 regs = self.regs
695
696 rpidx = regname
697
698 # select the required write port. these are pre-defined sizes
699 rfile = regs.rf[regfile.lower()]
700 wport = rfile.w_ports[rpidx]
701
702 print("connect wr", regname, "unary", rfile.unary, fspec)
703 print(regfile, regs.rf.keys())
704
705 # select the write-protection hazard vector. note that this still
706 # requires to WRITE to the hazard bitvector! read-requests need
707 # to RAISE the bitvector (set it to 1), which, duh, requires a WRITE
708 if self.make_hazard_vecs:
709 wv = regs.wv[regfile.lower()]
710 wvset = wv.w_ports["set"] # write-vec bit-level hazard ctrl
711 wvclr = wv.w_ports["clr"] # write-vec bit-level hazard ctrl
712
713 fspecs = fspec
714 if not isinstance(fspecs, list):
715 fspecs = [fspecs]
716
717 pplen = 0
718 writes = []
719 ppoffs = []
720 rdflags = []
721 wrflags = []
722 for i, fspec in enumerate(fspecs):
723 # get the regfile specs for this regfile port
724 (rf, wf, _read, _write, wid, fuspecs) = \
725 (fspec.rdport, fspec.wrport, fspec.read, fspec.write,
726 fspec.wid, fspec.specs)
727 print ("fpsec", i, "wrflag", wf, fspec, len(fuspecs))
728 ppoffs.append(pplen) # record offset for picker
729 pplen += len(fuspecs)
730
731 name = "%s_%s_%d" % (regfile, regname, i)
732 rdflag = Signal(name="rd_flag_"+name)
733 wrflag = Signal(name="wr_flag_"+name)
734 if rf is not None:
735 comb += rdflag.eq(rf)
736 else:
737 comb += rdflag.eq(0)
738 if wf is not None:
739 comb += wrflag.eq(wf)
740 else:
741 comb += wrflag.eq(0)
742 rdflags.append(rdflag)
743 wrflags.append(wrflag)
744
745 # create a priority picker to manage this port
746 wrpickers[regfile][rpidx] = wrpick = PriorityPicker(pplen)
747 setattr(m.submodules, "wrpick_%s_%s" % (regfile, rpidx), wrpick)
748
749 wsigs = []
750 wens = []
751 wvsets = []
752 wvseten = []
753 wvclren = []
754 addrs = []
755 for i, fspec in enumerate(fspecs):
756 # connect up the FU req/go signals and the reg-read to the FU
757 # these are arbitrated by Data.ok signals
758 (rf, wf, _read, _write, wid, fuspecs) = \
759 (fspec.rdport, fspec.wrport, fspec.read, fspec.write,
760 fspec.wid, fspec.specs)
761 for pi, fuspec in enumerate(fspec.specs):
762 (funame, fu, idx) = (fuspec.funame, fuspec.fu, fuspec.idx)
763 pi += ppoffs[i]
764 name = "%s_%s_%s_%d" % (funame, regfile, regname, idx)
765 # get (or set up) a write-latched copy of write register number
766 write = Signal.like(_write, name="write_"+name)
767 rname = "%s_%s_%s" % (funame, regfile, regname)
768 if rname not in fu.wr_latches:
769 wrl = Signal.like(_write, name="wrlatch_"+rname)
770 fu.wr_latches[rname] = write
771 with m.If(fu.issue_i):
772 sync += wrl.eq(_write)
773 comb += write.eq(_write)
774 with m.Else():
775 comb += write.eq(wrl)
776 else:
777 write = fu.wr_latches[rname]
778
779 # write-request comes from dest.ok
780 dest = fu.get_out(idx)
781 fu_dest_latch = fu.get_fu_out(idx) # latched output
782 name = "fu_wrok_%s_%s_%d" % (funame, regname, idx)
783 fu_wrok = Signal(name=name, reset_less=True)
784 comb += fu_wrok.eq(dest.ok & fu.busy_o)
785
786 # connect request-write to picker input, and output to go-wr
787 fu_active = fu_selected[funame]
788 pick = fu.wr.rel_o[idx] & fu_active
789 comb += wrpick.i[pi].eq(pick)
790 # create a single-pulse go write from the picker output
791 wr_pick = Signal(name="wpick_%s_%s_%d" % (funame, regname, idx))
792 comb += wr_pick.eq(wrpick.o[pi] & wrpick.en_o)
793 comb += fu.go_wr_i[idx].eq(rising_edge(m, wr_pick))
794
795 # connect the regspec write "reg select" number to this port
796 # only if one FU actually requests (and is granted) the port
797 # will the write-enable be activated
798 wname = "waddr_en_%s_%s_%d" % (funame, regname, idx)
799 addr_en = Signal.like(write, name=wname)
800 wp = Signal()
801 comb += wp.eq(wr_pick & wrpick.en_o)
802 comb += addr_en.eq(Mux(wp, write, 0))
803 if rfile.unary:
804 wens.append(addr_en)
805 else:
806 addrs.append(addr_en)
807 wens.append(wp)
808
809 # connect regfile port to input
810 print("reg connect widths",
811 regfile, regname, pi, funame,
812 dest.shape(), wport.i_data.shape())
813 wsigs.append(fu_dest_latch)
814
815 # now connect up the bitvector write hazard
816 if not self.make_hazard_vecs:
817 continue
818 res = self.make_hazards(m, regfile, rfile, wvclr, wvset,
819 funame, regname, idx,
820 addr_en, wp, fu, fu_active,
821 wrflags[i], write, fu_wrok)
822 wvaddr_en, wv_issue_en = res
823 wvclren.append(wvaddr_en) # set only: no data => clear bit
824 wvseten.append(wv_issue_en) # set data same as enable
825 wvsets.append(wv_issue_en) # because enable needs a 1
826
827 # here is where we create the Write Broadcast Bus. simple, eh?
828 comb += wport.i_data.eq(ortreereduce_sig(wsigs))
829 if rfile.unary:
830 # for unary-addressed
831 comb += wport.wen.eq(ortreereduce_sig(wens))
832 else:
833 # for binary-addressed
834 comb += wport.addr.eq(ortreereduce_sig(addrs))
835 comb += wport.wen.eq(ortreereduce_sig(wens))
836
837 if not self.make_hazard_vecs:
838 return
839
840 # for write-vectors
841 comb += wvclr.wen.eq(ortreereduce_sig(wvclren)) # clear (regfile write)
842 comb += wvset.wen.eq(ortreereduce_sig(wvseten)) # set (issue time)
843 comb += wvset.i_data.eq(ortreereduce_sig(wvsets))
844
845 def connect_wrports(self, m, fu_bitdict, fu_selected):
846 """connect write ports
847
848 orders the write regspecs into a dict-of-dicts, by regfile,
849 by regport name, then connects all FUs that want that regport
850 by way of a PriorityPicker.
851
852 note that the write-port wen, write-port data, and go_wr_i all need to
853 be on the exact same clock cycle. as there is a combinatorial loop bug
854 at the moment, these all use sync.
855 """
856 comb, sync = m.d.comb, m.d.sync
857 fus = self.fus.fus
858 regs = self.regs
859 # dictionary of lists of regfile write ports
860 byregfiles_wr, byregfiles_wrspec = self.get_byregfiles(False)
861
862 # same for write ports.
863 # BLECH! complex code-duplication! BLECH!
864 wrpickers = {}
865 for regfile, spec in byregfiles_wr.items():
866 fuspecs = byregfiles_wrspec[regfile]
867 wrpickers[regfile] = {}
868
869 if self.regreduce_en:
870 # argh, more port-merging
871 if regfile == 'INT':
872 fuspecs['o'] = [fuspecs.pop('o')]
873 fuspecs['o'].append(fuspecs.pop('o1'))
874 if regfile == 'FAST':
875 fuspecs['fast1'] = [fuspecs.pop('fast1')]
876 if 'fast2' in fuspecs:
877 fuspecs['fast1'].append(fuspecs.pop('fast2'))
878 if 'fast3' in fuspecs:
879 fuspecs['fast1'].append(fuspecs.pop('fast3'))
880
881 for (regname, fspec) in sort_fuspecs(fuspecs):
882 self.connect_wrport(m, fu_bitdict, fu_selected, wrpickers,
883 regfile, regname, fspec)
884
885 def get_byregfiles(self, readmode):
886
887 mode = "read" if readmode else "write"
888 regs = self.regs
889 fus = self.fus.fus
890 e = self.ireg.e # decoded instruction to execute
891
892 # dictionary of dictionaries of lists/tuples of regfile ports.
893 # first key: regfile. second key: regfile port name
894 byregfiles = defaultdict(lambda: defaultdict(list))
895 byregfiles_spec = defaultdict(dict)
896
897 for (funame, fu) in fus.items():
898 # create in each FU a receptacle for the read/write register
899 # hazard numbers. to be latched in connect_rd/write_ports
900 # XXX better that this is moved into the actual FUs, but
901 # the issue there is that this function is actually better
902 # suited at the moment
903 if readmode:
904 fu.rd_latches = {}
905 else:
906 fu.wr_latches = {}
907
908 print("%s ports for %s" % (mode, funame))
909 for idx in range(fu.n_src if readmode else fu.n_dst):
910 # construct regfile specs: read uses inspec, write outspec
911 if readmode:
912 (regfile, regname, wid) = fu.get_in_spec(idx)
913 else:
914 (regfile, regname, wid) = fu.get_out_spec(idx)
915 print(" %d %s %s %s" % (idx, regfile, regname, str(wid)))
916
917 # the PowerDecoder2 (main one, not the satellites) contains
918 # the decoded regfile numbers. obtain these now
919 if readmode:
920 rdport, read = regspec_decode_read(e, regfile, regname)
921 wrport, write = None, None
922 else:
923 rdport, read = None, None
924 wrport, write = regspec_decode_write(e, regfile, regname)
925
926 # construct the dictionary of regspec information by regfile
927 if regname not in byregfiles_spec[regfile]:
928 byregfiles_spec[regfile][regname] = \
929 ByRegSpec(rdport, wrport, read, write, wid, [])
930 # here we start to create "lanes"
931 fuspec = FUSpec(funame, fu, idx)
932 byregfiles[regfile][idx].append(fuspec)
933 byregfiles_spec[regfile][regname].specs.append(fuspec)
934
935 continue
936 # append a latch Signal to the FU's list of latches
937 rname = "%s_%s" % (regfile, regname)
938 if readmode:
939 if rname not in fu.rd_latches:
940 rdl = Signal.like(read, name="rdlatch_"+rname)
941 fu.rd_latches[rname] = rdl
942 else:
943 if rname not in fu.wr_latches:
944 wrl = Signal.like(write, name="wrlatch_"+rname)
945 fu.wr_latches[rname] = wrl
946
947 # ok just print that all out, for convenience
948 for regfile, spec in byregfiles.items():
949 print("regfile %s ports:" % mode, regfile)
950 fuspecs = byregfiles_spec[regfile]
951 for regname, fspec in fuspecs.items():
952 [rdport, wrport, read, write, wid, fuspecs] = fspec
953 print(" rf %s port %s lane: %s" % (mode, regfile, regname))
954 print(" %s" % regname, wid, read, write, rdport, wrport)
955 for (funame, fu, idx) in fuspecs:
956 fusig = fu.src_i[idx] if readmode else fu.dest[idx]
957 print(" ", funame, fu.__class__.__name__, idx, fusig)
958 print()
959
960 return byregfiles, byregfiles_spec
961
962 def __iter__(self):
963 yield from self.fus.ports()
964 yield from self.i.e.ports()
965 yield from self.l0.ports()
966 # TODO: regs
967
968 def ports(self):
969 return list(self)
970
971
972 if __name__ == '__main__':
973 pspec = TestMemPspec(ldst_ifacetype='testpi',
974 imem_ifacetype='',
975 addr_wid=48,
976 mask_wid=8,
977 reg_wid=64)
978 dut = NonProductionCore(pspec)
979 vl = rtlil.convert(dut, ports=dut.ports())
980 with open("test_core.il", "w") as f:
981 f.write(vl)