test: create generic PacketStreamer/PacketLogger and use it in link_tb/command_tb
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Thu, 18 Dec 2014 12:15:39 +0000 (13:15 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Thu, 18 Dec 2014 12:15:39 +0000 (13:15 +0100)
lib/sata/test/command_tb.py
lib/sata/test/common.py
lib/sata/test/link_tb.py

index b265e8281c6ff956e9fd3c6a8b8200fca8d84151..cb0c2137769f4188a42aabbb07b42aa0b0638be7 100644 (file)
@@ -1,4 +1,4 @@
-import random, copy
+import random
 
 from migen.fhdl.std import *
 from migen.genlib.record import *
@@ -24,48 +24,18 @@ class CommandTXPacket(list):
                for d in data:
                        self.append(d)
 
-class CommandStreamer(Module):
+class CommandStreamer(PacketStreamer):
        def __init__(self):
-               self.source = Source(command_tx_description(32))
-               ###
-               self.packets = []
-               self.packet = CommandTXPacket()
-               self.packet.done = 1
-               self.length = 0
-
-       def send(self, packet, blocking=True):
-               packet = copy.deepcopy(packet)
-               self.packets.append(packet)
-               if blocking:
-                       while packet.done == 0:
-                               yield
+               PacketStreamer.__init__(self, command_tx_description(32), CommandTXPacket)
 
        def do_simulation(self, selfp):
-               if len(self.packets) and self.packet.done:
-                       self.packet = self.packets.pop(0)
-
+               PacketStreamer.do_simulation(self, selfp)
                selfp.source.write = self.packet.write
                selfp.source.read = self.packet.read
                selfp.source.identify = self.packet.identify
                selfp.source.sector = self.packet.sector
                selfp.source.count = self.packet.count
 
-               if not self.packet.ongoing and not self.packet.done:
-                       selfp.source.stb = 1
-                       selfp.source.sop = 1
-                       if len(self.packet) > 0:
-                               selfp.source.data = self.packet.pop(0)
-                       self.packet.ongoing = True
-               elif selfp.source.stb == 1 and selfp.source.ack == 1:
-                       selfp.source.sop = 0
-                       selfp.source.eop = (len(self.packet) == 1)
-                       if len(self.packet) > 0:
-                               selfp.source.stb = 1
-                               selfp.source.data = self.packet.pop(0)
-                       else:
-                               self.packet.done = 1
-                               selfp.source.stb = 0
-
 class CommandRXPacket(list):
        def __init__(self):
                self.ongoing = False
@@ -76,16 +46,9 @@ class CommandRXPacket(list):
                self.success = 0
                self.failed = 0
 
-class CommandLogger(Module):
+class CommandLogger(PacketLogger):
        def __init__(self):
-               self.sink = Sink(command_rx_description(32))
-               ###
-               self.packet = CommandRXPacket()
-
-       def receive(self):
-               self.packet.done = 0
-               while self.packet.done == 0:
-                       yield
+               PacketLogger.__init__(self, command_rx_description(32), CommandRXPacket)
 
        def do_simulation(self, selfp):
                selfp.sink.ack = 1
@@ -99,7 +62,7 @@ class CommandLogger(Module):
                        self.packet.append(selfp.sink.data)
                elif selfp.sink.stb:
                        self.packet.append(selfp.sink.data)
-               if (selfp.sink.stb == 1 and selfp.sink.eop == 1):
+               if selfp.sink.stb == 1 and selfp.sink.eop == 1:
                        self.packet.done = True
 
 class TB(Module):
index 243898068985ed2b99136571f783156ef64e7aa8..c908debf80fbaf3be410381872dc759b4ac7a2bf 100644 (file)
@@ -32,6 +32,70 @@ def check(p1, p2):
 def randn(max_n):
        return random.randint(0, max_n-1)
 
+class PacketStreamer(Module):
+       def __init__(self, description, packet_class):
+               self.source = Source(description)
+               ###
+               self.packets = []
+               self.packet = packet_class()
+               self.packet.done = 1
+
+       def send(self, packet, blocking=True):
+               packet = copy.deepcopy(packet)
+               self.packets.append(packet)
+               if blocking:
+                       while packet.done == 0:
+                               yield
+
+       def do_simulation(self, selfp):
+               if len(self.packets) and self.packet.done:
+                       self.packet = self.packets.pop(0)
+               if not self.packet.ongoing and not self.packet.done:
+                       selfp.source.stb = 1
+                       selfp.source.sop = 1
+                       if len(self.packet) > 0:
+                               if hasattr(selfp.source, "data"):
+                                       selfp.source.data = self.packet.pop(0)
+                               else:
+                                       selfp.source.d = self.packet.pop(0)
+                       self.packet.ongoing = True
+               elif selfp.source.stb == 1 and selfp.source.ack == 1:
+                       selfp.source.sop = 0
+                       selfp.source.eop = (len(self.packet) == 1)
+                       if len(self.packet) > 0:
+                               selfp.source.stb = 1
+                               if hasattr(selfp.source, "data"):
+                                       selfp.source.data = self.packet.pop(0)
+                               else:
+                                       selfp.source.d = self.packet.pop(0)
+                       else:
+                               self.packet.done = 1
+                               selfp.source.stb = 0
+
+class PacketLogger(Module):
+       def __init__(self, description, packet_class):
+               self.sink = Sink(description)
+               ###
+               self.packet_class = packet_class
+               self.packet = packet_class()
+
+       def receive(self):
+               self.packet.done = 0
+               while self.packet.done == 0:
+                       yield
+
+       def do_simulation(self, selfp):
+               selfp.sink.ack = 1
+               if selfp.sink.stb == 1 and selfp.sink.sop == 1:
+                       self.packet = self.packet_class()
+               if selfp.sink.stb:
+                       if hasattr(selfp.sink, "data"):
+                               self.packet.append(selfp.sink.data)
+                       else:
+                               self.packet.append(selfp.sink.d)
+               if selfp.sink.stb == 1 and selfp.sink.eop == 1:
+                       self.packet.done = True
+
 class AckRandomizer(Module):
        def __init__(self, description, level=0):
                self.level = level
index 907f5896ab431ba601516fb885e496e20bfa8288..d4de61dc70541c14afa979b54b702edef69e10f4 100644 (file)
@@ -1,4 +1,4 @@
-import random, copy
+import random
 
 from migen.fhdl.std import *
 from migen.genlib.record import *
@@ -7,62 +7,16 @@ from migen.sim.generic import run_simulation
 from lib.sata.common import *
 from lib.sata.link import SATALink
 
-from lib.sata.test.hdd import *
 from lib.sata.test.common import *
+from lib.sata.test.hdd import *
 
-class LinkStreamer(Module):
+class LinkStreamer(PacketStreamer):
        def __init__(self):
-               self.source = Source(link_description(32))
-               ###
-               self.packets = []
-               self.packet = LinkTXPacket()
-               self.packet.done = 1
-
-       def send(self, packet, blocking=True):
-               packet = copy.deepcopy(packet)
-               self.packets.append(packet)
-               if blocking:
-                       while packet.done == 0:
-                               yield
+               PacketStreamer.__init__(self, link_description(32), LinkTXPacket)
 
-       def do_simulation(self, selfp):
-               if len(self.packets) and self.packet.done:
-                       self.packet = self.packets.pop(0)
-               if not self.packet.ongoing and not self.packet.done:
-                       selfp.source.stb = 1
-                       selfp.source.sop = 1
-                       selfp.source.d = self.packet.pop(0)
-                       self.packet.ongoing = True
-               elif selfp.source.stb == 1 and selfp.source.ack == 1:
-                       selfp.source.sop = 0
-                       selfp.source.eop = (len(self.packet) == 1)
-                       if len(self.packet) > 0:
-                               selfp.source.stb = 1
-                               selfp.source.d = self.packet.pop(0)
-                       else:
-                               self.packet.done = 1
-                               selfp.source.stb = 0
-
-class LinkLogger(Module):
+class LinkLogger(PacketLogger):
        def __init__(self):
-               self.sink = Sink(link_description(32))
-               ###
-               self.packet = LinkRXPacket()
-
-       def receive(self):
-               self.packet.done = 0
-               while self.packet.done == 0:
-                       yield
-
-       def do_simulation(self, selfp):
-               selfp.sink.ack = 1
-               if selfp.sink.stb == 1 and selfp.sink.sop == 1:
-                       self.packet = LinkRXPacket()
-                       self.packet.append(selfp.sink.d)
-               elif selfp.sink.stb:
-                       self.packet.append(selfp.sink.d)
-               if (selfp.sink.stb ==1 and selfp.sink.eop ==1):
-                       self.packet.done = True
+               PacketLogger.__init__(self, link_description(32), LinkRXPacket)
 
 class TB(Module):
        def __init__(self):