Commodore 64 - .d64 format loader issue

HxC Floppy emulator support for all others computers...
Post Reply
shattered
Posts: 4
Joined: Wed Oct 17, 2018 9:58 pm

Commodore 64 - .d64 format loader issue

Post by shattered » Wed Oct 17, 2018 10:09 pm

I have found a bug in GCR encoder used by d64 loader (bit order is reversed and track number is off by one). I have not tested the fix on a real C64, but HFE images converted with "hxcfe -conv:HXC_HFE" load OK in emulation (MAME). Please review this diff to svn HEAD:

Code: Select all

--- HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c	(revision 1660)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c	(working copy)
@@ -111,10 +111,10 @@
 		nongcrcode=nongcrpart[l];
 
 
-		if(!((nongcrcode>>shift)&0xF))
+		if(!((nongcrcode>>(4-shift))&0xF))
 		{
 
-			gcrcode=gcrencodingtable[(byte>>shift)&0xF];
+			gcrcode=gcrencodingtable[(byte>>(4-shift))&0xF];
 
 			for(j=0;j<5;j++)
 			{
@@ -139,7 +139,7 @@
 		}
 		else
 		{//non gcr - direct copy
-			quartet=(byte>>shift)&0xF;
+			quartet=(byte>>(4-shift))&0xF;
 			for(j=0;j<4;j++)
 			{
 
@@ -253,7 +253,7 @@
 				j++;
 				tempdata[j]=l;    // Sector
 				j++;
-				tempdata[j]=tracknumber;  // Track
+				tempdata[j]=tracknumber + 1;  // Track
 				j++;
 				tempdata[j]=0xA1;  // Format ID byte #2
 				j++;

Jeff
Site Admin
Posts: 6463
Joined: Fri Oct 20, 2006 12:12 am
Location: Paris
Contact:

Re: Commodore 64 - .d64 format loader issue

Post by Jeff » Fri Oct 19, 2018 12:10 am

Thanks, the modifications are committed.
I think that this is the first feedback i got about this code/feature since 2009. :o

shattered
Posts: 4
Joined: Wed Oct 17, 2018 9:58 pm

Re: Commodore 64 - .d64 format loader issue

Post by shattered » Fri Oct 19, 2018 9:28 am

Thanks, there's more to come - I am trying to write .d64 to disks using a Supercard Pro setup, having lots of fun :)

Found an off-by one, this causes inner tracks to be too long:

Code: Select all

Index: HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c
===================================================================
--- HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c (revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c (working copy)
@@ -284,7 +284,7 @@
                                tempdata[j++] = 0x00;  // $00 ("off" bytes)
                                tempdata[j++] = 0x00;  // $00 ("off" bytes)
 
-                               for(k=0;k<9;k++) // sector gap
+                               for(k=0;k<8;k++) // sector gap
                                {
                                        tempdata[j]=0x55;
                                        tempnongcr[j]=0xFF;
+ there's dead code in scp_writer.c -- "if( floppy->tracks[0]->floppyRPM > 350 && floppy->tracks[0]->floppyRPM < 340)"

shattered
Posts: 4
Joined: Wed Oct 17, 2018 9:58 pm

Re: Commodore 64 - .d64 format loader issue

Post by shattered » Fri Nov 02, 2018 8:17 pm

It's working now -- tested on a real C64 and 1451 drive, using "index" writing mode.

SCP software expects C64 images to be 96tpi, so I'm doubling every track (this is interpreted as image with half tracks, which probably helps -- my drive is 96tpi). Setting number of heads is apparently not critical -- SCP software expects C64 images to be single sided anyway -- but length of second gap is critical.

Code: Select all

--- HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_format.h	(revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_format.h	(working copy)
@@ -375,8 +375,8 @@
 	uint8_t  start_track;
 	uint8_t  end_track;
 	uint8_t  flags;
-	uint8_t  RFU_0;
-	uint8_t  RFU_1;
+	uint8_t  bit_cell_width;
+	uint8_t  number_of_heads;
 	uint8_t  RFU_2;
 	uint32_t file_data_checksum;
 }scp_header;
===================================================================
--- HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_writer.c	(revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_writer.c	(working copy)
@@ -287,6 +288,8 @@
 
 			case C64_DD_FLOPPYMODE:
 				scph.disk_type = 0x00;
+				tracknumber *= 2;
+				scph.end_track = tracknumber - 1;
 				break;
 
 			case S950_HD_FLOPPYMODE:
@@ -308,12 +311,26 @@
 
 		if(floppy->tracks[0])
 		{
-			if( floppy->tracks[0]->floppyRPM > 350 && floppy->tracks[0]->floppyRPM < 340)
+			if( floppy->tracks[0]->floppyRPM > 350 /* && floppy->tracks[0]->floppyRPM < 340 */)
 			{
 				scph.flags |= 0x04;
 			}
 		}
 
+		switch (floppy->floppyNumberOfSide)
+		{
+		case 1:
+			scph.number_of_heads = 1;
+			break;
+
+		case 2:
+			scph.number_of_heads = 0;
+			break;
+
+		default:
+			scph.number_of_heads = 0;
+			break;
+		}
 		scph.version = 0x09;
 
 		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"SCP_libWrite_DiskFile : Flags=0x%.2X Disktype=0x%.2X NumberOfRevolution=%d Version=%d NbTrack=%d NbSide:%d",scph.flags,scph.disk_type,scph.number_of_revolution,scph.version,floppy->floppyNumberOfTrack,floppy->floppyNumberOfSide);
@@ -344,7 +361,10 @@
 			}
 			else
 			{
-				write_scp_track(f,floppy->tracks[i]->sides[0],&track_checksum,i,scph.number_of_revolution);
+				if (floppy->floppyiftype == C64_DD_FLOPPYMODE)
+					write_scp_track(f,floppy->tracks[i>>1]->sides[0],&track_checksum,i,scph.number_of_revolution);
+				else
+					write_scp_track(f,floppy->tracks[i]->sides[0],&track_checksum,i,scph.number_of_revolution);
 			}
 
 			file_checksum = file_checksum + track_checksum;
Index: HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c
===================================================================
--- HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c	(revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c	(working copy)
@@ -284,7 +284,7 @@
 				tempdata[j++] = 0x00;  // $00 ("off" bytes)
 				tempdata[j++] = 0x00;  // $00 ("off" bytes)
 
-				for(k=0;k<9;k++) // sector gap
+				for(k=0;k<8;k++) // sector gap
 				{
 					tempdata[j]=0x55;
 					tempnongcr[j]=0xFF;

Jeff
Site Admin
Posts: 6463
Joined: Fri Oct 20, 2006 12:12 am
Location: Paris
Contact:

Re: Commodore 64 - .d64 format loader issue

Post by Jeff » Sat Nov 03, 2018 9:40 pm

shattered wrote:
Fri Nov 02, 2018 8:17 pm
It's working now -- tested on a real C64 and 1451 drive, using "index" writing mode.

SCP software expects C64 images to be 96tpi, so I'm doubling every track (this is interpreted as image with half tracks, which probably helps -- my drive is 96tpi). Setting number of heads is apparently not critical -- SCP software expects C64 images to be single sided anyway -- but length of second gap is critical.

Code: Select all

--- HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_format.h	(revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_format.h	(working copy)
@@ -375,8 +375,8 @@
 	uint8_t  start_track;
 	uint8_t  end_track;
 	uint8_t  flags;
-	uint8_t  RFU_0;
-	uint8_t  RFU_1;
+	uint8_t  bit_cell_width;
+	uint8_t  number_of_heads;
 	uint8_t  RFU_2;
 	uint32_t file_data_checksum;
 }scp_header;
===================================================================
--- HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_writer.c	(revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/loaders/scp_loader/scp_writer.c	(working copy)
@@ -287,6 +288,8 @@
 
 			case C64_DD_FLOPPYMODE:
 				scph.disk_type = 0x00;
+				tracknumber *= 2;
+				scph.end_track = tracknumber - 1;
 				break;
 
 			case S950_HD_FLOPPYMODE:
@@ -308,12 +311,26 @@
 
 		if(floppy->tracks[0])
 		{
-			if( floppy->tracks[0]->floppyRPM > 350 && floppy->tracks[0]->floppyRPM < 340)
+			if( floppy->tracks[0]->floppyRPM > 350 /* && floppy->tracks[0]->floppyRPM < 340 */)
 			{
 				scph.flags |= 0x04;
 			}
 		}
 
+		switch (floppy->floppyNumberOfSide)
+		{
+		case 1:
+			scph.number_of_heads = 1;
+			break;
+
+		case 2:
+			scph.number_of_heads = 0;
+			break;
+
+		default:
+			scph.number_of_heads = 0;
+			break;
+		}
 		scph.version = 0x09;
 
 		imgldr_ctx->hxcfe->hxc_printf(MSG_DEBUG,"SCP_libWrite_DiskFile : Flags=0x%.2X Disktype=0x%.2X NumberOfRevolution=%d Version=%d NbTrack=%d NbSide:%d",scph.flags,scph.disk_type,scph.number_of_revolution,scph.version,floppy->floppyNumberOfTrack,floppy->floppyNumberOfSide);
@@ -344,7 +361,10 @@
 			}
 			else
 			{
-				write_scp_track(f,floppy->tracks[i]->sides[0],&track_checksum,i,scph.number_of_revolution);
+				if (floppy->floppyiftype == C64_DD_FLOPPYMODE)
+					write_scp_track(f,floppy->tracks[i>>1]->sides[0],&track_checksum,i,scph.number_of_revolution);
+				else
+					write_scp_track(f,floppy->tracks[i]->sides[0],&track_checksum,i,scph.number_of_revolution);
 			}
 
 			file_checksum = file_checksum + track_checksum;
Index: HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c
===================================================================
--- HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c	(revision 1670)
+++ HxCFloppyEmulator/libhxcfe/trunk/sources/tracks/gcr_track.c	(working copy)
@@ -284,7 +284,7 @@
 				tempdata[j++] = 0x00;  // $00 ("off" bytes)
 				tempdata[j++] = 0x00;  // $00 ("off" bytes)
 
-				for(k=0;k<9;k++) // sector gap
+				for(k=0;k<8;k++) // sector gap
 				{
 					tempdata[j]=0x55;
 					tempnongcr[j]=0xFF;
Thanks a lot ! Will push the modifications to the repository.

Post Reply