-
Notifications
You must be signed in to change notification settings - Fork 4
/
project_01.py
173 lines (120 loc) · 3.5 KB
/
project_01.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
# Boolean Logic
#
# See https://www.nand2tetris.org/project01
from nand import Nand, chip
# SOLVERS: remove this import to get started
from nand.solutions import solved_01
@chip
def Not(inputs, outputs):
in_ = inputs.in_
# SOLVERS: replace this with a Nand
n1 = solved_01.Not(in_=in_)
outputs.out = n1.out
@chip
def Or(inputs, outputs):
a = inputs.a
b = inputs.b
# SOLVERS: replace this with one or more Nands and Nots
n1 = solved_01.Or(a=a, b=b)
outputs.out = n1.out
@chip
def And(inputs, outputs):
a = inputs.a
b = inputs.b
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.And(a=a, b=b)
outputs.out = n1.out
@chip
def Xor(inputs, outputs):
a = inputs.a
b = inputs.b
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.Xor(a=a, b=b)
outputs.out = n1.out
@chip
def Mux(inputs, outputs):
a = inputs.a
b = inputs.b
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.Mux(a=a, b=b, sel=sel)
outputs.out = n1.out
@chip
def DMux(inputs, outputs):
in_ = inputs.in_
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.DMux(in_=in_, sel=sel)
outputs.a = n1.a
outputs.b = n1.b
@chip
def DMux4Way(inputs, outputs):
in_ = inputs.in_
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
# Hint: use sel[0] and sel[1] to extract each bit
n1 = solved_01.DMux4Way(in_=in_, sel=sel)
outputs.a = n1.a
outputs.b = n1.b
outputs.c = n1.c
outputs.d = n1.d
@chip
def DMux8Way(inputs, outputs):
in_ = inputs.in_
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.DMux8Way(in_=in_, sel=sel)
outputs.a = n1.a
outputs.b = n1.b
outputs.c = n1.c
outputs.d = n1.d
outputs.e = n1.e
outputs.f = n1.f
outputs.g = n1.g
outputs.h = n1.h
@chip
def Not16(inputs, outputs):
in_ = inputs.in_
# SOLVERS: replace this with one or more Nands and/or components defined above
# Hint: use outputs.out[0] = ... in_[0] ..., etc. to connect each bit of the output
n1 = solved_01.Not16(in_=in_)
outputs.out = n1.out
@chip
def And16(inputs, outputs):
a = inputs.a
b = inputs.b
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.And16(a=a, b=b)
outputs.out = n1.out
@chip
def Mux16(inputs, outputs):
a = inputs.a
b = inputs.b
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.Mux16(a=a, b=b, sel=sel)
outputs.out = n1.out
@chip
def Mux4Way16(inputs, outputs):
a = inputs.a
b = inputs.b
c = inputs.c
d = inputs.d
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.Mux4Way16(a=a, b=b, c=c, d=d, sel=sel)
outputs.out = n1.out
@chip
def Mux8Way16(inputs, outputs):
a = inputs.a
b = inputs.b
c = inputs.c
d = inputs.d
e = inputs.e
f = inputs.f
g = inputs.g
h = inputs.h
sel = inputs.sel
# SOLVERS: replace this with one or more Nands and/or components defined above
n1 = solved_01.Mux8Way16(a=a, b=b, c=c, d=d, e=e, f=f, g=g, h=h, sel=sel)
outputs.out = n1.out