-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tdc_simple.py
128 lines (102 loc) · 2.89 KB
/
tdc_simple.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from amaranth import *
from amaranth.sim import *
# output format:
# | falling | rising | time |
# |---------|--------|------|
# | 33 | 32 | 31 0 |
class TdcSimple(Elaboratable):
def __init__(self, name, domain="fast"):
# in
self.clk = ClockSignal(domain)
self.enable = Signal()
self.input = Signal()
self.time = Signal(32)
self.name = name
# out
self.output = Signal(32 + 2)
self.rdy = Signal()
self._domain = domain
# internal
self.sample = Signal()
self.falling = Signal()
self.rising = Signal()
self.prev = Signal()
def elaborate(self, platform):
m = Module()
with m.If(self.enable == 1):
m.d[self._domain] += [
self.sample.eq(self.input),
self.prev.eq(self.sample)
]
with m.Else():
m.d[self._domain] += [
self.sample.eq(0),
self.prev.eq(0)
]
m.d.comb += [
self.falling.eq((self.prev == 1) & (self.sample == 0)),
self.rising.eq((self.prev == 0) & (self.sample == 1))
]
with m.If((self.falling == 1) | (self.rising == 1)):
m.d[self._domain] += [
self.output.eq(Cat(Cat(Cat(self.time), self.rising),
self.falling)),
self.rdy.eq(1)
]
with m.Else():
m.d[self._domain] += [
self.output.eq(C(0, unsigned(34))),
self.rdy.eq(0)
]
return m
if __name__ == "__main__":
dut = TdcSimple("test")
i0 = Signal()
en = Signal()
t = Signal(32)
out = Signal(34)
m = Module()
m.domains += ClockDomain("fast")
m.domains += ClockDomain("sync")
m.domains += ClockDomain("what")
m.submodules.dut = dut
m.d.comb += [
dut.enable.eq(en),
dut.input.eq(i0),
dut.time.eq(t),
out.eq(dut.output)
]
sim = Simulator(m)
stop = 200
def time():
for i in range(stop):
yield t.eq(t + 1)
yield
def proc():
yield
def pulse(steps):
for i in range(steps):
yield i0.eq(1)
yield
yield i0.eq(0)
yield
def pause(steps):
for i in range(steps):
yield
def input():
yield en.eq(0)
for i in range(5):
yield from pulse(i)
yield from pause(5)
yield en.eq(1)
for i in range(5):
yield from pulse(i)
yield from pause(5)
sim.add_clock(1/10e6, domain="what")
sim.add_clock(1/1e6, domain="fast")
sim.add_sync_process(time, domain="fast")
sim.add_clock(1/1e6)
sim.add_sync_process(proc)
sim.add_sync_process(input)
with sim.write_vcd("tdc_simple.vcd", "tdc_simple_orig.gtkw"):
sim.run()