RPGHacker / asar

(Now) official repository of the SNES assembler Asar, originally created by Alcaro
Other
195 stars 42 forks source link

Freespace finder is broken when RATS tags cross banks #292

Closed spooonsss closed 6 months ago

spooonsss commented 7 months ago

Asar cb78f31f8

freespace-1.asm

macro rats(length)
  db $53, $54, $41, $52    ;  STAR
  dw <length>
  dw <length>^$FFFF
endmacro

org $108000
%rats($7000)

org $10FF00
%rats($F000)

org $129000
db "This is in %rats($F000)"

org $1F8FFF
db 0

freespace-2.asm

freecode
rep $2000 : db $55
db "This is the end of freecode"
dd if=SMW.smc of=SMW.sfc bs=512 skip=1
asar_symbols.cmd  freespace-1.asm  smw.sfc
\cygwin64\bin\hexdump.exe -C smw.sfc > 1
asar_symbols.cmd  freespace-2.asm  smw.sfc
\cygwin64\bin\hexdump.exe -C smw.sfc > 2

The freecode block is placed inside the second manual RATS tag:

00080000  53 54 41 52 00 70 ff 8f  00 00 00 00 00 00 00 00  |STAR.p..........|
00080010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00087f00  53 54 41 52 00 f0 ff 0f  00 00 00 00 00 00 00 00  |STAR............|
00087f10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00087ff0  00 00 00 00 00 00 00 00  53 54 41 52 1a 20 e5 df  |........STAR. ..|
00088000  55 55 55 55 55 55 55 55  55 55 55 55 55 55 55 55  |UUUUUUUUUUUUUUUU|
*
0008a000  54 68 69 73 20 69 73 20  74 68 65 20 65 6e 64 20  |This is the end |
0008a010  6f 66 20 66 72 65 65 63  6f 64 65 00 00 00 00 00  |of freecode.....|
0008a020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00091000  54 68 69 73 20 69 73 20  69 6e 20 25 72 61 74 73  |This is in %rats|
00091010  28 24 46 30 30 30 29 00  00 00 00 00 00 00 00 00  |($F000).........|
00091020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000f9000

Looks like this code won't work correctly without checking for RATS tags:

if (
        ((start+8)&~banksize)!=((start+size-1)&~banksize&0xFFFFFF)//if the contents won't fit in this bank...
    &&
        (start&banksize&0xFFFFF8)!=(banksize&0xFFFFF8)//and the RATS tag can't fit in the bank either...
    )
{
    start&=~banksize&0xFFFFFF;//round it down to the start of the bank,
    start|=banksize&0xFFFFF8;//then round it up to the end minus the RATS tag...
    continue;
}
randomdude999 commented 6 months ago

i've messed around with freespaces a bunch in asar 2.0, and as far as i can tell this issue does not reproduce there anymore.