2014-12-14 10:20:22 -05:00
|
|
|
import random, copy
|
2014-12-12 16:26:04 -05:00
|
|
|
|
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.genlib.record import *
|
|
|
|
from migen.sim.generic import run_simulation
|
|
|
|
|
2014-12-14 04:45:26 -05:00
|
|
|
from lib.sata.common import *
|
2014-12-12 16:26:04 -05:00
|
|
|
from lib.sata.link import SATALink
|
|
|
|
from lib.sata.transport import SATATransport
|
|
|
|
from lib.sata.command import SATACommand
|
|
|
|
|
2014-12-14 10:20:22 -05:00
|
|
|
from lib.sata.test.hdd import *
|
2014-12-12 16:26:04 -05:00
|
|
|
from lib.sata.test.common import *
|
|
|
|
|
2014-12-14 06:49:35 -05:00
|
|
|
class CommandTXPacket(list):
|
2014-12-15 07:26:53 -05:00
|
|
|
def __init__(self, write=0, read=0, identify=0, sector=0, count=0, data=[]):
|
2014-12-14 06:49:35 -05:00
|
|
|
self.ongoing = False
|
|
|
|
self.done = False
|
|
|
|
self.write = write
|
|
|
|
self.read = read
|
|
|
|
self.identify = identify
|
2014-12-15 07:26:53 -05:00
|
|
|
self.sector = sector
|
|
|
|
self.count = count
|
2014-12-14 06:49:35 -05:00
|
|
|
for d in data:
|
|
|
|
self.append(d)
|
|
|
|
|
|
|
|
class CommandStreamer(Module):
|
|
|
|
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):
|
2014-12-14 10:20:22 -05:00
|
|
|
packet = copy.deepcopy(packet)
|
2014-12-14 06:49:35 -05:00
|
|
|
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)
|
|
|
|
|
|
|
|
selfp.source.write = self.packet.write
|
|
|
|
selfp.source.read = self.packet.read
|
|
|
|
selfp.source.identify = self.packet.identify
|
2014-12-15 07:26:53 -05:00
|
|
|
selfp.source.sector = self.packet.sector
|
|
|
|
selfp.source.count = self.packet.count
|
2014-12-14 06:49:35 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
self.done = False
|
|
|
|
self.write = 0
|
|
|
|
self.read = 0
|
|
|
|
self.identify = 0
|
|
|
|
self.success = 0
|
|
|
|
self.failed = 0
|
|
|
|
|
|
|
|
class CommandLogger(Module):
|
|
|
|
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
|
|
|
|
|
|
|
|
def do_simulation(self, selfp):
|
|
|
|
selfp.sink.ack = 1
|
|
|
|
if selfp.sink.stb == 1 and selfp.sink.sop == 1:
|
|
|
|
self.packet = CommandRXPacket()
|
|
|
|
self.packet.write = selfp.sink.write
|
|
|
|
self.packet.read = selfp.sink.read
|
|
|
|
self.packet.identify = selfp.sink.identify
|
|
|
|
self.packet.sucess = selfp.sink.success
|
2014-12-14 06:59:02 -05:00
|
|
|
self.packet.failed = selfp.sink.failed
|
2014-12-14 06:49:35 -05:00
|
|
|
self.packet.append(selfp.sink.data)
|
|
|
|
elif selfp.sink.stb:
|
|
|
|
self.packet.append(selfp.sink.data)
|
2014-12-14 09:32:00 -05:00
|
|
|
if (selfp.sink.stb == 1 and selfp.sink.eop == 1):
|
2014-12-14 06:49:35 -05:00
|
|
|
self.packet.done = True
|
|
|
|
|
2014-12-12 16:26:04 -05:00
|
|
|
class TB(Module):
|
|
|
|
def __init__(self):
|
2014-12-14 10:20:22 -05:00
|
|
|
self.submodules.hdd = HDD(
|
2014-12-15 07:26:53 -05:00
|
|
|
link_debug=False, link_random_level=50,
|
2014-12-14 10:20:22 -05:00
|
|
|
transport_debug=False, transport_loopback=False,
|
2014-12-14 14:30:21 -05:00
|
|
|
hdd_debug=True)
|
2014-12-14 10:20:22 -05:00
|
|
|
self.submodules.link = SATALink(self.hdd.phy)
|
2014-12-12 16:26:04 -05:00
|
|
|
self.submodules.transport = SATATransport(self.link)
|
|
|
|
self.submodules.command = SATACommand(self.transport)
|
|
|
|
|
2014-12-14 06:49:35 -05:00
|
|
|
self.submodules.streamer = CommandStreamer()
|
2014-12-14 15:03:14 -05:00
|
|
|
streamer_ack_randomizer = AckRandomizer(command_tx_description(32), level=50)
|
2014-12-14 10:38:38 -05:00
|
|
|
self.submodules += streamer_ack_randomizer
|
2014-12-14 06:49:35 -05:00
|
|
|
self.submodules.logger = CommandLogger()
|
2014-12-14 15:03:14 -05:00
|
|
|
logger_ack_randomizer = AckRandomizer(command_rx_description(32), level=50)
|
2014-12-14 10:38:38 -05:00
|
|
|
self.submodules += logger_ack_randomizer
|
2014-12-14 06:49:35 -05:00
|
|
|
self.comb += [
|
2014-12-14 10:38:38 -05:00
|
|
|
Record.connect(self.streamer.source, streamer_ack_randomizer.sink),
|
|
|
|
Record.connect(streamer_ack_randomizer.source, self.command.sink),
|
|
|
|
Record.connect(self.command.source, logger_ack_randomizer.sink),
|
|
|
|
Record.connect(logger_ack_randomizer.source, self.logger.sink)
|
2014-12-14 06:49:35 -05:00
|
|
|
]
|
|
|
|
|
2014-12-12 16:26:04 -05:00
|
|
|
def gen_simulation(self, selfp):
|
2014-12-15 07:26:53 -05:00
|
|
|
hdd = self.hdd
|
|
|
|
hdd.malloc(0, 64)
|
|
|
|
write_data = [i for i in range(hdd.sectors2dwords(2))]
|
|
|
|
write_len = hdd.dwords2sectors(len(write_data))
|
|
|
|
write_packet = CommandTXPacket(write=1, sector=2, count=write_len, data=write_data)
|
2014-12-14 10:20:22 -05:00
|
|
|
yield from self.streamer.send(write_packet)
|
2014-12-14 09:32:00 -05:00
|
|
|
yield from self.logger.receive()
|
2014-12-15 07:26:53 -05:00
|
|
|
read_packet = CommandTXPacket(read=1, sector=2, count=write_len)
|
2014-12-14 10:20:22 -05:00
|
|
|
yield from self.streamer.send(read_packet)
|
2014-12-14 06:49:35 -05:00
|
|
|
yield from self.logger.receive()
|
2014-12-14 10:20:22 -05:00
|
|
|
read_data = self.logger.packet
|
|
|
|
|
|
|
|
# check results
|
|
|
|
s, l, e = check(write_data, read_data)
|
|
|
|
print("shift "+ str(s) + " / length " + str(l) + " / errors " + str(e))
|
2014-12-12 16:26:04 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-12-15 07:26:53 -05:00
|
|
|
run_simulation(TB(), ncycles=2048, vcd_name="my.vcd", keep_files=True)
|