mirror of
https://github.com/ehw-fit/ariths-gen.git
synced 2025-04-21 06:11:24 +01:00
Started work on exporting adders into .c files. Needs optimization.
This commit is contained in:
parent
7fba0f0aea
commit
f18f6159f5
@ -28,34 +28,115 @@ class bus():
|
|||||||
#jednovstupove
|
#jednovstupove
|
||||||
class not_gate():
|
class not_gate():
|
||||||
def __init__(self, input_a):
|
def __init__(self, input_a):
|
||||||
|
self.gate_type = "not_gate"
|
||||||
if input_a == 1:
|
if input_a == 1:
|
||||||
self.y = 0
|
self.y = 0
|
||||||
else:
|
else:
|
||||||
self.y = 1
|
self.y = 1
|
||||||
|
|
||||||
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, init_run=True):
|
||||||
|
with open(filename,'w') as f:
|
||||||
|
if main_component == True:
|
||||||
|
f.write('#include <stdint.h>\n\n')
|
||||||
|
f.write('uint8_t not_gate(uint8_t a) {\n')
|
||||||
|
f.write(' uint8_t y;\n')
|
||||||
|
f.write(' uint8_t n1 = (a >> 0) & 0x1;\n')
|
||||||
|
f.write(' y = ~n1;\n')
|
||||||
|
f.write(' return y;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
else:
|
||||||
|
if init_run == True:
|
||||||
|
if line_var_cnt != 0:
|
||||||
|
file_object.write(', ')
|
||||||
|
file_object.write(f'n_{wire_id}=0')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
#dvouvstupove
|
#dvouvstupove
|
||||||
class or_gate():
|
class or_gate():
|
||||||
def __init__(self, input_a, input_b):
|
def __init__(self, input_a, input_b):
|
||||||
|
self.gate_type = "or_gate"
|
||||||
if (input_a == 1 or input_b == 1):
|
if (input_a == 1 or input_b == 1):
|
||||||
self.y = 1
|
self.y = 1
|
||||||
else:
|
else:
|
||||||
self.y = 0
|
self.y = 0
|
||||||
|
|
||||||
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, init_run=True):
|
||||||
|
with open(filename,'w') as f:
|
||||||
|
if main_component == True:
|
||||||
|
f.write('#include <stdint.h>\n\n')
|
||||||
|
f.write('uint8_t or_gate(uint8_t a, uint8_t b) {\n')
|
||||||
|
f.write(' uint8_t y;\n')
|
||||||
|
f.write(' uint8_t n1 = (a >> 0) & 0x1;\n')
|
||||||
|
f.write(' uint8_t n2 = (b >> 0) & 0x1;\n')
|
||||||
|
f.write(' y = n1 | n2;\n')
|
||||||
|
f.write(' return y;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
else:
|
||||||
|
#todo wtf
|
||||||
|
if init_run == True:
|
||||||
|
if line_var_cnt != 0:
|
||||||
|
file_object.write(', ')
|
||||||
|
file_object.write(f'n_{wire_id}=0')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class xor_gate():
|
class xor_gate():
|
||||||
def __init__(self, input_a, input_b):
|
def __init__(self, input_a, input_b):
|
||||||
|
self.gate_type = "xor_gate"
|
||||||
if (input_a == 1 and input_b == 0) or (input_a == 0 and input_b == 1):
|
if (input_a == 1 and input_b == 0) or (input_a == 0 and input_b == 1):
|
||||||
self.y = 1
|
self.y = 1
|
||||||
else:
|
else:
|
||||||
self.y = 0
|
self.y = 0
|
||||||
|
|
||||||
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, init_run=True):
|
||||||
|
with open(filename,'w') as f:
|
||||||
|
if main_component == True:
|
||||||
|
f.write('#include <stdint.h>\n\n')
|
||||||
|
f.write('uint8_t xor_gate(uint8_t a, uint8_t b) {\n')
|
||||||
|
f.write(' uint8_t y;\n')
|
||||||
|
f.write(' uint8_t n1 = (a >> 0) & 0x1;\n')
|
||||||
|
f.write(' uint8_t n2 = (b >> 0) & 0x1;\n')
|
||||||
|
f.write(' y = n1 ^ n2;\n')
|
||||||
|
f.write(' return y;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
else:
|
||||||
|
if init_run == True:
|
||||||
|
if line_var_cnt != 0:
|
||||||
|
file_object.write(', ')
|
||||||
|
file_object.write(f'n_{wire_id}=0')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
class and_gate():
|
class and_gate():
|
||||||
def __init__(self, input_a, input_b):
|
def __init__(self, input_a, input_b):
|
||||||
|
self.gate_type = "and_gate"
|
||||||
if input_a == 1 and input_b == 1:
|
if input_a == 1 and input_b == 1:
|
||||||
self.y = 1
|
self.y = 1
|
||||||
else:
|
else:
|
||||||
self.y = 0
|
self.y = 0
|
||||||
|
|
||||||
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, init_run=True):
|
||||||
|
with open(filename,'w') as f:
|
||||||
|
if main_component == True:
|
||||||
|
f.write('#include <stdint.h>\n\n')
|
||||||
|
f.write('uint8_t and_gate(uint8_t a, uint8_t b) {\n')
|
||||||
|
f.write(' uint8_t y;\n')
|
||||||
|
f.write(' uint8_t n1 = (a >> 0) & 0x1;\n')
|
||||||
|
f.write(' uint8_t n2 = (b >> 0) & 0x1;\n')
|
||||||
|
f.write(' y = n1 & n2;\n')
|
||||||
|
f.write(' return y;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
else:
|
||||||
|
if init_run == True:
|
||||||
|
if line_var_cnt != 0:
|
||||||
|
file_object.write(', ')
|
||||||
|
file_object.write(f'n_{wire_id}=0')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
#ARITMETICKE OBVODY
|
#ARITMETICKE OBVODY
|
||||||
class arithmetic_circuit():
|
class arithmetic_circuit():
|
||||||
@ -76,13 +157,31 @@ class arithmetic_circuit():
|
|||||||
return self.out.get_value(index)
|
return self.out.get_value(index)
|
||||||
|
|
||||||
#Export do jinych reprezentaci
|
#Export do jinych reprezentaci
|
||||||
def to_C(self, filename):
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, init_run=True):
|
||||||
|
self.line_wires_cnt = 0
|
||||||
|
if main_component == True:
|
||||||
with open(filename,'w') as f:
|
with open(filename,'w') as f:
|
||||||
f.write('#include <stdint.h>\n\n')
|
f.write('#include <stdint.h>\n\n')
|
||||||
f.write(f'uint64_t {self.circuit_type} () \n')
|
|
||||||
|
if self.circuit_type == 'ha':
|
||||||
|
f.write('uint8_t ha(uint8_t a, uint8_t b) {\n')
|
||||||
|
f.write(' uint8_t out = 0;\n')
|
||||||
|
elif self.circuit_type == 'fa':
|
||||||
|
f.write('uint8_t fa(uint8_t a, uint8_t b, uint8_t cin) {\n')
|
||||||
|
f.write(' uint8_t out = 0;\n')
|
||||||
|
else:
|
||||||
|
f.write(f'uint64_t {self.circuit_type}(uint64_t a, uint64_t b) ')
|
||||||
|
f.write('{\n')
|
||||||
|
f.write(' uint64_t out = 0;\n')
|
||||||
|
|
||||||
|
f.write('uint8_t ')
|
||||||
for component in self.component_list:
|
for component in self.component_list:
|
||||||
print (component)
|
component.export_to_C(filename, False)
|
||||||
|
|
||||||
|
f.write(';\n')
|
||||||
|
|
||||||
|
f.write(' return out;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -108,6 +207,41 @@ class half_adder(arithmetic_circuit):
|
|||||||
def get_sum_out(self):
|
def get_sum_out(self):
|
||||||
return self.out.get(0).value
|
return self.out.get(0).value
|
||||||
|
|
||||||
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, init_run=True):
|
||||||
|
if main_component == True:
|
||||||
|
with open(filename,'w') as f:
|
||||||
|
f.write('#include <stdint.h>\n\n')
|
||||||
|
f.write('uint8_t ha(uint8_t a, uint8_t b) {\n')
|
||||||
|
f.write(' uint8_t out = 0;\n')
|
||||||
|
f.write(' uint8_t ')
|
||||||
|
|
||||||
|
#inicializace vstupu
|
||||||
|
for component in self.component_list:
|
||||||
|
component.export_to_C(filename, False, file_object=f, wire_id=wire_id, line_var_cnt=line_var_cnt)
|
||||||
|
wire_id +=1
|
||||||
|
line_var_cnt +=1
|
||||||
|
|
||||||
|
if line_var_cnt == 8:
|
||||||
|
line_var_cnt = 0
|
||||||
|
f.write(';\n')
|
||||||
|
|
||||||
|
wire_id=0
|
||||||
|
|
||||||
|
#prirazeni hodnot
|
||||||
|
for component in self.component_list:
|
||||||
|
component.export_to_C(filename, False, file_object=f, wire_id=wire_id, init_run=False)
|
||||||
|
wire_id +=1
|
||||||
|
|
||||||
|
|
||||||
|
f.write(';\n')
|
||||||
|
|
||||||
|
f.write(' return out;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class full_adder(arithmetic_circuit):
|
class full_adder(arithmetic_circuit):
|
||||||
def __init__(self, input_a, input_b, carry_in):
|
def __init__(self, input_a, input_b, carry_in):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -145,6 +279,31 @@ class full_adder(arithmetic_circuit):
|
|||||||
def get_sum_out(self):
|
def get_sum_out(self):
|
||||||
return self.out.get(0).value
|
return self.out.get(0).value
|
||||||
|
|
||||||
|
def export_to_C(self, filename, main_component=True, file_object=None, wire_id=0, line_var_cnt=0, mode):
|
||||||
|
if main_component == True:
|
||||||
|
with open(filename,'w') as f:
|
||||||
|
f.write('#include <stdint.h>\n\n')
|
||||||
|
f.write('uint8_t fa(uint8_t a, uint8_t b, uint8_t cin) {\n')
|
||||||
|
f.write(' uint8_t out = 0;\n')
|
||||||
|
f.write(' uint8_t ')
|
||||||
|
|
||||||
|
for component in self.component_list:
|
||||||
|
component.export_to_C(filename, False, file_object=f, wire_id=wire_id, line_var_cnt=line_var_cnt)
|
||||||
|
wire_id +=1
|
||||||
|
line_var_cnt +=1
|
||||||
|
|
||||||
|
if line_var_cnt == 8:
|
||||||
|
f.write(';\n')
|
||||||
|
|
||||||
|
|
||||||
|
f.write(';\n')
|
||||||
|
|
||||||
|
f.write(' return out;\n')
|
||||||
|
f.write('}\n')
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ripple_carry_adder(arithmetic_circuit):
|
class ripple_carry_adder(arithmetic_circuit):
|
||||||
def __init__(self, input_bus_a, input_bus_b):
|
def __init__(self, input_bus_a, input_bus_b):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
@ -155,19 +314,19 @@ class ripple_carry_adder(arithmetic_circuit):
|
|||||||
self.out = bus(N+1)
|
self.out = bus(N+1)
|
||||||
|
|
||||||
#postupne pridani jednobitovych scitacek
|
#postupne pridani jednobitovych scitacek
|
||||||
for i in range(N):
|
for input_index in range(N):
|
||||||
#prvni je polovicni scitacka
|
#prvni je polovicni scitacka
|
||||||
if i == 0:
|
if input_index == 0:
|
||||||
obj_ha = half_adder(input_bus_a.get_value(i), input_bus_b.get_value(i))
|
obj_ha = half_adder(input_bus_a.get_value(input_index), input_bus_b.get_value(input_index))
|
||||||
self.add_component(obj_ha)
|
self.add_component(obj_ha)
|
||||||
self.out.connect(i, obj_ha.get_sum_out())
|
self.out.connect(input_index, obj_ha.get_sum_out())
|
||||||
else:
|
else:
|
||||||
obj_fa = full_adder(input_bus_a.get_value(i), input_bus_b.get_value(i), self.get_previous_component().get_carry_out())
|
obj_fa = full_adder(input_bus_a.get_value(input_index), input_bus_b.get_value(input_index), self.get_previous_component().get_carry_out())
|
||||||
self.add_component(obj_fa)
|
self.add_component(obj_fa)
|
||||||
self.out.connect(i, obj_fa.get_sum_out())
|
self.out.connect(input_index, obj_fa.get_sum_out())
|
||||||
if i == (N-1):
|
if input_index == (N-1):
|
||||||
self.out.connect(N, obj_fa.get_carry_out())
|
self.out.connect(N, obj_fa.get_carry_out())
|
||||||
|
|
||||||
def to_C(self, filename):
|
#Export do jinych reprezentaci
|
||||||
super().to_C(filename)
|
|
||||||
#todo
|
#todo
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user