More work on EMBED macro.

This commit is contained in:
Scott Duensing 2024-05-23 17:17:15 -05:00
parent 8fbd2f1b72
commit 9753ff753e
8 changed files with 52 additions and 36 deletions

View file

@ -130,19 +130,19 @@ typedef struct colorS {
#define IBSTR2(x) #x #define IBSTR2(x) #x
#define IBSTR(x) IBSTR2(x) #define IBSTR(x) IBSTR2(x)
#define EMBED(name, file, address) \ #define EMBED(name, file, address) \
__asm__(".section binary." IBSTR(name) ",\"aR\" \n" \ __asm__(".section ." IBSTR(name) ",\"aR\" \n" \
".global incbin_" IBSTR(name) "_start\n" \ ".global " IBSTR(name) "_start\n" \
".balign 16\n" \ ".balign 16\n" \
"incbin_" IBSTR(name) "_start:\n" \ IBSTR(name) "_start:\n" \
".incbin \"" file "\"\n" \ ".incbin \"" file "\"\n" \
\ \
".global incbin_" IBSTR(name) "_end\n" \ ".global " IBSTR(name) "_end\n" \
".balign 1\n" \ ".balign 1\n" \
"incbin_" IBSTR(name) "_end:\n" \ IBSTR(name) "_end:\n" \
".byte 0\n" \ ".byte 0\n" \
); \ ); \
extern __attribute__((aligned(16))) const char ## name ## _start[]; \ extern __attribute__((aligned(16))) const char name ## _start[]; \
extern const char ## name ## _end[] extern const char name ## _end[]
// Single-byte // Single-byte

View file

@ -8,6 +8,9 @@ file(READ clang.cfg CONFIG)
install(FILES install(FILES
link.ld link.ld
declare.ld
memory.ld
sections.ld
output.ld output.ld
TYPE LIB) TYPE LIB)

View file

@ -0,0 +1 @@
/* This is generated by the overlay tool - DO NOT EDIT. */

View file

@ -38,8 +38,7 @@ __block22_lma = (22<<24)|__SLOT_ADDR;
__block23_lma = (23<<24)|__SLOT_ADDR; __block23_lma = (23<<24)|__SLOT_ADDR;
/* Stash preloaded binary data */ /* Stash preloaded binary data */
__binarydata_lma = 0x54000; /* Block 42 */ INCLUDE declare.ld
__BINARYDATA_SIZE = 0x2C000; /* Size of A2-3D2, 3D/2D data area, and 2 bitmaps. */
MEMORY { MEMORY {
block8 : ORIGIN = __block8_lma, LENGTH = __BLOCK_SIZE block8 : ORIGIN = __block8_lma, LENGTH = __BLOCK_SIZE
@ -58,7 +57,7 @@ MEMORY {
block21 : ORIGIN = __block21_lma, LENGTH = __BLOCK_SIZE block21 : ORIGIN = __block21_lma, LENGTH = __BLOCK_SIZE
block22 : ORIGIN = __block22_lma, LENGTH = __BLOCK_SIZE block22 : ORIGIN = __block22_lma, LENGTH = __BLOCK_SIZE
block23 : ORIGIN = __block23_lma, LENGTH = __BLOCK_SIZE block23 : ORIGIN = __block23_lma, LENGTH = __BLOCK_SIZE
/* binarydata : ORIGIN = __binarydata_lma, LENGTH = __BINARYDATA_SIZE */ INCLUDE memory.ld
} }
REGION_ALIAS("c_writeable", ram) REGION_ALIAS("c_writeable", ram)
@ -82,7 +81,7 @@ SECTIONS {
.block21 : { *(.block21 .block21.*) } >block21 end_block21 = .; .block21 : { *(.block21 .block21.*) } >block21 end_block21 = .;
.block22 : { *(.block22 .block22.*) } >block22 end_block22 = .; .block22 : { *(.block22 .block22.*) } >block22 end_block22 = .;
.block23 : { *(.block23 .block23.*) } >block23 end_block23 = .; .block23 : { *(.block23 .block23.*) } >block23 end_block23 = .;
/* .binarydata : { *(.binarydata .binarydata.*) } >binarydata end_binarydata = .; */ INCLUDE sections.ld
} }
OUTPUT_FORMAT { OUTPUT_FORMAT {
@ -95,18 +94,9 @@ OUTPUT_FORMAT {
BYTE(0x00) BYTE(0x00)
TRIM(ram) TRIM(ram)
/* Overlay Segments */ /* Overlay Segments and Binary Data */
INCLUDE output.ld INCLUDE output.ld
/* Binary Data */
/*
SHORT(ORIGIN(binarydata))
BYTE(ORIGIN(binarydata)>>16)
SHORT(end_binarydata - __binarydata_lma)
BYTE((end_binarydata - __binarydata_lma)>>16)
TRIM(binarydata)
*/
/* Launch the program, at _start */ /* Launch the program, at _start */
SHORT(_start) SHORT(_start)
LONG(0) LONG(0)

View file

@ -0,0 +1 @@
/* This is generated by the overlay tool - DO NOT EDIT. */

View file

@ -1 +1 @@
/* This is generated by the overlay tool. */ /* This is generated by the overlay tool - DO NOT EDIT. */

View file

@ -0,0 +1 @@
/* This is generated by the overlay tool - DO NOT EDIT. */

View file

@ -301,7 +301,6 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo
} }
// Is this an EMBED? "EMBED(name, file, address);" // Is this an EMBED? "EMBED(name, file, address);"
found = false; // This is tracking if we're quoted or not.
start = strstr(buffer, "EMBED"); start = strstr(buffer, "EMBED");
if (start != NULL) { if (start != NULL) {
// Scan for opening paren, three arguments. // Scan for opening paren, three arguments.
@ -312,6 +311,7 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo
d = NULL; // Start of second argument. d = NULL; // Start of second argument.
e = NULL; // End of second argument. e = NULL; // End of second argument.
f = NULL; // Start of third argument. f = NULL; // Start of third argument.
found = false; // This is tracking if we're quoted or not.
for (x=0; x<strlen(buffer); x++) { for (x=0; x<strlen(buffer); x++) {
switch (c) { switch (c) {
case 0: // Opening parenthesis. case 0: // Opening parenthesis.
@ -337,11 +337,11 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo
if (buffer[x] == '"') { if (buffer[x] == '"') {
// ***TODO*** This will barf if the filename has a " in it. // ***TODO*** This will barf if the filename has a " in it.
if (!found) { if (!found) {
d = &buffer[x]; d = &buffer[x+1];
} else { } else {
found = !found;
e = &buffer[x]; e = &buffer[x];
} }
found = !found;
} else { } else {
if ((!found) && (buffer[x] == ',')) c++; if ((!found) && (buffer[x] == ',')) c++;
} }
@ -366,10 +366,10 @@ void parseCFile(char *filename, char *targetFile, FILE *trampoline, char *trampo
// Filename. // Filename.
x = *e; x = *e;
*e = 0; *e = 0;
newEmbed->name = strdup(d); newEmbed->filename = strdup(d);
*e = x; *e = x;
// Address. // Address.
newEmbed->address = atol(f); newEmbed->address = strtol(f, NULL, 0);
// Add to list. // Add to list.
if (_embeds == NULL) { if (_embeds == NULL) {
_embeds = newEmbed; _embeds = newEmbed;
@ -442,6 +442,7 @@ int main(int argc, char *argv[]) {
DIR *dir; DIR *dir;
struct dirent *dirent; struct dirent *dirent;
struct stat fileStat; struct stat fileStat;
char *generated = "This is generated by the overlay tool - DO NOT EDIT.";
/* /*
* Command line: * Command line:
@ -485,7 +486,7 @@ int main(int argc, char *argv[]) {
free(targetDir); free(targetDir);
return 1; return 1;
} }
fprintf(out, "// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.\n\n"); fprintf(out, "// %s\n\n", generated);
fprintf(out, "#ifndef TRAMPOLINE_H\n"); fprintf(out, "#ifndef TRAMPOLINE_H\n");
fprintf(out, "#define TRAMPOLINE_H\n\n"); fprintf(out, "#define TRAMPOLINE_H\n\n");
@ -596,12 +597,34 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
fprintf(out, "/* %s */\n\n", generated);
fprintf(out2, "/* %s */\n\n", generated);
fprintf(out3, "/* %s */\n\n", generated);
// Populate three linker sections. // Populate three linker sections.
_embedsHead = _embeds; _embedsHead = _embeds;
while (_embedsHead != NULL) { while (_embedsHead != NULL) {
// Declaration. // Declaration.
in = fopen(_embedsHead->filename, "rb"); // ***TODO*** This path will be wrong. length = 0;
if (in == NULL) { // Try to open embedded file relative to each provided directory.
// This includes the target directory.
for (x=2; x<argc; x++) {
sourceDir = strdup(argv[x]);
utilFixPathSeparators(&sourceDir, true);
targetFile = utilCreateString("%s%s", sourceDir, _embedsHead->filename);
printf("Checking [%s]\n", targetFile);
free(sourceDir);
in = fopen(targetFile, "rb");
free(targetFile);
if (in != NULL) {
fseek(in, 0, SEEK_END);
length = ftell(in) + 1; // Without the +1 the linker will overflow the segment.
fclose(in);
break;
}
}
if (length == 0) {
fprintf(stderr, "ERROR! Cannot locate %s!\n", _embedsHead->filename);
free(targetDir); free(targetDir);
free(linkerFile); free(linkerFile);
free(linkerFile2); free(linkerFile2);
@ -611,16 +634,12 @@ int main(int argc, char *argv[]) {
fclose(out3); fclose(out3);
return 1; return 1;
} }
fseek(in, 0, SEEK_END); fprintf(out, " __%s_lma = 0x%lx;\n", _embedsHead->name, _embedsHead->address);
length = ftell(in); fprintf(out, " __%s_size = 0x%lx;\n\n", _embedsHead->name, length);
fclose(in);
fprintf(out, " __%s_lma = %lx;\n", _embedsHead->name, _embedsHead->address);
fprintf(out, " __%s_size = %lx;\n\n", _embedsHead->name, length);
// MEMORY. // MEMORY.
fprintf(out2, " %s : ORIGIN = __%s_lma, LENGTH = __%s_size\n", _embedsHead->name, _embedsHead->name, _embedsHead->name); fprintf(out2, " %s : ORIGIN = __%s_lma, LENGTH = __%s_size\n", _embedsHead->name, _embedsHead->name, _embedsHead->name);
// SECTIONS. // SECTIONS.
fprintf(out3, " .%s : { *(.%s .%s*) } >%s end_%s = .;\n", _embedsHead->name, _embedsHead->name, _embedsHead->name, _embedsHead->name, _embedsHead->name); fprintf(out3, " .%s : { *(.%s .%s*) } >%s end_%s = .;\n", _embedsHead->name, _embedsHead->name, _embedsHead->name, _embedsHead->name, _embedsHead->name);
_embedsHead = _embedsHead->next; _embedsHead = _embedsHead->next;
} }
@ -640,6 +659,7 @@ int main(int argc, char *argv[]) {
free(linkerFile); free(linkerFile);
return 1; return 1;
} }
fprintf(out, "/* %s */\n\n", generated);
for (x=0; x<_totalBanks; x++) { for (x=0; x<_totalBanks; x++) {
fprintf(out, " SHORT(%d*0x2000)\n", x+8); fprintf(out, " SHORT(%d*0x2000)\n", x+8);
fprintf(out, " BYTE(%d/8)\n", x+8); fprintf(out, " BYTE(%d/8)\n", x+8);