Blame 0001-Added-B-switch-to-specify-DIR-block-size.-Also-made-.patch

434e7f9
From c84df7b7e4528911bf286ce5e40ffe72afacd013 Mon Sep 17 00:00:00 2001
434e7f9
From: Claus <claus@protovision.games>
434e7f9
Date: Mon, 14 Jan 2019 18:49:00 +0100
434e7f9
Subject: [PATCH 01/30] Added -B switch to specify DIR block size. Also made
434e7f9
 the actual block size the default for loop files (was always 0 before).
434e7f9
434e7f9
---
434e7f9
 cc1541.c                  | 43 +++++++++++++++++++++++---------
434e7f9
 test_cc1541/test_cc1541.c | 52 +++++++++++++++++++++++++++++++++++++++
434e7f9
 2 files changed, 84 insertions(+), 11 deletions(-)
434e7f9
434e7f9
diff --git a/cc1541.c b/cc1541.c
434e7f9
index 8bf507f..1e45275 100644
434e7f9
--- a/cc1541.c
434e7f9
+++ b/cc1541.c
434e7f9
@@ -71,6 +71,7 @@ typedef struct
434e7f9
     int track;
434e7f9
     int sector;
434e7f9
     int nrSectors;
434e7f9
+    int nrSectorsShown;
434e7f9
     int mode;
434e7f9
 } imagefile;
434e7f9
 
434e7f9
@@ -97,7 +98,7 @@ void
434e7f9
 usage()
434e7f9
 {
434e7f9
     printf("\n*** This is cc1541 version " VERSION " built on " __DATE__ " ***\n\n");
434e7f9
-    printf("Usage: cc1541 -niFSsfeErbcwlxtdu45q image.[d64|g64|d71]\n\n");
434e7f9
+    printf("Usage: cc1541 -niFSsfeErbcwlBxtdu45q image.[d64|g64|d71]\n\n");
434e7f9
     printf("-n diskname   Disk name, default='DEFAULT'.\n");
434e7f9
     printf("-i id         Disk ID, default='LODIS'.\n");
434e7f9
     printf("-F            Next file first sector on a new track (default=3).\n");
434e7f9
@@ -124,6 +125,8 @@ usage()
434e7f9
     printf("              local name is used. After file written, the filename is unset.\n");
434e7f9
     printf("-l filename   Write loop file (an additional dir entry) to existing file to\n");
434e7f9
     printf("              disk, set filename with -f.\n");
434e7f9
+    printf("-B numblocks  Write the given value as file size in blocks to the directory for\n");
434e7f9
+    printf("              the next file.\n");    
434e7f9
     printf("-x            Don't split files over dirtrack hole (default split files).\n");
434e7f9
     printf("-t            Use dirtrack to also store files (makes -x useless) (default no).\n");
434e7f9
     printf("-d track      Maintain a shadow directory (copy of the actual directory).\n");
434e7f9
@@ -558,7 +561,7 @@ wipe_file(image_type type, unsigned char* image, unsigned int track, unsigned in
434e7f9
 }
434e7f9
 
434e7f9
 static int
434e7f9
-find_file(image_type type, unsigned char* image, char* filename, unsigned char *track, unsigned char *sector)
434e7f9
+find_file(image_type type, unsigned char* image, char* filename, unsigned char *track, unsigned char *sector, unsigned int *numblocks)
434e7f9
 {
434e7f9
     int direntryindex = 0;
434e7f9
 
434e7f9
@@ -575,7 +578,7 @@ find_file(image_type type, unsigned char* image, char* filename, unsigned char *
434e7f9
                     if (dirstrcmp((char *) image + dirblock + entryOffset + FILENAMEOFFSET, filename) == 0) {
434e7f9
                         *track = image[dirblock + entryOffset + FILETRACKOFFSET];
434e7f9
                         *sector = image[dirblock + entryOffset + FILESECTOROFFSET];
434e7f9
-
434e7f9
+						*numblocks = image[dirblock + entryOffset + FILEBLOCKSLOOFFSET] + 256*image[dirblock + entryOffset + FILEBLOCKSHIOFFSET];
434e7f9
                         return direntryindex;
434e7f9
                     }
434e7f9
                     break;
434e7f9
@@ -852,7 +855,7 @@ write_files(image_type type, unsigned char* image, imagefile* files, int num_fil
434e7f9
 
434e7f9
         if (file->mode & MODE_LOOPFILE) {
434e7f9
             /* find loopfile source files */
434e7f9
-            int direntryindex = find_file(type, image, file->localname, &track, &sector);
434e7f9
+            int direntryindex = find_file(type, image, file->localname, &track, &sector, &file->nrSectors);
434e7f9
             if (direntryindex >= 0) {
434e7f9
                 file->track = track;
434e7f9
                 file->sector = sector;
434e7f9
@@ -862,16 +865,19 @@ write_files(image_type type, unsigned char* image, imagefile* files, int num_fil
434e7f9
                 image[entryOffset + FILETRACKOFFSET] = file->track;
434e7f9
                 image[entryOffset + FILESECTOROFFSET] = file->sector;
434e7f9
 
434e7f9
-                image[entryOffset + FILEBLOCKSLOOFFSET] = 0;
434e7f9
-                image[entryOffset + FILEBLOCKSHIOFFSET] = 0;
434e7f9
+				if (file->nrSectorsShown == -1) {
434e7f9
+					file->nrSectorsShown = file->nrSectors;
434e7f9
+				}
434e7f9
+                image[entryOffset + FILEBLOCKSLOOFFSET] = file->nrSectorsShown % 256;
434e7f9
+                image[entryOffset + FILEBLOCKSHIOFFSET] = file->nrSectorsShown / 256;
434e7f9
 
434e7f9
                 if (shadowdirtrack > 0) {
434e7f9
                     entryOffset = linear_sector(type, shadowdirtrack, file->direntrysector) * BLOCKSIZE + file->direntryoffset;
434e7f9
                     image[entryOffset + FILETRACKOFFSET] = file->track;
434e7f9
                     image[entryOffset + FILESECTOROFFSET] = file->sector;
434e7f9
 
434e7f9
-                    image[entryOffset + FILEBLOCKSLOOFFSET] = 0;
434e7f9
-                    image[entryOffset + FILEBLOCKSHIOFFSET] = 0;
434e7f9
+                    image[entryOffset + FILEBLOCKSLOOFFSET] = file->nrSectors;
434e7f9
+                    image[entryOffset + FILEBLOCKSHIOFFSET] = file->nrSectors;
434e7f9
                 }
434e7f9
 
434e7f9
                 continue;
434e7f9
@@ -1155,8 +1161,11 @@ write_files(image_type type, unsigned char* image, imagefile* files, int num_fil
434e7f9
         image[entryOffset + FILETRACKOFFSET] = file->track;
434e7f9
         image[entryOffset + FILESECTOROFFSET] = file->sector;
434e7f9
 
434e7f9
-        image[entryOffset + FILEBLOCKSLOOFFSET] = file->nrSectors & 255;
434e7f9
-        image[entryOffset + FILEBLOCKSHIOFFSET] = file->nrSectors >> 8;
434e7f9
+		if (file->nrSectorsShown < 0) {
434e7f9
+			file->nrSectorsShown = file->nrSectors;
434e7f9
+		}
434e7f9
+        image[entryOffset + FILEBLOCKSLOOFFSET] = file->nrSectorsShown & 255;
434e7f9
+        image[entryOffset + FILEBLOCKSHIOFFSET] = file->nrSectorsShown >> 8;
434e7f9
 
434e7f9
         if (shadowdirtrack > 0) {
434e7f9
             entryOffset = linear_sector(type, shadowdirtrack, file->direntrysector) * BLOCKSIZE + file->direntryoffset;
434e7f9
@@ -1372,6 +1381,7 @@ main(int argc, char* argv[])
434e7f9
     int sectorInterleave = 0;
434e7f9
     int dir_sector_interleave = 3;
434e7f9
     int numdirblocks = 2;
434e7f9
+    int nrSectorsShown = -1;
434e7f9
     char* filename = NULL;
434e7f9
     int set_header = 0;
434e7f9
 
434e7f9
@@ -1468,6 +1478,7 @@ main(int argc, char* argv[])
434e7f9
                 files[nrFiles].sectorInterleave = sectorInterleave ? sectorInterleave : defaultSectorInterleave;
434e7f9
                 files[nrFiles].first_sector_new_track = first_sector_new_track;
434e7f9
                 files[nrFiles].nrSectors = 0;
434e7f9
+                files[nrFiles].nrSectorsShown = nrSectorsShown;                
434e7f9
                 nrFiles++;
434e7f9
             } else {
434e7f9
                 fprintf(stderr, "File '%s' (%d) not found\n", argv[j + 1], nrFiles + 1);
434e7f9
@@ -1477,6 +1488,7 @@ main(int argc, char* argv[])
434e7f9
             filename = NULL;
434e7f9
             first_sector_new_track = default_first_sector_new_track;
434e7f9
             sectorInterleave = 0;
434e7f9
+            nrSectorsShown = -1;
434e7f9
             j++;
434e7f9
         } else if (strcmp(argv[j], "-x") == 0) {
434e7f9
             dirtracksplit = 0;
434e7f9
@@ -1492,7 +1504,16 @@ main(int argc, char* argv[])
434e7f9
                 printf("Error parsing argument for -u\n");
434e7f9
                 return -1;
434e7f9
             }
434e7f9
-        } else if (strcmp(argv[j], "-4") == 0) {
434e7f9
+		} else if (strcmp(argv[j], "-B") == 0) {
434e7f9
+			if ((argc < j + 2) || !sscanf(argv[++j], "%d", &nrSectorsShown)) {
434e7f9
+				printf("Error parsing argument for -B\n");
434e7f9
+				return -1;
434e7f9
+			}
434e7f9
+			if (nrSectorsShown < 0 || nrSectorsShown > 65535) {
434e7f9
+				printf("Argument must be between 0 and 65535 for -B\n");
434e7f9
+				return -1;
434e7f9
+			}
434e7f9
+		} else if (strcmp(argv[j], "-4") == 0) {
434e7f9
             type = IMAGE_D64_EXTENDED_SPEED_DOS;
434e7f9
         } else if (strcmp(argv[j], "-5") == 0) {
434e7f9
             type = IMAGE_D64_EXTENDED_DOLPHIN_DOS;
434e7f9
diff --git a/test_cc1541/test_cc1541.c b/test_cc1541/test_cc1541.c
434e7f9
index c6b6a93..f541add 100644
434e7f9
--- a/test_cc1541/test_cc1541.c
434e7f9
+++ b/test_cc1541/test_cc1541.c
434e7f9
@@ -557,6 +557,58 @@ main(int argc, char* argv[]) {
434e7f9
     remove("1.prg");
434e7f9
     remove("2.prg");
434e7f9
 
434e7f9
+	description = "File should have DIR block size 0 for -B";
434e7f9
+	test++;
434e7f9
+	create_value_file("1.prg", 3 * 254, 1);
434e7f9
+	if (run_binary_cleanup(binary, "-B 0 -w 1.prg", "image.d64", &image, &size) != NO_ERROR) {
434e7f9
+		printf("UNRESOLVED: %s\n", description);
434e7f9
+	}
434e7f9
+	else if (image[track_offset[17] + 256 + 30] == 0 && image[track_offset[17] + 256 + 31] == 0) {
434e7f9
+		passed++;
434e7f9
+	}
434e7f9
+	else {
434e7f9
+		printf("FAIL: %s\n", description);
434e7f9
+	}
434e7f9
+	remove("1.prg");
434e7f9
+
434e7f9
+	description = "File should have DIR block size 65535 for -B";
434e7f9
+	test++;
434e7f9
+	create_value_file("1.prg", 3 * 254, 1);
434e7f9
+	if (run_binary_cleanup(binary, "-B 65535 -w 1.prg", "image.d64", &image, &size) != NO_ERROR) {
434e7f9
+		printf("UNRESOLVED: %s\n", description);
434e7f9
+	}
434e7f9
+	else if (image[track_offset[17] + 256 + 30] == (char)255 && image[track_offset[17] + 256 + 31] == (char)255) {
434e7f9
+		passed++;
434e7f9
+	}
434e7f9
+	else {
434e7f9
+		printf("FAIL: %s\n", description);
434e7f9
+	}
434e7f9
+	remove("1.prg");
434e7f9
+
434e7f9
+    description = "Loop file should have actual DIR block size per default";
434e7f9
+    test++;
434e7f9
+    create_value_file("1.prg", 258 * 254, 1);
434e7f9
+    if (run_binary_cleanup(binary, "-w 1.prg -f LOOP.PRG -l 1.PRG", "image.d64", &image, &size) != NO_ERROR) {
434e7f9
+        printf("UNRESOLVED: %s\n", description);
434e7f9
+    } else if (image[track_offset[17] + 256 + 32 + 30] == 2 && image[track_offset[17] + 256 + 32 + 31] == 1) {
434e7f9
+        passed++;
434e7f9
+    } else {
434e7f9
+        printf("FAIL: %s\n", description);
434e7f9
+    }
434e7f9
+    remove("1.prg");
434e7f9
+   
434e7f9
+    description = "Loop file should have DIR block size 258 for -B";
434e7f9
+    test++;
434e7f9
+    create_value_file("1.prg", 39 * 254, 1);
434e7f9
+    if (run_binary/*_cleanup*/(binary, "-w 1.prg -f LOOP.PRG -B 258 -l 1.PRG", "image.d64", &image, &size) != NO_ERROR) {
434e7f9
+        printf("UNRESOLVED: %s\n", description);
434e7f9
+    } else if (image[track_offset[17] + 256 + 32 + 30] == 2 && image[track_offset[17] + 256 + 32 + 31] == 1) {
434e7f9
+        passed++;
434e7f9
+    } else {
434e7f9
+        printf("FAIL: %s\n", description);
434e7f9
+    }
434e7f9
+    remove("1.prg");
434e7f9
+
434e7f9
     /* clean up */
434e7f9
     if (image != NULL) {
434e7f9
         free(image);
434e7f9
-- 
434e7f9
2.21.0
434e7f9