From edb8f8563cbfb6500c22ea8644ff5f9664045777 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20DEL=20NERO?= Date: Mon, 13 Nov 2023 08:11:05 +0100 Subject: [PATCH] use "HXCRAMFILE" api. --- .../loaders/hfe_loader/exthfe_writer.c | 24 +- .../loaders/hfe_loader/hfe_hddd_a2_writer.c | 24 +- .../sources/loaders/hfe_loader/hfe_writer.c | 42 +- .../sources/loaders/hfe_loader/hfev3_writer.c | 473 +++++++++--------- 4 files changed, 258 insertions(+), 305 deletions(-) diff --git a/libhxcfe/sources/loaders/hfe_loader/exthfe_writer.c b/libhxcfe/sources/loaders/hfe_loader/exthfe_writer.c index 2e7ce0116..479e192dc 100644 --- a/libhxcfe/sources/loaders/hfe_loader/exthfe_writer.c +++ b/libhxcfe/sources/loaders/hfe_loader/exthfe_writer.c @@ -73,19 +73,9 @@ void extaddpad(unsigned char * track,int mfmsize,int tracksize) } } -typedef struct RAMFILE_ -{ - uint8_t * ramfile; - int32_t ramfile_size; -}RAMFILE; - -FILE * rfopen(char* fn,char * mode,RAMFILE * rf); -int rfwrite(void * buffer,int size,int mul,FILE * file,RAMFILE * rf); -int rfclose(FILE *f,RAMFILE * rf); - int EXTHFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename) { - RAMFILE rf; + HXCRAMFILE rf; picexttrack * track; FILE * hxcpicfile; @@ -103,7 +93,7 @@ int EXTHFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char factor=1;// factor=1-> 50% duty cycle // factor=2-> 25% duty cycle imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Write HFE file %s for the standalone emulator.",filename); - hxcpicfile = rfopen(filename,"wb",&rf); + hxcpicfile = hxc_ram_fopen(filename,"wb",&rf); if(hxcpicfile) { @@ -166,7 +156,7 @@ int EXTHFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char else FILEHEADER->single_step=0xFF; - rfwrite(FILEHEADER,512,1,hxcpicfile,&rf); + hxc_ram_fwrite(FILEHEADER,512,1,hxcpicfile,&rf); tracklistlen=((((((FILEHEADER->number_of_track)+1)*sizeof(picexttrack))/512)+1)); offsettrack=(unsigned char*) malloc(tracklistlen*512); @@ -218,7 +208,7 @@ int EXTHFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char i++; }; - rfwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); + hxc_ram_fwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); i=0; while(i<(FILEHEADER->number_of_track)) @@ -327,7 +317,7 @@ int EXTHFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char } - rfwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); + hxc_ram_fwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); free(mfmtracks0); free(mfmtracks1); @@ -347,12 +337,12 @@ int EXTHFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char } else { - rfclose(hxcpicfile,&rf); + hxc_ram_fclose(hxcpicfile,&rf); imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot create %s!",filename); return -1; } - rfclose(hxcpicfile,&rf); + hxc_ram_fclose(hxcpicfile,&rf); imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks written to the file",FILEHEADER->number_of_track); diff --git a/libhxcfe/sources/loaders/hfe_loader/hfe_hddd_a2_writer.c b/libhxcfe/sources/loaders/hfe_loader/hfe_hddd_a2_writer.c index b015770b5..138e6a913 100644 --- a/libhxcfe/sources/loaders/hfe_loader/hfe_hddd_a2_writer.c +++ b/libhxcfe/sources/loaders/hfe_loader/hfe_hddd_a2_writer.c @@ -43,19 +43,9 @@ extern void addpad(unsigned char * track,int mfmsize,int tracksize); -typedef struct RAMFILE_ -{ - uint8_t * ramfile; - int32_t ramfile_size; -}RAMFILE; - -FILE * rfopen(char* fn,char * mode,RAMFILE * rf); -int rfwrite(void * buffer,int size,int mul,FILE * file,RAMFILE * rf); -int rfclose(FILE *f,RAMFILE * rf); - int HFE_HDDD_A2_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename) { - RAMFILE rf; + HXCRAMFILE rf; pictrack * track; FILE * hxcpicfile; @@ -79,7 +69,7 @@ int HFE_HDDD_A2_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy return -1; } - hxcpicfile=rfopen(filename,"wb",&rf); + hxcpicfile=hxc_ram_fopen(filename,"wb",&rf); if(hxcpicfile) { @@ -140,7 +130,7 @@ int HFE_HDDD_A2_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy FILEHEADER->single_step=0x00; } - rfwrite(FILEHEADER,512,1,hxcpicfile,&rf); + hxc_ram_fwrite(FILEHEADER,512,1,hxcpicfile,&rf); tracklistlen=((((((FILEHEADER->number_of_track)+1)*sizeof(pictrack))/512)+1)); offsettrack=(unsigned char*) malloc(tracklistlen*512); @@ -193,7 +183,7 @@ int HFE_HDDD_A2_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy i++; }; - rfwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); + hxc_ram_fwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); i=0; while(i<(FILEHEADER->number_of_track)) @@ -279,7 +269,7 @@ int HFE_HDDD_A2_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy } } - rfwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); + hxc_ram_fwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); free(mfmtracks0); free(mfmtracks1); @@ -298,12 +288,12 @@ int HFE_HDDD_A2_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy } else { - rfclose(hxcpicfile,&rf); + hxc_ram_fclose(hxcpicfile,&rf); imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot create %s!",filename); return -1; } - rfclose(hxcpicfile,&rf); + hxc_ram_fclose(hxcpicfile,&rf); imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks written to the file",FILEHEADER->number_of_track); diff --git a/libhxcfe/sources/loaders/hfe_loader/hfe_writer.c b/libhxcfe/sources/loaders/hfe_loader/hfe_writer.c index 855b0b40b..0d2876060 100644 --- a/libhxcfe/sources/loaders/hfe_loader/hfe_writer.c +++ b/libhxcfe/sources/loaders/hfe_loader/hfe_writer.c @@ -70,37 +70,9 @@ void addpad(unsigned char * track,int mfmsize,int tracksize) } } -typedef struct RAMFILE_ -{ - uint8_t * ramfile; - int32_t ramfile_size; -}RAMFILE; - -FILE * rfopen(char* fn,char * mode,RAMFILE * rf) -{ - rf->ramfile=0; - rf->ramfile_size=0; - return (FILE *)1; -}; - -int rfwrite(void * buffer,int size,int mul,FILE * file,RAMFILE * rf) -{ - rf->ramfile = realloc(rf->ramfile,rf->ramfile_size+size); - memcpy(&rf->ramfile[rf->ramfile_size],buffer,size); - rf->ramfile_size = rf->ramfile_size + size; - return size; -} - -int rfclose(FILE *f,RAMFILE * rf) -{ - if(rf->ramfile) - free(rf->ramfile); - return 0; -}; - int HFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename) { - RAMFILE rf; + HXCRAMFILE rf; pictrack * track; FILE * hxcpicfile; @@ -130,7 +102,7 @@ int HFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * return HXCFE_BADPARAMETER; } - hxcpicfile=rfopen(filename,"wb",&rf); + hxcpicfile = hxc_ram_fopen(filename,"wb",&rf); if(hxcpicfile) { @@ -201,7 +173,7 @@ int HFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * else FILEHEADER->single_step=0xFF; - rfwrite(FILEHEADER,512,1,hxcpicfile,&rf); + hxc_ram_fwrite(FILEHEADER,512,1,hxcpicfile,&rf); tracklistlen=((((((FILEHEADER->number_of_track)+1)*sizeof(pictrack))/512)+1)); offsettrack=(unsigned char*) malloc(tracklistlen*512); @@ -253,7 +225,7 @@ int HFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * i++; }; - rfwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); + hxc_ram_fwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); i=0; while(i<(FILEHEADER->number_of_track)) @@ -366,7 +338,7 @@ int HFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * } - rfwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); + hxc_ram_fwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); free(mfmtracks0); free(mfmtracks1); @@ -387,12 +359,12 @@ int HFE_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * } else { - rfclose(hxcpicfile,&rf); + hxc_ram_fclose(hxcpicfile,&rf); imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot create %s!",filename); return -1; } - rfclose(hxcpicfile,&rf); + hxc_ram_fclose(hxcpicfile,&rf); imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks written to the file",FILEHEADER->number_of_track); diff --git a/libhxcfe/sources/loaders/hfe_loader/hfev3_writer.c b/libhxcfe/sources/loaders/hfe_loader/hfev3_writer.c index 5eb930703..8ba807cac 100644 --- a/libhxcfe/sources/loaders/hfe_loader/hfev3_writer.c +++ b/libhxcfe/sources/loaders/hfe_loader/hfev3_writer.c @@ -55,7 +55,6 @@ opcodetrack precalcopcodestracks[256]; static void addpad(unsigned char * track,int mfmsize,int tracksize) { - int i,j,lastindex; lastindex = tracksize; @@ -81,40 +80,13 @@ static void addpad(unsigned char * track,int mfmsize,int tracksize) } } -typedef struct RAMFILE_ -{ - uint8_t * ramfile; - int32_t ramfile_size; -}RAMFILE; - -static FILE * rfopen(char* fn,char * mode,RAMFILE * rf) -{ - rf->ramfile=0; - rf->ramfile_size=0; - return (FILE *)1; -}; - -static int rfwrite(void * buffer,int size,int mul,FILE * file,RAMFILE * rf) -{ - rf->ramfile = realloc(rf->ramfile,rf->ramfile_size+size); - memcpy(&rf->ramfile[rf->ramfile_size],buffer,size); - rf->ramfile_size = rf->ramfile_size + size; - return size; -} - -static int rfclose(FILE *f,RAMFILE * rf) -{ - if(rf->ramfile) - free(rf->ramfile); - return 0; -}; - int HFEV3_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char * filename) { - RAMFILE rf; + HXCRAMFILE rf; pictrack * track; - FILE * hxcpicfile; + FILE * outfile; + FILE * ram_outfile; picfileformatheader * FILEHEADER; unsigned char * mfmtracks0,*mfmtracks1,*mfmtrackfinal; @@ -139,267 +111,296 @@ int HFEV3_libWrite_DiskFile(HXCFE_IMGLDR* imgldr_ctx,HXCFE_FLOPPY * floppy,char return -1; } - hxcpicfile=rfopen(filename,"wb",&rf); + ram_outfile = hxc_ram_fopen(filename,"wb",&rf); - if(hxcpicfile) - { - FILEHEADER=(picfileformatheader *) malloc(512); - memset(FILEHEADER,0xFF,512); - memcpy(&FILEHEADER->HEADERSIGNATURE,"HXCHFEV3",8); + outfile = NULL; + mfmtracks0 = NULL; + mfmtracks1 = NULL; + mfmtrackfinal = NULL; + offsettrack = NULL; - FILEHEADER->number_of_track=(unsigned char)floppy->floppyNumberOfTrack; - FILEHEADER->number_of_side=floppy->floppyNumberOfSide; - if(floppy->floppyBitRate!=VARIABLEBITRATE) - { - FILEHEADER->bitRate=(floppy->floppyBitRate)/1000; - } + FILEHEADER = (picfileformatheader *) malloc(512); + if( !FILEHEADER ) + goto error; + + memset(FILEHEADER,0xFF,512); + memcpy(&FILEHEADER->HEADERSIGNATURE,"HXCHFEV3",8); + + FILEHEADER->number_of_track=(unsigned char)floppy->floppyNumberOfTrack; + FILEHEADER->number_of_side=floppy->floppyNumberOfSide; + if(floppy->floppyBitRate!=VARIABLEBITRATE) + { + FILEHEADER->bitRate=(floppy->floppyBitRate)/1000; + } + else + { + if(floppy->tracks[0]->sides[0]->bitrate == VARIABLEBITRATE) + FILEHEADER->bitRate=(unsigned short)((uint32_t)(floppy->tracks[0]->sides[0]->timingbuffer[ (floppy->tracks[0]->sides[0]->tracklen/8) / 2])/1000); else - { - if(floppy->tracks[0]->sides[0]->bitrate == VARIABLEBITRATE) - FILEHEADER->bitRate=(unsigned short)((uint32_t)(floppy->tracks[0]->sides[0]->timingbuffer[ (floppy->tracks[0]->sides[0]->tracklen/8) / 2])/1000); - else - FILEHEADER->bitRate=(floppy->tracks[0]->sides[0]->bitrate)/1000; - } - FILEHEADER->floppyRPM=0;//floppy->floppyRPM; + FILEHEADER->bitRate=(floppy->tracks[0]->sides[0]->bitrate)/1000; + } - FILEHEADER->floppyinterfacemode=(unsigned char)floppy->floppyiftype; + FILEHEADER->floppyRPM=0;//floppy->floppyRPM; + FILEHEADER->floppyinterfacemode=(unsigned char)floppy->floppyiftype; - imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Floppy interface mode %s (%s)", hxcfe_getFloppyInterfaceModeName(imgldr_ctx->hxcfe,FILEHEADER->floppyinterfacemode), - hxcfe_getFloppyInterfaceModeDesc(imgldr_ctx->hxcfe,FILEHEADER->floppyinterfacemode) ); + imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"Floppy interface mode %s (%s)", hxcfe_getFloppyInterfaceModeName(imgldr_ctx->hxcfe,FILEHEADER->floppyinterfacemode), + hxcfe_getFloppyInterfaceModeDesc(imgldr_ctx->hxcfe,FILEHEADER->floppyinterfacemode) ); - FILEHEADER->track_encoding=0; - FILEHEADER->formatrevision=0; - FILEHEADER->track_list_offset=1; - FILEHEADER->write_protected=1; - if( hxcfe_getEnvVarValue( imgldr_ctx->hxcfe, "HFE_WRITER_WRITENOTALLOWED" ) ) - { - FILEHEADER->write_allowed = 0; - } + FILEHEADER->track_encoding=0; + FILEHEADER->formatrevision=0; + FILEHEADER->track_list_offset=1; + FILEHEADER->write_protected=1; + if( hxcfe_getEnvVarValue( imgldr_ctx->hxcfe, "HFE_WRITER_WRITENOTALLOWED" ) ) + { + FILEHEADER->write_allowed = 0; + } - if(floppy->tracks[floppy->floppyNumberOfTrack/2]->sides[0]->track_encoding) - { - FILEHEADER->track_encoding=floppy->tracks[floppy->floppyNumberOfTrack/2]->sides[0]->track_encoding; - } + if(floppy->tracks[floppy->floppyNumberOfTrack/2]->sides[0]->track_encoding) + { + FILEHEADER->track_encoding=floppy->tracks[floppy->floppyNumberOfTrack/2]->sides[0]->track_encoding; + } - if(floppy->floppyNumberOfTrack) + if(floppy->floppyNumberOfTrack) + { + if( floppy->tracks[0]->number_of_side ) { - if( floppy->tracks[0]->number_of_side ) + if(floppy->tracks[0]->sides[0]->track_encoding != FILEHEADER->track_encoding) { - if(floppy->tracks[0]->sides[0]->track_encoding != FILEHEADER->track_encoding) - { - FILEHEADER->track0s0_altencoding = 0x00; - FILEHEADER->track0s0_encoding = floppy->tracks[0]->sides[0]->track_encoding; - } + FILEHEADER->track0s0_altencoding = 0x00; + FILEHEADER->track0s0_encoding = floppy->tracks[0]->sides[0]->track_encoding; } + } - if(floppy->tracks[0]->number_of_side == 2 ) + if(floppy->tracks[0]->number_of_side == 2 ) + { + if(floppy->tracks[0]->sides[1]->track_encoding != FILEHEADER->track_encoding) { - if(floppy->tracks[0]->sides[1]->track_encoding != FILEHEADER->track_encoding) - { - FILEHEADER->track0s1_altencoding = 0x00; - FILEHEADER->track0s1_encoding = floppy->tracks[0]->sides[1]->track_encoding; - } + FILEHEADER->track0s1_altencoding = 0x00; + FILEHEADER->track0s1_encoding = floppy->tracks[0]->sides[1]->track_encoding; } - } - if(floppy->double_step) - FILEHEADER->single_step=0x00; - else - FILEHEADER->single_step=0xFF; - - rfwrite(FILEHEADER,512,1,hxcpicfile,&rf); + } - tracklistlen=((((((FILEHEADER->number_of_track)+1)*sizeof(pictrack))/512)+1)); - offsettrack=(unsigned char*) malloc(tracklistlen*512); - memset(offsettrack,0xFF,tracklistlen*512); + if(floppy->double_step) + FILEHEADER->single_step=0x00; + else + FILEHEADER->single_step=0xFF; - i=0; - trackpos=FILEHEADER->track_list_offset+tracklistlen; + hxc_ram_fwrite(FILEHEADER,512,1,ram_outfile,&rf); - while(i<(FILEHEADER->number_of_track)) - { - mfmsize=0; - mfmsize2=0; - - side2 = 0; - if (floppy->tracks[0]->number_of_side == 2) - side2 = 1; - - final_buffer_len = GenOpcodesTrack(imgldr_ctx->hxcfe, - floppy->tracks[i]->sides[0]->indexbuffer, - floppy->tracks[i]->sides[0]->databuffer, - floppy->tracks[i]->sides[0]->tracklen, - floppy->tracks[i]->sides[side2]->databuffer, - floppy->tracks[i]->sides[side2]->tracklen, - floppy->tracks[i]->sides[0]->flakybitsbuffer, - floppy->tracks[i]->sides[side2]->flakybitsbuffer, - floppy->tracks[i]->sides[0]->bitrate, - floppy->tracks[i]->sides[0]->timingbuffer, - floppy->tracks[i]->sides[side2]->bitrate, - floppy->tracks[i]->sides[side2]->timingbuffer, - &final_buffer_H0, - &final_buffer_H1, - &final_randombuffer); - - precalcopcodestracks[i].opcodetrack_H0 = (unsigned char *)malloc(final_buffer_len); - precalcopcodestracks[i].opcodetrack_H1 = (unsigned char *)malloc(final_buffer_len); - precalcopcodestracks[i].randomopcodetrack = (unsigned char *)malloc(final_buffer_len); - precalcopcodestracks[i].tracklen = final_buffer_len; - - memcpy(precalcopcodestracks[i].opcodetrack_H0,final_buffer_H0,final_buffer_len); - memcpy(precalcopcodestracks[i].opcodetrack_H1,final_buffer_H1,final_buffer_len); - memcpy(precalcopcodestracks[i].randomopcodetrack,final_randombuffer,final_buffer_len); - - imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"HFEv3 Track %d Size: %d bytes",i,final_buffer_len); - - if(final_randombuffer) - free(final_randombuffer); - if(final_buffer_H0) - free(final_buffer_H0); - if(final_buffer_H1) - free(final_buffer_H1); - - /* - mfmsize=floppy->tracks[i]->sides[0]->tracklen; - 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; - if(mfmsize2&7) - mfmsize2=(mfmsize2/8)+1; - else - mfmsize2=mfmsize2/8; - } + tracklistlen=((((((FILEHEADER->number_of_track)+1)*sizeof(pictrack))/512)+1)); - if(mfmsize2>mfmsize) mfmsize=mfmsize2; -*/ - mfmsize = final_buffer_len * 2; + offsettrack = (unsigned char*) malloc(tracklistlen*512); + if( !offsettrack ) + goto error; - if(mfmsize>0xFFFF) - { - imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Argg!! track %d too long (%x) and shorten to 0xFFFF !",i,mfmsize*2); - mfmsize=0xFFFF; - } + memset(offsettrack,0xFF,tracklistlen*512); - tracklenarray[i] = mfmsize / 2; + i=0; + trackpos=FILEHEADER->track_list_offset+tracklistlen; - track=(pictrack *)(offsettrack+(i*sizeof(pictrack))); - track->track_len = mfmsize; - track->offset = trackpos; + while(i<(FILEHEADER->number_of_track)) + { + mfmsize=0; + mfmsize2=0; + + side2 = 0; + if (floppy->tracks[0]->number_of_side == 2) + side2 = 1; + + final_buffer_len = GenOpcodesTrack(imgldr_ctx->hxcfe, + floppy->tracks[i]->sides[0]->indexbuffer, + floppy->tracks[i]->sides[0]->databuffer, + floppy->tracks[i]->sides[0]->tracklen, + floppy->tracks[i]->sides[side2]->databuffer, + floppy->tracks[i]->sides[side2]->tracklen, + floppy->tracks[i]->sides[0]->flakybitsbuffer, + floppy->tracks[i]->sides[side2]->flakybitsbuffer, + floppy->tracks[i]->sides[0]->bitrate, + floppy->tracks[i]->sides[0]->timingbuffer, + floppy->tracks[i]->sides[side2]->bitrate, + floppy->tracks[i]->sides[side2]->timingbuffer, + &final_buffer_H0, + &final_buffer_H1, + &final_randombuffer); + + precalcopcodestracks[i].opcodetrack_H0 = (unsigned char *)malloc(final_buffer_len); + precalcopcodestracks[i].opcodetrack_H1 = (unsigned char *)malloc(final_buffer_len); + precalcopcodestracks[i].randomopcodetrack = (unsigned char *)malloc(final_buffer_len); + precalcopcodestracks[i].tracklen = final_buffer_len; + + memcpy(precalcopcodestracks[i].opcodetrack_H0,final_buffer_H0,final_buffer_len); + memcpy(precalcopcodestracks[i].opcodetrack_H1,final_buffer_H1,final_buffer_len); + memcpy(precalcopcodestracks[i].randomopcodetrack,final_randombuffer,final_buffer_len); + + imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"HFEv3 Track %d Size: %d bytes",i,final_buffer_len); + + if(final_randombuffer) + free(final_randombuffer); + + if(final_buffer_H0) + free(final_buffer_H0); + + if(final_buffer_H1) + free(final_buffer_H1); + + /* + mfmsize=floppy->tracks[i]->sides[0]->tracklen; + if(mfmsize&7) + mfmsize=(mfmsize/8)+1; + else + mfmsize=mfmsize/8; - if( ( mfmsize ) % 512 ) - trackpos=trackpos+(((mfmsize)/512)+1); - else - trackpos=trackpos+((mfmsize)/512); - i++; - }; + if(floppy->tracks[i]->number_of_side==2) + { + mfmsize2=floppy->tracks[i]->sides[1]->tracklen; + if(mfmsize2&7) + mfmsize2=(mfmsize2/8)+1; + else + mfmsize2=mfmsize2/8; + } - rfwrite(offsettrack,512*tracklistlen,1,hxcpicfile,&rf); + if(mfmsize2>mfmsize) mfmsize=mfmsize2; +*/ + mfmsize = final_buffer_len * 2; - i=0; - while(i<(FILEHEADER->number_of_track)) + if(mfmsize>0xFFFF) { - hxcfe_imgCallProgressCallback(imgldr_ctx,i,(FILEHEADER->number_of_track) ); + imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Argg!! track %d too long (%x) and shorten to 0xFFFF !",i,mfmsize*2); + mfmsize=0xFFFF; + } - track=(pictrack *)(offsettrack+(i*sizeof(pictrack))); + tracklenarray[i] = mfmsize / 2; - 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; + track=(pictrack *)(offsettrack+(i*sizeof(pictrack))); + track->track_len = mfmsize; + track->offset = trackpos; - mfmtracks0=(unsigned char*) malloc(tracksize); - mfmtracks1=(unsigned char*) malloc(tracksize); - mfmtrackfinal=(unsigned char*) malloc(tracksize*2); + if( ( mfmsize ) % 512 ) + trackpos=trackpos+(((mfmsize)/512)+1); + else + trackpos=trackpos+((mfmsize)/512); - if( mfmtracks0 && mfmtracks1 && mfmtrackfinal) - { - memset(mfmtracks0,0x00,tracksize); - memset(mfmtracks1,0x00,tracksize); - memset(mfmtrackfinal,0x55,tracksize*2); + i++; + }; - memcpy(mfmtracks0,precalcopcodestracks[i].opcodetrack_H0,tracklenarray[i]); - addpad(mfmtracks0,mfmsize,tracksize); + hxc_ram_fwrite(offsettrack,512*tracklistlen,1,ram_outfile,&rf); - if(floppy->tracks[i]->number_of_side==2) - { - memcpy(mfmtracks1,precalcopcodestracks[i].opcodetrack_H1,tracklenarray[i]); - addpad(mfmtracks1,mfmsize2,tracksize); - } - else - { - memcpy(mfmtracks1,mfmtracks0,tracksize); - } + i=0; + while(i<(FILEHEADER->number_of_track)) + { + hxcfe_imgCallProgressCallback(imgldr_ctx,i,(FILEHEADER->number_of_track) ); - for(k=0;ktrack_len%512) + tracksize=((track->track_len&(~0x1FF))+0x200)/2;//(((track->track_len/512)+1)*512)/2; + else + tracksize=track->track_len/2; - // head 0 - mfmtrackfinal[(k*512)+j]= LUT_ByteBitsInverter[mfmtracks0[(k*256)+j]]; + mfmtracks0=(unsigned char*) malloc(tracksize); + mfmtracks1=(unsigned char*) malloc(tracksize); + mfmtrackfinal=(unsigned char*) malloc(tracksize*2); - // head 1 - mfmtrackfinal[(k*512)+j+256]= LUT_ByteBitsInverter[mfmtracks1[(k*256)+j]]; - } - } + if( mfmtracks0 && mfmtracks1 && mfmtrackfinal) + { + memset(mfmtracks0,0x00,tracksize); + memset(mfmtracks1,0x00,tracksize); + memset(mfmtrackfinal,0x55,tracksize*2); - rfwrite(mfmtrackfinal,tracksize*2,1,hxcpicfile,&rf); + memcpy(mfmtracks0,precalcopcodestracks[i].opcodetrack_H0,tracklenarray[i]); + addpad(mfmtracks0,mfmsize,tracksize); - free(mfmtracks0); - free(mfmtracks1); - free(mfmtrackfinal); + if(floppy->tracks[i]->number_of_side==2) + { + memcpy(mfmtracks1,precalcopcodestracks[i].opcodetrack_H1,tracklenarray[i]); + addpad(mfmtracks1,mfmsize2,tracksize); } else { - if( mfmtracks0 ) - free(mfmtracks0); + memcpy(mfmtracks1,mfmtracks0,tracksize); + } + + for(k=0;khxcfe->hxc_printf(MSG_ERROR,"Cannot create %s!",filename); - return -1; + goto error; } - rfclose(hxcpicfile,&rf); + i++; + }; - imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks written to the file",FILEHEADER->number_of_track); + free(offsettrack); + offsettrack = NULL; - free(FILEHEADER); + if( !rf.ramfile ) + goto error; - return 0; - } - else + outfile = hxc_fopen(filename,"wb"); + if(!outfile) { - imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot create %s!",filename); + hxc_ram_fclose(outfile,&rf); + imgldr_ctx->hxcfe->hxc_printf(MSG_ERROR,"Cannot create %s !",filename); + free(FILEHEADER); - return -1; + return HXCFE_ACCESSERROR; } -} + fwrite(rf.ramfile,rf.ramfile_size,1,outfile); + hxc_fclose(outfile); + + hxc_ram_fclose(ram_outfile,&rf); + + imgldr_ctx->hxcfe->hxc_printf(MSG_INFO_1,"%d tracks written to the file",FILEHEADER->number_of_track); + + free(FILEHEADER); + + return HXCFE_NOERROR; + +error: + hxc_ram_fclose(outfile,&rf); + + if( outfile ) + hxc_fclose( outfile ); + + if( FILEHEADER ) + free( FILEHEADER ); + + if( offsettrack ) + free( offsettrack ); + + if( mfmtracks0 ) + free(mfmtracks0); + + if( mfmtracks1 ) + free(mfmtracks1); + + if( mfmtrackfinal ) + free(mfmtrackfinal); + + return HXCFE_INTERNALERROR; +} \ No newline at end of file