forked from M-Labs/artiq
serwb/scrambler: simplify and set scrambler input data to 0 when sink.stb == 0
This commit is contained in:
parent
7fe49a78d2
commit
2c627cd061
|
@ -31,75 +31,51 @@ class _Scrambler(Module):
|
||||||
|
|
||||||
|
|
||||||
class Scrambler(Module):
|
class Scrambler(Module):
|
||||||
def __init__(self, sync_interval=1024):
|
def __init__(self, sync_interval=2**10):
|
||||||
self.enable = Signal()
|
|
||||||
self.sink = sink = stream.Endpoint([("data", 32)])
|
self.sink = sink = stream.Endpoint([("data", 32)])
|
||||||
self.source = source = stream.Endpoint([("d", 32), ("k", 4)])
|
self.source = source = stream.Endpoint([("d", 32), ("k", 4)])
|
||||||
|
|
||||||
# # #
|
# # #
|
||||||
|
|
||||||
# scrambler
|
# Scrambler
|
||||||
self.submodules.scrambler = scrambler = _Scrambler(32)
|
self.submodules.scrambler = scrambler = _Scrambler(32)
|
||||||
|
|
||||||
# insert K.29.7 as sync character
|
# Insert K29.7 SYNC character every "sync_interval" cycles
|
||||||
# every sync_interval cycles
|
|
||||||
count = Signal(max=sync_interval)
|
count = Signal(max=sync_interval)
|
||||||
self.submodules.fsm = fsm = ResetInserter()(FSM(reset_state="DISABLE"))
|
self.sync += If(source.ack, count.eq(count + 1))
|
||||||
self.comb += fsm.reset.eq(~self.enable)
|
self.comb += [
|
||||||
fsm.act("DISABLE",
|
source.stb.eq(1),
|
||||||
sink.connect(source, omit={"data"}),
|
If(count == 0,
|
||||||
source.k.eq(0b0000),
|
|
||||||
source.d.eq(sink.data),
|
|
||||||
NextState("SYNC")
|
|
||||||
)
|
|
||||||
fsm.act("SYNC",
|
|
||||||
scrambler.reset.eq(1),
|
scrambler.reset.eq(1),
|
||||||
source.stb.eq(1),
|
source.k.eq(0b1),
|
||||||
source.k[0].eq(1),
|
source.d.eq(K(29, 7)),
|
||||||
source.d[:8].eq(K(29, 7)),
|
).Else(
|
||||||
NextValue(count, 0),
|
If(sink.stb, scrambler.i.eq(sink.data)),
|
||||||
If(source.ack,
|
source.k.eq(0),
|
||||||
NextState("DATA")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
fsm.act("DATA",
|
|
||||||
scrambler.i.eq(sink.data),
|
|
||||||
sink.ack.eq(source.ack),
|
|
||||||
source.stb.eq(1),
|
|
||||||
source.d.eq(scrambler.o),
|
source.d.eq(scrambler.o),
|
||||||
If(source.stb & source.ack,
|
If(source.ack,
|
||||||
scrambler.ce.eq(1),
|
sink.ack.eq(1),
|
||||||
NextValue(count, count + 1),
|
scrambler.ce.eq(1)
|
||||||
If(count == (sync_interval - 1),
|
|
||||||
NextState("SYNC")
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class Descrambler(Module):
|
class Descrambler(Module):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.enable = Signal()
|
|
||||||
self.sink = sink = stream.Endpoint([("d", 32), ("k", 4)])
|
self.sink = sink = stream.Endpoint([("d", 32), ("k", 4)])
|
||||||
self.source = source = stream.Endpoint([("data", 32)])
|
self.source = source = stream.Endpoint([("data", 32)])
|
||||||
|
|
||||||
# # #
|
# # #
|
||||||
|
|
||||||
# descrambler
|
# Descrambler
|
||||||
self.submodules.descrambler = descrambler = _Scrambler(32)
|
self.submodules.descrambler = descrambler = _Scrambler(32)
|
||||||
self.comb += descrambler.i.eq(sink.d)
|
self.comb += descrambler.i.eq(sink.d)
|
||||||
|
|
||||||
# detect K29.7 and synchronize descrambler
|
# Detect K29.7 SYNC character and synchronize Descrambler
|
||||||
self.submodules.fsm = fsm = ResetInserter()(FSM(reset_state="DISABLE"))
|
self.comb += \
|
||||||
self.comb += fsm.reset.eq(~self.enable)
|
|
||||||
fsm.act("DISABLE",
|
|
||||||
sink.connect(source, omit={"d", "k"}),
|
|
||||||
source.data.eq(sink.d),
|
|
||||||
NextState("SYNC_DATA")
|
|
||||||
)
|
|
||||||
fsm.act("SYNC_DATA",
|
|
||||||
If(sink.stb,
|
If(sink.stb,
|
||||||
If((sink.k == 1) & (sink.d == K(29,7)),
|
If((sink.k == 0b1) & (sink.d == K(29,7)),
|
||||||
sink.ack.eq(1),
|
sink.ack.eq(1),
|
||||||
descrambler.reset.eq(1)
|
descrambler.reset.eq(1)
|
||||||
).Else(
|
).Else(
|
||||||
|
@ -111,4 +87,3 @@ class Descrambler(Module):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
|
|
@ -101,7 +101,7 @@ class DUTCore(Module):
|
||||||
|
|
||||||
class TestSERWBCore(unittest.TestCase):
|
class TestSERWBCore(unittest.TestCase):
|
||||||
def test_scrambler(self):
|
def test_scrambler(self):
|
||||||
def generator(dut):
|
def generator(dut, rand_level=50):
|
||||||
# prepare test
|
# prepare test
|
||||||
prng = random.Random(42)
|
prng = random.Random(42)
|
||||||
i = 0
|
i = 0
|
||||||
|
@ -115,7 +115,7 @@ class TestSERWBCore(unittest.TestCase):
|
||||||
yield dut.scrambler.sink.data.eq(i)
|
yield dut.scrambler.sink.data.eq(i)
|
||||||
|
|
||||||
# check
|
# check
|
||||||
yield dut.descrambler.source.ack.eq(prng.randrange(2))
|
yield dut.descrambler.source.ack.eq(prng.randrange(prng.randrange(100) > rand_level)
|
||||||
if (yield dut.descrambler.source.stb) & (yield dut.descrambler.source.ack):
|
if (yield dut.descrambler.source.stb) & (yield dut.descrambler.source.ack):
|
||||||
current_data = (yield dut.descrambler.source.data)
|
current_data = (yield dut.descrambler.source.data)
|
||||||
if (current_data != (last_data + 1)):
|
if (current_data != (last_data + 1)):
|
||||||
|
|
Loading…
Reference in New Issue