2014-11-12 12:20:34 -05:00
|
|
|
import subprocess
|
|
|
|
|
2014-11-11 12:47:34 -05:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
|
|
|
|
from lib.sata.std import *
|
2014-12-02 15:34:16 -05:00
|
|
|
from lib.sata.link.test.common import *
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
class BFMDword():
|
|
|
|
def __init__(self, dat=0):
|
|
|
|
self.dat = dat
|
|
|
|
self.start = 1
|
|
|
|
self.done = 0
|
|
|
|
|
|
|
|
class BFMSource(Module):
|
|
|
|
def __init__(self, dw):
|
|
|
|
self.source = Source(phy_layout(dw))
|
|
|
|
###
|
|
|
|
self.dword = BFMDword()
|
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
def send(self, dword):
|
|
|
|
self.dword = dword
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
def do_simulation(self, selfp):
|
2014-12-02 15:34:16 -05:00
|
|
|
selfp.source.stb = 1
|
|
|
|
selfp.source.charisk = 0b0000
|
|
|
|
for k, v in primitives.items():
|
|
|
|
if v == self.dword.dat:
|
|
|
|
selfp.source.charisk = 0b0001
|
|
|
|
selfp.source.data = self.dword.dat
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
class BFMSink(Module):
|
|
|
|
def __init__(self, dw):
|
|
|
|
self.sink = Sink(phy_layout(dw))
|
|
|
|
###
|
|
|
|
self.dword = BFMDword()
|
|
|
|
|
|
|
|
def receive(self):
|
|
|
|
self.dword.done = 0
|
|
|
|
while self.dword.done == 0:
|
|
|
|
yield
|
|
|
|
|
|
|
|
def do_simulation(self, selfp):
|
|
|
|
self.dword.done = 0
|
|
|
|
selfp.sink.ack = 1
|
|
|
|
if selfp.sink.stb == 1:
|
|
|
|
self.dword.done = 1
|
|
|
|
self.dword.dat = selfp.sink.data
|
|
|
|
|
|
|
|
class BFMPHY(Module):
|
|
|
|
def __init__(self, dw):
|
|
|
|
self.dw = dw
|
|
|
|
|
|
|
|
self.submodules.bfm_sink = BFMSink(dw)
|
|
|
|
self.submodules.bfm_source = BFMSource(dw)
|
|
|
|
|
|
|
|
self.source = self.bfm_source.source
|
|
|
|
self.sink = self.bfm_sink.sink
|
|
|
|
|
|
|
|
self.dword = 0
|
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
def send(self, dword):
|
2014-11-11 12:47:34 -05:00
|
|
|
packet = BFMDword(dword)
|
2014-12-03 03:17:51 -05:00
|
|
|
self.bfm_source.send(packet)
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
def receive(self):
|
|
|
|
yield from self.bfm_sink.receive()
|
|
|
|
self.rx_dword = self.bfm_sink.dword.dat
|
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
def __repr__(self):
|
|
|
|
# receive
|
|
|
|
receiving = "%08x " %self.rx_dword
|
2014-12-03 05:12:26 -05:00
|
|
|
receiving += decode_primitive(self.rx_dword)
|
2014-12-03 03:17:51 -05:00
|
|
|
receiving += " "*(16-len(receiving))
|
|
|
|
|
|
|
|
# send
|
|
|
|
sending = "%08x " %self.bfm_source.dword.dat
|
2014-12-03 05:12:26 -05:00
|
|
|
sending += decode_primitive(self.bfm_source.dword.dat)
|
2014-12-03 03:17:51 -05:00
|
|
|
sending += " "*(16-len(sending))
|
|
|
|
|
|
|
|
return receiving + sending
|
|
|
|
|
|
|
|
|
2014-11-11 12:47:34 -05:00
|
|
|
class BFM(Module):
|
2014-12-03 03:17:51 -05:00
|
|
|
def __init__(self, dw, debug=False, hold_random_level=0):
|
2014-11-11 12:47:34 -05:00
|
|
|
self.debug = debug
|
2014-12-03 03:17:51 -05:00
|
|
|
self.hold_random_level = hold_random_level
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
self.submodules.phy = BFMPHY(dw)
|
2014-11-12 12:20:34 -05:00
|
|
|
self.get_scrambler_ref()
|
|
|
|
|
2014-12-03 05:12:26 -05:00
|
|
|
self.rx_cont_ongoing = False
|
2014-11-12 12:20:34 -05:00
|
|
|
self.rx_packet_ongoing = False
|
|
|
|
self.rx_packet = []
|
|
|
|
|
2014-12-03 03:17:51 -05:00
|
|
|
self.run = True
|
|
|
|
|
2014-11-12 12:20:34 -05:00
|
|
|
def get_scrambler_ref(self):
|
2014-12-02 13:53:13 -05:00
|
|
|
with subprocess.Popen(["./scrambler"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
|
|
|
|
process.stdin.write("0x10000".encode("ASCII"))
|
|
|
|
out, err = process.communicate()
|
2014-11-12 12:20:34 -05:00
|
|
|
self.scrambler_ref = [int(e, 16) for e in out.decode("utf-8").split("\n")[:-1]]
|
|
|
|
|
|
|
|
def descramble(self, packet):
|
|
|
|
p = []
|
|
|
|
for i in range(len(packet)):
|
|
|
|
v = packet[i] ^ self.scrambler_ref[i]
|
|
|
|
p.append(v)
|
|
|
|
return p
|
|
|
|
|
|
|
|
def check_crc(self, packet):
|
2014-12-02 14:02:43 -05:00
|
|
|
stdin = ""
|
|
|
|
for v in packet[:-1]:
|
|
|
|
stdin += "0x%08x " %v
|
|
|
|
stdin += "exit"
|
|
|
|
with subprocess.Popen("./crc", stdin=subprocess.PIPE, stdout=subprocess.PIPE) as process:
|
|
|
|
process.stdin.write(stdin.encode("ASCII"))
|
|
|
|
out, err = process.communicate()
|
|
|
|
crc = int(out.decode("ASCII"), 16)
|
|
|
|
if packet[-1] != crc:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return packet[:-1]
|
2014-11-12 12:20:34 -05:00
|
|
|
|
|
|
|
def packet_callback(self, packet):
|
|
|
|
packet = self.descramble(packet)
|
|
|
|
packet = self.check_crc(packet)
|
2014-12-02 15:34:16 -05:00
|
|
|
print("----")
|
2014-11-12 12:20:34 -05:00
|
|
|
for v in packet:
|
|
|
|
print("%08x" %v)
|
2014-12-02 15:34:16 -05:00
|
|
|
print("----")
|
2014-11-12 12:20:34 -05:00
|
|
|
|
|
|
|
def dword_callback(self, dword):
|
2014-12-03 05:50:31 -05:00
|
|
|
# CONT detection
|
2014-12-03 05:12:26 -05:00
|
|
|
if dword == primitives["CONT"]:
|
|
|
|
self.rx_cont_ongoing = True
|
|
|
|
elif is_primitive(dword):
|
|
|
|
self.rx_cont_ongoing = False
|
|
|
|
|
2014-11-12 12:20:34 -05:00
|
|
|
# X_RDY / WTRM response
|
|
|
|
if dword == primitives["X_RDY"]:
|
2014-12-03 03:17:51 -05:00
|
|
|
self.phy.send(primitives["R_RDY"])
|
2014-12-02 15:34:16 -05:00
|
|
|
|
|
|
|
elif dword == primitives["WTRM"]:
|
2014-12-03 03:17:51 -05:00
|
|
|
self.phy.send(primitives["R_OK"])
|
2014-11-12 12:20:34 -05:00
|
|
|
|
2014-12-02 15:34:16 -05:00
|
|
|
# HOLD response
|
|
|
|
elif dword == primitives["HOLD"]:
|
2014-12-03 03:17:51 -05:00
|
|
|
self.phy.send(primitives["HOLDA"])
|
2014-12-02 15:34:16 -05:00
|
|
|
|
2014-12-03 05:50:31 -05:00
|
|
|
# Packet capture
|
2014-12-02 15:34:16 -05:00
|
|
|
elif dword == primitives["EOF"]:
|
2014-11-12 12:20:34 -05:00
|
|
|
self.rx_packet_ongoing = False
|
|
|
|
self.packet_callback(self.rx_packet)
|
|
|
|
|
2014-12-02 15:34:16 -05:00
|
|
|
elif self.rx_packet_ongoing:
|
|
|
|
if dword != primitives["HOLD"]:
|
|
|
|
n = randn(100)
|
2014-12-03 03:17:51 -05:00
|
|
|
if n < self.hold_random_level:
|
|
|
|
self.phy.send(primitives["HOLD"])
|
2014-12-02 15:34:16 -05:00
|
|
|
else:
|
2014-12-03 03:17:51 -05:00
|
|
|
self.phy.send(primitives["R_RDY"])
|
2014-12-03 05:12:26 -05:00
|
|
|
if not is_primitive(dword):
|
|
|
|
if not self.rx_cont_ongoing:
|
|
|
|
self.rx_packet.append(dword)
|
2014-12-02 15:34:16 -05:00
|
|
|
|
|
|
|
elif dword == primitives["SOF"]:
|
2014-11-12 12:20:34 -05:00
|
|
|
self.rx_packet_ongoing = True
|
|
|
|
self.rx_packet = []
|
2014-11-11 12:47:34 -05:00
|
|
|
|
|
|
|
def gen_simulation(self, selfp):
|
2014-12-03 03:17:51 -05:00
|
|
|
self.phy.send(primitives["SYNC"])
|
2014-11-11 12:47:34 -05:00
|
|
|
while True:
|
|
|
|
yield from self.phy.receive()
|
2014-12-03 03:17:51 -05:00
|
|
|
if self.debug:
|
|
|
|
print(self.phy)
|
2014-11-12 12:20:34 -05:00
|
|
|
self.dword_callback(self.phy.rx_dword)
|