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 2016-06-04 19:20:10

MrElephant
Member
Registered: 2014-01-29
Posts: 40

Switching Banks and Tileset Troubles

Hello All,

I am having trouble doing two things in gbdk.  Switching the tilesets when I switch banks,
and I am also having trouble with my tiles disappearing in the background when they overlap. (lighter colors vanishing when darker colors overlap)

In regards to the switching banks/background tiles I have found this information.
http://gbdk.sourceforge.net/doc/html/c0303.html
http://gbdk.sourceforge.net/doc/html/c01.html
http://gbdk.sourceforge.net/guidelines.html
http://gbdev.gg8.se/forums/viewtopic.php?id=207
http://gbdev.gg8.se/forums/viewtopic.php?id=175

In the last link Mills states that if you use SWITCH_ROM_MBC5( 1 ); --you are switching to the first rom bank from rom bank, so in theory
if I press the A button using this code I should be able to switch my tileset

SWITCH_ROM_MBC5( 1 ); 
if(key==J_A){
     set_bkg_data(0,500,bkgdata2);
    
     wait_vbl_done();                     //wait for the visual blank to be done
for(i=0 ; i<20 ; i++)
{
set_win_tiles(0);
set_bkg_tiles(0,0,20,16,map2);           
wait_vbl_done();
SHOW_BKG; 


This is how I am compiling my game
lcc -Wl-yo32 -Wl-ya32 -Wl-yt8 -Wf-bo1 -o bank1.o bank1.c
lcc -Wl-yo32 -Wl-ya32 -Wl-yt8 HyouryuuShounenkeith.gb HyouryuuShounenkeith.c


Can someone let me know what I am doing wrong on the background thing---and how to make lighter colors show
when overlapping darker ones.  I have looked at the banked example in the example folder--the makefile is really hard
to understand.

Thanks,

-MrElephant


Working in Gameboy BASIC.

Offline

 

#2 2016-06-06 11:01:00

Jonas
Member
From: Frankfurt, Germany
Registered: 2016-06-06
Posts: 112
Website

Re: Switching Banks and Tileset Troubles

Might the reason for your problem be that you try to load too many tiles with the set_bkg_data() function?

Have a look at this thread: http://gbdev.gg8.se/forums/viewtopic.php?id=282

Regards
Jonas

Offline

 

#3 2016-06-06 11:16:21

rychan
Member
From: Paignton, Devon, UK
Registered: 2015-12-16
Posts: 103
Website

Re: Switching Banks and Tileset Troubles

Yeah, I believe the limit would be 256, you can have 384 in total, arranged like so:

SPRITE TILES 0-127
BKG TILES 0-127
SPRITE or BKGTILES 128 - 255

Offline

 

#4 2016-06-06 12:55:05

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

Re: Switching Banks and Tileset Troubles

It looks like you're using the Memory Bank Controller 5 functions to switch ROM and RAM banks. Is "-Wl-yt8" the argument you use to set the MBC type to MBC5? I don't see any MBC5 value in the list of supported values for x in -Wl-ytx.

Offline

 

#5 2016-06-06 17:06:26

rychan
Member
From: Paignton, Devon, UK
Registered: 2015-12-16
Posts: 103
Website

Re: Switching Banks and Tileset Troubles

Is using MBC1 not a possibility instead? it's much easier to manage.

Offline

 

#6 2016-06-06 17:21:34

MrElephant
Member
Registered: 2014-01-29
Posts: 40

Re: Switching Banks and Tileset Troubles

I want to thank everyone first of all for getting back to me. 
@tag365
I used the -wl-yt8 method to try and compile 64 rom banks
According to Mills at this link http://gbdev.gg8.se/forums/viewtopic.php?id=175 he was able to store multiple images
using that compile method.

This is what I originally was trying to do
cd C:\gbdk\bin
lcc -Wl-yo32 -Wl-ya32 -Wl-yt3 -Wf-bo1 -o bank1.o bank1.c
lcc -Wl-yo32 -Wl-ya32 -Wl-yt3 HyouryuuShounenkeith.gb HyouryuuShounenkeith.c


After you brought this point up to me I did some more research into the matter, and I found this
http://www.devrs.com/gb/files/gbdok.html

"
Note! Using MBC5 and bankswitching seems to have a slight error in the code.

#define SWITCH_ROM_MBC5(b)
\*(unsigned char *)0x2000 = (b)&0xFF; \*(unsigned char *)0x3000 = (b)>>8
SWITCH_ROM_MBC5(b) switches the current changable 16k ROM to the bank specified by b.
"

@Jonas I have viewed that link, and the emulator is not giving me an error when I start the game; when I press the "a" button, nothing happens.
@rychan I tried doing what you said and put the tile limit down to 265, and nothing happened either.  I think it is something to do with how I am
compiling the game.


I feel utterly confused on the whole idea behind the bank switching idea now, and I am not getting anywhere with it. 


Are there any ideas behind my second question, about tile layers---sprites being above backgrounds?  I know you were able to accomplish this
rychan  with your car game, but your example code doesn't show different tiles layers.

Edit:  while making this post I saw that rychan posted again---I just need to get 18 pictures into the game--trying to rebuild a game that was
never released ---and all I have are the screenshots from it.

Last edited by MrElephant (2016-06-06 17:35:59)


Working in Gameboy BASIC.

Offline

 

#7 2016-06-06 17:45:54

rychan
Member
From: Paignton, Devon, UK
Registered: 2015-12-16
Posts: 103
Website

Re: Switching Banks and Tileset Troubles

How odd, Sprites are normally above background layers by default. You might have an odd attribute set somewhere perhaps? Not sure... though.


It does sound like a compile issue you have there perhaps. Not sure how much this will help but here's my make batch file for Formula racing, using MBC1 style switching (Bank 0 is fixed)

..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -c -o racemedo.o racemedo.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo1 -Wf-ba1 -c -o rombank1.o rombank1.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo2 -Wf-ba2 -c -o rombank2.o rombank2.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo3 -Wf-ba3 -c -o rombank3.o rombank3.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo4 -Wf-ba4 -c -o rombank4.o rombank4.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo5 -Wf-ba5 -c -o rombank5.o rombank5.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo6 -Wf-ba6 -c -o rombank6.o rombank6.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wf-bo7 -Wf-ba7 -c -o rombank7.o rombank7.c
..\bin\lcc -Wa-l -Wl-m -Wl-j -DUSE_SFR_FOR_REG -Wl-yt2 -Wl-yo8 -Wl-ya1 -yp0x143=0x80 -yp0x146=0x03 -yp0x14B=0x33 -o racemedo.gb racemedo.o rombank1.o rombank2.o rombank3.o rombank4.o rombank5.o rombank6.o rombank7.o


pause

Offline

 

#8 2016-06-06 17:56:04

Jonas
Member
From: Frankfurt, Germany
Registered: 2016-06-06
Posts: 112
Website

Re: Switching Banks and Tileset Troubles

MrElephant wrote:

Are there any ideas behind my second question, about tile layers---sprites being above backgrounds?  I know you were able to accomplish this
rychan  with your car game, but your example code doesn't show different tiles layers.

I'm not sure whether I understand the question correctly. Is your problem that you see parts of the backgound below sprites? If so, keep in mind that one of the four colors of a sprite is always "transparent". Which color that is can be changed by using OBP0_REG = [...].

rychan wrote:

Is using MBC1 not a possibility instead? it's much easier to manage.

That sounds sensible. If you don't have a huge game, use SWITCH_ROM_MBC1() instead of SWITCH_ROM_MBC5().

Offline

 

#9 2016-06-06 18:07:43

MrElephant
Member
Registered: 2014-01-29
Posts: 40

Re: Switching Banks and Tileset Troubles

My problem is that even the dark gray colour disappears below the black color.  Anything that is over black disappears.

Edit:  I think I got it, don't know what I was doing wrong.  Everything is appearing now, except the transparent white colour.

My question about the MBC1 is--will it be able to support the 18 pictures/tilesets?


Okay it is still not working---am I allowed to post my code on here so we can dissect it?  It might be a little long, and I don't know if
there is a way to put spoiler boxes on here.

2nd Edit:

Here are the files, if anyone wants to look--its a bit disorganized.  Will tidy it up a bit later.  Its on mediafire-just two c files.
https://www.mediafire.com/?wt590jhf48nt95c

Last edited by MrElephant (2016-06-07 00:45:33)


Working in Gameboy BASIC.

Offline

 

#10 2016-06-07 00:50:32

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

Re: Switching Banks and Tileset Troubles

rychan wrote:

Is using MBC1 not a possibility instead? it's much easier to manage.

No, it's not! Just because it has a lower number doesn't mean it easier. On the contrary, MBC1 is more complex on the software side in order to make the electronics in the chip simpler and cheaper to produce. That's why, on MBC1, only the lower 5 bits of the ROM address are selectable through the $2000 register, and the rest through a shared ROM/RAM selection register. On MBC5, you can address up to 256 ROM banks simply by writing a single value to $2000. Much easier!

For small ROMs (up to 512 kB) using only 8 kB of SRAM (or none), MBC1 and MBC5 are mostly compatible so choosing one over the other is a moot point anyway. Choosing one or the other will typically just work on both. For bigger ROMs or ROMs using more SRAM space, you should consider what happens when burning the ROM to a flash cartridge. While emulators will look at headers and adjust their hardware emulation accordingly, many flash carts will typically emulate a certain MBC and will have no idea what the ROM header says. In this case, using the wrong MBC may simply crash the program or load incorrect data when trying to access higher banks.


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

Offline

 

#11 2016-06-07 03:04:29

Tauwasser
Member
Registered: 2010-10-23
Posts: 160

Re: Switching Banks and Tileset Troubles

MrElephant wrote:

Okay it is still not working---am I allowed to post my code on here so we can dissect it?  It might be a little long, and I don't know if
there is a way to put spoiler boxes on here.

Took a quick look over your code.

Code:

int bank1(int i) BANKED  {
    ...
    if(key==J_B){ 
        SWITCH_ROM_MBC1(0); <-----------------------------
    }
}

You should never ever use SWITCH_ROM_MBCX in a bank other than 0 unless you know what you're doing. Now, with MBC1 you're lucky, because rom bank 0x00 cannot be selected, performing SWITCH_ROM_MBC1(0) has the same effect as SWITCH_ROM_MBC1(1) and in your case that means it has no effect, because your code is already in bank 0x01. On MBC5, SWITCH_ROM_MBC5(0) will actually map rom bank 0x00 to 0x4000-0x7FFF and code will just execute from the same address in bank 0x00, likely crashing and/or misbehaving.

Code:

int main(void){
    ...
    SWITCH_ROM_MBC1(1);
  
    //if(key==J_A){
        // set_bkg_data(0,255,bkgdata2);
        
        // wait_vbl_done();                     //wait for the visual blank to be done
        //for(i=0 ; i<20 ; i++)
        // {
            // set_bkg_tiles(0,0,20,16,map2);           //this displays the tile on line number 16  /*  x,y,w,h,tilenumber */
            //wait_vbl_done();
            //SHOW_BKG;
        // }
 
    if(key==J_RIGHT){           //if right button is down
        ...
    }

Your code assumes that key is already set to the button you hold down instantly. This isn't the case. You should actively wait for a key press or have a while loop around the whole key switch logic to make sure the user actually has time to press a button. Right now, your code is likely so fast, that you would basically have to start with button A held down to see the background data code executed.

Secondly, and more importantly, you actually exit the main function. Now, I'm not sure what kind of init code gbdk uses and if it catches this condition, but it is customary to never exit main ever, because even if this condition is handled at crt level, it's just as likely to restart main as to loop forever or outright crash.

Offline

 

#12 2016-06-07 04:09:47

rychan
Member
From: Paignton, Devon, UK
Registered: 2015-12-16
Posts: 103
Website

Re: Switching Banks and Tileset Troubles

nitro2k01 wrote:

rychan wrote:

Is using MBC1 not a possibility instead? it's much easier to manage.

No, it's not! Just because it has a lower number doesn't mean it easier. On the contrary, MBC1 is more complex on the software side in order to make the electronics in the chip simpler and cheaper to produce. That's why, on MBC1, only the lower 5 bits of the ROM address are selectable through the $2000 register, and the rest through a shared ROM/RAM selection register. On MBC5, you can address up to 256 ROM banks simply by writing a single value to $2000. Much easier!

For small ROMs (up to 512 kB) using only 8 kB of SRAM (or none), MBC1 and MBC5 are mostly compatible so choosing one over the other is a moot point anyway. Choosing one or the other will typically just work on both. For bigger ROMs or ROMs using more SRAM space, you should consider what happens when burning the ROM to a flash cartridge. While emulators will look at headers and adjust their hardware emulation accordingly, many flash carts will typically emulate a certain MBC and will have no idea what the ROM header says. In this case, using the wrong MBC may simply crash the program or load incorrect data when trying to access higher banks.

My apologies Nitro, I've not made a large enough ROM yet. sometime soon though!

Offline

 

#13 2016-06-07 22:12:10

MrElephant
Member
Registered: 2014-01-29
Posts: 40

Re: Switching Banks and Tileset Troubles

Thank you for responding everyone, and sorry that I am slow to respond today--was at my grandparents house, and I have no other choice in that matter-so that is that.

@Tauwasser
so if I switch back to mcb5 and take out the SWITCH_ROM_MBC5(0) while putting in the while loop I should be good?

What I hope to accomplish at the end of all of this is that when an overworld reaches a point offscreen, it switches to a different tileset.  When I get back home ill test what
tauwasser said.

I want to thank everyone again for all of the help they have given me; it is much appreciated.


Working in Gameboy BASIC.

Offline

 

#14 2016-06-09 00:43:44

MrElephant
Member
Registered: 2014-01-29
Posts: 40

Re: Switching Banks and Tileset Troubles

@tauwasser, I switched what you said, but I wasn't able to get anything to change.

Is my file setup wrong--having the banks in separate files--according to multiple sources I am allowed to contain
the program in multiple files.---I just don't understand why it isn't working.

Does anyone know of a Gameboy IRC?


Working in Gameboy BASIC.

Offline

 

#15 2016-06-09 22:30:41

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

Re: Switching Banks and Tileset Troubles

#gbdev on EFNet.


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

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson