git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

commit 8de2a68801e1824459d93a0daf2270d2b53fc278
parent 056d3a0c6e24fcebd35ca1466afef5cad6a999f1
Author: Toxa <untoxa@mail.ru>
Date:   Tue,  6 Apr 2021 14:18:58 +0300

Merge branch 'develop' of https://github.com/gbdk-2020/gbdk-2020 into develop

Diffstat:
Mgbdk-lib/examples/gb/banks_autobank/Makefile34+++++++++++++++-------------------
Mgbdk-support/bankpack/files.c13++++++++++++-
Mgbdk-support/bankpack/files.h3++-
Mgbdk-support/bankpack/obj_data.c11++++++-----
Mgbdk-support/lcc/lcc.c128++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
5 files changed, 153 insertions(+), 36 deletions(-)

diff --git a/gbdk-lib/examples/gb/banks_autobank/Makefile b/gbdk-lib/examples/gb/banks_autobank/Makefile @@ -1,7 +1,8 @@ LCC = ../../../bin/lcc -Wa-l -Wl-m BANKPACK = ../../../bin/bankpack -# CFLAGS = +# uncomment for lcc verbose output +# CFLAGS = -v BINS = autobanks.gb CSOURCES := $(wildcard *.c) @@ -10,15 +11,6 @@ ASMSOURCES := $(wildcard *.s) # Compiling will produce .o files OBJS = $(CSOURCES:%.c=%.o) $(ASMSOURCES:%.c=%.o) -# Process: .c and .s files -> .o -> (bankpack) -> .rel -> (linker) ... -> .gb -# -# In this makefile autobanked .o files get converted to .rel files (-ext=.rel) -# which will be passed to the linker instead of .o files. -# -# This allows correct packing every time, even -# for source files that don't get re-compiled. -OBJS_AUTOBANKED = $(CSOURCES:%.c=%.rel) $(ASMSOURCES:%.c=%.rel) - all: $(BINS) make.bat: Makefile @@ -34,20 +26,24 @@ make.bat: Makefile %.s: %.c $(LCC) $(CFLAGS) -S -o $@ $< -# Auto-bank the .o files into .rel files -# (called with $(OBJS), which is .o files) -# -v : prints out assigned bank info -# -ext=.rel : set output extension for autobanked files to .rel +# Process: .c and .s files -> .o -> (bankpack) -> .rel -> (linker) ... -> .gb # -# Link the compiled object files into a autobanks.gb ROM file -# (called with $(OBJS_AUTOBANKED), which is .rel files) +# -autobank : Tells lcc to call bankpack for the compiled object (.o) files before linking +# -Wb-ext=.rel : Set output extension for autobanked files to .rel +# -Wb-v : Prints out assigned autobank info (optional) # -Wl-yoA : Have makebin automatically calculate required number of ROM banks (otherwise use -Wl-yo4 in this example) # -Wl-ya4 : Use 4 RAM banks # -Wl-yt19 : Use MBC5 cartridge type $(BINS): $(OBJS) - $(BANKPACK) -ext=.rel -v -yt19 $(OBJS) - $(LCC) $(CFLAGS) -Wl-yt19 -Wl-yoA -Wl-ya4 -o $(BINS) $(OBJS_AUTOBANKED) -# ./romusage autobanks.map -g + $(LCC) $(CFLAGS) -autobank -Wb-ext=.rel -Wb-v -Wl-yt19 -Wl-yoA -Wl-ya4 -o $(BINS) $(OBJS) + + +# It's also possible to do all the compiling, autobanking and linking in a single call to lcc +# with none of the other makefile "targets" above. You can try this target out by using "make onepass". +# Notice that the input files are the .c and .asm sources instead of the compiled object files. +onepass: + $(LCC) $(CFLAGS) -autobank -Wb-ext=.rel -Wb-v -Wl-yt19 -Wl-yoA -Wl-ya4 -o $(BINS) $(CSOURCES) $(ASMSOURCES) + clean: rm -f *.o *.lst *.map *.gb *.ihx *.sym *.cdb *.adb *.asm *.rel diff --git a/gbdk-support/bankpack/files.c b/gbdk-support/bankpack/files.c @@ -59,7 +59,7 @@ void files_add(char * filename) { } -char * file_get_name_by_id(uint32_t file_id) { +char * file_get_name_in_by_id(uint32_t file_id) { file_item * files = (file_item *)filelist.p_array; @@ -70,6 +70,17 @@ char * file_get_name_by_id(uint32_t file_id) { } +char * file_get_name_out_by_id(uint32_t file_id) { + + file_item * files = (file_item *)filelist.p_array; + + if ((file_id >= 0) && (file_id < filelist.count)) + return files[file_id].name_out; + else + return NULL; +} + + // Update output names based on -path= and -ext= option params static void files_set_output_name(void) { diff --git a/gbdk-support/bankpack/files.h b/gbdk-support/bankpack/files.h @@ -19,7 +19,8 @@ void files_init(void); void files_cleanup(void); void files_add(char *); -char * file_get_name_by_id(uint32_t); +char * file_get_name_in_by_id(uint32_t); +char * file_get_name_out_by_id(uint32_t); void files_set_out_ext(char *); void files_set_out_path(char *); diff --git a/gbdk-support/bankpack/obj_data.c b/gbdk-support/bankpack/obj_data.c @@ -342,7 +342,7 @@ static bool bank_check_mbc1_ok(uint32_t bank_num) { static void bank_check_area_size(area_item * p_area) { if (p_area->size > BANK_SIZE_ROM) { printf("BankPack: ERROR! Area %s, bank %d, size %d is too large for bank size %d (file %s)\n", - p_area->name, p_area->bank_num_in, p_area->size, BANK_SIZE_ROM, file_get_name_by_id(p_area->file_id)); + p_area->name, p_area->bank_num_in, p_area->size, BANK_SIZE_ROM, file_get_name_in_by_id(p_area->file_id)); exit(EXIT_FAILURE); } } @@ -369,11 +369,11 @@ static void banks_assign_area(area_item * p_area) { (p_area->bank_num_in <= bank_limit_rom_max)) { if ((g_mbc_type == MBC_TYPE_MBC1) && (!bank_check_mbc1_ok(p_area->bank_num_in))) - printf("BankPack: Warning: Area in fixed bank assigned to MBC1 excluded bank: %d, file: %s\n", p_area->bank_num_in, file_get_name_by_id(p_area->file_id)); + printf("BankPack: Warning: Area in fixed bank assigned to MBC1 excluded bank: %d, file: %s\n", p_area->bank_num_in, file_get_name_in_by_id(p_area->file_id)); bank_add_area(&banks[p_area->bank_num_in], p_area->bank_num_in, p_area); } else - printf("BankPack: Warning: Area in fixed bank %d is outside specified range %d - %d, file: %s\n", p_area->bank_num_in, bank_limit_rom_min, bank_limit_rom_max, file_get_name_by_id(p_area->file_id)); + printf("BankPack: Warning: Area in fixed bank %d is outside specified range %d - %d, file: %s\n", p_area->bank_num_in, bank_limit_rom_min, bank_limit_rom_max, file_get_name_in_by_id(p_area->file_id)); return; } else if (p_area->bank_num_in == BANK_NUM_AUTO) { @@ -484,12 +484,13 @@ int banks_show(void) { printf("Bank %d: size=%5d, free=%5d\n", c, banks[c].size, banks[c].free); for (a = 0; a < arealist.count; a++) { if (areas[a].bank_num_out == c) { - printf(" +- Area: name=%8s, size=%5d, bank_in=%3d, bank_out=%3d, file=%s\n", + printf(" +- Area: name=%8s, size=%5d, bank_in=%3d, bank_out=%3d, file=%s -> %s\n", areas[a].name, areas[a].size, areas[a].bank_num_in, areas[a].bank_num_out, - file_get_name_by_id(areas[a].file_id)); + file_get_name_in_by_id(areas[a].file_id), + file_get_name_out_by_id(areas[a].file_id)); } } } diff --git a/gbdk-support/lcc/lcc.c b/gbdk-support/lcc/lcc.c @@ -34,6 +34,7 @@ struct list { /* circular list nodes: */ static void *alloc(int); List append(char *, List); extern char *basepath(char *); +extern char *path_stripext(char *); static int callsys(char *[]); extern char *concat(const char *, const char *); static void compose(char *[], List, List, List); @@ -56,6 +57,8 @@ extern int suffix(char *, char *[], int); extern char *tempname(char *); static void Fixllist(); +static void list_rewrite_exts(List, char *, char *); +static void list_duplicate_to_new_exts(List, char *, char *); extern char *cpp[], *include[], *com[], *as[], *bankpack[], *ld[], *ihxcheck[], *mkbin[], inputs[], *suffixes[]; extern int option(char *); @@ -70,7 +73,7 @@ static int cflag; /* -c specified */ static int Kflag; /* -K specified */ static int autobankflag; /* -K specified */ static int verbose; /* incremented for each -v */ -static List bankpacklist; /* bankpack flags */ +static List bankpack_flags; /* bankpack flags */ static List ihxchecklist; /* ihxcheck flags */ static List mkbinlist; /* loader files, flags */ static List llist[2]; /* [1] = loader files, [0] = flags */ @@ -85,6 +88,8 @@ static char **av; /* argument vector */ char *tempdir = TEMPDIR; /* directory for temporary files */ char *progname; static List lccinputs; /* list of input directories */ +char bankpack_newext[1024] = {'\0'}; + int main(int argc, char *argv[]) { int i, j, nf; @@ -202,9 +207,22 @@ int main(int argc, char *argv[]) { // if auto bank assignment is enabled, modify obj files before linking if (autobankflag) { - compose(bankpack, bankpacklist, llist[1], 0); - if (callsys(av)) + compose(bankpack, bankpack_flags, llist[1], 0); + + if (callsys(av)) { errcnt++; + } else { + // If bankpack has -ext= flag set to write obj files + // out to a new extension then rewrite the + // linker list (llist[1]) and delete list (rmlist). + // The delete list likely only has temp obj files such + // as from a single-pass build: lcc -o out.gb in1.c in2.c + if (bankpack_newext[0]) { + char * obj_suffix = ".o"; + list_rewrite_exts(llist[1], obj_suffix, bankpack_newext); + list_duplicate_to_new_exts(rmlist, obj_suffix, bankpack_newext); + } + } } // Call linker @@ -345,6 +363,36 @@ char *basepath(char *name) { return s; } +// path_stripext - return path with extension removed, +// e.g. /usr/drh/foo.c => /usr/drh/foo +char *path_stripext(char *name) { + char * copy_str = strsave(name); + char * end_str = copy_str + strlen(copy_str); + + // Work from end of string backward, + // truncate string at first "." char + while (end_str > copy_str) { + if (*end_str == '.') { + *end_str = '\0'; + break; + } + end_str--; + } + return copy_str; +} + + +// Check if an extension matches the end of a filename +int matches_ext(const char * filename, const char * ext) +{ + // Only test match if filename is larger than extension + // Then check the end of the filename for [ext] length of chars + if (strlen(filename) >= strlen(ext)) + return strncmp(filename + strlen(filename) - strlen(ext), ext, strlen(ext)) == 0; + else return 0; +} + + #ifndef WIN32 #define _P_WAIT 0 @@ -776,12 +824,20 @@ static void opt(char *arg) { case 'a': /* Assembler */ alist = append(&arg[3], alist); return; - case 'i': /* ihxcheck arg list */ - ihxchecklist = append(&arg[3], ihxchecklist); - return; - case 'b': /* bankpacklist arg list */ - bankpacklist = append(&arg[3], bankpacklist); - return; + case 'i': /* ihxcheck arg list */ + ihxchecklist = append(&arg[3], ihxchecklist); + return; + case 'b': /* auto bankpack_flags arg list */ + bankpack_flags = append(&arg[3], bankpack_flags); + // -Wb-ext=[.some-extension] + // If bankpack is going to rewrite input object files to a new extension + // then save that extension for rewriting the linker list (llist[1]) + if (strstr(&arg[3], "-ext=") != NULL) { + if (arg[8]) { + sprintf(bankpack_newext, "%s", &arg[8]); + } + } + return; case 'l': /* Linker */ if(arg[4] == 'y' && (arg[5] == 't' || arg[5] == 'o' || arg[5] == 'a' || arg[5] == 'p') && (arg[6] != '\0' && arg[6] != ' ')) goto makebinoption; //automatically pass -yo -ya -yt -yp options to makebin (backwards compatibility) @@ -807,7 +863,7 @@ static void opt(char *arg) { // If MBC option is present for makebin (-Wl-yt <n> or -Wm-yt <n>) then make a copy for bankpack to use if (arg[5] == 't') - bankpacklist = append(&arg[3], bankpacklist); + bankpack_flags = append(&arg[3], bankpack_flags); } else { sprintf(tmp, "%c%c", arg[3], arg[4]); //-s if(arg[5]) @@ -1055,3 +1111,55 @@ char *tempname(char *suffix) { rmlist = append(name, rmlist); return name; } + + +// Replace extensions for filenames in a list +static void list_rewrite_exts(List list_in, char * ext_match, char * ext_new) +{ + char * filepath_old; + + // Iterate through list and replace file extensions + if (list_in) { + List list_t = list_in; + do { + if (list_t->str) { + // Check to see if filname has desired extension + if (matches_ext(list_t->str, ext_match)) { + + // Save a copy to free after re-assignment + filepath_old = list_t->str; + // Create a new string with the replaced suffix (stringf() allocs) + list_t->str = stringf("%s%s", path_stripext(list_t->str), ext_new); + if (verbose > 0) fprintf(stderr,"lcc: rename link obj (from -autobank): %s -> %s\n", filepath_old, list_t->str); + } + } + // Move to next list item, exit if start of list is reached + list_t = list_t->link; + } while (list_t != list_in); + } +} + + +// Replace extensions for filenames in a list +static void list_duplicate_to_new_exts(List list_in, char * ext_match, char * ext_new) +{ + // List may have entries appended, cache original start + List list_start = list_in; + + // Iterate through list and replace file extensions + if (list_in) { + List list_t = list_in; + do { + if (list_t->str) { + // Check to see if filname has desired extension + if (matches_ext(list_t->str, ext_match)) { + + list_in = append(stringf("%s%s", path_stripext(list_t->str), ext_new), list_in); + if (verbose > 0) fprintf(stderr,"lcc: add to rmlist (from -autobank): %s -> %s\n", list_t->str, stringf("%s%s", path_stripext(list_t->str), ext_new)); + } + } + // Move to next list item, exit if start of list is reached + list_t = list_t->link; + } while (list_t != list_start); + } +}

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.