shithub: rgbds

Download patch

ref: 5ee058f21718406ff4cd5cd3c0c4811512ed167a
parent: b40c567aeecca9708fca3c6baa91a1848835bb52
parent: 420ea638a73b30f8dc4cf6cc2ace6c1ca3fe3794
author: Anthony J. Bentley <[email protected]>
date: Sun Feb 5 18:24:13 EST 2017

Merge branch 'linker-refactor-assign' of https://github.com/Ben10do/rgbds

--- a/include/link/assign.h
+++ b/include/link/assign.h
@@ -5,6 +5,7 @@
 
 enum eBankDefine {
 	BANK_ROM0 = 0,
+	BANK_ROMX,
 	BANK_WRAM0 = 512,
 	BANK_WRAMX,
 	BANK_VRAM = 520,
@@ -11,7 +12,19 @@
 	BANK_HRAM = 522,
 	BANK_SRAM = 523
 };
-#define MAXBANKS	527
+
+enum eBankCount {
+	BANK_COUNT_ROM0 = 1,
+	BANK_COUNT_ROMX = 511,
+	BANK_COUNT_WRAM0 = 1,
+	BANK_COUNT_WRAMX = 7,
+	BANK_COUNT_VRAM = 2,
+	BANK_COUNT_HRAM = 1,
+	BANK_COUNT_SRAM = 4
+};
+
+#define MAXBANKS	(BANK_COUNT_ROM0 + BANK_COUNT_ROMX + BANK_COUNT_WRAM0 + BANK_COUNT_WRAMX \
+					+ BANK_COUNT_VRAM + BANK_COUNT_HRAM + BANK_COUNT_SRAM)
 
 extern SLONG area_Avail(SLONG bank);
 extern void AssignSections(void);
--- a/src/link/assign.c
+++ b/src/link/assign.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include "extern/err.h"
 #include "link/mylink.h"
@@ -13,6 +14,14 @@
 	struct sFreeArea *pPrev, *pNext;
 };
 
+struct sSectionAttributes {
+	const char *name;
+	SLONG bank;
+	SLONG offset; // bank + offset = bank originally stored in a section struct
+	SLONG minBank;
+	SLONG bankCount;
+};
+
 struct sFreeArea *BankFree[MAXBANKS];
 SLONG MaxAvail[MAXBANKS];
 SLONG MaxBankUsed;
@@ -20,11 +29,37 @@
 SLONG MaxSBankUsed;
 SLONG MaxVBankUsed;
 
-#define DOMAXBANK(x)	{if( (x)>MaxBankUsed ) MaxBankUsed=(x);}
+const enum eSectionType SECT_MIN = SECT_WRAM0;
+const enum eSectionType SECT_MAX = SECT_SRAM;
+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}
+};
+
+#define DOMAXBANK(x, y) {switch (x) { \
+	case SECT_ROMX: DOMAXRBANK(y); break; \
+	case SECT_WRAMX: DOMAXWBANK(y); break; \
+	case SECT_SRAM: DOMAXSBANK(y); break; \
+	case SECT_VRAM: DOMAXVBANK(y); break; \
+	default: errx(1, "DOMAXBANK used with invalid parameters"); break; }}
+#define DOMAXRBANK(x)	{if( (x)>MaxBankUsed ) MaxBankUsed=(x);}
 #define DOMAXWBANK(x)	{if( (x)>MaxWBankUsed ) MaxWBankUsed=(x);}
 #define DOMAXSBANK(x)	{if( (x)>MaxSBankUsed ) MaxSBankUsed=(x);}
 #define DOMAXVBANK(x)	{if( (x)>MaxVBankUsed ) MaxVBankUsed=(x);}
 
+void
+ensureSectionTypeIsValid(enum eSectionType type)
+{
+	if (type < SECT_MIN || type > SECT_MAX) {
+		errx(1, "(INTERNAL) Invalid section type found.");
+	}
+}
+
 SLONG 
 area_Avail(SLONG bank)
 {
@@ -42,7 +77,7 @@
 	return (r);
 }
 
-SLONG 
+SLONG
 area_AllocAbs(struct sFreeArea ** ppArea, SLONG org, SLONG size)
 {
 	struct sFreeArea *pArea;
@@ -54,12 +89,12 @@
 			if (org == pArea->nOrg) {
 				pArea->nOrg += size;
 				pArea->nSize -= size;
-				return (org);
+				return 0;
 			} else {
 				if ((org + size - 1) ==
 				    (pArea->nOrg + pArea->nSize - 1)) {
 					pArea->nSize -= size;
-					return (org);
+					return 0;
 				} else {
 					struct sFreeArea *pNewArea;
 
@@ -75,7 +110,7 @@
 						pNewArea->nSize -=
 						    size + pArea->nSize;
 
-						return (org);
+						return 0;
 					} else {
 						err(1, NULL);
 					}
@@ -86,30 +121,20 @@
 		pArea = *ppArea;
 	}
 
-	return (-1);
-}
-
-SLONG
-area_AllocAbsSRAMAnyBank(SLONG org, SLONG size)
-{
-	int i;
-	for (i = 0; i < 4; ++i) {
-		if (area_AllocAbs(&BankFree[BANK_SRAM + i], org, size) == org) {
-			return BANK_SRAM + i;
-		}
-	}
-
 	return -1;
 }
 
 SLONG
-area_AllocAbsWRAMAnyBank(SLONG org, SLONG size)
+area_AllocAbsAnyBank(SLONG org, SLONG size, enum eSectionType type)
 {
-	SLONG i;
+	ensureSectionTypeIsValid(type);
 
-	for (i = 1; i <= 7; i += 1) {
-		if (area_AllocAbs(&BankFree[BANK_WRAMX + i - 1], org, size) == org) {
-			return BANK_WRAMX + i - 1;
+	SLONG startBank = SECT_ATTRIBUTES[type].bank;
+	SLONG bankCount = SECT_ATTRIBUTES[type].bankCount;
+	
+	for (int i = 0; i < bankCount; i++) {
+		if (area_AllocAbs(&BankFree[startBank + i], org, size) != -1) {
+			return startBank + i;
 		}
 	}
 
@@ -116,33 +141,7 @@
 	return -1;
 }
 
-SLONG
-area_AllocAbsVRAMAnyBank(SLONG org, SLONG size)
-{
-	if (area_AllocAbs(&BankFree[BANK_VRAM], org, size) == org) {
-		return BANK_VRAM;
-	}
-	if (area_AllocAbs(&BankFree[BANK_VRAM + 1], org, size) == org) {
-		return BANK_VRAM + 1;
-	}
-
-	return -1;
-}
-
 SLONG 
-area_AllocAbsROMXAnyBank(SLONG org, SLONG size)
-{
-	SLONG i;
-
-	for (i = 1; i <= 511; i += 1) {
-		if (area_AllocAbs(&BankFree[i], org, size) == org)
-			return (i);
-	}
-
-	return (-1);
-}
-
-SLONG 
 area_Alloc(struct sFreeArea ** ppArea, SLONG size)
 {
 	struct sFreeArea *pArea;
@@ -166,25 +165,16 @@
 }
 
 SLONG
-area_AllocVRAMAnyBank(SLONG size)
-{
-	SLONG i, org;
+area_AllocAnyBank(SLONG size, enum eSectionType type) {
+	ensureSectionTypeIsValid(type);
 
-	for (i = BANK_VRAM; i <= BANK_VRAM + 1; i += 1) {
-		if ((org = area_Alloc(&BankFree[i], size)) != -1)
-			return ((i << 16) | org);
-	}
-
-	return (-1);
-}
-
-SLONG
-area_AllocSRAMAnyBank(SLONG size)
-{
-	SLONG i, org;
-	for (i = 0; i < 4; ++i) {
-		if ((org = area_Alloc(&BankFree[BANK_SRAM + i], size)) != -1) {
-			return (i << 16) | org;
+	SLONG startBank = SECT_ATTRIBUTES[type].bank;
+	SLONG bankCount = SECT_ATTRIBUTES[type].bankCount;
+	
+	for (int i = 0; i < bankCount; i++) {
+		SLONG org = area_Alloc(&BankFree[startBank + i], size);
+		if (org != -1) {
+			return ((startBank + i) << 16) | org;
 		}
 	}
 
@@ -191,35 +181,8 @@
 	return -1;
 }
 
-SLONG
-area_AllocWRAMAnyBank(SLONG size)
-{
-	SLONG i, org;
-
-	for (i = 1; i <= 7; i += 1) {
-		if ((org = area_Alloc(&BankFree[BANK_WRAMX + i - 1], size)) != -1) {
-			return (i << 16) | org;
-		}
-	}
-
-	return -1;
-}
-
-SLONG 
-area_AllocROMXAnyBank(SLONG size)
-{
-	SLONG i, org;
-
-	for (i = 1; i <= 511; i += 1) {
-		if ((org = area_Alloc(&BankFree[i], size)) != -1)
-			return ((i << 16) | org);
-	}
-
-	return (-1);
-}
-
 struct sSection *
-FindLargestWRAM(void)
+FindLargestSection(enum eSectionType type)
 {
 	struct sSection *pSection, *r = NULL;
 	SLONG nLargest = 0;
@@ -226,7 +189,7 @@
 
 	pSection = pSections;
 	while (pSection) {
-		if (pSection->oAssigned == 0 && pSection->Type == SECT_WRAMX) {
+		if (pSection->oAssigned == 0 && pSection->Type == type) {
 			if (pSection->nByteSize > nLargest) {
 				nLargest = pSection->nByteSize;
 				r = pSection;
@@ -237,142 +200,44 @@
 	return r;
 }
 
-struct sSection *
-FindLargestVRAM(void)
-{
-	struct sSection *pSection, *r = NULL;
-	SLONG nLargest = 0;
-
-	pSection = pSections;
-	while (pSection) {
-		if (pSection->oAssigned == 0 && pSection->Type == SECT_VRAM) {
-			if (pSection->nByteSize > nLargest) {
-				nLargest = pSection->nByteSize;
-				r = pSection;
-			}
-		}
-		pSection = pSection->pNext;
-	}
-	return r;
-}
-
-struct sSection *
-FindLargestSRAM(void)
-{
-	struct sSection *pSection, *r = NULL;
-	SLONG nLargest = 0;
-
-	pSection = pSections;
-	while (pSection) {
-		if (pSection->oAssigned == 0 && pSection->Type == SECT_SRAM) {
-			if (pSection->nByteSize > nLargest) {
-				nLargest = pSection->nByteSize;
-				r = pSection;
-			}
-		}
-		pSection = pSection->pNext;
-	}
-	return r;
-}
-
-struct sSection *
-FindLargestCode(void)
-{
-	struct sSection *pSection, *r = NULL;
-	SLONG nLargest = 0;
-
-	pSection = pSections;
-	while (pSection) {
-		if (pSection->oAssigned == 0 && pSection->Type == SECT_ROMX) {
-			if (pSection->nByteSize > nLargest) {
-				nLargest = pSection->nByteSize;
-				r = pSection;
-			}
-		}
-		pSection = pSection->pNext;
-	}
-	return (r);
-}
-
 void
-AssignVRAMSections(void)
+AssignBankedSections(enum eSectionType type)
 {
-	struct sSection *pSection;
+	ensureSectionTypeIsValid(type);
 
-	while ((pSection = FindLargestVRAM())) {
-		SLONG org;
-
-		if ((org = area_AllocVRAMAnyBank(pSection->nByteSize)) != -1) {
-			pSection->nOrg = org & 0xFFFF;
-			pSection->nBank = org >> 16;
-			pSection->oAssigned = 1;
-			DOMAXVBANK(pSection->nBank);
-		} else {
-			errx(1, "Unable to place VRAM section anywhere");
-		}
-	}
-}
-
-void
-AssignSRAMSections(void)
-{
 	struct sSection *pSection;
 
-	while ((pSection = FindLargestSRAM())) {
+	while ((pSection = FindLargestSection(type))) {
 		SLONG org;
 
-		if ((org = area_AllocSRAMAnyBank(pSection->nByteSize)) != -1) {
+		if ((org = area_AllocAnyBank(pSection->nByteSize, type)) != -1) {
 			pSection->nOrg = org & 0xFFFF;
 			pSection->nBank = org >> 16;
-			pSection->nBank += BANK_SRAM;
 			pSection->oAssigned = 1;
-			DOMAXSBANK(pSection->nBank);
+			DOMAXBANK(pSection->Type, pSection->nBank);
 		} else {
-			errx(1, "Unable to place SRAM section anywhere");
+			errx(1, "Unable to place %s section anywhere",
+				 SECT_ATTRIBUTES[type].name);
 		}
 	}
 }
 
-void
-AssignWRAMSections(void)
+bool
+VerifyAndSetBank(struct sSection *pSection)
 {
-	struct sSection *pSection;
+	ensureSectionTypeIsValid(pSection->Type);
 
-	while ((pSection = FindLargestWRAM())) {
-		SLONG org;
-
-		if ((org = area_AllocWRAMAnyBank(pSection->nByteSize)) != -1) {
-			pSection->nOrg = org & 0xFFFF;
-			pSection->nBank = org >> 16;
-			pSection->nBank += BANK_WRAMX - 1;
-			pSection->oAssigned = 1;
-			DOMAXWBANK(pSection->nBank);
-		} else {
-			errx(1, "Unable to place WRAMX section anywhere");
-		}
+	if (pSection->nBank >= SECT_ATTRIBUTES[pSection->Type].minBank
+		&& pSection->nBank < SECT_ATTRIBUTES[pSection->Type].minBank + SECT_ATTRIBUTES[pSection->Type].bankCount) {
+		pSection->nBank += SECT_ATTRIBUTES[pSection->Type].bank + SECT_ATTRIBUTES[pSection->Type].offset;
+		return true;
+		
+	} else {
+		return false;
 	}
 }
 
-void 
-AssignCodeSections(void)
-{
-	struct sSection *pSection;
-
-	while ((pSection = FindLargestCode())) {
-		SLONG org;
-
-		if ((org = area_AllocROMXAnyBank(pSection->nByteSize)) != -1) {
-			pSection->nOrg = org & 0xFFFF;
-			pSection->nBank = org >> 16;
-			pSection->oAssigned = 1;
-			DOMAXBANK(pSection->nBank);
-		} else {
-			errx(1, "Unable to place ROMX section anywhere");
-		}
-	}
-}
-
-void 
+void
 AssignSections(void)
 {
 	SLONG i;
@@ -392,17 +257,15 @@
 			err(1, NULL);
 		}
 
-		if (i == 0) {
+		if (i == BANK_ROM0) {
 			/* ROM0 bank */
 			BankFree[i]->nOrg = 0x0000;
 			if (options & OPT_SMALL) {
 				BankFree[i]->nSize = 0x8000;
-				MaxAvail[i] = 0x8000;
 			} else {
 				BankFree[i]->nSize = 0x4000;
-				MaxAvail[i] = 0x4000;
 			}
-		} else if (i >= 1 && i <= 511) {
+		} else if (i >= BANK_ROMX && i < BANK_ROMX + BANK_COUNT_ROMX) {
 			/* Swappable ROM bank */
 			BankFree[i]->nOrg = 0x4000;
 			/*
@@ -411,37 +274,34 @@
 			 */
 			if (options & OPT_SMALL) {
 				BankFree[i]->nSize = 0;
-				MaxAvail[i] = 0;
 			} else {
 				BankFree[i]->nSize = 0x4000;
-				MaxAvail[i] = 0x4000;
 			}
 		} else if (i == BANK_WRAM0) {
 			/* WRAM */
 			BankFree[i]->nOrg = 0xC000;
 			BankFree[i]->nSize = 0x1000;
-			MaxAvail[i] = 0x1000;
-		} else if (i >= BANK_SRAM && i <= BANK_SRAM + 3) {
+		} else if (i >= BANK_SRAM && i < BANK_SRAM + BANK_COUNT_SRAM) {
 			/* Swappable SRAM bank */
 			BankFree[i]->nOrg = 0xA000;
 			BankFree[i]->nSize = 0x2000;
-			MaxAvail[i] = 0x2000;
-		} else if (i >= BANK_WRAMX && i <= BANK_WRAMX + 6) {
+		} else if (i >= BANK_WRAMX && i < BANK_WRAMX + BANK_COUNT_WRAMX) {
 			/* Swappable WRAM bank */
 			BankFree[i]->nOrg = 0xD000;
 			BankFree[i]->nSize = 0x1000;
-			MaxAvail[i] = 0x1000;
-		} else if (i == BANK_VRAM || i == BANK_VRAM + 1) {
+		} else if (i >= BANK_VRAM && i < BANK_VRAM + BANK_COUNT_VRAM) {
 			/* Swappable VRAM bank */
 			BankFree[i]->nOrg = 0x8000;
 			BankFree[i]->nSize = 0x2000;
-			MaxAvail[i] = 0x2000;
 		} else if (i == BANK_HRAM) {
 			/* HRAM */
 			BankFree[i]->nOrg = 0xFF80;
 			BankFree[i]->nSize = 0x007F;
-			MaxAvail[i] = 0x007F;
+		} else {
+			errx(1, "(INTERNAL) Unknown bank type!");
 		}
+		
+		MaxAvail[i] = BankFree[i]->nSize;
 		BankFree[i]->pPrev = NULL;
 		BankFree[i]->pNext = NULL;
 	}
@@ -460,241 +320,31 @@
 
 			switch (pSection->Type) {
 			case SECT_WRAM0:
-				if (area_AllocAbs
-				    (&BankFree[BANK_WRAM0], pSection->nOrg,
-					pSection->nByteSize) != pSection->nOrg) {
-					errx(1,
-					    "Unable to load fixed WRAM0 "
-					    "section at $%lX", pSection->nOrg);
-				}
-				pSection->oAssigned = 1;
-				pSection->nBank = BANK_WRAM0;
-				break;
 			case SECT_HRAM:
-				if (area_AllocAbs
-				    (&BankFree[BANK_HRAM], pSection->nOrg,
-					pSection->nByteSize) != pSection->nOrg) {
-					errx(1, "Unable to load fixed HRAM "
-					    "section at $%lX", pSection->nOrg);
+			case SECT_ROM0:
+				pSection->nBank = SECT_ATTRIBUTES[pSection->Type].bank;
+				if (area_AllocAbs(&BankFree[pSection->nBank], pSection->nOrg,
+					 pSection->nByteSize) == -1) {
+					errx(1, "Unable to load fixed %s section at $%lX",
+						 SECT_ATTRIBUTES[pSection->Type].name,
+						 pSection->nOrg);
 				}
 				pSection->oAssigned = 1;
-				pSection->nBank = BANK_HRAM;
 				break;
-			case SECT_SRAM:
-				if (pSection->nBank == -1) {
-					/*
-					 * User doesn't care which bank.
-					 * Therefore he must here be specifying
-					 * position within the bank.
-					 * Defer until later.
-					 */
-					;
-				} else {
-					/*
-					 * User specified which bank to use.
-					 * Does he also care about position
-					 * within the bank?
-					 */
-					if (pSection->nOrg == -1) {
-						/*
-						 * Nope, any position will do
-						 * Again, we'll do that later
-						 *
-						 */
-						;
-					} else {
-						/*
-						 * Bank and position within the
-						 * bank are hardcoded.
-						 */
 
-						if (pSection->nBank >= 0
-						    && pSection->nBank <= 3) {
-							pSection->nBank +=
-							    BANK_SRAM;
-							if (area_AllocAbs
-							    (&BankFree
-							    [pSection->nBank],
-							    pSection->nOrg,
-							    pSection->nByteSize)
-							    != pSection->nOrg) {
-								errx(1,
-"Unable to load fixed SRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-							}
-							DOMAXVBANK(pSection->
-							    nBank);
-							pSection->oAssigned = 1;
-						} else {
-							errx(1,
-"Unable to load fixed SRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-						}
-					}
-				}
-				break;
+			case SECT_SRAM:
 			case SECT_WRAMX:
-				if (pSection->nBank == -1) {
-					/*
-					 * User doesn't care which bank.
-					 * Therefore he must here be specifying
-					 * position within the bank.
-					 * Defer until later.
-					 */
-					;
-				} else {
-					/*
-					 * User specified which bank to use.
-					 * Does he also care about position
-					 * within the bank?
-					 */
-					if (pSection->nOrg == -1) {
-						/*
-						 * Nope, any position will do
-						 * Again, we'll do that later
-						 *
-						 */
-						;
-					} else {
-						/*
-						 * Bank and position within the
-						 * bank are hardcoded.
-						 */
-
-						if (pSection->nBank >= 0
-						    && pSection->nBank <= 6) {
-							pSection->nBank +=
-							    BANK_WRAMX;
-							if (area_AllocAbs
-							    (&BankFree
-							    [pSection->nBank],
-							    pSection->nOrg,
-							    pSection->nByteSize)
-							    != pSection->nOrg) {
-								errx(1,
-"Unable to load fixed WRAMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-							}
-							DOMAXWBANK(pSection->
-							    nBank);
-							pSection->oAssigned = 1;
-						} else {
-							errx(1,
-"Unable to load fixed WRAMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-						}
-					}
-				}
-				break;
 			case SECT_VRAM:
-				if (pSection->nBank == -1) {
-					/*
-					 * User doesn't care which bank.
-					 * Therefore he must here be specifying
-					 * position within the bank.
-					 * Defer until later.
-					 */
-					;
-				} else {
-					/*
-					 * User specified which bank to use.
-					 * Does he also care about position
-					 * within the bank?
-					 */
-					if (pSection->nOrg == -1) {
-						/*
-						 * Nope, any position will do
-						 * Again, we'll do that later
-						 *
-						 */
-						;
-					} else {
-						/*
-						 * Bank and position within the
-						 * bank are hardcoded.
-						 */
-
-						if (pSection->nBank >= 0
-						    && pSection->nBank <= 1) {
-							pSection->nBank +=
-							    BANK_VRAM;
-							if (area_AllocAbs
-							    (&BankFree
-							    [pSection->nBank],
-							    pSection->nOrg,
-							    pSection->nByteSize)
-							    != pSection->nOrg) {
-								errx(1,
-"Unable to load fixed VRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-							}
-							DOMAXVBANK(pSection->
-							    nBank);
-							pSection->oAssigned = 1;
-						} else {
-							errx(1,
-"Unable to load fixed VRAM section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-						}
-					}
-				}
-				break;
-			case SECT_ROM0:
-				if (area_AllocAbs
-				    (&BankFree[BANK_ROM0], pSection->nOrg,
-					pSection->nByteSize) != pSection->nOrg) {
-					errx(1, "Unable to load fixed ROM0 "
-					    "section at $%lX", pSection->nOrg);
-				}
-				pSection->oAssigned = 1;
-				pSection->nBank = BANK_ROM0;
-				break;
 			case SECT_ROMX:
-				if (pSection->nBank == -1) {
-					/*
-					 * User doesn't care which bank, so he must want to
-					 * decide which position within that bank.
-					 * We'll do that at a later stage when the really
-					 * hardcoded things are allocated
-					 *
-					 */
-				} else {
-					/*
-					 * User wants to decide which bank we use
-					 * Does he care about the position as well?
-					 *
-					 */
-
-					if (pSection->nOrg == -1) {
-						/*
-						 * Nope, any position will do
-						 * Again, we'll do that later
-						 *
-						 */
+				if (pSection->nBank != -1 && pSection->nOrg != -1) {
+					if (VerifyAndSetBank(pSection) &&
+						area_AllocAbs(&BankFree[pSection->nBank], pSection->nOrg, pSection->nByteSize) != -1) {
+						DOMAXBANK(pSection->Type, pSection->nBank);
+						pSection->oAssigned = 1;
 					} else {
-						/*
-						 * How hardcore can you possibly get? Why does
-						 * he even USE this package? Yeah let's just
-						 * direct address everything, shall we?
-						 * Oh well, the customer is always right
-						 *
-						 */
-
-						if (pSection->nBank >= 1
-						    && pSection->nBank <= 511) {
-							if (area_AllocAbs
-							    (&BankFree
-								[pSection->nBank],
-								pSection->nOrg,
-								pSection->
-								nByteSize) !=
-							    pSection->nOrg) {
-								errx(1,
-								    "Unable to load fixed ROMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-							}
-							DOMAXBANK(pSection->
-							    nBank);
-							pSection->oAssigned = 1;
-						} else {
-							errx(1,
-							"Unable to load fixed ROMX section at $%lX in bank $%02lX", pSection->nOrg, pSection->nBank);
-						}
+						errx(1,
+							 "Unable to load fixed %s section at $%lX in bank $%02lX", SECT_ATTRIBUTES[pSection->Type].name, pSection->nOrg, pSection->nBank);
 					}
-
 				}
 				break;
 			}
@@ -710,70 +360,27 @@
 	pSection = pSections;
 	while (pSection) {
 		if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_ROMX
-		    && pSection->nOrg == -1 && pSection->nBank != -1) {
-			/* User wants to have a say... and he's pissed */
-			if (pSection->nBank >= 1 && pSection->nBank <= 511) {
-				if ((pSection->nOrg =
-					area_Alloc(&BankFree[pSection->nBank],
-					    pSection->nByteSize)) == -1) {
-					errx(1,
-					"Unable to load fixed ROMX section into bank $%02lX", pSection->nBank);
+			&& pSection->nOrg == -1 && pSection->nBank != -1) {
+			switch (pSection->Type) {
+			case SECT_ROMX:
+			case SECT_SRAM:
+			case SECT_VRAM:
+			case SECT_WRAMX:
+				if (VerifyAndSetBank(pSection) &&
+					(pSection->nOrg = area_Alloc(&BankFree[pSection->nBank], pSection->nByteSize)) != -1) {
+					pSection->oAssigned = 1;
+					DOMAXBANK(pSection->Type, pSection->nBank);
+				} else {
+					errx(1, "Unable to load fixed %s section into bank $%02lX",
+						 SECT_ATTRIBUTES[pSection->Type].name, pSection->nBank);
 				}
-				pSection->oAssigned = 1;
-				DOMAXBANK(pSection->nBank);
-			} else {
-				errx(1, "Unable to load fixed ROMX section into bank $%02lX", pSection->nBank);
+				break;
+
+			default: // Handle other sections later
+				break;
 			}
-		} else if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_SRAM
-		    && pSection->nOrg == -1 && pSection->nBank != -1) {
-			pSection->nBank += BANK_SRAM;
-			/* User wants to have a say... and he's pissed */
-			if (pSection->nBank >= BANK_SRAM && pSection->nBank <= BANK_SRAM + 3) {
-				if ((pSection->nOrg =
-					area_Alloc(&BankFree[pSection->nBank],
-					    pSection->nByteSize)) == -1) {
-					errx(1, "Unable to load fixed SRAM section into bank $%02lX", pSection->nBank);
-				}
-				pSection->oAssigned = 1;
-				DOMAXSBANK(pSection->nBank);
-			} else {
-				errx(1, "Unable to load fixed VRAM section into bank $%02lX", pSection->nBank);
-			}
-		} else if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_VRAM
-		    && pSection->nOrg == -1 && pSection->nBank != -1) {
-			pSection->nBank += BANK_VRAM;
-			/* User wants to have a say... and he's pissed */
-			if (pSection->nBank >= BANK_VRAM && pSection->nBank <= BANK_VRAM + 1) {
-				if ((pSection->nOrg =
-					area_Alloc(&BankFree[pSection->nBank],
-					    pSection->nByteSize)) == -1) {
-					errx(1, "Unable to load fixed VRAM section into bank $%02lX", pSection->nBank);
-				}
-				pSection->oAssigned = 1;
-				DOMAXVBANK(pSection->nBank);
-			} else {
-				errx(1, "Unable to load fixed VRAM section into bank $%02lX", pSection->nBank);
-			}
-		} else if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_WRAMX
-		    && pSection->nOrg == -1 && pSection->nBank != -1) {
-			pSection->nBank += BANK_WRAMX;
-			/* User wants to have a say... and he's pissed */
-			if (pSection->nBank >= BANK_WRAMX && pSection->nBank <= BANK_WRAMX + 6) {
-				if ((pSection->nOrg =
-					area_Alloc(&BankFree[pSection->nBank],
-					    pSection->nByteSize)) == -1) {
-					errx(1, "Unable to load fixed WRAMX section into bank $%02lX", pSection->nBank - BANK_WRAMX);
-				}
-				pSection->oAssigned = 1;
-				DOMAXWBANK(pSection->nBank);
-			} else {
-				errx(1, "Unable to load fixed WRAMX section into bank $%02lX", pSection->nBank - BANK_WRAMX);
-			}
 		}
+
 		pSection = pSection->pNext;
 	}
 
@@ -785,58 +392,27 @@
 	pSection = pSections;
 	while (pSection) {
 		if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_ROMX
-		    && pSection->nOrg != -1 && pSection->nBank == -1) {
-			/* User wants to have a say... and he's back with a
-			 * vengeance */
-			if ((pSection->nBank =
-				area_AllocAbsROMXAnyBank(pSection->nOrg,
-				    pSection->nByteSize)) ==
-			    -1) {
-				errx(1, "Unable to load fixed ROMX section at $%lX into any bank", pSection->nOrg);
+			&& pSection->nOrg != -1 && pSection->nBank == -1) {
+			switch (pSection->Type) {
+			case SECT_ROMX:
+			case SECT_VRAM:
+			case SECT_SRAM:
+			case SECT_WRAMX:
+				if ((pSection->nBank =
+					area_AllocAbsAnyBank(pSection->nOrg, pSection->nByteSize,
+						pSection->Type)) == -1) {
+					errx(1, "Unable to load fixed %s section at $%lX into any bank",
+						 SECT_ATTRIBUTES[pSection->Type].name, pSection->nOrg);
+				}
+				pSection->oAssigned = 1;
+				DOMAXBANK(pSection->Type, pSection->nBank);
+				break;
+					
+			default: // Handle other sections later
+				break;
 			}
-			pSection->oAssigned = 1;
-			DOMAXBANK(pSection->nBank);
-		} else if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_VRAM
-		    && pSection->nOrg != -1 && pSection->nBank == -1) {
-			/* User wants to have a say... and he's back with a
-			 * vengeance */
-			if ((pSection->nBank =
-				area_AllocAbsVRAMAnyBank(pSection->nOrg,
-				    pSection->nByteSize)) ==
-			    -1) {
-				errx(1, "Unable to load fixed VRAM section at $%lX into any bank", pSection->nOrg);
-			}
-			pSection->oAssigned = 1;
-			DOMAXVBANK(pSection->nBank);
-		} else if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_SRAM
-		    && pSection->nOrg != -1 && pSection->nBank == -1) {
-			/* User wants to have a say... and he's back with a
-			 * vengeance */
-			if ((pSection->nBank =
-				area_AllocAbsSRAMAnyBank(pSection->nOrg,
-				    pSection->nByteSize)) ==
-			    -1) {
-				errx(1, "Unable to load fixed SRAM section at $%lX into any bank", pSection->nOrg);
-			}
-			pSection->oAssigned = 1;
-			DOMAXSBANK(pSection->nBank);
-		} else if (pSection->oAssigned == 0
-		    && pSection->Type == SECT_WRAMX
-		    && pSection->nOrg != -1 && pSection->nBank == -1) {
-			/* User wants to have a say... and he's back with a
-			 * vengeance */
-			if ((pSection->nBank =
-				area_AllocAbsWRAMAnyBank(pSection->nOrg,
-				    pSection->nByteSize)) ==
-			    -1) {
-				errx(1, "Unable to load fixed WRAMX section at $%lX into any bank", pSection->nOrg);
-			}
-			pSection->oAssigned = 1;
-			DOMAXWBANK(pSection->nBank);
 		}
+
 		pSection = pSection->pNext;
 	}
 
@@ -851,40 +427,23 @@
 		if (pSection->oAssigned == 0) {
 			switch (pSection->Type) {
 			case SECT_WRAM0:
-				if ((pSection->nOrg =
-					area_Alloc(&BankFree[BANK_WRAM0],
-					    pSection->nByteSize)) == -1) {
-					errx(1, "WRAM0 section too large");
-				}
-				pSection->nBank = BANK_WRAM0;
-				pSection->oAssigned = 1;
-				break;
 			case SECT_HRAM:
+			case SECT_ROM0:
+				pSection->nBank = SECT_ATTRIBUTES[pSection->Type].bank;
 				if ((pSection->nOrg =
-					area_Alloc(&BankFree[BANK_HRAM],
+					area_Alloc(&BankFree[pSection->nBank],
 					    pSection->nByteSize)) == -1) {
-					errx(1, "HRAM section too large");
+					errx(1, "%s section too large", SECT_ATTRIBUTES[pSection->Type].name);
 				}
-				pSection->nBank = BANK_HRAM;
 				pSection->oAssigned = 1;
 				break;
+
 			case SECT_SRAM:
-				break;
 			case SECT_VRAM:
-				break;
 			case SECT_WRAMX:
-				break;
-			case SECT_ROM0:
-				if ((pSection->nOrg =
-					area_Alloc(&BankFree[BANK_ROM0],
-					    pSection->nByteSize)) == -1) {
-					errx(1, "ROM0 section too large");
-				}
-				pSection->nBank = BANK_ROM0;
-				pSection->oAssigned = 1;
-				break;
 			case SECT_ROMX:
 				break;
+
 			default:
 				errx(1, "(INTERNAL) Unknown section type!");
 				break;
@@ -893,10 +452,10 @@
 		pSection = pSection->pNext;
 	}
 
-	AssignCodeSections();
-	AssignVRAMSections();
-	AssignWRAMSections();
-	AssignSRAMSections();
+	AssignBankedSections(SECT_ROMX);
+	AssignBankedSections(SECT_VRAM);
+	AssignBankedSections(SECT_WRAMX);
+	AssignBankedSections(SECT_SRAM);
 }
 
 void