dm642loader.c

来自「dm642pci加载代码 DM642 DSP PCI loader, coul」· C语言 代码 · 共 598 行 · 第 1/2 页

C
598
字号

  /* check magic number */
  if (!ISCOFF(fhdr->f_magic))
  {
      swap2byte(&(fhdr->f_magic));
      if (!ISCOFF(fhdr->f_magic)) 
      {
	printf("bad magic number %d in target binary `%s' (not an executable)",
		fhdr->f_magic, fname);
	return 0;
      }
      byte_swapped = 1;
      /* swap the rest of the header */
      swap2byte(&(fhdr->f_nscns));
      swap4byte(&(fhdr->f_timdat));
      swap4byte(&(fhdr->f_symptr));
      swap4byte(&(fhdr->f_nsyms));
      swap2byte(&(fhdr->f_opthdr));
      swap2byte(&(fhdr->f_flags));
      swap2byte(&(fhdr->f_target_id));
  }

  /* goto aout header */
  ahdr = (coff_aouthdr *) fileptr;

/*printf(" DSP load(1): 'ahdr' is %lx\n", ahdr);
printf("DSP load(1): 'ahdr->entrypt' is %lx\n", ahdr->entrypt);
*/

  fileptr = fileptr + sizeof(coff_aouthdr);
  
  /* swap bytes in the aouthdr if necessary */
  if (byte_swapped == 1) 
  {
    swap2byte(&(ahdr->magic));
    swap2byte(&(ahdr->vstamp));
    swap4byte(&(ahdr->tsize));
    swap4byte(&(ahdr->dsize));
    swap4byte(&(ahdr->bsize));
    swap4byte(&(ahdr->entrypt));
    swap4byte(&(ahdr->text_start));
    swap4byte(&(ahdr->data_start));
  }


  /*printf("processing %d sections\n", fhdr->f_nscns);*/

  /* loop through the section headers and then the sections */
  fileptr_saved = fileptr;
  for (i = 0; i < fhdr->f_nscns; i++)
  {
/*printf("    section %d: ", i);*/
      fileptr = fileptr_saved;
      shdr = (coff_scnhdr *) fileptr;
      fileptr += sizeof(coff_scnhdr);
      fileptr_saved = fileptr;
      
      /* swap bytes in the scnhdr if necessary, except s_name */
      if (byte_swapped == 1) 
      {
	swap4byte(&(shdr->s_paddr));
	swap4byte(&(shdr->s_vaddr));
	swap4byte(&(shdr->s_size));
	swap4byte(&(shdr->s_scnptr));
	swap4byte(&(shdr->s_relptr));
	swap4byte(&(shdr->s_lnnoptr));
	swap4byte(&(shdr->s_nreloc));
	swap4byte(&(shdr->s_nlnno));
	swap4byte(&(shdr->s_flags));
	swap2byte(&(shdr->s_reserved));
	swap2byte(&(shdr->s_page));
      }


     /* printf("%8s %8u %8x %8x %8x %8x\n", shdr->s_name, shdr->s_size, 
            shdr->s_paddr, shdr->s_vaddr, shdr->s_scnptr, shdr->s_flags);*/

      if (shdr->s_flags == COFF_SECTYPE_COPY) /*skip copy section*/
      {
  	 /*printf("ignore copy section '%s' flag: 0x%xh, starting at 0x%08x with %d bytes\n", shdr->s_name, shdr->s_flags, shdr->s_vaddr, shdr->s_size);*/
      }
      else if (shdr->s_flags & COFF_SECTYPE_TEXT) /*load text section*/
      {
	if (shdr->s_size > 0) 
        {
	  /*printf("text section '%s', flag: 0x%xh, starting at 0x%08x with %d bytes\n", shdr->s_name,  shdr->s_flags, shdr->s_vaddr, shdr->s_size);*/
	  fileptr = fbuf + shdr->s_scnptr;
	  for (j = 0; j < shdr->s_size; j+=4) 
          {
	    UINT8 a, b, c, d;
	    UINT32 data;
	    a = *(fileptr+j);
	    b = *(fileptr+j+1);
	    c = *(fileptr+j+2);
	    d = *(fileptr+j+3);
	    data = (d << 24) | (c << 16) | (b << 8) | (a);
	    vDspMemoryWrite(shdr->s_vaddr + j, 4, data);
	  }
	}
	else
	{
	  /*printf("text section '%s' size equal to zero\n", shdr->s_name);*/
	}

      } 
      else if (shdr->s_flags & COFF_SECTYPE_DATA) /*load data section*/
      {

	if (shdr->s_size > 0) 
        {
	  /*printf("data section '%s', flag: 0x%xh, starting at 0x%08x with %d bytes\n", shdr->s_name, shdr->s_flags, shdr->s_vaddr, shdr->s_size);*/
	  fileptr = fbuf + shdr->s_scnptr;
	  for (j = 0; j < shdr->s_size; j+=4) 
          {
	    UINT8 a, b, c, d;
	    UINT32 data;
	    a = *(fileptr+j);
	    b = *(fileptr+j+1);
	    c = *(fileptr+j+2);
	    d = *(fileptr+j+3);
	    data = (d << 24) | (c << 16) | (b << 8) | (a);
	    vDspMemoryWrite(shdr->s_vaddr + j, 4, data);
	  }

	}
	else
	{
	    /*printf("data section '%s' size equal to zero\n", shdr->s_name);*/
	}

      } 
      else if (shdr->s_flags & COFF_SECTYPE_BSS) /*load bss section*/
      {
	  /*printf("irnore bss section '%s' starting at 0x%08x with %d bytes\n", shdr->s_name, shdr->s_vaddr, shdr->s_size);*/
      }
      else 
      {
          /*printf("ignore unknown section '%s' starting at 0x%08x with %d bytes\n", shdr->s_name, shdr->s_vaddr, shdr->s_size);*/
      }
	
    }


  u32EntryPoint = ahdr->entrypt;
  
 /* printf("DSP load: Free fbuf (%lx)\n", (long)fbuf);

  printf(" DSP load: 'ahdr' is %lx\n", ahdr);
  if (ahdr)
    printf("DSP load: 'ahdr->entrypt' is %lx\n", ahdr->entrypt);
*/

  free(fbuf);

  /*printf("DSP load: Done\n");*/

  return u32EntryPoint;
}
/*warm reset the DSP*/
void vDspReset()
{
	int i=0;
	sysOutByte(base2 + DM642_IO_HDCR, DM642_HDCR_WARMRESET);
	for(i=0; i<10000; i++)
	{
		/*wait 18 DSP cycle to get response*/
	}
	/*clear PCI interrupt and enable it
   	IER = 0;
	IFR = 0;*/

}


/*load the coff to dsp and get it running, filename: a coff filename (.out)*/
int iDspLoad(char* filename, int index)
{
	char buf = 0;
	if(!iDspPciInit(index))
	{
		printf("Can't find the DSP%d PCI device\n", index);
		return 0;
	}
	
	/*warm reset the dsp*/
	vDspReset();
	/*init the EMIF*/
	vDspEmifInit();
	/*verify memory*/
	if(!iDspVerifyMemory())
	{
		printf("DSP%d memory verification failed\n", index+1);
		return 0;
	}
	//else
	//	printf("DSP%d memory verified ok\n", index+1);

	/*clear 20M EMIF space to zero*/
	sysOutWord(base2+DM642_IO_DSPP, 0x200);
	memset((void*)base0, 0, 0x400000);
	sysOutWord(base2+DM642_IO_DSPP, 0x201);
	memset((void*)base0, 0, 0x400000);
	sysOutWord(base2+DM642_IO_DSPP, 0x202);
	memset((void*)base0, 0, 0x400000);
	sysOutWord(base2+DM642_IO_DSPP, 0x203);
	memset((void*)base0, 0, 0x400000);
	sysOutWord(base2+DM642_IO_DSPP, 0x204);
	memset((void*)base0, 0, 0x400000);


	/*set DSP page register to 0 and clear 256K IRAM space to zero*/
	DSPP = 0;
	sysOutWord(base2+DM642_IO_DSPP, DSPP);
	memset((void*)base0, 0, 0x40000);

	/*check the pci boot mode*/
	buf = sysInByte(base2 + DM642_IO_HDCR);
	if(!(buf&0x4))
	{
		printf("Not in PCI boot mode\n");
		return 0;
	}

	/*load .out file to DSP memory*/
	if (ulDspLoad(filename) == 0)
	{
		printf("DSP %d loading failed\n");
		return 0;
	}

	printf("DSP %d loaded\n", index);
	return 1;
}



int iSetLEDs(int iDSP, unsigned char ucData)
{
	UINT32 u32LEDreg = 0x90080017;

	// Set the addressing context
	iDspPciInit(iDSP);

	vDspEmifInit();

	vDspMemoryWrite(u32LEDreg, 1, ucData);

	return 0;
}

extern int bTestDsp;
int iDspVerifyMemory()
{
	int i, j = 0;
        int iTestRange = 0x1000; /*only verify 1k for every 4M*/
        if(bTestDsp)
		iTestRange = 0x400000; /*the QA team, we scan whole external memory*/	

	/*verify internal memory*/
	DSPP = 0;
	sysOutWord(base2+0x08, DSPP);
	memset(base0, '*', 0x40000);
	for(j=0; j<0x40000; j++)
	{
		if(*((UINT8*)(base0+j)) != '*')
		{
			printf("memory verification failed at 0x%x\n",j);
			return 0;
		}
	}
	printf("internal ram verified\n");
     /* DSP code will handle the external memory test, Dec, 2, 2006, Tao
  if(bTestDsp)
	   printf("Scanning external memory, this will take about 1 minute...");
	for(i=0; i<8; i++)
	{
		DSPP = 0x200+i;
		sysOutWord(base2+0x08, DSPP);
		memset(base0, '*',iTestRange);
		for(j=0; j<iTestRange; j++)  
		{
			if(*((UINT8*)(base0+j)) != '*')
			{
				printf("memory verification failed at 0x%x\n", i, 0x400000*DSPP+j);
				return 0;
			}
		}
	}

	printf("external ram verified\n");
*/
	return 1;
	
}




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?