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