Skip to content

Commit

Permalink
Squashed 'tools/freesia/' changes from d54170f..0cd3976
Browse files Browse the repository at this point in the history
0cd3976 adjust how the extra space on either end is handled

git-subtree-dir: tools/freesia
git-subtree-split: 0cd3976237543c8c972786efab8578bc2089c3d0
  • Loading branch information
Zeturic committed May 15, 2024
1 parent e0f8dd7 commit 85fc5c7
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@

integer = wraps(int)(partial(int, base=0))

FREE_WORD = b"\xff\xff\xff\xff"

def find_free_space(*, rom, needed_bytes, start_at=0):
start_at &= 0x01FFFFFF

# this adds 8 and then rounds up to a multiple of 4
# e.g. 1 -> 12 because 12 is the next multiple of 4 above 1+8
# e.g. 4 -> 12 because 4+8 is already a multiple of 4
adjusted = (needed_bytes + 11) & ~3
# round needed_bytes up to next multiple of 4
# e.g. 10 -> 12 because that's the next multiple of 4
# e.g. 16 -> 16 because it's already a multiple of 4
rounded = (needed_bytes + 3) & ~3

needle = b"\xff" * adjusted
needle = b"\xff" * rounded
pos = rom.find(needle, start_at)

while pos & 0b11 != 0 and pos != -1:
while (pos & 0b11 != 0 or not fenced(rom, pos, rounded)) and pos != -1:
pos = rom.find(needle, pos + 1)

if pos == -1:
return -1
return pos | 0x08000000

return (pos + 4) | 0x08000000
def fenced(rom, start, found_bytes):
stop = start + found_bytes + 1
lfenced = start == 0 or rom[start - 4: start] == FREE_WORD
rfenced = stop == len(rom) or rom[stop: stop + 4] == FREE_WORD
return lfenced and rfenced

def main(args=None):
argparser = ArgumentParser(description="Locates free space inside a GBA ROM.")
Expand Down

0 comments on commit 85fc5c7

Please sign in to comment.