/*                                                                          */
/* Function:-issue application depend memory copy it may do nothing in case */
/*           of host responsibility or read data from flash                 */
/*          -performs minimal HW init(memory access EMIF)                   */
/*          -performs checksum test                                         */
/*          -if checksum sucesses  it calls to _c_int00(application)        */
/*          -if checksum fails it stands in place                           */
/*          -within process it reports SW location debug code to            */
/*           mbdr_dsp_common_status, which located in constant address, it  */
/*           enables user(CPU) reads status for debug                       */
/*          -status report is implemented by calling to                     */
/*           mbdr_dsp_debug_signal_app, which generally indicates status    */
/*           by LEDs                                                        */
/*                                                                          */
/* Background:                                                              */ 
/*                                                                          */
/*  If application is being loaded by debugger programs starts runnig in    */
/*  application entry point _c_int00{remark: _c_int_00 location is random}  */
/*                                                                          */
/*  But if application is loaded from flash the following process occurs    */
/*  by HW response.                                                         */
/*  With the release of RESET line, the DSP internal periphersl is incharge */
/*  of copy 1Kbyte from Flash memory to address 00000000 in DSP internal    */
/*  RAM and then start run the code from address 0, without any SW support  */
/*                                                                          */
/*  These 1kbyte program is named boot loader. It is being written by user  */
/*  and located at first 1kbyte, the only code before it just sets stack    */
/*  pointer to temporary area                                               */
/*  In order to be able to use the same boot loader for different           */
/*  application. We defined for mbdr dsp application the following demands: */
/*                                                                          */
/*  1. Memory map                                                           */
/*                                                                          */
/* +------------+---------------------------------------------------------+ */
/* |range       | Content                                                 | */
/* +------------+---------------------------------------------------------+ */
/* |00000000:   | Boot Loader Code                                        | */
/* |000003ff    | .mbdr_dsp_boot_loader_code_section                      | */
/* |            | .mbdr_dsp_application_emif_init                         | */
/* +------------+---------------------------------------------------------+ */
/* |00000800:   | Boot Loader stack                                       | */
/* |000008ff    | .mbdr_dsp_boot_loader_stack_section                     | */
/* +------------+---------------------------------------------------------+ */
/* |00000900:   | Application boot loader header see                      | */
/* |0000097f    | _mbdr_dsp_boot_loader_header_t                          | */
/* +------------+---------------------------------------------------------+ */
/* |00000980:   | Application mbdr_dsp_boot_loader_report_int0_location   | */
/* |000009ff    | .mbdr_dsp_boot_loader_report_int0_location_sect         | */
/* |            | User don't have to write these routine                  | */
/* +------------+---------------------------------------------------------+ */
/* |00000a00:   | ___binit__ These sections is created automatically by   | */
/* |00000aff    | TI LINKER, TI linker has macros that support inserting  | */
/* |            | copy sections to these area                             | */
/* +------------+---------------------------------------------------------+ */
/* |00000b00:   | application memory, entry point must be here            | */
/* |000fff00    | {these area is being loaded from flash}                 | */
/* +------------+---------------------------------------------------------+ */
/* |000fff00:   | Application/Boot Loader config area, updated by host    | */
/* |000fffff    |                                                         | */
/* +------------+---------------------------------------------------------+ */
/*                                                                          */

#include "windef.h"
#include "mbdr_dsp_common_sync_msg.h"
#include "mbdr_dsp_utils.h"
#include "mbdr_dsp_common_boot_loader_int.h"
#include "mbdr_dsp_common_boot_loader_app.h"
#include "mbdr_dsp_common_ti_peripheral_utils.h"
#include "mbdr_dsp_common_status.h"
#include "mbdr_dsp_common_debug_signal_app.h"
#include "mbdr_dsp_core_type_const.h"


#pragma DATA_MEM_BANK (mbdr_dsp_boot_loader_stack, 0);

#pragma DATA_SECTION  (mbdr_dsp_boot_loader_stack,".mbdr_dsp_boot_loader_stack_sect");
LONG mbdr_dsp_boot_loader_stack[MBDR_DSP_BOOT_STACK_DW_SIZE]; 

#pragma DATA_MEM_BANK (mbdr_dsp_common_status, 0);

#pragma DATA_SECTION  (mbdr_dsp_common_status,".mbdr_dsp_common_status_sect");
mbdr_dsp_common_status_t mbdr_dsp_common_status[1];



//  asm("  .sect	.mbdr_dsp_boot_loader_set_stack_sect ");
//  asm("  MVK.S2        0x08e0,SP ");
//  asm("  nop ");
//  asm("  nop ");
//  asm("  nop ");

#pragma CODE_SECTION  (mbdr_dsp_boot_loader,".mbdr_dsp_boot_loader_sect");
void mbdr_dsp_boot_loader(void)
{/* mbdr_dsp_boot_loader */

  LONG              physical_layer_buffer[2];

  ULONG             checksum, i;
  USHORT           *next_checksum_u16_p;

  /* application depend routine that enables access flash, external memory */
  /* GPIO basic setting                                                    */
  mbdr_dsp_application_emif_init();



  /* set test debug main module status code */

  /* after sync indication */
  mbdr_dsp_debug_signal_app(
              MBDR_DSP_MAIN_MODULE_CODE_T_BOOT_LOADER_START,
              &physical_layer_buffer[0]);


  /* after sync indication & status update */
  mbdr_dsp_debug_signal_app(
              MBDR_DSP_MAIN_MODULE_CODE_T_BOOT_LOADER_AFTER_SYNC_WITH_CPU,
              &physical_layer_buffer[0]);

        

  /* application depend routine that enables access to */
  /* flash & external memory from DSP                  */
  mbdr_dsp_application_emif_init();

  /* enables application to copy program by itself, useful for */
  /* applications, which boot from flash                       */
  mbdr_dsp_boot_loader_application_copy_program();


  /* computes checksum */
  next_checksum_u16_p = (USHORT *)
               ( MBDR_DSP_CPU_DSP_BOOT_LOADER_APPLICATION_MEMORY_BASE
                +MBDR_DSP_COMMON_L2_BASE);

  /* computes checksum */
  checksum = 0;
  for(i=0; 
	  i<(( MBDR_DSP_COMMON_STATUS_BYTE_OFFSET
	      -MBDR_DSP_CPU_DSP_BOOT_LOADER_APPLICATION_MEMORY_BASE)
	     /sizeof(USHORT));
	  i++)
	  checksum += *next_checksum_u16_p++;

  /* reports checksum computation result */
  mbdr_dsp_common_status[0].boot_loader_add_checksum = checksum;

  /* reports boot loader version from boot loader view */
  mbdr_dsp_common_status[0].boot_loader_version_from_boot
		       = MBDR_DSP_BOOT_LOADER_SW_VERSION;

  if(mbdr_dsp_common_status[0].expected_add_checksum
     !=
     mbdr_dsp_common_status[0].boot_loader_add_checksum)
  {/* reports checksum error and waits foreover */
            mbdr_dsp_debug_signal_app(
                MBDR_DSP_MAIN_MODULE_CODE_T_BOOT_LOADER_CHECKSUM_FAULT,
                &physical_layer_buffer[0]);
      //iv      while(1)
	//iv		{
	//iv		}
  }/* reports checksum error and waits foreover */

  /* report successful load from flash */
  mbdr_dsp_debug_signal_app(
            MBDR_DSP_MAIN_MODULE_CODE_T_BOOT_LOADER_AFTER_COPY_FROM_FLASH,
            &physical_layer_buffer[0]);        

       /* derive int_0 location */
       //mbdr_dsp_boot_loader_report_int0_location();

  /* set test debug main module status code */
  mbdr_dsp_debug_signal_app(
              MBDR_DSP_MAIN_MODULE_CODE_T_BOOT_LOADER_FINISH,
              &physical_layer_buffer[0]);

  /* go to application int0 */
  mbdr_dsp_boot_loader_header[0].c_int00_proc_p();
      

}/* mbdr_dsp_boot_loader */


/* the following routine exists in order to consist boot code in application */
void mbdr_dsp_boot_loader_never_reach(void)
{
  mbdr_dsp_boot_loader();
}



