-
Notifications
You must be signed in to change notification settings - Fork 0
/
gates.py
330 lines (245 loc) · 8.81 KB
/
gates.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from utils import is_n_bit_vector
# elementary logic gates
def AND(x: bool, y: bool) -> bool:
"""And gate."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert isinstance(y, bool), "`y` must be of type `bool`"
# body
out = x and y
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def OR(x: bool, y: bool) -> bool:
"""Or gate."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert isinstance(y, bool), "`y` must be of type `bool`"
# body
out = x or y
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def NOT(x: bool) -> bool:
"""Not gate."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
# body
out = not x
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def NAND(x: bool, y: bool) -> bool:
"""Nand gate."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert isinstance(y, bool), "`y` must be of type `bool`"
# body
out = not (x and y)
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def XOR(x: bool, y: bool) -> bool:
"""Xor gate."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert isinstance(y, bool), "`y` must be of type `bool`"
# body
out = ((not x) and y) or (x and (not y))
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def MUX(x: bool, y: bool, sel: bool) -> bool:
"""Selects between two inputs."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert isinstance(y, bool), "`y` must be of type `bool`"
assert isinstance(sel, bool), "`sel` must be of type `bool`"
# body
out = (
(x and (not y) and (not sel))
or (x and y and (not sel))
or ((not x) and y and sel)
or (x and y and sel)
)
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def DMUX(x: bool, sel: bool) -> tuple[bool, bool]:
"""Channels the input to one out of two outputs."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert isinstance(sel, bool), "`sel` must be of type `bool`"
# body
out = (x and (not sel), x and sel)
# post-conditions
assert (
isinstance(out, tuple)
and len(out) == 2
and all(isinstance(o, bool) for o in out)
), "Output must be 2-tuple of `bool`s"
return out
# 16-bit variants
def NOT16(xs: tuple[bool, ...]) -> tuple[bool, ...]:
"""16-bit Not."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be 16-tuple of `bool`s"
# body
out = tuple(not x for x in xs)
# post-conditions
assert is_n_bit_vector(out, n=16), "Output must be 16-tuple of `bool`s"
return out
def AND16(xs: tuple[bool, ...], ys: tuple[bool, ...]) -> tuple[bool, ...]:
"""16-bit And."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be 16-tuple of `bool`s"
assert is_n_bit_vector(ys, n=16), "`ys` must be 16-tuple of `bool`s"
# body
out = tuple(x and y for x, y in zip(xs, ys))
# post-conditions
assert is_n_bit_vector(out, n=16), "Output must be 16-tuple of `bool`s"
return out
def OR16(xs: tuple[bool, ...], ys: tuple[bool, ...]) -> tuple[bool, ...]:
"""16-bit Or."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be 16-tuple of `bool`s"
assert is_n_bit_vector(ys, n=16), "`ys` must be 16-tuple of `bool`s"
# body
out = tuple(x or y for x, y in zip(xs, ys))
# post-conditions
assert is_n_bit_vector(out, n=16), "Output must be 16-tuple of `bool`s"
return out
def MUX16(xs: tuple[bool, ...], ys: tuple[bool, ...], sel: bool) -> tuple[bool, ...]:
"""Selects between two 16-bit inputs."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be 16-tuple of `bool`s"
assert is_n_bit_vector(ys, n=16), "`ys` must be 16-tuple of `bool`s"
assert isinstance(sel, bool), "`sel` must be of type `bool`"
# body
out = tuple(MUX(x, y, sel) for x, y in zip(xs, ys))
# post-conditions
assert is_n_bit_vector(out, n=16), "Output must be 16-tuple of `bool`s"
return out
# multi-way variants
def OR8WAY(xs: tuple[bool, ...]) -> bool:
"""8-way Or."""
# pre-conditions
assert is_n_bit_vector(xs, n=8), "`xs` must be an 8-tuple of `bool`s"
# body
out = xs[0] or (
xs[1] or (xs[2] or (xs[3] or (xs[4] or (xs[5] or (xs[6] or xs[7])))))
)
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def OR16WAY(xs: tuple[bool, ...]) -> bool:
"""16-way Or."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be an 16-tuple of `bool`s"
# body
out = OR(OR8WAY(xs[:8]), OR8WAY(xs[8:]))
# post-conditions
assert isinstance(out, bool), "Output must be of type `bool`"
return out
def MUX4WAY16(
xs: tuple[bool, ...],
ys: tuple[bool, ...],
zs: tuple[bool, ...],
ws: tuple[bool, ...],
sel: tuple[bool, ...],
) -> tuple[bool, ...]:
"""Selects between four 16-bit inputs."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(ys, n=16), "`ys` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(zs, n=16), "`zs` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(ws, n=16), "`ws` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(sel, n=2), "`sel` must be a 2-tuple of `bool`s"
# body
out = MUX16(
MUX16(
MUX16(
xs,
ys,
(not sel[0]) and sel[1],
),
zs,
sel[0] and (not sel[1]),
),
ws,
sel[0] and sel[1],
)
# post-conditions
assert is_n_bit_vector(out, n=16), "Output must be 16-tuple of `bool`s"
return out
def MUX8WAY16(
xs: tuple[bool, ...],
ys: tuple[bool, ...],
zs: tuple[bool, ...],
ws: tuple[bool, ...],
us: tuple[bool, ...],
vs: tuple[bool, ...],
ms: tuple[bool, ...],
ns: tuple[bool, ...],
sel: tuple[bool, ...],
) -> tuple[bool, ...]:
"""Selects between eight 16-bit inputs."""
# pre-conditions
assert is_n_bit_vector(xs, n=16), "`xs` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(ys, n=16), "`ys` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(zs, n=16), "`zs` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(ws, n=16), "`ws` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(us, n=16), "`us` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(vs, n=16), "`vs` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(ms, n=16), "`ms` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(ns, n=16), "`ns` must be a 16-tuple of `bool`s"
assert is_n_bit_vector(sel, n=3), "`sel` must be a 3-tuple of `bool`s"
# body
out = MUX16(
MUX4WAY16(xs, ys, zs, ws, sel=(sel[1], sel[2])),
MUX4WAY16(us, vs, ms, ns, sel=(sel[1], sel[2])),
sel[0],
)
# post-conditions
assert is_n_bit_vector(out, n=16), "Output must be 16-tuple of `bool`s"
return out
def DMUX4WAY(x: bool, sel: tuple[bool, ...]) -> tuple[bool, bool, bool, bool]:
"""Channels the input to one out of four outputs."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert is_n_bit_vector(sel, n=2), "`sel` must be a 2-tuple of `bool`s"
# body
x1, x2 = DMUX(x, sel[1])
x3, x4 = DMUX(x, sel[1])
out = (
((not sel[0]) and (not sel[1])) and x1,
((not sel[0]) and sel[1]) and x2,
(sel[0] and (not sel[1])) and x3,
(sel[0] and sel[1]) and x4,
)
# post-conditions
assert is_n_bit_vector(out, n=4), "Output must be a 4-tuple of `bool`s"
return out
def DMUX8WAY(
x: bool, sel: tuple[bool, ...]
) -> tuple[bool, bool, bool, bool, bool, bool, bool, bool]:
"""Channels the input to one out of eight outputs."""
# pre-conditions
assert isinstance(x, bool), "`x` must be of type `bool`"
assert is_n_bit_vector(sel, n=3), "`sel` must be a 3-tuple of `bool`s"
# body
x1, x2, x3, x4 = DMUX4WAY(x, sel=(sel[1], sel[2]))
x5, x6, x7, x8 = DMUX4WAY(x, sel=(sel[1], sel[2]))
out = (
(not sel[0]) and x1,
(not sel[0]) and x2,
(not sel[0]) and x3,
(not sel[0]) and x4,
sel[0] and x5,
sel[0] and x6,
sel[0] and x7,
sel[0] and x8,
)
# post-conditions
assert is_n_bit_vector(out, n=8), "Output must be a 8-tuple of `bool`s"
return out