shithub: rgbds

Download patch

ref: 7f37eef2186d36dcb68f263054e27f410bb2c37b
parent: 8521e45edcda6645a3ab047dc7a0372d19a27ba2
author: Antonio Niño Díaz <[email protected]>
date: Sun Jan 14 20:11:21 EST 2018

Cleanup BANK related definitions

Simplify comparisons and remove magic numbers.

Signed-off-by: Antonio Niño Díaz <[email protected]>

--- a/include/common.h
+++ b/include/common.h
@@ -3,4 +3,34 @@
 
 #define RGBDS_OBJECT_VERSION_STRING "RGB6"
 
+enum eBankCount {
+	BANK_COUNT_ROM0  = 1,
+	BANK_COUNT_ROMX  = 511,
+	BANK_COUNT_WRAM0 = 1,
+	BANK_COUNT_WRAMX = 7,
+	BANK_COUNT_VRAM  = 2,
+	BANK_COUNT_OAM   = 1,
+	BANK_COUNT_HRAM  = 1,
+	BANK_COUNT_SRAM  = 16
+};
+
+enum eBankGBCount {
+	BANK_MIN_ROM0  = 0,
+	BANK_MAX_ROM0  = BANK_COUNT_ROM0  + BANK_MIN_ROM0  - 1,
+	BANK_MIN_ROMX  = 1,
+	BANK_MAX_ROMX  = BANK_COUNT_ROMX  + BANK_MIN_ROMX  - 1,
+	BANK_MIN_WRAM0 = 0,
+	BANK_MAX_WRAM0 = BANK_COUNT_WRAM0 + BANK_MIN_WRAM0 - 1,
+	BANK_MIN_WRAMX = 1,
+	BANK_MAX_WRAMX = BANK_COUNT_WRAMX + BANK_MIN_WRAMX - 1,
+	BANK_MIN_VRAM  = 0,
+	BANK_MAX_VRAM  = BANK_COUNT_VRAM  + BANK_MIN_VRAM  - 1,
+	BANK_MIN_OAM   = 0,
+	BANK_MAX_OAM   = BANK_COUNT_OAM   + BANK_MIN_OAM   - 1,
+	BANK_MIN_HRAM  = 0,
+	BANK_MAX_HRAM  = BANK_COUNT_HRAM  + BANK_MIN_HRAM  - 1,
+	BANK_MIN_SRAM  = 0,
+	BANK_MAX_SRAM  = BANK_COUNT_SRAM  + BANK_MIN_SRAM  - 1
+};
+
 #endif /* RGBDS_COMMON_H */
--- a/include/link/assign.h
+++ b/include/link/assign.h
@@ -3,37 +3,24 @@
 
 #include <stdint.h>
 
+#include "common.h"
 #include "mylink.h"
 
-enum eBankCount {
-	BANK_COUNT_ROM0 = 1,
-	BANK_COUNT_ROMX = 511,
-	BANK_COUNT_WRAM0 = 1,
-	BANK_COUNT_WRAMX = 7,
-	BANK_COUNT_VRAM = 2,
-	BANK_COUNT_OAM  = 1,
-	BANK_COUNT_HRAM = 1,
-	BANK_COUNT_SRAM = 16
-};
-
+/* Bank numbers as seen by the linker */
 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_OAM   = BANK_VRAM  + BANK_COUNT_VRAM,
-	BANK_HRAM  = BANK_OAM   + BANK_COUNT_OAM,
-	BANK_SRAM  = BANK_HRAM  + BANK_COUNT_HRAM
+	BANK_INDEX_ROM0  = 0,
+	BANK_INDEX_ROMX  = BANK_INDEX_ROM0  + BANK_COUNT_ROM0,
+	BANK_INDEX_WRAM0 = BANK_INDEX_ROMX  + BANK_COUNT_ROMX,
+	BANK_INDEX_WRAMX = BANK_INDEX_WRAM0 + BANK_COUNT_WRAM0,
+	BANK_INDEX_VRAM  = BANK_INDEX_WRAMX + BANK_COUNT_WRAMX,
+	BANK_INDEX_OAM   = BANK_INDEX_VRAM  + BANK_COUNT_VRAM,
+	BANK_INDEX_HRAM  = BANK_INDEX_OAM   + BANK_COUNT_OAM,
+	BANK_INDEX_SRAM  = BANK_INDEX_HRAM  + BANK_COUNT_HRAM,
+	BANK_INDEX_MAX   = BANK_INDEX_SRAM  + BANK_COUNT_SRAM
 };
 
-#define MAXBANKS	(BANK_COUNT_ROM0 + BANK_COUNT_ROMX	\
-			+ BANK_COUNT_WRAM0 + BANK_COUNT_WRAMX	\
-			+ BANK_COUNT_VRAM + BANK_COUNT_OAM	\
-			+ BANK_COUNT_HRAM + BANK_COUNT_SRAM)
-
 extern int32_t MaxBankUsed;
-extern int32_t MaxAvail[MAXBANKS];
+extern int32_t MaxAvail[BANK_INDEX_MAX];
 
 int32_t area_Avail(int32_t bank);
 void AssignSections(void);
@@ -45,5 +32,14 @@
 					 enum eSectionType type, int32_t bank);
 uint32_t AssignSectionAddressAndBankByName(const char *name, uint32_t address,
 					   int32_t bank);
+
+int BankIndexIsROM0(int32_t bank);
+int BankIndexIsROMX(int32_t bank);
+int BankIndexIsWRAM0(int32_t bank);
+int BankIndexIsWRAMX(int32_t bank);
+int BankIndexIsVRAM(int32_t bank);
+int BankIndexIsOAM(int32_t bank);
+int BankIndexIsHRAM(int32_t bank);
+int BankIndexIsSRAM(int32_t bank);
 
 #endif /* RGBDS_LINK_ASSIGN_H */
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -17,6 +17,7 @@
 #include "asm/rpn.h"
 #include "asm/symbol.h"
 
+#include "common.h"
 #include "linkdefs.h"
 
 char *tzNewMacro;
@@ -31,23 +32,23 @@
 	switch (secttype) {
 	case SECT_ROMX:
 		stype = "ROMX";
-		minbank = 1;
-		maxbank = 0x1ff;
+		minbank = BANK_MIN_ROMX;
+		maxbank = BANK_MAX_ROMX;
 		break;
 	case SECT_SRAM:
 		stype = "SRAM";
-		minbank = 0;
-		maxbank = 15;
+		minbank = BANK_MIN_SRAM;
+		maxbank = BANK_MAX_SRAM;
 		break;
 	case SECT_WRAMX:
 		stype = "WRAMX";
-		minbank = 1;
-		maxbank = 7;
+		minbank = BANK_MIN_WRAMX;
+		maxbank = BANK_MAX_WRAMX;
 		break;
 	case SECT_VRAM:
 		stype = "VRAM";
-		minbank = 0;
-		maxbank = 1;
+		minbank = BANK_MIN_VRAM;
+		maxbank = BANK_MAX_VRAM;
 		break;
 	default:
 		yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections");
--- a/src/link/assign.c
+++ b/src/link/assign.c
@@ -27,8 +27,8 @@
 	int32_t bankCount;
 };
 
-struct sFreeArea *BankFree[MAXBANKS];
-int32_t MaxAvail[MAXBANKS];
+struct sFreeArea *BankFree[BANK_INDEX_MAX];
+int32_t MaxAvail[BANK_INDEX_MAX];
 int32_t MaxBankUsed;
 int32_t MaxWBankUsed;
 int32_t MaxSBankUsed;
@@ -37,14 +37,14 @@
 const enum eSectionType SECT_MIN = SECT_WRAM0;
 const enum eSectionType SECT_MAX = SECT_OAM;
 const struct sSectionAttributes SECT_ATTRIBUTES[] = {
-	{"WRAM0", BANK_WRAM0, 0, 0, BANK_COUNT_WRAM0},
-	{"VRAM",  BANK_VRAM,  0, 0, BANK_COUNT_VRAM},
-	{"ROMX",  BANK_ROMX, -1, 1, BANK_COUNT_ROMX},
-	{"ROM0",  BANK_ROM0,  0, 0, BANK_COUNT_ROM0},
-	{"HRAM",  BANK_HRAM,  0, 0, BANK_COUNT_HRAM},
-	{"WRAMX", BANK_WRAMX, 0, 0, BANK_COUNT_WRAMX},
-	{"SRAM",  BANK_SRAM,  0, 0, BANK_COUNT_SRAM},
-	{"OAM",   BANK_OAM,   0, 0, BANK_COUNT_OAM}
+	{"WRAM0", BANK_INDEX_WRAM0, 0, 0, BANK_COUNT_WRAM0},
+	{"VRAM",  BANK_INDEX_VRAM,  0, 0, BANK_COUNT_VRAM},
+	{"ROMX",  BANK_INDEX_ROMX, -1, 1, BANK_COUNT_ROMX},
+	{"ROM0",  BANK_INDEX_ROM0,  0, 0, BANK_COUNT_ROM0},
+	{"HRAM",  BANK_INDEX_HRAM,  0, 0, BANK_COUNT_HRAM},
+	{"WRAMX", BANK_INDEX_WRAMX, 0, 0, BANK_COUNT_WRAMX},
+	{"SRAM",  BANK_INDEX_SRAM,  0, 0, BANK_COUNT_SRAM},
+	{"OAM",   BANK_INDEX_OAM,   0, 0, BANK_COUNT_OAM}
 };
 
 static void do_max_bank(enum eSectionType Type, int32_t nBank)
@@ -77,6 +77,54 @@
 		errx(1, "(INTERNAL) Invalid section type found.");
 }
 
+int BankIndexIsROM0(int32_t bank)
+{
+	return (bank >= BANK_INDEX_ROM0) &&
+	       (bank < (BANK_INDEX_ROM0 + BANK_COUNT_ROM0));
+}
+
+int BankIndexIsROMX(int32_t bank)
+{
+	return (bank >= BANK_INDEX_ROMX) &&
+	       (bank < (BANK_INDEX_ROMX + BANK_COUNT_ROMX));
+}
+
+int BankIndexIsWRAM0(int32_t bank)
+{
+	return (bank >= BANK_INDEX_WRAM0) &&
+	       (bank < (BANK_INDEX_WRAM0 + BANK_COUNT_WRAM0));
+}
+
+int BankIndexIsWRAMX(int32_t bank)
+{
+	return (bank >= BANK_INDEX_WRAMX) &&
+	       (bank < (BANK_INDEX_WRAMX + BANK_COUNT_WRAMX));
+}
+
+int BankIndexIsVRAM(int32_t bank)
+{
+	return (bank >= BANK_INDEX_VRAM) &&
+	       (bank < (BANK_INDEX_VRAM + BANK_COUNT_VRAM));
+}
+
+int BankIndexIsOAM(int32_t bank)
+{
+	return (bank >= BANK_INDEX_OAM) &&
+	       (bank < (BANK_INDEX_OAM + BANK_COUNT_OAM));
+}
+
+int BankIndexIsHRAM(int32_t bank)
+{
+	return (bank >= BANK_INDEX_HRAM) &&
+	       (bank < (BANK_INDEX_HRAM + BANK_COUNT_HRAM));
+}
+
+int BankIndexIsSRAM(int32_t bank)
+{
+	return (bank >= BANK_INDEX_SRAM) &&
+	       (bank < (BANK_INDEX_SRAM + BANK_COUNT_SRAM));
+}
+
 int32_t area_Avail(int32_t bank)
 {
 	int32_t r;
@@ -447,13 +495,15 @@
 	 * Initialize the memory areas
 	 */
 
-	for (i = 0; i < MAXBANKS; i += 1) {
+	for (i = 0; i < BANK_INDEX_MAX; i += 1) {
 		BankFree[i] = malloc(sizeof(*BankFree[i]));
 
-		if (!BankFree[i])
-			err(1, NULL);
+		if (!BankFree[i]) {
+			errx(1, "%s: Couldn't allocate mem for bank %d",
+			     __func__, i);
+		}
 
-		if (i == BANK_ROM0) {
+		if (BankIndexIsROM0(i)) {
 			/* ROM0 bank */
 			BankFree[i]->nOrg = 0x0000;
 			if (options & OPT_TINY)
@@ -460,11 +510,11 @@
 				BankFree[i]->nSize = 0x8000;
 			else
 				BankFree[i]->nSize = 0x4000;
-		} else if (i >= BANK_ROMX && i < BANK_ROMX + BANK_COUNT_ROMX) {
+		} else if (BankIndexIsROMX(i)) {
 			/* Swappable ROM bank */
 			BankFree[i]->nOrg = 0x4000;
 			BankFree[i]->nSize = 0x4000;
-		} else if (i == BANK_WRAM0) {
+		} else if (BankIndexIsWRAM0(i)) {
 			/* WRAM */
 			BankFree[i]->nOrg = 0xC000;
 			if (options & OPT_CONTWRAM)
@@ -471,30 +521,30 @@
 				BankFree[i]->nSize = 0x2000;
 			else
 				BankFree[i]->nSize = 0x1000;
-		} else if (i >= BANK_SRAM && i < BANK_SRAM + BANK_COUNT_SRAM) {
+		} else if (BankIndexIsSRAM(i)) {
 			/* Swappable SRAM bank */
 			BankFree[i]->nOrg = 0xA000;
 			BankFree[i]->nSize = 0x2000;
-		} else if (i >= BANK_WRAMX && i < BANK_WRAMX + BANK_COUNT_WRAMX) {
+		} else if (BankIndexIsWRAMX(i)) {
 			/* Swappable WRAM bank */
 			BankFree[i]->nOrg = 0xD000;
 			BankFree[i]->nSize = 0x1000;
-		} else if (i >= BANK_VRAM && i < BANK_VRAM + BANK_COUNT_VRAM) {
+		} else if (BankIndexIsVRAM(i)) {
 			/* Swappable VRAM bank */
 			BankFree[i]->nOrg = 0x8000;
-			if (options & OPT_DMG_MODE && i != BANK_VRAM)
+			if (options & OPT_DMG_MODE && i != BANK_INDEX_VRAM)
 				BankFree[i]->nSize = 0;
 			else
 				BankFree[i]->nSize = 0x2000;
-		} else if (i == BANK_OAM) {
+		} else if (BankIndexIsOAM(i)) {
 			BankFree[i]->nOrg = 0xFE00;
 			BankFree[i]->nSize = 0x00A0;
-		} else if (i == BANK_HRAM) {
+		} else if (BankIndexIsHRAM(i)) {
 			/* HRAM */
 			BankFree[i]->nOrg = 0xFF80;
 			BankFree[i]->nSize = 0x007F;
 		} else {
-			errx(1, "(INTERNAL) Unknown bank type!");
+			errx(1, "%s: Unknown bank type %d", __func__, i);
 		}
 
 		MaxAvail[i] = BankFree[i]->nSize;
--- a/src/link/mapfile.c
+++ b/src/link/mapfile.c
@@ -6,9 +6,9 @@
 
 #include "extern/err.h"
 
+#include "link/assign.h"
 #include "link/main.h"
 #include "link/mylink.h"
-#include "link/assign.h"
 
 static int32_t currentbank;
 static int32_t sfbank;
@@ -47,40 +47,48 @@
 
 void MapfileInitBank(int32_t bank)
 {
+	if ((bank < 0) || (bank >= BANK_INDEX_MAX))
+		errx(1, "%s: Unknown bank %d\n", __func__, bank);
+
 	if (mf) {
 		currentbank = bank;
-		if (bank == BANK_ROM0)
+		if (BankIndexIsROM0(bank)) {
 			fprintf(mf, "ROM Bank #0 (HOME):\n");
-		else if (bank < BANK_WRAM0)
-			fprintf(mf, "ROM Bank #%d:\n", bank);
-		else if (bank == BANK_WRAM0)
+		} else if (BankIndexIsROMX(bank)) {
+			fprintf(mf, "ROM Bank #%d:\n",
+				bank - BANK_INDEX_ROMX + 1);
+		} else if (BankIndexIsWRAM0(bank)) {
 			fprintf(mf, "WRAM Bank #0:\n");
-		else if (bank < BANK_VRAM)
-			fprintf(mf, "WRAM Bank #%d:\n", bank - BANK_WRAMX + 1);
-		else if (bank == BANK_HRAM)
-			fprintf(mf, "HRAM:\n");
-		else if (bank == BANK_VRAM || bank == BANK_VRAM + 1)
-			fprintf(mf, "VRAM Bank #%d:\n", bank - BANK_VRAM);
-		else if (bank == BANK_OAM)
+		} else if (BankIndexIsWRAMX(bank)) {
+			fprintf(mf, "WRAM Bank #%d:\n",
+				bank - BANK_INDEX_WRAMX + 1);
+		} else if (BankIndexIsVRAM(bank)) {
+			fprintf(mf, "VRAM Bank #%d:\n", bank - BANK_INDEX_VRAM);
+		} else if (BankIndexIsOAM(bank)) {
 			fprintf(mf, "OAM:\n");
-		else if (bank < MAXBANKS)
-			fprintf(mf, "SRAM Bank #%d:\n", bank - BANK_SRAM);
+		} else if (BankIndexIsHRAM(bank)) {
+			fprintf(mf, "HRAM:\n");
+		} else if (BankIndexIsSRAM(bank)) {
+			fprintf(mf, "SRAM Bank #%d:\n", bank - BANK_INDEX_SRAM);
+		}
 	}
 	if (sf) {
-		if (bank < BANK_WRAM0)
-			sfbank = bank;
-		else if (bank == BANK_WRAM0)
+		if (BankIndexIsROM0(bank))
 			sfbank = 0;
-		else if (bank < BANK_VRAM)
-			sfbank = bank - BANK_WRAMX + 1;
-		else if (bank == BANK_HRAM)
+		else if (BankIndexIsROMX(bank))
+			sfbank = bank - BANK_INDEX_ROMX + 1;
+		else if (BankIndexIsWRAM0(bank))
 			sfbank = 0;
-		else if (bank == BANK_VRAM || bank == BANK_VRAM + 1)
-			sfbank = bank - BANK_VRAM;
-		else if (bank == BANK_OAM)
+		else if (BankIndexIsWRAMX(bank))
+			sfbank = bank - BANK_INDEX_WRAMX + 1;
+		else if (BankIndexIsVRAM(bank))
+			sfbank = bank - BANK_INDEX_VRAM;
+		else if (BankIndexIsOAM(bank))
 			sfbank = 0;
-		else if (bank < MAXBANKS)
-			sfbank = bank - BANK_SRAM;
+		else if (BankIndexIsHRAM(bank))
+			sfbank = 0;
+		else if (BankIndexIsSRAM(bank))
+			sfbank = bank - BANK_INDEX_SRAM;
 		else
 			sfbank = 0;
 	}
--- a/src/link/output.c
+++ b/src/link/output.c
@@ -20,18 +20,18 @@
 	const struct sSection *pSect;
 	uint8_t *mem;
 
-	mem = malloc(MaxAvail[BANK_ROM0]);
+	mem = malloc(MaxAvail[BANK_INDEX_ROM0]);
 	if (!mem)
 		return;
 
 	if (f_overlay != NULL) {
 		fseek(f_overlay, 0L, SEEK_SET);
-		if (fread(mem, 1, MaxAvail[BANK_ROM0], f_overlay) !=
-		    MaxAvail[BANK_ROM0]) {
+		if (fread(mem, 1, MaxAvail[BANK_INDEX_ROM0], f_overlay) !=
+		    MaxAvail[BANK_INDEX_ROM0]) {
 			warnx("Failed to read data from overlay file.");
 		}
 	} else {
-		memset(mem, fillchar, MaxAvail[BANK_ROM0]);
+		memset(mem, fillchar, MaxAvail[BANK_INDEX_ROM0]);
 	}
 	MapfileInitBank(0);
 
@@ -47,7 +47,7 @@
 
 	MapfileCloseBank(area_Avail(0));
 
-	fwrite(mem, 1, MaxAvail[BANK_ROM0], f);
+	fwrite(mem, 1, MaxAvail[BANK_INDEX_ROM0], f);
 	free(mem);
 }
 
@@ -144,7 +144,7 @@
 	 * Add regular sections
 	 */
 
-	for (i = BANK_WRAM0; i < MAXBANKS; i++) {
+	for (i = BANK_INDEX_WRAM0; i < BANK_INDEX_MAX; i++) {
 		const struct sSection *pSect;
 
 		MapfileInitBank(i);
--- a/src/link/patch.c
+++ b/src/link/patch.c
@@ -52,17 +52,17 @@
 
 static int32_t getrealbankfrominternalbank(int32_t n)
 {
-	if ((n == BANK_WRAM0) || (n == BANK_ROM0) || (n == BANK_OAM) ||
-	    (n == BANK_HRAM)) {
+	if (BankIndexIsWRAM0(n) || BankIndexIsROM0(n) ||
+	    BankIndexIsOAM(n)   || BankIndexIsHRAM(n)) {
 		return 0;
-	} else if ((n >= BANK_ROMX) && (n < (BANK_ROMX + BANK_COUNT_ROMX))) {
-		return n - BANK_ROMX + 1;
-	} else if ((n >= BANK_WRAMX) && (n < (BANK_WRAMX + BANK_COUNT_WRAMX))) {
-		return n - BANK_WRAMX + 1;
-	} else if ((n >= BANK_VRAM) && (n < (BANK_VRAM + BANK_COUNT_VRAM))) {
-		return n - BANK_VRAM;
-	} else if ((n >= BANK_SRAM) && (n < (BANK_SRAM + BANK_COUNT_SRAM))) {
-		return n - BANK_SRAM;
+	} else if (BankIndexIsROMX(n)) {
+		return n - BANK_INDEX_ROMX + 1;
+	} else if (BankIndexIsWRAMX(n)) {
+		return n - BANK_INDEX_WRAMX + 1;
+	} else if (BankIndexIsVRAM(n)) {
+		return n - BANK_INDEX_VRAM;
+	} else if (BankIndexIsSRAM(n)) {
+		return n - BANK_INDEX_SRAM;
 	}
 
 	errx(1, "%s: Unknown bank %d", __func__, n);
--- a/src/link/script.c
+++ b/src/link/script.c
@@ -26,7 +26,7 @@
 	uint32_t address; /* current address to write sections to */
 	uint32_t top_address; /* not inclusive */
 	enum eSectionType type;
-} bank[MAXBANKS];
+} bank[BANK_INDEX_MAX];
 
 static int32_t current_bank = -1; /* Bank as seen by the bank array */
 static int32_t current_real_bank = -1; /* bank as seen by the GB */
@@ -35,8 +35,8 @@
 {
 	int32_t i;
 
-	for (i = 0; i < MAXBANKS; i++) {
-		if (i == BANK_ROM0) {
+	for (i = 0; i < BANK_INDEX_MAX; i++) {
+		if (BankIndexIsROM0(i)) {
 			/* ROM0 bank */
 			bank[i].address = 0x0000;
 			if (options & OPT_TINY)
@@ -44,12 +44,12 @@
 			else
 				bank[i].top_address = 0x4000;
 			bank[i].type = SECT_ROM0;
-		} else if (i >= BANK_ROMX && i < BANK_ROMX + BANK_COUNT_ROMX) {
+		} else if (BankIndexIsROMX(i)) {
 			/* Swappable ROM bank */
 			bank[i].address = 0x4000;
 			bank[i].top_address = 0x8000;
 			bank[i].type = SECT_ROMX;
-		} else if (i == BANK_WRAM0) {
+		} else if (BankIndexIsWRAM0(i)) {
 			/* WRAM */
 			bank[i].address = 0xC000;
 			if (options & OPT_CONTWRAM)
@@ -57,38 +57,38 @@
 			else
 				bank[i].top_address = 0xD000;
 			bank[i].type = SECT_WRAM0;
-		} else if (i >= BANK_SRAM && i < BANK_SRAM + BANK_COUNT_SRAM) {
+		} else if (BankIndexIsSRAM(i)) {
 			/* Swappable SRAM bank */
 			bank[i].address = 0xA000;
 			bank[i].top_address = 0xC000;
 			bank[i].type = SECT_SRAM;
-		} else if (i >= BANK_WRAMX && i < BANK_WRAMX + BANK_COUNT_WRAMX) {
+		} else if (BankIndexIsWRAMX(i)) {
 			/* Swappable WRAM bank */
 			bank[i].address = 0xD000;
 			bank[i].top_address = 0xE000;
 			bank[i].type = SECT_WRAMX;
-		} else if (i >= BANK_VRAM && i < BANK_VRAM + BANK_COUNT_VRAM) {
+		} else if (BankIndexIsVRAM(i)) {
 			/* Swappable VRAM bank */
 			bank[i].address = 0x8000;
 			bank[i].type = SECT_VRAM;
-			if (options & OPT_DMG_MODE && i != BANK_VRAM) {
+			if (options & OPT_DMG_MODE && i != BANK_INDEX_VRAM) {
 				/* In DMG the only available bank is bank 0. */
 				bank[i].top_address = 0x8000;
 			} else {
 				bank[i].top_address = 0xA000;
 			}
-		} else if (i == BANK_OAM) {
+		} else if (BankIndexIsOAM(i)) {
 			/* OAM */
 			bank[i].address = 0xFE00;
 			bank[i].top_address = 0xFEA0;
 			bank[i].type = SECT_OAM;
-		} else if (i == BANK_HRAM) {
+		} else if (BankIndexIsHRAM(i)) {
 			/* HRAM */
 			bank[i].address = 0xFF80;
 			bank[i].top_address = 0xFFFF;
 			bank[i].type = SECT_HRAM;
 		} else {
-			errx(1, "(INTERNAL) Unknown bank type!");
+			errx(1, "%s: Unknown bank type %d", __func__, i);
 		}
 	}
 }
@@ -98,7 +98,7 @@
 	if (strcmp(type, "ROM0") == 0) {
 		if (bank != 0)
 			errx(1, "(Internal) Trying to assign a bank number to ROM0.\n");
-		current_bank = BANK_ROM0;
+		current_bank = BANK_INDEX_ROM0;
 		current_real_bank = 0;
 		return;
 	} else if (strcmp(type, "ROMX") == 0) {
@@ -108,7 +108,7 @@
 			errx(1, "ROMX index too big (%d > %d).\n", bank,
 			     BANK_COUNT_ROMX);
 		}
-		current_bank = BANK_ROMX + bank - 1;
+		current_bank = BANK_INDEX_ROMX + bank - 1;
 		current_real_bank = bank;
 		return;
 	} else if (strcmp(type, "VRAM") == 0) {
@@ -116,13 +116,15 @@
 			errx(1, "VRAM index too big (%d >= %d).\n", bank,
 			     BANK_COUNT_VRAM);
 		}
-		current_bank = BANK_VRAM + bank;
+		current_bank = BANK_INDEX_VRAM + bank;
 		current_real_bank = bank;
 		return;
 	} else if (strcmp(type, "WRAM0") == 0) {
-		if (bank != 0)
-			errx(1, "(Internal) Trying to assign a bank number to WRAM0.\n");
-		current_bank = BANK_WRAM0;
+		if (bank != 0) {
+			errx(1, "%s: Trying to assign a bank number to WRAM0.\n",
+			     __func__);
+		}
+		current_bank = BANK_INDEX_WRAM0;
 		current_real_bank = 0;
 		return;
 	} else if (strcmp(type, "WRAMX") == 0) {
@@ -132,7 +134,7 @@
 			errx(1, "WRAMX index too big (%d > %d).\n", bank,
 			     BANK_COUNT_WRAMX);
 		}
-		current_bank = BANK_WRAMX + bank - 1;
+		current_bank = BANK_INDEX_WRAMX + bank - 1;
 		current_real_bank = bank - 1;
 		return;
 	} else if (strcmp(type, "SRAM") == 0) {
@@ -140,24 +142,28 @@
 			errx(1, "SRAM index too big (%d >= %d).\n", bank,
 			     BANK_COUNT_SRAM);
 		}
-		current_bank = BANK_SRAM + bank;
+		current_bank = BANK_INDEX_SRAM + bank;
 		current_real_bank = bank;
 		return;
 	} else if (strcmp(type, "OAM") == 0) {
-		if (bank != 0)
-			errx(1, "(Internal) Trying to assign a bank number to OAM.\n");
-		current_bank = BANK_OAM;
+		if (bank != 0) {
+			errx(1, "%s: Trying to assign a bank number to OAM.\n",
+			     __func__);
+		}
+		current_bank = BANK_INDEX_OAM;
 		current_real_bank = 0;
 		return;
 	} else if (strcmp(type, "HRAM") == 0) {
-		if (bank != 0)
-			errx(1, "(Internal) Trying to assign a bank number to HRAM.\n");
-		current_bank = BANK_HRAM;
+		if (bank != 0) {
+			errx(1, "%s: Trying to assign a bank number to HRAM.\n",
+			     __func__);
+		}
+		current_bank = BANK_INDEX_HRAM;
 		current_real_bank = 0;
 		return;
 	}
 
-	errx(1, "(Internal) Unknown section type \"%s\".\n", type);
+	errx(1, "%s: Unknown section type \"%s\".\n", __func__, type);
 }
 
 void script_SetAddress(uint32_t addr)