-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdynamic_adder.py
More file actions
38 lines (22 loc) · 1010 Bytes
/
Copy pathdynamic_adder.py
File metadata and controls
38 lines (22 loc) · 1010 Bytes
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
from component import Component
from adder import Adder
class DynamicAdder(Component):
def __init__(self, bits, input_data):
self.INPUT_LENGTH = bits * 2 + 1
self.bits = bits
super().__init__()
self.adders = [Adder([]) for _ in range(bits)]
self.update(input_data)
def update(self, input_data):
self.clear_input(input_data)
#-----------Logic--------#
self.output.clear()
#first one has to get carry from input
self.adders[0].update([self.input[self.bits-1],self.input[self.bits*2-1],self.input[self.bits*2]])
self.output.append(self.adders[0].output[0])
for i in range(1,self.bits):
self.adders[i].update([self.input[self.bits-1-i],self.input[self.bits*2-1-i],self.adders[i-1].output[1]])
self.output.append(self.adders[i].output[0])
self.output.reverse()
#carry bit
self.output.append(self.adders[self.bits-1].output[1])