/*                                                                          */
/* Function    : tracker memory allocation                                  */


#include <stdio.h>
#include <string.h>
#include  "windef.h"
#include  "mbdr_dsp_common_status.h"
#include  "mbdr_dsp_dbg_print_stream_puts.h"
#include  "mbdr_dsp_debug_print_utils.h"
#include  "mbdr_dsp_ti_utils.h"
#include  "mbdr_dsp_debug_prob.h"
#include  "mbdr_dsp_debug_probs.h"

char    mbdr_dsp_dbg_global_print_line[MBDR_DSP_DBG_GLOBAL_PRINT_LINE_SIZE];
CHAR  mbdr_dsp_q3_to_string[8][4]=
{
 {'.', '0', '0',  0}, /* 000 */
 {'.', '1', '2',  0}, /* 001 */
 {'.', '2', '5',  0}, /* 010 */
 {'.', '3', '7',  0}, /* 011 */
 {'.', '5', '0',  0}, /* 100 */
 {'.', '6', '2',  0}, /* 101 */
 {'.', '7', '5',  0}, /* 110 */
 {'.', '8', '7',  0}, /* 111 */
};



void mbdr_dsp_dbg_print_line(char pi_line_element_ascii_code)
{/* mbdr_dsp_dbg_print_line */

  //SHORT my_temp;

  /* clears line */
  MBDR_DSP_PRINT_CLEAR_GLOBAL_LINE_M;

  /* Print Delimiter */
  memset(&mbdr_dsp_dbg_global_print_line[0], pi_line_element_ascii_code, 80);
  mbdr_dsp_dbg_global_print_line[80] = 0;
  //MBDR_DSP_DEBUG_PRINT_LINE_PUT_EOL_M(my_temp);


  mbdr_dsp_dbg_print_output_stream_print(mbdr_dsp_dbg_print_stream_p,
                                         mbdr_dsp_dbg_global_print_line);

  /* clears line */
  MBDR_DSP_PRINT_CLEAR_GLOBAL_LINE_M;

}/* mbdr_dsp_dbg_print_line */


void mbdr_dsp_debug_print_fraction_of_s32_f16(LONG   pi_s32_f16,
                                              char  *pio_string_p)
{/* mbdr_dsp_debug_print_fraction_of_s32_f16 */

  LONG decimal_print;

  /* compansate only if fraction != 0*/
  if((pi_s32_f16 >= 0)
     |
     ((pi_s32_f16&0x0ffff) == 0))
     decimal_print =
       (MBDR_DSP_TI_UTILS_MUL_S32Q16_M((100<<16), (pi_s32_f16&0x0ffff)))/(1<<16);
  else
  {

     decimal_print =
       (MBDR_DSP_TI_UTILS_MUL_S32Q16_M((100<<16), (0x0ffff-(pi_s32_f16&0x0ffff))))/(1<<16);
  }


  sprintf(&pio_string_p[strlen(pio_string_p)], ".");
  sprintf(&pio_string_p[strlen(pio_string_p)], "%02d", decimal_print);

}/* mbdr_dsp_debug_print_fraction_of_s32_f16 */

void mbdr_dsp_debug_print_s32_q16(LONG  pi_s32_q16,
                                  LONG  pi_integer_part_string_size,
                                  char *pio_string_p)
{/* mbdr_dsp_debug_print_s32_q16 */
 char print_control[4] = {'%', '4', 'd', 0};
 //SHORT i;

 //if((pi_s32_q16 < 0)
 //    &
 //   (((pi_s32_q16 & 0x0ffff0000)^0x0ffff0000) == 0))
 //{
   /* replace 4 by user required string size */
 //  print_control[1] = '0' + pi_integer_part_string_size - 2;
 //  for(i=0; i<pi_integer_part_string_size-2; i++)
 //      sprintf(pio_string_p, " " );

 //  sprintf(pio_string_p, "-0" );
 //}
 //else
 //{

   /* replace 4 by user required string size */
   print_control[1] = '0' + pi_integer_part_string_size;
   //                 -(pi_s32_q16<0);

   sprintf(pio_string_p, &print_control[0], pi_s32_q16/(1<<16));
 //}
 mbdr_dsp_debug_print_fraction_of_s32_f16(pi_s32_q16,
                                          &pio_string_p[(strlen(pio_string_p))]);
}/* mbdr_dsp_debug_print_s32_q16 */




void mbdr_dsp_debug_terminate_bp(void)
{
      asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
      asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
}

void mbdr_dsp_debug_wait_and_terminate(LONG pi_nop_wait)
{/* mbdr_dsp_debug_wait_and_terminate */

  /* overcome simulator problem */
  mbdr_dsp_utils_nop_wait(pi_nop_wait);

  /* used for debugger break point  */
  asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
  asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
  mbdr_dsp_debug_terminate_bp();
  asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
  asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
  mbdr_dsp_dbg_general_end();

}/* mbdr_dsp_debug_wait_and_terminate */


void mbdr_dsp_dbg_general_end()
{/* mbdr_dsp_dbg_general_end */

 
  /* used for debugger break point  */
  asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
  asm(" nop "); asm(" nop "); asm(" nop "); asm(" nop ");
  
}/* mbdr_dsp_dbg_general_end */

void mbdr_dsp_print_new_line(void)
{/* mbdr_dsp_print_new_line */

   MBDR_DSP_PRINT_CLEAR_GLOBAL_LINE_M;
   //sprintf(MBDR_DSP_PRINT_GET_GLOBAL_LINE_END_M, " \n");
   mbdr_dsp_dbg_print_output_stream_print(mbdr_dsp_dbg_print_stream_p,
                                                 mbdr_dsp_dbg_global_print_line);
   /* clears line Delimiter */
   MBDR_DSP_PRINT_CLEAR_GLOBAL_LINE_M;

}/* mbdr_dsp_print_new_line */



void mbdr_dsp_dbg_print_line_to_stream_and_clear(
             mbdr_dsp_dbg_print_output_stream_procs_t *pio_output_stream_manage_p)
{/* mbdr_dsp_dbg_print_line_to_stream_and_clear */

  mbdr_dsp_dbg_print_output_stream_print(
       pio_output_stream_manage_p,
       &mbdr_dsp_dbg_global_print_line[0]);

  /* clears global line  */
  MBDR_DSP_PRINT_CLEAR_GLOBAL_LINE_M;

}/* mbdr_dsp_dbg_print_line_to_stream_and_clear */

