Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2024-07-22 08:50:33

Clemmerson
New member
Registered: 2024-07-22
Posts: 1

Mysterious bank switch bug

Background:
I have created split const tables for items. The tables are what items can spawn at certain locations.
I have a massive switch case function to determine the location for the items.

This works until I bank switch to the table area in ROM. I was using a variable to switch banks but it failed sooner so I used the autobanking directly.

Code:

            case 7:
                table_bank = BANK(swamp_1);
                printf("Switching to bank: %d\n", table_bank); // Debugging output
                disable_interrupts();
                SWITCH_ROM_MBC5(BANK(swamp_1));
                //printf("Switched to bank: %d\n", BANK(swamp_1)); // Debugging output
                
                loot_table = swamp_skeleton_loot_table;
                loot_table_size = swamp_skeleton_loot_table_size;
                printf("Loot table size: %d\n", loot_table_size);
                delay(3000);
                //SWITCH_ROM_MBC5(function_bank);
                enable_interrupts();
                break;

https://i.ibb.co/cJ06TkR/First.png https://i.ibb.co/86ff5L3/Four.png https://i.ibb.co/Vg0FSDN/second.png https://i.ibb.co/Y06bjr7/Third.png


Let me know if you need additional information, presh any help.



I have failed to mention, the switch-case is part of a function in a different bank than the data tables.

I suspect, and I could be wrong, that when I switch from the bank containing the function to a bank with only table data the issue arrises where it can no longer execute the remaining data in the case from the stack ptr.

I am exploring ways to have a copy of the selectItemIndex function in each table bank as well.

Last edited by nitro2k01 (2024-07-26 15:23:12)

Offline

 

#2 2024-07-26 08:12:35

toxa
Member
Registered: 2020-02-13
Posts: 309

Re: Mysterious bank switch bug

you can not switch banks in the code which is in some bank as well. it is like trying to change the stool under your butt while you are sitting on it.

ps: this article explains rom bank switching on the game boy: https://laroldsjubilantjunkyard.com/tut … ory-banks/

Last edited by toxa (2024-07-26 08:15:19)

Offline

 

#3 2024-07-26 15:23:52

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 249

Re: Mysterious bank switch bug

Admin note: use code BBCode tags instead of triple backticks for encapsulating code on this forum.


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#4 2024-07-27 00:18:02

Tag365
Member
Registered: 2016-06-01
Posts: 44

Re: Mysterious bank switch bug

toxa wrote:

you can not switch banks in the code which is in some bank as well. it is like trying to change the stool under your butt while you are sitting on it.

ps: this article explains rom bank switching on the game boy: https://laroldsjubilantjunkyard.com/tut … ory-banks/

Yeah, switching the bank will immediately take effect, and make the next code you run from the new bank. Not accounting for this results in your code running at either in the correct address but the wrong bank, or both the wrong address and wrong bank, so it can cause a crash as you're likely running code not intended to be run in that situation.

For running code on a different bank while you're on bank one or above, you'll likely want some kind of function or routine in the main bank, bank zero, that is called to first switch banks then to switch to the correct address for the code you're executing. Then when it returns you want it to bring you back to the previous run time address in the prior bank. A similar concept is needed for code on bank one or higher intended to get data from a different bank, you need to get the data, then somehow return to both the correct bank and address in order to continue the code from where you left off.

Of course, you should reserve bank zero, the fixed bank, for any code that needs to be there in order to access code and data from other banks. Anything that could be implemented in another bank should not be placed in bank zero in order to keep space free for stuff that needs to be there. If you run out of space in bank zero, you should check for ways to refactor the code to not need as much code there, and ways to move data that isn't code into another bank.

Last edited by Tag365 (2024-07-27 00:30:28)

Offline

 

#5 2024-08-06 10:13:29

nitro2k01
Administrator
Registered: 2008-02-22
Posts: 249

Re: Mysterious bank switch bug

toxa wrote:

you can not switch banks in the code which is in some bank as well. it is like trying to change the stool under your butt while you are sitting on it.

Technically you could if you duplicated the same code across the source and destination bank, or just spliced them correctly. It's one of those situations where just because you can, doesn't mean you should, but you can. This is for example useful for the Wisdom Tree mapper, which switches out the whole 32k area and can start in any bank on power on.


Blog: Gameboy Genius
"A journey of a thousand miles begins with one small step"
Old Chinese Proverb

Offline

 

#6 2024-08-07 09:25:36

toxa
Member
Registered: 2020-02-13
Posts: 309

Re: Mysterious bank switch bug

you can not switch banks directly from the arbitrary C code which is located in some bank, while using GBDK-2020. neither you can use the wisdom tree mapper with GBDK-2020 as well out of the box.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson