2015-04-13 08:47:44 -04:00
|
|
|
import random
|
|
|
|
|
2015-02-21 17:13:43 -05:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.genlib.fsm import *
|
|
|
|
from migen.actorlib.fifo import *
|
|
|
|
from migen.flow.actor import EndpointDescription
|
|
|
|
|
|
|
|
user_layout = EndpointDescription(
|
2015-04-13 08:29:44 -04:00
|
|
|
[("dst", 8),
|
|
|
|
("length", 4*8),
|
|
|
|
("error", 1),
|
|
|
|
("d", 8)
|
2015-04-13 08:09:58 -04:00
|
|
|
],
|
|
|
|
packetized=True
|
2015-02-21 17:13:43 -05:00
|
|
|
)
|
|
|
|
|
2015-04-13 08:29:44 -04:00
|
|
|
phy_layout = [("d", 8)]
|
2015-02-21 17:13:43 -05:00
|
|
|
|
2015-04-13 08:27:31 -04:00
|
|
|
|
2015-03-22 06:08:47 -04:00
|
|
|
class LiteUSBPipe:
|
2015-04-13 08:09:58 -04:00
|
|
|
def __init__(self, layout):
|
|
|
|
self.sink = Sink(layout)
|
|
|
|
self.source = Source(layout)
|
2015-02-21 17:13:43 -05:00
|
|
|
|
2015-04-13 08:27:31 -04:00
|
|
|
|
2015-03-22 06:08:47 -04:00
|
|
|
class LiteUSBTimeout(Module):
|
2015-04-13 08:09:58 -04:00
|
|
|
def __init__(self, clk_freq, length):
|
|
|
|
cnt_max = int(clk_freq*length)
|
|
|
|
width = bits_for(cnt_max)
|
|
|
|
|
|
|
|
self.clear = Signal()
|
|
|
|
self.done = Signal()
|
|
|
|
|
|
|
|
cnt = Signal(width)
|
|
|
|
self.sync += \
|
|
|
|
If(self.clear,
|
|
|
|
cnt.eq(0)
|
|
|
|
).Elif(~self.done,
|
|
|
|
cnt.eq(cnt+1)
|
|
|
|
)
|
|
|
|
self.comb += self.done.eq(cnt == cnt_max)
|
2015-02-21 17:13:43 -05:00
|
|
|
|
2015-04-13 08:47:44 -04:00
|
|
|
|
2015-02-21 17:13:43 -05:00
|
|
|
#
|
|
|
|
# TB
|
|
|
|
#
|
|
|
|
def randn(max_n):
|
2015-04-13 08:09:58 -04:00
|
|
|
return random.randint(0, max_n-1)
|
2015-02-21 17:13:43 -05:00
|
|
|
|
2015-04-13 08:27:31 -04:00
|
|
|
|
2015-02-21 17:13:43 -05:00
|
|
|
class RandRun:
|
2015-04-13 08:09:58 -04:00
|
|
|
def __init__(self, level=0):
|
|
|
|
self.run = True
|
|
|
|
self.level = level
|
|
|
|
|
|
|
|
def do_simulation(self, selfp):
|
|
|
|
self.run = True
|
|
|
|
n = randn(100)
|
|
|
|
if n < self.level:
|
|
|
|
self.run = False
|