############################################################################### # # Usage : python netlistsort.py myfile.net # Ouput : myfile.net.nls # # net list line is of the form: # NET 'CNFG_CCLK' U100-Y19 U150-5 # comment # # Step I = read the file and build the nets # # - parse each Net List line # - if line does NOT start with word 'NET' : skip line # - extract second word on the line : this is the current net name # - if net has not already been seen : create new entry # - extract next word in the line untill '#' or end of line # - add this word (=new connection) to current net name # # Step II = write output file of sorted nets # # - print all nets in order of net size # - single point nets first, then two point nets, then three... # - for each section list net names in alphabetical order # ############################################################################### ############################################################################### # Created : 12 Jul 04 - V1.0 # Modified : 14 Jul 04 - V1.1 fix bug in reporting total number of nets ############################################################################### netlistsort_version = "V1.1" ############################################################################### print '.' print ' Net List Sorter ' print ' -------------' print ' netlistsort ' + netlistsort_version print ' -------------' ############################################################################### import sys import os import string import time ####################################### # Some constant values ####################################### flag_comment = '#' #wait_on_exit = '' # un-comment this line to wait before exiting ####################################### # Initialize global variables ####################################### netlist_file_path = './' netlist_file_name = '' netlist_dict = {} netsize_dict = {} ############################################################################ # close all possibly opened files on Exit # Make exit actions a function so that we can use it for aborting from anywhere ############################################################################ def Done(): #---------- print '.' if ( globals().has_key( 'netlist_file_object' ) ) : netlist_file_object.close () if ( globals().has_key( 'sorted_file' ) ) : sorted_file_object.close () print '.' print 'Done.' if ( globals().has_key( 'wait_on_exit' ) ) : raw_input ( '' ) sys.exit() ####################################### # Find Net List file, if it wasn't passed as argument ####################################### if ( len (sys.argv) > 1 ) : netlist_file = sys.argv[1] else : #Ask for Net List file we are supposed to parse #---------------------------------------------- print '.' print 'What is the Net List file ? ' netlist_file = raw_input ( 'Enter File Name : ' ) #Separate path and directory #---------------------------- netlist_file_split = os.path.split ( netlist_file ) netlist_file_name = netlist_file_split[1] if ( netlist_file_split[0] != '' ) : netlist_file_path = netlist_file_split[0] + '/' # get a listing of the apropriate directory #------------------------------------------ netlist_file_path_list = os.listdir( netlist_file_path ) #check for config file existence #------------------------------- if ( netlist_file_name not in netlist_file_path_list ) : print '.' print "netlistsort> Error: Could not locate file <" + netlist_file_name + '>' Done() ####################################### # Read Net List File ####################################### #read and remember the Net List file print '.' print 'Parsing Net List file <' + netlist_file_name + '>' try: netlist_file_object = open ( netlist_file_path+netlist_file_name, 'r' ) netlist_lines = netlist_file_object.readlines () netlist_file_object.close () except: print '.' print "netlistsort> Error: could not find or read Net List file" Done() ####################################### # Parse Net List File ####################################### # recursive helper function to append the connections to the current net def append_connections ( connection_list ) : global net_size global net_entry next_word = connection_list [0] if next_word[0] <> flag_comment : net_size = net_size + 1 net_entry.append ( next_word ) if len(connection_list) > 1 : append_connections ( connection_list[1:] ) print '.' print 'Parsing and Sorting Net List file <' + netlist_file_name + '>' #scan through all lines in the file for line in netlist_lines : # only consider lines starting with the right keyword if ( line[0:3] == 'NET' ) : # split line intou words words_in_line = string.split ( line ) # the second word on the line is the net name net_name = words_in_line[1] # we may have already encountered this net, # or need to create a new enty if netlist_dict.has_key ( net_name ) : net_entry = netlist_dict[net_name] net_size = int ( net_entry[0] ) netsize_dict[ net_size ] = netsize_dict[ net_size ] - 1 else : net_size = 0 net_entry = [ '0' ] # add to this net the connections defined on this line append_connections ( words_in_line [2:] ) # now update the dictionary for this entry net_entry[0] = str ( net_size ) netlist_dict [net_name] = net_entry # we also keep track of the distribuion of net sizes if netsize_dict.has_key ( net_size ) : netsize_dict[ net_size ] = netsize_dict[ net_size ] + 1 else : netsize_dict[ net_size ] = 1 ####################################### # Generate Output File ####################################### # open new output file sorted_file_name = netlist_file_path+netlist_file_name + '.nls' print '.' print 'Writing Output file <%s>' % sorted_file_name sorted_file_object = open ( sorted_file_name, 'w' ) # write header to sorted output file sorted_file_object.write ( flag_comment + " netlistsort>----------------------------------------------\n" ) sorted_file_object.write ( flag_comment + " netlistsort> " + netlistsort_version ) sorted_file_object.write ( " -- " + time.asctime ( time.localtime( time.time() ) ) + '\n' ) sorted_file_object.write ( flag_comment + " netlistsort> Net List Sorted from <" + netlist_file_name + '>\n' ) sorted_file_object.write ( flag_comment + " netlistsort>----------------------------------------------\n" ) # sort net_names by building a sorted list of keys to iterate on net_names = netlist_dict.keys() net_names.sort() # same thing for net sizes net_sizes = netsize_dict.keys() net_sizes.sort() sorted_file_object.write ( '\n' ) sorted_file_object.write ( '*** Found %d Nets in Net List \n' % len ( netlist_dict ) ) sorted_file_object.write ( '\n' ) # one section per net size for net_size in net_sizes : # only print the nets with at least one entry if netsize_dict[net_size] <> 0 : sorted_file_object.write ( '\n' ) sorted_file_object.write ( '*** %d Net Entries with %d Connections \n' % (netsize_dict[net_size], net_size) ) sorted_file_object.write ( '\n' ) # print each net of this size (in alphabetical order) for net_name in net_names : if netlist_dict[net_name][0] == str ( net_size ) : # build each line before writing to file to control line length # also try to tabulate net name output_line = ' ' + net_name + (25-len(net_name))*' ' + ' ' for connection_num in range ( 1, len (netlist_dict[net_name]) ) : # start a new line if this one gets too long if len ( output_line ) > 72 : sorted_file_object.write ( output_line + '\n' ) output_line = 29 * ' ' connection = netlist_dict[net_name][connection_num] # add this connection, while tabulating output_line = output_line + (9-len(connection))*' ' + ' ' + connection sorted_file_object.write ( output_line + '\n' ) # write trailer to sorted output file sorted_file_object.write ( '\n' ) sorted_file_object.write ( '\n' ) sorted_file_object.write ( flag_comment + " netlistsort>----------------------------------------------\n" ) sorted_file_object.write ( flag_comment + " netlistsort> Done Sorting <" + netlist_file_name + '>\n' ) sorted_file_object.write ( flag_comment + " netlistsort> " + netlistsort_version ) sorted_file_object.write ( " -- " + time.asctime ( time.localtime( time.time() ) ) + '\n' ) sorted_file_object.write ( flag_comment + " netlistsort>----------------------------------------------\n" ) # done sorted_file_object.close () Done ()