From 03cdce11fa253b54c3cdb6f83682b2a5ca3e036b Mon Sep 17 00:00:00 2001 From: trillian Date: Mon, 4 Mar 2024 05:11:01 +0200 Subject: [PATCH] fix freecode in hirom returning wrong banks --- src/asar/libsmw.cpp | 12 +++++++++++- tests/freespace_hirom.asm | 5 +++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/asar/libsmw.cpp b/src/asar/libsmw.cpp index ec8811fc..c11f64f2 100644 --- a/src/asar/libsmw.cpp +++ b/src/asar/libsmw.cpp @@ -516,7 +516,17 @@ void WalkMetadata(int loc, void(*func)(int loc, char * name, int len, const unsi int getsnesfreespace(int size, int target_bank, bool autoexpand, bool respectbankborders, bool align, bool write_rats, int search_start) { - return pctosnes(getpcfreespace(size, target_bank, autoexpand, respectbankborders, align, write_rats, snestopc(search_start))); + int pcfree = getpcfreespace(size, target_bank, autoexpand, respectbankborders, align, write_rats, snestopc(search_start)); + int snesfree = pctosnes(pcfree); + if(snesfree == -1) return -1; + bool isforcode = target_bank == -2; + // not sure if this is ever a concern for other mappers... + if(isforcode && (mapper == hirom || mapper == exhirom)) { + snesfree = snesfree & ~0x400000; + // maybe this should throw an internal error or something instead lol + if((snesfree&0x8000) != 0x8000) return -1; + } + return snesfree; } bool openrom(const char * filename, bool confirm) diff --git a/tests/freespace_hirom.asm b/tests/freespace_hirom.asm index 51e57d14..3b0abfd2 100644 --- a/tests/freespace_hirom.asm +++ b/tests/freespace_hirom.asm @@ -1,5 +1,5 @@ ;`+ -;`08000 53 54 41 52 01 00 fe ff 01 02 +;`08000 53 54 41 52 02 00 fd ff 08 80 80 ;`0fff8 53 54 41 52 fd ff 02 00 ;`1fffd 03 ;`2fff8 53 54 41 52 ff ff 00 00 @@ -11,7 +11,8 @@ norom ; hack: this disables checksum generation hirom freecode cleaned -db $01,$02 +codelbl: +dl codelbl freedata cleaned skip $fffd