Skip to content

Commit 9a5f2de

Browse files
committed
UPDATE: json-creater.py
I had pushed the wrong version... So here is the right reason
1 parent 775640a commit 9a5f2de

File tree

2 files changed

+254
-20
lines changed

2 files changed

+254
-20
lines changed

src/json_creator/json-creator.py

+122-20
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,135 @@
22

33
def create_element(element_type, element_id, name=None):
44
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+
]}
612
return {"id": element_id, "connections": []}
713

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+
935
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+
},
1248
"Timing": timing
1349
}
1450
connections.append(connection)
1551

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": {}}
2954
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)
32134

33135
json_output = {
34136
"LUTs": list(elements["LUTs"].values()),
@@ -43,4 +145,4 @@ def generate_json():
43145
print("JSON file 'output.json' has been generated.")
44146

45147
if __name__ == "__main__":
46-
generate_json()
148+
user_input()

src/json_creator/output.json

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
{
2+
"LUTs": [
3+
{
4+
"id": "140",
5+
"connections": [
6+
{
7+
"io": "input",
8+
"id": "1"
9+
},
10+
{
11+
"io": "input",
12+
"id": "2"
13+
},
14+
{
15+
"io": "output",
16+
"id": "0"
17+
}
18+
]
19+
}
20+
],
21+
"FlipFlops": [
22+
{
23+
"id": "1",
24+
"connections": [
25+
{
26+
"port": "clock"
27+
},
28+
{
29+
"port": "output"
30+
},
31+
{
32+
"port": "input"
33+
}
34+
]
35+
}
36+
],
37+
"IOs": [
38+
{
39+
"name": "Async_reset",
40+
"io": "output"
41+
},
42+
{
43+
"name": "userInput",
44+
"io": "output"
45+
},
46+
{
47+
"name": "userOutput",
48+
"io": "input"
49+
},
50+
{
51+
"name": "Clock",
52+
"io": "output"
53+
}
54+
],
55+
"Connections": [
56+
{
57+
"Input": {
58+
"type": "userInput",
59+
"id": "0",
60+
"io": "output",
61+
"port": "0"
62+
},
63+
"Output": {
64+
"type": "lut",
65+
"id": "140",
66+
"io": "input",
67+
"port": "2"
68+
},
69+
"Timing": 235.697
70+
},
71+
{
72+
"Input": {
73+
"type": "Async_reset",
74+
"id": "0",
75+
"io": "output",
76+
"port": "0"
77+
},
78+
"Output": {
79+
"type": "lut",
80+
"id": "140",
81+
"io": "input",
82+
"port": "1"
83+
},
84+
"Timing": 617.438
85+
},
86+
{
87+
"Input": {
88+
"type": "Clock",
89+
"id": "0",
90+
"io": "output",
91+
"port": "0"
92+
},
93+
"Output": {
94+
"type": "DFF",
95+
"id": "1",
96+
"io": "clock",
97+
"port": "0"
98+
},
99+
"Timing": 10.0
100+
},
101+
{
102+
"Input": {
103+
"type": "DFF",
104+
"id": "1",
105+
"io": "output",
106+
"port": "0"
107+
},
108+
"Output": {
109+
"type": "userOutput",
110+
"id": "0",
111+
"io": "input",
112+
"port": "0"
113+
},
114+
"Timing": 1079.77
115+
},
116+
{
117+
"Input": {
118+
"type": "lut",
119+
"id": "140",
120+
"io": "output",
121+
"port": "0"
122+
},
123+
"Output": {
124+
"type": "DFF",
125+
"id": "1",
126+
"io": "input",
127+
"port": "0"
128+
},
129+
"Timing": 96.0
130+
}
131+
]
132+
}

0 commit comments

Comments
 (0)