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 2017-06-04 18:49:50

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

using asm files in combonation with a C game

So I've written a game in C. I want to display a couple of large background images. I can get one to display but anymore causes this problem: http://gbdev.gg8.se/forums/viewtopic.php?id=340

I hear this can be fixed by loading these image.c files as asm files instead, how do I do that?

Offline

 

#2 2017-06-05 11:48:52

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: using asm files in combonation with a C game

I doubt a data array will take less space in asm than in C unless there's a big bug on GBDK (which is posible)

Offline

 

#3 2017-06-05 12:37:27

Zalo
Member
From: Spain
Registered: 2016-07-26
Posts: 103
Website

Re: using asm files in combonation with a C game

I just remembered... Are you declaring the array const? The Game Boy Map Builder doesn't do this by default but it is necessary to store it on the proper bank in the Rom, otherwise it goes to the Ram

Offline

 

#4 2017-06-05 15:36:06

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

I'll check this thanks.

Yes, they're constant. I also tried putting them in the main C file rather than as imports but nothing happened. In a different thread someone else said that putting their arrays as assembly rather than C fixed the problem, that's why I asked. So could someone please actually answer the question.

Last edited by jobalisk (2017-06-05 15:53:25)

Offline

 

#5 2017-06-06 03:58:03

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

Re: using asm files in combonation with a C game

The error message you got indicates that too much data is stored in a ROM bank which causes a bank overflow. Generally, the solution to that problem is to implement bank switching. Like Zalo, I doubt whether declaring arrays in assembly will help since that shouldn't have an impact on the size of your code.

However, how to declare arrays in assembly and use them in a game that is written in C is explained here:

http://gbdev.gg8.se/forums/viewtopic.php?id=91
http://gbdev.gg8.se/forums/viewtopic.php?id=145

Offline

 

#6 2017-06-06 15:19:40

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

ok, that's for that. Before I try it out, can you link me how to do bank switching as well, I'm kinda a nub here when it comes to C for gameboy

Offline

 

#7 2017-06-06 18:05:56

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

Offline

 

#8 2017-06-06 20:01:03

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

Thanks, ill check these out sometime later in the week when I get back to my PC.

Offline

 

#9 2017-06-10 20:46:27

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

Ok, so just trying to work out compiling my files, I keep getting this error when compiling the second bank.

https://preview.ibb.co/fbfskv/Capture.jpg

Here's the code:

#include <gb.h>
#include "story2.dat"
#include "alphabet.c"
//part 2 of the intro story as a seperate file
void storyPart2(void){
   

    set_bkg_data(0,255, story2_tile_data);
    set_bkg_tiles(0,0,16,16,story2_map_data);

   
}

void alphabetset(void){
    set_bkg_data(0,255, alphabeto);
}

Offline

 

#10 2017-06-11 13:39:29

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

Re: using asm files in combonation with a C game

Probably you didn'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.

For example, if you declare a UINT8 variable that is called MyNumber in advojoboB.c (which goes to bank 2) and you want to use that variable in advojobo.c (which goes to bank 0), it must look like this:

Declare the variable in advojoboB.c:

Code:

UINT8 MyNumber;

And include this declaration in advojobo.c:

Code:

extern UINT8 MyNumber;

Also, as far as I remember, you have to include gb.h and stdio.h separately in all banks.

Offline

 

#11 2017-06-11 15:11:22

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

that's the thing though, there are no variables declared in the code, the entire thing is whats up above. OK, Solved it, changing it to gb/gb.h fixes the problem.

Last edited by jobalisk (2017-06-11 15:24:18)

Offline

 

#12 2017-06-11 15:30:25

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

...So I finally get it all ready to compile as separate files for multiple banks then when I hit compile I get the same old overflow message...

Assuming I have two files advojobo.c and advojoboB.c and that they are written correctly, what would be the way to compile them so that advojoboB.c is in a separate memory bank, I've tried reading through the documentation above but I don't seem to be getting anywhere.

Last edited by jobalisk (2017-06-11 15:35:42)

Offline

 

#13 2017-06-11 16:41:36

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

Re: using asm files in combonation with a C game

About like this:

Code:

c:\GBDK\bin\lcc -Wa-l -Wl-m -c -o advojobo.o advojobo.c
c:\GBDK\bin\lcc -Wa-l -Wl-m -Wf-bo2 -c -o advojoboB.o advojoboB.c
c:\GBDK\bin\lcc -Wa-l -Wl-m -Wl-j -Wl-yt0x01 -Wl-yo16 -o advojobo.gb advojobo.o advojoboB.o
pause

Offline

 

#14 2017-06-12 14:54:03

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

rats, that still generates the overflow error. I must have some code wrong somewhere or something.

Offline

 

#15 2017-06-12 18:15:42

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

Re: using asm files in combonation with a C game

Well, then you have to try and isolate your error. You wrote that you tried to include data for one background image (which worked) and then for more than one background image (which didn't work). Try this:

- Remove some of the data for the second background image to make it smaller. First remove some bytes, then more and finally only leave some bytes in the array.
- Include some other array instead of the second background image.
- Include the second image and remove the first instead.
- Include both images and remove some other code from your file.
- etc.

If you know which part of the code causes the problem, you can post it here. Maybe somebody will have an idea what's wrong with it.

And one more thing that might possibly cause the problem: If you place code in one of the upper ROM banks, you must not include the files that go to the upper ROM banks in the file that goes to ROM bank 0. In your example, that means: Don't put an #include "advojoboB.c" in beginning of advojobo.c.

Offline

 

#16 2017-06-13 04:15:09

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

well I haven't referenced the second file in the first. I tried making the images smaller a while back which allowed me to use 2 rather than one, but the smaller images lack impact. Besides they are only 128*128 to begin with. I'm confused because I can fit 1 128*128 image as standard in the ROM bank 0 so I'd assume I can fit 1 in the second ROM bank. I'll have a poke around switching things out and see what happens.

UPDATE

Ok, so I tried commenting out the first image and the custom alphabet I had made then just had story 2 loading from the second ROM. However this still caused an overload error even though the image will load fine if I include it in the main file... I'm a bit confused here.

I'll post the code below

B:


#include <stdio.h>
#include <gb/gb.h>
#include "story2.dat"
#include "alphabet.c"
//part 2 of the intro story as a seperate file
void storyPart2(void){
   

    set_bkg_data(0,255, story2_tile_data);
    set_bkg_tiles(0,0,16,16,story2_map_data);

   
}

void alphabetset(void){
    set_bkg_data(0,255, alphabeto);
}



Here's the relevant part of A:


//Cold Autumn Fire
//Job Dyer 27/05/2017

#include <stdio.h>
#include <gb/gb.h>
#include "jobalisk.c"
#include "ground.c"
#include "advojobomap.c"
#include "blank1.c"

#include "story1.dat"


unsigned int gameLoop = 1; //game loop, duh
unsigned int walkAlt = 0; //are we using the left or right sprite to walk with ect.
unsigned int direction1 = 0; //direction the player is facing. 0 is DOWN, 1 is LEFT, 2 is RIGHT, 3 is UP
unsigned int sp1x = 75; //X coordinates for Player 1's first sprite
unsigned int sp1y = 80; //Y coordinates for Player 1's first sprite
unsigned int sp2x = 83; //X coordinates for Player 1's first sprite
unsigned int sp2y = 80; //Y coordinates for Player 1's first sprite

unsigned int sp1xbk = 75; //X coordinates for Player 1's first sprite backup
unsigned int sp1ybk = 80; //Y coordinates for Player 1's first sprite backup
unsigned int sp2xbk = 83; //X coordinates for Player 1's first sprite backup
unsigned int sp2ybk = 80; //Y coordinates for Player 1's first sprite backup

unsigned int bkgndX = 0; //X coordinate for the Background (for scrolling ect)

unsigned int playerSpeed = 4; //players walking speed
unsigned int spriteDelay = 100; //delay time between switching sprites
unsigned int majorActionDelay = 200; //delay for things like jumping, punching, kickingm ect
unsigned int playerHealth = 6; //health the player has
unsigned int playerPunchD = 1; //how much dammage a punch does
unsigned int playerKickD = 2; //how much dammage a kick does
unsigned int PlayerKO = 0; //is the player KOed?
unsigned int isJumping = 0; //int for if the player is jumping
unsigned int isPunching = 0; //int for if the player is punching (so it cant just stay punched)
unsigned int isKicking = 0; //int for if the player is kicking
unsigned int isBlocking = 0; //int for if the player is in block mode
unsigned int isCrouching = 0; //int for if the player is in crouch mode.
unsigned int jumpTester = 0; //seeing how high we have jumped so far
unsigned int maxJumpHight = 20; //maximum hight for the jump
unsigned int groundLevel = 75; //default position taken from begining of the jump.
unsigned int jumpSpeed = 2; //speed at which the player jumps.


void storyPart2(); //preload the story loader


void ResetGame() //resets all variables
{
    gameLoop = 1;
    walkAlt = 0;
    direction1 = 0;
    sp1x = 75;
    sp1y = 80;
    sp2x = 83;
    sp2y = 80;
    sp1xbk = 75;
    sp1ybk = 80;
    sp2xbk = 83;
    sp2ybk = 80;
    playerSpeed = 4;
    spriteDelay = 100;
    majorActionDelay = 200;
    playerHealth = 6;
    playerPunchD = 1;
    playerKickD = 2;
    PlayerKO = 0;
    groundLevel = 75;
    isJumping = 0;
    isPunching = 0;
    isKicking = 0;
    isBlocking = 0;
    isCrouching = 0;
    jumpTester = 0;
    bkgndX = 0;
}

void clsrn()
{//screen clearer
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
void gameOver()
{
    HIDE_SPRITES;
    set_bkg_data(0,0, NULL);
    set_bkg_tiles(0,0,32,32,NULL);
    clsrn();
    printf("\n\n\n\n\n\n     GAME OVER!\n\n"); //run the game over message
    waitpad(J_A);
    ResetGame();

}
void gameWon()
{
    HIDE_SPRITES;
    set_bkg_data(0,1, NULL);
    set_bkg_tiles(0,0,32,32,NULL);
    clsrn();
    printf("\n\n\n\n\n\n     YOU WIN!\n\n\n\n\n");
    waitpad(J_A);
    ResetGame();
}


int main()
{
    printf(" ");
    printf("\n\n\n\nEternityWare Studios"); //intro
    delay(1000);
    printf("\n\n\n\n");
    printf("     Presents:");
    delay(2000);
    resetter1: //restart from here
    gameLoop = 1;
    walkAlt = 0;
    direction1 = 0;
    clsrn();
    SWITCH_ROM_MBC1(2);    // switch to ROM bank 2
    alphabetset();        // Routine to load the alphabet section there
    SWITCH_ROM_MBC1(1);    // switch to ROM bank 1
    printf("\n\n\n\n\n\n     Mid Autumn\n        Wind\n\n\n\n\n\n");
    delay(2000);
    printf("\n\n\n\n\n\n    Press START"); //wait for start
    waitpad(J_START);
    clsrn();

   
    SPRITES_8x16; //set sprites to load as max (8*16)
   
    //start intro movie
   
    HIDE_BKG; //scene 1
    disable_interrupts();
    set_bkg_data(0,255, story1_tile_data);
    set_bkg_tiles(0,0,16,16,story1_map_data);
    enable_interrupts();
    SHOW_BKG;
    delay(2000);
   
   
    HIDE_BKG; //scene 2 (loading from second file
    disable_interrupts();
    SWITCH_ROM_MBC1(2);    // switch to ROM bank 2
    storyPart2();        // Routine to load the story section there
    enable_interrupts();
    SHOW_BKG;   
    delay(2000);
   
   
    HIDE_BKG;
    disable_interrupts();
    SWITCH_ROM_MBC1(0);    // switch to ROM bank 0
    set_bkg_data(0,1, NULL);
    set_bkg_tiles(0,0,32,32,NULL);
    enable_interrupts();
    SHOW_BKG;
    delay(2000);

   
    set_bkg_data(0,24, groundTiles);

Last edited by jobalisk (2017-06-13 04:20:14)

Offline

 

#17 2017-06-13 07:29:08

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

Re: using asm files in combonation with a C game

Wait, you mean "only 128*128" pixels or tiles??

If it's 128*128 tiles, the problem is pretty obvious: 128*128 tiles means that you need 128*128 = 16,384 bytes of memory. That's precisely the size of one ROM bank. So, if you add just one additional line of code to that bank or GBDK adds just one single byte (for whatever reason) you get a bank overflow.

The reason it works with one of your images and without bank switching is that you can access ROM banks 0 and 1 without bank switching. If you compile your code (which is something like 1 kB) and one of the images (which is 16 kB) you get 1 kB + 16 kB = 17 kB. GBDK will automatically compile that to the first two banks which should result in a working ROM file. If you add a second 16 kB of data you have 1 kB + 16 kB + 16 kB = 33 kB. That's more than GBDK can fit into the first two ROM banks and therefore you get the overflow message.

If you want to display two images of roughly that size, you have to put each image in a separate ROM bank first. Then you have to reduce the size maybe by 20 % or so - just in case GBDK adds something or you want to include some lines of code in that bank.

Also, from your code I can see that you didn't do this:

Jonas wrote:

Probably you didn'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.

If you have this in ROM bank 2:

Code:

#include <stdio.h>
#include <gb/gb.h>
#include "story2.dat"
#include "alphabet.c"
//part 2 of the intro story as a seperate file
void storyPart2(void){
   

    set_bkg_data(0,255, story2_tile_data);
    set_bkg_tiles(0,0,16,16,story2_map_data);

   
}

void alphabetset(void){
    set_bkg_data(0,255, alphabeto);
}

You have to remove this from ROM bank 0:

Code:

void storyPart2(); //preload the story loader

And then replace it by that in ROM bank 0:

Code:

extern void storyPart2(void);
extern void alphabetset(void);

Good luck.

Last edited by Jonas (2017-06-13 07:33:52)

Offline

 

#18 2017-06-13 15:11:52

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

oh sorry, I meant 128 by 128 pixels, not tiles, so its not that large. I'll give it a shot thanks.

The second story file and the alphabet file wont fit in the same ROM bank, being 12 and 11 kb each. Though I don't think that's what's causing the problem as I have compiled it without including the alphabet file and it still errors.

Doing what you suggested and then compiling still generates the same error.

OK, here's something interesting. commenting out everything but import gb and import stdio in the second file still causes an overload. In other words the issue is with the second file itself rather than what it contains.

looking at the .o file somehow the standard .o file for the basic game with nothing but the story1 file is 161kb somehow. the main C file advojobo.c is somehow 16kb on its own with the map, sprites and other files coming to something like 15kb together. in other words, everything seems far larger than is logical. I'm starting to wonder if the problem is actually my text editor.

Last edited by jobalisk (2017-06-13 15:27:54)

Offline

 

#19 2017-06-13 23:53:21

cabbage
New member
Registered: 2016-06-10
Posts: 5

Re: using asm files in combonation with a C game

what is this story1.dat and story2.dat?
what format are they in, and what do they contain?
how did you generate them?

Offline

 

#20 2017-06-15 07:00:41

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

Re: using asm files in combonation with a C game

Yes, that would be interesting.

On the basis of what you told about your code/data, it isn't really plausible that the size should exceed one ROM bank.

Offline

 

#21 2017-06-15 15:03:24

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

the story 1 and 2 contain the map and data for 2 images of 128 by 128 pixels. I can use one of these or the custom alphabet with the main program without using the extra rom banks. There are an additional 3 of these data files I haven't used yet because I want to get 2 working before I add the others. I have no idea why it would be compiling to 161kb though. As it works perfectly fine when I'm just compiling the one C file. I've checked the text editor and can confirm there is no problem there which is even more confusing.

Offline

 

#22 2017-06-15 16:45:09

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

Re: using asm files in combonation with a C game

Okay... To be honest, I'm not sure whether we'll solve that.

But what's the precise error message you see? The "ERROR: address overflow" message you mentioned in post #1 or the "implicit declaration" and "undefined identifier" stuff you showed us in post #9? Or both?

Offline

 

#23 2017-06-16 15:37:22

jobalisk
Member
From: I don't even know anymore...
Registered: 2017-05-28
Posts: 55
Website

Re: using asm files in combonation with a C game

Its the one from post 1. it means that there is too much being put into the ROM banks I think or something like that right?

Offline

 

#24 2017-06-16 18:44:54

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

Re: using asm files in combonation with a C game

Yes right. It means that the linker tried to write code to an addess beyond $8000 which is not allowed since the space available for ROM data only reaches from $0000 to $7FFF. Normally, the reason is that your code is too large and doesn't fit. But based on the information you provided, it shouldn't exceed the limit.

It's always possile that there's a bug in GBDK (there are plenty). There may well be another explanation, but I don't have it...

Offline

 

#25 2017-06-16 21:00:09

ISSOtm
Member
From: Somewhere in Echo RAM
Registered: 2017-04-18
Posts: 160
Website

Re: using asm files in combonation with a C game

Theory : the file is included in a .h file, thus included multiple times thus going past the boundary. Very unlikely, though.


The French Lord Of Laziness.
Legend of Zelda and Undertale fan, I also tend to break Pokémon R/B/Y a little too much.

Twitter | Me on GCL | Discord : ISSOtm#9015 | Skype : isso.tm (I don't login anymore)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson