-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab7.py
146 lines (120 loc) · 4.05 KB
/
lab7.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
"""
Created on 3/25/21
@author: Avery Cunningham
Pledge: I pledge my honor that I have abided by the Stevens Honor System
CS115 - Lab 7
"""
# lab exercise: circuits in Python
# Study the comments and code provided,
# before doing the exercises.
# Logic gates; should only be applied
# to "bits", i.e., either 0 and 1
def gnot(x):
assert x in [0,1]
return int(not(x))
def gand(x,y):
assert x in [0,1] and y in [0,1]
return x and y
def gor(x,y):
assert x in [0,1] and y in [0,1]
return x or y
# Example: XOR
# Definition x y | x xor y
# 0 0 | 0
# 0 1 | 1
# 1 0 | 1
# 1 1 | 0
# Here is an expression for the 1-rows, using ! for not
# !xy + x!y
# Here is code using only the logic gate functions:
def XOR(x,y):
return gor( gand(gnot(x),y), gand(x,gnot(y)) )
def testXOR():
assert XOR(0,0) == 0
assert XOR(0,1) == 1
assert XOR(1,0) == 1
assert XOR(1,1) == 0
print("testXOR success")
testXOR()
# EXERCISE
# Define this function as a single return using
# only the logic gate functions.
def gor3(x,y,z):
'''Or of three inputs.'''
return gor(gor(x,y), z)
# EXERCISE
# Full adder. See Lecture 6 slide 10
# Implement this as a single return, using only
# the logical gate functions.
# You may also use gor3 or similar helper functions
# that you write using just gates.
# And you may use assigned-once variables:
# think of those as named wires.
def FA(x,y,cin):
'''Assume x, y, and cin are bits.
Return the pair of bits (carry_out,sum) such that
sum is the low bit of x+y+cin and carry_out is
the high bit of x+y+carry_in.'''
s = XOR(XOR(x, y), cin)
c = gor(gand(x,y), gand(cin, XOR(x, y)))
return (c, s)
def FAtest(x,y,c):
'''Compute FA using integer arithmetic.'''
s = (x+y+c) % 2
d = 1 if x+y+c >= 2 else 0
return (d,s)
def testFA():
assert FA(0,0,0) == FAtest(0,0,0)
assert FA(0,1,0) == FAtest(0,1,0)
assert FA(1,1,1) == FAtest(1,1,1)
print("testFA successful on 3 out of 8 cases")
testFA()
# Review slide 12 of Lecture 6 ("A Circuit for Adding") before continuing.
def twoBitAdd(xx,yy):
'''Assume xx and yy are pairs (xt,xo) and (yt,yo) of bits.
Return (cout,(zt,zo)) where (zt,zo) is their two-bit sum
is the carry bit. Note: xo is the one's place and xt is
the two's place. ALERT: use the notation xx[0] to refer to xt,
and xx[1] to refer to xo.'''
(c,zo) = FA(xx[1],yy[1],0)
(d,zt) = FA(xx[0],yy[0],c)
return (d,(zt,zo))
# Notice the assignments to two variables at once,
# which only works if the right-hand side evaluates to a pair.
def test_twoBitAdd():
zero = (0,0)
one = (0,1)
two = (1,0)
three = (1,1)
c,ww = twoBitAdd(one,zero)
assert( ww == (0,1) and c == 0 )
c,ww = twoBitAdd(one,one)
assert( ww == (1,0) and c == 0 )
c,ww = twoBitAdd(three,three)
assert( ww == (1,0) and c == 1 )
print("test_twoBitAdd worked (but incomplete test)")
test_twoBitAdd()
# EXERCISE: implement the following, using gates and/or FA.
# Hint: you might start by defining something like twoBitAdd
# but that also has a carry input.
def fourBitAdd(xxxx,yyyy):
'''Assume xxxx is a quadruple (xe,xf,xt,xo) of four bits,
with xe the high-order bit (i.e., eight's place). Likewise
yyyy. Return (c,zzzz) where zzzz is their four-bit sum
and c is the carry.'''
(c,sum1) = FA(xxxx[3],yyyy[3],0)
(c,sum2) = FA(xxxx[2],yyyy[2],c)
(c,sum3) = FA(xxxx[1],yyyy[1],c)
(c,sum4) = FA(xxxx[0],yyyy[0],c)
return (c,(sum4,sum3,sum2,sum1))
# EXERCISE: implement the following.
def test_fourBitAdd():
'''at least four test cases'''
assert fourBitAdd((0,0,0,0), (0,0,0,0)) == (0, (0,0,0,0))
assert fourBitAdd((0,0,0,1), (0,0,0,0)) == (0, (0,0,0,1))
assert fourBitAdd((0,0,1,0), (0,0,0,0)) == (0, (0,0,1,0))
assert fourBitAdd((1,0,0,0), (1,0,0,0)) == (1, (0,0,0,0))
assert fourBitAdd((1,1,1,1), (0,1,1,0)) == (1, (0,1,0,1))
assert fourBitAdd((1,1,1,1), (1,1,1,1)) == (1, (1,1,1,0))
print("test_fourBitBitAdd worked")
test_fourBitAdd()