#==========================================================================*/
#
#  Realtek Semiconductor Corp.
#  GDB Script for automatically flash programming
#  
#  Usage : just use the run.sh script to get everthing worked
#
#
#  phinex hung (phinexhung@realtek.com)
#  Nov. 16, 2011
#
#==========================================================================*/

#set height to 0 to avoid "return" press needed for scroll
set height 0
set width 0

#set file name
file flashprog_dbg.exe

#connection 
target remote localhost:4281 

#load your file to memory 
load

#break before setting flash_addr
b load_flash_addr
#break before load memory
b load_memory
#break in case of memory comparision error
b compare_error

#disable all messages
set verbose off
set complaints 0
set confirm off
set exec-done-display off
#set debug aix-thread off
#set debug dwarf2-die 0
#set debug displaced off 
set debug event 0
set debug expression 0
set debug frame 0
#set debug infrun off
set debug observer 0
set debug overload 0
set debugvarobj 0
set pagination off
#set remote memory-write-packet-size fixed
#set remote memory-write-packet-size 3000
#set download-write-size 4096
disable display

set $start_addr = 0
set $end_addr = 0x10000


set $file_size = 
set $unit_size = 0x10000

set $left_size = $file_size % $unit_size

if ($left_size != 0)
set $loop_num = $file_size / $unit_size + 1
else
set $loop_num = $file_size / $unit_size
end

#printf "Loop Count 0x%x\n", $loop_num

set $spi_addr =  0x81010000
set $load_addr = 0x89000000
set $count = 0
set remotetimeout 1000
c
  
while $loop_num > 0

  if (flag != 0)
    printf "Error in comparision!\n"
  end


  set flash_addr = $spi_addr + $count * $unit_size 

  printf "Erase SPI flash block with address %x!\n", flash_addr

  c

  #printf "Loading file to memory from 0x%x to 0x%x\n", $start_addr, $end_addr

  #read data to memory from a file to be loaded to SPI flash
  restore tmpfile binary $load_addr-$start_addr $start_addr $end_addr
  
  c
  
  set $start_addr = $start_addr + 0x10000 
  set $end_addr = $end_addr + 0x10000
  
  
  #decrease the counter  
  set $loop_num = $loop_num - 1

  set $count = $count + 1

end

  #printf your message  
  #printf "Automatically programing flash process has been completed successfully!\n"
    
  #printf your message  
  #set remotetimeout 1000
  #set $end_addr = $start_addr + $left_size
set $dump_end_addr = $spi_addr + $file_size 
  #printf "dump data to file for check from address 0x%x to address 0x%x...\n", $spi_addr , $dump_end_addr
    
dump binary memory tmpfile $spi_addr $dump_end_addr
    
 #printf "dump data finished ! please verify your data with hex compare software .\n"
  #quit from GDB
quit 
  #confirm for GDB input
y

