Exemple #1
0
int MSA_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	FILE * f;
	unsigned char signature[3];

	floppycontext->hxc_printf(MSG_DEBUG,"MSA_libIsValidDiskFile");

	if(hxc_checkfileext(imgfile,"msa"))
	{
		f=hxc_fopen(imgfile,"rb");
		if(f)
		{
			fread(signature,3,1,f);
			hxc_fclose(f);
			if(signature[0]==0x0E && signature[1]==0x0F && signature[2]==0x00)
			{
				floppycontext->hxc_printf(MSG_DEBUG,"MSA_libIsValidDiskFile : MSA file !");
				return HXCFE_VALIDFILE;
			}
		}
	}

	floppycontext->hxc_printf(MSG_DEBUG,"MSA_libIsValidDiskFile : non MSA file !");
	return HXCFE_BADFILE;
}
Exemple #2
0
int VTR_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	FILE *f;
	vtrucco_picfileformatheader header;

	floppycontext->hxc_printf(MSG_DEBUG,"VTR_libIsValidDiskFile");

	if(imgfile)
	{
		if(strlen(imgfile))
		{
			f=hxc_fopen(imgfile,"rb");
			if(f==NULL)
			{
				return HXCFE_ACCESSERROR;
			}
			fread(&header,sizeof(header),1,f);
			hxc_fclose(f);

			if( !strncmp(header.HEADERSIGNATURE,"VTrucco",7))
			{
				floppycontext->hxc_printf(MSG_DEBUG,"VTR_libIsValidDiskFile : VTrucco file !");
				return HXCFE_VALIDFILE;
			}
			else
			{
				floppycontext->hxc_printf(MSG_DEBUG,"VTR_libIsValidDiskFile : non VTrucco file !");
				return HXCFE_BADFILE;
			}
		}
	}

	return HXCFE_BADPARAMETER;
}
Exemple #3
0
int EXTADF_libIsValidDiskFile(HXCFE_IMGLDR * imgldr_ctx,char * imgfile)
{
    FILE * f;
    unsigned char header[12];

    imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EXTADF_libIsValidDiskFile");

    if( hxc_checkfileext(imgfile,"adf") )
    {

        f=hxc_fopen(imgfile,"rb");
        if(f==NULL)
        {
            imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"EXTADF_libIsValidDiskFile : Cannot open %s !",imgfile);
            return HXCFE_ACCESSERROR;
        }

        memset(header,0,sizeof(header));
        fread(header,12,1,f);
        hxc_fclose(f);

        if(!strncmp((char*)header,"UAE-1ADF",8))
        {
            imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EXTADF_libIsValidDiskFile : Extended ADF file (new version)!");
            return HXCFE_VALIDFILE;
        }

        return HXCFE_BADFILE;
    }
    else
    {
        imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EXTADF_libIsValidDiskFile : non Extended ADF file !");
        return HXCFE_BADFILE;
    }
}
Exemple #4
0
int JV3_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	int offset1, offset2;
	unsigned short SectorPerTrack, NumberOfTrack, SectorSize, NumberOfEntries;
	unsigned int   total_data;
	unsigned char  StartIdSector,NumberOfSide;
	FILE *f;
	JV3SectorHeader sh[JV3_HEADER_MAX];

	floppycontext->hxc_printf(MSG_DEBUG,"JV3_libIsValidDiskFile");

	if( hxc_checkfileext(imgfile,"jv3") ||
		hxc_checkfileext(imgfile,"dsk")
		)
	{

		f=hxc_fopen(imgfile,"rb");
		if(f==NULL) 
		{
			floppycontext->hxc_printf(MSG_ERROR,"JV3_libIsValidDiskFile : Cannot open %s !",imgfile);
			return HXCFE_ACCESSERROR;
		}					
		
		fread(sh, sizeof(JV3SectorHeader), JV3_HEADER_MAX, f);
       	if ((total_data = JV3_disk_geometry(sh, &NumberOfSide, &SectorPerTrack, &NumberOfTrack, &SectorSize, &StartIdSector, &NumberOfEntries)) != 0)
		{
			offset1 = ftell(f);
			fseek (f , 0 , SEEK_END);
			offset2 = ftell(f); 
			hxc_fclose(f);

			if (total_data == (unsigned int)(offset2 - offset1 -1)) {
				floppycontext->hxc_printf(MSG_DEBUG,"JV3_libIsValidDiskFile : JV3 file !");
				return HXCFE_VALIDFILE;
			} else {
				floppycontext->hxc_printf(MSG_DEBUG,"JV3_libIsValidDiskFile : non JV3 file !");
				return HXCFE_BADFILE;
			}
		}
		else
		{
			hxc_fclose(f);
			floppycontext->hxc_printf(MSG_DEBUG,"JV3_libIsValidDiskFile : non JV3 file !");
			return HXCFE_BADFILE;
		}
	}
	else
	{
		floppycontext->hxc_printf(MSG_DEBUG,"JV3_libIsValidDiskFile : non JV3 file !");
		return HXCFE_BADFILE;
	}

	return HXCFE_BADPARAMETER;
}
int VDK_libIsValidDiskFile(HXCFE_IMGLDR * imgldr_ctx,char * imgfile)
{
	int filesize;
	vdk_header vdk_h;
	FILE * f;

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"VDK_libIsValidDiskFile");
	if(imgfile)
	{

		f=hxc_fopen(imgfile,"rb");
		if(f==NULL)
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"VDK_libIsValidDiskFile : Cannot open %s !",imgfile);
			return -1;
		}

		fseek (f , 0 , SEEK_END);
		filesize=ftell(f);
		fseek (f , 0 , SEEK_SET);

		fread(&vdk_h,sizeof(vdk_header),1,f);

		hxc_fclose(f);

		if(vdk_h.signature==0x6B64)
		{
			filesize=filesize-vdk_h.header_size;

			if(filesize%256)
			{
				imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"VDK_libIsValidDiskFile : non VDK file !");
				return HXCFE_BADFILE;
			}

			imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"VDK_libIsValidDiskFile : VDK file !");
			return HXCFE_VALIDFILE;
		}

		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"VDK_libIsValidDiskFile : non VDK file !");
		return HXCFE_BADFILE;
	}

	return HXCFE_BADPARAMETER;
}
Exemple #6
0
int GKH_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	gkh_header header;
	FILE * f;

	floppycontext->hxc_printf(MSG_DEBUG,"GKH_libIsValidDiskFile");

	if( hxc_checkfileext(imgfile,"gkh"))
	{

		f=hxc_fopen(imgfile,"rb");
		if(f==NULL) 
		{
			floppycontext->hxc_printf(MSG_ERROR,"GKH_libIsValidDiskFile : Cannot open %s !",imgfile);
			return HXCFE_ACCESSERROR;
		}

		fread(&header,sizeof(header),1,f);
		hxc_fclose(f);

		if(!memcmp(&header.header_tag,"TDDFI",5))
		{
			floppycontext->hxc_printf(MSG_DEBUG,"GKH_libIsValidDiskFile : GKH file !");
		}
		else
		{
			floppycontext->hxc_printf(MSG_ERROR,"GKH_libIsValidDiskFile : Bad header !!");
			return HXCFE_BADFILE;
		}

		return HXCFE_VALIDFILE;
	}
	else
	{
		floppycontext->hxc_printf(MSG_DEBUG,"GKH_libIsValidDiskFile : non GKH file !");
		return HXCFE_BADFILE;
	}

	return HXCFE_BADPARAMETER;
}
int STX_libIsValidDiskFile(HXCFE_IMGLDR * imgldr_ctx,char * imgfile)
{
	FILE * f;
	pasti_fileheader * fileheader;

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"STX_libIsValidDiskFile");

	if(hxc_checkfileext(imgfile,"stx"))
	{
		f=hxc_fopen(imgfile,"rb");
		if(f==NULL)
			return HXCFE_ACCESSERROR;

		fileheader=(pasti_fileheader*)malloc(sizeof(pasti_fileheader));
		fread( fileheader, sizeof(pasti_fileheader), 1, f );

		hxc_fclose(f);

		if(strcmp((char*)&fileheader->headertag,"RSY"))
		{
			free(fileheader);
			imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"STX_libIsValidDiskFile : non STX file (bad header)!");
			return HXCFE_BADFILE;
		}
		else
		{
			free(fileheader);
			imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"STX_libIsValidDiskFile : STX file !");
			return HXCFE_VALIDFILE;
		}
	}
	else
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"STX_libIsValidDiskFile : non STX file !");
		return HXCFE_BADFILE;
	}
}
Exemple #8
0
int HFE_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	FILE *f;
	picfileformatheader header;

	floppycontext->hxc_printf(MSG_DEBUG,"HFE_libIsValidDiskFile");

	if(imgfile)
	{
		f=hxc_fopen(imgfile,"rb");
		if(f==NULL) 
		{
			return HXCFE_ACCESSERROR;
		}
		fread(&header,sizeof(header),1,f);
		hxc_fclose(f);

		if( !strcmp(header.HEADERSIGNATURE,"HXCPICFE"))
		{
			floppycontext->hxc_printf(MSG_DEBUG,"HFE_libIsValidDiskFile : HFE file !");
			return HXCFE_VALIDFILE;
		}
		else
		{
			floppycontext->hxc_printf(MSG_DEBUG,"HFE_libIsValidDiskFile : non HFE file !");
			return HXCFE_BADFILE;
		}
	}
	else
	{
		floppycontext->hxc_printf(MSG_DEBUG,"HFE_libIsValidDiskFile : non HFE file !");
		return HXCFE_BADFILE;
	}

	return HXCFE_BADPARAMETER;
}
int OLDEXTADF_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	FILE * f;
	unsigned char header[12];

	floppycontext->hxc_printf(MSG_DEBUG,"OLDEXTADF_libIsValidDiskFile");

	if( hxc_checkfileext(imgfile,"adf") )
	{
		f=hxc_fopen(imgfile,"rb");
		if(f==NULL)
		{
			floppycontext->hxc_printf(MSG_ERROR,"OLDEXTADF_libIsValidDiskFile : Cannot open %s !",imgfile);
			return HXCFE_ACCESSERROR;
		}

		memset(header,0,sizeof(header));
		fread(header,12,1,f);
		hxc_fclose(f);

		if(!strncmp((char*)header,"UAE--ADF",8))
		{
			floppycontext->hxc_printf(MSG_DEBUG,"OLDEXTADF_libIsValidDiskFile : Extended ADF file (old version)!");
			return HXCFE_VALIDFILE;
		}

		return HXCFE_BADFILE;
	}
	else
	{
		floppycontext->hxc_printf(MSG_DEBUG,"OLDEXTADF_libIsValidDiskFile : non Old Extended ADF file !");
		return HXCFE_BADFILE;
	}

	return HXCFE_BADPARAMETER;
}
Exemple #10
0
int FDI_libIsValidDiskFile(HXCFLOPPYEMULATOR* floppycontext,char * imgfile)
{
	FILE *f;
	fdi_header f_header;

	floppycontext->hxc_printf(MSG_DEBUG,"FDI_libIsValidDiskFile");

	if( hxc_checkfileext(imgfile,"fdi") )
	{
	
		f=hxc_fopen(imgfile,"rb");
		if(f)
		{
			fread(&f_header,sizeof(fdi_header),1,f);
			hxc_fclose(f);

			if(f_header.signature[0]=='F' && f_header.signature[1]=='D' && f_header.signature[2]=='I')
			{
				floppycontext->hxc_printf(MSG_DEBUG,"FDI_libIsValidDiskFile : FDI file !");
				return HXCFE_VALIDFILE;
			}

			floppycontext->hxc_printf(MSG_DEBUG,"FDI_libIsValidDiskFile : non FDI file !");
			return HXCFE_BADFILE;
		}

		return HXCFE_ACCESSERROR;
	}
	else
	{
		floppycontext->hxc_printf(MSG_DEBUG,"FDI_libIsValidDiskFile : non FDI file !");
		return HXCFE_BADFILE;
	}

	return HXCFE_BADPARAMETER;
}
int FEI_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	int i,j,k,filesize;
	int rpm;
	HXCFE_CYLINDER* currentcylinder;
	HXCFE_SIDE* currentside;
	int tracksize;


	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"FEI_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);

	if(!(filesize%25000))
	{
		floppydisk->floppyNumberOfTrack=(filesize/2) / 25000;
		floppydisk->floppyNumberOfSide=2;
		floppydisk->floppyBitRate=500000;
		floppydisk->floppySectorPerTrack=-1;
		floppydisk->floppyiftype=ATARIST_DD_FLOPPYMODE;
		tracksize=25000;
		rpm=300;
	}
	else
	{

		if(!(filesize%12500))
		{
			floppydisk->floppyNumberOfTrack=(filesize/2) / 12500;
			floppydisk->floppyNumberOfSide=2;
			floppydisk->floppyBitRate=250000;
			floppydisk->floppySectorPerTrack=-1;
			floppydisk->floppyiftype=ATARIST_DD_FLOPPYMODE;
			tracksize=12500;
			rpm=300;
		}
	}

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"FEI File : %d track, %d side, %d bit/s, %d sectors, mode %d",
		floppydisk->floppyNumberOfTrack,
		floppydisk->floppyNumberOfSide,
		floppydisk->floppyBitRate,
		floppydisk->floppySectorPerTrack,
		floppydisk->floppyiftype);


	floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);
	memset(floppydisk->tracks,0,sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);

	for(i=0;i<floppydisk->floppyNumberOfTrack;i++)
	{

		for(j=0;j<floppydisk->floppyNumberOfSide;j++)
		{

			hxcfe_imgCallProgressCallback(imgldr_ctx, (i<<1) + (j&1),floppydisk->floppyNumberOfTrack*2);

			if(!floppydisk->tracks[i])
			{
				floppydisk->tracks[i]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
				currentcylinder=floppydisk->tracks[i];
			}

			currentcylinder->sides[j]=tg_alloctrack(floppydisk->floppyBitRate,UNKNOWN_ENCODING,currentcylinder->floppyRPM,tracksize*8,2500,-2500,0x00);
			currentside=currentcylinder->sides[j];
			currentside->number_of_sector=floppydisk->floppySectorPerTrack;

			fseek(f,(tracksize*i)+(tracksize*floppydisk->floppyNumberOfTrack*j),SEEK_SET);

			imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"read track %d side %d at offset 0x%x (0x%x bytes)",
												i,
												j,
												ftell(f),
												tracksize);

			fread(currentside->databuffer,tracksize,1,f);

			for(k=0;k<tracksize;k++)
			{
				currentside->databuffer[k]=bit_inverter[currentside->databuffer[k]];
			}
		}
	}

	hxc_fclose(f);
	return HXCFE_NOERROR;
}
Exemple #12
0
int MSA_libLoad_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	unsigned int filesize;
	unsigned int i,j,k,l,l2;
	unsigned int file_offset;
	unsigned char * flatimg,c,skew;
	unsigned char   gap3len,interleave,numberofside,numberofsectorpertrack;
	unsigned short  rpm;
	unsigned short  sectorsize;
	unsigned short  numberoftrack;
	unsigned int    extractfilesize,filetracksize;
	unsigned char   fileheader[5*2];
	unsigned char   trackheader[1*2];
	unsigned char*  tmpbuffer;
	unsigned long   len;
	unsigned char   trackformat;

	CYLINDER* currentcylinder;
	
	floppycontext->hxc_printf(MSG_DEBUG,"MSA_libLoad_DiskFile %s",imgfile);
	
	f=hxc_fopen(imgfile,"rb");
	if(f==NULL) 
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}
	
	fseek (f , 0 , SEEK_END); 
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET); 
	if(filesize!=0)
	{		
		sectorsize=512; // msa file support only 512bytes/sector floppies.
		
		fread(fileheader,5*sizeof(unsigned short),1,f);
		if(fileheader[0]==0x0E && fileheader[1]==0x0F)
		{
			numberoftrack=((256*fileheader[8])+fileheader[9])+1;
			numberofside=((256*fileheader[4])+fileheader[5])+1;
			numberofsectorpertrack=fileheader[2]*256+fileheader[3];
			
			extractfilesize=(numberofsectorpertrack*512)*(numberoftrack+1)*(numberofside);
			
			flatimg=(unsigned char*)malloc(extractfilesize);
			memset(flatimg,0,extractfilesize);
			
			// chargement et decompression msa.
			j=0;
			i=0;
			do
			{
				fread(trackheader,2,1,f);
				filetracksize=((trackheader[0]*256)+trackheader[1]);
				if(filetracksize==((unsigned int)numberofsectorpertrack*512))
				{
					tmpbuffer=(unsigned char*)malloc(filetracksize);
					memset(tmpbuffer,0,filetracksize);
					fread(tmpbuffer,filetracksize,1,f);
					memcpy(flatimg+j,tmpbuffer,filetracksize);
					free(tmpbuffer);
					j=j+filetracksize;
				}
				else
				{
					k=0;
					l=0;
					tmpbuffer=(unsigned char*)malloc(filetracksize);
					memset(tmpbuffer,0,filetracksize);

					fread(tmpbuffer,filetracksize,1,f);
					do
					{
						if(tmpbuffer[k]!=0xE5)
						{
							if(l+j>extractfilesize)
							{
								return HXCFE_FILECORRUPTED;
							}
							
							flatimg[l+j]=tmpbuffer[k];
							l++;
							k++;
						}
						else
						{
							k++;
							c=tmpbuffer[k];
							k++;
							len=256*tmpbuffer[k];
							k++;
							len=len+tmpbuffer[k];
							k++;
							
							if(l+j+len>extractfilesize)
							{
								return HXCFE_FILECORRUPTED;
							}
							else
							{
								for(l2=0;l2<len;l2++)
								{
									flatimg[l+j]=c;
									l++;
								}
							}

						}
					}while(k<filetracksize);
					
					j=j+l;
					
					free(tmpbuffer);
				}
				
				i++;
			}while(i<(unsigned int)(numberoftrack*(numberofside)));
			
			hxc_fclose(f);
			
			floppydisk->floppyNumberOfTrack=numberoftrack;
			floppydisk->floppyNumberOfSide=numberofside;
			floppydisk->floppySectorPerTrack=numberofsectorpertrack;

			if(floppydisk->floppySectorPerTrack<15)
			{
				floppydisk->floppyBitRate=DEFAULT_DD_BITRATE;
				floppydisk->floppyiftype=ATARIST_DD_FLOPPYMODE;
				skew=2;
			}
			else
			{
				floppydisk->floppyiftype=ATARIST_HD_FLOPPYMODE;
				floppydisk->floppyBitRate=DEFAULT_HD_BITRATE;
				skew=4;
			}

			trackformat=ISOFORMAT_DD;
			gap3len=84;
			interleave=1;
			switch(floppydisk->floppySectorPerTrack)
			{
				case 10:
					gap3len=30;
				break;
				case 11:
					trackformat=ISOFORMAT_DD11S;
					gap3len=3;
					interleave=2;
				break;
				case 19:
					gap3len=70;
				break;
				case 20:
					gap3len=40;
				break;
				case 21:
					gap3len=18;
				break;
			}

			floppydisk->tracks=(CYLINDER**)malloc(sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);
			
			rpm=300;
								
			for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
			{
				
				floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
				currentcylinder=floppydisk->tracks[j];
				
				for(i=0;i<floppydisk->floppyNumberOfSide;i++)
				{
					file_offset=(sectorsize*(j*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide))+
								(sectorsize*(floppydisk->floppySectorPerTrack)*i);
					
					currentcylinder->sides[i]=tg_generateTrack(&flatimg[file_offset],sectorsize,floppydisk->floppySectorPerTrack,(unsigned char)j,(unsigned char)i,1,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,rpm,trackformat,gap3len,0,2500,-2500);
				}
			}
			free(flatimg);
			
			floppycontext->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");
		}
		return HXCFE_NOERROR;
	}
	
	floppycontext->hxc_printf(MSG_ERROR,"file size=%d !?",filesize);
	return HXCFE_BADFILE;
}
Exemple #13
0
int FDI_libLoad_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	
	FILE * f;
	unsigned int  i,j,k,file_size;
	unsigned char interleave;
	unsigned short rpm;
	int number_of_track,number_of_side,number_of_sectorpertrack;
	unsigned char tempsector[256];
	unsigned char trackformat;
	unsigned char skew;
	unsigned int  trackoffset,tempoffset,file_offset;
	SECTORCONFIG* sectorconfig;
	CYLINDER* currentcylinder;

	fdi_header f_header;
	fdi_track_header track_header;
	fdi_sector_header sector_header;

	floppycontext->hxc_printf(MSG_DEBUG,"FDI_libLoad_DiskFile %s",imgfile);
	
	f=hxc_fopen(imgfile,"rb");
	if(f==NULL) 
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}
	

	fseek(f,0,SEEK_END);
	file_size=ftell(f);
	fseek(f,0,SEEK_SET);

	fread(&f_header,sizeof(fdi_header),1,f);

	if(f_header.signature[0]!='F' || f_header.signature[1]!='D' || f_header.signature[2]!='I')
	{
		floppycontext->hxc_printf(MSG_ERROR,"Bad FDI file !");
		hxc_fclose(f);
		return HXCFE_BADFILE;
	}

	fseek(f,f_header.diskdescription_offset,SEEK_SET);
	fread(tempsector,f_header.data_offset - f_header.diskdescription_offset,1,f);
	floppycontext->hxc_printf(MSG_INFO_1,"Disk:%s",tempsector);


	trackoffset=f_header.additionnal_infos_len+0xE;
	fseek(f,trackoffset,SEEK_SET);

	number_of_track=f_header.number_of_cylinders;
	number_of_side =f_header.number_of_heads;
	number_of_sectorpertrack=-1;

	rpm=300;
	interleave=1;
	skew=0;

	floppydisk->floppyBitRate=250000;
	floppydisk->floppyiftype=GENERIC_SHUGART_DD_FLOPPYMODE;
	floppydisk->floppyNumberOfTrack=number_of_track;
	floppydisk->floppyNumberOfSide=number_of_side;
	floppydisk->floppySectorPerTrack=number_of_sectorpertrack;
	floppydisk->tracks=(CYLINDER**)malloc(sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);
	trackformat=IBMFORMAT_DD;		
	floppycontext->hxc_printf(MSG_DEBUG,"rpm %d bitrate:%d track:%d side:%d sector:%d",rpm,floppydisk->floppyBitRate,floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,floppydisk->floppySectorPerTrack);
		
			
	for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
	{	
		floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
		currentcylinder=floppydisk->tracks[j];
				
		for(i=0;i<floppydisk->floppyNumberOfSide;i++)
		{
			fread(&track_header,sizeof(fdi_track_header),1,f);
			floppycontext->hxc_printf(MSG_DEBUG,"[%d:%d] %d sectors, Track Offset :0x%x:",j,i,track_header.number_of_sectors,track_header.track_offset+f_header.data_offset);

			sectorconfig=(SECTORCONFIG*)malloc(sizeof(SECTORCONFIG)*track_header.number_of_sectors);
			memset(sectorconfig,0,sizeof(SECTORCONFIG)*track_header.number_of_sectors);
			
			for(k=0;k<track_header.number_of_sectors;k++)
			{
				fread(&sector_header,sizeof(fdi_sector_header),1,f);

				file_offset=f_header.data_offset+track_header.track_offset+sector_header.sector_offset;

				floppycontext->hxc_printf(MSG_DEBUG,"[%d:%d] Cyl:%d,Head:%d,Sec:%d,Size:%d,Flags:0x%.2X,Offset:0x%.8x",
					j,i,sector_header.cylinder_number,                                                                                                   
					sector_header.head_number,
					sector_header.sector_number,
					128<<sector_header.sector_size,
					sector_header.flags,
					file_offset
					);

				tempoffset=ftell(f);

				sectorconfig[k].cylinder=sector_header.cylinder_number;
				sectorconfig[k].head=sector_header.head_number;
				sectorconfig[k].sector=sector_header.sector_number;
				sectorconfig[k].sectorsize=128<<sector_header.sector_size;
				sectorconfig[k].gap3=255;
				sectorconfig[k].fill_byte=246;
				sectorconfig[k].bitrate=floppydisk->floppyBitRate;
				sectorconfig[k].trackencoding=trackformat;
				
				if(!(sector_header.flags&0x1F))
				{
					sectorconfig[k].use_alternate_data_crc=0xFF;
					sectorconfig[k].data_crc=0xAAAA;
				}

				if(sector_header.flags&0x80)
				{
					sectorconfig[k].alternate_datamark=0xF8;
					sectorconfig[k].use_alternate_datamark=1;
				}
				
				if(file_offset<file_size)
				{
					sectorconfig[k].input_data=malloc(sectorconfig[k].sectorsize);
					fseek(f,file_offset,SEEK_SET);
					fread(sectorconfig[k].input_data,sectorconfig[k].sectorsize,1,f);
				}

				fseek(f,tempoffset,SEEK_SET);
				
			}
					
			currentcylinder->sides[i]=tg_generateTrackEx(track_header.number_of_sectors,sectorconfig,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,rpm,trackformat,0,2500|NO_SECTOR_UNDER_INDEX,-2500);

			for(k=0;k<track_header.number_of_sectors;k++)
			{
				free(sectorconfig[k].input_data);
			}
			free(sectorconfig);
		}
	}
			
			
	floppycontext->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");

	hxc_fclose(f);

	hxcfe_sanityCheck(floppycontext,floppydisk);

	return HXCFE_NOERROR;
}
Exemple #14
0
int JV3_libLoad_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppydisk,char * imgfile,void * parameters)
{

	FILE * f;
	unsigned int filesize,cur_pos;
	unsigned int i,j,k,bitrate;
	unsigned short SectorSize, NumberofEntries;
	unsigned char  gap3len,interleave,StartIdSector;
	unsigned short rpm;
	unsigned char  trackformat;
	unsigned short sector_found;

	SECTORCONFIG*	sectorconfig;
	CYLINDER*		currentcylinder;

	JV3SectorHeader sh[JV3_HEADER_MAX];
	JV3SectorsOffsets *pOffset, *SectorsOffsets;
	unsigned char write_protected;
	unsigned int inc;

	floppycontext->hxc_printf(MSG_DEBUG,"JV3_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);

	if(filesize!=0)
	{
		fread(sh, sizeof(JV3SectorHeader), JV3_HEADER_MAX, f);

		JV3_disk_geometry(sh, &floppydisk->floppyNumberOfSide, &floppydisk->floppySectorPerTrack, &floppydisk->floppyNumberOfTrack, &SectorSize, &StartIdSector, &NumberofEntries);
		
		fread(&write_protected, sizeof(write_protected), 1, f);                                                                         // just to jump this infomation


		SectorsOffsets = JV3_offset(sh, floppydisk->floppyNumberOfSide, floppydisk->floppySectorPerTrack, floppydisk->floppyNumberOfTrack, NumberofEntries, f);

		bitrate=250000;
		rpm=300;
		interleave=1;
		gap3len=255;
		trackformat=IBMFORMAT_SD;

		floppydisk->floppyBitRate=bitrate;
		floppydisk->floppyiftype=GENERIC_SHUGART_DD_FLOPPYMODE;
		floppydisk->tracks=(CYLINDER**)malloc(sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);

		floppycontext->hxc_printf(MSG_DEBUG,"rpm %d bitrate:%d track:%d side:%d sector:%d",rpm,bitrate,floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,floppydisk->floppySectorPerTrack);

		sectorconfig=(SECTORCONFIG*)malloc(sizeof(SECTORCONFIG)*floppydisk->floppySectorPerTrack);
		memset(sectorconfig,0,sizeof(SECTORCONFIG)*floppydisk->floppySectorPerTrack);

		
		for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
		{

			floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
			currentcylinder=floppydisk->tracks[j];

			for(i=0;i<floppydisk->floppyNumberOfSide;i++)
			{
				inc = 0;                                    // used to build track data
				memset(sectorconfig,0,sizeof(SECTORCONFIG)*floppydisk->floppySectorPerTrack);
				sector_found=0;

				cur_pos = GetFirstPos(SectorsOffsets,NumberofEntries,j,i);

				for(k=0;k<floppydisk->floppySectorPerTrack;k++)
				{
					pOffset = GetSectorPosition(SectorsOffsets,NumberofEntries,cur_pos);

		    		if (pOffset == NULL) 
					{
						inc += SectorSize;
					} 
					else 
					{
						
						sectorconfig[sector_found].sectorsize=pOffset->size;
						sectorconfig[sector_found].input_data=malloc(sectorconfig[sector_found].sectorsize);
						memset(sectorconfig[sector_found].input_data,0,sectorconfig[sector_found].sectorsize);

						fseek(f, pOffset->offset, SEEK_SET);
						fread(sectorconfig[sector_found].input_data,pOffset->size,1,f);
						
						inc += pOffset->size;
						
						if (pOffset->DAM != 0xFB) 
						{
							sectorconfig[sector_found].use_alternate_datamark=1;
							sectorconfig[sector_found].alternate_datamark=pOffset->DAM;
						}
						
						if(pOffset->density)
						{
							sectorconfig[sector_found].trackencoding=IBMFORMAT_DD;
							if(!sector_found) trackformat=IBMFORMAT_DD;
						}
						else
						{
							sectorconfig[sector_found].trackencoding=IBMFORMAT_SD;
							if(!sector_found) trackformat=IBMFORMAT_SD;
						}

						if(pOffset->bad_sector)
						{
							sectorconfig[sector_found].use_alternate_data_crc = 0x01;
							sectorconfig[sector_found].data_crc = 0xAA55;
						}

						sectorconfig[sector_found].cylinder = pOffset->track_id;
						sectorconfig[sector_found].head = i;
						sectorconfig[sector_found].sector = pOffset->sector_id;
						sectorconfig[sector_found].bitrate = floppydisk->floppyBitRate;
						sectorconfig[sector_found].gap3 = gap3len;

						sector_found++;

					}

					cur_pos = GetNextPos(SectorsOffsets,NumberofEntries,j,i,cur_pos);

				}
				currentcylinder->sides[i]=tg_generateTrackEx(sector_found,sectorconfig,interleave,0,floppydisk->floppyBitRate,rpm,trackformat,0,2500|NO_SECTOR_UNDER_INDEX,-2500);

				for(k=0;k<floppydisk->floppySectorPerTrack;k++)
				{
					free(sectorconfig[k].input_data);
				}

			}
		}

		free(sectorconfig);
		free(SectorsOffsets);
		floppycontext->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");

		hxc_fclose(f);

		hxcfe_sanityCheck(floppycontext,floppydisk);

		return HXCFE_NOERROR;
	}

	floppycontext->hxc_printf(MSG_ERROR,"file size=%d !?",filesize);
	hxc_fclose(f);
	return HXCFE_BADFILE;
}
int OLDEXTADF_libLoad_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	unsigned int filesize;
	unsigned int i,j;
	unsigned char* trackdata;
	int	tracklen;
	CYLINDER* currentcylinder;
	unsigned int numberoftrack;

	unsigned char header[12];
	unsigned char * tracktable;
	unsigned int trackindex,tracksize;

	unsigned char gap3len,skew,trackformat,interleave;
	unsigned short sectorsize;

	floppycontext->hxc_printf(MSG_DEBUG,"OLDEXTADF_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);

	if(!filesize)
	{
		floppycontext->hxc_printf(MSG_ERROR,"Bad file size : %d !",filesize);
		hxc_fclose(f);
		return HXCFE_BADFILE;
	}

	memset(header,0,sizeof(header));
	fread(header,8,1,f);

	numberoftrack=0;
	if(!strncmp((char*)header,"UAE--ADF",8))
	{
		numberoftrack=160;
		tracktable=malloc(4*numberoftrack);
		memset(tracktable,0,4*numberoftrack);

		fread(tracktable,4*numberoftrack,1,f);
	}
	trackindex=0;

	floppydisk->floppyNumberOfTrack=numberoftrack>>1;

	sectorsize=512;
	interleave=1;
	gap3len=0;
	skew=0;
	trackformat=AMIGAFORMAT_DD;

	floppydisk->floppySectorPerTrack=-1;
	floppydisk->floppyNumberOfSide=2;
	floppydisk->floppyBitRate=DEFAULT_AMIGA_BITRATE;
	floppydisk->floppyiftype=AMIGA_DD_FLOPPYMODE;
	floppydisk->tracks=(CYLINDER**)malloc(sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);

	tracklen=(DEFAULT_AMIGA_BITRATE/(DEFAULT_AMIGA_RPM/60))/4;

	floppycontext->hxc_printf(MSG_DEBUG,"OLD Extended ADF : %x tracks",numberoftrack);

	for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
	{

		floppydisk->tracks[j]=allocCylinderEntry(DEFAULT_AMIGA_RPM,floppydisk->floppyNumberOfSide);
		currentcylinder=floppydisk->tracks[j];

		for(i=0;i<floppydisk->floppyNumberOfSide;i++)
		{

			if(trackindex<numberoftrack)
			{

				tracksize=tracktable[(4*trackindex)+2] * 0x100     + \
						  tracktable[(4*trackindex)+3];

				if(tracksize)
				{

					if(tracktable[(4*trackindex)+0] || tracktable[(4*trackindex)+1] )
					{


						floppycontext->hxc_printf(MSG_DEBUG,"[%.3d:%.1X] Reading Non-DOS track at 0x%.8x, Size : 0x%.8x",j,i,ftell(f),tracksize);

						currentcylinder->sides[i]=tg_alloctrack(DEFAULT_AMIGA_BITRATE,AMIGA_MFM_ENCODING,DEFAULT_AMIGA_RPM,(tracksize+2)*8,2500,-100,0x00);

						currentcylinder->sides[i]->databuffer[0]=tracktable[(4*trackindex)+0];
						currentcylinder->sides[i]->databuffer[1]=tracktable[(4*trackindex)+1];

						fread(&currentcylinder->sides[i]->databuffer[2],tracksize,1,f);

						currentcylinder->sides[i]->number_of_sector=floppydisk->floppySectorPerTrack;


					}
					else
					{


						floppycontext->hxc_printf(MSG_DEBUG,"[%.3d:%.1X] Reading DOS track at 0x%.8x, Size : 0x%.8x",j,i,ftell(f),tracksize);

						trackdata=(unsigned char*)malloc(tracksize);

						fread(trackdata,tracksize,1,f);

						currentcylinder->sides[i]=tg_generateTrack(trackdata,sectorsize,(unsigned short)(tracksize/sectorsize),(unsigned char)j,(unsigned char)i,0,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,currentcylinder->floppyRPM,trackformat,gap3len,0,2500,-11150);

						free(trackdata);
					}
				}
				else
				{
					floppycontext->hxc_printf(MSG_DEBUG,"[%.3d:%.1X] Null Size track!",j,i);
					currentcylinder->sides[i]=tg_alloctrack(DEFAULT_AMIGA_BITRATE,AMIGA_MFM_ENCODING,DEFAULT_AMIGA_RPM,(tracklen)*8,2500,-11360,0x00);
				}
			}
			else
			{
				floppycontext->hxc_printf(MSG_DEBUG,"[%.3d:%.1X] No track!",j,i);
				currentcylinder->sides[i]=tg_alloctrack(DEFAULT_AMIGA_BITRATE,AMIGA_MFM_ENCODING,DEFAULT_AMIGA_RPM,(tracklen)*8,2500,-11360,0x00);
			}


			trackindex++;

		}
	}

	if(tracktable)	free(tracktable);
	floppycontext->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");
	hxc_fclose(f);

	hxcfe_sanityCheck(floppycontext,floppydisk);

	return HXCFE_NOERROR;
}
int EDE_libIsValidDiskFile(HXCFE_IMGLDR * imgldr_ctx,char * imgfile)
{
	unsigned char header_buffer[512];
	FILE * f;

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EDE_libIsValidDiskFile");

	if(	hxc_checkfileext(imgfile,"ede") ||
		hxc_checkfileext(imgfile,"eda") ||
		hxc_checkfileext(imgfile,"eds") ||
		hxc_checkfileext(imgfile,"edt") ||
		hxc_checkfileext(imgfile,"edv")
		)
	{

		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EDE_libIsValidDiskFile : EDE file !");

		f=hxc_fopen(imgfile,"rb");
		if(f==NULL)
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"EDE_libIsValidDiskFile : Cannot open %s !",imgfile);
			return HXCFE_ACCESSERROR;
		}

		fread(header_buffer,0x200,1,f);

		hxc_fclose(f);
		if((header_buffer[0]==0x0D) && (header_buffer[1]==0x0A))
		{
			switch(header_buffer[0x1FF])
			{
				case 0x01:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : Mirage (DD) format");
				break;
				case 0x02:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : SQ-80 (DD) format");
				break;
				case 0x03:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : EPS (DD) format");
				break;
				case 0x04:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : VFX-SD (DD) format");
				break;
				case 0xcb:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : ASR-10 HD format");
				break;
				case 0xcc:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : TS-10/12 HD format");
				break;
				case 0x07:
					imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EDE_libIsValidDiskFile : TS-10/12 DD format");
				break;
				default:
					imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"EDE_libIsValidDiskFile : Unknow format : %x !",header_buffer[0x1FF]);
					return HXCFE_BADFILE;
				break;
			}
		}

		else
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"EDE_libIsValidDiskFile : Bad header !!");
			return HXCFE_BADFILE;
		}

		return HXCFE_VALIDFILE;
	}
	else
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EDE_libIsValidDiskFile : non EDE file !");
		return HXCFE_BADFILE;
	}
}
Exemple #17
0
int D64_libLoad_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	unsigned int filesize;
	unsigned int i,j,k;
	unsigned char* trackdata;
	int tracklen;
	unsigned short rpm;
	unsigned short sectorsize;
	CYLINDER* currentcylinder;
	SIDE* currentside;
	unsigned char * errormap;
	d64trackpos * tracklistpos;
	unsigned int number_of_track;
	int errormap_size;


	floppycontext->hxc_printf(MSG_DEBUG,"D64_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);


	errormap_size=0;
	errormap=0;
	switch(filesize)
	{
		case 174848:
			//35 track, no errors 
			number_of_track=35;
			break;

		case 175531:
			//35 track, 683 error bytes
			number_of_track=35;

			errormap_size=683;
			errormap=(unsigned char*)malloc(errormap_size);
			memset(errormap,0,errormap_size);
			fseek(f,errormap_size,SEEK_END);
			fread(errormap,errormap_size,1,f);
			
			break;

		case 196608:
			//40 track, no errors
			number_of_track=40;
			break;
		
		case 197376:
			//40 track, 768 error bytes
			number_of_track=40;

			errormap_size=768;
			errormap=(unsigned char*)malloc(errormap_size);
			memset(errormap,0,errormap_size);
			fseek(f,errormap_size,SEEK_END);
			fread(errormap,errormap_size,1,f);
			
			break;

		default:
			// not supported !
			floppycontext->hxc_printf(MSG_ERROR,"Unsupported D64 file size ! (%d Bytes)",filesize);
			hxc_fclose(f);
			return HXCFE_UNSUPPORTEDFILE;
			
			break;
	}


	tracklistpos=(d64trackpos*)malloc(number_of_track*sizeof(d64trackpos));
	memset(tracklistpos,0,number_of_track*sizeof(d64trackpos));

	i=0;
	k=0;
	do
	{
		tracklistpos[i].number_of_sector=21;
		tracklistpos[i].fileoffset=k;
		tracklistpos[i].bitrate=307693;//XTAL_16_MHz / 13

		k=k+(21*256);
		i++;
	}while(i<number_of_track && i<17);

	i=17;
	do
	{
		tracklistpos[i].number_of_sector=19;
		tracklistpos[i].fileoffset=k;
		tracklistpos[i].bitrate=285715; //XTAL_16_MHz / 14

		k=k+(19*256);
		i++;
	}while(i<number_of_track && i<24);

	i=24;
	do
	{
		tracklistpos[i].number_of_sector=18;
		tracklistpos[i].fileoffset=k;
		tracklistpos[i].bitrate=266667;//XTAL_16_MHz / 15

		k=k+(18*256);
		i++;
	}while(i<number_of_track && i<30);
		
	i=30;
	do
	{
		tracklistpos[i].number_of_sector=17;
		tracklistpos[i].fileoffset=k;
		tracklistpos[i].bitrate=250000;//XTAL_16_MHz / 16

		k=k+(17*256);
		i++;
	}while(i<number_of_track);


	floppydisk->floppyNumberOfTrack=number_of_track;
	floppydisk->floppyNumberOfSide=1;
	floppydisk->floppySectorPerTrack=-1;
	
	sectorsize=256; // c64 file support only 256bytes/sector floppies.
	
	floppydisk->floppyBitRate=VARIABLEBITRATE;
	floppydisk->floppyiftype=C64_DD_FLOPPYMODE;
	floppydisk->tracks=(CYLINDER**)malloc(sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);
	
	rpm=300;

	floppycontext->hxc_printf(MSG_INFO_1,"filesize:%dkB, %d tracks, %d side(s), rpm:%d",filesize/1024,floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,rpm);
					
	for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
	{
		
		trackdata=(unsigned char*)malloc(sectorsize*tracklistpos[j].number_of_sector);

		floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
		currentcylinder=floppydisk->tracks[j];				
		
		for(i=0;i<floppydisk->floppyNumberOfSide;i++)
		{
					
			tracklen=(tracklistpos[j].bitrate/(rpm/60))/4;
				
			currentcylinder->sides[i]=malloc(sizeof(SIDE));
			memset(currentcylinder->sides[i],0,sizeof(SIDE));
			currentside=currentcylinder->sides[i];
					
			currentside->number_of_sector=tracklistpos[j].number_of_sector;
			currentside->tracklen=tracklen;
					
			currentside->databuffer=malloc(currentside->tracklen);
			memset(currentside->databuffer,0,currentside->tracklen);
					
			currentside->flakybitsbuffer=0;
			
			currentside->indexbuffer=malloc(currentside->tracklen);
			memset(currentside->indexbuffer,0,currentside->tracklen);
					
			currentside->timingbuffer=0;
			currentside->bitrate=tracklistpos[j].bitrate;
			currentside->track_encoding=UNKNOWN_ENCODING;
			
			fseek (f , tracklistpos[j].fileoffset , SEEK_SET);
					
			fread(trackdata,sectorsize*currentside->number_of_sector,1,f);
		
			floppycontext->hxc_printf(MSG_DEBUG,"Track:%d Size:%d File offset:%d Number of sector:%d Bitrate:%d",j,currentside->tracklen,tracklistpos[j].fileoffset,tracklistpos[j].number_of_sector,currentside->bitrate);

			BuildGCRTrack(currentside->number_of_sector,sectorsize,j,i,trackdata,currentside->databuffer,&currentside->tracklen);
	
			currentside->tracklen=currentside->tracklen*8;

		}

		free(trackdata);
	}
	
	if(errormap) free(errormap);
			
	floppycontext->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");
		
	hxc_fclose(f);
	return HXCFE_NOERROR;
}
Exemple #18
0
int XML_libWrite_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppy,char * filename)
{
	int i,j,k,s;
	FILE * xmlfile;
	int fileoffset,nb_sectorfound;
	int nbsect,firstsectid,sectorsize,imagesize;
	char trackformat[32];
	SECTORSEARCH* ss;
	SECTORCONFIG** sca;
	sect_offset ** sorted_sectoffset,**sectoffset;

	floppycontext->hxc_printf(MSG_INFO_1,"Write XML file %s...",filename);

	fileoffset = 0;

	xmlfile=hxc_fopen(filename,"w+");
	if(xmlfile)
	{
		imagesize = hxcfe_getFloppySize(floppycontext,floppy,0);

		ss=hxcfe_initSectorSearch(floppycontext,floppy);
		if(ss)
		{
			fprintf(xmlfile,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
			fprintf(xmlfile,"<!-- HxC Floppy Emulator Disk Layout -->\n");

			fprintf(xmlfile,"<disk_layout>\n");

			fprintf(xmlfile,"\t<disk_layout_name>AUTOGENERATEDLAYOUT</disk_layout_name>\n");
			fprintf(xmlfile,"\t<disk_layout_description>Auto Generated Disk Layout</disk_layout_description>\n");
			fprintf(xmlfile,"\t<prefered_file_extension>img</prefered_file_extension>\n");
			fprintf(xmlfile,"\t<interface_mode>GENERIC_SHUGART_DD_FLOPPYMODE</interface_mode>\n");

			fprintf(xmlfile,"\t<file_size>%d</file_size>\n",imagesize);

			fprintf(xmlfile,"\t<layout>\n");

			fprintf(xmlfile,"\t\t<number_of_track>%d</number_of_track>\n",floppy->floppyNumberOfTrack);
			fprintf(xmlfile,"\t\t<number_of_side>%d</number_of_side>\n",floppy->floppyNumberOfSide);

			gettracktype(ss,0,0,&nbsect,&firstsectid,(char*)&trackformat,&sectorsize);

			fprintf(xmlfile,"\t\t<format>%s</format>\n",trackformat);

			fprintf(xmlfile,"\t\t<start_sector_id>%d</start_sector_id>\n",firstsectid);
			fprintf(xmlfile,"\t\t<sector_per_track>%d</sector_per_track>\n",nbsect);

			fprintf(xmlfile,"\t\t<sector_size>%d</sector_size>\n",sectorsize);

			fprintf(xmlfile,"\t\t<formatvalue>%d</formatvalue>\n",0x00);

			fprintf(xmlfile,"\t\t<gap3>%d</gap3>\n",0xFF);

			fprintf(xmlfile,"\t\t<bitrate>%d</bitrate>\n",floppy->floppyBitRate);
			fprintf(xmlfile,"\t\t<pregap>%d</pregap>\n",0);
			fprintf(xmlfile,"\t\t<rpm>%d</rpm>\n",floppy->tracks[0]->floppyRPM);

			fprintf(xmlfile,"\t\t<track_list>\n");


			for(j=0;j<floppy->floppyNumberOfTrack;j++)
			{
				for(i=0;i<floppy->floppyNumberOfSide;i++)
				{
					fprintf(xmlfile,"\t\t\t<track track_number=\"%.2d\" side_number=\"%d\">\n",j,i);
					fprintf(xmlfile,"\t\t\t\t<data_offset>0x%.6X</data_offset>\n",fileoffset);

					gettracktype(ss,j,i,&nbsect,&firstsectid,(char*)&trackformat,&sectorsize);
					fprintf(xmlfile,"\t\t\t\t<format>%s</format>\n",trackformat);

					sca = hxcfe_getAllTrackISOSectors(ss,j,i,&nb_sectorfound);
					fprintf(xmlfile,"\t\t\t\t<sector_list>\n");

					if(sca && nb_sectorfound)
					{
						sectoffset = malloc(sizeof(sect_offset*) * nb_sectorfound);
						sorted_sectoffset = malloc(sizeof(sect_offset*) * nb_sectorfound);
						if(sorted_sectoffset && sectoffset)
						{
							memset(sectoffset,0,sizeof(sect_offset*) * nb_sectorfound);
							memset(sorted_sectoffset,0,sizeof(sect_offset*) * nb_sectorfound);
							for(s=0;s<nb_sectorfound;s++)
							{
								sorted_sectoffset[s] = malloc(sizeof(sect_offset));
								sorted_sectoffset[s]->ss = sca[s];
								sorted_sectoffset[s]->offset = 0;
							}

							memcpy(sectoffset,sorted_sectoffset,sizeof(sect_offset*) * nb_sectorfound);

							quickSort(sorted_sectoffset, 0, nb_sectorfound - 1);

							for(s=0;s<nb_sectorfound;s++)
							{
								sorted_sectoffset[s]->offset = fileoffset;
								fileoffset += sca[s]->sectorsize;
							}

							free(sorted_sectoffset);

							for(s=0;s<nb_sectorfound;s++)
							{

								fprintf(xmlfile,"\t\t\t\t\t<sector sector_id=\"%d\" sector_size=\"%d\">\n",sca[s]->sector,sca[s]->sectorsize);
								if(sca[s]->fill_byte_used)
								{
									fprintf(xmlfile,"\t\t\t\t\t\t<data_fill>0x%.2X</data_fill>\n",sca[s]->fill_byte);
								}
								else
								{
									if(sca[s]->input_data)
									{
										fprintf(xmlfile,"\t\t\t\t\t\t<sector_data>");
										k=0;
										do
										{
											fprintf(xmlfile,"%.2X",sca[s]->input_data[k]);
											k++;
										}while(k<(int)sca[s]->sectorsize);

										fprintf(xmlfile,"</sector_data>\n");
									}
								}

								if(sca[s]->use_alternate_datamark)
								{
									fprintf(xmlfile,"\t\t\t\t\t\t<datamark>0x%.2X</datamark>\n",sca[s]->alternate_datamark);
								}

								fprintf(xmlfile,"\t\t\t\t\t\t<data_offset>0x%.6X</data_offset>\n",(unsigned int)sectoffset[s]->offset);
								fprintf(xmlfile,"\t\t\t\t\t</sector>\n");

								hxcfe_freeSectorConfig  (ss,sca[s]);
							}

							for(s=0;s<nb_sectorfound;s++)
							{
								free(sectoffset[s]);
							}
							free(sectoffset);
						}

						free(sca);
					}

					fprintf(xmlfile,"\t\t\t\t</sector_list>\n");

					fprintf(xmlfile,"\t\t\t</track>\n");
				}
			}
			hxcfe_deinitSectorSearch(ss);
		}

		fprintf(xmlfile,"\t\t</track_list>\n");
		fprintf(xmlfile,"\t</layout>\n");
		fprintf(xmlfile,"</disk_layout>\n");

		hxc_fclose(xmlfile);
	}

	return 0;
}
int ADF_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	unsigned int filesize;
	int i,j;
	unsigned short rpm;
	unsigned int file_offset;
	unsigned char* trackdata;
	unsigned char gap3len,skew,trackformat,interleave;
	unsigned short sectorsize;
	HXCFE_CYLINDER* currentcylinder;

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"ADF_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);

	if(!filesize)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Bad file size : %d !",filesize);
		hxc_fclose(f);
		return HXCFE_BADFILE;
	}

	if(filesize<100*11*2*512)
	{
		floppydisk->floppySectorPerTrack=11;
		rpm=DEFAULT_AMIGA_RPM;
	}
	else
	{
		floppydisk->floppySectorPerTrack=22;
		rpm=DEFAULT_AMIGA_RPM/2;
	}

	floppydisk->floppyNumberOfSide=2;

	if((filesize/(512*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide))<80)
		floppydisk->floppyNumberOfTrack=80;
	else
		floppydisk->floppyNumberOfTrack=filesize/(512*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide);

	floppydisk->floppyBitRate=DEFAULT_AMIGA_BITRATE;
	floppydisk->floppyiftype=AMIGA_DD_FLOPPYMODE;
	floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*(floppydisk->floppyNumberOfTrack+4));

	sectorsize=512;
	interleave=1;
	gap3len=0;
	skew=0;
	trackformat=AMIGAFORMAT_DD;

	trackdata = (unsigned char*)malloc(sectorsize*floppydisk->floppySectorPerTrack);

	for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
	{

		floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
		currentcylinder=floppydisk->tracks[j];

		for(i=0;i<floppydisk->floppyNumberOfSide;i++)
		{
			hxcfe_imgCallProgressCallback(imgldr_ctx,(j<<1) | (i&1),floppydisk->floppyNumberOfTrack*2 );

			file_offset=(sectorsize*(j*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide))+
					(sectorsize*(floppydisk->floppySectorPerTrack)*i);
			fseek (f , file_offset , SEEK_SET);
			fread(trackdata,sectorsize*floppydisk->floppySectorPerTrack,1,f);

			currentcylinder->sides[i]=tg_generateTrack(trackdata,sectorsize,floppydisk->floppySectorPerTrack,(unsigned char)j,(unsigned char)i,0,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,currentcylinder->floppyRPM,trackformat,gap3len,0,2500,-11150);
		}
	}

	// Add 4 empty tracks
	for(j=floppydisk->floppyNumberOfTrack;j<(floppydisk->floppyNumberOfTrack + 4);j++)
	{
		floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
		currentcylinder=floppydisk->tracks[j];

		for(i=0;i<floppydisk->floppyNumberOfSide;i++)
		{
			currentcylinder->sides[i]=tg_generateTrack(trackdata,sectorsize,0,(unsigned char)j,(unsigned char)i,0,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,currentcylinder->floppyRPM,trackformat,gap3len,0,2500,-11150);
		}
	}

	floppydisk->floppyNumberOfTrack += 4;


	free(trackdata);

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");
	hxc_fclose(f);
	return HXCFE_NOERROR;
}
int TI99V9T9_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename)
{
	int i,j,k;
	FILE * ti99v9t9file;
	int32_t nbsector,imagesize;

	int32_t numberofsector,numberofside,numberoftrack;
	int32_t density = ISOIBM_FM_ENCODING;
	int file_offset;
	int32_t sectorsize = 256;
	unsigned char * diskimage;
	int error = 0;
	HXCFE_SECTORACCESS* ss;
	HXCFE_SECTCFG* sc;

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Write TI99 V9T9 file %s...",filename);

	imagesize=hxcfe_getFloppySize(imgldr_ctx->hxcfe,floppy,&nbsector);

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Disk size : %d Bytes %d Sectors",imagesize,nbsector);

	ss = hxcfe_initSectorAccess(imgldr_ctx->hxcfe, floppy);
	if (ss)
	{
		sc = hxcfe_searchSector(ss, 0, 0, 0, density);
		if (!sc)
		{
			density = ISOIBM_MFM_ENCODING;
			sc = hxcfe_searchSector(ss, 0, 0, 0, density);
			if (!sc)
			{
				imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "This disk is neither FM nor MFM.  Exiting.");
				return HXCFE_FILECORRUPTED;
			}
		}

		// sc->input_data should contain the disk geometry

		numberofside = sc->input_data[0x12];
		numberofsector = sc->input_data[0x0c];
		numberoftrack = sc->input_data[0x11];

		if ( (numberofside < 1) && (numberofside > 2))
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Image claims it has %i sides, which is clearly wrong.  Exiting.", numberofside);
			return HXCFE_FILECORRUPTED;
		}
 
		if ( (numberoftrack != 40) && (numberoftrack != 80))
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Image claims each side has %i tracks, which is clearly wrong.  Exiting.", numberoftrack);
			return HXCFE_FILECORRUPTED;
		}
 
		if ( (numberofsector != 9) && (numberofsector != 16) && (numberofsector != 18) && (numberofsector != 36))
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Image claims each track has %i sectors, which is clearly wrong.  Exiting.", numberofsector);
			return HXCFE_FILECORRUPTED;
		}

		if ( (numberofsector * numberoftrack * numberofside) != nbsector )
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR, "Disk geometry %i/%i/%i does not match disk length of %i sectors.  Exiting.", numberofside, numberoftrack, numberofsector, nbsector);
			return HXCFE_FILECORRUPTED;
		}


 
		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1, "Disk geometry is %i sides, %i tracks per side, %i sectors per track.", numberofside, numberoftrack, numberofsector);

		imagesize = numberofsector * numberoftrack * numberofside * sectorsize;
		diskimage = malloc(imagesize);
		if (!diskimage)
			return HXCFE_INTERNALERROR;
		memset(diskimage, 0xF6, imagesize);

		for(i=0;i<numberofside;i++)
		{
			for(j=0;j<numberoftrack;j++)
			{
				hxcfe_imgCallProgressCallback(imgldr_ctx, j + (i*numberoftrack),numberofside*numberoftrack);

				for(k=0;k<numberofsector;k++)
				{
					sc = hxcfe_searchSector(ss,j,i,k,density);
					if(sc)
					{
						if(sc->use_alternate_data_crc)
							imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Warning : Bad Data CRC : T:%d H:%d S:%d Size :%dB",j,i,k,sc->sectorsize);

						if(sc->sectorsize == sectorsize)
						{
							if(i==0)
								file_offset=(j*numberofsector)*sectorsize + ( k * sectorsize );
							else
								file_offset=(  numberoftrack      *numberofsector*sectorsize) +
												(((numberoftrack-1)-j)*numberofsector*sectorsize) +
												( k * sectorsize );
							memcpy(&diskimage[file_offset], sc->input_data, sectorsize);
						} else {
							error++;
							imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Bad Sector Size : T:%d H:%d S:%d Size :%dB, Should be %dB",j,i,k,sc->sectorsize,sectorsize);
						}

						hxcfe_freeSectorConfig(ss,sc);
					} else {
						imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Sector not found : T:%d H:%d S:%d",j,i,k);
					}
				}
			}
		}

		if(!error)
		{
			ti99v9t9file=hxc_fopen(filename,"wb");
			if(ti99v9t9file)
			{
				fwrite(diskimage,imagesize,1,ti99v9t9file);
				hxc_fclose(ti99v9t9file);
			} else {
				free(diskimage);
				hxcfe_deinitSectorAccess(ss);
				return HXCFE_ACCESSERROR;
			}
		}
	}

	free(diskimage);
	hxcfe_deinitSectorAccess(ss);

	if(!error)
		return HXCFE_NOERROR;
	else
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Errors were found in this disk image.");
		return HXCFE_FILECORRUPTED;
	}
}
Exemple #21
0
int IMD_libWrite_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppy,char * filename)
{
	int i,j,k,l,nbsector;
	FILE * imdfile;
	char * log_str;
	char   tmp_str[256];
	char rec_mode;

	unsigned char sector_numbering_map[256];
	unsigned char cylinder_numbering_map[256];
	unsigned char side_numbering_map[256];

	int track_cnt,bitrate;
	int sectorlistoffset,trackinfooffset;
	imd_trackheader imd_th;

	struct tm * ts;
	time_t currenttime;

	SECTORSEARCH* ss;
	SECTORCONFIG** sca;

//	struct DateTime reptime;


	floppycontext->hxc_printf(MSG_INFO_1,"Write IMD file %s...",filename);

	log_str=0;
	imdfile=hxc_fopen(filename,"wb");
	if(imdfile)
	{

		currenttime=time (NULL);
		ts=localtime(&currenttime);

		fprintf(imdfile,"IMD 1.17: %.2d/%.2d/%.4d %.2d:%.2d:%.2d\r\n",ts->tm_mday,ts->tm_mon,ts->tm_year+1900,ts->tm_hour,ts->tm_min,ts->tm_sec);
		fprintf(imdfile,"File generated by the HxC Floppy Emulator software v%s\r\n",STR_FILE_VERSION2);
		fprintf(imdfile,"%c",0x1A);

		memset(sector_numbering_map,0,0x100);
		memset(cylinder_numbering_map,0,0x100);
		memset(side_numbering_map,0,0x100);

		track_cnt=0;

		ss=hxcfe_initSectorSearch(floppycontext,floppy);
		if(ss)
		{

			for(j=0;j<(int)floppy->floppyNumberOfTrack;j++)
			{
				for(i=0;i<(int)floppy->floppyNumberOfSide;i++)
				{
					sprintf(tmp_str,"track:%.2d:%d file offset:0x%.6x, sectors: ",j,i,(unsigned int)ftell(imdfile));

					log_str=0;
					log_str=realloc(log_str,strlen(tmp_str)+1);
					memset(log_str,0,strlen(tmp_str)+1);
					strcat(log_str,tmp_str);

					rec_mode=2;

					sca = hxcfe_getAllTrackSectors(ss,j,i,ISOIBM_MFM_ENCODING,&nbsector);
					if(!sca)
					{
						sca = hxcfe_getAllTrackSectors(ss,j,i,ISOIBM_FM_ENCODING,&nbsector);
						rec_mode=1;
						if(!nbsector)
						{
							rec_mode=3;
							sca = hxcfe_getAllTrackSectors(ss,j,i,MEMBRAIN_MFM_ENCODING,&nbsector);
						}
					}

					memset(&imd_th,0,sizeof(imd_trackheader));

					imd_th.physical_head=i;

					l=0;
					while((l<nbsector) && sca[l]->head == i )
					{
						l++;
					}
					if(l!=nbsector)
					{
						imd_th.physical_head=imd_th.physical_head | 0x40;
					}


					l=0;
					while((l<nbsector) && sca[l]->cylinder == j)
					{
						l++;
					}
					if(l!=nbsector)
					{
						imd_th.physical_head=imd_th.physical_head | 0x80;
					}

					imd_th.physical_cylinder=j;
					imd_th.number_of_sector=nbsector;

					imd_th.track_mode_code=rec_mode;

					bitrate = floppy->tracks[j]->sides[i]->bitrate;
					if(floppy->tracks[j]->sides[i]->timingbuffer)
					{
						bitrate = floppy->tracks[j]->sides[i]->timingbuffer[2];
					}

					if(bitrate < 550000 && bitrate > 450000)
						bitrate = 500000;
					if(bitrate < 350000 && bitrate > 280000)
						bitrate = 300000;
					if(bitrate < 280000 && bitrate > 230000)
						bitrate = 250000;

					switch(bitrate)
					{
						case 250000:
							imd_th.track_mode_code=2;
							break;
						case 300000:
							imd_th.track_mode_code=1;
							break;
						case 500000:
							imd_th.track_mode_code=0;
							break;
						default:
							imd_th.track_mode_code=2;
							break;
					}

					if(rec_mode==2)
					{
						imd_th.track_mode_code=imd_th.track_mode_code+3;
					}

					if(rec_mode==3)
					{
						imd_th.track_mode_code=imd_th.track_mode_code+0x80;
					}

					if(nbsector)
					{
						imd_th.sector_size_code=size_to_code(sca[0]->sectorsize);
					}

					trackinfooffset=ftell(imdfile);
					fwrite(&imd_th,sizeof(imd_trackheader),1,imdfile);
					sectorlistoffset=ftell(imdfile);

					for(k=0;k<nbsector;k++)
					{
						sector_numbering_map[k] = sca[k]->sector;
						cylinder_numbering_map[k] = sca[k]->cylinder;
						side_numbering_map[k]= sca[k]->head;
					}

					fwrite(sector_numbering_map,imd_th.number_of_sector,1,imdfile);
					if(imd_th.physical_head & 0x80)fwrite(cylinder_numbering_map,imd_th.number_of_sector,1,imdfile);
					if(imd_th.physical_head & 0x40)fwrite(side_numbering_map,imd_th.number_of_sector,1,imdfile);

					if(nbsector)
					{

						k=0;
						do
						{
							if(sca[k]->input_data)
							{
								l=0;
								while((l<(int)sca[k]->sectorsize) && sca[k]->input_data[l]==sca[k]->input_data[0])
								{
									l++;
								}

								if(l!=(int)sca[k]->sectorsize)
								{
									fputs("\1",imdfile);
									fwrite(sca[k]->input_data,sca[k]->sectorsize,1,imdfile);
								}
								else
								{
									fputs("\2",imdfile);
									fwrite(sca[k]->input_data,1,1,imdfile);
								}
							}
							else
							{
								fputs("\2",imdfile);
								fwrite(&sca[k]->fill_byte,1,1,imdfile);
							}

							sprintf(tmp_str,"%d ",sca[k]->sector);
							log_str=realloc(log_str,strlen(log_str)+strlen(tmp_str)+1);
							strcat(log_str,tmp_str);
							k++;

						}while(k<nbsector);


						k=0;
						do
						{
							if(sca[k])
							{
								if(sca[k]->input_data)
									free(sca[k]->input_data);
								free(sca[k]);
							}
							k++;
						}while(k<nbsector);


						log_str=realloc(log_str,strlen(log_str)+strlen(tmp_str)+1);
						strcat(log_str,tmp_str);

					}

					track_cnt++;

					floppycontext->hxc_printf(MSG_INFO_1,log_str);
					free(log_str);

				}

			}

			hxcfe_deinitSectorSearch(ss);
		}

		hxc_fclose(imdfile);
	}

	return 0;
}
int HFE_HDDD_A2_libWrite_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppy,char * filename)
{

	pictrack * track;

	FILE * hxcpicfile;

	picfileformatheader * FILEHEADER;
	unsigned char * mfmtracks0,*mfmtracks1,*mfmtrackfinal;
	unsigned char * offsettrack;
	int mfmsize,mfmsize2,i_conv;
	unsigned int i,j,k;
	unsigned int trackpos;
	unsigned int tracklistlen;
	unsigned int tracksize;
	
	unsigned short fm_pulses;

	floppycontext->hxc_printf(MSG_INFO_1,"Write HFE file %s for the standalone emulator (with HDDD A2 support).",filename);

	if(!floppy->floppyNumberOfTrack)
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot create zero track HFE file");
		return -1;
	}

	ramfile=0;
	ramfile_size=0;

	hxcpicfile=rfopen(filename,"wb");

	if(hxcpicfile)
	{
		FILEHEADER=(picfileformatheader *) malloc(512);
		memset(FILEHEADER,0xFF,512);
		memcpy(&FILEHEADER->HEADERSIGNATURE,"HXCPICFE",8);

		FILEHEADER->number_of_track=(unsigned char)floppy->floppyNumberOfTrack;
		FILEHEADER->number_of_side=floppy->floppyNumberOfSide;
		if(floppy->floppyBitRate!=VARIABLEBITRATE)
		{
			FILEHEADER->bitRate=(floppy->floppyBitRate * 2)/1000;
		}
		else
		{
			if(floppy->tracks[0]->sides[0]->bitrate == VARIABLEBITRATE)
				FILEHEADER->bitRate=(unsigned short)((unsigned long)(floppy->tracks[0]->sides[0]->timingbuffer[ (floppy->tracks[0]->sides[0]->tracklen/8) / 2] * 2)/1000);
			else
				FILEHEADER->bitRate=(floppy->tracks[0]->sides[0]->bitrate * 2)/1000;
		}
		FILEHEADER->floppyRPM=0;//floppy->floppyRPM;

		FILEHEADER->floppyinterfacemode=(unsigned char)floppy->floppyiftype;

		floppycontext->hxc_printf(MSG_INFO_1,"Floppy interface mode %s (%s)",	hxcfe_getFloppyInterfaceModeName(floppycontext,FILEHEADER->floppyinterfacemode),
																			hxcfe_getFloppyInterfaceModeDesc(floppycontext,FILEHEADER->floppyinterfacemode) );

		FILEHEADER->track_encoding=0;
		FILEHEADER->formatrevision=0;
		FILEHEADER->track_list_offset=1;
		FILEHEADER->write_protected=1;

		if(floppy->tracks[floppy->floppyNumberOfTrack/2]->sides[0]->track_encoding)
		{
			FILEHEADER->track_encoding=floppy->tracks[floppy->floppyNumberOfTrack/2]->sides[0]->track_encoding;
		}

		if( FILEHEADER->track_encoding == APPLEII_GCR1_ENCODING )
		{
			FILEHEADER->track_encoding = APPLEII_HDDD_A2_GCR1_ENCODING;
		}

		if( FILEHEADER->track_encoding == APPLEII_GCR2_ENCODING )
		{
			FILEHEADER->track_encoding = APPLEII_HDDD_A2_GCR2_ENCODING;
		}

		if(floppy->double_step)
			FILEHEADER->single_step=0x00;
		else
			FILEHEADER->single_step=0xFF;

		rfwrite(FILEHEADER,512,1,hxcpicfile);

		tracklistlen=((((((FILEHEADER->number_of_track)+1)*sizeof(pictrack))/512)+1));
		offsettrack=(unsigned char*) malloc(tracklistlen*512);
		memset(offsettrack,0xFF,tracklistlen*512);

		i=0;
		trackpos=FILEHEADER->track_list_offset+tracklistlen;

		while(i<(FILEHEADER->number_of_track))
		{
				mfmsize=0;
				mfmsize2=0;

				mfmsize=floppy->tracks[i]->sides[0]->tracklen * 2;
				if(mfmsize&7)
					mfmsize=(mfmsize/8)+1;
				else
					mfmsize=mfmsize/8;


				if(floppy->tracks[i]->number_of_side==2)
				{
					mfmsize2=floppy->tracks[i]->sides[1]->tracklen * 2;
					if(mfmsize2&7)
						mfmsize2=(mfmsize2/8)+1;
					else
						mfmsize2=mfmsize2/8;
				}

				if(mfmsize2>mfmsize) mfmsize=mfmsize2;

				if(mfmsize*2>0xFFFF)
				{
					floppycontext->hxc_printf(MSG_ERROR,"Argg!! track %d too long (%x) and shorten to 0xFFFF !",i,mfmsize*2);
					mfmsize=0x7FFF;
				}

				track=(pictrack *)(offsettrack+(i*sizeof(pictrack)));
				track->track_len=mfmsize*2;
				track->offset=trackpos;

				if((mfmsize*2)%512)
					trackpos=trackpos+(((mfmsize*2)/512)+1);
				else
					trackpos=trackpos+((mfmsize*2)/512);

				//trackpos=trackpos+(((mfmsize*2)/512)+1);
			i++;
		};

		rfwrite(offsettrack,512*tracklistlen,1,hxcpicfile);

		i=0;
		while(i<(FILEHEADER->number_of_track))
		{

				mfmsize=floppy->tracks[i]->sides[0]->tracklen * 2;
				if(mfmsize&7)
					mfmsize=(mfmsize/8)+1;
				else
					mfmsize=mfmsize/8;

				mfmsize2=0;
				if(floppy->tracks[i]->number_of_side==2)
				{
					mfmsize2=floppy->tracks[i]->sides[1]->tracklen * 2;
					if(mfmsize2&7)
						mfmsize2=(mfmsize2/8)+1;
					else
						mfmsize2=mfmsize2/8;
				}

				if(mfmsize>0x7FFF)
				{
					mfmsize=0x7FFF;
				}
				if(mfmsize2>0x7FFF)
				{
					mfmsize2=0x7FFF;
				}
				track=(pictrack *)(offsettrack+(i*sizeof(pictrack)));

				if(track->track_len%512)
					tracksize=((track->track_len&(~0x1FF))+0x200)/2;//(((track->track_len/512)+1)*512)/2;
				else
					tracksize=track->track_len/2;

				mfmtracks0=(unsigned char*) malloc(tracksize);
				mfmtracks1=(unsigned char*) malloc(tracksize);
				mfmtrackfinal=(unsigned char*) malloc(tracksize*2);

				memset(mfmtracks0,0x00,tracksize);
				memset(mfmtracks1,0x00,tracksize);
				memset(mfmtrackfinal,0x55,tracksize*2);

				for(i_conv=0;i_conv<(mfmsize/2);i_conv++)
				{
					// Add the FM Clocks
					fm_pulses = ext_a2_bit_expander[floppy->tracks[i]->sides[0]->databuffer[i_conv]] | 0x2222;
					mfmtracks0[(i_conv*2)+0] = fm_pulses >> 8;
					mfmtracks0[(i_conv*2)+1] = fm_pulses &  0xFF;
				}

				addpad(mfmtracks0,mfmsize,tracksize);

				if(floppy->tracks[i]->number_of_side==2)
				{
					for(i_conv=0;i_conv<(mfmsize2/2);i_conv++)
					{
						// Add the FM Clocks
						fm_pulses = ext_a2_bit_expander[floppy->tracks[i]->sides[1]->databuffer[i_conv]] | 0x2222;
						mfmtracks1[(i_conv*2)+0] = fm_pulses >> 8;
						mfmtracks1[(i_conv*2)+1] = fm_pulses &  0xFF;
					}
					addpad(mfmtracks1,mfmsize2,tracksize);
				}
				else
				{
					memset(mfmtracks1,0xAA,tracksize);
				}

				for(k=0;k<tracksize/256;k++)
				{

					for(j=0;j<256;j++)
					{
						// inversion des bits pour le EUSART du PIC.

						// head 0
						mfmtrackfinal[(k*512)+j]=     bit_inverter[mfmtracks0[(k*256)+j]];
						// head 1
						mfmtrackfinal[(k*512)+j+256]= bit_inverter[mfmtracks1[(k*256)+j]];

					}
				}

				rfwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile);

				free(mfmtracks0);
				free(mfmtracks1);
				free(mfmtrackfinal);
				
			i++;
		};

		free(offsettrack);


#ifdef FASTWRITE
		hxcpicfile=hxc_fopen(filename,"wb");
		if(hxcpicfile)
		{
			fwrite(ramfile,ramfile_size,1,hxcpicfile);
			hxc_fclose(hxcpicfile);
		}
		else
		{
			rfclose(hxcpicfile);
			floppycontext->hxc_printf(MSG_ERROR,"Cannot create %s!",filename);
			return -1;
		}

#endif

		rfclose(hxcpicfile);

		floppycontext->hxc_printf(MSG_INFO_1,"%d tracks written to the file",FILEHEADER->number_of_track);

		free(FILEHEADER);

		return 0;
	}
int JVC_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	unsigned int filesize;
	int i,j,k,skew;
	int file_offset;
	unsigned char* trackdata;
	int headerSize;
	int gap3len,interleave;
	int sectorsize,rpm;
	jvc_header jvc_h;
	unsigned char Sector_attribute_flag;
	HXCFE_CYLINDER* currentcylinder;
	unsigned char trackformat;
	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"JVC_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);


	if(filesize!=0)
	{
		headerSize = filesize % 256;

		// default values
		jvc_h.Setors_per_track=18;
		jvc_h.Side_count=1;
		jvc_h.Sector_size_code=1;
		jvc_h.First_sector_ID=1;
		jvc_h.Sector_attribute_flag=0;

		fread(&jvc_h, headerSize,1,f);

		sectorsize=128<<jvc_h.Sector_size_code;

		floppydisk->floppySectorPerTrack=jvc_h.Setors_per_track;
		floppydisk->floppyNumberOfSide= jvc_h.Side_count;
		if( jvc_h.Sector_attribute_flag)
		{
			floppydisk->floppyNumberOfTrack=(filesize - headerSize) / (jvc_h.Setors_per_track * ((128 << jvc_h.Sector_size_code) + 1) ) / jvc_h.Side_count;
		}
		else
		{
			floppydisk->floppyNumberOfTrack=(filesize - headerSize) / (jvc_h.Setors_per_track * (128 << jvc_h.Sector_size_code)) / jvc_h.Side_count;
		}


		floppydisk->floppyBitRate=DEFAULT_DD_BITRATE;
		floppydisk->floppyiftype=GENERIC_SHUGART_DD_FLOPPYMODE;

		trackformat=ISOFORMAT_DD;
		skew=0;
		interleave=1;
		gap3len=255;

		floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);

		rpm=300; // normal rpm

		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"filesize:%dkB, %d tracks, %d side(s), %d sectors/track, sector size: %dB , gap3:%d, interleave:%d,rpm:%d bitrate:%d",filesize/1024,floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,floppydisk->floppySectorPerTrack,sectorsize,gap3len,interleave,rpm,floppydisk->floppyBitRate);

		trackdata=(unsigned char*)malloc(sectorsize*floppydisk->floppySectorPerTrack);

		for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
		{

			floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
			currentcylinder=floppydisk->tracks[j];

			for(i=0;i<floppydisk->floppyNumberOfSide;i++)
			{
				hxcfe_imgCallProgressCallback(imgldr_ctx, (j<<1) + (i&1),floppydisk->floppyNumberOfTrack*2);

				if(jvc_h.Sector_attribute_flag)
					file_offset=((sectorsize+1)*(j*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide))+
						((sectorsize+1)*(floppydisk->floppySectorPerTrack)*i);
				else
					file_offset=(sectorsize*(j*floppydisk->floppySectorPerTrack*floppydisk->floppyNumberOfSide))+
						(sectorsize*(floppydisk->floppySectorPerTrack)*i);

				fseek (f , file_offset , SEEK_SET);

				Sector_attribute_flag=0;
				for(k=0;k<jvc_h.Setors_per_track;k++)
				{
					if(jvc_h.Sector_attribute_flag)
					{

						fread(&Sector_attribute_flag,1,1,f);
					}

					fread(&trackdata[k*sectorsize],sectorsize,1,f);
				}

				currentcylinder->sides[i]=tg_generateTrack(trackdata,sectorsize,floppydisk->floppySectorPerTrack,(unsigned char)j,(unsigned char)i,1,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,currentcylinder->floppyRPM,trackformat,gap3len,0,2500 | NO_SECTOR_UNDER_INDEX,-2500);
			}
		}

		free(trackdata);

		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");

		hxc_fclose(f);
		return HXCFE_NOERROR;
	}

	imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"file size=%d !?",filesize);
	hxc_fclose(f);
	return HXCFE_BADFILE;
}
int EMUI_RAW_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	int i;
	HXCFE_CYLINDER* currentcylinder;
	HXCFE_SIDE* currentside;
	unsigned char sector_data[0xE00];
	int tracknumber,sidenumber;


	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EMUI_RAW_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	floppydisk->floppyNumberOfTrack=35;
	floppydisk->floppyNumberOfSide=1;
	floppydisk->floppyBitRate=DEFAULT_EMUII_BITRATE;
	floppydisk->floppySectorPerTrack=1;
	floppydisk->floppyiftype=EMU_SHUGART_FLOPPYMODE;

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EmuI File : %d track, %d side, %d bit/s, %d sectors, mode %d",
		floppydisk->floppyNumberOfTrack,
		floppydisk->floppyNumberOfSide,
		floppydisk->floppyBitRate,
		floppydisk->floppySectorPerTrack,
		floppydisk->floppyiftype);


	floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);
	memset(floppydisk->tracks,0,sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);

	for(i=0;i<floppydisk->floppyNumberOfTrack;i++)
	{

		hxcfe_imgCallProgressCallback(imgldr_ctx,i,floppydisk->floppyNumberOfTrack);

		tracknumber=i;
		sidenumber=0;

		fseek(f,i*0xE00,SEEK_SET);
		fread(&sector_data,0xE00,1,f);

		if(!floppydisk->tracks[tracknumber])
		{
			floppydisk->tracks[tracknumber]=allocCylinderEntry(300,floppydisk->floppyNumberOfSide);
		}

		currentcylinder=floppydisk->tracks[tracknumber];

		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"read track %d side %d at offset 0x%x (0x%x bytes)",
			tracknumber,
			sidenumber,
			(0xE00*tracknumber*2)+(sidenumber*0xE00),
			0xE00);


			currentcylinder->sides[sidenumber]=tg_alloctrack(floppydisk->floppyBitRate,EMU_FM_ENCODING,currentcylinder->floppyRPM,((floppydisk->floppyBitRate/5)*2),2000,-2000,0x00);
			currentside=currentcylinder->sides[sidenumber];
			currentside->number_of_sector=floppydisk->floppySectorPerTrack;

			BuildEmuIITrack(imgldr_ctx->hxcfe,tracknumber,sidenumber,sector_data,currentside->databuffer,&currentside->tracklen,1);
	}

	hxc_fclose(f);
	return HXCFE_NOERROR;
}
Exemple #25
0
int HFE_libLoad_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	picfileformatheader header;
	unsigned int i,j,k,l,offset,offset2;
	CYLINDER* currentcylinder;
	SIDE* currentside;
    pictrack* trackoffsetlist;
    unsigned int tracks_base;
    unsigned char * hfetrack;
	unsigned int nbofblock,tracklen;


	floppycontext->hxc_printf(MSG_DEBUG,"HFE_libLoad_DiskFile %s",imgfile);
	
	f=hxc_fopen(imgfile,"rb");
	if(f==NULL) 
	{
		floppycontext->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}
	
	fread(&header,sizeof(header),1,f);

	if(!strcmp(header.HEADERSIGNATURE,"HXCPICFE"))
	{

		floppydisk->floppyNumberOfTrack=header.number_of_track;
		floppydisk->floppyNumberOfSide=header.number_of_side;
		floppydisk->floppyBitRate=header.bitRate*1000;
		floppydisk->floppySectorPerTrack=-1;
		floppydisk->floppyiftype=header.floppyinterfacemode;


		floppycontext->hxc_printf(MSG_DEBUG,"HFE File : %d track, %d side, %d bit/s, %d sectors, interface mode %s, track encoding:%s",
			floppydisk->floppyNumberOfTrack,
			floppydisk->floppyNumberOfSide,
			floppydisk->floppyBitRate,
			floppydisk->floppySectorPerTrack,
			floppydisk->floppyiftype<0xC?interfacemodecode[floppydisk->floppyiftype]:"Unknow!",
			(header.track_encoding&(~3))?trackencodingcode[4]:trackencodingcode[header.track_encoding&0x3]);

        trackoffsetlist=(pictrack*)malloc(sizeof(pictrack)* header.number_of_track);
        memset(trackoffsetlist,0,sizeof(pictrack)* header.number_of_track);
        fseek( f,512,SEEK_SET);
        fread( trackoffsetlist,sizeof(pictrack)* header.number_of_track,1,f);

        tracks_base= 512+( (((sizeof(pictrack)* header.number_of_track)/512)+1)*512);
        fseek( f,tracks_base,SEEK_SET);

		floppydisk->tracks=(CYLINDER**)malloc(sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);
		memset(floppydisk->tracks,0,sizeof(CYLINDER*)*floppydisk->floppyNumberOfTrack);

		for(i=0;i<floppydisk->floppyNumberOfTrack;i++)
		{			

			fseek(f,(trackoffsetlist[i].offset*512),SEEK_SET);
			if(trackoffsetlist[i].track_len&0x1FF)
			{
				tracklen=(trackoffsetlist[i].track_len&(~0x1FF))+0x200;
			}
			else
			{
				tracklen=trackoffsetlist[i].track_len;
			}

			hfetrack=(unsigned char*)malloc( tracklen );
			
			floppycontext->hxc_printf(MSG_DEBUG,"HFE File : reading track %d, track size:%d - file offset:%.8X",
				i,tracklen,(trackoffsetlist[i].offset*512));
			
			fread( hfetrack,tracklen,1,f);


			floppydisk->tracks[i]=(CYLINDER*)malloc(sizeof(CYLINDER));
			currentcylinder=floppydisk->tracks[i];
			currentcylinder->number_of_side=floppydisk->floppyNumberOfSide;
			currentcylinder->sides=(SIDE**)malloc(sizeof(SIDE*)*currentcylinder->number_of_side);
			memset(currentcylinder->sides,0,sizeof(SIDE*)*currentcylinder->number_of_side);
			currentcylinder->floppyRPM=header.floppyRPM;
			

		/*	floppycontext->hxc_printf(MSG_DEBUG,"read track %d side %d at offset 0x%x (0x%x bytes)",
			trackdesc.track_number,
			trackdesc.side_number,
			trackdesc.mfmtrackoffset,
			trackdesc.mfmtracksize);
*/
			for(j=0;j<currentcylinder->number_of_side;j++)
			{
				currentcylinder->sides[j]=malloc(sizeof(SIDE));
				memset(currentcylinder->sides[j],0,sizeof(SIDE));
				currentside=currentcylinder->sides[j];

				currentside->number_of_sector=floppydisk->floppySectorPerTrack;
				currentside->tracklen=tracklen/2;

				currentside->databuffer=malloc(currentside->tracklen);
				memset(currentside->databuffer,0,currentside->tracklen);
						
				currentside->flakybitsbuffer=0;
							
				currentside->indexbuffer=malloc(currentside->tracklen);
				memset(currentside->indexbuffer,0,currentside->tracklen);
							
				for(k=0;k<256;k++)
				{
					currentside->indexbuffer[k]=0xFF;
				}
						
				currentside->timingbuffer=0;
				currentside->bitrate=floppydisk->floppyBitRate;

				currentside->track_encoding=header.track_encoding;

				if( i == 0 )
				{
					if ( j == 0 )
					{
						if(!header.track0s0_altencoding)
						{
							currentside->track_encoding = header.track0s0_encoding;
						}
					}
					else
					{
						if(!header.track0s1_altencoding)
						{
							currentside->track_encoding = header.track0s1_encoding;
						}
					}
				}
				

				nbofblock=(currentside->tracklen/256);
				for(k=0;k<nbofblock;k++)
				{
					for(l=0;l<256;l++)
					{
						offset=(k*256)+l;
						offset2=(k*512)+l+(256*j);
						currentside->databuffer[offset]=bit_inverter[hfetrack[offset2]];
					}
				}

				currentside->tracklen=currentside->tracklen*8;

			}
		
			free(hfetrack);
		}
		
		free(trackoffsetlist);
	
		hxc_fclose(f);
		return HXCFE_NOERROR;
	}	
	
	hxc_fclose(f);	
	floppycontext->hxc_printf(MSG_ERROR,"bad header");
	return HXCFE_BADFILE;
}
int TeleDisk_libIsValidDiskFile(HXCFE_IMGLDR * imgldr_ctx,char * imgfile)
{
	int pathlen,i;
	FILE * f;
	unsigned char crctable[32];
	unsigned char CRC16_High,CRC16_Low;
	TELEDISK_HEADER td_header;
	unsigned char * ptr;

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile");
	if(imgfile)
	{
		pathlen=strlen(imgfile);
		if(pathlen!=0)
		{
			f=hxc_fopen(imgfile,"rb");
			if(f==NULL)
			{
				imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libIsValidDiskFile : Cannot open %s !",imgfile);
				return HXCFE_ACCESSERROR;
			}

			fseek(f,0,SEEK_SET);

			memset(&td_header,0,sizeof(TELEDISK_HEADER));
			fread( &td_header, sizeof(TELEDISK_HEADER), 1, f );

			i=ftell(f);

			if(ftell(f)==sizeof(TELEDISK_HEADER))
			{

				if ( ((td_header.TXT[0]!='t') || (td_header.TXT[1]!='d')) && ((td_header.TXT[0]!='T') || (td_header.TXT[1]!='D')))
				{
					imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile : bad header tag !");
					hxc_fclose(f);
					return HXCFE_BADFILE;
				}

				CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000);
				ptr=(unsigned char*)&td_header;
				for(i=0;i<0xA;i++)
				{
					CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable );
				}

				if(((td_header.CRC[1]<<8)|td_header.CRC[0]) != ((CRC16_High<<8)|CRC16_Low))
				{
					imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"bad header crc !");
					hxc_fclose(f);
     				return HXCFE_BADFILE;
				}

				imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile : it's a Tele disk file!");
				hxc_fclose(f);
				return HXCFE_VALIDFILE;
			}

			imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libIsValidDiskFile : bad header tag !");
			hxc_fclose(f);
     		return HXCFE_BADFILE;
		}
	}

	return HXCFE_BADPARAMETER;
}
int EDE_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters)
{

	FILE * f;
	unsigned int filesize;
	int i,j,l,k;
	int gap3len,interleave;
	int rpm,sectorsize;
	HXCFE_CYLINDER* currentcylinder;
	unsigned char header_buffer[512];
	int header_offset;
	int blocknum;
	int number_of_block;
	unsigned char bitmask;
	int floppy_buffer_index;
	unsigned char trackformat;
	int skew;
	HXCFE_SECTCFG  * sectorconfig;
	unsigned int sectorsizelayout[32];
	unsigned int sectoridlayout[32];

	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"EDE_libLoad_DiskFile %s",imgfile);

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek (f , 0 , SEEK_END);
	filesize=ftell(f);
	fseek (f , 0 , SEEK_SET);


	fread(header_buffer,0x200,1,f);

	if((filesize!=0) && (header_buffer[0]==0x0D) && (header_buffer[1]==0x0A))
	{

		floppydisk->floppyiftype=GENERIC_SHUGART_DD_FLOPPYMODE;
		sectorsize=512;
		rpm=300;
		trackformat=ISOFORMAT_DD;
		skew=0;
		switch(header_buffer[0x1FF])
		{

			case 0x01:
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"Mirage (DD) format");
				header_offset=0xA0;
				sectorsize=1024;
				floppydisk->floppyBitRate=250000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=1;
				floppydisk->floppySectorPerTrack=6;
				gap3len=255;
				interleave=1;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=1;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k-1;
				sectoridlayout[0]=5;
				sectorsizelayout[0]=512;
				break;

			case 0x02:
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"SQ-80 (DD) format");
				header_offset=0xA0;
				sectorsize=1024;
				rpm=290;
				floppydisk->floppyBitRate=250000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=2;
				floppydisk->floppySectorPerTrack=6;
				gap3len=255;
				interleave=1;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=1;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k-1;
				sectoridlayout[0]=5;
				sectorsizelayout[0]=512;
				break;

			case 0x03:
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"EPS (DD) format");
				header_offset=0xA0;
				floppydisk->floppyBitRate=250000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=2;
				floppydisk->floppySectorPerTrack=10;
				gap3len=36;
				interleave=1;
				skew=2;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k;
				break;

			case 0x04:
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"VFX-SD (DD) format");
				header_offset=0xA0;
				floppydisk->floppyBitRate=250000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=2;
				floppydisk->floppySectorPerTrack=10;
				gap3len=36;
				interleave=1;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k;
				break;

			case 0xcb:
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"ASR-10 HD format");
				floppydisk->floppyiftype=IBMPC_HD_FLOPPYMODE;
				header_offset=0x60;
				floppydisk->floppyBitRate=500000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=2;
				floppydisk->floppySectorPerTrack=20;
				// ASR 10 GAP3 verified with an HFE image formatted from one ASR-10
				gap3len = 36;
				interleave=1;
				skew=2;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k;
				break;

			case 0xcc:
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"TS-10/12 HD format");
				floppydisk->floppyiftype=IBMPC_HD_FLOPPYMODE;
				header_offset=0x60;
				floppydisk->floppyBitRate=500000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=2;
				floppydisk->floppySectorPerTrack=20;
				gap3len=40;
				interleave=1;
				skew=2;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k;
				break;

			case 0x07:
				floppydisk->floppyiftype=IBMPC_DD_FLOPPYMODE;
				imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_0,"TS-10/12 DD format");
				header_offset=0xA0;
				floppydisk->floppyBitRate=250000;
				floppydisk->floppyNumberOfTrack=80;
				floppydisk->floppyNumberOfSide=2;
				floppydisk->floppySectorPerTrack=10;
				gap3len=40;
				interleave=1;
				skew=2;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectorsizelayout[k]=sectorsize;
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)sectoridlayout[k]=k;
				break;

			default:
				imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Unknow format : %x !",header_buffer[0x1FF]);
				hxc_fclose(f);
				return HXCFE_BADFILE;
			break;
		}

		number_of_block=floppydisk->floppyNumberOfTrack*floppydisk->floppyNumberOfSide*floppydisk->floppySectorPerTrack;

		sectorconfig=malloc(sizeof(HXCFE_SECTCFG) * floppydisk->floppySectorPerTrack);
		memset(sectorconfig,0,sizeof(HXCFE_SECTCFG) * floppydisk->floppySectorPerTrack);

		floppy_buffer_index=0;
		blocknum=0;
		bitmask=0x80;

		floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);

		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks, %d side(s), %d sectors/track, gap3:%d, interleave:%d,rpm:%d",floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,floppydisk->floppySectorPerTrack,gap3len,interleave,rpm);

		for(j=0;j<floppydisk->floppyNumberOfTrack;j++)
		{

			floppydisk->tracks[j]=allocCylinderEntry(rpm,floppydisk->floppyNumberOfSide);
			currentcylinder=floppydisk->tracks[j];

			for(i=0;i<floppydisk->floppyNumberOfSide;i++)
			{
				hxcfe_imgCallProgressCallback(imgldr_ctx,(j<<1) + (i&1),floppydisk->floppyNumberOfTrack*2 );

				memset(sectorconfig,0,sizeof(HXCFE_SECTCFG)*floppydisk->floppySectorPerTrack);
				for(k=0;k<floppydisk->floppySectorPerTrack;k++)
				{
					sectorconfig[k].head=i;
					sectorconfig[k].cylinder=j;
					sectorconfig[k].sector=sectoridlayout[k];
					sectorconfig[k].sectorsize=sectorsizelayout[k];
					sectorconfig[k].bitrate=floppydisk->floppyBitRate;
					sectorconfig[k].gap3=gap3len;
					sectorconfig[k].trackencoding=trackformat;
					sectorconfig[k].input_data=malloc(sectorsize);
					memset(sectorconfig[k].input_data,0,sectorsize);

					if(blocknum<number_of_block)
					{
						if(!(header_buffer[header_offset]&bitmask))
						{
							imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"T:%.3d S:%d Sector:%.2d Size:%.4d File offset: 0x%.8x",j,i,sectorconfig[k].sector,sectorconfig[k].sectorsize,ftell(f));
							fread(sectorconfig[k].input_data,sectorsize,1,f);
						}
						else
						{
							imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"T:%.3d S:%d Sector:%.2d Size:%.4d File offset: ----------",j,i,sectorconfig[k].sector,sectorconfig[k].sectorsize);
							for(l=0;l<(sectorconfig[k].sectorsize/2);l++)
							{
								sectorconfig[k].input_data[(l*2)]=0x6D;
								sectorconfig[k].input_data[(l*2)+1]=0xB6;
							}
						}
						bitmask=bitmask>>1;
						if(!bitmask)
						{
							header_offset++;
							bitmask=0x80;
						}
						blocknum++;
					}
					else
					{
						imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"T:%.3d S:%d Sector:%.2d Size:%.4d",j,i,sectorconfig[k].sector,sectorconfig[k].sectorsize);
						for(l=0;l<(sectorconfig[k].sectorsize/2);l++)
						{
							sectorconfig[k].input_data[(l*2)]=0x6D;
							sectorconfig[k].input_data[(l*2)+1]=0xB6;
						}
					}
				}

				currentcylinder->sides[i]=tg_generateTrackEx(floppydisk->floppySectorPerTrack,sectorconfig,interleave,(unsigned char)(((j<<1)|(i&1))*skew),floppydisk->floppyBitRate,rpm,trackformat,128,2500,-2500);

				for(k=0;k<floppydisk->floppySectorPerTrack;k++)
				{
					free(sectorconfig[k].input_data);
				}
			}
		}
Exemple #28
0
int D88_libWrite_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppy,char * filename)
{
	int i,j,k,nbsector;
	FILE * d88file;
	char * log_str;
	char   tmp_str[256];
	int track_cnt,density;
	d88_fileheader d88_fh;
	d88_sector d88_s;
	unsigned long tracktable[164];

	int mfmdd_found;
	int mfmhd_found;
	int fmdd_found;
	int fmhd_found;

	int maxtrack;

	int nb_valid_sector;

	SECTORSEARCH* ss;
	SECTORCONFIG** sca;

	floppycontext->hxc_printf(MSG_INFO_1,"Write D88 file %s...",filename);

	log_str=0;

	mfmdd_found = 0;
	mfmhd_found = 0;
	fmdd_found = 0;
	fmhd_found = 0;
	maxtrack = 0;

	d88file=hxc_fopen(filename,"wb");
	if(d88file)
	{
		memset(&d88_fh,0,sizeof(d88_fileheader));
		sprintf((char*)&d88_fh.name,"HxCFE");
		fwrite(&d88_fh,sizeof(d88_fileheader),1,d88file);

		memset(tracktable,0,sizeof(tracktable));
		fwrite(&tracktable, sizeof(tracktable),1,d88file);

		track_cnt=0;

		ss = hxcfe_initSectorSearch(floppycontext,floppy);
		if( ss )
		{
			for(j=0;j<(int)floppy->floppyNumberOfTrack;j++)
			{
				for(i=0;i<(int)floppy->floppyNumberOfSide;i++)
				{
					sprintf(tmp_str,"track:%.2d:%d file offset:0x%.6x, sectors: ",j,i,(unsigned int)ftell(d88file));

					log_str=0;
					log_str=realloc(log_str,strlen(tmp_str)+1);
					memset(log_str,0,strlen(tmp_str)+1);
					strcat(log_str,tmp_str);

					if((j<<1 | i) <164)
					{
						sca = hxcfe_getAllTrackISOSectors(ss,j,i,&nbsector);
						if(sca)
						{

							tracktable[(j<<1 | i)] = ftell(d88file);

							nb_valid_sector = 0;
							for(k = 0; k < nbsector; k++)
							{
								if(sca[k]->sectorsize)
								{
									nb_valid_sector++;
								}
							}

							for(k = 0; k < nbsector; k++)
							{
								if(sca[k]->sectorsize)
								{
									memset(&d88_s,0,sizeof(d88_sector));

									density = 0;

									if(sca[k]->trackencoding == ISOFORMAT_DD )
									{
										density = 1;

										if(sca[k]->bitrate>400000)
											mfmhd_found++;
										else
											mfmdd_found++;
									}

									if(	sca[k]->trackencoding == ISOFORMAT_SD )
									{

										if(sca[k]->bitrate>400000)
											fmhd_found++;
										else
											fmdd_found++;
									}

									d88_s.cylinder = j;
									d88_s.head = i;
									d88_s.number_of_sectors = nb_valid_sector;
									d88_s.sector_id = sca[k]->sector;

									if(sca[k]->use_alternate_datamark && sca[k]->alternate_datamark == 0xF8)
										d88_s.deleted_data = 0x10;

									if(!density)
										d88_s.density = 0x40;

									d88_s.sector_size = size_to_code_d88(sca[k]->sectorsize);
									d88_s.sector_length = sca[k]->sectorsize;
									if(!sca[k]->input_data)
										d88_s.sector_length = 0;

									if( sca[k]->alternate_addressmark != 0xFE )
									{
										d88_s.sector_status = 0xE0; // NO ADDRESS MARK
									}
									else
									{
										if( sca[k]->use_alternate_header_crc )
										{
											d88_s.sector_status = 0xA0; // ID CRC ERROR
										}
										else
										{
											if( sca[k]->alternate_datamark != 0xFB && sca[k]->alternate_datamark != 0xF8 )
											{
												d88_s.sector_status = 0xF0; // NO DATA MARK
											}
											else
											{
												if( sca[k]->use_alternate_data_crc )
												{
													d88_s.sector_status = 0xB0; // DATA CRC ERROR
												}
												else
												{
													if( sca[k]->alternate_datamark == 0xF8 )
														d88_s.sector_status = 0x10; // DELETED SECTOR
												}
											}
										}
									}

									fwrite(&d88_s,sizeof(d88_sector),1,d88file);

									if(d88_s.sector_length)
										fwrite(sca[k]->input_data,sca[k]->sectorsize,1,d88file);
								}

								if(sca[k]->input_data)
									free(sca[k]->input_data);

								free(sca[k]);
							}

							if(i==0)
								maxtrack++;

							free(sca);
						}
					}

					floppycontext->hxc_printf(MSG_INFO_1,log_str);
					free(log_str);

				}
			}

			hxcfe_deinitSectorSearch(ss);

		}

		// Media flag. 00h = 2D, 10h = 2DD, 20h = 2HD.
		if(	(maxtrack >= 46) )
		{
			// 2DD : 300RPM, 250Kb/s, 2 sides, 80 tracks floppy format
			// 2DD
			d88_fh.media_flag = 0x10;
		}

		if(	(maxtrack >= 46) && (mfmhd_found>mfmdd_found))
		{
			// 2HD : 360RPM, 500Kb/s, 2 sides, 77 tracks floppy format
			// 2HD
			d88_fh.media_flag = 0x20;
		}

		if(maxtrack < 46)
		{
			// 2D : 300RPM, 250Kb/s, 2 sides, 40 tracks floppy format . 
			// 2D
			d88_fh.media_flag = 0x00;
		}

		d88_fh.file_size = ftell(d88file);
		fseek(d88file,0,SEEK_SET);
		fwrite(&d88_fh,sizeof(d88_fileheader),1,d88file);
		fwrite(&tracktable, sizeof(tracktable),1,d88file);

		hxc_fclose(d88file);
	}

	return 0;
}
Exemple #29
0
int RAW_libWrite_DiskFile(HXCFLOPPYEMULATOR* floppycontext,FLOPPY * floppy,char * filename)
{	
	int i,j,k,l,nbsector;
	FILE * rawfile;
	char * log_str;
	char   tmp_str[256];
	int sectorsize,track_type_id;

	SECTORSEARCH* ss;
	SECTORCONFIG** sca;

	floppycontext->hxc_printf(MSG_INFO_1,"Write RAW file %s...",filename);

	track_type_id=0;
	log_str=0;

	rawfile=hxc_fopen(filename,"wb");
	if(rawfile)
	{
		ss=hxcfe_initSectorSearch(floppycontext,floppy);
		if(ss)
		{
			for(j=0;j<(int)floppy->floppyNumberOfTrack;j++)
			{
				for(i=0;i<(int)floppy->floppyNumberOfSide;i++)
				{
					sprintf(tmp_str,"track:%.2d:%d file offset:0x%.6x, sectors: ",j,i,(unsigned int)ftell(rawfile));

					log_str=0;
					log_str=realloc(log_str,strlen(tmp_str)+1);
					memset(log_str,0,strlen(tmp_str)+1);
					strcat(log_str,tmp_str);

					k=0;
					do
					{
						switch(track_type_id)
						{
							case 0:
								sca = hxcfe_getAllTrackSectors(ss,j,i,ISOIBM_MFM_ENCODING,&nbsector);
							break;
							case 1:
								sca = hxcfe_getAllTrackSectors(ss,j,i,ISOIBM_FM_ENCODING,&nbsector);
							break;
							case 2:
								sca = hxcfe_getAllTrackSectors(ss,j,i,AMIGA_MFM_ENCODING,&nbsector);
							break;
							case 3:
								sca = hxcfe_getAllTrackSectors(ss,j,i,EMU_FM_ENCODING,&nbsector);
							break;
							case 4:
								sca = hxcfe_getAllTrackSectors(ss,j,i,TYCOM_FM_ENCODING,&nbsector);
							break;
							case 5:
								sca = hxcfe_getAllTrackSectors(ss,j,i,MEMBRAIN_MFM_ENCODING,&nbsector);
							break;

						}

						if(!nbsector)
							track_type_id=(track_type_id+1)%6;

						k++;

					}while(!nbsector && k<6);

					if(nbsector)
					{
						sectorsize=sca[0]->sectorsize;
						for(l=0;l<256;l++)
						{

							k=0;
							do
							{
								if(sca[k]->sector==l)
								{
									if(sca[k]->sectorsize!=(unsigned int)sectorsize)
									{
										sectorsize=-1;
									}
									if(sca[k]->input_data)
										fwrite(sca[k]->input_data,sca[k]->sectorsize,1,rawfile);

									sprintf(tmp_str,"%d ",sca[k]->sector);
									log_str=realloc(log_str,strlen(log_str)+strlen(tmp_str)+1);
									strcat(log_str,tmp_str);
									break;
								}

								k++;
							}while(k<nbsector);
						}

						k=0;
						do
						{
							if(sca[k])
							{
								if(sca[k]->input_data)
									free(sca[k]->input_data);
								free(sca[k]);
							}
							k++;
						}while(k<nbsector);


						if(sectorsize!=-1)
						{
							sprintf(tmp_str,",%dB/s",sectorsize);
						}
						else
						{
							sprintf(tmp_str,"");
						}

						log_str=realloc(log_str,strlen(log_str)+strlen(tmp_str)+1);
						strcat(log_str,tmp_str);

					}

					floppycontext->hxc_printf(MSG_INFO_1,log_str);
					free(log_str);

				}
			}
			
			hxcfe_deinitSectorSearch(ss);
		}

		hxc_fclose(rawfile);
	}

	return 0;
}
int TeleDisk_libLoad_DiskFile(HXCFE_IMGLDR * imgldr_ctx,HXCFE_FLOPPY * floppydisk,char * imgfile,void * parameters)
{
	FILE * f;
	unsigned int i;
	unsigned int file_offset;
	uint32_t tracklen;
	unsigned char interleave,skew,trackformat;
	unsigned short rpm,sectorsize;
	int Compress,numberoftrack,sidenumber;
	unsigned short * datalen;
	HXCFE_CYLINDER* currentcylinder;
	HXCFE_SIDE* currentside;
	TELEDISK_HEADER        *td_header;
	TELEDISK_TRACK_HEADER  *td_track_header;
	TELEDISK_SECTOR_HEADER *td_sector_header;
	TELEDISK_COMMENT * td_comment;
	unsigned char tempdata[8*1024];
	unsigned char crctable[32];
	unsigned char CRC16_High,CRC16_Low;
	unsigned char * ptr;
	uint32_t filesize;
	HXCFE_SECTCFG  * sectorconfig;
	unsigned char * fileimage;
	uint32_t fileimage_buffer_offset;
	int rlen;
	imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"TeleDisk_libLoad_DiskFile %s",imgfile);

	hxcfe_imgCallProgressCallback(imgldr_ctx,0,100 );

	f=hxc_fopen(imgfile,"rb");
	if(f==NULL)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : Cannot open %s !",imgfile);
		return HXCFE_ACCESSERROR;
	}

	fseek(f,0,SEEK_END);
	filesize=ftell(f);
	if(!filesize)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : 0 byte file !");
		hxc_fclose(f);
		return HXCFE_BADFILE;
	}
	fseek(f,0,SEEK_SET);

	fileimage_buffer_offset=0;
	fileimage=(unsigned char*)malloc(filesize+512);
	if(!fileimage)
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : Malloc error !");
		hxc_fclose(f);
		return HXCFE_INTERNALERROR;
	}
	memset(fileimage,0,filesize+512);
	fread(fileimage,filesize,1,f);
	hxc_fclose(f);


	td_header=(TELEDISK_HEADER*)&fileimage[fileimage_buffer_offset];
	fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_HEADER);

	if ( ((td_header->TXT[0]!='t') || (td_header->TXT[1]!='d')) && ((td_header->TXT[0]!='T') || (td_header->TXT[1]!='D')))
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : bad header tag !");
		free(fileimage);
		return HXCFE_BADFILE;
	}

	CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000);
	ptr=(unsigned char*)td_header;
	for(i=0;i<0xA;i++)
	{
		CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable );
	}

	if(((td_header->CRC[1]<<8)|td_header->CRC[0])!=((CRC16_High<<8)|CRC16_Low))
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : bad header crc !");
		free(fileimage);
		return HXCFE_BADFILE;
	}

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Teledisk version : %d",td_header->TDVer);
	if((td_header->TDVer>21) || (td_header->TDVer<10))
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"TeleDisk_libLoad_DiskFile : Unsupported version !");
		free(fileimage);
		return HXCFE_BADFILE;
	}

	Compress=0;
	if(((td_header->TXT[0]=='T') && (td_header->TXT[1]=='D')))
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Normal compression");
		Compress=0;
	}

	if(((td_header->TXT[0]=='t') && (td_header->TXT[1]=='d')))
	{
		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Advanced compression");
		fileimage=unpack(fileimage,filesize);
		Compress=1;
	}

	td_header=(TELEDISK_HEADER*)&fileimage[0];

	if(td_header->TrkDens&0x80)
	{

		CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000);

		td_comment=(TELEDISK_COMMENT *)&fileimage[fileimage_buffer_offset];
		fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_COMMENT);

		//fread( &td_comment, sizeof(td_comment), 1, f );
		ptr=(unsigned char*)td_comment;
		ptr=ptr+2;
		for(i=0;i<sizeof(TELEDISK_COMMENT)-2;i++)
		{
			CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable );
		}

		memcpy(&tempdata,&fileimage[fileimage_buffer_offset],td_comment->Len);
		fileimage_buffer_offset=fileimage_buffer_offset+td_comment->Len;

		ptr=(unsigned char*)&tempdata;
		for(i=0;i<td_comment->Len;i++)
		{
			CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable );
		}


		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Creation date: %.2d/%.2d/%.4d %.2d:%.2d:%.2d",td_comment->bDay,\
																							td_comment->bMon+1,\
																							td_comment->bYear+1900,\
																							td_comment->bHour,\
																							td_comment->bMin,\
																							td_comment->bSec);
		imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"TeleDisk_libLoad_DiskFile : Comment: %s",tempdata);

	}

	interleave=1;
	numberoftrack=0;
	sectorsize=512;

	file_offset=fileimage_buffer_offset;

	floppydisk->floppyNumberOfSide=td_header->Surface;

	td_track_header=(TELEDISK_TRACK_HEADER  *)&fileimage[fileimage_buffer_offset];
	fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER);

	while(td_track_header->SecPerTrk!=0xFF)
	{
		if(td_track_header->PhysCyl>numberoftrack)
		{
			numberoftrack=td_track_header->PhysCyl;
		}
		CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000);
		ptr=(unsigned char*)td_track_header;
		for(i=0;i<0xA;i++)
		{
			CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable );
		}


		for ( i=0;i < td_track_header->SecPerTrk;i++ )
		{
			td_sector_header=(TELEDISK_SECTOR_HEADER  *)&fileimage[fileimage_buffer_offset];
			fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_SECTOR_HEADER);

			if  ( (td_sector_header->Syndrome & 0x30) == 0 && (td_sector_header->SLen & 0xf8) == 0 )
			{
				//fileimage_buffer_offset=fileimage_buffer_offset+sizeof(unsigned short);

				datalen=(unsigned short*)&fileimage[fileimage_buffer_offset];
				fileimage_buffer_offset=fileimage_buffer_offset+(*datalen)+2;
			}
		}
		td_track_header=(TELEDISK_TRACK_HEADER  *)&fileimage[fileimage_buffer_offset];
		fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER);
	}

	floppydisk->floppyNumberOfTrack=numberoftrack+1;
	floppydisk->floppySectorPerTrack=-1;
	floppydisk->tracks=(HXCFE_CYLINDER**)malloc(sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);
	memset(floppydisk->tracks,0,sizeof(HXCFE_CYLINDER*)*floppydisk->floppyNumberOfTrack);

	//Source disk density (0 = 250K bps,  1 = 300K bps,  2 = 500K bps ; +128 = single-density FM)
	switch(td_header->Dens)
	{
		case 0:
			floppydisk->floppyBitRate=250000;
			break;
		case 1:
			floppydisk->floppyBitRate=300000;
			break;
		case 2:
			floppydisk->floppyBitRate=500000;
			break;
		default:
			floppydisk->floppyBitRate=250000;
			break;
	}

	floppydisk->floppyiftype=GENERIC_SHUGART_DD_FLOPPYMODE;

	skew=1;
	rpm=300; // normal rpm
	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks, %d side(s), gap3:%d,rpm:%d bitrate:%d",floppydisk->floppyNumberOfTrack,floppydisk->floppyNumberOfSide,floppydisk->floppySectorPerTrack,rpm,floppydisk->floppyBitRate);

	tracklen=(floppydisk->floppyBitRate/(rpm/60))/4;

	//////////////////////////////////
	fileimage_buffer_offset=file_offset;

	td_track_header=(TELEDISK_TRACK_HEADER  *)&fileimage[fileimage_buffer_offset];
	fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER);

	while(td_track_header->SecPerTrk!=0xFF)
	{
		if(td_track_header->PhysSide&0x7F)
		{
			sidenumber=1;
		}
		else
		{
			sidenumber=0;
		}

		if(td_track_header->PhysSide&0x80)
		{
			trackformat=IBMFORMAT_SD;
		}
		else
		{
			trackformat=IBMFORMAT_DD;
		}

		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"------------- Track:%d, Side:%d, Number of Sector:%d -------------",td_track_header->PhysCyl,sidenumber,td_track_header->SecPerTrk);

		if(!floppydisk->tracks[td_track_header->PhysCyl])
		{
			floppydisk->tracks[td_track_header->PhysCyl]=(HXCFE_CYLINDER*)malloc(sizeof(HXCFE_CYLINDER));
			memset(floppydisk->tracks[td_track_header->PhysCyl],0,sizeof(HXCFE_CYLINDER));
		}

		currentcylinder=floppydisk->tracks[td_track_header->PhysCyl];
		currentcylinder->number_of_side=floppydisk->floppyNumberOfSide;
		if(!currentcylinder->sides)
		{
			currentcylinder->sides=(HXCFE_SIDE**)malloc(sizeof(HXCFE_SIDE*)*currentcylinder->number_of_side);
			memset(currentcylinder->sides,0,sizeof(HXCFE_SIDE*)*currentcylinder->number_of_side);
		}

		currentcylinder->floppyRPM=rpm;

		////////////////////crc track header///////////////////
		CRC16_Init(&CRC16_High,&CRC16_Low,(unsigned char*)crctable,0xA097,0x0000);
		ptr=(unsigned char*)td_track_header;
		for(i=0;i<0x3;i++)
		{
			CRC16_Update(&CRC16_High,&CRC16_Low, ptr[i],(unsigned char*)crctable );
		}
		if(CRC16_Low!=td_track_header->CRC)
		{
			imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"!!!! Track header CRC Error !!!!");
		}
		////////////////////////////////////////////////////////

		sectorconfig=(HXCFE_SECTCFG  *)malloc(sizeof(HXCFE_SECTCFG)*td_track_header->SecPerTrk);
		memset(sectorconfig,0,sizeof(HXCFE_SECTCFG)*td_track_header->SecPerTrk);
		for ( i=0;i < td_track_header->SecPerTrk;i++ )
		{
			td_sector_header=(TELEDISK_SECTOR_HEADER  *)&fileimage[fileimage_buffer_offset];
			fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_SECTOR_HEADER);

			sectorconfig[i].cylinder=td_sector_header->Cyl;
			sectorconfig[i].head=td_sector_header->Side;
			sectorconfig[i].sector=td_sector_header->SNum;
			sectorconfig[i].sectorsize=128<<td_sector_header->SLen;
			sectorconfig[i].bitrate=floppydisk->floppyBitRate;
			sectorconfig[i].gap3=255;
			sectorconfig[i].trackencoding=trackformat;


			if(td_sector_header->Syndrome & 0x04)
			{
				sectorconfig[i].use_alternate_datamark=1;
				sectorconfig[i].alternate_datamark=0xF8;
			}

			if(td_sector_header->Syndrome & 0x02)
			{
				sectorconfig[i].use_alternate_data_crc=2;
			}

			if(td_sector_header->Syndrome & 0x20)
			{
				sectorconfig[i].missingdataaddressmark=1;
			}

			sectorconfig[i].input_data=malloc(sectorconfig[i].sectorsize);
			if  ( (td_sector_header->Syndrome & 0x30) == 0 && (td_sector_header->SLen & 0xf8) == 0 )
			{
				//fileimage_buffer_offset=fileimage_buffer_offset+sizeof(unsigned short);

				datalen=(unsigned short*)&fileimage[fileimage_buffer_offset];
				memcpy(&tempdata,&fileimage[fileimage_buffer_offset],(*datalen)+2);
				fileimage_buffer_offset=fileimage_buffer_offset+(*datalen)+2;

				rlen=RLEExpander(tempdata,sectorconfig[i].input_data,(int)*datalen);
			}
			else
			{
				memset(sectorconfig[i].input_data,0,sectorconfig[i].sectorsize);
			}

			imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"track:%d, side:%d, sector:%d, sectorsize:%d, flag:%.2x",sectorconfig[i].cylinder,sectorconfig[i].head,sectorconfig[i].sector,sectorconfig[i].sectorsize,td_sector_header->Syndrome);
		}

		currentside=tg_generateTrackEx((unsigned short)td_track_header->SecPerTrk,sectorconfig,interleave,0,floppydisk->floppyBitRate,rpm,trackformat,0,2500 | NO_SECTOR_UNDER_INDEX,-2500);
		currentcylinder->sides[sidenumber]=currentside;

		for ( i=0;i < td_track_header->SecPerTrk;i++ )
		{
			if(sectorconfig[i].input_data)
				free(sectorconfig[i].input_data);
		}
		free(sectorconfig);

		td_track_header=(TELEDISK_TRACK_HEADER  *)&fileimage[fileimage_buffer_offset];
		fileimage_buffer_offset=fileimage_buffer_offset+sizeof(TELEDISK_TRACK_HEADER);

	}

	imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"track file successfully loaded and encoded!");

	free(fileimage);

	hxcfe_imgCallProgressCallback(imgldr_ctx,100,100 );

	hxcfe_sanityCheck(imgldr_ctx->hxcfe,floppydisk);

	return HXCFE_NOERROR;
}