From e1659ecd41dfac70eb021c0f5fe983ed6f043433 Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 3 Jul 2018 17:07:05 -0400 Subject: Introduce linkerscript. Addresses of sections will now be added to the linkerscript via `org`, and the section name will be the path/to/file. If there is more than one section in the file, then add a @SectionName after the path/to/file to describe the section. --- tools/fix_sections.py | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tools/fix_sections.py (limited to 'tools/fix_sections.py') diff --git a/tools/fix_sections.py b/tools/fix_sections.py new file mode 100644 index 0000000..266bb10 --- /dev/null +++ b/tools/fix_sections.py @@ -0,0 +1,134 @@ +import os, errno +import re + +os.chdir("..") +cwd = os.getcwd() + +debug_lines_startswith = [ + "SECTION ", + "else", + "SECTION ", + "endc" +] + +with open("pokegold-spaceworld.link", "r") as f: + linkerscript = f.read() + +def clean_section(line, file, multiple): + global linkerscript + lines = line.lstrip().split("\"") + file += "@" + lines[1] if multiple else "" + linkerscript = linkerscript.replace("\"" + lines[1] + "\"", "\"" + file + "\"") + if "ROMX" in lines[2]: + return "SECTION \"%s\", ROMX" % file + elif "HRAM" in lines[2]: + return "SECTION \"%s\", HRAM" % file + elif "VRAM" in lines[2]: + return "SECTION \"%s\", VRAM" % file + elif "ROM0" in lines[2]: + return "SECTION \"%s\", ROM0" % file + elif "SRAM" in lines[2]: + return "SECTION \"%s\", SRAM" % file + else: + raise + +TEMP_PATH = "" + +for root, dirs, files in os.walk(cwd): + for file in files: + rel_root = os.path.relpath(root, cwd) + if not rel_root.startswith("build") and not rel_root.startswith("temp") and file.endswith(".asm") and file != "rst.asm" and file != "wram.asm": + canonical_path = os.path.join(root, file) + rel_path = os.path.relpath(canonical_path, cwd) + with open(canonical_path, "r") as f: + contents = f.read() + content_lines = contents.splitlines() + + if "SECTION" in contents: + print(canonical_path) + modify_flag = False + skip_next_line = False + for i, line in enumerate(content_lines): + if not skip_next_line: + if line.lstrip().startswith("SECTION"): + modify_flag = True + content_lines[i] = clean_section(content_lines[i], rel_path, contents.count("SECTION") > 1) + elif "if DEBUG" in line: + debug_content_lines = content_lines[i+1:i+5] + debug_code = False + for debug_content_line, debug_line_startswith in zip(debug_content_lines, debug_lines_startswith): + if not debug_content_line.lstrip().startswith(debug_line_startswith): + break + else: + modify_flag = True + content_lines[i] = "; " + content_lines[i] + content_lines[i+1] = clean_section(content_lines[i+1], rel_path, contents.count("SECTION") > 2) + content_lines[i+2] = "; " + content_lines[i+2] + content_lines[i+3] = "; " + content_lines[i+3] + content_lines[i+4] = "; " + content_lines[i+4] + skip_next_line = True + + """ + if debug_code: + debug_content_lines = content_lines[i+1:] + debug_code_index = 0 + while True: + #if debug_content_lines[debug_code_index].startswith("else"): + # conditional_type = 0 + # break + if debug_content_lines[debug_code_index].strip().startswith("endc"): + break + debug_code_index += 1 + print(line) + for debug_content_line in debug_content_lines[:debug_code_index+1]: + print(debug_content_line) + """ + else: + skip_next_line = False + + if modify_flag: + output = "\n".join(content_lines) + print("rel root: " + rel_root) + try: + os.makedirs(TEMP_PATH + rel_root) + except OSError as e: + if e.errno != errno.EEXIST: + raise + + with open(TEMP_PATH + rel_path, "w+") as f: + f.write(output) + +linkerscript_lines = linkerscript.splitlines() + +i = 0 +while i < len(linkerscript_lines): + line = linkerscript_lines[i] + if "\"Shim for " in line: + no_pop_count = 0 + shim_addr = line.replace(", ", " ; ").split(" ; ")[1] + if linkerscript_lines[i-1] == "\torg " + shim_addr and linkerscript_lines[i-1] != "\torg $4000": + print(linkerscript_lines.pop(i-1)) + else: + no_pop_count += 1 + print(linkerscript_lines.pop(i-1 + no_pop_count)) + + if linkerscript_lines[i-1 + no_pop_count] == "\t; " + shim_addr: + print(linkerscript_lines.pop(i-1 + no_pop_count)) + else: + no_pop_count += 1 + + i -= 3 - no_pop_count + print("") + elif "ROMX" in line: + linkerscript_lines.insert(i+1, "\torg $4000") + i += 1 + else: + i += 1 + +for i in range(len(linkerscript_lines)): + linkerscript_lines[i] = linkerscript_lines[i].split(" ; ")[0] + +linkerscript = "\n".join(linkerscript_lines) + +with open(TEMP_PATH + "pokegold-spaceworld.link", "w+") as f: + f.write(linkerscript) \ No newline at end of file -- cgit v1.2.3 From ca0ba8785741c30744ee5d741e83344f40ab7f07 Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Tue, 3 Jul 2018 17:09:08 -0400 Subject: Clean up unused code. --- tools/fix_sections.py | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'tools/fix_sections.py') diff --git a/tools/fix_sections.py b/tools/fix_sections.py index 266bb10..e00917c 100644 --- a/tools/fix_sections.py +++ b/tools/fix_sections.py @@ -67,22 +67,6 @@ for root, dirs, files in os.walk(cwd): content_lines[i+3] = "; " + content_lines[i+3] content_lines[i+4] = "; " + content_lines[i+4] skip_next_line = True - - """ - if debug_code: - debug_content_lines = content_lines[i+1:] - debug_code_index = 0 - while True: - #if debug_content_lines[debug_code_index].startswith("else"): - # conditional_type = 0 - # break - if debug_content_lines[debug_code_index].strip().startswith("endc"): - break - debug_code_index += 1 - print(line) - for debug_content_line in debug_content_lines[:debug_code_index+1]: - print(debug_content_line) - """ else: skip_next_line = False -- cgit v1.2.3 From ca09233c9eca4c6b9b0ccfba436dcd7e3b3e669c Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Sun, 8 Jul 2018 17:34:41 -0400 Subject: Build files in maps/ --- tools/fix_sections.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'tools/fix_sections.py') diff --git a/tools/fix_sections.py b/tools/fix_sections.py index e00917c..fa40b4b 100644 --- a/tools/fix_sections.py +++ b/tools/fix_sections.py @@ -11,13 +11,17 @@ debug_lines_startswith = [ "endc" ] -with open("pokegold-spaceworld.link", "r") as f: +with open("pokegold-spaceworld-gen.link", "r") as f: linkerscript = f.read() def clean_section(line, file, multiple): global linkerscript lines = line.lstrip().split("\"") - file += "@" + lines[1] if multiple else "" + if "@" not in lines[1] and not file == lines[1]: + file += ("@" + lines[1]) if multiple else "" + else: + file = lines[1] + linkerscript = linkerscript.replace("\"" + lines[1] + "\"", "\"" + file + "\"") if "ROMX" in lines[2]: return "SECTION \"%s\", ROMX" % file @@ -32,7 +36,7 @@ def clean_section(line, file, multiple): else: raise -TEMP_PATH = "" +TEMP_PATH = "temp/" for root, dirs, files in os.walk(cwd): for file in files: -- cgit v1.2.3 From 5a6620021ac2d7ea78c0ec45291cf8a169567206 Mon Sep 17 00:00:00 2001 From: luckytyphlosion <10688458+luckytyphlosion@users.noreply.github.com> Date: Sun, 8 Jul 2018 18:00:32 -0400 Subject: Add maps to linkerscript --- tools/fix_sections.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'tools/fix_sections.py') diff --git a/tools/fix_sections.py b/tools/fix_sections.py index fa40b4b..3d864ed 100644 --- a/tools/fix_sections.py +++ b/tools/fix_sections.py @@ -1,5 +1,6 @@ import os, errno import re +import fix_sections_directory os.chdir("..") cwd = os.getcwd() @@ -17,10 +18,10 @@ with open("pokegold-spaceworld-gen.link", "r") as f: def clean_section(line, file, multiple): global linkerscript lines = line.lstrip().split("\"") - if "@" not in lines[1] and not file == lines[1]: - file += ("@" + lines[1]) if multiple else "" - else: + if "@" in lines[1] or file == lines[1] or file == "hram.asm" or file == "vram.asm" or file == "sram.asm": file = lines[1] + else: + file += ("@" + lines[1]) if multiple else "" linkerscript = linkerscript.replace("\"" + lines[1] + "\"", "\"" + file + "\"") if "ROMX" in lines[2]: @@ -36,7 +37,7 @@ def clean_section(line, file, multiple): else: raise -TEMP_PATH = "temp/" +TEMP_PATH = fix_sections_directory.TEMP_DIRECTORY for root, dirs, files in os.walk(cwd): for file in files: @@ -89,9 +90,17 @@ for root, dirs, files in os.walk(cwd): linkerscript_lines = linkerscript.splitlines() i = 0 +clean_wram = False + while i < len(linkerscript_lines): line = linkerscript_lines[i] - if "\"Shim for " in line: + if clean_wram: + if "org $dfff" not in line: + print(linkerscript_lines.pop(i)) + else: + clean_wram = False + i += 1 + elif "\"Shim for " in line: no_pop_count = 0 shim_addr = line.replace(", ", " ; ").split(" ; ")[1] if linkerscript_lines[i-1] == "\torg " + shim_addr and linkerscript_lines[i-1] != "\torg $4000": @@ -107,16 +116,22 @@ while i < len(linkerscript_lines): i -= 3 - no_pop_count print("") - elif "ROMX" in line: + elif "ROMX" in line and "org $4000" not in linkerscript_lines[i+1]: linkerscript_lines.insert(i+1, "\torg $4000") i += 1 + elif line.startswith("WRAM0"): + linkerscript_lines.insert(i+1, "\torg $c000") + i += 1 + elif "\"Map Buffer\"" in line: + clean_wram = True + i += 1 else: i += 1 for i in range(len(linkerscript_lines)): linkerscript_lines[i] = linkerscript_lines[i].split(" ; ")[0] -linkerscript = "\n".join(linkerscript_lines) +linkerscript = "\n".join(linkerscript_lines) + "\n" with open(TEMP_PATH + "pokegold-spaceworld.link", "w+") as f: f.write(linkerscript) \ No newline at end of file -- cgit v1.2.3