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.
There are quite a few threads about bank switching in general and using GBDK in particular. First, do you use GBDK or rgbds? In case of GBDK, the example directory contains some example bank switching code, which is unfortunately outdate, see this thread. For RGBDS, you can check out the GitHub documentation, which is sparse, but useful.
Offline
I'm using GBDK. I don't really know what to do. What code do I put in my game? What should I type in when I compile? What I was planning to do was make a 1MB game instead of a 32k game.
Last edited by chris (2017-08-13 04:24:44)
Offline
You might be better reading how bankswitching works on the GB. I'll try giving you a crash course here.
Since the ROM is larger than what the GB can access, inside all >32k cartridges is a chip that allows ROM banking. Most cartridges (AFAIK) use a type of chip known as the MBC (Memory Bank Controller), Nintendo's official chip.
There are a couple chips, some of them are documented here.
I don't know how you do this with GBDK, but you should pick
- Which chip you want (usually : MBC3 if planning to use RTC features, MBC5 otherwise)
- How many banks you will use (see this chart
... and then write to the appropriate memory location to perform the bankswitch.
Offline
I got the 64k ROM working good now, but how do I make it 128k (1 MB)? I want to tell it to use 8 banks.
How will I know when I run out of room and need to use a different bank?
Offline
So I got a 128k file, but I use 8 ROM banks and 4 RAM banks. It told me 8 RAM banks wasn't usable. I don't know what a RAM bank is. I know RAM stands for "random access memory," and ROM stands for "read-only memory," but I don't know why it's called Random access. What is random about it?
bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -c -o test.o ftff20.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo1 -Wf-ba1 -c -o bank_1.o bank_1.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo2 -Wf-ba2 -c -o bank_2.o bank_2.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo3 -Wf-ba3 -c -o bank_3.o bank_3.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo1 -Wf-ba1 -c -o bank_3.o bank_3.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo2 -Wf-ba2 -c -o bank_3.o bank_3.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo3 -Wf-ba3 -c -o bank_3.o bank_3.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo3 -Wf-ba3 -c -o bank_3.o bank_3.c bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wl-yt5 -Wl-yo8 -Wl-ya4 -o ftff21.gb test.o bank_1.o bank_2.o bank_3.o bank_3.o bank_3.o bank_3.o bank_3.o pause
Offline
RAM banks, in this case, refer to SRAM banks : the number of banks of RAM that are present in the (emulated) cartridge that contains your game.
There are only a select number of SRAM banks you can use, which is further restricted by which MBC you're using.
As for why RAM is "random", Wikipedia is your friend.
Offline
If you start seeing messages like "WARNING: possibly wrote twice at addr 42ce (20->21)", or if you get an error message like "ERROR: address overflow (addr 8853 >= 8000)" and your resulting ROM image is blanked out, then you have filled up a ROM bank and are now wrapping around to the beginning of the ROM bank. If this occurs you need to either start shrinking code or data or move some code or data to another ROM bank.
To change the amount of ROM banks in your game change the number in "-Wl-yo8" to a different multiple of two. Choose between 4, 8, 16, 32, 64, 128, 256, or 512 ROM banks.
The maximum amount of ROM banks you can ever possibly have in a Game Boy game is 512 banks of 16,384 bytes of data, allowing you to store up to 8 megabytes of data, or 8,388,608 bytes (this requires the MBC5 memory bank controller though). This may not seem like much, however almost all Game Boy games use less than 512 banks of ROM data. I have not yet seen a Game Boy game ROM file that is actually eight megabytes in size. For example, the English version of Pokémon Crystal Version has 128 banks of ROM data equating to 2 megabytes of ROM data. I don't think anyone will actually even reach the maximum ROM size possible of 512 banks.
You can also add a "-Wl-ya4" to the line which has "-Wl-yo8" to add SRAM (Save RAM) to your game. The maximum amount of SRAM banks is 16 banks of 8,192 bytes of data, allowing you to save up to 128 kilobytes of data equal to 131,072 bytes of data (this requires the MBC5 memory bank controller as well). This SRAM can be used for any purpose, and a battery can be added to save the SRAM data across restarts, allowing you to add a save game feature to your game, or add. Of course you don't need to have SRAM if you aren't going to save data across multiple sessions.
You are not forced to use a certain MBC for your game, but each MBC works differently so read the wiki article about the MBC controller to see a general idea of how you're supposed to access it.
By the way 128 kilobytes does not equal 1 megabyte, it equals 1 megabit of data (1 megabyte divided by eight).
Offline
I got messages that say "WARNING: possibly wrote twice at addr 40xx", where xx is between 00-1d, and the ROM works fine. What is wrong? My code can be downloaded in the link in my signature.
Offline
All I want to do is compile a .gb file with 2 separate banks, but god forbid I be able to do that. I keep getting files with nothing in them. How do I make this? Please help me.
Here are the two files I want together.
http://www.atari2600land.com/gameboy/help.zip
Offline
Sorry for not replying quickly, but I haven't come on the forum in a while. The "WARNING: possibly wrote twice at addr 40xx" messages mean that you have filled up a ROM bank and are now wrapping around to the beginning of the ROM bank. If this occurs you need to either start shrinking code or data or move some code or data to another ROM bank.
Last edited by Tag365 (2017-10-17 22:04:54)
Offline
I don't know how to bankswitch. I looked at various codes, tried everything I could possibly think of and nothing worked. What am I doing wrong? How do I do this?
http://www.atari2600land.com/gameboy/tr … switch.zip
Offline
It appears that you're putting too much data into the first bank, making it spill into the second bank... I haven't coded games for the Game Boy in a while, so I will probably need to get used to it again before I can help. Also, I'm more interested in improving my wiki, more specifically at this time trying to create some new infoboxes for my wiki... so it will have to wait. Sorry.
Last edited by Tag365 (2017-10-20 14:44:00)
Offline
chris wrote:
I got messages that say "WARNING: possibly wrote twice at addr 40xx", where xx is between 00-1d, and the ROM works fine. What is wrong? My code can be downloaded in the link in my signature.
I currently don't have the time to have a closer look at your code (which at first glance looks quite complex). But as far as I remember, that particular error can occur if you don't declare your variables and functions properly. If you want to access a variable or function that is declared in another C file that goes to a different bank, you have to declare it as "extern" in the file that uses the variable or function. Did you do that?
For example, if you declare a UINT8 variable that is called MyNumber in Bank2.c (which goes to bank 2) and you want to use that variable in Bank0.c (which goes to bank 0), it must look like this:
Declare the variable in Bank2.c:
UINT8 MyNumber;
And include this declaration in Bank0.c:
extern UINT8 MyNumber;
Offline
So at least you managed to compile it.
There are countless possible reasons for the game freezing. The most probable in relation to bank switching are:
- You tried to call a function that is located in a ROM bank other than bank 0 and that bank is not switched in.
- You have code in your game that switches banks and isn't located in bank 0. That won't work. All bank switching instructions must be located in the code that goes to ROM bank 0.
- You have code in one of the upper banks that tries to access functions or data located in one of the other upper banks. That won't work either. The code in the upper ROM banks can only access functions or data which are either in the same ROM bank or in ROM bank 0.
Last edited by Jonas (2017-10-22 07:50:12)
Offline