#!/usr/bin/perl
#################################################################################
#
#  Usage: GB3PlusProgBuild1.pl [<PLX>|<PLD>|<MAINT>]
#
#################################################################################

# Change the following line when the firmware version or board build changes
$pld_fw_file    = "GB3-FW-All-#1.xsvf";
$build_number   = "1.0";

# Other variables used in this program
$board_name     = "Gimle-16-PCI-Basic-64";
$jtag_connector = "J8";
$plxprom_brdswitch    = "/TD";
$pldprog_brdswitch    = "/TD";
$maintprom_brdswitch  = "/TD";
$pci9056file = "Gimle-16-pci-basic-64.eep";

print "##########################################################################\n";
print "#\n";
print "#  Odin TeleSystems Inc\n";
print "#\n";
print "#  Configuration program for the $board_name board\n";
print "#  using the following file as the PLD firmware file:\n";
print "#\n";
print "#      $pld_fw_file\n";
print "#\n";
print "##########################################################################\n\n";

if ( ("\U$ARGV[0]" eq "PLX") || ("\U$ARGV[0]" eq "PLD") || ("\U$ARGV[0]" eq "MAINT") || ("\U$ARGV[0]" eq "") ) {
  # Command parameters ok
} else {
  print "\a\a\a\aUsage: \n\n";
  print "  GB3PlusProgBuild2.pl [<PLX>|<PLD>|<MAINT>]\n";
  exit (1);
}


print "The $board_name board should be connected via the $jtag_connector JTAG connector\n";
print "to a Hermod-PCI JTAG controller board.\n";
print "\n";
print "This program will erase all existing configuration data on the board and program\n";
print "it with new data\n";
print "\n";
print "Do you wish to continue?\n";

&yes_no;

# Prompt for scanning of the Odin Part Number (to get the Build number)
# It is too hard to scan the narrow Bar code for the Odin Part number
# $build_number  = &scan_part_no;


# Prompt for scanning of the Serial Number of the Board
$serial_number = &scan_serial_no;


if ("\U$ARGV[0]" eq "") {
  # Run all configuration steps

  print "Running all configuration steps for $board_name Build:$build_number $serial_number\n";
  &RunPlxProm;
  &RunMaintProm;
  &RunPldProg;

} elsif ("\U$ARGV[0]" eq "PLX")  {
  # Run the PLX EEPROM configuration step
  &RunPlxProm;

} elsif ("\U$ARGV[0]" eq "PLD")  {
  # Run the PLD configuration step
  &RunPldProg;

} elsif ("\U$ARGV[0]" eq "MAINT")  {
  # Run the MAINT EEPROM configuration step
  &RunMaintProm;
} else {
  print "\a\a\aERROR: Unknown command line parameter $ARGV[0]\n";
  exit (1);
}

print "\aExecution completed successfully\n";
exit (0);




################################################################
sub RunPlxProm {

  print "Running the PLX EEPROM configuration step (file = $pci9056file)\n";
  if (system "PlxProm $plxprom_brdswitch /Zbc0xFFFF^u0^Y^$pci9056file^q /LPlxProm-$serial_number.log") {
    print "\a\a\aERROR: PlxProm failed.\n";
    exit (1);
  }

} #RunPlxProm

  
################################################################
sub RunPldProg {

  print "Running the PLD PROGRAMMING configuration step (file = $pld_fw_file)\n";
  if (system "PldProg $pldprog_brdswitch $pld_fw_file") {
    print "\a\a\aERROR: PldProg failed.\n";
    exit (1);
  }

} #RunPldProg

  
################################################################
sub RunMaintProm {

  print "Running the MAINT EEPROM configuration step (serno=$serial_number, build=$build_number)\n";
  if (system "MaintProm /E $maintprom_brdswitch /ZS$serial_number^I$build_number^q /LMaintProm-$serial_number.log") {
    print "\a\a\a\aERROR: MaintProm failed.\n";
    exit (1);
  }

} #RunMaintProm

  

###############################################################################
sub yes_no {
  while ($key = <STDIN>) {
 
    chomp ($key);

    if ( ($key eq "y") || ($key eq "Y") ) {
      # exit the while loop
      last;
    } elsif ( ($key eq "n") || ($key eq "N") ) {
      print "\a\a\aConfiguration aborted. Exiting.\n";
      exit 1;
    } else {
      print "You pressed $key, I expected 'y' or 'n' \n";
      print "Try again...\n";
    }
  }
  
} #yes_no 

  

###############################################################################
sub scan_part_no {

  print "\n";
  print "Scan the Odin part number (P/N): ";
  print "\n";


  while (1) {
 
    chomp($pn = <STDIN>);

    $_ = $pn;

    #                                    Rev        Build  
    #     3 :   HAA     1040    1   -    1.0      -   3     A
    if (/\d[:](H[M|A]A)(\d{4})(\d+)[-]([\w\d\.]+)[-](\d+)([A-Z]+)/) {
      # This could be a valid part number
      print "\a";
      print "Part number      : $pn \n";
      print "Product number   : $1-$2-$3 \n";
      print "Product revision : $4 \n";
      $bno = $5 . ".0";
      print "Build number     : $bno \n";
      print "Accounting ver   : $6 \n";
      last; # break the loop
    } else {
      print "\a\a\a\a\a";
      print "That is not a valid Odin Part Number\n";
      print "Please verify that you are scanning the Odin Part Number.\n";
      print "Please scan again...\n";
    }
  }
  
  $bno;
} #scan_part_no 



###############################################################################
sub scan_serial_no {

  print "\n";
  print "Scan the Odin serial number (S/N): ";
  print "\n";

  while (1) {
 
    chomp($serialno = <STDIN>);

    $_ = $serialno;

    if (/^\w{3,5}X\d{9}$/) {
      # This seems like a valid serial number
      # print "\aSerial number   : $serialno \n";
      last; # break the loop
    } else {
      print "\a\a\a\a\aThat is not a valid Odin Serial Number\n";
      print "Please verify that you are scanning the Odin Serial Number.\n";
      print "Please scan again...\n";
    }
  }
  
  $serialno;
} #scan_serial_no 




