-
Notifications
You must be signed in to change notification settings - Fork 0
/
non-generalized-using graphviz.txt
69 lines (58 loc) · 1.49 KB
/
non-generalized-using graphviz.txt
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
import graphviz
# Parse Verilog code and extract state information
verilog_code = """
module FiveStagePipeline (
input clk,
input reset,
input [7:0] data_in,
output [7:0] data_out
);
reg [7:0] stage1;
reg [7:0] stage2;
reg [7:0] stage3;
reg [7:0] stage4;
reg [7:0] stage5;
always @(posedge clk or posedge reset) begin
if (reset) begin
stage1 <= 8'b0;
stage2 <= 8'b0;
stage3 <= 8'b0;
stage4 <= 8'b0;
stage5 <= 8'b0;
end
else begin
stage1 <= data_in;
stage2 <= stage1;
stage3 <= stage2;
stage4 <= stage3;
stage5 <= stage4;
end
end
assign data_out = stage5;
endmodule
"""
# Extract states and transitions from parsed Verilog code
states = []
transitions = []
lines = verilog_code.splitlines()
for line in lines:
line = line.strip()
if line.startswith("reg") or line.startswith("wire"):
state_name = line.split()[2].rstrip(";")
states.append(state_name)
elif "<=" in line:
lhs, rhs = line.split("<=")
lhs = lhs.strip()
rhs = rhs.strip(";").strip()
transitions.append((lhs, rhs))
# Create a Directed Graph (Digraph)
G = graphviz.Digraph('finite_state_machine', filename='state_diagram.gv')
G.attr(rankdir='LR', size='8,5')
# Add nodes (states) to the graph
for state in states:
G.node(state)
# Add edges (transitions) to the graph
for transition in transitions:
G.edge(*transition)
# Save the state diagram to a file and view it
G.render(view=True)