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-22 18:38:00

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

How to read data from bank 1+ while in bank 1+ and not the same bank??

I'm developing a game on the Game Boy using GBDK. The game, which uses the MBC1 microchip, has more than 16 kilobytes of code and thus cannot fit entirely in bank zero. I have recently moved a few of the save functions to bank one, and I am already running out of space again on bank zero to fit code in. Most of the functions in the game switch to banks with tileset data and map data, in order to set up the background map.

Tauwasser wrote:

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.

So, how do you read data from other banks while executing in a bank higher than zero without crashing or malfunctioning?? Is it even possible to do that without crashing?!? As I already said, most of the functions in the game access banks with tileset data and map data. I want to somehow move most of the code in these functions to bank one or higher.

Offline

 

#2 2016-06-22 19:04:03

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

You call a function that's in bank 0 to do it for you. That function can then restore the original bank and return safely.


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

Offline

 

#3 2016-06-22 19:58:12

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

nitro2k01 wrote:

You call a function that's in bank 0 to do it for you. That function can then restore the original bank and return safely.

What if you're out of space in bank zero? Is it possible to get the data in other banks or is that the only way?

Offline

 

#4 2016-06-22 20:01:28

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Most likely, you can move something out of bank 0. Otherwise, you could theoretically move code into RAM and run it there. Maybe with feasible in C, but easy with asm.


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

Offline

 

#5 2016-06-23 07:51:13

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Yeah, err, what nitro2k01 said. Also consider that I have only once seen a commercial game fully use bank 0x00. If you somehow manage to have so much functionality in bank 0x00 that it's full, maybe you need to follow a different approach altogether.

Offline

 

#6 2016-06-23 09:24:48

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Tauwasser wrote:

Also consider that I have only once seen a commercial game fully use bank 0x00. If you somehow manage to have so much functionality in bank 0x00 that it's full, maybe you need to follow a different approach altogether.

Then what is the average size of bank zero in commercial games? I'm confused on how commercial games don't use bank zero fully, they must have code on other banks.

Offline

 

#7 2016-06-23 09:29:16

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Bank 0 is always 16 kiB but that doesn't mean you can't leave empty space in it.


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

Offline

 

#8 2016-06-24 16:10:43

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Most games just have a bunch of functions like

Code:

@1234:
push af
ld a, [CURBANK]
push af
ld a, $03
ld [$2000], a
call $5678
pop af
ld [$2000], a
pop af
ret

in ROM bank 0x00. Sure, some other stuff, too. But code that is very long and doesn't access other bank data frequently can just live somewhere else and might only need to be callable through bank 0x00. Then you have the generic "load byte from other bank" code that takes a bank, an address, and returns the byte there to a caller in another bank. It requires some planning, but it's usually very doable to cut down on bank 0x00 size.

Last edited by Tauwasser (2016-06-24 16:11:15)

Offline

 

#9 2016-06-25 14:56:19

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Great! Now I know how to make my games larger! Thank you for your information.

Offline

 

#10 2016-06-26 05:36:42

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Let's not forget the writes to CURBANK in that code!

Code:

@1234:
push af
ld a, [CURBANK]
push af
ld a, $03
ld [CURBANK],a
ld [$2000], a
call $5678
pop af
ld [CURBANK],a
ld [$2000], a
pop af
ret

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

Offline

 

#11 2016-06-30 07:39:58

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

Re: How to read data from bank 1+ while in bank 1+ and not the same bank??

Lol yeah, seems I futzed that up tongue

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson