#***************************************************************************
#    filename:            : rlx_gdb_spi_flash_driver_script.scr  
#    description          : RLX GDB SPI flash driver script example
#    created              : Apr 16 2009
#	   version                : 1.0
#    copyright            : (C) 2009 by Realtek Co.
#    email                : yf_chen@realsil.com.cn
#***************************************************************************/
#===========================================================================
#
#                        EDIT HISTORY FOR MODULE
#
#when        who              what, where, why
#--------    -----            ----------------------------------------------------------
#===========================================================================*/
#   Description :  SPI flash driver realized with GDB commands script file and a lite flash driver
#                  run in target memory , the main purpose is for accelerate spi flash program with limited target memory. 
#                  The script file load lite flash driver firstly , and initial the 
#                   registers and parameters(variables) for the lite flash driver ,
#                   download data in target file(to be loaded to flash) to target memory buffer,
#                   set a breakpoint in the lite flash driver ,
#                   then , run , when the driver stopped at breakpoint , it will 
#                   re-initial all registers needed in the flash driver and run again
#                   until the data in the file programed over.
#                  For program check ,the script file will dump all data in flash to a file.  
#                  You may compare them for flash driver verification.
#
#   Usage :        run rsdk-elf-gdb
#                  under the gdb console ,please input "source rlx_gdb_spi_flash_driver_script.scr"
#==========================================================================*/

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

#set file name
file rlx_ep_fw_spi_flash_el.exe

#connection 
target remote 172.29.17.34:26120

#load your file to memory 
load

#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 3000

#set the file size
set $file_size = 0xcf5d4  

#set the memory size
set $max_mem_size = 32 * 1024  

#set the data size to max memory size subtract 2k SIZE(program size)
set $prog_size = 2048
set $size =  $max_mem_size - $prog_size

#set loop times
set $loop_num = $file_size / $size

#set left data
set $left_size = $file_size % $size
printf "left size = %d\n",$left_size

#set buffer size 
set $buf_size = $size

#set offset of buffer
set $buf_off = $prog_size

#set the start address of memory buffer
#for example : 0x80200800
set $buf_addr = $pc + $buf_off

#set the flash controller fifo size
set $s0 = 0x80

#set buffer start offset
set $start_addr = 0

#set end address of memory buffer
set $end_addr = $start_addr + $buf_size

#set SPI flash address #0x81000000
set $spi_addr = 0x81000000

#set dump address
set $dump_start_addr = $spi_addr
set $dump_end_addr = $spi_addr + $buf_size - 1

#set registers to be used in your file
#buffer start address
set $t4 = $pc + $buf_off
set $t4_buf = $pc + $buf_off
 
#set registers to be used in your file
set $gp = $size

#set the SPI program base address
set $t3 = 0 

#set file name variable
#set $file_name = "rlx_ep_spi_test.bin"

#set file check name variable
#set $file_chk_name "rlx_ep_spi_test.chk"

disable display

  printf "erase SPI flash !\n"
  
  b   debug_spi_fast_wr_end

while $loop_num > 0

  #set breakpoint to symbol in your file

 #commands

#silent
#execute loop 

  #read data to memory from a file to be loaded to SPI flash
  restore rlx_ep_spi_test.bin binary $buf_addr-$start_addr $start_addr $end_addr
  
  #continue  
  c
  
  #p /x $s0
  #set the data size to be programed to spi flash
  set $gp = $size 
  #p /x $t3 = $t3 + $buf_size
  #p /x $t3
  set $t4 = $t4_buf
  
  #set $buf_addr = $buf_addr  - $start_addr
  set $start_addr = $start_addr + $buf_size 
  set $end_addr = $start_addr + $buf_size
  #printf "start_addr = 0x%x , end_addr = 0x%x\n" , $start_addr, $end_addr
  
  #set $dump_start_addr = $dump_start_addr + $buf_size 
  #set $dump_end_addr = $dump_start_addr + $buf_size - 1
  
  #decrease the counter  
  set $loop_num = $loop_num - 1
  p  $loop_num
  #printf "$start_addr = 0x%x\n",$start_addr
  #p /x *($start_addr | 0x80000000)


end

  #set breakpoint to symbol in your file
  #b   debug_spi_fast_wr_end 

  # program the left data (not aligned to 30k)
  set $end_addr = $start_addr + $left_size
  printf "start_addr = 0x%x , end_addr = 0x%x\n" , $start_addr, $end_addr
  restore rlx_ep_spi_test.bin binary $buf_addr-$start_addr $start_addr $end_addr
  set $gp = $left_size
  set $t4 = $t4_buf
  
  #continue  
  c

  #printf your message  
  printf "spi flash success !\n"
    
  #printf your message  
  #set remotetimeout 1000
  #set $end_addr = $start_addr + $left_size
  set $dump_end_addr = $spi_addr + $end_addr
  printf "dump data to file for check from address 0x%x to address 0x%x...\n", $spi_addr , $dump_end_addr
    
  dump binary memory  rlx_ep_spi_test.chk $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

