2013-09-21 07:04:07 -04:00
|
|
|
from migen.fhdl.std import *
|
|
|
|
|
2013-09-22 05:35:02 -04:00
|
|
|
def dec2bin(d, nb=0):
|
|
|
|
if d=="x":
|
|
|
|
return "x"*nb
|
|
|
|
elif d==0:
|
|
|
|
b="0"
|
2013-09-21 07:04:07 -04:00
|
|
|
else:
|
2013-09-22 05:35:02 -04:00
|
|
|
b=""
|
|
|
|
while d!=0:
|
|
|
|
b="01"[d&1]+b
|
|
|
|
d=d>>1
|
|
|
|
return b.zfill(nb)
|
2013-03-18 18:57:51 -04:00
|
|
|
|
|
|
|
class RisingEdge(Module):
|
2013-09-22 05:35:02 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.i = Signal()
|
|
|
|
self.o = Signal()
|
2013-03-18 18:57:51 -04:00
|
|
|
####
|
|
|
|
i_d = Signal()
|
2013-09-21 07:04:07 -04:00
|
|
|
self.sync += i_d.eq(self.i)
|
|
|
|
self.comb += self.o.eq(self.i & ~i_d)
|
2013-03-18 18:57:51 -04:00
|
|
|
|
|
|
|
class FallingEdge(Module):
|
2013-09-22 05:35:02 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.i = Signal()
|
|
|
|
self.o = Signal()
|
2013-03-18 18:57:51 -04:00
|
|
|
####
|
|
|
|
i_d = Signal()
|
2013-09-21 07:04:07 -04:00
|
|
|
self.sync += i_d.eq(self.i)
|
|
|
|
self.comb += self.o.eq(~self.i & i_d)
|
2013-03-18 18:57:51 -04:00
|
|
|
|
|
|
|
class FreqGen(Module):
|
2013-09-22 05:35:02 -04:00
|
|
|
def __init__(self, clk_freq, freq):
|
2013-03-18 18:57:51 -04:00
|
|
|
cnt_max = int(clk_freq/freq/2)
|
2013-09-22 05:35:02 -04:00
|
|
|
self.o = Signal()
|
2013-03-18 18:57:51 -04:00
|
|
|
####
|
2013-09-22 05:35:02 -04:00
|
|
|
cnt = Signal(max=cnt_max)
|
2013-03-18 18:57:51 -04:00
|
|
|
self.sync += [
|
|
|
|
If(cnt >= cnt_max,
|
|
|
|
cnt.eq(0),
|
|
|
|
self.o.eq(~self.o)
|
|
|
|
).Else(
|
|
|
|
cnt.eq(cnt+1)
|
|
|
|
)
|
2013-09-22 05:35:02 -04:00
|
|
|
]
|
2013-03-18 18:57:51 -04:00
|
|
|
|
|
|
|
RISING_EDGE = 1
|
|
|
|
FALLING_EDGE = 0
|
|
|
|
|
|
|
|
class EventGen(Module):
|
2013-09-22 05:35:02 -04:00
|
|
|
def __init__(self, level=RISING_EDGE, clk_freq=0, length=1):
|
2013-03-18 18:57:51 -04:00
|
|
|
cnt_max = int(length*clk_freq)
|
2013-09-22 05:35:02 -04:00
|
|
|
self.o = Signal()
|
2013-03-18 18:57:51 -04:00
|
|
|
###
|
2013-09-22 05:35:02 -04:00
|
|
|
cnt = Signal(max=cnt_max)
|
2013-03-18 18:57:51 -04:00
|
|
|
|
|
|
|
if level == RISING_EDGE:
|
2013-09-22 05:35:02 -04:00
|
|
|
self.submodules.edge_detect = RisingEdge()
|
2013-03-18 18:57:51 -04:00
|
|
|
elif level == FALLING_EDGE:
|
2013-09-22 05:35:02 -04:00
|
|
|
self.submodules.edge_detect = FallingEdge()
|
|
|
|
|
|
|
|
self.i = self.edge_detect.i
|
2013-03-18 18:57:51 -04:00
|
|
|
|
|
|
|
self.sync += [
|
2013-09-22 05:35:02 -04:00
|
|
|
If(self.edge_detect.o == 1,
|
2013-03-18 18:57:51 -04:00
|
|
|
cnt.eq(0),
|
|
|
|
self.o.eq(1)
|
|
|
|
).Elif(cnt >= cnt_max,
|
|
|
|
self.o.eq(0)
|
|
|
|
).Else(
|
|
|
|
cnt.eq(cnt+1)
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
class PwmGen(Module):
|
2013-09-22 05:35:02 -04:00
|
|
|
def __init__(self, width):
|
2013-03-18 18:57:51 -04:00
|
|
|
self.ratio = Signal(width)
|
2013-09-22 05:35:02 -04:00
|
|
|
self.o = Signal()
|
2013-03-18 18:57:51 -04:00
|
|
|
###
|
|
|
|
cnt = Signal(width)
|
|
|
|
self.sync += [
|
|
|
|
If(cnt == 0,
|
|
|
|
self.o.eq(1)
|
|
|
|
).Elif(cnt >= self.ratio,
|
|
|
|
self.o.eq(0)
|
|
|
|
),
|
|
|
|
cnt.eq(cnt+1)
|
2013-09-22 05:35:02 -04:00
|
|
|
]
|