#include "BOARD.h"
char fgets0(char *s,int n,FILE *F)
{
    s[0]=0;
    fgets(s,n,F);
    return(s[0]);
}

bool	BOARD::loaddata(const char *filename,char *block,unsigned long startaddr)
{
    FILE *FR;
    char dd[1024];
    char filetype;
    unsigned int i,u,len;
    bool r=true;

	i=strlen(filename);
	while((i>0)&&(filename[i-1]!='/')&&(filename[i-1]!='\\')&&(filename[i-1]!='.')) i--;
	if ((i>0)&&(filename[i-1]=='.')) {
		if (!stricmp(filename+i,"t")) filetype='t';
		else filetype='b';
	} else filetype='b';
	if (filetype=='b') {
		FR=fopen(filename,"rb");
		if (FR==NULL) return(FALSE);
		fseek(FR,0,SEEK_END);
		len=ftell(FR);
		fseek(FR,0,0);
		for(i=0;i<(len-(len&(sizeof(dd)-1)));i+=sizeof(dd)) {
			fread(dd,sizeof(dd),1,FR);
			if (!memcmp(block,"ICACHE",6)) 
				r=top->icache->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
			else if (!memcmp(block,"DCACHE",6)) 
				r=top->dcache->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
			else if (!memcmp(block,"EMBSRAM",7)) 
				r=top->sram->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
			else if (!memcmp(block,"EXTSRAM",7)) 
				r=extsram->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
			else if (!memcmp(block,"SDRAM",5)) 
				r=sdram_if->sdram->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
			startaddr+=sizeof(dd);
			if (!r) break;
		}
		i=len-i;
		if (r&&(i!=0)) {
			fread(dd,i,1,FR);
			if (!memcmp(block,"ICACHE",6)) 
				top->icache->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"DCACHE",6)) 
				top->dcache->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"EMBSRAM",7)) 
				top->sram->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"EXTSRAM",7)) 
				extsram->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"SDRAM",5)) 
				sdram_if->sdram->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
		}
	} else if (filetype=='t') {
		char s[128];
		FR=fopen(filename,"r");
		if (FR==NULL) return(FALSE);
		i=0;
		while(fgets0(s,100,FR)!=0) {
			sscanf(s,"%x",&u);
			dd[i]=u;
			i++;
			if (i==sizeof(dd)) {
				if (!memcmp(block,"ICACHE",6)) 
					r=top->icache->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
				else if (!memcmp(block,"DCACHE",6)) 
					r=top->dcache->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
				else if (!memcmp(block,"EMBSRAM",7)) 
					r=top->sram->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
				else if (!memcmp(block,"EXTSRAM",7)) 
					r=extsram->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
				else if (!memcmp(block,"SDRAM",5)) 
					r=sdram_if->sdram->loaddata((unsigned long *)dd,startaddr,sizeof(dd)/4);
				startaddr+=sizeof(dd);
				i=0;
				if (!r) break;
			}
		}
		if (r&&(i!=0)) {
			if (!memcmp(block,"ICACHE",6)) 
				top->icache->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"DCACHE",6)) 
				top->dcache->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"EMBSRAM",7)) 
				top->sram->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"EXTSRAM",7)) 
				extsram->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
			else if (!memcmp(block,"SDRAM",5)) 
				sdram_if->sdram->loaddata((unsigned long *)dd,startaddr,(i+3)/4);
		}
    }
    fclose(FR);
    return(true);
    return(1);
}
