2
2
3
3
def create_element (element_type , element_id , name = None ):
4
4
if element_type == "IO" :
5
- return {"name" : name , "type" : "input" if "input" in name .lower () else "output" , "id" : str (element_id )}
5
+ return {"name" : name , "io" : "input" if "output" in name .lower () else "output" }
6
+ elif element_type == "FlipFlop" :
7
+ return {"id" : element_id , "connections" : [
8
+ {"port" : "clock" },
9
+ {"port" : "output" },
10
+ {"port" : "input" }
11
+ ]}
6
12
return {"id" : element_id , "connections" : []}
7
13
8
- def add_connection (elements , connections , input_id , output_id , timing ):
14
+ def add_connection (elements , connections , input_id , output_id , timing , input_port = "0" , output_port = "0" , input_io = "0" , output_io = "0" ):
15
+ # Determine element types from the dictionaries
16
+ print (input_io )
17
+ print (output_io )
18
+ if input_id in elements ["LUTs" ]:
19
+ input_type = "lut"
20
+ elif input_id in elements ["FlipFlops" ]:
21
+ input_type = "DFF"
22
+ else :
23
+ input_type = elements ["IOs" ].get (input_id , {}).get ("name" )
24
+
25
+ if output_id in elements ["LUTs" ]:
26
+ output_type = "lut"
27
+ elif output_id in elements ["FlipFlops" ]:
28
+ output_type = "DFF"
29
+ else :
30
+ output_type = elements ["IOs" ].get (output_id , {}).get ("name" )
31
+ if input_type is None or output_type is None :
32
+ print (f"Error: One of the elements ({ input_id } , { output_id } ) does not exist." )
33
+ return
34
+
9
35
connection = {
10
- "Input" : {"type" : "lut" if input_id in elements ["LUTs" ] else "DFF" , "id" : str (input_id ), "io" : "output" , "port" : "0" },
11
- "Output" : {"type" : "lut" if output_id in elements ["LUTs" ] else "DFF" , "id" : str (output_id ), "io" : "input" , "port" : "0" },
36
+ "Input" : {
37
+ "type" : input_type ,
38
+ "id" : str (input_id ) if input_type != elements ["IOs" ].get (input_id , {}).get ("name" ) else "0" ,
39
+ "io" : input_io if input_type == "DFF" else "output" ,
40
+ "port" : input_port if input_type == "lut" else "0"
41
+ },
42
+ "Output" : {
43
+ "type" : output_type ,
44
+ "id" : str (output_id ) if output_type != elements ["IOs" ].get (output_id , {}).get ("name" ) else "0" ,
45
+ "io" : output_io if output_type == "DFF" else "input" ,
46
+ "port" : output_port if output_type == "lut" else "0"
47
+ },
12
48
"Timing" : timing
13
49
}
14
50
connections .append (connection )
15
51
16
- def generate_json ():
17
- elements = {
18
- "LUTs" : {
19
- "175" : create_element ("LUT" , "175" ),
20
- "173" : create_element ("LUT" , "173" )
21
- },
22
- "FlipFlops" : {
23
- "0" : create_element ("FlipFlop" , "0" )
24
- },
25
- "IOs" : {
26
- "0" : create_element ("IO" , "0" , "userInput" )
27
- }
28
- }
52
+ def user_input ():
53
+ elements = {"LUTs" : {}, "FlipFlops" : {}, "IOs" : {}}
29
54
connections = []
30
- add_connection (elements , connections , "175" , "173" , 235.697 )
31
- add_connection (elements , connections , "173" , "0" , 158.641 )
55
+
56
+ # Creating elements
57
+ while True :
58
+ element_type = input ("Enter element type (LUT, FlipFlop, IO) or 'done' to finish: " )
59
+ if element_type .lower () == 'done' :
60
+ break
61
+ if element_type != "IO" :
62
+ while True :
63
+ element_id = input ("Enter element ID (numeric): " )
64
+ if element_id .isdigit ():
65
+ break
66
+ else :
67
+ print ("Invalid input. Please enter a numeric ID." )
68
+ key = element_id
69
+ elements [element_type + "s" ][key ] = create_element (element_type , key )
70
+ if element_type == "LUT" :
71
+ # For LUTs, let the user add port definitions
72
+ while True :
73
+ add_port = input (f"Do you want to add a port for { element_type } with ID { element_id } ? (yes/no): " )
74
+ if add_port .lower () != "yes" :
75
+ break
76
+ port_io = input ("Enter port's io (input/output): " )
77
+ port_id = input ("Enter port id: " )
78
+ elements [element_type + "s" ][key ]["connections" ].append ({
79
+ "io" : port_io ,
80
+ "id" : port_id
81
+ })
82
+ elif element_type == "FlipFlop" :
83
+ # For FlipFlops, automatically add the three ports.
84
+ elements [element_type + "s" ][key ]["connections" ] = [
85
+ {"port" : "clock" },
86
+ {"port" : "output" },
87
+ {"port" : "input" }
88
+ ]
89
+ else :
90
+ name = input ("Enter name (for IO element): " )
91
+ key = name
92
+ elements ["IOs" ][key ] = create_element ("IO" , key , name )
93
+
94
+ # Adding global connections between elements
95
+ while True :
96
+ add_conn = input ("Do you want to add a connection between elements? (yes/no): " )
97
+ if add_conn .lower () != "yes" :
98
+ break
99
+
100
+ input_id = input ("Enter input element ID or IO name: " )
101
+ output_id = input ("Enter output element ID or IO name: " )
102
+
103
+ # If the input element is a LUT or a DFF, prompt for its output port.
104
+ input_port = "0"
105
+ input_io = "0"
106
+ if input_id in elements ["LUTs" ]:
107
+ input_port = input ("Enter the port number for the LUT output: " )
108
+ if input_port == "" :
109
+ input_port = "0"
110
+ if input_id in elements ["FlipFlops" ]:
111
+ input_io = input ("Enter the port number for the FlipFlop output: " )
112
+ if input_io == "" :
113
+ input_io = "0"
114
+
115
+ # If the output element is a LUT or a DFF, prompt for its input port.
116
+ output_port = "0"
117
+ output_io = "0"
118
+ if output_id in elements ["LUTs" ]:
119
+ output_port = input ("Enter the port number for the LUT input: " )
120
+ if output_port == "" :
121
+ output_port = "0"
122
+ if output_id in elements ["FlipFlops" ]:
123
+ print ("test" )
124
+ output_io = input ("Enter the port number for the FlipFlop input: " )
125
+ if output_io == "" :
126
+ output_io = "0"
127
+
128
+ try :
129
+ timing = float (input ("Enter timing value: " ))
130
+ except ValueError :
131
+ print ("Invalid timing value. Please enter a numeric value." )
132
+ continue
133
+ add_connection (elements , connections , input_id , output_id , timing , input_port , output_port , input_io , output_io )
32
134
33
135
json_output = {
34
136
"LUTs" : list (elements ["LUTs" ].values ()),
@@ -43,4 +145,4 @@ def generate_json():
43
145
print ("JSON file 'output.json' has been generated." )
44
146
45
147
if __name__ == "__main__" :
46
- generate_json ()
148
+ user_input ()
0 commit comments