Find total number of instances of a module from rtl without using tool (python)

39 views Asked by At

Main goal: List all the instances from a given rtl and sum up Constraint: can not use tool for the purpose Issue: Can not get proper instances from the rtl. I have simple rtl given below:

RTL link

How can i find the toal number of instances from the rtl without using tool?

I tried the below python code:

import re
def count_instances(verilog_code):
    # Regular expression to match Verilog module instantiation
    module_instantiation_pattern = re.compile(r'\s*([\w_]+)\s*([\w_]+)\s*\(', re.IGNORECASE)

    # Dictionary to store instance count for each module
    instance_count = {}

    # Search for module instantiations in the Verilog code
    matches = re.findall(module_instantiation_pattern, verilog_code)
    print(matches)

    # Count instances for each module
    for match in matches:
        module_name = match[0]
        instance_name = match[1]
        

        if module_name not in instance_count:
            instance_count[module_name] = 1
        else:
            instance_count[module_name] += 1

    return instance_count



def main():
    # Read Verilog RTL code from a file
    with open('hdl\\test\\test.v', 'r') as file:
        rtl_code = file.read()

    # Count instances
    instance_count = count_instances(rtl_code)
    

    # Print the results
    print("Instance count for each module:")
    for module_name, count in instance_count.items():
        print(f"{module_name}: {count} instances")

    # Calculate and print the total number of instances
    total_instances = sum(instance_count.values())
    print(f"\nTotal number of instances: {total_instances}")

# Call the main function directly
main()

In the code, I have tried to instantiate with a module name to find instances under that specific module. Then get tha matching tuples from the findall containing matched elements. Then storing them in a dictionary.

Can you suggest anything other than that or modifications?

0

There are 0 answers