GBA problem with (manual)double-buffer.

Started by
5 comments, last by Arek the Absolute 18 years, 1 month ago
I'm having a problem with double-buffering on the GBA! Because of my love of low-level programming I decided to go with the GBA and it's memory... video memory being an unsigned short at 0x6000000. I made a random-pixel-plotter program... only to mess up making a double-buffer:

#include <cstdlib>
#include <ctime>
#include <cstring>

#include "GBA_Main.hpp"

GBA::uShort *pVideo = NULL;
GBA::uShort *pBackVideo = (GBA::uShort*)malloc(sizeof(GBA::uShort) * 240 * 160);

int main(void) {
    srand(time(0));
    
    GBA::SetMode(GBA::VIDEO_MODE3 | GBA::VIDEO_BACKGROUND2);
    
    pVideo = GBA::GetVideo();
    
    while(true) {
        for (unsigned i = 0; i < 240; i++)
            for (unsigned j = 0; j < 160; j++)
                *(/*pBack*/pVideo + j * 240 + i) = GBA::Rgb16(rand() % 256, rand() % 256, rand() % 256);

        //memcpy(pVideo, pBackVideo, 240 * 160 * sizeof(GBA::uShort));
    }

    free(pBackVideo);

    return 0;
}


This is the whole two programs in one piece.... The /*pBack*/pVideo is pBackVideo on the other program, and the memcpy is not commented out... Now, progam one returns this:(yes, the code above) Image Hosted by ImageShack.us The other one WITH the double buffer is this: Image Hosted by ImageShack.us Any ideas as to why? THANKS! This is programmed in C++ in Visual HAM. EDIT: And her is the GBA header file, it's purely mine: (from scratch, not some lib)

#ifndef GBA_MAIN_HPP
#define GBA_MAIN_HPP

namespace GBA {
    typedef unsigned short uShort;
    typedef unsigned long uLong;
    
    inline uShort *GetVideo() {
        return (uShort*)0x6000000;
    }
    
    inline uLong *GetSetting() {
        return (uLong*)0x4000000;
    }
    
    const int VIDEO_MODE3 = 0x3;
    const int VIDEO_BACKGROUND2 = 0x400;
    
    inline void SetMode(const uLong Type) {
        *GetSetting() = Type;
    }
    
    inline uShort Rgb16(uShort r, uShort g, uShort b) {
        return (r | (g << 5) | (b << 10));
    }
}

#endif

Advertisement
Its been a while since ive programmed my GBA but theres a free book about GBA development here. Hope it helps.

http://www.jharbour.com/gameboy/default.aspx
Quote:Original post by cNoob
Its been a while since ive programmed my GBA but theres a free book about GBA development here. Hope it helps.

http://www.jharbour.com/gameboy/default.aspx


Thank you, that's the book I'm following anywayz... I wanted to see if I can program a double-buffer but so far no luck...

Any ideas peoplez?

Note: The 'z' just soundz cool [grin]. Ok, I'll stop.
Disclaimer: It's been a few years since I've touched anything on the GBA, so my memory is slipping. I'll try and confirm what I'm saying against some of my old posts here, because I'm pretty sure I encountered something similar.

That said, I seem to remember that the GBA literally cannot allocate enough memory to supply a full back buffer for mode 3. I think if you're going to do something like this, you're going to need to find another system, perhaps only buffering certain parts at certain times, or something of that nature.

As an additional comment, you might want to consider looking up DMA channels. The GBA is slow enough that it will make a HUGE difference when you use the finer points of the hardware to deal with straight copies like with the memcpy bit there. Obviously it doesn't matter in this tiny example, but that's just a word of advice.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig
Quote:Original post by Arek the Absolute
Disclaimer: It's been a few years since I've touched anything on the GBA, so my memory is slipping. I'll try and confirm what I'm saying against some of my old posts here, because I'm pretty sure I encountered something similar.

That said, I seem to remember that the GBA literally cannot allocate enough memory to supply a full back buffer for mode 3. I think if you're going to do something like this, you're going to need to find another system, perhaps only buffering certain parts at certain times, or something of that nature.

As an additional comment, you might want to consider looking up DMA channels. The GBA is slow enough that it will make a HUGE difference when you use the finer points of the hardware to deal with straight copies like with the memcpy bit there. Obviously it doesn't matter in this tiny example, but that's just a word of advice.


Hmm... Interesting. Thanks!

I think I totally forgot that this is actually supposed to run on a GBA.... I just started GBA programming so I decided to do this, thinking that I used to do this when I am programming for the computer :).

Thanks, rat++.
I have a hard time believing it is okay to call a function from outside main (malloc, I'm looking at you). Try moving it inside main.
Quote:Original post by Arek the Absolute
Disclaimer: It's been a few years since I've touched anything on the GBA, so my memory is slipping. I'll try and confirm what I'm saying against some of my old posts here, because I'm pretty sure I encountered something similar.


Just wanted to follow this up real quick, as I found the thread where I was asking about something similar.

As an anonymous poster said almost four years ago now:
Quote:Original post by Anonymous Poster
"The GBA only has 32kb IWRAM (used for storing variables and stuff) and 256kb EWRAM (usually used to store data). Your backbuffer doesn't fit in that 32kb IWRAM, that's why it isn't working."


The whole thread is here.
-Arek the Absolute"The full quartet is pirates, ninjas, zombies, and robots. Create a game which involves all four, and you risk being blinded by the sheer level of coolness involved." - Superpig

This topic is closed to new replies.

Advertisement