#!/usr/local/bin/perl -w

#if (($#ARGV < 1) || ($ARGV[0]=~m#^-h$#)) {
if ( $#ARGV < 0 ) {
  print <<'EOF';
	assign_pins [-h] [-a array_name] pin_listing_filename

        [-h] gives detailed help
        [-a] allows you to specify the array name to use when
             assigning pins.  Defaut name is "iosig"

	A file must be given containing a list of pins to 
	which an array of signals will be assigned sequentially.
EOF
  exit
}
else {
   if ($ARGV[0]=~m#^-h$#) {
      print <<'EOF';

***************************************************************
assign_pins                           Author:  Steven Sutankayo
                                        Date:  Sept. 14, 1998 
****************************************************************
This script provides a way to partially automate pin assignments
in MaxPlusII.  You create a file that contains a list of pins in
the order that you wish them to be assigned.  No extraneous 
characters are allowed in the file, just numbers.

--- Sample Pin List File foo.st ------
2
3
7
8
13
---------------------------------------

entering "assign_pins foo.lst" would generate the following output:

--------------------------------------
	|iosig1 :	BIDIR_PIN = 2 ; 
	|iosig2 :	BIDIR_PIN = 3 ; 
	|iosig3 :	BIDIR_PIN = 7 ; 
	|iosig4 :	BIDIR_PIN = 8 ; 
	|iosig5 :	BIDIR_PIN = 13 ; 
--------------------------------------

Specific signals can also be assigned:

--- Sample Pin List File foo.lst ----
clock 2
3
my_signal 7
8
output_signal 13
----------------------------------------

Entering "assign_pins -a my_array foo.lst" would generate:

----------------------------------------
	|clock :	BIDIR_PIN = 2 ; 
	|my_array1 :	BIDIR_PIN = 3 ; 
	|my_signal :	BIDIR_PIN = 7 ; 
	|my_array2 :	BIDIR_PIN = 8 ; 
	|output_signal :	BIDIR_PIN = 13 ; 
----------------------------------------

EOF
exit
   }
}

if ($ARGV[0]=~m#^-a#) {
   shift(@ARGV);
   $array_name = shift(@ARGV);
}
else {
   $array_name = "iosig";
}
print STDERR "array name is $array_name\n";

$autonum = 1;
open( PINLIST, $ARGV[0] ) || die "couldn\'t open $ARGV[0]";

while (<PINLIST>) {
   chop();
   next if (m/^\s*$/);

   # if there are two columns, then use those for assignments
   if ( m/(\w+)\s+(\w+)/ ) {
      $signame = $1;
      $pin = $2;
   }
   else {
      # chop the whitespace and get the pin number
      s/\s*//;
      $pin = $_;
      $signame = "${array_name}${autonum}";
      $autonum++;
   }

   # write the line 
   print("	|$signame :	BIDIR_PIN = $pin \; \n");
}


