

#include "board.h"


#include <string.h>
#include <math.h>




/******************************************************************************
**                          FUNCTION DEFINITIONS
******************************************************************************/


#define TRACE_LEN 16 
unsigned int opt_old_trace [TRACE_LEN];
unsigned int opt_new_trace [TRACE_LEN];
unsigned int tx_link [TRACE_LEN];
unsigned int tx_src [TRACE_LEN];



/*
** The main function. Application starts here.
*/
#define NUM_SAMPLES_PER_AUDIO_CH (NUM_SAMPLES_PER_AUDIO_BUF/2)
float trace_buf_L[NUM_SAMPLES_PER_AUDIO_CH*4];
float trace_buf_R[NUM_SAMPLES_PER_AUDIO_CH*4];

float proc_buf_L[NUM_SAMPLES_PER_AUDIO_CH];
float proc_buf_R[NUM_SAMPLES_PER_AUDIO_CH];

// conver input audio int to float
// input is a signed short val with 2MSByte 0x0000
// convert to float (-1, 1)
float AuIn2Float(unsigned int in_val_i){
  float scale = 1.0/32767;
  float out_f = 0;
  short temp = 0;
  temp = in_val_i & 0xffff;
  out_f = scale * temp; 
  return out_f;
}

// conver float to output audio int 
// float val should be  (-1, 1)
// output is a signed short val with 2MSByte 0x0000
unsigned int Float2AuOut(float in_val_f){
  float scale = 32767;
  unsigned int out_i = 0;
  short temp_s = 0;
  int temp_i = 0;
  temp_s = in_val_f *scale; 
  temp_i =  temp_s;
  out_i = temp_i & 0xffff; 
  return out_i;
}

void test_malloc(unsigned int buf_len){
  unsigned int * ptr = NULL;
  unsigned int i, val;
  ptr = (unsigned int *) malloc(buf_len * sizeof(unsigned int));
  if(ptr == NULL){
	  printf("malloc failed\n");
  }
  else{
	  printf("malloc OK.\n");
  }

  val = 0xAA55AA55;
  for(i = 0; i < buf_len; i ++){
    ptr[i] = val;
  }
  for(i = 0; i < buf_len; i ++){
    unsigned int read = ptr[i];
    if(read != val){
      printf("Data check error, address:0x%x, write:0x%x, read:0x%x\n", i, val, read );
    }
  }
  printf("Data check PASS.\n");
  free(ptr);

}


#pragma DATA_SECTION(big_data, "ddr2_section");
#define DATA_LEN 65536
unsigned int big_data[DATA_LEN];

void enable_all_INT(){
  IntEnable(C674X_MASK_INT4);
  IntEnable(C674X_MASK_INT5);
  IntEnable(C674X_MASK_INT6);
}
void disable_all_INT(){
  IntDisable(C674X_MASK_INT4);
  IntDisable(C674X_MASK_INT5);
  IntDisable(C674X_MASK_INT6);
}


int main(void)
{
  unsigned int i, val;
    init_tx_buf();
  
  // board_init();



  val = 0xAA55AA55;
  for(i = 0; i < DATA_LEN; i ++){
    big_data[i] = val;
  }
  for(i = 0; i < DATA_LEN; i ++){
    unsigned int read = big_data[i];
    if(read != val){
      printf("Data check error, address:0x%x, write:0x%x, read:0x%x\n", i, val, read );
    }
  }
  printf("Data Array Check Done\n");
#if 1
  printf("Data Array Check Done\n");
  disable_all_INT();
    test_malloc(0x40000);
    enable_all_INT();
#endif

  while(1);
  
}

/***************************** End Of File ***********************************/
