shithub: rgbds

Download patch

ref: e8a16c6f532864886f96684c57f4f211d0126607
parent: 2cb50730a1fa85b3376036f2d8b0d13c99b873c3
author: Antonio Niño Díaz <antonio_nd@outlook.com>
date: Sun Apr 22 16:36:36 EDT 2018

Don't generate output file if overlay isn't found

Previously, the output file was opened before trying to open the
overlay. Because of this, rgblink generated an empty output file if the
overlay couldn't be opened.

Now the overlay is opened (and checked) before the output file, so the
output file is only generated if the overlay is found.

Signed-off-by: Antonio Niño Díaz <antonio_nd@outlook.com>

--- a/src/link/output.c
+++ b/src/link/output.c
@@ -110,46 +110,53 @@
 	FILE *f_overlay = NULL;
 
 	/*
-	 * Apply overlay
+	 * Load overlay
 	 */
 
-	f = fopen(tzOutname, "wb");
+	if (tzOverlayname) {
+		f_overlay = fopen(tzOverlayname, "rb");
 
-	if (f != NULL) {
-		if (tzOverlayname) {
-			f_overlay = fopen(tzOverlayname, "rb");
+		if (!f_overlay) {
+			errx(1, "Failed to open overlay file %s\n",
+			     tzOverlayname);
+		}
 
-			if (!f_overlay) {
-				errx(1, "Failed to open overlay file %s\n",
-				     tzOverlayname);
-			}
+		fseek(f_overlay, 0, SEEK_END);
 
-			fseek(f_overlay, 0, SEEK_END);
+		if (ftell(f_overlay) % 0x4000 != 0)
+			errx(1, "Overlay file must be aligned to 0x4000 bytes.");
 
-			if (ftell(f_overlay) % 0x4000 != 0)
-				errx(1, "Overlay file must be aligned to 0x4000 bytes.");
+		MaxOverlayBank = (ftell(f_overlay) / 0x4000) - 1;
 
-			MaxOverlayBank = (ftell(f_overlay) / 0x4000) - 1;
+		if (MaxOverlayBank < 1)
+			errx(1, "Overlay file must be at least 0x8000 bytes.");
 
-			if (MaxOverlayBank < 1)
-				errx(1, "Overlay file must be at least 0x8000 bytes.");
+		if (MaxOverlayBank > MaxBankUsed)
+			MaxBankUsed = MaxOverlayBank;
+	}
 
-			if (MaxOverlayBank > MaxBankUsed)
-				MaxBankUsed = MaxOverlayBank;
-		}
+	/*
+	 * Write ROM.
+	 */
 
+	f = fopen(tzOutname, "wb");
+	if (f != NULL) {
 		writehome(f, f_overlay);
 		for (i = 1; i <= MaxBankUsed; i += 1)
 			writebank(f, f_overlay, i);
 
 		fclose(f);
-
-		if (tzOverlayname)
-			fclose(f_overlay);
 	}
 
 	/*
-	 * Add regular sections
+	 * Close overlay
+	 */
+
+	if (tzOverlayname)
+		fclose(f_overlay);
+
+	/*
+	 * Add regular sections to map and sym files.
 	 */
 
 	for (i = BANK_INDEX_WRAM0; i < BANK_INDEX_MAX; i++) {