whoops, regfiles are uppercase
[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 from nmigen import Elaboratable, Module, Signal
8 from nmigen.cli import rtlil
9
10 from nmutil.picker import PriorityPicker
11 from nmutil.util import treereduce
12
13 from soc.fu.compunits.compunits import AllFunctionUnits
14 from soc.regfile.regfiles import RegFiles
15 from soc.decoder.power_decoder import create_pdecode
16 from soc.decoder.power_decoder2 import PowerDecode2
17
18
19
20 def ortreereduce(tree, attr="data_o"):
21 return treereduce(tree, operator.or_, lambda x: getattr(x, attr))
22
23
24 class NonProductionCore(Elaboratable):
25 def __init__(self):
26 self.fus = AllFunctionUnits()
27 self.regs = RegFiles()
28 self.pdecode = pdecode = create_pdecode()
29 self.pdecode2 = PowerDecode2(pdecode) # instruction decoder
30 self.ivalid_i = self.pdecode2.e.valid # instruction is valid
31
32 def elaborate(self, platform):
33 m = Module()
34 comb = m.d.comb
35
36 m.submodules.pdecode2 = dec2 = self.pdecode2
37 m.submodules.fus = self.fus
38 self.regs.elaborate_into(m, platform)
39 regs = self.regs
40 fus = self.fus.fus
41
42 # enable-signals for each FU, get one bit for each FU (by name)
43 fu_enable = Signal(len(fus), reset_less=True)
44 fu_bitdict = {}
45 for i, funame in enumerate(fus.keys()):
46 fu_bitdict[funame] = fu_enable[i]
47
48 # dictionary of lists of regfile read ports
49 byregfiles_rd = {}
50 byregfiles_rdspec = {}
51 for (funame, fu) in fus.items():
52 print ("read ports for %s" % funame)
53 for idx in range(fu.n_src):
54 (regfile, regname, wid) = fu.get_in_spec(idx)
55 print (" %d %s %s %s" % (idx, regfile, regname, str(wid)))
56 rdflag, read, _ = dec2.regspecmap(regfile, regname)
57 if regfile not in byregfiles_rd:
58 byregfiles_rd[regfile] = {}
59 byregfiles_rdspec[regfile] = {}
60 if regname not in byregfiles_rdspec[regfile]:
61 byregfiles_rdspec[regfile][regname] = \
62 [rdflag, read, wid, []]
63 # here we start to create "lanes"
64 if idx not in byregfiles_rd[regfile]:
65 byregfiles_rd[regfile][idx] = []
66 fuspec = (funame, fu, idx)
67 byregfiles_rd[regfile][idx].append(fuspec)
68 byregfiles_rdspec[regfile][regname][3].append(fuspec)
69
70 # ok just print that out, for convenience
71 for regfile, spec in byregfiles_rd.items():
72 print ("regfile read ports:", regfile)
73 fuspecs = byregfiles_rdspec[regfile]
74 for regname, fspec in fuspecs.items():
75 [rdflag, read, wid, fuspec] = fspec
76 print (" regfile read port %s lane: %s" % (regfile, regname))
77 print (" %s" % regname, wid, read, rdflag)
78 for (funame, fu, idx) in fuspec:
79 print (" ", funame, fu, idx, fu.src_i[idx])
80 print ()
81
82 # okaay, now we need a PriorityPicker per regfile per regfile port
83 # loootta pickers... peter piper picked a pack of pickled peppers...
84 rdpickers = {}
85 for regfile, spec in byregfiles_rd.items():
86 fuspecs = byregfiles_rdspec[regfile]
87 rdpickers[regfile] = {}
88 for rpidx, (regname, fspec) in enumerate(fuspecs.items()):
89 # get the regfile specs for this regfile port
90 (rdflag, read, wid, fuspec) = fspec
91
92 # "munge" the regfile port index, due to full-port access
93 if regfile in ['XER', 'CA']:
94 if regname.startswith('full'):
95 rpidx = 0 # by convention, first port
96 else:
97 rpidx += 1 # start indexing port 0 from 1
98
99 # select the required read port. these are pre-defined sizes
100 print (regfile, regs.rf.keys())
101 rport = regs.rf[regfile.lower()].r_ports[rpidx]
102
103 # create a priority picker to manage this port
104 rdpickers[regfile][rpidx] = rdpick = PriorityPicker(len(fuspec))
105 setattr(m.submodules, "rdpick_%s_%d" % (regfile, rpidx), rdpick)
106
107 # connect the regspec "reg select" number to this port
108 with m.If(rdpick.en_o):
109 comb += rport.ren.eq(read)
110
111 # connect up the FU req/go signals and the reg-read to the FU
112 for pi, (funame, fu, idx) in enumerate(fuspec):
113 # connect request-read to picker input, and output to go-rd
114 fu_active = fu_bitdict[funame]
115 comb += rdpick.i[pi].eq(fu.rd_rel_o[idx] & fu_active)
116 comb += fu.go_rd_i[idx].eq(rdpick.o[pi])
117 # connect regfile port to input
118 print ("reg connect widths",
119 regfile, regname, pi, funame,
120 fu.src_i[idx].shape(), rport.data_o.shape())
121 comb += fu.src_i[idx].eq(rport.data_o)
122
123 return m
124
125 def __iter__(self):
126 yield from self.fus.ports()
127 yield from self.pdecode2.ports()
128 # TODO: regs
129
130 def ports(self):
131 return list(self)
132
133
134 if __name__ == '__main__':
135 dut = NonProductionCore()
136 vl = rtlil.convert(dut, ports=dut.ports())
137 with open("non_production_core.il", "w") as f:
138 f.write(vl)