#! /bin/csh -f
#
# This csh script attaches XTALK_ACTIVE_TIME properties to Allegro nets.
# The input file is produced by Verilog using the Active Times PLI package.
# After this script runs the Allegro drawing is updated.
# The file active_times.netin remains as a side effect.
# If a net_map file is used it must map the net names in the input file
# to Allegro net names.
# You must have Allegro (specifically netin & il_allegro) to run this.
# If you have an intrsignoise license DRC will be kept up to date.

if ($#argv < 1) then
    echo "usage: atimes2allegro atimes_data [-m net_map] [-b allegro_brd]"
    exit (1)
endif

set DATFILE = $argv[1]
shift argv

set MAPFILE = ""
set BRDFILE = ""

while ($#argv > 0)
    switch ($argv[1])
        case -m:
            shift argv
            set MAPFILE = $argv[1]
            breaksw
        case -b:
            shift argv
            set BRDFILE = $argv[1]
            breaksw
    endsw
    shift argv
end

il_allegro <<ENDIL
setPrompts "" ""

procedure( xatReadDataFile( filename )
    "read a list of 'active_times data from a file"
    let( (fp line data)
        fp = infile( filename )
        if( ! fp then
            error( strcat( "unable to open " filename " for input" ))
        else
            while( line = lineread( fp )
                unless( line == t || caar( line ) != 'active_times
                    data = cdar( line )
                )
            )
            close( fp )
        )
        data
    )
)

procedure( xatWriteNetinFile( data filename )
    "write an Allegro netin file to attach XTALK_ACTIVE_TIME properties to nets"    let( (fp)
        fp = outfile( filename )
        if( ! fp then
            error( strcat( "unable to open " filename " for output" ))
        else
            fprintf( fp "\$NETS\n" )
            fprintf( fp "\$A_PROPERTIES\n" )
            foreach( pair data
                fprintf( fp "XTALK_ACTIVE_TIME '%s' ; '%s'\n" cadr( pair ) car( pair ))
            )
            fprintf( fp "\$END\n" )
            close( fp )
        )
    )
)
procedure( xatMapNetNames( data filename )
    "modify net names according to a mapping file"
    let( (fp line mapdata)
        fp = infile( filename )
        if( ! fp then
            error( strcat( "unable to open " filename " for input" ))
        else
            while( line = lineread( fp )
                unless( line == t || caar( line ) != 'net_map
                    mapdata = cdar( line )
                )
            )
            close( fp )
        )
        if( mapdata then
            foreach( pair mapdata
                data = subst( cadr( pair ) car( pair) data ))
        )
    )
    data
)

procedure( xatMungeNetName( name )
    "algorithmically change a Verilog net name to an Allegro net name"
    let( (tail lastDot)
        lastDot = rindex( name ".")
        tail = upperCase( if( lastDot substring( lastDot 2) name))
        name = ""
        foreach( word parseString( tail "<>_")
            name = strcat( name word)
        )
        name
    )
)

procedure( xatMungeNetNames( data )
    "modify net names algorithmically"
    foreach( mapcar line data
        list( xatMungeNetName( car( line)) cadr( line))
    )
)

procedure( xatATimesToAllegro( datfile brdfile mapfile )
    "read an active_times data file and update an Allegro drawing"
    let( (data)
        data = xatReadDataFile( datfile )
        if( mapfile != "" then
            data = xatMapNetNames( data mapfile )
        else
            data = xatMungeNetNames( data )
        )
        xatWriteNetinFile( data "active_times.net" )
        unless( brdfile == ""
            sh( strcat( "netin active_times.net " brdfile )))
    "netin file active_times.net produced"
    )
)

xatATimesToAllegro( "$DATFILE" "$BRDFILE" "$MAPFILE" )

ENDIL


