debugging score6600 matrix
[soc.git] / src / experiment / score6600.py
1 from nmigen.compat.sim import run_simulation
2 from nmigen.cli import verilog, rtlil
3 from nmigen import Module, Const, Signal, Array, Cat, Elaboratable
4
5 from regfile.regfile import RegFileArray, treereduce
6 from scoreboard.fn_unit import IntFnUnit, FPFnUnit, LDFnUnit, STFnUnit
7 from scoreboard.fu_fu_matrix import FUFUDepMatrix
8 from scoreboard.fu_reg_matrix import FURegDepMatrix
9 from scoreboard.global_pending import GlobalPending
10 from scoreboard.group_picker import GroupPicker
11 from scoreboard.issue_unit import IntFPIssueUnit, RegDecode
12
13 from compalu import ComputationUnitNoDelay
14
15 from alu_hier import ALU
16 from nmutil.latch import SRLatch
17
18 from random import randint
19
20
21 class Scoreboard(Elaboratable):
22 def __init__(self, rwid, n_regs):
23 """ Inputs:
24
25 * :rwid: bit width of register file(s) - both FP and INT
26 * :n_regs: depth of register file(s) - number of FP and INT regs
27 """
28 self.rwid = rwid
29 self.n_regs = n_regs
30
31 # Register Files
32 self.intregs = RegFileArray(rwid, n_regs)
33 self.fpregs = RegFileArray(rwid, n_regs)
34
35 # inputs
36 self.int_store_i = Signal(reset_less=True) # instruction is a store
37 self.int_dest_i = Signal(max=n_regs, reset_less=True) # Dest R# in
38 self.int_src1_i = Signal(max=n_regs, reset_less=True) # oper1 R# in
39 self.int_src2_i = Signal(max=n_regs, reset_less=True) # oper2 R# in
40
41 self.issue_o = Signal(reset_less=True) # instruction was accepted
42
43 def elaborate(self, platform):
44 m = Module()
45
46 m.submodules.intregs = self.intregs
47 m.submodules.fpregs = self.fpregs
48
49 # register ports
50 int_dest = self.intregs.write_port("dest")
51 int_src1 = self.intregs.read_port("src1")
52 int_src2 = self.intregs.read_port("src2")
53
54 fp_dest = self.fpregs.write_port("dest")
55 fp_src1 = self.fpregs.read_port("src1")
56 fp_src2 = self.fpregs.read_port("src2")
57
58 # Int ALUs
59 add = ALU(self.rwid)
60 sub = ALU(self.rwid)
61 m.submodules.comp1 = comp1 = ComputationUnitNoDelay(self.rwid, 1, add)
62 m.submodules.comp2 = comp2 = ComputationUnitNoDelay(self.rwid, 1, sub)
63 int_alus = [comp1, comp2]
64
65 m.d.comb += comp1.oper_i.eq(Const(0)) # temporary/experiment: op=add
66 m.d.comb += comp2.oper_i.eq(Const(1)) # temporary/experiment: op=sub
67
68 # Int FUs
69 if_l = []
70 int_src1_pend_v = []
71 int_src2_pend_v = []
72 int_rd_pend_v = []
73 int_wr_pend_v = []
74 for i, a in enumerate(int_alus):
75 # set up Integer Function Unit, add to module (and python list)
76 fu = IntFnUnit(self.n_regs, shadow_wid=0)
77 setattr(m.submodules, "intfu%d" % i, fu)
78 if_l.append(fu)
79 # collate the read/write pending vectors (to go into global pending)
80 int_src1_pend_v.append(fu.src1_pend_o)
81 int_src2_pend_v.append(fu.src2_pend_o)
82 int_rd_pend_v.append(fu.int_rd_pend_o)
83 int_wr_pend_v.append(fu.int_wr_pend_o)
84 int_fus = Array(if_l)
85
86 # Count of number of FUs
87 n_int_fus = len(if_l)
88 n_fp_fus = 0 # for now
89
90 n_fus = n_int_fus + n_fp_fus # plus FP FUs
91
92 # Integer FU-FU Dep Matrix
93 intfudeps = FUFUDepMatrix(n_int_fus, n_int_fus)
94 m.submodules.intfudeps = intfudeps
95 # Integer FU-Reg Dep Matrix
96 intregdeps = FURegDepMatrix(n_int_fus, self.n_regs)
97 m.submodules.intregdeps = intregdeps
98
99 # Integer Priority Picker 1: Adder + Subtractor
100 intpick1 = GroupPicker(2) # picks between add and sub
101 m.submodules.intpick1 = intpick1
102
103 # Global Pending Vectors (INT and TODO FP)
104 # NOTE: number of vectors is NOT same as number of FUs.
105 g_int_src1_pend_v = GlobalPending(self.n_regs, int_src1_pend_v)
106 g_int_src2_pend_v = GlobalPending(self.n_regs, int_src2_pend_v)
107 g_int_rd_pend_v = GlobalPending(self.n_regs, int_rd_pend_v)
108 g_int_wr_pend_v = GlobalPending(self.n_regs, int_wr_pend_v)
109 m.submodules.g_int_src1_pend_v = g_int_src1_pend_v
110 m.submodules.g_int_src2_pend_v = g_int_src2_pend_v
111 m.submodules.g_int_rd_pend_v = g_int_rd_pend_v
112 m.submodules.g_int_wr_pend_v = g_int_wr_pend_v
113
114 # INT/FP Issue Unit
115 regdecode = RegDecode(self.n_regs)
116 m.submodules.regdecode = regdecode
117 issueunit = IntFPIssueUnit(self.n_regs, n_int_fus, n_fp_fus)
118 m.submodules.issueunit = issueunit
119
120 #---------
121 # ok start wiring things together...
122 # "now hear de word of de looord... dem bones dem bones dem dryy bones"
123 # https://www.youtube.com/watch?v=pYb8Wm6-QfA
124 #---------
125
126 #---------
127 # Issue Unit is where it starts. set up some in/outs for this module
128 #---------
129 m.d.comb += [issueunit.i.store_i.eq(self.int_store_i),
130 regdecode.dest_i.eq(self.int_dest_i),
131 regdecode.src1_i.eq(self.int_src1_i),
132 regdecode.src2_i.eq(self.int_src2_i),
133 regdecode.enable_i.eq(1),
134 issueunit.i.dest_i.eq(regdecode.dest_o),
135 self.issue_o.eq(issueunit.issue_o)
136 ]
137 self.int_insn_i = issueunit.i.insn_i # enabled by instruction decode
138
139 # connect global rd/wr pending vectors
140 m.d.comb += issueunit.i.g_wr_pend_i.eq(g_int_wr_pend_v.g_pend_o)
141 # TODO: issueunit.f (FP)
142
143 # and int function issue / busy arrays, and dest/src1/src2
144 fn_busy_l = []
145 fn_issue_l = []
146 for i, fu in enumerate(if_l):
147 fn_issue_l.append(fu.issue_i)
148 fn_busy_l.append(fu.busy_o)
149 m.d.comb += fu.issue_i.eq(issueunit.i.fn_issue_o[i])
150 m.d.comb += fu.dest_i.eq(self.int_dest_i)
151 m.d.comb += fu.src1_i.eq(self.int_src1_i)
152 m.d.comb += fu.src2_i.eq(self.int_src2_i)
153 # XXX sync, so as to stop a simulation infinite loop
154 m.d.sync += issueunit.i.busy_i[i].eq(fu.busy_o)
155
156 fn_issue_o = Signal(len(fn_issue_l), reset_less=True)
157 m.d.comb += fn_issue_o.eq(Cat(*fn_issue_l))
158 #fn_issue_o = issueunit.i.fn_issue_o
159 #---------
160 # connect fu-fu matrix
161 #---------
162
163 m.d.comb += intfudeps.rd_pend_i.eq(g_int_rd_pend_v.g_pend_o)
164 m.d.comb += intfudeps.wr_pend_i.eq(g_int_wr_pend_v.g_pend_o)
165
166 # Group Picker... done manually for now. TODO: cat array of pick sigs
167 go_rd_o = intpick1.go_rd_o
168 go_wr_o = intpick1.go_wr_o
169 go_rd_i = intfudeps.go_rd_i
170 go_wr_i = intfudeps.go_wr_i
171 m.d.comb += go_rd_i[0].eq(go_rd_o[0]) # add rd
172 m.d.comb += go_wr_i[0].eq(go_wr_o[0]) # add wr
173
174 m.d.comb += go_rd_i[1].eq(go_rd_o[1]) # sub rd
175 m.d.comb += go_wr_i[1].eq(go_wr_o[1]) # sub wr
176
177 m.d.comb += intfudeps.issue_i.eq(fn_issue_o)
178
179 # Connect INT FU go_rd/wr
180 for i, fu in enumerate(if_l):
181 m.d.comb += fu.go_rd_i.eq(go_rd_o[i])
182 m.d.comb += fu.go_wr_i.eq(go_wr_o[i])
183
184 # Connect INT Fn Unit global wr/rd pending
185 for fu in if_l:
186 m.d.comb += fu.g_int_wr_pend_i.eq(g_int_wr_pend_v.g_pend_o)
187 m.d.comb += fu.g_int_rd_pend_i.eq(g_int_rd_pend_v.g_pend_o)
188
189 #---------
190 # connect fu-dep matrix
191 #---------
192 r_go_rd_i = intregdeps.go_rd_i
193 r_go_wr_i = intregdeps.go_wr_i
194 m.d.comb += r_go_rd_i.eq(go_rd_o)
195 m.d.comb += r_go_wr_i.eq(go_wr_o)
196
197 m.d.comb += intregdeps.dest_i.eq(regdecode.dest_o)
198 m.d.comb += intregdeps.src1_i.eq(regdecode.src1_o)
199 m.d.comb += intregdeps.src2_i.eq(regdecode.src2_o)
200 m.d.comb += intregdeps.issue_i.eq(fn_issue_o)
201
202 # Connect Picker
203 #---------
204 m.d.sync += intpick1.req_rel_i[0].eq(int_alus[0].req_rel_o)
205 m.d.sync += intpick1.req_rel_i[1].eq(int_alus[1].req_rel_o)
206 int_readable_o = intfudeps.readable_o
207 int_writable_o = intfudeps.writable_o
208 m.d.comb += intpick1.readable_i[0].eq(int_readable_o[0]) # add rd
209 m.d.comb += intpick1.writable_i[0].eq(int_writable_o[0]) # add wr
210 m.d.comb += intpick1.readable_i[1].eq(int_readable_o[1]) # sub rd
211 m.d.comb += intpick1.writable_i[1].eq(int_writable_o[1]) # sub wr
212
213 #---------
214 # Connect Register File(s)
215 #---------
216 m.d.comb += int_dest.wen.eq(intregdeps.dest_rsel_o)
217 m.d.comb += int_src1.ren.eq(intregdeps.src1_rsel_o)
218 m.d.comb += int_src2.ren.eq(intregdeps.src2_rsel_o)
219
220 # merge (OR) all integer FU / ALU outputs to a single value
221 # bit of a hack: treereduce needs a list with an item named "dest_o"
222 dest_o = treereduce(int_alus)
223 m.d.comb += int_dest.data_i.eq(dest_o)
224
225 # connect ALUs
226 for i, alu in enumerate(int_alus):
227 m.d.comb += alu.go_rd_i.eq(go_rd_o[i])
228 m.d.comb += alu.go_wr_i.eq(go_wr_o[i])
229 m.d.comb += alu.issue_i.eq(fn_issue_l[i])
230 m.d.comb += alu.src1_i.eq(int_src1.data_o)
231 m.d.comb += alu.src2_i.eq(int_src2.data_o)
232 m.d.sync += if_l[i].req_rel_i.eq(alu.req_rel_o) # pipe out ready
233
234 return m
235
236
237 def __iter__(self):
238 yield from self.intregs
239 yield from self.fpregs
240 yield self.int_store_i
241 yield self.int_dest_i
242 yield self.int_src1_i
243 yield self.int_src2_i
244 yield self.issue_o
245 #yield from self.int_src1
246 #yield from self.int_dest
247 #yield from self.int_src1
248 #yield from self.int_src2
249 #yield from self.fp_dest
250 #yield from self.fp_src1
251 #yield from self.fp_src2
252
253 def ports(self):
254 return list(self)
255
256 IADD = 0
257 ISUB = 1
258
259 class RegSim:
260 def __init__(self, rwidth, nregs):
261 self.rwidth = rwidth
262 self.regs = [0] * nregs
263
264 def op(self, op, src1, src2, dest):
265 src1 = self.regs[src1]
266 src2 = self.regs[src2]
267 if op == IADD:
268 val = (src1 + src2) & ((1<<(self.rwidth))-1)
269 elif op == ISUB:
270 val = (src1 - src2) & ((1<<(self.rwidth))-1)
271 self.regs[dest] = val
272
273 def setval(self, dest, val):
274 self.regs[dest] = val
275
276 def dump(self, dut):
277 for i, val in enumerate(self.regs):
278 reg = yield dut.intregs.regs[i].reg
279 okstr = "OK" if reg == val else "!ok"
280 print("reg %d expected %x received %x %s" % (i, val, reg, okstr))
281
282 def check(self, dut):
283 for i, val in enumerate(self.regs):
284 reg = yield dut.intregs.regs[i].reg
285 if reg != val:
286 print("reg %d expected %x received %x\n" % (i, val, reg))
287 yield from self.dump(dut)
288 assert False
289
290 def int_instr(dut, alusim, op, src1, src2, dest):
291 for i in range(len(dut.int_insn_i)):
292 yield dut.int_insn_i[i].eq(0)
293 yield dut.int_dest_i.eq(dest)
294 yield dut.int_src1_i.eq(src1)
295 yield dut.int_src2_i.eq(src2)
296 yield dut.int_insn_i[op].eq(1)
297 alusim.op(op, src1, src2, dest)
298
299
300 def print_reg(dut, rnums):
301 rs = []
302 for rnum in rnums:
303 reg = yield dut.intregs.regs[rnum].reg
304 rs.append("%x" % reg)
305 rnums = map(str, rnums)
306 print ("reg %s: %s" % (','.join(rnums), ','.join(rs)))
307
308
309 def scoreboard_sim(dut, alusim):
310 yield dut.int_store_i.eq(0)
311
312 for i in range(1, dut.n_regs):
313 yield dut.intregs.regs[i].reg.eq(i)
314 alusim.setval(i, i)
315
316 yield
317 yield
318
319 if False:
320 yield from int_instr(dut, alusim, IADD, 4, 3, 5)
321 yield from print_reg(dut, [3,4,5])
322 yield
323 yield from int_instr(dut, alusim, IADD, 5, 2, 5)
324 yield from print_reg(dut, [3,4,5])
325 yield
326 yield from int_instr(dut, alusim, ISUB, 5, 1, 3)
327 yield from print_reg(dut, [3,4,5])
328 yield
329 for i in range(len(dut.int_insn_i)):
330 yield dut.int_insn_i[i].eq(0)
331 yield from print_reg(dut, [3,4,5])
332 yield
333 yield from print_reg(dut, [3,4,5])
334 yield
335 yield from print_reg(dut, [3,4,5])
336 yield
337
338 yield from alusim.check(dut)
339
340 for i in range(1):
341 src1 = randint(1, dut.n_regs-1)
342 src2 = randint(1, dut.n_regs-1)
343 while True:
344 dest = randint(1, dut.n_regs-1)
345 break
346 if dest not in [src1, src2]:
347 break
348 src1 = 3
349 src2 = 1
350 dest = 1
351
352 op = randint(0, 1)
353 op = 0
354 print ("random %d: %d %d %d %d\n" % (i, op, src1, src2, dest))
355 yield from int_instr(dut, alusim, op, src1, src2, dest)
356 yield from print_reg(dut, [3,4,5])
357 yield
358 yield from print_reg(dut, [3,4,5])
359 for i in range(len(dut.int_insn_i)):
360 yield dut.int_insn_i[i].eq(0)
361 yield
362 yield
363 yield
364
365
366 yield
367 yield from print_reg(dut, [3,4,5])
368 yield
369 yield from print_reg(dut, [3,4,5])
370 yield
371 yield
372 yield
373 yield
374 yield from alusim.check(dut)
375
376
377 def explore_groups(dut):
378 from nmigen.hdl.ir import Fragment
379 from nmigen.hdl.xfrm import LHSGroupAnalyzer
380
381 fragment = dut.elaborate(platform=None)
382 fr = Fragment.get(fragment, platform=None)
383
384 groups = LHSGroupAnalyzer()(fragment._statements)
385
386 print (groups)
387
388
389 def test_scoreboard():
390 dut = Scoreboard(32, 8)
391 alusim = RegSim(32, 8)
392 vl = rtlil.convert(dut, ports=dut.ports())
393 with open("test_scoreboard6600.il", "w") as f:
394 f.write(vl)
395
396 run_simulation(dut, scoreboard_sim(dut, alusim),
397 vcd_name='test_scoreboard6600.vcd')
398
399
400 if __name__ == '__main__':
401 test_scoreboard()