296cbb84c5ef18b1cb637b82cedb3c2bae862860
[litex.git] / liteeth / test / model / phy.py
1 from liteeth.common import *
2 from liteeth.mac.common import *
3 from liteeth.test.common import *
4
5 def print_phy(s):
6 print_with_prefix(s, "[PHY]")
7
8 # PHY model
9 class PHYSource(PacketStreamer):
10 def __init__(self, dw):
11 PacketStreamer.__init__(self, eth_phy_description(dw))
12
13 class PHYSink(PacketLogger):
14 def __init__(self, dw):
15 PacketLogger.__init__(self, eth_phy_description(dw))
16
17 class PHY(Module):
18 def __init__(self, dw, debug=False):
19 self.dw = dw
20 self.debug = debug
21
22 self.submodules.phy_source = PHYSource(dw)
23 self.submodules.phy_sink = PHYSink(dw)
24
25 self.source = self.phy_source.source
26 self.sink = self.phy_sink.sink
27
28 self.mac_callback = None
29
30 def set_mac_callback(self, callback):
31 self.mac_callback = callback
32
33 def send(self, datas):
34 packet = Packet(datas)
35 if self.debug:
36 r = ">>>>>>>>\n"
37 r += "length " + str(len(datas)) + "\n"
38 for d in datas:
39 r += "%02x" %d
40 print_phy(r)
41 yield from self.phy_source.send(packet)
42
43 def receive(self):
44 yield from self.phy_sink.receive()
45 if self.debug:
46 r = "<<<<<<<<\n"
47 r += "length " + str(len(self.phy_sink.packet)) + "\n"
48 for d in self.phy_sink.packet:
49 r += "%02x" %d
50 print_phy(r)
51 self.packet = self.phy_sink.packet