@echo off

set loader_rev=1.2

:main
color 70
set user_input=none
set broadtype=none
cls
echo.
echo                   7125 Firmware Loader %loader_rev%  (April 19, 2007)  
echo.  
echo     1.  LM
echo.
echo     2.  LC
echo.
echo     3.  RDC
echo.
echo     4.  TX
echo.
echo     5.  IOP
echo.
echo.
echo.
echo.
echo     8.  test utilities
echo.
echo     9.  conversion utilities   
echo.
echo     0.  exit
echo.
echo.
set /p user_input=selection? :  
set broadtype=none 
if %user_input%==1 set broadtype=lm
if %user_input%==2 set broadtype=lcu
if %user_input%==3 set broadtype=rdc
if %user_input%==4 set broadtype=tx
if %user_input%==5 set broadtype=iop
if %user_input%==8 goto test_menu
if %user_input%==9 goto conversion_menu
if %user_input%==0 goto end
if %user_input%==none goto main
if %broadtype%==none goto main
goto %broadtype%

:lm
set type=lm_hs
goto load_type_menu

:lcu
set user_input=none
set type=none
cls
echo.
echo                   LC type selection  
echo. 
echo.
echo.
echo.
echo     1.  ROV 
echo.
echo.
echo.
echo     2.  AUV   
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo     0.  back
echo.
echo.
set /p user_input=LC type? :  
set type=none  
if %user_input%==1 set type=lcu_rov_su
if %user_input%==2 set type=lcu_auv
if %user_input%==0 goto main
if %user_input%==none goto lcu
if %type%==none goto lcu
goto load_type_menu

:rdc
set type=rdc_7125
goto load_type_menu

:tx
set user_input=none
set type=none
cls  
echo. 
echo                TX type selection
echo.
echo.                
echo.
echo.
echo     1.  TX200K               
echo.               
echo.
echo.              
echo.
echo     2.  TX396K
echo.
echo. 
echo.
echo.
echo.
echo.
echo.
echo.
echo     0.  back
echo.
echo.
set /p user_input=TX type? :   
set type=none 
if %user_input%==1 set type=stx200k
if %user_input%==2 set type=stx396k
if %user_input%==0 goto main
if %user_input%==none goto tx
if %type%==none goto tx
goto load_type_menu

:iop
set user_input=none
set type=none
cls   
echo.
echo                 IOP type selection
echo.
echo.
echo. 
echo.
echo.
echo     1.  Dry  (ROV)    
echo.
echo.
echo.
echo.
echo     2.  Wet  (AUV)
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo     0.  back
echo.
echo.
set /p user_input=IOP type? :  
set type=none 
if %user_input%==1 set type=iopd_simrad_em
if %user_input%==2 set type=iopw_auv
if %user_input%==0 goto main
if %user_input%==none goto iop
if %type%==none goto iop


:load_type_menu

set load_directory=released
if not exist experiment\%type% goto operation
set load_directory=none
set user_input=none
cls  
echo.
echo               Loading Directory
echo.
echo.
echo.
echo      There exists an experiment directory for this selection
echo.
echo.
echo.
echo.
echo     1.  program with archived 7125 loader %loader_rev% code
echo.
echo.
echo.
echo     2.  program with experiment directory code
echo.
echo.
echo.
echo.
echo.
echo     0.  back
echo. 
echo.
set /p user_input=selection :  
set load_type=none  
if %user_input%==1 set load_directory=released
if %user_input%==2 set load_directory=experiment
if %user_input%==0 goto main
if %user_input%==none goto load_type_menu
if %load_directory%==none goto load_type_menu
if %load_directory%==experiment color E

:operation
set user_input=none
set operation=none
set dest_adr=2a
cls  
echo.
echo                 Operation
echo.
echo.
echo     1.  load with debug board      ( PLD, DSP_BOOT, DSP_SYS, FPGA ) 
echo.
echo.
echo.
echo     2.  load in-system             ( DSP_SYS, FPGA )
echo.
echo.
echo.
echo     3.  update DSP_BOOT in-system  ( DSP_BOOT )
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo     0.  back
echo. 
echo.
set /p user_input=what operation would you like to do? :  
set operation=none  
if %user_input%==1 set operation=load_all_with_debug_board
if %user_input%==2 set operation=update_dspsys_and_fpga_in_system
if %user_input%==3 set operation=update_boot_in_system
if %user_input%==0 goto main
if %user_input%==none goto operation
if %operation%==none goto operation
if %operation%==load_all_with_debug_board goto execute
if %broadtype%==lm goto execute 
if %broadtype%==lcu goto execute
if %broadtype%==iop goto execute
if %broadtype%==rdc goto execute

:which_tx_port
set user_input=none
set dest_adr=none
cls
echo.
echo                TX port selection  
echo.  
echo.
echo.
echo.
echo     1.  port A     
echo.
echo.
echo.
echo     2.  port B      
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo     0.  back
echo.
echo.
set /p user_input=TX port? :  
if %user_input%==1 set dest_adr=2a
if %user_input%==2 set dest_adr=2b
if %user_input%==0 goto operation
if %user_input%==none goto which_tx_port
if %dest_adr%==none goto which_tx_port

:execute
IF EXIST sys.hex del sys.hex
IF EXIST fpga.bin del fpga.bin
IF EXIST rx_fpga.bin del rx_fpga.bin
IF EXIST sec1.bin del sec1.bin
IF EXIST sec1.enc del sec1.enc
IF EXIST boot.hex del boot.hex
IF EXIST boot.bin del boot.bin
IF EXIST boot.enc del boot.enc
IF EXIST cpld.jed del cpld.jed
IF EXIST fpga.bsd del fpga.bsd
IF EXIST ldsec.enc del ldsec.enc
IF EXIST ldboot.enc del ldboot.enc

cls
echo checking for a DSP system .hex file  
IF NOT EXIST %load_directory%\%type%\sys\*.hex GOTO error_nofile
copy %load_directory%\%type%\sys\*.hex sys.hex
echo found
echo.
echo checking for an fpga .bin file 

IF NOT EXIST %load_directory%\%type%\fpga\*.bin GOTO error_nofile
copy /B %load_directory%\%type%\fpga\*.bin fpga.bin 
echo found
if %broadtype%==rdc goto rdc_files 
goto pick_encoder
:rdc_files
echo checking for an rx fpga .bin file 
IF NOT EXIST %load_directory%\%type%\rx_fpga\*.bin GOTO error_nofile
copy /B %load_directory%\%type%\rx_fpga\*.bin rx_fpga.bin
echo found

:pick_encoder
goto %broadtype%_enc

:lm_enc
set dest_adr=30
copy halt_lm.enc halt.enc
copy ldboot_lm.enc ldboot.enc
copy ldsec_lm.enc ldsec.enc
copy xc2v250.bsd fpga.bsd
Flash_3sectors_XC2V250 sys.hex fpga.bin sec1.bin
flashenc_ldsect 0x30 0x10 1 512 500000 100 sec1.bin sec1.enc
goto end_of_enc

:lcu_enc
set dest_adr=20
copy halt_lcu.enc halt.enc
copy ldboot_lcu.enc ldboot.enc
copy ldsec_lcu.enc ldsec.enc
copy xc2v500.bsd fpga.bsd
Flash_3sectors_XC2V500 sys.hex fpga.bin sec1.bin
flashenc_ldsect 0x20 0x10 1 512 500000 100 sec1.bin sec1.enc
goto end_of_enc

:iop_enc
copy xc2v500.bsd fpga.bsd
Flash_3sectors_XC2v250 sys.hex fpga.bin  sec1.bin

if %type%==iopw_auv goto iopw_auv_enc
set dest_adr=31
copy halt_iopd.enc halt.enc
copy ldboot_iopd.enc ldboot.enc
copy ldsec_iopd.enc ldsec.enc
flashenc_ldsect 0x31 0x10 1 512 500000 100 sec1.bin sec1.enc
goto end_of_enc
:iopw_auv_enc
set dest_adr=25
copy halt_iopw.enc halt.enc
copy ldboot_iopw.enc ldboot.enc
copy ldsec_iopw.enc ldsec.enc
flashenc_ldsect 0x25 0x10 1 512 500000 100 sec1.bin sec1.enc
goto end_of_enc

:rdc_enc
set dest_adr=26
copy halt_rdc.enc halt.enc
copy ldboot_rdc.enc ldboot.enc
copy ldsec_rdc.enc ldsec.enc
copy xc2v500.bsd fpga.bsd
Flash_3sectors_rdc sys.hex fpga.bin rx_fpga.bin sec1.bin
flashenc_ldsect 0x26 0x10 1 512 500000 100 sec1.bin sec1.enc
goto end_of_enc

:tx_enc
copy halt_tx%dest_adr%.enc halt.enc
copy ldboot_tx%dest_adr%.enc ldboot.enc
copy ldsec_tx%dest_adr%.enc ldsec.enc
copy xc2v1000.bsd fpga.bsd
Flash_3sectors_XC2v1000 sys.hex fpga.bin  sec1.bin
flashenc_ldsect 0x%dest_adr% 0x10 1 512 500000 100 sec1.bin sec1.enc
goto end_of_enc

:end_of_enc
del sec1.bin

if %operation%==load_all_with_debug_board goto load_with_debug_board

if %operation%==update_boot_in_system goto load_boot_in_system

:load_in_system
CALL halt_lmtest.bat
lmtest sec1.enc
del sec1.enc > NUL
echo about to send load secondary command
sleep 1
lmtest ldsec.enc 
echo about to send reload 0 command
sleep 3
lmtest ldboot.enc
sleep 1
goto test_menu

:load_boot_in_system
copy %load_directory%\%type%\boot\*.hex boot.hex
boot_noFPGA boot.hex boot.bin
flashenc_ldsect 0x%dest_adr% 0x10 0 512 50000 100 boot.bin boot.enc
lmtest halt.enc
sleep 1
lmtest boot.enc
sleep 2
lmtest ldboot.enc
goto test_menu


:load_with_debug_board
cls
echo checking for a cPLD .jed file  
IF NOT EXIST %load_directory%\%type%\cpld\*.jed GOTO error_nofile
copy %load_directory%\%type%\cpld\*.jed cpld.jed
echo found
echo.
echo checking for a DSP_boot .hex file 
IF NOT EXIST %load_directory%\%type%\boot\*.hex GOTO error_nofile
copy %load_directory%\%type%\boot\*.hex boot.hex
echo found
sleep 1
impact -batch cpld.cmd
sleep 1
jtag EraseChip
boot_noFPGA boot.hex boot.bin
sleep 1
jtag boot.bin
sleep 1
del boot.bin
impact -batch cpld.cmd
sleep 1
del cpld.jed
cls
echo.
echo.  
echo     Flash was erased, then PLD and Boot code were burned in. 
echo.      
echo.
echo     toggle reset on the debug board and look for a blinking 
echo     red LED. If this fails to happen try removing all connections  
echo     for a few seconds or abort and start over. To abort use ctrl-c.
echo.
echo     Once you have a blinking red LED press enter to continue
echo.  
echo     The red LED should then stop toggling for a few seconds,    
echo     toggle again for a few more, turn off for a while, 
echo     and start blinking again. This will be indicative of the DSP 
echo     correctly receiving the firmware load.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
echo.
set /p user_input=press enter to continue:  


call halt_wtx.bat
wtx com1 sec1.enc
echo about to send load secondary command
sleep 2
wtx com1 ldsec.enc 
echo about to send reload 0 command
sleep 3
echo.
echo.
echo.
echo     ALT-X to exit com 1 monitor and continue programming sequence   
echo.
echo.
echo.
echo.
wtx com1 ldboot.enc
sleep 0.1
talk 1 115200 TEXT

:congratulations
photons

:test_menu
IF EXIST sys.hex del sys.hex
IF EXIST fpga.bin del fpga.bin
IF EXIST rx_fpga.bin del rx_fpga.bin
IF EXIST sec1.bin del sec1.bin
IF EXIST sec1.enc del sec1.enc
IF EXIST boot.hex del boot.hex
IF EXIST boot.bin del boot.bin
IF EXIST boot.enc del boot.enc
IF EXIST cpld.jed del cpld.jed
IF EXIST fpga.bsd del fpga.bsd
IF EXIST 0.bin del 0.bin
set user_input=none
cls
echo.
echo                          TEST menu
echo.  
echo.
echo.
echo     1.  view debug UART output (toggle reset after monitor comes up) 
echo.
echo.
echo.
echo     2.  send 12 ping_all commands with debug board 
echo.
echo.
echo.
echo     3.  send 12 ping_all commands in-system
echo.                            
echo.
echo.
echo     4.  back to main menu
echo.
echo.
echo.
echo     0.  exit
echo.
set /p user_input=enter your selection :  
if %user_input%==4 goto main
if %user_input%==1 goto debug_monitor
if %user_input%==2 goto ping_wtx
if %user_input%==3 goto ping_lmtest
if %user_input%==0 goto end
goto test_menu

:debug_monitor
talk 1 115200 TEXT
goto test_menu

:ping_wtx
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
sleep 0.2
wtx com1 pat_all.enc
goto test_menu

:ping_lmtest
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
sleep 0.1
lmtest pat_all.enc
goto test_menu

:error_nofile
echo.
echo.
echo.
echo.
echo.
ECHO      * ERROR file not found *
ECHO.
PAUSE
goto main


:conversion_menu
cls
set user_input=none
echo.  
echo                         7125 in-system conversion menu 
echo.
echo     1a.  load conversion Troj_an into LC               1b. convert LC
echo.
echo.
echo     2a.  load conversion Troj_an into RDC              2b. convert RDC
echo. 
echo.
echo     3a.  load conversion Troj_an into TX396K (port a)  3b. convert TX396K a
echo.
echo.
echo     4a.  load conversion Troj_an into TX200K (port b)  4b. convert TX200K b
echo.
echo.
echo     5a.  load conversion Troj_an into TX200K (port a)  5b. convert TX200K a
echo.
echo.
echo     6a.  load conversion Troj_an into all              6b. convert all
echo.
echo.
echo     0.  back
echo.
set /p user_input=enter your selection :  

if %user_input%==1a call lc_troj.bat
if %user_input%==1b call lc_convert.bat
if %user_input%==2a call rdc_troj.bat
if %user_input%==2b call rdc_convert.bat
if %user_input%==3a call tx_396_a_troj.bat
if %user_input%==3b call tx_396_a_convert.bat
if %user_input%==4a call tx_200_b_troj.bat
if %user_input%==4b call tx_200_b_convert.bat
if %user_input%==5a call tx_200_a_troj.bat
if %user_input%==5b call tx_200_a_convert.bat
if %user_input%==6a call all_troj.bat
if %user_input%==6b call all_convert.bat
if %user_input%==0 goto main
goto conversion_menu

:end
