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 2015-04-26 12:07:50

com
New member
Registered: 2015-04-26
Posts: 7

Bankswitching Problem

EDIT:
The following problem only exists on my virtual boy advance version. On other emulators or the DMG my game and the example don't crash.
_____________________________________________________________________________________

Hi, I'm programming a gb classic  game and use different banks for arrays in s-files. That wasn't a problem. Now i have to put c-code in banks and when i switch the bank to code from an other c-file the game crashes and shows gray bars without showing any compiling errors. When I compile the gbdk samplecode in examples/gb/banked I have the same problem without changing anything in the code or the makefile. It runs until the banked function gets called.
_____________________________________________________________________________________
SOLUTION

Tauwasser wrote:

Ok, apparently, the definition of "NONBANKED" changed over time. banks.c is a very old example.

NONBANKED will always put the function in question into section _HOME now, which is ROM bank 0x00. Remove all NONBANKED attributes and sdcc will put the code into the right bank.

If you set attribute

NONBANKED - put it into rom bank 0x00, call does not generate bank changing code
BANKED - put into bank (pragma or command line argument -Wf-boN), call does generate bank changing code
no attribute - put into bank (pragma or command line argument -Wf-boN), call does not generate bank changing code

cYa,

Tauwasser

com wrote:

It works smile After removing all the NONBANKs and BANKs ist running.
To call code from bank 3 when I am in bank 2 I call a function in bank 2 thats calling a function in bank 0 thats calling a function in bank 3. And the function in bank 0 passes on the variables and switches the banks on way to bank 3 and back.

So use SWITCH_ROM_MBC1("n") to swap to bank n und -Wf-bo"n" in the batch file only!
No #pragma bank="n".

Last edited by com (2015-04-30 17:20:19)

Offline

 

#2 2015-04-27 15:18:17

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

Re: Bankswitching Problem

What memory bank controller are you using? Are you switching from rom bank 0x00 or from another rom bank?

cYa,

Tauwasser

Offline

 

#3 2015-04-27 15:57:44

com
New member
Registered: 2015-04-26
Posts: 7

Re: Bankswitching Problem

I dont't know which bank controller I'm using.
- I'm using for example -Wf-bo3 -c -o bank_3.o bank3.c in my batch.
- Every file in banks begins with #pragma bank=3.
- Functions look like this: int bank3(int i) BANKED.
- To switch a bank i use SWITCH_ROM_MBC1(3);

The first call of the function in bank 3 (from bank 0) crashes the game. When I use SWITCH_ROM_MBC1(3); before or even if I don't use it like in the example.

This gbdk example switches from bank 2 directly to bank 3. Thaught it should work too if it's an example.

Do I understand it right?
If I use NONBANKED, is the code stored in other banks too, but can be called from bank 0 only? Didn't use it before.

Last edited by com (2015-04-27 16:12:45)

Offline

 

#4 2015-04-27 17:53:34

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

Re: Bankswitching Problem

You use MBC1 if you call SWITCH_ROM_MBC1(3). I had trouble setting up gbdk 2.95 on Windows. It would not compile anything for me if the files contained "BANKED" instead of "banked" for some reason. Support is for an older rgbds version... Anyway, I successfully built examples/gb/banked and ran it.

http://i1290.photobucket.com/albums/b527/tauwasser/bgb00001_zps408vgnas.png

The only trouble was that the Makefile never sets the game title to anything sensible, so 0x0143 ends up being 0xFF == Game Boy Color only. Since none of the examples set up the colors, the text isn't visible. Edit your image by hand using a hex editor and make sure 0x0143 is 0x00. Make sure header checksum is correct, i.e. rgbfix -v a.gb.

Beyond that, debug in BGB. The examples/gb/banked stuff does return to rom bank 0x00 for every banked call, so everything ends up being fine.

cYa,

Tauwasser

Last edited by Tauwasser (2015-04-27 17:54:37)

Offline

 

#5 2015-04-27 18:23:25

com
New member
Registered: 2015-04-26
Posts: 7

Re: Bankswitching Problem

Damn, no, I get this screen when I call any banked function...
https://dl-web.dropbox.com/get/Share/bars.png?_subject_uid=113379696&w=AAALggATeCyNSxIIeyBtnO4GkPU8ttBUEUzp0gk3m1TbRQ

There is an other example examples/gb/banks.c. Does bank_3() get stored in baks 3?

The compiler throws this warning, if I put enough data into bank 0 and bank 3 to fill one bank, but not to fill both banks:
...
WARNING: possibly wrote twice at addr 410b (2A->07)
WARNING: possibly wrote twice at addr 410c (6E->C8)
WARNING: possibly wrote twice at addr 410d (67->07)
...

banks.c:

  SWITCH_ROM_MBC1(3);
  bank_3();

bank_3.c:

void bank_3() NONBANKED /* In ROM bank 3 */
{
  puts("I'm in ROM bank 3");
}

Last edited by com (2015-04-27 18:54:01)

Offline

 

#6 2015-04-27 19:39:57

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

Re: Bankswitching Problem

Your dropbox picture is private.

Haven't had time to play around with examples/gb/banks yet, but I think everything was assembled/compiled without errors. This is what I get:

http://i1290.photobucket.com/albums/b527/tauwasser/bgb00001_zpstxnv17xj.pnghttp://i1290.photobucket.com/albums/b527/tauwasser/bgb00003_zpst099ff3g.png

The right image is after adding the following code to banks.c:

Code:

void main(void)
{
  puts("Program Start...");

 /* enable MBC1 mode 0x01 (MODE_4M_256k) */ <----
 *(unsigned char *)0x6000 = 0x01u;          <----

  ENABLE_RAM_MBC1;
  var_internal = 1;

For some reason, MBC1 is never switched into the correct mode to do RAM banking, so naturally, it doesn't work (left image). With the fix, it works as expected (right image), although the sym file is still produced incorrectly:

Code:

; Area: _DATA_0
00:A000 _var_0
; Area: _DATA_1
00:A000 _var_1
; Area: _CODE_1
; Area: _DATA_2
00:A000 _var_2
; Area: _CODE_2
; Area: _DATA_3
00:A000 _var_3
; Area: _CODE_3

All variables are in bank 0x00, while in reality, they're obviously not. Not sure where the problem is with that ATM. Possibly a back-annotation problem, as the linker might not be able to tell what banking options the individual object files were compiled with.

EDIT:

com wrote:

There is an other example examples/gb/banks.c. Does bank_3() get stored in baks 3?

I double-checked and you're right. The rom banks (bank_1.c, bank_2.c etc) are also put in rom bank 0x00, despite the compiler flags. A #pragma bank=N (N = 1..3) didn't help either.

cYa,

Tauwasser

Last edited by Tauwasser (2015-04-27 19:53:09)

Offline

 

#7 2015-04-27 19:49:14

com
New member
Registered: 2015-04-26
Posts: 7

Re: Bankswitching Problem

This example runs on my pc too. But the functions are stored in bank 0, even if it says "I'm in ROM bank 3", aren't they? My problem is, that my code (functions, not the data like variables) is too long for one bank. When I split the code into several banks by using BANKED, the game crashes. If I use NONBANKED like in this example, the function void bank_3() NONBANKED is stored in bank 0. Or if they dosen't, the the game crashes with a white screen when i produce more code than 16kb, even if it's spread into varios banks.

Offline

 

#8 2015-04-27 20:44:22

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

Re: Bankswitching Problem

Ok, apparently, the definition of "NONBANKED" changed over time. banks.c is a very old example.

NONBANKED will always put the function in question into section _HOME now, which is ROM bank 0x00. Remove all NONBANKED attributes and sdcc will put the code into the right bank.

If you set attribute

NONBANKED - put it into rom bank 0x00, call does not generate bank changing code
BANKED - put into bank (pragma or command line argument -Wf-boN), call does generate bank changing code
no attribute - put into bank (pragma or command line argument -Wf-boN), call does not generate bank changing code

cYa,

Tauwasser

Last edited by Tauwasser (2015-04-27 20:46:23)

Offline

 

#9 2015-04-27 21:25:54

com
New member
Registered: 2015-04-26
Posts: 7

Re: Bankswitching Problem

It works smile Thank you so munch! After removing all the NONBANKs and BANKs ist running.
To call code from bank 3 when I am in bank 2 I call a function in bank 2 thats calling a function in bank 0 thats calling a function in bank 3. And the function in bank 0 passes on the variables and switches the banks on way to bank 3 and back.
Maybe you have a solution for an other little problem too. While I load tiledata into vram I want the music to go on. Using wait_vbl_done(); would slow down this process too much. And when I do it only every 7-8 tile the music slows down too. So I count down to get nearly consistent framerate. But thats not perfect at all. Do you know a methode to access the system clock or have an other solution?

Offline

 

#10 2015-04-28 15:59:54

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

Re: Bankswitching Problem

Hmm, this sounds like you manage banking by hand. That's not the intention of having a C compiler which does it for you. You should have declared and defined the functions as BANKED, then a call from bank2 to bank3 should automatically switch to a pre-defined routine that handles the bank switching and argument passing.

As for music, you can use the timer or vblank itself to handle it. I think right now you do it in your mainloop in software, which is bound to be called at uneven intervals.

cYa,

Tauwasser

Offline

 

#11 2015-04-28 20:42:06

com
New member
Registered: 2015-04-26
Posts: 7

Re: Bankswitching Problem

Using banked crashes my game. It works for me switching banks by hand.
Music is perfect now. Starting the music routine from interrupt-handler produces screen glitches. Counting up a counter during interrupt call and calling the music-handler, while the counter is greater than 0 works perfectly.

Offline

 

#12 2015-04-29 15:01:22

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

Re: Bankswitching Problem

com wrote:

Using banked crashes my game. It works for me switching banks by hand.

At this point, you probably need to show your code in order for anybody to be able to solve it.

com wrote:

Music is perfect now. Starting the music routine from interrupt-handler produces screen glitches.

Then your music routine takes up a considerable amount of time. Use an easier music format that doesn't involve many calculations in order to play music and this should be solved.

cYa,

Tauwasser

Offline

 

#13 2015-04-29 20:17:07

com
New member
Registered: 2015-04-26
Posts: 7

Re: Bankswitching Problem

Found the problem smile I used Visual Boy Advance. On all other emulator and my DMG the game dosen't crash.
For music I use gbt player.
By the way, do you use gbt? Is there an option to fade out tones like for example in lsdj or do I have to write multiple tones with the descending volume to the mod-file?
EDIT: Ah, found a version with doc smile

Last edited by com (2015-05-02 08:03:22)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson