Add RgmiiPhy

This commit is contained in:
Alex Forencich
2020-12-24 14:35:51 -08:00
parent a8a1bbde30
commit 4b768e267d
2 changed files with 44 additions and 2 deletions

View File

@@ -27,7 +27,7 @@ from collections import deque
import cocotb
from cocotb.triggers import RisingEdge, FallingEdge, Timer, First, Event
from cocotb.utils import get_sim_time
from cocotb.utils import get_sim_time, get_sim_steps
from .version import __version__
from .gmii import GmiiFrame
@@ -312,3 +312,45 @@ class RgmiiSink(object):
if frame is not None:
frame.data.append(d_val)
frame.error.append(er_val)
class RgmiiPhy:
def __init__(self, txd, tx_ctl, tx_clk, rxd, rx_ctl, rx_clk, reset=None, speed=1000e6, *args, **kwargs):
self.tx_clk = tx_clk
self.rx_clk = rx_clk
self.tx = RgmiiSink(txd, tx_ctl, tx_clk, reset)
self.rx = RgmiiSource(rxd, rx_ctl, rx_clk, reset)
self.rx_clk.setimmediatevalue(0)
self._clock_cr = None
self.set_speed(speed)
def set_speed(self, speed):
if speed in (10e6, 100e6, 1000e6):
self.speed = speed
else:
raise ValueError("Invalid speed selection")
if self._clock_cr is not None:
self._clock_cr.kill()
if self.speed == 1000e6:
self._clock_cr = cocotb.fork(self._run_clock(8*1e9/self.speed))
self.tx.mii_mode = False
self.rx.mii_mode = False
else:
self._clock_cr = cocotb.fork(self._run_clock(4*1e9/self.speed))
self.tx.mii_mode = True
self.rx.mii_mode = True
async def _run_clock(self, period):
half_period = get_sim_steps(period / 2.0, 'ns')
t = Timer(half_period)
while True:
await t
self.rx_clk <= 1
await t
self.rx_clk <= 0