leave off number being subtracted from "ready_o" calculation
[soc.git] / src / scoreboard / test_iq.py
1 """ testing of InstructionQ
2 """
3
4 from copy import deepcopy
5 from random import randint
6 from nmigen.compat.sim import run_simulation
7 from nmigen.cli import verilog, rtlil
8
9 from scoreboard.instruction_q import InstructionQ
10 from nmutil.nmoperator import eq
11
12
13 class IQSim:
14 def __init__(self, dut, iq, n_in, n_out):
15 self.dut = dut
16 self.iq = iq
17 self.oq = []
18 self.n_in = n_in
19 self.n_out = n_out
20
21 def send(self):
22 i = 0
23 while i < len(self.iq):
24 sendlen = randint(1, self.n_in)
25 print (sendlen)
26 sendlen = min(len(self.iq) - i, sendlen)
27 print (len(self.iq)-i, sendlen)
28 for idx in range(sendlen):
29 instr = self.iq[i+idx]
30 yield from eq(self.dut.data_i[idx], instr)
31 yield self.dut.p_add_i.eq(sendlen)
32 o_p_ready = yield self.dut.p_ready_o
33 while not o_p_ready:
34 yield
35 o_p_ready = yield self.dut.p_ready_o
36
37 yield
38 yield self.dut.p_add_i.eq(0)
39
40 print ("send", len(self.iq), i, sendlen)
41
42 # wait random period of time before queueing another value
43 for j in range(randint(0, 3)):
44 yield
45
46 i += sendlen
47
48 yield self.dut.p_add_i.eq(0)
49 yield
50
51 print ("send ended")
52
53 ## wait random period of time before queueing another value
54 #for i in range(randint(0, 3)):
55 # yield
56
57 #send_range = randint(0, 3)
58 #if send_range == 0:
59 # send = True
60 #else:
61 # send = randint(0, send_range) != 0
62
63 def rcv(self):
64 i = 0
65 yield
66 yield
67 yield
68 while i < len(self.iq):
69 rcvlen = randint(1, self.n_out)
70 #print ("outreq", rcvlen)
71 yield self.dut.n_sub_i.eq(rcvlen)
72 n_sub_o = yield self.dut.n_sub_o
73 yield
74 if n_sub_o == 0:
75 continue
76
77 print ("recv", n_sub_o)
78 i += n_sub_o
79
80 print ("recv ended")
81
82
83 def mk_insns(n_insns, wid, opwid):
84 res = []
85 for i in range(n_insns):
86 op1 = randint(0, (1<<wid)-1)
87 op2 = randint(0, (1<<wid)-1)
88 dst = randint(0, (1<<wid)-1)
89 oper = randint(0, (1<<opwid)-1)
90 res.append({'oper_i': oper, 'dest_i': dst, 'src1_i': op1, 'src2_i': op2})
91 return res
92
93
94 def test_iq():
95 wid = 8
96 opwid = 4
97 qlen = 5
98 n_in = 3
99 n_out = 3
100 dut = InstructionQ(wid, opwid, qlen, n_in, n_out)
101 insns = mk_insns(10, wid, opwid)
102
103 vl = rtlil.convert(dut, ports=dut.ports())
104 with open("test_iq.il", "w") as f:
105 f.write(vl)
106
107 test = IQSim(dut, insns, n_in, n_out)
108 print (insns)
109 run_simulation(dut, [test.rcv(), test.send()
110 ],
111 vcd_name="test_iq.vcd")
112
113 if __name__ == '__main__':
114 test_iq()