Add overlay file option to rgblink (-O)

This option takes a file and places fixed sections on top of it.
Should prove useful for patches and partial disassemblies.
This commit is contained in:
Sanqui
2016-03-28 01:02:05 +02:00
parent 2ea2e47231
commit 2e9c68f8c3
5 changed files with 74 additions and 12 deletions

View File

@@ -8,9 +8,12 @@
#include "link/assign.h"
char *tzOutname;
char *tzOverlayname = NULL;
SLONG MaxOverlayBank;
void
writehome(FILE * f)
writehome(FILE * f, FILE * f_overlay)
{
struct sSection *pSect;
UBYTE *mem;
@@ -18,15 +21,20 @@ writehome(FILE * f)
mem = malloc(MaxAvail[BANK_ROM0]);
if (!mem)
return;
memset(mem, fillchar, MaxAvail[BANK_ROM0]);
if (f_overlay != NULL) {
fseek(f_overlay, 0L, SEEK_SET);
fread(mem, 1, MaxAvail[BANK_ROM0], f_overlay);
} else {
memset(mem, fillchar, MaxAvail[BANK_ROM0]);
}
MapfileInitBank(0);
pSect = pSections;
while (pSect) {
if (pSect->Type == SECT_ROM0) {
memcpy(mem + pSect->nOrg, pSect->pData,
pSect->nByteSize);
pSect->nByteSize);
MapfileWriteSection(pSect);
}
pSect = pSect->pNext;
@@ -39,7 +47,7 @@ writehome(FILE * f)
}
void
writebank(FILE * f, SLONG bank)
writebank(FILE * f, FILE * f_overlay, SLONG bank)
{
struct sSection *pSect;
UBYTE *mem;
@@ -48,14 +56,19 @@ writebank(FILE * f, SLONG bank)
if (!mem)
return;
memset(mem, fillchar, MaxAvail[bank]);
if (f_overlay != NULL && bank <= MaxOverlayBank) {
fseek(f_overlay, bank*0x4000, SEEK_SET);
fread(mem, 1, MaxAvail[bank], f_overlay);
} else {
memset(mem, fillchar, MaxAvail[bank]);
}
MapfileInitBank(bank);
pSect = pSections;
while (pSect) {
if (pSect->Type == SECT_ROMX && pSect->nBank == bank) {
memcpy(mem + pSect->nOrg - 0x4000, pSect->pData,
pSect->nByteSize);
pSect->nByteSize);
MapfileWriteSection(pSect);
}
pSect = pSect->pNext;
@@ -73,18 +86,48 @@ out_Setname(char *tzOutputfile)
tzOutname = tzOutputfile;
}
void
out_SetOverlayname(char *tzOverlayfile)
{
tzOverlayname = tzOverlayfile;
}
void
Output(void)
{
SLONG i;
FILE *f;
FILE *f_overlay = NULL;
if ((f = fopen(tzOutname, "wb"))) {
writehome(f);
if (tzOverlayname) {
f_overlay = fopen(tzOverlayname, "rb");
fseek(f_overlay, 0, SEEK_END);
if (ftell(f_overlay) % 0x4000 != 0) {
fprintf(stderr, "Overlay file must be aligned to 0x4000 bytes");
exit(1);
}
MaxOverlayBank = (ftell(f_overlay) / 0x4000) - 1;
if (MaxOverlayBank < 1) {
fprintf(stderr, "Overlay file be at least 0x8000 bytes");
exit(1);
}
if (MaxOverlayBank > MaxBankUsed) {
MaxBankUsed = MaxOverlayBank;
}
}
writehome(f, f_overlay);
for (i = 1; i <= MaxBankUsed; i += 1)
writebank(f, i);
writebank(f, f_overlay, i);
fclose(f);
if (tzOverlayname) {
fclose(f_overlay);
}
}
for (i = BANK_WRAM0; i < MAXBANKS; i++) {
struct sSection *pSect;