/*                                                                          */
/* Function    : initialization utils, which are common for both multi core */
/*               and single core platforms                                  */
/*                                                                          */
/* Used By     : DSP                                                        */
/*                                                                          */
/*                                                                          */

#include  "windef.h"
#include  "mbdr_dsp_utils.h"
#include  "mbdr_dsp_common_main_init_utils.h"
#include  "mbdr_dsp_platform_proc_ptrs.h"
#include  "mbdr_dsp_common_status.h"
#include  "mbdr_dsp_common_boot_loader_app.h"
#include  "mbdr_dsp_common_sync_msg.h"
#include  "mbdr_dsp_trace_sw_module_code.h"




#include  "mbdr_dsp_common_debug_signal_app.h"
#include  "mbdr_dsp_debug_trace_sw_module_code.h"


mbdr_dsp_platform_external_io_access_proc_ptrs_t 
                  mbdr_dsp_platform_external_io_access_proc_ptrs;

mbdr_dsp_core_process_proc_ptrs_t 
                     mbdr_dsp_core_process_proc_ptrs;

mbdr_dsp_platform_core_debug_proc_ptrs_t 
                     mbdr_dsp_platform_core_debug_proc_ptrs;

volatile LONG mbdr_dsp_always_true_f = 1;
ULONG mbdr_dsp_disable_get_flags;


void  mbdr_dsp_main_init_manager_verify_boot_loader_application_sync(void)
{/* mbdr_dsp_common_main_init_manager */
  SHORT i;


  mbdr_dsp_common_status[0].boot_loader_version_from_application
                                           = MBDR_DSP_BOOT_LOADER_SW_VERSION;
  mbdr_dsp_common_status[0].dsp_sw_version = mbdr_dsp_application_sw_version;

  /**********************************************/
  /* stick SW in a case of boot loader mismatch */
  /**********************************************/
  while(mbdr_dsp_common_status[0].boot_loader_version_from_boot
        !=
        mbdr_dsp_common_status[0].boot_loader_version_from_application)
  {/* MBDR_DSP_MAIN_MODULE_CODE_T_MAIN_BOOT_LOADER_VERSION_MISMATCH */
    
	/* reports boot loader mismatch error */
    MBDR_DSP_SET_SW_MAIN_MODULE_CODE_M(
        MBDR_DSP_MAIN_MODULE_CODE_T_MAIN_BOOT_LOADER_VERSION_MISMATCH);

    /* deleberate wait to enable host easy access to mbdr_dsp_common_status */
    for(i=1; i<1000; i++);

  }/* MBDR_DSP_MAIN_MODULE_CODE_T_MAIN_BOOT_LOADER_VERSION_MISMATCH */

}/* mbdr_dsp_common_main_init_manager */


void mbdr_dsp_common_main_init_manager_memory_init(
        const mbdr_dsp_main_init_manager_params_t
                             *pi_mbdr_dsp_main_init_manager_params_p,
        mbdr_dsp_boot_loader_config_t *po_mbdr_dsp_config_p)
{/* mbdr_dsp_common_main_init_manager_memory_init */

  SHORT i;
  LONG  physical_layer_buffer[2];

  /*************************************************************/
  /* clear status structure, except boot loader reports fields */
  /*************************************************************/
  memset(&mbdr_dsp_common_status[0], 
         0,
         /* clears part of structure */
          (ULONG)&mbdr_dsp_common_status[0].
                      boot_loader_version_from_application
		 -(ULONG)&mbdr_dsp_common_status[0]);

  

  /******************************************************/
  /* report application sw version                      */
  /******************************************************/
  mbdr_dsp_common_status[0].dsp_sw_version 
                          = mbdr_dsp_application_sw_version;



  /* initialize trade sw module */
  mbdr_dsp_trace_sw_module_init();

  /* signs arrive to main */
  MBDR_DSP_SET_SW_MAIN_MODULE_CODE_M(
                          MBDR_DSP_MAIN_MODULE_CODE_T_MAIN_START);
  MBDR_DSP_SET_MIDDLE_MODULE_CODE_STATUS_M(
          MBDR_DSP_MAIN_START_MIDDLE_MODULE_CODE_T_SEND_SIGNAL_APP);
  MBDR_DSP_TRACE_SW_MODULE_CODE_M; 		   

  /* after read sync message from CPU indication */
  mbdr_dsp_debug_signal_app(MBDR_DSP_MAIN_MODULE_CODE_T_MAIN_START,
                            &physical_layer_buffer[0]);

  /* initialize config area reset data */
  po_mbdr_dsp_config_p->common.self_activate_mode  
          = MBDR_DSP_CONFIG_SELF_ACTIVATE_MODE_T_DISABLE;
  po_mbdr_dsp_config_p->common.wait_in_place_after_self_activate_in_rtc_units
                                                                       = 0;
  po_mbdr_dsp_config_p->common.mission_self_activate_queue_used_entries = 1;
  po_mbdr_dsp_config_p->common.minimal_exception_report_severity_code   = 0;
  po_mbdr_dsp_config_p->common.sample_source_from_memory_f              = 0;
  po_mbdr_dsp_config_p->common.re_synchronization_request 
                         = MBDR_DSP_RE_SYNCHRONIZATION_REQUEST_T_NO_REQUEST;
  memset((void *)
         &po_mbdr_dsp_config_p->common.mission_self_activate_queue[0],
         0,
         MBDR_DSP_MISSION_SELF_ACTIVATE_QUEUE_SIZE*sizeof(USHORT));
  memset((void *)
         &po_mbdr_dsp_config_p->common.force_fatal_exception_flags[0],
         0,
         MBDR_DSP_EXCEPTIONS_NUMBER/MBDR_DSP_8);

  /* initialize application depend global list */
  for(i=0; i<MBDR_DSP_MAIN_INIT_MANAGER_PARAMS_INIT_LIST_SIZE; i++)
      *pi_mbdr_dsp_main_init_manager_params_p->init_list[i].dw_p =
	   pi_mbdr_dsp_main_init_manager_params_p->init_list[i].init_value;       

  MBDR_DSP_SET_MIDDLE_MODULE_CODE_STATUS_M(
        MBDR_DSP_MAIN_START_MIDDLE_MODULE_CODE_T_SEND_INIT_PERIPHERALS);

}/* mbdr_dsp_common_main_init_manager_memory_init */

void mbdr_dsp_common_main_init_wait_for_config_ready(
          volatile mbdr_dsp_boot_loader_config_t *pio_mbdr_dsp_config_p)
{/* mbdr_dsp_common_main_init_wait_for_config_ready */

	volatile LONG *config_ready_magic_dw_p;


	config_ready_magic_dw_p = (volatile LONG *)
			                  ((ULONG)pio_mbdr_dsp_config_p
			                   +DSP_CONFIG_READY_SW_MAGIC_WORD_BYTE_OFFSET);


    *config_ready_magic_dw_p = 0;


    /* waits till CPU signs that config is ready  */
    MBDR_DSP_SET_MIDDLE_MODULE_CODE_STATUS_M(
          MBDR_DSP_MAIN_START_MIDDLE_MODULE_CODE_T_WAIT_FOR_SYNC);
    *config_ready_magic_dw_p = 0;
    asm(" nop "); 
    asm(" nop ");
    asm(" nop "); 
    asm(" nop ");
    asm(" nop "); 
    asm(" nop ");
    asm(" nop "); 
    asm(" nop ");
	mbdr_dsp_main_init_bp_before_poll_dw_with_wait();
    asm(" nop "); 
    asm(" nop ");
    asm(" nop "); 
    asm(" nop ");
    asm(" nop "); 
    asm(" nop ");
    asm(" nop "); 
    asm(" nop ");
    mbdr_dsp_utils_poll_dw_with_wait(
    		        config_ready_magic_dw_p,
                    MBDR_DSP_CONFIG_READY_MAGIC_WORD,
                    1000);
}/* mbdr_dsp_common_main_init_wait_for_config_ready */

/* routine is used for break point in debug mode  */
void mbdr_dsp_main_init_bp_before_poll_dw_with_wait(void)
{/* mbdr_dsp_main_init_bp_before_poll_dw_with_wait */
      asm(" nop "); 
      asm(" nop ");
      asm(" nop "); 
      asm(" nop ");
      asm(" nop "); 
      asm(" nop ");
      asm(" nop "); 
      asm(" nop ");
}/* mbdr_dsp_main_init_bp_before_poll_dw_with_wait */

void mbdr_dsp_before_main_bp(void)
{/* mbdr_dsp_before_main_bp */
      asm(" nop "); 
      asm(" nop ");
      asm(" nop "); 
      asm(" nop ");
      asm(" nop "); 
      asm(" nop ");
      asm(" nop "); 
      asm(" nop ");
}/* mbdr_dsp_before_main_bp */

/*  routine can be used for dummy initilization, for                         */
/*  mbdr_dsp_main_init_manager_params_t.multi_core_platform_init_hook_proc_p */
void mbdr_dsp_common_main_init_multi_core_platform_init_hook_dummy(
             mbdr_dsp_common_config_multi_core_t pi_config_multi_core)
{/* mbdr_dsp_common_main_init_multi_core_platform_init_hook_dummy */

  /* avoid warning */
  mbdr_dsp_utils_dummy = pi_config_multi_core.spare_0;

}/* mbdr_dsp_common_main_init_multi_core_platform_init_hook_dummy */


