|
| 1 | +""" |
| 2 | +Module for flattening the SV design files. |
| 3 | +""" |
| 4 | +import os |
| 5 | +import re |
| 6 | + |
| 7 | +def find_verilog_files(): |
| 8 | + """Find all Verilog (.sv, .v) files in the current directory.""" |
| 9 | + return [f for f in os.listdir('.') if f.endswith(('.sv', '.v'))] |
| 10 | + |
| 11 | +def identify_top_module(file_list): |
| 12 | + """Identify the file containing the top module definition.""" |
| 13 | + top_module_regex = re.compile(r"module\s+top\s*\(") |
| 14 | + for file in file_list: |
| 15 | + with open(file, 'r') as f: |
| 16 | + for line in f: |
| 17 | + if top_module_regex.search(line): |
| 18 | + return file |
| 19 | + return None |
| 20 | + |
| 21 | +def create_flattened_file(top_file, file_list): |
| 22 | + """Create a flattened Verilog file with all file contents.""" |
| 23 | + current_dir = os.path.basename(os.getcwd()) |
| 24 | + output_file_name = f"flattened_{current_dir}.sv" |
| 25 | + |
| 26 | + with open(output_file_name, 'w') as output_file: |
| 27 | + if top_file: |
| 28 | + # Write the top module first |
| 29 | + with open(top_file, 'r') as top_module: |
| 30 | + output_file.write(f"// Content from {top_file}\n") |
| 31 | + output_file.write(top_module.read()) |
| 32 | + output_file.write("\n\n") |
| 33 | + |
| 34 | + # Write the rest of the files |
| 35 | + for file in file_list: |
| 36 | + if file != top_file: |
| 37 | + with open(file, 'r') as verilog_file: |
| 38 | + output_file.write(f"// Content from {file}\n") |
| 39 | + output_file.write(verilog_file.read()) |
| 40 | + output_file.write("\n\n") |
| 41 | + |
| 42 | + print(f"Flattened file created: {output_file_name}") |
| 43 | + |
| 44 | +def main(): |
| 45 | + """Main function to generate the flattened Verilog file.""" |
| 46 | + print("Searching for Verilog files...") |
| 47 | + verilog_files = find_verilog_files() |
| 48 | + |
| 49 | + if not verilog_files: |
| 50 | + print("No Verilog files found in the current directory.") |
| 51 | + return |
| 52 | + |
| 53 | + print("Identifying the top module...") |
| 54 | + top_file = identify_top_module(verilog_files) |
| 55 | + |
| 56 | + if top_file: |
| 57 | + print(f"Top module found in: {top_file}") |
| 58 | + else: |
| 59 | + print("No top module found. Files will be combined in arbitrary order.") |
| 60 | + |
| 61 | + print("Creating flattened file...") |
| 62 | + create_flattened_file(top_file, verilog_files) |
| 63 | + |
| 64 | +if __name__ == "__main__": |
| 65 | + main() |
0 commit comments