argh - bad hack, detecting when there are no registers to write, in MultiCompUnit
[soc.git] / src / soc / experiment / compalu_multi.py
1 """Computation Unit (aka "ALU Manager").
2
3 Manages a Pipeline or FSM, ensuring that the start and end time are 100%
4 monitored. At no time may the ALU proceed without this module notifying
5 the Dependency Matrices. At no time is a result production "abandoned".
6 This module blocks (indicates busy) starting from when it first receives
7 an opcode until it receives notification that
8 its result(s) have been successfully stored in the regfile(s)
9
10 Documented at http://libre-soc.org/3d_gpu/architecture/compunit
11 """
12
13 from nmigen import Module, Signal, Mux, Elaboratable, Repl, Cat, Const
14 from nmigen.hdl.rec import (Record, DIR_FANIN, DIR_FANOUT)
15
16 from nmutil.latch import SRLatch, latchregister
17 from nmutil.iocontrol import RecordObject
18
19 from soc.fu.regspec import RegSpec, RegSpecALUAPI
20
21
22 def find_ok(fields):
23 """find_ok helper function - finds field ending in "_ok"
24 """
25 for field_name in fields:
26 if field_name.endswith("_ok"):
27 return field_name
28 return None
29
30
31 def go_record(n, name):
32 r = Record([('go', n, DIR_FANIN),
33 ('rel', n, DIR_FANOUT)], name=name)
34 r.go.reset_less = True
35 r.rel.reset_less = True
36 return r
37
38
39 # see https://libre-soc.org/3d_gpu/architecture/regfile/ section on regspecs
40
41 class CompUnitRecord(RegSpec, RecordObject):
42 """CompUnitRecord
43
44 base class for Computation Units, to provide a uniform API
45 and allow "record.connect" etc. to be used, particularly when
46 it comes to connecting multiple Computation Units up as a block
47 (very laborious)
48
49 LDSTCompUnitRecord should derive from this class and add the
50 additional signals it requires
51
52 :subkls: the class (not an instance) needed to construct the opcode
53 :rwid: either an integer (specifies width of all regs) or a "regspec"
54
55 see https://libre-soc.org/3d_gpu/architecture/regfile/ section on regspecs
56 """
57 def __init__(self, subkls, rwid, n_src=None, n_dst=None, name=None):
58 RegSpec.__init__(self, rwid, n_src, n_dst)
59 RecordObject.__init__(self, name)
60 self._subkls = subkls
61 n_src, n_dst = self._n_src, self._n_dst
62
63 # create source operands
64 src = []
65 for i in range(n_src):
66 j = i + 1 # name numbering to match src1/src2
67 name = "src%d_i" % j
68 rw = self._get_srcwid(i)
69 sreg = Signal(rw, name=name, reset_less=True)
70 setattr(self, name, sreg)
71 src.append(sreg)
72 self._src_i = src
73
74 # create dest operands
75 dst = []
76 for i in range(n_dst):
77 j = i + 1 # name numbering to match dest1/2...
78 name = "dest%d_o" % j
79 rw = self._get_dstwid(i)
80 dreg = Signal(rw, name=name, reset_less=True)
81 setattr(self, name, dreg)
82 dst.append(dreg)
83 self._dest = dst
84
85 # operation / data input
86 self.oper_i = subkls(name="oper_i") # operand
87
88 # create read/write and other scoreboard signalling
89 self.rd = go_record(n_src, name="rd") # read in, req out
90 self.wr = go_record(n_dst, name="wr") # write in, req out
91 self.rdmaskn = Signal(n_src, reset_less=True) # read mask
92 self.wrmask = Signal(n_dst, reset_less=True) # write mask
93 self.issue_i = Signal(reset_less=True) # fn issue in
94 self.shadown_i = Signal(reset=1) # shadow function, defaults to ON
95 self.go_die_i = Signal() # go die (reset)
96
97 # output (busy/done)
98 self.busy_o = Signal(reset_less=True) # fn busy out
99 self.done_o = Signal(reset_less=True)
100
101
102 class MultiCompUnit(RegSpecALUAPI, Elaboratable):
103 def __init__(self, rwid, alu, opsubsetkls, n_src=2, n_dst=1):
104 """MultiCompUnit
105
106 * :rwid: width of register latches (TODO: allocate per regspec)
107 * :alu: ALU (pipeline, FSM) - must conform to nmutil Pipe API
108 * :opsubsetkls: subset of Decode2ExecuteType
109 * :n_src: number of src operands
110 * :n_dst: number of destination operands
111 """
112 RegSpecALUAPI.__init__(self, rwid, alu)
113 self.opsubsetkls = opsubsetkls
114 self.cu = cu = CompUnitRecord(opsubsetkls, rwid, n_src, n_dst)
115 n_src, n_dst = self.n_src, self.n_dst = cu._n_src, cu._n_dst
116 print ("n_src %d n_dst %d" % (self.n_src, self.n_dst))
117
118 # convenience names for src operands
119 for i in range(n_src):
120 j = i + 1 # name numbering to match src1/src2
121 name = "src%d_i" % j
122 setattr(self, name, getattr(cu, name))
123
124 # convenience names for dest operands
125 for i in range(n_dst):
126 j = i + 1 # name numbering to match dest1/2...
127 name = "dest%d_o" % j
128 setattr(self, name, getattr(cu, name))
129
130 # more convenience names
131 self.rd = cu.rd
132 self.wr = cu.wr
133 self.rdmaskn = cu.rdmaskn
134 self.wrmask = cu.wrmask
135 self.go_rd_i = self.rd.go # temporary naming
136 self.go_wr_i = self.wr.go # temporary naming
137 self.rd_rel_o = self.rd.rel # temporary naming
138 self.req_rel_o = self.wr.rel # temporary naming
139 self.issue_i = cu.issue_i
140 self.shadown_i = cu.shadown_i
141 self.go_die_i = cu.go_die_i
142
143 # operation / data input
144 self.oper_i = cu.oper_i
145 self.src_i = cu._src_i
146
147 self.busy_o = cu.busy_o
148 self.dest = cu._dest
149 self.data_o = self.dest[0] # Dest out
150 self.done_o = cu.done_o
151
152
153 def _mux_op(self, m, sl, op_is_imm, imm, i):
154 # select imm if opcode says so. however also change the latch
155 # to trigger *from* the opcode latch instead.
156 src_or_imm = Signal(self.cu._get_srcwid(i), reset_less=True)
157 src_sel = Signal(reset_less=True)
158 m.d.comb += src_sel.eq(Mux(op_is_imm, self.opc_l.q, self.src_l.q[i]))
159 m.d.comb += src_or_imm.eq(Mux(op_is_imm, imm, self.src_i[i]))
160 # overwrite 1st src-latch with immediate-muxed stuff
161 sl[i][0] = src_or_imm
162 sl[i][2] = src_sel
163 sl[i][3] = ~op_is_imm # change rd.rel[i] gate condition
164
165 def elaborate(self, platform):
166 m = Module()
167 m.submodules.alu = self.alu
168 m.submodules.src_l = src_l = SRLatch(False, self.n_src, name="src")
169 m.submodules.opc_l = opc_l = SRLatch(sync=False, name="opc")
170 m.submodules.req_l = req_l = SRLatch(False, self.n_dst, name="req")
171 m.submodules.rst_l = rst_l = SRLatch(sync=False, name="rst")
172 m.submodules.rok_l = rok_l = SRLatch(sync=False, name="rdok")
173 self.opc_l, self.src_l = opc_l, src_l
174
175 # ALU only proceeds when all src are ready. rd_rel_o is delayed
176 # so combine it with go_rd_i. if all bits are set we're good
177 all_rd = Signal(reset_less=True)
178 m.d.comb += all_rd.eq(self.busy_o & rok_l.q &
179 (((~self.rd.rel) | self.rd.go).all()))
180
181 # generate read-done pulse
182 all_rd_dly = Signal(reset_less=True)
183 all_rd_pulse = Signal(reset_less=True)
184 m.d.sync += all_rd_dly.eq(all_rd)
185 m.d.comb += all_rd_pulse.eq(all_rd & ~all_rd_dly)
186
187 # create rising pulse from alu valid condition.
188 alu_done = Signal(reset_less=True)
189 alu_done_dly = Signal(reset_less=True)
190 alu_pulse = Signal(reset_less=True)
191 alu_pulsem = Signal(self.n_dst, reset_less=True)
192 m.d.comb += alu_done.eq(self.alu.n.valid_o)
193 m.d.sync += alu_done_dly.eq(alu_done)
194 m.d.comb += alu_pulse.eq(alu_done & ~alu_done_dly)
195 m.d.comb += alu_pulsem.eq(Repl(alu_pulse, self.n_dst))
196
197 # write_requests all done
198 # req_done works because any one of the last of the writes
199 # is enough, when combined with when read-phase is done (rst_l.q)
200 wr_any = Signal(reset_less=True)
201 req_done = Signal(reset_less=True)
202 m.d.comb += self.done_o.eq(self.busy_o & \
203 ~((self.wr.rel & ~self.wrmask).bool()))
204 m.d.comb += wr_any.eq(self.wr.go.bool())
205 m.d.comb += req_done.eq(wr_any & ~self.alu.n.ready_i & \
206 ((req_l.q & self.wrmask) == 0))
207 # argh, complicated hack: if there are no regs to write,
208 # instead of waiting for regs that are never going to happen,
209 # we indicate "done" when the ALU is "done"
210 with m.If((self.wrmask == 0) & \
211 self.alu.n.ready_i & self.alu.n.valid_o & self.busy_o):
212 m.d.comb += req_done.eq(1)
213
214 # shadow/go_die
215 reset = Signal(reset_less=True)
216 rst_r = Signal(reset_less=True) # reset latch off
217 reset_w = Signal(self.n_dst, reset_less=True)
218 reset_r = Signal(self.n_src, reset_less=True)
219 m.d.comb += reset.eq(req_done | self.go_die_i)
220 m.d.comb += rst_r.eq(self.issue_i | self.go_die_i)
221 m.d.comb += reset_w.eq(self.wr.go | Repl(self.go_die_i, self.n_dst))
222 m.d.comb += reset_r.eq(self.rd.go | Repl(self.go_die_i, self.n_src))
223
224 # read-done,wr-proceed latch
225 m.d.comb += rok_l.s.eq(self.issue_i) # set up when issue starts
226 m.d.comb += rok_l.r.eq(self.alu.n.valid_o & self.busy_o) # ALU done
227
228 # wr-done, back-to-start latch
229 m.d.comb += rst_l.s.eq(all_rd) # set when read-phase is fully done
230 m.d.comb += rst_l.r.eq(rst_r) # *off* on issue
231
232 # opcode latch (not using go_rd_i) - inverted so that busy resets to 0
233 m.d.sync += opc_l.s.eq(self.issue_i) # set on issue
234 m.d.sync += opc_l.r.eq(req_done) # reset on ALU
235
236 # src operand latch (not using go_wr_i)
237 m.d.sync += src_l.s.eq(Repl(self.issue_i, self.n_src))
238 m.d.sync += src_l.r.eq(reset_r)
239
240 # dest operand latch (not using issue_i)
241 m.d.comb += req_l.s.eq(alu_pulsem)
242 m.d.comb += req_l.r.eq(reset_w)
243
244 # create a latch/register for the operand
245 oper_r = self.opsubsetkls(name="oper_r")
246 latchregister(m, self.oper_i, oper_r, self.issue_i, "oper_l")
247
248 # and for each output from the ALU: capture when ALU output is valid
249 drl = []
250 wrok = []
251 for i in range(self.n_dst):
252 name = "data_r%d" % i
253 lro = self.get_out(i)
254 ok = Const(1, 1)
255 if isinstance(lro, Record):
256 data_r = Record.like(lro, name=name)
257 print ("wr fields", i, lro, data_r.fields)
258 # bye-bye abstract interface design..
259 fname = find_ok(data_r.fields)
260 if fname:
261 ok = data_r[fname]
262 else:
263 data_r = Signal.like(lro, name=name, reset_less=True)
264 wrok.append(ok)
265 latchregister(m, lro, data_r, alu_pulsem, name + "_l")
266 drl.append(data_r)
267
268 # ok, above we collated anything with an "ok" on the output side
269 # now actually use those to create a write-mask. this basically
270 # is now the Function Unit API tells the Comp Unit "do not request
271 # a regfile port because this particular output is not valid"
272 m.d.comb += self.wrmask.eq(Cat(*wrok))
273
274 # pass the operation to the ALU
275 m.d.comb += self.get_op().eq(oper_r)
276
277 # create list of src/alu-src/src-latch. override 1st and 2nd one below.
278 # in the case, for ALU and Logical pipelines, we assume RB is the
279 # 2nd operand in the input "regspec". see for example
280 # soc.fu.alu.pipe_data.ALUInputData
281 sl = []
282 print ("src_i", self.src_i)
283 for i in range(self.n_src):
284 sl.append([self.src_i[i], self.get_in(i), src_l.q[i], Const(1,1)])
285
286 # if the operand subset has "zero_a" we implicitly assume that means
287 # src_i[0] is an INT reg type where zero can be multiplexed in, instead.
288 # see https://bugs.libre-soc.org/show_bug.cgi?id=336
289 if hasattr(oper_r, "zero_a"):
290 # select zero imm if opcode says so. however also change the latch
291 # to trigger *from* the opcode latch instead.
292 self._mux_op(m, sl, oper_r.zero_a, 0, 0)
293
294 # if the operand subset has "imm_data" we implicitly assume that means
295 # "this is an INT ALU/Logical FU jobbie, RB is muxed with the immediate"
296 if hasattr(oper_r, "imm_data"):
297 # select immediate if opcode says so. however also change the latch
298 # to trigger *from* the opcode latch instead.
299 op_is_imm = oper_r.imm_data.imm_ok
300 imm = oper_r.imm_data.imm
301 self._mux_op(m, sl, op_is_imm, imm, 1)
302
303 # create a latch/register for src1/src2 (even if it is a copy of imm)
304 for i in range(self.n_src):
305 src, alusrc, latch, _ = sl[i]
306 latchregister(m, src, alusrc, latch, name="src_r%d" % i)
307
308 # -----
309 # ALU connection / interaction
310 # -----
311
312 # on a go_read, tell the ALU we're accepting data.
313 m.submodules.alui_l = alui_l = SRLatch(False, name="alui")
314 m.d.comb += self.alu.p.valid_i.eq(alui_l.q)
315 m.d.sync += alui_l.r.eq(self.alu.p.ready_o & alui_l.q)
316 m.d.comb += alui_l.s.eq(all_rd_pulse)
317
318 # ALU output "ready" side. alu "ready" indication stays hi until
319 # ALU says "valid".
320 m.submodules.alu_l = alu_l = SRLatch(False, name="alu")
321 m.d.comb += self.alu.n.ready_i.eq(alu_l.q)
322 m.d.sync += alu_l.r.eq(self.alu.n.valid_o & alu_l.q)
323 m.d.comb += alu_l.s.eq(all_rd_pulse)
324
325 # -----
326 # outputs
327 # -----
328
329 slg = Cat(*map(lambda x: x[3], sl)) # get req gate conditions
330 # all request signals gated by busy_o. prevents picker problems
331 m.d.comb += self.busy_o.eq(opc_l.q) # busy out
332
333 # read-release gated by busy (and read-mask)
334 bro = Repl(self.busy_o, self.n_src)
335 m.d.comb += self.rd.rel.eq(src_l.q & bro & slg & ~self.rdmaskn)
336
337 # write-release gated by busy and by shadow (and write-mask)
338 brd = Repl(self.busy_o & self.shadown_i, self.n_dst)
339 m.d.comb += self.wr.rel.eq(req_l.q & brd & self.wrmask)
340
341 # output the data from the latch on go_write
342 for i in range(self.n_dst):
343 with m.If(self.wr.go[i]):
344 m.d.comb += self.dest[i].eq(drl[i])
345
346 return m
347
348 def __iter__(self):
349 yield self.rd.go
350 yield self.wr.go
351 yield self.issue_i
352 yield self.shadown_i
353 yield self.go_die_i
354 yield from self.oper_i.ports()
355 yield self.src1_i
356 yield self.src2_i
357 yield self.busy_o
358 yield self.rd.rel
359 yield self.wr.rel
360 yield self.data_o
361
362 def ports(self):
363 return list(self)
364
365