Report slack totals at the end of the .map file (#781)

Fixes #777
This commit is contained in:
Rangi
2021-03-05 05:53:27 -08:00
committed by GitHub
parent a4049a3f1b
commit c878ff8775

View File

@@ -19,6 +19,8 @@
#include "linkdefs.h" #include "linkdefs.h"
#include "platform.h" // MIN_NB_ELMS
#define BANK_SIZE 0x4000 #define BANK_SIZE 0x4000
FILE *outputFile; FILE *outputFile;
@@ -39,6 +41,18 @@ static struct {
} *banks; } *banks;
} sections[SECTTYPE_INVALID]; } sections[SECTTYPE_INVALID];
/* Defines the order in which types are output to the sym and map files */
static enum SectionType typeMap[SECTTYPE_INVALID] = {
SECTTYPE_ROM0,
SECTTYPE_ROMX,
SECTTYPE_VRAM,
SECTTYPE_SRAM,
SECTTYPE_WRAM0,
SECTTYPE_WRAMX,
SECTTYPE_OAM,
SECTTYPE_HRAM
};
void out_AddSection(struct Section const *section) void out_AddSection(struct Section const *section)
{ {
static uint32_t maxNbBanks[] = { static uint32_t maxNbBanks[] = {
@@ -64,8 +78,7 @@ void out_AddSection(struct Section const *section)
sections[section->type].banks = sections[section->type].banks =
realloc(sections[section->type].banks, realloc(sections[section->type].banks,
sizeof(*sections[0].banks) * minNbBanks); sizeof(*sections[0].banks) * minNbBanks);
for (uint32_t i = sections[section->type].nbBanks; for (uint32_t i = sections[section->type].nbBanks; i < minNbBanks; i++) {
i < minNbBanks; i++) {
sections[section->type].banks[i].sections = NULL; sections[section->type].banks[i].sections = NULL;
sections[section->type].banks[i].zeroLenSections = NULL; sections[section->type].banks[i].zeroLenSections = NULL;
} }
@@ -323,12 +336,13 @@ static void writeSymBank(struct SortedSections const *bankSections)
/** /**
* Write a bank's contents to the map file * Write a bank's contents to the map file
* @param bankSections The bank's sections * @param bankSections The bank's sections
* @return The bank's slack space
*/ */
static void writeMapBank(struct SortedSections const *sectList, static uint16_t writeMapBank(struct SortedSections const *sectList,
enum SectionType type, uint32_t bank) enum SectionType type, uint32_t bank)
{ {
if (!mapFile) if (!mapFile)
return; return 0;
struct SortedSection const *section = sectList->sections; struct SortedSection const *section = sectList->sections;
struct SortedSection const *zeroLenSection = sectList->zeroLenSections; struct SortedSection const *zeroLenSection = sectList->zeroLenSections;
@@ -367,6 +381,34 @@ static void writeMapBank(struct SortedSections const *sectList,
else else
fprintf(mapFile, " SLACK: $%04" PRIx16 " byte%s\n\n", slack, fprintf(mapFile, " SLACK: $%04" PRIx16 " byte%s\n\n", slack,
slack == 1 ? "" : "s"); slack == 1 ? "" : "s");
return slack;
}
/**
* Write the total slack space by section type to the map file
* @param slackMap The total slack space by section type
*/
static void writeMapSlack(uint32_t slackMap[MIN_NB_ELMS(SECTTYPE_INVALID)])
{
if (!mapFile)
return;
fputs("FREE:\n", mapFile);
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) {
enum SectionType type = typeMap[i];
// Do not output slack space for VRAM or OAM
if (type == SECTTYPE_VRAM || type == SECTTYPE_OAM)
continue;
if (sections[type].nbBanks > 0) {
fprintf(mapFile, " %s: $%04" PRIx32 " byte%s in %" PRIu32 " bank%s\n",
typeNames[type], slackMap[type], slackMap[type] == 1 ? "" : "s",
sections[type].nbBanks, sections[type].nbBanks == 1 ? "" : "s");
}
}
} }
/** /**
@@ -377,16 +419,7 @@ static void writeSymAndMap(void)
if (!symFileName && !mapFileName) if (!symFileName && !mapFileName)
return; return;
enum SectionType typeMap[SECTTYPE_INVALID] = { uint32_t slackMap[SECTTYPE_INVALID] = {0};
SECTTYPE_ROM0,
SECTTYPE_ROMX,
SECTTYPE_VRAM,
SECTTYPE_SRAM,
SECTTYPE_WRAM0,
SECTTYPE_WRAMX,
SECTTYPE_OAM,
SECTTYPE_HRAM
};
symFile = openFile(symFileName, "w"); symFile = openFile(symFileName, "w");
mapFile = openFile(mapFileName, "w"); mapFile = openFile(mapFileName, "w");
@@ -397,16 +430,16 @@ static void writeSymAndMap(void)
for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) { for (uint8_t i = 0; i < SECTTYPE_INVALID; i++) {
enum SectionType type = typeMap[i]; enum SectionType type = typeMap[i];
if (sections[type].nbBanks > 0) { for (uint32_t bank = 0; bank < sections[type].nbBanks; bank++) {
for (uint32_t bank = 0; bank < sections[type].nbBanks; struct SortedSections const *sect = &sections[type].banks[bank];
bank++) {
writeSymBank(&sections[type].banks[bank]); writeSymBank(sect);
writeMapBank(&sections[type].banks[bank], slackMap[type] += writeMapBank(sect, type, bank);
type, bank);
}
} }
} }
writeMapSlack(slackMap);
closeFile(symFile); closeFile(symFile);
closeFile(mapFile); closeFile(mapFile);
} }