Automatic ca65/ld65 bank chooser

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
NovaSquirrel
Posts: 392
Joined: Fri Feb 27, 2009 2:35 pm
Location: Fort Wayne, Indiana
Contact:

Automatic ca65/ld65 bank chooser

Post by NovaSquirrel » Fri Jan 17, 2020 10:33 pm

After playing with RGBDS for Game Boy Color development, I noticed that it lets you skip specifying what bank a section is located in entirely and it just tries to pack it somewhere where there's room.

Because I found it super useful, I wrote a Python script to shove that feature into ca65/ld65. For any segments not declared in your linker config, it scans through your object files with od65 to find out how big those segments actually are, and splices a new segment declaration into the linker config that puts the segment somewhere where there's room, using the first-fit decreasing strategy.

The script has a lot of stuff hardcoded but I think it could easily be adapted to 16KB bank mappers like UNROM, rather than just LoROM SNES games. There's probably a lot of problems I don't solve yet (like I don't know how aligns play into segment sizes), and I had some difficult-to-solve issues when putting things in the bank the vectors reside in, so it doesn't try to allocate anything in there right now. You also can't yet specify that specific segments must go in the same bank together, so in that case they must either be renamed to be the same thing or they should be manually placed.

Ideally it'd be cool to have this sort of feature directly in ld65!

User avatar
gauauu
Posts: 700
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Automatic ca65/ld65 bank chooser

Post by gauauu » Mon Jan 20, 2020 11:42 am

Does it do anything to help you when switching to the bank you want? Since you don't know where a segment will end up?

User avatar
rainwarrior
Posts: 7824
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Automatic ca65/ld65 bank chooser

Post by rainwarrior » Mon Jan 20, 2020 5:28 pm

gauauu wrote:
Mon Jan 20, 2020 11:42 am
Does it do anything to help you when switching to the bank you want? Since you don't know where a segment will end up
A SEGMENT has a "bank" attribute, which is exactly the right tool that purpose. See .bank.

E.g.

Code: Select all

lda #.bank(mything)
sta $8000
jsr mything

User avatar
gauauu
Posts: 700
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: Automatic ca65/ld65 bank chooser

Post by gauauu » Mon Jan 20, 2020 9:39 pm

rainwarrior wrote:
Mon Jan 20, 2020 5:28 pm
gauauu wrote:
Mon Jan 20, 2020 11:42 am
Does it do anything to help you when switching to the bank you want? Since you don't know where a segment will end up
A SEGMENT has a "bank" attribute, which is exactly the right tool that purpose. See .bank.

E.g.

Code: Select all

lda #.bank(mything)
sta $8000
jsr mything
Huh. I'm wondering how I missed this for so long. Thanks.

tepples
Posts: 22017
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Automatic ca65/ld65 bank chooser

Post by tepples » Mon Jan 20, 2020 10:39 pm

Though on 65816 you're more likely to use .bankbyte(addr) or ^addr, both of which mean ((addr >> 16) & $FF).

Post Reply