-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blinker.py
75 lines (62 loc) · 1.66 KB
/
blinker.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
69
70
71
72
73
74
75
from amaranth import *
from amaranth.sim import *
from edge_to_pulse import EdgeToPulse
class Blinker(Elaboratable):
def __init__(self, width=10000, bits=16):
self.go = Signal()
self.counter = Signal(unsigned(8))
self.output = Signal()
self.width = Signal(bits, reset=width)
def elaborate(self, platform):
ed = EdgeToPulse()
count = Signal(unsigned(8))
m = Module()
m.d.comb += [
ed.width.eq(self.width),
self.output.eq(ed.output)
]
with m.If((self.go == 1)):
m.d.sync += [
count.eq(self.counter)
]
with m.If((count > 0) & (ed.output == 0)) as blinker:
m.d.sync += [
count.eq(count - 1),
ed.input.eq(1)
]
with m.Else():
m.d.sync += [
ed.input.eq(0)
]
m.submodules.edge_to_pulse = ed
return m
if __name__ == "__main__":
dut = Blinker(width = 5)
sim = Simulator(dut)
def strobe():
yield dut.go.eq(1)
yield
yield dut.go.eq(0)
yield
def proc():
assert((yield dut.output) == 0)
yield
yield dut.counter.eq(4)
yield dut.go.eq(1)
yield
yield
yield dut.go.eq(0)
yield
assert((yield dut.output) == 1)
yield
yield
yield
yield
yield
assert((yield dut.output) == 0)
for i in range(30):
yield
sim.add_clock(1/12e6)
sim.add_sync_process(proc)
with sim.write_vcd('blinker.vcd', 'blinker_orig.gtkw'):
sim.run()