xref: /XiangShan/scripts/sram_size_collect.py (revision 7a8a1a5d6c961d6844a3dc8f70798d0abcfe47ac)
1*7a8a1a5dSGuokai Chenimport os
2*7a8a1a5dSGuokai Chenimport re
3*7a8a1a5dSGuokai Chenimport argparse
4*7a8a1a5dSGuokai Chenfrom datetime import datetime
5*7a8a1a5dSGuokai Chen
6*7a8a1a5dSGuokai Chen# Define a function to extract information from a given Verilog file
7*7a8a1a5dSGuokai Chendef extract_info_from_verilog(file_path):
8*7a8a1a5dSGuokai Chen    with open(file_path, 'r') as file:
9*7a8a1a5dSGuokai Chen        content = file.read()
10*7a8a1a5dSGuokai Chen
11*7a8a1a5dSGuokai Chen    # Use regular expressions to extract information from the comment line
12*7a8a1a5dSGuokai Chen    match = re.search(r'// name:array_(\d+)_ext depth:(\d+) width:(\d+) masked:(\w+) maskGran:(\d+) maskSeg:(\d+)', content)
13*7a8a1a5dSGuokai Chen
14*7a8a1a5dSGuokai Chen    if match:
15*7a8a1a5dSGuokai Chen        x = int(match.group(1))
16*7a8a1a5dSGuokai Chen        y = int(match.group(2))
17*7a8a1a5dSGuokai Chen        z = int(match.group(3))
18*7a8a1a5dSGuokai Chen        t = match.group(4) == 'true'
19*7a8a1a5dSGuokai Chen        m = int(match.group(5))
20*7a8a1a5dSGuokai Chen        n = int(match.group(6))
21*7a8a1a5dSGuokai Chen
22*7a8a1a5dSGuokai Chen        return (x, y, z, m, n, t)
23*7a8a1a5dSGuokai Chen    else:
24*7a8a1a5dSGuokai Chen        return None
25*7a8a1a5dSGuokai Chen
26*7a8a1a5dSGuokai Chen# Create an argument parser
27*7a8a1a5dSGuokai Chenparser = argparse.ArgumentParser(description="Process Verilog files in a directory")
28*7a8a1a5dSGuokai Chen
29*7a8a1a5dSGuokai Chen# Add an argument for the directory path
30*7a8a1a5dSGuokai Chenparser.add_argument("directory", help="Path to the directory containing Verilog files")
31*7a8a1a5dSGuokai Chen
32*7a8a1a5dSGuokai Chen# Parse the command line arguments
33*7a8a1a5dSGuokai Chenargs = parser.parse_args()
34*7a8a1a5dSGuokai Chen
35*7a8a1a5dSGuokai Chen# Get the last level directory name from the input path
36*7a8a1a5dSGuokai Chenlast_dir = os.path.basename(os.path.normpath(args.directory))
37*7a8a1a5dSGuokai Chen
38*7a8a1a5dSGuokai Chen# Generate the output file name with the current time and last level directory name
39*7a8a1a5dSGuokai Chenoutput_file_name = f"{last_dir}_{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.txt"
40*7a8a1a5dSGuokai Chen
41*7a8a1a5dSGuokai Chen# List to store extracted information
42*7a8a1a5dSGuokai Cheninfo_list = []
43*7a8a1a5dSGuokai Chen
44*7a8a1a5dSGuokai Chen# Iterate through the files in the specified directory
45*7a8a1a5dSGuokai Chenfor filename in os.listdir(args.directory):
46*7a8a1a5dSGuokai Chen    if filename.startswith("array_") and filename.endswith("_ext.v"):
47*7a8a1a5dSGuokai Chen        file_path = os.path.join(args.directory, filename)
48*7a8a1a5dSGuokai Chen        info = extract_info_from_verilog(file_path)
49*7a8a1a5dSGuokai Chen        if info is not None:
50*7a8a1a5dSGuokai Chen            info_list.append(info)
51*7a8a1a5dSGuokai Chen
52*7a8a1a5dSGuokai Chen# Sort the list of tuples based on Y, Z, M, N, and T (excluding X)
53*7a8a1a5dSGuokai Cheninfo_list.sort(key=lambda tup: tup[1:])
54*7a8a1a5dSGuokai Chen
55*7a8a1a5dSGuokai Chen# Define the order for printing the information
56*7a8a1a5dSGuokai Chenoutput_order = ["X", "Y", "Z", "M", "N", "T"]
57*7a8a1a5dSGuokai Chen
58*7a8a1a5dSGuokai Chen# Write the information to the output file
59*7a8a1a5dSGuokai Chenwith open(output_file_name, 'w') as output_file:
60*7a8a1a5dSGuokai Chen    for info in info_list:
61*7a8a1a5dSGuokai Chen        info_dict = dict(zip(output_order, info))
62*7a8a1a5dSGuokai Chen        output_file.write(f"Y:{info_dict['Y']} Z:{info_dict['Z']} M:{info_dict['M']} N:{info_dict['N']} T:{info_dict['T']}\n")
63*7a8a1a5dSGuokai Chen
64*7a8a1a5dSGuokai Chenprint(f"File info has been written to {output_file_name}")
65