From 9ba947097404997ff3051386ca284c5ef496ae36 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Thu, 18 Dec 2014 13:15:39 +0100 Subject: [PATCH] test: create generic PacketStreamer/PacketLogger and use it in link_tb/command_tb --- lib/sata/test/command_tb.py | 51 ++++------------------------- lib/sata/test/common.py | 64 +++++++++++++++++++++++++++++++++++++ lib/sata/test/link_tb.py | 58 ++++----------------------------- 3 files changed, 77 insertions(+), 96 deletions(-) diff --git a/lib/sata/test/command_tb.py b/lib/sata/test/command_tb.py index b265e8281..cb0c21377 100644 --- a/lib/sata/test/command_tb.py +++ b/lib/sata/test/command_tb.py @@ -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): diff --git a/lib/sata/test/common.py b/lib/sata/test/common.py index 243898068..c908debf8 100644 --- a/lib/sata/test/common.py +++ b/lib/sata/test/common.py @@ -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 diff --git a/lib/sata/test/link_tb.py b/lib/sata/test/link_tb.py index 907f5896a..d4de61dc7 100644 --- a/lib/sata/test/link_tb.py +++ b/lib/sata/test/link_tb.py @@ -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 + PacketStreamer.__init__(self, link_description(32), LinkTXPacket) - 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 - 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):