/*                                                                          */
/* File Name   : mbdr_dsp_main_init_manager.c                               */
/*                                                                          */
/* Written By  : 79556                                                     */
/*                                                                          */
/* versions    :                                                            */
/*+--------+-----------+---------------------------------------------------+*/
/*| Version| Date      | Change content                                    |*/
/*+--------+-----------+---------------------------------------------------+*/
/*|        |14/Dec/2007|- Creation                                         |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.10|26/Dec/2007|- report application information in status         |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.11|03/Jan/2008|- re_synchronization_request                                  |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.22|15/May/2008|- report SW middle_module_code                     |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.30|03/Jul/2008|- config.Sample_Source_From_Memory_f               |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.45|23/Nov/2008|- mbdr_dsp_main_init_bp_before_poll_dw_with_wait   |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.50|22/Dec/2008|- MBDR_DSP_MAIN_START_MIDDLE_MODULE_CODE_T         |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.61|25/Jan/2009|- support delay between self operations            |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.69|03/Mrs/2009|- main_init_manager, init i/o after get 0x1234567  |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.01.81|19/May/2009|- mbdr_dsp_before_main_bp, debug break point       |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.33|09/Feb/2010|- boot loader synchronization check:               |*/
/*|        |           |  it enables verification of synchronization       |*/
/*|        |           |  between application and boot version, in a case  |*/
/*|        |           |  of mismatch SW will stand in place               |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.34|11/Feb/2010|- split mbdr_dsp_platform_proc_ptrs_t to           |*/
/*|        |           |   mbdr_dsp_platform_external_io_access_proc_ptrs_t|*/
/*|        |           |   mbdr_dsp_platform_core_process_proc_ptrs_t      |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.37|19/Feb/2010|- mbdr_dsp_common_main_init_multi_core_platform_   |*/
/*|        |           |    init_hook                                      |*/
/*|        |           |- mbdr_dsp_common_main_kernel_loop                 |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.38|24/Feb/2010|- mbdr_dsp_core_process_proc_ptrs_t                |*/
/*|        |           |- mbdr_dsp_platform_core_debug_proc_ptrs_t         |*/
/*|        |           |- main_init_managers manages synchronization       |*/
/*|        |           |  process of DSP & host(if necessary)              |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.39| 3/Mrs/2010|- mbdr_dsp_common_main_init_utils.c/h              |*/
/*+--------+-----------+---------------------------------------------------+*/
/*| 0.02.66|08/Feb/2011|- Documentation                                    |*/
/*+--------+-----------+---------------------------------------------------+*/
/*                                                                          */
/* Function    : manages initialization flow:                               */
/*                    boot loaded synchronization  check                    */
/*                    post platform depend pointers                         */
/*                    manages synchonization with host state machine        */
/*                    calls platform depend user hook(used for simulation)  */
/*                    finally it operates main loop                         */
/*                                                                          */
/* Inputs      : pi_mbdr_dsp_main_init_manager_params_p->                   */
/*      globals: operation_mode(global from host)                           */
/*               mbdr_dsp_platform_external_io_access_proc_ptrs_tbl         */
/*                                                                          */
/* Outputs:    : mbdr_dsp_platform_external_io_access_proc_ptrs             */
/*               mbdr_dsp_core_process_proc_ptrs                            */
/*                                                                          */

#include  "windef.h"
#include  "string.h"
#include  "mbdr_dsp_common_main_flow_data.h"
#include  "mbdr_dsp_common_main_init_manager.h"
#include  "mbdr_dsp_common_status.h"
#include  "mbdr_dsp_common_debug_signal_app.h"
#include  "mbdr_dsp_common_boot_loader_app.h"
#include  "mbdr_dsp_common_boot_loader_int.h"
#include  "mbdr_dsp_common_sync_msg.h"
#include  "mbdr_dsp_common_main_init_utils.h"

#include  "mbdr_dsp_platform_proc_ptrs.h"
#include  "mbdr_dsp_sw_version.h"
#include  "mbdr_dsp_utils.h"
#include  "mbdr_dsp_cpu_common_hwimage.h"
#include  "mbdr_dsp_system_utils.h"

void mbdr_dsp_common_main_init_manager(
              const mbdr_dsp_main_init_manager_params_t
                             *pi_mbdr_dsp_main_init_manager_params_p)
{/* mbdr_dsp_common_main_init_manager */

  SHORT operation_mode;
  register volatile mbdr_dsp_boot_loader_config_t *mbdr_dsp_config_p;
  mbdr_dsp_cpu_common_interface_t
              *dsp_cpu_common_interface_in_dsp_memory_p,
              *dsp_cpu_common_interface_in_cpu_memory_p;
  mbdr_dsp_common_config_multi_core_t my_config_multi_core;
  LONG  physical_layer_buffer[2];

  /* verify synchronization between boot loader & application */
  mbdr_dsp_main_init_manager_verify_boot_loader_application_sync();


  while(mbdr_dsp_always_true_f)
  {/* while(mbdr_dsp_always_true_f) */

    /* initilize helpful pointer  */
    mbdr_dsp_config_p =
       (volatile mbdr_dsp_boot_loader_config_t *)MBDR_DSP_CPU_DSP_CONFIG_ADDRESS;


    /* memory initialization */
    mbdr_dsp_common_main_init_manager_memory_init(
             pi_mbdr_dsp_main_init_manager_params_p,
             (mbdr_dsp_boot_loader_config_t *)mbdr_dsp_config_p);

    /* waits for config ready */
    mbdr_dsp_common_main_init_wait_for_config_ready(mbdr_dsp_config_p);


	/*******************************************************/
    /* initilizes helpful pointers                         */
    /*******************************************************/
	dsp_cpu_common_interface_in_cpu_memory_p =
      (mbdr_dsp_cpu_common_interface_t *)
               mbdr_dsp_config_p->common.mbdr_dsp_cpu_main_interface_p;
    dsp_cpu_common_interface_in_dsp_memory_p =
     pi_mbdr_dsp_main_init_manager_params_p->common_interface_dsp_image_p;
    my_config_multi_core = mbdr_dsp_config_p->common.multi_core;

    /* performs application depend internal peripherals init */
    pi_mbdr_dsp_main_init_manager_params_p->internal_peripherals_init_proc_p();

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

    /* application hook to initialize platform tables */
    pi_mbdr_dsp_main_init_manager_params_p->
       multi_core_platform_init_hook_proc_p(my_config_multi_core);

    /* initialize platform procs pointer up to operation mode */
    operation_mode = mbdr_dsp_config_p->common.operation_mode;
    memcpy(&mbdr_dsp_platform_external_io_access_proc_ptrs,
           &mbdr_dsp_platform_external_io_access_proc_ptrs_tbl[operation_mode],
           sizeof(mbdr_dsp_platform_external_io_access_proc_ptrs_t));
    memcpy(&mbdr_dsp_platform_core_debug_proc_ptrs,
           &mbdr_dsp_platform_core_debug_proc_tbl[operation_mode],
           sizeof(mbdr_dsp_platform_core_debug_proc_ptrs_t));
    memcpy(&mbdr_dsp_core_process_proc_ptrs,
           &mbdr_dsp_core_process_proc_ptrs_tbl[
                 my_config_multi_core.my_dsp_index
                *my_config_multi_core.multi_core_active_f],
           sizeof(mbdr_dsp_core_process_proc_ptrs_t));

    /*******************************************************/
    /* simulates host wake up in full system do nothing    */
    /*******************************************************/
	mbdr_dsp_platform_core_debug_proc_ptrs.host_wake_up();

	/*******************************************************/
    /* performs DSP core depend, work 0.2                  */
    /* then reports dsp_state_number=1                     */
    /*******************************************************/
    mbdr_dsp_core_process_proc_ptrs.init_0_2(my_config_multi_core);
	MBDR_DSP_UTILS_WRITE_32BIT_TO_DSP_AND_CPU_M(
	  1,
	  &dsp_cpu_common_interface_in_dsp_memory_p->dsp_state_number,
	  &dsp_cpu_common_interface_in_cpu_memory_p->dsp_state_number);

	/*****************************************************************/
    /* simulates host: poll dsp_state_number=1                       */
    /*                 performs work 1_0                             */
    /*                 reports cpu_state_number=1                    */
	/*****************************************************************/
	mbdr_dsp_platform_core_debug_proc_ptrs.host_poll_dsp_state_number(1);
	mbdr_dsp_platform_core_debug_proc_ptrs.host_sync_with_dsp_work_1_0();
	mbdr_dsp_platform_core_debug_proc_ptrs.host_write_cpu_state_number(1);


	/*******************************************************/
    /* waits till (cpu_state_number == 1)                  */
    /* performs DSP core depend, work 1.0                  */
    /* then reports dsp_state_number=2                     */
    /*******************************************************/
    mbdr_dsp_utils_poll_dw_with_wait(
        (volatile LONG *)
        &dsp_cpu_common_interface_in_dsp_memory_p->cpu_state_number,
        1,
        256);
    mbdr_dsp_core_process_proc_ptrs.init_2_0(my_config_multi_core);
	MBDR_DSP_UTILS_WRITE_32BIT_TO_DSP_AND_CPU_M(
	  2,
	  &dsp_cpu_common_interface_in_dsp_memory_p->dsp_state_number,
	  &dsp_cpu_common_interface_in_cpu_memory_p->dsp_state_number);

	/*****************************************************************/
    /* simulates host: poll dsp_state_number=2                       */
    /*                 performs work 1_0                             */
    /*                 reports cpu_state_number=2                    */
	/*****************************************************************/
	mbdr_dsp_platform_core_debug_proc_ptrs.host_poll_dsp_state_number(2);
	mbdr_dsp_platform_core_debug_proc_ptrs.host_write_cpu_state_number(2);


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

    /* In simulator mode simulates:            */
    /*    1. debug printing of HW commands     */
    /*    2. getting mission from host         */
    /*    3. getting descriptors from HW       */
    /* In other modes do nothing               */
    mbdr_dsp_platform_core_debug_proc_ptrs.host_write_mission_to_dsp();

    /* Initialize print stream  */
    mbdr_dsp_platform_core_debug_proc_ptrs.dsp_initialize_print_stream();

	/* used for break point for debug usage  */
    mbdr_dsp_before_main_bp();

    /******************************/
    /* operate main infinite loop */
    /******************************/
    mbdr_dsp_main_kernel_loop();

  }/* while(mbdr_dsp_always_true_f) */


  /* routine is practically never called since mbdr_dsp_??_main_loop */
  /* is endless loop, call purpose is linking boot loader            */
  mbdr_dsp_boot_loader_never_reach();

}/* mbdr_dsp_common_main_init_manager */
