domingo, 14 de marzo de 2021

Jaguar Coding Nightmare

At last!!! Today I've put together all three games that I've been developing for the last... I don't know and I don't want to remember, it was too much time.

I think that I've spent more than 70% of the time fixing bugs, some of them were my own mistakes, when you code all by yourself it's normal to make mistakes but some of them were caused by some kind of mismatch between the tools and the lack of OS, these were very hard to find.

I haven't used any library, it's 100% my own code, well... Christmas Craze and Classic Kong are a port from the SNES version, but I had to rewrite some parts to use the Jaguar hardware.

Here you have a couple of errors that took me a lot of time to spot and fix it.

Data alignment

The Jaguar it's very picky with this, if you try to access a 32bits value (must be aligned to 4 bytes boundary) with the GPU but the data it's aligned to a 2 bytes boundary you won't read the correct value. It's ok but when you code in C sometimes you can forget to align the data or the compiler can do some nasty things (see below -flto).

Object Processor

The same as above but this time the data must be aligned to a 16 bytes boundary (phrase) or sometimes to a 32 bytes boundary (dphrase) for scaled objects.

Also when the Object Processor reads the list, it'll modify the Bitmapped Objects, and if you make a mistake in the list you'll hang the Jaguar instead of having a wrong display, sometimes because it will read data outside the Object Processor List and can smash the code.

-flto

Link time optimization, with this flags the compiler delay optimization to the link phase, I don't know if it a bit buggy or if it should be used with other flags but when I was using this flag (I wanted the fastest code 😜) some data missed the alignment, this means you are in big problems with the GPU and the Object Processor.


-fno-zero-initialized-in-bss

When I was coding the games (any of them) and the menu, sometimes the code didn't work (about 1/20 of the times), I uploaded the code to the Skunkboard and nothing happens. I thought that I got some wrong init code, I've looked at the init code at the Jaguar SDK and even I disassembled a couple of games to have a look at the init code, but everything looked fine.

When I coded the menu to launch any of the three games I got the following issues with each game.

  1. Christmas Craze, it worked.
  2. Classic Kong, always hangs at the intro when you start to play (Kong climbing with Pauline).
  3. BurgerTom, sometimes it played the menu music, sometimes you can see the menu with graphics glitches, sometimes it just hangs at the very beginning.
It was really weird because all games begin with the same code, upload the GPU code, init the sprite system, init the sound system. If I uploaded the code of any game to the skunkboard it worked but failed if the game was launched from the menu, weird because it was a simple copy game code to $4000 and jmp $4000, the error must be somewhere, not in the menu... 

After a couple of print debugging (I wish to have a proper debugger 😞), I realized that this part of the code executed in a different way when the game was uploaded to the skunkboard than it was copied from the menu, _text_strip wasn't NULL 😮.

...
static SPRITE_STRIP *_text_strip = NULL;
...
void init_text()
{
    if ( _text_strip == NULL )
        _text_strip = new_strip(256, 224, 0);

    clear_strip(_text_strip);
    ...
}

Looking at the map file generated by the linker, _text_strip was located at the bss segment. By default gcc compiler put all data initialized to zero into bss (-fzero-initialized-in-bss) because the OS will fill the bss section with zeros, BUT we don't have any OS on the Jaguar so _text_strip can have any value, actually, It will have some value from the menu code or data.


So after a lot of headaches I could finish all games, sometimes it was my own error, sometimes the tools didn't worked fine (-flto) and sometimes the lack of OS support makes that some features of the tools useless (-fzero-initialized-in-bss).


Let's hope that future projects will take me a lot of less time 😉.

No hay comentarios:

Publicar un comentario