-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPGAinitializer.py
More file actions
306 lines (284 loc) · 8.95 KB
/
FPGAinitializer.py
File metadata and controls
306 lines (284 loc) · 8.95 KB
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
import numpy as np
import sys #exit
from os.path import exists
from os.path import getsize
from FPGAstructure import *
from logic_synthesizer import *
import synthesis_engine as sy
import synthesis_engine6 as sy6
# This program sets up the FPGA based on user input
def check_file(filename):
file_exists = exists(filename)
return file_exists
def get_LUTS_num():
while True:
LUTS_num=input("Number of LUTS: ")
if LUTS_num.isnumeric():
if int(LUTS_num)>0:
LUTS_num = int(LUTS_num)
break
print("Input must be a integer that is greater than 0.")
return LUTS_num
def get_LUT_type():
while True:
print("What type of LUTs are you using?")
print("4: 4-input 6: 6-input")
LUTS_type=input()
if LUTS_type == '4':
LUTS_type=4
break
elif LUTS_type == '6':
LUTS_type=6
break
else:
print("Invalid input. Please pick option A or B.")
return LUTS_type
def get_connectivity():
while True:
print("What kind of connectivity between the LUTs?")
print("A: Fully-connected B:Partially-connected (file required)")
connect_type=input()
if connect_type.upper()=='A':
connect_type=9
break
elif connect_type.upper()=='B':
#connect_type=2
connect_type=9
print("This program currently doesn't support partial connection between LUTS.") #TODO erase once done
break
else:
print("Invalid input. Please pick option A or B")
return connect_type
def specify_connections(connect_type): #TODO this function
if connect_type == 2:
connections = 1
#while True:
# filename = input("File Name: ")
# if check_file(filename):
# with open(filename, 'r') as file: #TODO fix this assumption
# line = file.readline() #must be on one line
# #TODO any processing
# connections = line
# break
# else:
# print("That file doesn't seem to exist.")
else:
#print("Fully connected")
connections = 1
return connections
def get_IO():
while True:
input_num=input("How many inputs? ")
output_num=input("How many outputs? ")
if input_num.isnumeric() & output_num.isnumeric():
if (int(input_num) > 0) & (int(output_num) > 0):
input_num = int(input_num)
output_num = int(output_num)
break
print("Inputs must be integers greater than 0")
return input_num, output_num
def get_equations():
equations = []
while True:
print("Please input .eqns file for the logic expressions")
eqns_file_path=input()
if exists(eqns_file_path):
if eqns_file_path[-5:]==".eqns": #check file extension
with open(eqns_file_path, 'r') as file:
lines_list=file.readlines()
break
else:
print("Please choose a eqns file.")
break
else:
print("File {} doesnt exist.".format(eqns_file_path))
print("Please try again.")
for line in lines_list:
if line.startswith('#'):
pass
else:
eqn = line.replace(" ","")
eqn = eqn.strip() #remove \n
equations.append(eqn)
return equations
def basic_prompt(word):
while True:
print("Do you want the FPGA to be {}?".format(word))
print("A: Yes B: No")
user_input = input()
if user_input.upper() == 'A':
user_input = True
break
elif user_input.upper() == 'B':
user_input = False
break
else:
print("Please pick option A or B.")
return user_input
#integration
def craft_new_FPGA( LUTS_num, LUT_type, connect_type, connections, input_num, output_num, equations): #TODO partial equations
#optimizes equations
minimized=basic_prompt("minimized")
if minimized:
equations_arrays=textToArray(equations)
equations=minimize_equations(equations_arrays)
equations_backup = equations[:] #makes a deep copy just in case
factored=basic_prompt("factored")
if factored:
equations=toFactorer(equations) #changes string format
equations=mutual_factor(equations)
subs=basic_prompt("substituted")
if subs:
equations=substituition(equations)
equations=toSplitter(equations) #changes string format
if LUT_type == 4:
print("LUT4")
LUTS = sy.assign_LUTs(equations,LUTS_num)
LUTS = sy.get_LUTS_global()
elif LUT_type == 6:
print("LUT6")
LUTS = sy6.assign_LUTs(equations, LUTS_num)
LUTS = sy6.get_LUTS_global()
#TODO partial equations here or below..
#At this point, you have the final equations for assignment
if LUTS_num < len(LUTS):
print("This FPGA doesn't have enough LUTS. Using minimized equations instead.")
equations = equations_backup
if LUT_type == 4:
LUTS = sy.assign_LUTs(equations, LUTS_num)
LUTS = sy.get_LUTS_global()
elif LUT_type == 6:
LUTS = sy6.assign_LUTs(equations, LUTS_num)
LUTS = sy6.get_LUTS_global()
if LUTS_num < len(LUTS):
print("This FPGA still doesn't have enough LUTS.")
print("Ending program...")
sys.exit()
FPGA1 = FPGA()
FPGA1.set_LUTS(LUTS)
#if connect_type == 2: #partial
# FPGA1.set_connections(connections) #TODO
FPGA1.updateOutputs()
FPGA1.updateInputs()
return FPGA1
#TODO assuming bitstream parser returns a list of lut objects
def recraft_FPGA(luts):
FPGA1 = FPGA()
FPGA1.set_LUTS(luts)
FPGA1.updateOutputs()
FPGA1.updateInputs()
return FPGA1
def output_prompt():
print("FPGA Display Options:")
print("[1] Show all LUT assignments")
print("[2] Show specific LUT assignment")
print("[3] Show internal connections")
print("[4] Show external input assignments")
print("[5] Show external output assignments")
print("[6] Craft bitstream of current FPGA")
print("[7] Show resource allocation")
print("[8] Show FPGA visually")
print("[h] Show this prompt again")
print("[q] Quit program")
def main():
useBitstream = False
print("Setting up FPGA...")
while True:
print("Do you have a existing design in the form of a bitstream file?")
print("A: Yes B: No")
bitstream_Exist=input()
if bitstream_Exist.upper()=='A':
useBitstream=True
bitstream_file=input("Bitstream Filename: ")
bitstream_file = bitstream_file + ".bits"
if check_file(bitstream_file):
print("File exists.")
break
else:
print("Sorry that file doesn't seem to exist")
elif bitstream_Exist.upper()=='B':
print("Creating a new FPGA...")
break
else:
print("Invalid input. Please pick option A or B.")
#===============Recraft FPGA
if useBitstream:
LUTS = sy.build_from_bitstream(bitstream_file) #only 4 input :(
LUTS = sy.get_LUTS_global()
print("LUTS")
print(LUTS)
sy.print_LUTs()
for i in range(len(LUTS)):
LUT = LUTS[i]
LUT.output = str(i)
#print(LUT.output)
FPGA1 = recraft_FPGA(LUTS)
#================MAKE NEW FPGA via USER INPUT
else:
LUTS_num = get_LUTS_num()
LUT_type = get_LUT_type()
connect_type = get_connectivity()
connections = specify_connections(connect_type)
input_num, output_num = get_IO()
equations = get_equations()
FPGA1 = craft_new_FPGA(LUTS_num, LUT_type, connect_type, connections, input_num, output_num, equations)
bitstream_file = ""
#================User output
key = 'i'
output_prompt()
while key != 'q':
key=input("What do you want to do?\n")
if key=='1': # Show all LUT assignments
LUTS = FPGA1.get_LUTS()
for i in range(len(LUTS)):
print("LUT{}: {}".format(i,LUTS[i]))
elif key == '2': # Show specific LUT assignment
num = input("Which LUT do you want to see? 1-{}\n".format(LUTS_num))
if int(num) <= FPGA1.get_num_luts(): #makesure no index error
LUT = FPGA1.get_LUT(int(num))
print(LUT)
else:
print("LUT{} doesn't exist".format(num))
elif key == '3': # Show internal connections
conn = FPGA1.get_connections()
print(conn)
elif key == '4': # Show external inputs
ex_inputs = FPGA1.get_inputs()
for i in range(len(ex_inputs)):
print("I{}: {}".format(i, ex_inputs[i]))
elif key == '5': # Show external outputs
ex_outputs = FPGA1.get_outputs()
for i in range(len(ex_outputs)):
print("O{}: {}".format(i, ex_outputs[i]))
elif key == '6': # Craft bitstream
bitstream_file = input("Please put a name for the bitstream file.\n")
bitstream_file = bitstream_file + ".bits"
sy.write_bitstream(bitstream_file)
print("Saved as {}".format(bitstream_file))
elif key == '7': # Show resource allocation
used_LUTS = FPGA1.get_num_luts() #TODO get total number of luts
used_connections = FPGA1.get_num_internal_connections()
#total_connection = FPGA1.get_lut_size() * used_LUTS
#print("{} {} {}".format(used_LUTS, used_connections, total_connection))
if 'LUTS_num' in locals():
print("% of LUT: {:.2f}".format((used_LUTS/LUTS_num) * 100)) #luts / connections of nodes
total_connection = LUT_type * LUTS_num
else:
print("% of LUT: {:.2f} used".format((used_LUTS/26) * 100))
total_connection = FPGA1.get_lut_size() * 26
print("% of connections: {:.2f}".format((used_connections/total_connection) * 100)) #number of connections
bitstream_exists=check_file(bitstream_file)
if bitstream_exists:
print("Total memory required: {}".format(getsize(bitstream_file))) #size of bitstream
else:
print("-- Create bitstream to find total memory required --")
elif key == '8': # Show FPGA visually
FPGA1.show_FPGA()
elif key == 'h':
output_prompt()
elif key == 'q': #quit
sys.exit()
else:
print("Enter h to see prompts again.")
if __name__=="__main__":
main()