How to implement swappable banks with CC65/LD65?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
Yoshimaster96
Posts: 16
Joined: Sat Jan 09, 2016 5:30 pm

How to implement swappable banks with CC65/LD65?

Post by Yoshimaster96 »

What it says on the tin. I dunno how to set up the linker script so it works with multiple banks, each with their own .c file.

What I have is this (it's for a custom 6502-based system with RAM at $0000-$1FFF and $6000-$7FFF):

Code: Select all

MEMORY {
    ZP: start = $00, size = $100, type = rw;
    BSS: start = $200, size = $1E00, type = rw;
    BSS2: start = $6000, size = $2000, type = rw;
    HEADER: start = $0000, size = $10, file = %O;
    BANK0: start = $8000, size = $4000, file = "bank/b0.bin";
    BANK1: start = $8000, size = $4000, file = "bank/b1.bin";
    BANK2: start = $8000, size = $4000, file = "bank/b2.bin";
    BANK3: start = $8000, size = $4000, file = "bank/b3.bin";
    BANK4: start = $8000, size = $4000, file = "bank/b4.bin";
    BANK5: start = $8000, size = $4000, file = "bank/b5.bin";
    BANK6: start = $8000, size = $4000, file = "bank/b6.bin";
    BANK7: start = $8000, size = $4000, file = "bank/b7.bin";
}
SEGMENTS {
    EXEHDR: load = HEADER, type = wprot;
    CODE0: load = BANK0, type = wprot, define = yes;
    CODE1: load = BANK0, type = wprot, define = yes;
    CODE2: load = BANK0, type = wprot, define = yes;
    CODE3: load = BANK0, type = wprot, define = yes;
    CODE4: load = BANK0, type = wprot, define = yes;
    CODE5: load = BANK0, type = wprot, define = yes;
    CODE6: load = BANK0, type = wprot, define = yes;
    CODE7: load = BANK0, type = wprot, define = yes;
    ZEROPAGE: load = ZP, type = zp;
    BSS: load = BSS, type = bss;
    BSS2: load = BSS2, type = bss;
}
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: How to implement swappable banks with CC65/LD65?

Post by rainwarrior »

Maybe you've already caught this type but those load = should point to BANK1, BANK2, etc.

Make sure the CRT stuff goes in a fixed bank. I'm assuming you're using UxROM style banking? (16k banks $8000, 16k fixed at $C000). Make BANK7 start at $C000, and load CODE + DATA + RODATA segments there. This will be your fixed bank, and the CRT library stuff uses those segments by name already. Don't let your banked C code use those segments.

After that, make sure to use #pragma code-name / data-name / rodata-name in all your banked C code to put them into the CODE0, CODE1, etc. segments.

Finally, you will have to manually bankswitch before calling any of your banked code... or actually I think calima implemented a way to automate it, but you'll need to write an assembly wrapper for it: https://forums.nesdev.com/viewtopic.php?f=2&t=15959
Post Reply