Skip to content

Commit

Permalink
Allow padding to coexist with overlay file (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 authored May 17, 2024
1 parent e2633d5 commit 352551d
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 23 deletions.
1 change: 1 addition & 0 deletions include/link/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern char const *symFileName;
extern char const *overlayFileName;
extern char const *outputFileName;
extern uint8_t padValue;
extern bool hasPadValue;
extern uint16_t scrambleROMX;
extern uint8_t scrambleWRAMX;
extern uint8_t scrambleSRAM;
Expand Down
3 changes: 0 additions & 3 deletions man/rgblink.1
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ address)!
Write the ROM image to the given file.
.It Fl p Ar pad_value , Fl \-pad Ar pad_value
When inserting padding between sections, pad with this value.
Has no effect if
.Fl O
is specified.
The default is 0.
.It Fl S Ar spec , Fl \-scramble Ar spec
Enables a different
Expand Down
13 changes: 6 additions & 7 deletions src/asm/section.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,15 +846,14 @@ void sect_BinaryFile(std::string const &name, int32_t startPos) {
if (verbose)
printf("Aborting (-MG) on INCBIN file '%s' (%s)\n", name.c_str(), strerror(errno));
failedOnMissingInclude = true;
return;
} else {
error("Error opening INCBIN file '%s': %s\n", name.c_str(), strerror(errno));
}
error("Error opening INCBIN file '%s': %s\n", name.c_str(), strerror(errno));
return;
}
Defer closeFile{[&] { fclose(file); }};

int32_t fsize = -1;
int byte;

if (fseek(file, 0, SEEK_END) != -1) {
fsize = ftell(file);
Expand All @@ -865,6 +864,7 @@ void sect_BinaryFile(std::string const &name, int32_t startPos) {
}

fseek(file, startPos, SEEK_SET);

if (!reserveSpace(fsize - startPos))
return;
} else {
Expand All @@ -877,7 +877,7 @@ void sect_BinaryFile(std::string const &name, int32_t startPos) {
(void)fgetc(file);
}

while ((byte = fgetc(file)) != EOF) {
for (int byte; (byte = fgetc(file)) != EOF;) {
if (fsize == -1)
growSection(1);
writebyte(byte);
Expand All @@ -887,6 +887,7 @@ void sect_BinaryFile(std::string const &name, int32_t startPos) {
error("Error reading INCBIN file '%s': %s\n", name.c_str(), strerror(errno));
}

// Output a slice of a binary file
void sect_BinaryFileSlice(std::string const &name, int32_t startPos, int32_t length) {
if (startPos < 0) {
error("Start position cannot be negative (%" PRId32 ")\n", startPos);
Expand Down Expand Up @@ -920,10 +921,8 @@ void sect_BinaryFileSlice(std::string const &name, int32_t startPos, int32_t len
}
Defer closeFile{[&] { fclose(file); }};

int32_t fsize;

if (fseek(file, 0, SEEK_END) != -1) {
fsize = ftell(file);
int32_t fsize = ftell(file);

if (startPos > fsize) {
error("Specified start position is greater than length of file\n");
Expand Down
2 changes: 2 additions & 0 deletions src/link/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ char const *symFileName; // -n
char const *overlayFileName; // -O
char const *outputFileName; // -o
uint8_t padValue; // -p
bool hasPadValue = false;
// Setting these three to 0 disables the functionality
uint16_t scrambleROMX = 0; // -S
uint8_t scrambleWRAMX = 0;
Expand Down Expand Up @@ -387,6 +388,7 @@ int main(int argc, char *argv[]) {
value = 0xFF;
}
padValue = value;
hasPadValue = true;
break;
}
case 'S':
Expand Down
36 changes: 24 additions & 12 deletions src/link/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ static uint32_t checkOverlaySize() {
fseek(overlayFile, 0, SEEK_SET);

if (overlaySize % BANK_SIZE)
errx("Overlay file must have a size multiple of 0x4000");
warnx("Overlay file does not have a size multiple of 0x4000");
else if (is32kMode && overlaySize != 0x8000)
warnx("Overlay is not exactly 0x8000 bytes large");
else if (overlaySize < 0x8000)
warnx("Overlay is less than 0x8000 bytes large");

uint32_t nbOverlayBanks = overlaySize / BANK_SIZE;

if (is32kMode && nbOverlayBanks != 2)
errx("Overlay must be exactly 0x8000 bytes large");

if (nbOverlayBanks < 2)
errx("Overlay must be at least 0x8000 bytes large");

return nbOverlayBanks;
return (overlaySize + BANK_SIZE - 1) / BANK_SIZE;
}

/*
Expand All @@ -149,6 +145,22 @@ static void coverOverlayBanks(uint32_t nbOverlayBanks) {
}
}

static uint8_t getNextFillByte() {
if (overlayFile) {
int c = getc(overlayFile);
if (c != EOF) {
return c;
}

if (static bool warned = false; !hasPadValue && !warned) {
warnx("Output is larger than overlay file, but no padding value was specified");
warned = true;
}
}

return padValue;
}

/*
* Write a ROM bank's sections to the output file.
* @param bankSections The bank's sections, ordered by increasing address
Expand All @@ -164,7 +176,7 @@ static void
assume(section->offset == 0);
// Output padding up to the next SECTION
while (offset + baseOffset < section->org) {
putc(overlayFile ? getc(overlayFile) : padValue, outputFile);
putc(getNextFillByte(), outputFile);
offset++;
}

Expand All @@ -181,7 +193,7 @@ static void

if (!disablePadding) {
while (offset < size) {
putc(overlayFile ? getc(overlayFile) : padValue, outputFile);
putc(getNextFillByte(), outputFile);
offset++;
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/link/overlay/smaller/a.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SECTION "start", ROM0[0]
db 1, 2, 3, 4
ds 4
db 5, 6, 7, 8

SECTION "end", ROMX[$4000], BANK[2]
db 9, 10, 11, 12
ds 4
db 13, 14, 15, 16
1 change: 1 addition & 0 deletions test/link/overlay/smaller/out.err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
warning: Overlay file does not have a size multiple of 0x4000
Binary file added test/link/overlay/smaller/out.gb
Binary file not shown.
1 change: 1 addition & 0 deletions test/link/overlay/smaller/overlay.gb

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 11 additions & 1 deletion test/link/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,17 @@ rgblinkQuiet -o "$gbtemp2" "$outtemp"
tryCmp "$gbtemp" "$gbtemp2"
evaluateTest

test="overlay"
test="overlay/smaller"
startTest
"$RGBASM" -o "$otemp" "$test"/a.asm
continueTest
rgblinkQuiet -o "$gbtemp" -p 0x42 -O "$test"/overlay.gb "$otemp" 2>"$outtemp"
tryDiff "$test"/out.err "$outtemp"
# This test does not trim its output with 'dd' because it needs to verify the correct output size
tryCmp "$test"/out.gb "$gbtemp"
evaluateTest

test="overlay/tiny"
startTest
"$RGBASM" -o "$otemp" "$test"/a.asm
continueTest
Expand Down

0 comments on commit 352551d

Please sign in to comment.