This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
IC_7416374.py
264 lines (191 loc) · 7.45 KB
/
IC_7416374.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""Module containing stuff made out of 7416374 16-bit registers."""
# Disable pylint's "your name is too short" warning.
# pylint: disable=C0103
# Disable protected access warnings
# pylint: disable=W0212
from typing import List, Tuple
from nmigen import Signal, Module, Elaboratable, Array, ClockSignal, ResetSignal, ClockDomain
from nmigen.build import Platform
from nmigen.asserts import Assert, Assume, Past, Rose, Fell
from IC_7416244 import IC_mux32
from util import main
class IC_7416374(Elaboratable):
"""Contains logic for a 7416374 16-bit register."""
def __init__(self, clk: str, ext_init: bool = False):
"""Creats a 7416374 register.
clk is the name of the domain the register will clock on.
"""
attrs = [] if not ext_init else [("uninitialized", "")]
self.clk = clk
self.d = Signal(16)
self.n_oe = Signal()
self.q = Signal(16)
self._q = Signal(16, attrs=attrs)
def elaborate(self, _: Platform) -> Module:
"""Implements the logic of the register."""
m = Module()
m.d[self.clk] += self._q.eq(self.d)
m.d.comb += self.q.eq(0)
with m.If(~self.n_oe):
m.d.comb += self.q.eq(self._q)
return m
@classmethod
def formal(cls) -> Tuple[Module, List[Signal]]:
m = Module()
ph = ClockDomain("ph")
clk = ClockSignal("ph")
m.domains += ph
m.d.sync += clk.eq(~clk)
s = IC_7416374(clk="ph")
m.submodules += s
with m.If(s.n_oe):
m.d.comb += Assert(s.q == 0)
with m.If(~s.n_oe & Rose(clk)):
m.d.comb += Assert(s.q == Past(s.d))
with m.If(~s.n_oe & Fell(clk) & ~Past(s.n_oe)):
m.d.comb += Assert(s.q == Past(s.q))
sync_clk = ClockSignal("sync")
sync_rst = ResetSignal("sync")
# Make sure the clock is clocking
m.d.comb += Assume(sync_clk == ~Past(sync_clk))
# Don't want to test what happens when we reset.
m.d.comb += Assume(~sync_rst)
m.d.comb += Assume(~ResetSignal("ph"))
return m, [sync_clk, sync_rst, s.n_oe, s.q, s.d]
class IC_reg32(Elaboratable):
"""A 32-bit register from a pair of 16-bit registers."""
def __init__(self, clk: str, ext_init: bool = False):
self.clk = clk
self.d = Signal(32)
self.n_oe = Signal()
self.q = Signal(32)
self.ext_init = ext_init
def elaborate(self, _: Platform) -> Module:
"""Implements the logic of the register."""
m = Module()
regs = [IC_7416374(self.clk, self.ext_init),
IC_7416374(self.clk, self.ext_init)]
m.submodules += regs
for i in range(2):
m.d.comb += regs[i].n_oe.eq(self.n_oe)
m.d.comb += regs[i].d.eq(self.d[i*16:i*16+16])
m.d.comb += self.q[i*16:i*16+16].eq(regs[i].q)
return m
class IC_reg32_with_mux(Elaboratable):
"""A 32-bit register that multiplexes any of its inputs, or itself.
There is no output enable input.
"""
def __init__(self, clk: str, N: int, ext_init: bool = False, faster: bool = False):
"""Constructs a 32-bit register with multiplexed inputs.
Setting faster will make formal verification somewhat faster, since there aren't
so many nested submodules and extra logic.
"""
self.N = N
self.clk = clk
self.d = Array([Signal(32, name=f"d{i}") for i in range(N)])
self.n_sel = Signal(N)
self.q = Signal(32)
self._ext_init = ext_init
self._faster = faster
def elaborate(self, _: Platform) -> Module:
"""The logic."""
m = Module()
if self._faster:
attrs = [] if not self._ext_init else [("uninitialized", "")]
_q = Signal(32, attrs=attrs)
m.d.comb += self.q.eq(_q)
c = m.d[self.clk]
for i in range(self.N):
with m.If(~self.n_sel[i]):
c += _q.eq(self.d[i])
return m
r = IC_reg32(self.clk, self._ext_init)
mux = IC_mux32(self.N + 1)
m.submodules += [r, mux]
m.d.comb += r.n_oe.eq(0)
m.d.comb += r.d.eq(mux.y)
m.d.comb += self.q.eq(r.q)
m.d.comb += mux.n_sel.eq(self.n_sel)
m.d.comb += mux.n_sel[-1].eq(~self.n_sel != 0)
for i in range(self.N):
m.d.comb += mux.a[i].eq(self.d[i])
m.d.comb += mux.a[-1].eq(r.q)
return m
@classmethod
def formal(cls) -> Tuple[Module, List[Signal]]:
m = Module()
ph = ClockDomain("ph")
clk = ClockSignal("ph")
m.domains += ph
m.d.sync += clk.eq(~clk)
s = IC_reg32_with_mux(clk="ph", N=2, faster=True)
m.submodules += s
sync_clk = ClockSignal("sync")
sync_rst = ResetSignal("sync")
with m.If(Rose(clk)):
with m.Switch(~Past(s.n_sel)):
with m.Case(0b11):
m.d.comb += Assert(0)
with m.Case(0b01):
m.d.comb += Assert(s.q == Past(s.d[0]))
with m.Case(0b10):
m.d.comb += Assert(s.q == Past(s.d[1]))
with m.Default():
m.d.comb += Assert(s.q == Past(s.q))
# Make sure the clock is clocking
m.d.comb += Assume(sync_clk == ~Past(sync_clk))
# Don't want to test what happens when we reset.
m.d.comb += Assume(~sync_rst)
m.d.comb += Assume(~ResetSignal("ph"))
m.d.comb += Assume(s.n_sel != 0)
return m, [sync_clk, sync_rst, s.d[0], s.d[1], s.n_sel, s.q]
class IC_reg32_with_load(Elaboratable):
"""A 32-bit register that loads or retains its value."""
def __init__(self, clk):
self.clk = clk
self.d = Signal(32)
self.n_oe = Signal()
self.load = Signal()
self.q = Signal(32)
def elaborate(self, _: Platform) -> Module:
"""Implements the logic of the register."""
m = Module()
reg = IC_reg32(self.clk)
mux = IC_mux32(2)
m.submodules += [reg, mux]
m.d.comb += reg.n_oe.eq(self.n_oe)
m.d.comb += mux.a[0].eq(reg.q)
m.d.comb += mux.a[1].eq(self.d)
m.d.comb += mux.n_sel[0].eq(self.load)
m.d.comb += mux.n_sel[1].eq(~self.load)
m.d.comb += reg.d.eq(mux.y)
m.d.comb += self.q.eq(reg.q)
return m
@classmethod
def formal(cls) -> Tuple[Module, List[Signal]]:
m = Module()
ph = ClockDomain("clk")
clk = ClockSignal("ph")
m.domains += ph
m.d.sync += clk.eq(~clk)
s = IC_reg32_with_load(clk="ph")
m.submodules += s
sync_clk = ClockSignal("sync")
sync_rst = ResetSignal("sync")
with m.If(s.n_oe):
m.d.comb += Assert(s.q == 0)
with m.Else():
with m.If(~Past(s.n_oe) & ~Past(s.load)):
m.d.comb += Assert(s.q == Past(s.q))
with m.If(Past(s.load) & Rose(clk)):
m.d.comb += Assert(s.q == Past(s.d))
with m.If(~Past(s.n_oe) & Fell(clk)):
m.d.comb += Assert(s.q == Past(s.q))
# Make sure the clock is clocking
m.d.comb += Assume(sync_clk == ~Past(sync_clk))
# Don't want to test what happens when we reset.
m.d.comb += Assume(~sync_rst)
m.d.comb += Assume(~ResetSignal("ph"))
return m, [sync_clk, sync_rst, s.n_oe, s.load, s.d]
if __name__ == "__main__":
main(IC_reg32_with_mux)