link: wip bfm
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 12 Nov 2014 17:20:34 +0000 (18:20 +0100)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Wed, 12 Nov 2014 17:20:34 +0000 (18:20 +0100)
lib/sata/link/scrambler.py
lib/sata/link/test/bfm.py
lib/sata/link/test/link_tb.py

index df599fec066fee32094398e123373ba1e110069f..640e96f44966ac702c04ef43cff75b3d7fa04d96 100644 (file)
@@ -77,7 +77,7 @@ class SATAScrambler(Module):
 
                self.submodules.scrambler = Scrambler()
                ongoing = Signal()
-               self.sync += [
+               self.sync += \
                        If(sink.stb & sink.ack,
                                If(sink.eop,
                                        ongoing.eq(0)
@@ -85,10 +85,9 @@ class SATAScrambler(Module):
                                        ongoing.eq(1)
                                )
                        )
-               ]
                self.comb += [
-                       self.scrambler.ce.eq(sink.stb & (sink.sop | ongoing)),
-                       self.scrambler.reset.eq(~ongoing),
+                       self.scrambler.ce.eq(sink.stb & sink.ack & (sink.sop | ongoing)),
+                       self.scrambler.reset.eq(~(sink.sop | ongoing)),
                        Record.connect(sink, source),
                        source.d.eq(sink.d ^ self.scrambler.value)
                ]
index 10c87e429a774310bb913f0aa1cbbd63781d76d2..577d15cced4e2d210ce8a259b618339822bd5187 100644 (file)
@@ -1,3 +1,5 @@
+import subprocess
+
 from migen.fhdl.std import *
 
 from lib.sata.std import *
@@ -32,7 +34,7 @@ class BFMSource(Module):
                                if v == self.dword.dat:
                                        selfp.source.charisk = 0b0001
                        selfp.source.data = self.dword.dat
-               elif selfp.source.stb == 1 and selfp.source.ack == 1:
+               if selfp.source.stb == 1 and selfp.source.ack == 1:
                                self.dword.done = 1
                                selfp.source.stb = 0
 
@@ -81,8 +83,60 @@ class BFM(Module):
                ###
 
                self.submodules.phy = BFMPHY(dw)
+               self.get_scrambler_ref()
+
+               self.rx_packet_ongoing = False
+               self.rx_packet = []
+
+       def get_scrambler_ref(self):
+               p = subprocess.Popen(["./scrambler"], stdout=subprocess.PIPE)
+               out, err = p.communicate()
+               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):
+               # Todo from C Code or Python Code
+               return packet[:-1]
+
+       def packet_callback(self, packet):
+               packet = self.descramble(packet)
+               packet = self.check_crc(packet)
+               for v in packet:
+                       print("%08x" %v)
+
+       def dword_callback(self, dword):
+               print("%08x " %dword, end="")
+               for k, v in primitives.items():
+                       if dword == v:
+                               print(k, end="")
+               print("")
+
+               # X_RDY / WTRM response
+               if dword == primitives["X_RDY"]:
+                       self.phy.bfm_source.dwords.append(BFMDword(primitives["R_RDY"]))
+               if dword == primitives["WTRM"]:
+                       self.phy.bfm_source.dwords.append(BFMDword(primitives["R_OK"]))
+
+               # packet capture
+               if dword == primitives["EOF"]:
+                       self.rx_packet_ongoing = False
+                       self.packet_callback(self.rx_packet)
+
+               if self.rx_packet_ongoing:
+                       self.rx_packet.append(dword)
+
+               if dword == primitives["SOF"]:
+                       self.rx_packet_ongoing = True
+                       self.rx_packet = []
 
        def gen_simulation(self, selfp):
+               self.phy.bfm_source.dwords.append(BFMDword(primitives["SYNC"]))
                while True:
                        yield from self.phy.receive()
-                       print("%08x" %(self.phy.rx_dword))
+                       self.dword_callback(self.phy.rx_dword)
index 7753e5b0fd6900ecde13d479dd89d479bdbd9497..50dc4404baacf1d5cfd75ef5fab8f867b7ae7a6a 100644 (file)
@@ -76,15 +76,15 @@ class TB(Module):
                self.submodules.streamer = LinkStreamer(32)
                self.submodules.logger = LinkLogger(32)
                self.comb += [
-                       self.link_layer.sink.eq(self.streamer.source),
-                       self.logger.sink.eq(self.link_layer.source)
+                       Record.connect(self.streamer.source, self.link_layer.sink),
+                       Record.connect(self.link_layer.source, self.logger.sink)
                ]
 
        def gen_simulation(self, selfp):
                for i in range(200):
                        yield
-               yield from self.bfm.phy.send(BFMDword(primitives["R_RDY"]), False)
-               yield from self.streamer.send(LinkPacket([0, 1, 2, 3]))
+               for i in range(8):
+                       yield from self.streamer.send(LinkPacket([0, 1, 2, 3]))
 
 if __name__ == "__main__":
-       run_simulation(TB(), ncycles=5000, vcd_name="my.vcd", keep_files=True)
+       run_simulation(TB(), ncycles=512, vcd_name="my.vcd", keep_files=True)