shithub: rgbds

Download patch

ref: 523b7538f06934ab0d23f4745741f94ebb8534df
parent: 4f86a1253940f8f866413a2095ae3b21598d6b6f
author: Ben10do <[email protected]>
date: Fri Mar 10 14:24:54 EST 2017

Replace magic numbers with BANK_COUNT_* constants

Previously, some instances of the number of banks for each section remained hardcoded. These have been replaced with BANK_COUNT_* constants.

As a side-effect, this could fix a theoretical bug when using BANK(label) when the label is in a high SRAM bank (≥ 4).

--- a/include/link/assign.h
+++ b/include/link/assign.h
@@ -3,16 +3,6 @@
 
 #include "types.h"
 
-enum eBankDefine {
-	BANK_ROM0 = 0,
-	BANK_ROMX,
-	BANK_WRAM0 = 512,
-	BANK_WRAMX,
-	BANK_VRAM = 520,
-	BANK_HRAM = 522,
-	BANK_SRAM = 523
-};
-
 enum eBankCount {
 	BANK_COUNT_ROM0 = 1,
 	BANK_COUNT_ROMX = 511,
@@ -21,6 +11,16 @@
 	BANK_COUNT_VRAM = 2,
 	BANK_COUNT_HRAM = 1,
 	BANK_COUNT_SRAM = 16
+};
+
+enum eBankDefine {
+	BANK_ROM0  = 0,
+	BANK_ROMX  = BANK_ROM0  + BANK_COUNT_ROM0,
+	BANK_WRAM0 = BANK_ROMX  + BANK_COUNT_ROMX,
+	BANK_WRAMX = BANK_WRAM0 + BANK_COUNT_WRAM0,
+	BANK_VRAM  = BANK_WRAMX + BANK_COUNT_WRAMX,
+	BANK_HRAM  = BANK_VRAM  + BANK_COUNT_VRAM,
+	BANK_SRAM  = BANK_HRAM  + BANK_COUNT_HRAM
 };
 
 #define MAXBANKS	(BANK_COUNT_ROM0 + BANK_COUNT_ROMX + BANK_COUNT_WRAM0 + BANK_COUNT_WRAMX \
--- a/src/link/patch.c
+++ b/src/link/patch.c
@@ -69,11 +69,11 @@
 	}
 
 	if (nBank == BANK_WRAM0) return 0;
-	if (nBank >= BANK_WRAMX && nBank <= (BANK_WRAMX+6))
+	if (nBank >= BANK_WRAMX && nBank < (BANK_WRAMX + BANK_COUNT_WRAMX))
 		return nBank - BANK_WRAMX + 1;
-	if (nBank >= BANK_VRAM && nBank <= (BANK_VRAM+1))
+	if (nBank >= BANK_VRAM && nBank < (BANK_VRAM + BANK_COUNT_VRAM))
 		return nBank - BANK_VRAM;
-	if (nBank >= BANK_SRAM && nBank <= (BANK_SRAM+3))
+	if (nBank >= BANK_SRAM && nBank < (BANK_SRAM + BANK_COUNT_SRAM))
 		return nBank - BANK_SRAM;
 
 	return nBank;