test/axi: add AXILite2CSR and AXILiteSRAM tests
authorJędrzej Boczar <jboczar@antmicro.com>
Wed, 15 Jul 2020 10:30:28 +0000 (12:30 +0200)
committerJędrzej Boczar <jboczar@antmicro.com>
Wed, 15 Jul 2020 10:40:39 +0000 (12:40 +0200)
litex/soc/interconnect/axi.py
test/test_axi.py

index 1fc7af8aab26b86b946bb9b7e534e48eb759fe5a..d0886c1fb6734762b37ce097809a4f6dd9227daf 100644 (file)
@@ -138,6 +138,42 @@ class AXILiteInterface:
                     r.append(pad.eq(sig))
         return r
 
+    def write(self, addr, data, strb=None):
+        if strb is None:
+            strb = 2**len(self.w.strb) - 1
+        yield self.aw.valid.eq(1)
+        yield self.aw.addr.eq(addr)
+        yield self.w.data.eq(data)
+        yield self.w.valid.eq(1)
+        yield self.w.strb.eq(strb)
+        yield
+        while not (yield self.aw.ready):
+            yield
+        while not (yield self.w.ready):
+            yield
+        while not (yield self.b.valid):
+            yield
+        yield self.b.ready.eq(1)
+        resp = (yield self.b.resp)
+        yield
+        yield self.b.ready.eq(0)
+        return resp
+
+    def read(self, addr):
+        yield self.ar.valid.eq(1)
+        yield self.ar.addr.eq(addr)
+        yield
+        while not (yield self.ar.ready):
+            yield
+        while not (yield self.r.valid):
+            yield
+        yield self.r.ready.eq(1)
+        data = (yield self.r.data)
+        resp = (yield self.r.resp)
+        yield
+        yield self.r.ready.eq(0)
+        return (data, resp)
+
 # AXI Stream Definition ----------------------------------------------------------------------------
 
 class AXIStreamInterface(stream.Endpoint):
index 3152a4134497d2e3beba081cda5f56e609577017..fa694e412462f46345f4ff3ba442a78b7cba81dc 100644 (file)
@@ -7,7 +7,7 @@ import random
 from migen import *
 
 from litex.soc.interconnect.axi import *
-from litex.soc.interconnect import wishbone
+from litex.soc.interconnect import wishbone, csr_bus
 
 # Software Models ----------------------------------------------------------------------------------
 
@@ -358,5 +358,84 @@ class TestAXI(unittest.TestCase):
                     dut.errors += 1
 
         dut = DUT()
-        run_simulation(dut, [generator(dut)], vcd_name="toto.vcd")
+        run_simulation(dut, [generator(dut)])
+        self.assertEqual(dut.errors, 0)
+
+    def test_axilite2csr(self):
+        @passive
+        def csr_mem_handler(csr, mem):
+            while True:
+                adr = (yield csr.adr)
+                yield csr.dat_r.eq(mem[adr])
+                if (yield csr.we):
+                    mem[adr] = (yield csr.dat_w)
+                yield
+
+        class DUT(Module):
+            def __init__(self):
+                self.axi_lite = AXILiteInterface()
+                self.csr = csr_bus.Interface()
+                self.submodules.axilite2csr = AXILite2CSR(self.axi_lite, self.csr)
+                self.errors = 0
+
+        prng = random.Random(42)
+        mem_ref = [prng.randrange(255) for i in range(100)]
+
+        def generator(dut):
+            dut.errors = 0
+
+            for adr, ref in enumerate(mem_ref):
+                adr = adr << 2
+                data, resp = (yield from dut.axi_lite.read(adr))
+                self.assertEqual(resp, 0b00)
+                if data != ref:
+                    dut.errors += 1
+
+            write_data = [prng.randrange(255) for _ in mem_ref]
+
+            for adr, wdata in enumerate(write_data):
+                adr = adr << 2
+                resp = (yield from dut.axi_lite.write(adr, wdata))
+                self.assertEqual(resp, 0b00)
+                rdata, resp = (yield from dut.axi_lite.read(adr))
+                self.assertEqual(resp, 0b00)
+                if rdata != wdata:
+                    dut.errors += 1
+
+        dut = DUT()
+        mem = [v for v in mem_ref]
+        run_simulation(dut, [generator(dut), csr_mem_handler(dut.csr, mem)])
+        self.assertEqual(dut.errors, 0)
+
+    def test_axilite_sram(self):
+        class DUT(Module):
+            def __init__(self, size, init):
+                self.axi_lite = AXILiteInterface()
+                self.submodules.sram = AXILiteSRAM(size, init=init, bus=self.axi_lite)
+                self.errors = 0
+
+        def generator(dut, ref_init):
+            for adr, ref in enumerate(ref_init):
+                adr = adr << 2
+                data, resp = (yield from dut.axi_lite.read(adr))
+                self.assertEqual(resp, 0b00)
+                if data != ref:
+                    dut.errors += 1
+
+            write_data = [prng.randrange(255) for _ in ref_init]
+
+            for adr, wdata in enumerate(write_data):
+                adr = adr << 2
+                resp = (yield from dut.axi_lite.write(adr, wdata))
+                self.assertEqual(resp, 0b00)
+                rdata, resp = (yield from dut.axi_lite.read(adr))
+                self.assertEqual(resp, 0b00)
+                if rdata != wdata:
+                    dut.errors += 1
+
+        prng = random.Random(42)
+        init = [prng.randrange(2**32) for i in range(100)]
+
+        dut = DUT(size=len(init)*4, init=[v for v in init])
+        run_simulation(dut, [generator(dut, init)])
         self.assertEqual(dut.errors, 0)