-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edge_to_pulse.py
68 lines (53 loc) · 1.47 KB
/
edge_to_pulse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from amaranth import *
from amaranth.lib import wiring
from amaranth.lib.wiring import In, Out
from amaranth.sim import *
from edge_detect import EdgeDetector
class EdgeToPulse(wiring.Component):
o: Out(1)
i: In(1)
def __init__(self, bits=16):
self.width = Signal(bits)
self.bits = bits
self.ed = EdgeDetector()
super().__init__()
def elaborate(self, platform):
counter = Signal(self.bits)
m = Module()
ed = m.submodules.edge_detector = self.ed
m.d.comb += [
self.ed.i.eq(self.i),
self.o.eq((self.ed.rose == 1) | (counter > 0))
]
with m.If(self.ed.rose == 1):
m.d.sync += counter.eq(self.width - 1)
with m.If(counter > 0):
m.d.sync += counter.eq(counter - 1),
return m
if __name__=="__main__":
dut = EdgeToPulse(bits=2)
def strobe():
yield dut.i.eq(1)
yield
yield dut.i.eq(0)
yield
def proc():
assert((yield dut.o) == 0)
yield
yield dut.i.eq(1)
yield
yield
assert((yield dut.o) == 1)
yield dut.i.eq(0)
yield
yield
yield
assert((yield dut.o) == 0)
for i in range(10 * 2):
yield from strobe()
yield
sim = Simulator(dut)
sim.add_clock(1/12e6)
sim.add_sync_process(proc)
with sim.write_vcd('edge_to_pulse.vcd', 'edge_to_pulse_orig.gtkw'):
sim.run()