/*                                                                          */
/*      DESCRIPTION                                                         */
/*      functions to handle fast memory copy, designed to read from flash   */
/*      memory to internal DSP memory during boot loading process           */
/*                                                                          */
/*      performance measurements:                                           */
/*      test description: copy 1Mbyte from memory address 0x64000000 (flash)*/
/*                        to external memory                                */
/*      routine                              cycles      approx time(ms)    */
/*      ----------------------------------------------------------------    */
/*      mbdr_dsp_flash_read_memcpy           566231200   666.154            */
/*      mbdr_dsp_flash_read_memcpy_16_reg    521979032   614.092            */
/*      mbdr_dsp_flash_read_memcpy_32_reg    521892216   613.99             */
/*                                                                          */
#include "windef.h" 
#include "mbdr_dsp_flash_read_memcpy.h"


/*******************************************************************/
/* original routine - exist in boot loader program */
//void mbdr_dsp_flash_read_memcpy (void  *po_dst_ram_p,
//                                 void  *pi_src_flash_p,
//                                 ULONG   pi_byte_size)
//{/* mbdr_dsp_flash_read_memcpy */
//
//  LONG i;
//  SHORT *dst_ram_16bit_p, *src_flash_16bit_p;

//  dst_ram_16bit_p   = (SHORT *)po_dst_ram_p;
//  src_flash_16bit_p = (SHORT *)pi_src_flash_p;
//  for(i=0; i<pi_byte_size/sizeof(SHORT); i++)
//      *dst_ram_16bit_p++ = *src_flash_16bit_p++;

//}/* mbdr_dsp_flash_read_memcpy */
/*******************************************************************/

#define MBDR_DSP_FLASH_MEMCPY_RD_16_BIT_M(SRC_ADDR)\
    s0=SRC_ADDR++;\
    s1=SRC_ADDR++;\
    s2=SRC_ADDR++;\
	s3=SRC_ADDR++;\
	s4=SRC_ADDR++;\
	s5=SRC_ADDR++;\
	s6=SRC_ADDR++;\
	s7=SRC_ADDR++;\
	s8=SRC_ADDR++;\
	s9=SRC_ADDR++;\
	s10=SRC_ADDR++;\
	s11=SRC_ADDR++;\
	s12=SRC_ADDR++;\
	s13=SRC_ADDR++;\
	s14=SRC_ADDR++;\
	s15=SRC_ADDR++;
/*  */
#define MBDR_DSP_FLASH_MEMCPY_WR_16_BIT_M(DST_ADDR)\
    DST_ADDR++=s0;\
    DST_ADDR++=s1;\
    DST_ADDR++=s2;\
    DST_ADDR++=s3;\
	DST_ADDR++=s4;\
	DST_ADDR++=s5;\
	DST_ADDR++=s6;\
	DST_ADDR++=s7;\
	DST_ADDR++=s8;\
	DST_ADDR++=s9;\
	DST_ADDR++=s10;\
	DST_ADDR++=s11;\
	DST_ADDR++=s12;\
	DST_ADDR++=s13;\
	DST_ADDR++=s14;\
	DST_ADDR++=s15
void mbdr_dsp_flash_read_memcpy_16_reg(void  *po_dst_ram_p,
                                       void  *pi_src_flash_p,
                                       ULONG   pi_byte_size)
{/* mbdr_dsp_flash_read_memcpy_16_reg */

  LONG i;
  register SHORT s0, s1,  s2,  s3,  s4,  s5,  s6,  s7,
                 s8, s9, s10, s11, s12, s13, s14, s15;
  SHORT *dst_ram_16bit_p, *src_flash_16bit_p;

  dst_ram_16bit_p   = (SHORT *)po_dst_ram_p;
  src_flash_16bit_p = (SHORT *)pi_src_flash_p;

  for(i=0; i<(pi_byte_size/sizeof(SHORT)); i+=16)
  {
    MBDR_DSP_FLASH_MEMCPY_RD_16_BIT_M(*src_flash_16bit_p);
    MBDR_DSP_FLASH_MEMCPY_WR_16_BIT_M(*dst_ram_16bit_p);
  }

}/* mbdr_dsp_flash_read_memcpy_16_reg */





void mbdr_dsp_flash_read_memcpy_32_reg(void  *po_dst_ram_p,
                                       void  *pi_src_flash_p,
                                       ULONG   pi_byte_size)
{/* mbdr_dsp_flash_read_memcpy_32_reg */

  LONG i;
  register SHORT s0, s1,  s2,  s3,  s4,  s5,  s6,  s7,
                 s8, s9, s10, s11, s12, s13, s14, s15,
				 s16, s17,  s18,  s19,  s20,  s21,  s22,  s23,
                 s24, s25, s26, s27, s28, s29, s30, s31;
  SHORT *dst_ram_16bit_p, *src_flash_16bit_p;

  dst_ram_16bit_p   = (SHORT *)po_dst_ram_p;
  src_flash_16bit_p = (SHORT *)pi_src_flash_p;
  for(i=0; i<pi_byte_size/sizeof(SHORT); i+=32)
  {
    /* read 16 words from flash  */
    s0  = *src_flash_16bit_p++;
    s1  = *src_flash_16bit_p++;
    s2  = *src_flash_16bit_p++;
	s3  = *src_flash_16bit_p++;
	s4  = *src_flash_16bit_p++;
	s5  = *src_flash_16bit_p++;
	s6  = *src_flash_16bit_p++;
	s7  = *src_flash_16bit_p++;
	s8  = *src_flash_16bit_p++;
	s9  = *src_flash_16bit_p++;
	s10 = *src_flash_16bit_p++;
	s11 = *src_flash_16bit_p++;
	s12 = *src_flash_16bit_p++;
	s13 = *src_flash_16bit_p++;
	s14 = *src_flash_16bit_p++;
	s15 = *src_flash_16bit_p++;
    s16  = *src_flash_16bit_p++;
    s17  = *src_flash_16bit_p++;
    s18  = *src_flash_16bit_p++;
	s19  = *src_flash_16bit_p++;
	s20  = *src_flash_16bit_p++;
	s21  = *src_flash_16bit_p++;
	s22  = *src_flash_16bit_p++;
	s23  = *src_flash_16bit_p++;
	s24  = *src_flash_16bit_p++;
	s25  = *src_flash_16bit_p++;
	s26 = *src_flash_16bit_p++;
	s27 = *src_flash_16bit_p++;
	s28 = *src_flash_16bit_p++;
	s29 = *src_flash_16bit_p++;
	s30 = *src_flash_16bit_p++;
	s31 = *src_flash_16bit_p++;

	/* write 16 words to ram */
    *dst_ram_16bit_p++ = s0;
    *dst_ram_16bit_p++ = s1;
    *dst_ram_16bit_p++ = s2;
    *dst_ram_16bit_p++ = s3;
	*dst_ram_16bit_p++ = s4;
	*dst_ram_16bit_p++ = s5;
	*dst_ram_16bit_p++ = s6;
	*dst_ram_16bit_p++ = s7;
	*dst_ram_16bit_p++ = s8;
	*dst_ram_16bit_p++ = s9;
	*dst_ram_16bit_p++ = s10;
	*dst_ram_16bit_p++ = s11;
	*dst_ram_16bit_p++ = s12;
	*dst_ram_16bit_p++ = s13;
	*dst_ram_16bit_p++ = s14;
	*dst_ram_16bit_p++ = s15;
    *dst_ram_16bit_p++ = s16;
    *dst_ram_16bit_p++ = s17;
    *dst_ram_16bit_p++ = s18;
    *dst_ram_16bit_p++ = s19;
	*dst_ram_16bit_p++ = s20;
	*dst_ram_16bit_p++ = s21;
	*dst_ram_16bit_p++ = s22;
	*dst_ram_16bit_p++ = s23;
	*dst_ram_16bit_p++ = s24;
	*dst_ram_16bit_p++ = s25;
	*dst_ram_16bit_p++ = s26;
	*dst_ram_16bit_p++ = s27;
	*dst_ram_16bit_p++ = s28;
	*dst_ram_16bit_p++ = s29;
	*dst_ram_16bit_p++ = s30;
	*dst_ram_16bit_p++ = s31;
  }
}/* mbdr_dsp_flash_read_memcpy_32_reg */
