format some tests
[soc.git] / src / soc / config / test / test_pi2ls.py
1 from nmigen import Signal, Module, Record
2 from nmigen.back.pysim import Simulator, Delay
3 from nmigen.compat.sim import run_simulation, Settle
4 from nmutil.formaltest import FHDLTestCase
5 from nmigen.cli import rtlil
6 import unittest
7 from soc.config.test.test_loadstore import TestMemPspec
8 from soc.config.loadstore import ConfigMemoryPortInterface
9
10
11 def wait_busy(port, no=False):
12 while True:
13 busy = yield port.busy_o
14 print("busy", no, busy)
15 if bool(busy) == no:
16 break
17 yield
18
19
20 def wait_addr(port):
21 while True:
22 addr_ok = yield port.addr_ok_o
23 print("addrok", addr_ok)
24 if addr_ok:
25 break
26 yield
27
28
29 def wait_ldok(port):
30 while True:
31 ldok = yield port.ld.ok
32 print("ldok", ldok)
33 if ldok:
34 break
35 yield
36
37
38 def pi_st(port1, addr, data, datalen):
39
40 # have to wait until not busy
41 yield from wait_busy(port1, no=False) # wait until not busy
42
43 # set up a ST on the port. address first:
44 yield port1.is_st_i.eq(1) # indicate ST
45 yield port1.data_len.eq(datalen) # ST length (1/2/4/8)
46
47 yield port1.addr.data.eq(addr) # set address
48 yield port1.addr.ok.eq(1) # set ok
49 yield Settle()
50 yield from wait_addr(port1) # wait until addr ok
51 # yield # not needed, just for checking
52 # yield # not needed, just for checking
53 # assert "ST" for one cycle (required by the API)
54 yield port1.st.data.eq(data)
55 yield port1.st.ok.eq(1)
56 yield
57 yield port1.st.ok.eq(0)
58 yield from wait_busy(port1, True) # wait while busy
59
60 # can go straight to reset.
61 yield port1.is_st_i.eq(0) # end
62 yield port1.addr.ok.eq(0) # set !ok
63
64
65 def pi_ld(port1, addr, datalen):
66
67 # have to wait until not busy
68 yield from wait_busy(port1, no=False) # wait until not busy
69
70 # set up a LD on the port. address first:
71 yield port1.is_ld_i.eq(1) # indicate LD
72 yield port1.data_len.eq(datalen) # LD length (1/2/4/8)
73
74 yield port1.addr.data.eq(addr) # set address
75 yield port1.addr.ok.eq(1) # set ok
76 yield Settle()
77 yield from wait_addr(port1) # wait until addr ok
78 yield
79 yield from wait_ldok(port1) # wait until ld ok
80 data = yield port1.ld.data
81
82 # cleanup
83 yield port1.is_ld_i.eq(0) # end
84 yield port1.addr.ok.eq(0) # set !ok
85 yield from wait_busy(port1, no=False) # wait while not busy
86
87 return data
88
89
90 def pi_ldst(arg, dut):
91
92 # do two half-word stores at consecutive addresses, then two loads
93 addr1 = 0x04
94 addr2 = addr1 + 0x2
95 data = 0xbeef
96 data2 = 0xf00f
97 #data = 0x4
98 yield from pi_st(dut, addr1, data, 2)
99 yield from pi_st(dut, addr2, data2, 2)
100 result = yield from pi_ld(dut, addr1, 2)
101 result2 = yield from pi_ld(dut, addr2, 2)
102 arg.assertEqual(data, result, "data %x != %x" % (result, data))
103 arg.assertEqual(data2, result2, "data2 %x != %x" % (result2, data2))
104
105 # now load both in a 32-bit load to make sure they're really consecutive
106 data3 = data | (data2 << 16)
107 result3 = yield from pi_ld(dut, addr1, 4)
108 arg.assertEqual(data3, result3, "data3 %x != %x" % (result3, data3))
109
110
111 def tst_config_pi(testcls, ifacetype):
112 """set up a configureable memory test of type ifacetype
113 """
114 dut = Module()
115 pspec = TestMemPspec(ldst_ifacetype=ifacetype,
116 imem_ifacetype='',
117 addr_wid=48,
118 mask_wid=8,
119 reg_wid=64)
120 cmpi = ConfigMemoryPortInterface(pspec)
121 dut.submodules.pi = cmpi.pi
122 if hasattr(cmpi, 'lsmem'): # hmmm not happy about this
123 dut.submodules.lsmem = cmpi.lsmem.lsi
124 vl = rtlil.convert(dut, ports=[]) # dut.ports())
125 with open("test_pi_%s.il" % ifacetype, "w") as f:
126 f.write(vl)
127
128 run_simulation(dut, {"sync": pi_ldst(testcls, cmpi.pi.pi)},
129 vcd_name='test_pi_%s.vcd' % ifacetype)
130
131
132 class TestPIMem(unittest.TestCase):
133
134 def test_pi_mem(self):
135 tst_config_pi(self, 'testpi')
136
137 def test_pi2ls(self):
138 tst_config_pi(self, 'testmem')
139
140 def test_pi2ls_bare_wb(self):
141 tst_config_pi(self, 'test_bare_wb')
142
143
144 if __name__ == '__main__':
145 unittest.main(exit=False)