#
# ##############################################################################################
#
# LinkerScript_builder.tcl- builds starting point for the LinkerScript lab
#
# ToDo:
#    for 2020.1 - rewrite to use the "make" and "makestep" procs as we already do with the hardware
#
# History:
#    2021/08/30 - WK - 2021.1 - rewrote to make better use of vitis_helper, added boardSelect proc
#    2020/07/26 - WK - 2020.1
#    2020/02/26 - WK - 2019.2 release for Vitis (stolen from fileSystems)
#
# #############################################################################################
#
set badEnv 0

if {[catch {variable trainingPath $::env(TRAINING_PATH)} emsg]} {
   puts "ERROR!!! TRAINING_PATH environment variable not defined!";
   set badEnv 1;
} else {
   regsub -all {\\} $trainingPath / trainingPath;
}

puts "Version 2021.1 - 2020/08/30";

# the vitis helper contains a LOT of ease-of-use proc
# since the student shouldn't care about the actual construction, there 
# isn't any harm in using these proc as there is no loss of learning
source $trainingPath/CustEdIP/vitis_helper.tcl;  

# set up description of projects
set tcName        LinkerScript;
set labOrDemo     lab;
set platName      linkerScript_plat;
set sysName       linkerScript_sys;
set domName       linkerScript_dom;
set appName       linkerScript_app;
set appTemplate  {Empty Application(C)};
set processorName undefined; # psu_cortexa53_0;

# identify the steps in this lab following the _completer module. Note: there is not yet a "make" function 
set stepList {{boardSelect(ZCU104) buildStartingPoint}\
              {sourcesAdd(LED_Controller) modifyTemplates synthesisRun}
              {familiesAdd }
             }

# sets the variables used to direct the building of the projects
proc boardSelect {thing} {
   variable processorName;
   variable platform;
   variable debug;
   variable verbose;
   
   # figure out which board is being used and set the platform and processor names
   if {[string equal -nocase $thing VCK190es]} {
      set platform vck190_es1;
      set processorName psv_cortexa72_0;
   } elseif {[string equal -nocase $thing VCK190]} {
      set platform vck190;
      set processorName psv_cortexa72_0;
   } elseif {[string equal -nocase $thing ZCU104]} {
      set platform zcu104;
      set processorName psu_cortexa53_0;
   } else {
      puts stderr "unknown platform: $thing";
      return;
   }
}


# 1-2-4
proc buildStartingPoint {} {
   variable trainingPath;
   variable tcName;
   variable platName;
   variable hwSpec ;
   variable domName;
   variable sysName;
   variable appName;
   variable appTemplate;
   variable processorName;
   variable platform;
   variable labOrDemo;
   variable verbose;
   if {$verbose} { puts "LinkerScript_builder.tcl:buildStartingPoint"; }
      
   # make sure we're in the right place
   set workspaceName [getws];
   set targetWorkspace $trainingPath;
   append targetWorkspace / $tcName /lab;
   # if there is a drive specifier listed, make sure that it's capitalized
   if {[string first : $targetWorkspace] == 1 } { 
      set drive [string toupper [string range $targetWorkspace 0 1]];
      # replace drive with forced uppercase
      set strippedTargetWorkspace [string range $targetWorkspace 2 [string length $targetWorkspace]];
      set targetWorkspace $drive;
      append targetWorkspace $strippedTargetWorkspace;
   }
   puts "going to compare: $targetWorkspace with $workspaceName";

   # now look to see if we're in the right place
   if {$workspaceName != $targetWorkspace} {
      puts "The workspace should be $targetWorkspace, but instead we're in $workspaceName.";
      #puts "This mismatch will cause the script to fail, so we're going to force the workspace to the right place and continue...";
      #setws $targetWorkspace;
      puts "You must change the workspace to $targetWorkspace and re-run this script."
      puts "Exiting now...";
      exit 1;
   } else {
      puts "Workspace $workspaceName verified! Continuing...";
   }
   
   # clear the workspace (removal of platform projects still doesn't work - skip this for now and try again in the next release)
   projRemove all;
   
   # create a platform project from the XSA - if the project to build can't be found in the existing projects list, then ...	
   set    hwSpec $trainingPath/CustEdIP/UED_;
   append hwSpec $platform .xsa;
   puts "Creating the Platform Project $platName based on $hwSpec";
   set startTime [clock seconds];
   platform create -name $platName -hw $hwSpec -desc {Platform project for SystemDebugger lab};
   set elapsedTime [expr [clock seconds] - $startTime];
   puts "Platform created in $elapsedTime seconds";
    
   # next build the Domain for the newly created platform project
   set startTime [clock seconds];
   domain create -name $domName -proc $processorName -os standalone -desc {standalone BSP supporting first processor in the cluster};
   set elapsedTime [expr [clock seconds] - $startTime];
   puts "Domain created in $elapsedTime seconds";
   
   # customize the domain to include the xilinx fat file system 
   set libList { { xilffs fs_interface 2 } { xilffs ramfs_size 2098152000 } }
   domBuild $libList;	#Add libraries and configure parameters for the active domain, which was created with this domain create command

   # then build the application and load the provided files
   set labSourceList  { linkerScript_main.c utils_print.c utils_print.h platform.c platform.h platform_config.h};
   set demoSourceList { linkerScript_main.c utils_print.c utils_print.h platform.c platform.h platform_config.h};
   if {[string compare -nocase $labOrDemo lab] == 0} { set filesToCopy $labSourceList; } else { set filesToCopy $demoSourceList; }
   set startTime [clock seconds];
   appBuild $filesToCopy;
   set elapsedTime [expr [clock seconds] - $startTime];
   puts "Application project created in $elapsedTime seconds";

   # now that the projects are created, build everything related to this application
   set startTime [clock seconds];
   app build -name $appName;
   set elapsedTime [expr [clock seconds] - $startTime];
   
   puts "App and platform built in $elapsedTime seconds";
}

# done

puts "Done with linkerScript_builder.tcl";



