Page 1 of 1

PSMS Reloaded v0.1 - Problems with GsKit/CLUT

Posted: Mon Feb 25, 2008 1:26 am
by bootsector_
Hi folks!

I'm trying to bring back PSMS 1.2 (Sega Master System Emulator for PS2) to life again by converting it to GsKit and using some ideas/code I got from FCEUltra for PS2 by ragnarok2040.

It's already loading ROMs from every device (MC/USB/MASS/HDD...) and it's running at full speed with sound. However, as I'm a newbie with all this GsKit/PS2 stuff, I'm facing problems with the PS2 CLUT. This is leading me to a screen like this:

Image

instead of this:

Image

As you guys can see, colors are all messed up. I have tried a couple of things but with no success. Here are some fragments of pertinent code (you can download full sources and a working binary at the end of this post as well):

bitmap and clut data variables:

Code: Select all

u8 bitmap_data[256 * 256] __attribute__((aligned(128))) __attribute__ ((section (".bss"))); // SMS display is only 256x192, extra data is padding
u16 clut[256] __attribute__((aligned(16))) __attribute__ ((section (".bss")));
Setting up SMS screen texture:

Code: Select all

void setupSMSTexture(void) {
	SMSTEX.PSM = GS_PSM_T8;
	SMSTEX.ClutPSM = GS_PSM_CT16;
	SMSTEX.Clut = (u32 *)clut;
	SMSTEX.VramClut = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(16, 16, SMSTEX.ClutPSM), GSKIT_ALLOC_USERBUFFER);
	SMSTEX.Width = 256;
	SMSTEX.Height = 256;
	SMSTEX.TBW = 4; //256;
	SMSTEX.Vram = gsKit_vram_alloc(gsGlobal, gsKit_texture_size(SMSTEX.Width, SMSTEX.Height, SMSTEX.PSM), GSKIT_ALLOC_USERBUFFER);
}
Update video function:

Code: Select all

void update_video()
{

	SMSTEX.Mem = (u32 *)bitmap_data;
	
    gsKit_texture_upload(gsGlobal, &SMSTEX);

    /* vsync and flip buffer */
    gsKit_sync_flip(gsGlobal);

    /* execute render queue */
    gsKit_queue_exec(gsGlobal);
}
Palette building routine (this is called with indexes between 0 and 31):

Code: Select all

void palette_sync(int index)
{
    int r, g, b;

    if(IS_GG)
    {
        r = &#40;&#40;vdp.cram&#91;&#40;index << 1&#41; | 0&#93; >> 1&#41; & 7&#41; << 5;
        g = &#40;&#40;vdp.cram&#91;&#40;index << 1&#41; | 0&#93; >> 5&#41; & 7&#41; << 5;
        b = &#40;&#40;vdp.cram&#91;&#40;index << 1&#41; | 1&#93; >> 1&#41; & 7&#41; << 5;
    &#125;
    else
    &#123;
        r = &#40;&#40;vdp.cram&#91;index&#93; >> 0&#41; & 3&#41; << 6;
        g = &#40;&#40;vdp.cram&#91;index&#93; >> 2&#41; & 3&#41; << 6;
        b = &#40;&#40;vdp.cram&#91;index&#93; >> 4&#41; & 3&#41; << 6;
    &#125;

    bitmap.pal.color&#91;index&#93;&#91;0&#93; = r;
    bitmap.pal.color&#91;index&#93;&#91;1&#93; = g;
    bitmap.pal.color&#91;index&#93;&#91;2&#93; = b;

    pixel&#91;index&#93; = MAKE_PIXEL&#40;r, g, b&#41;;

    bitmap.pal.dirty&#91;index&#93; = bitmap.pal.update = 1;
	
	clut&#91;index&#93; = &#40; &#40;&#40;b>>3&#41;<<10&#41; | &#40;&#40;g>>3&#41;<<5&#41; | &#40;g>>3&#41; &#41;;
	
	//clut&#91;index | 0x20&#93; = &#40; &#40;&#40;b>>3&#41;<<10&#41; | &#40;&#40;g>>3&#41;<<5&#41; | &#40;r>>3&#41; &#41;;
	//clut&#91;index | 0x40&#93; = &#40; &#40;&#40;b>>3&#41;<<10&#41; | &#40;&#40;g>>3&#41;<<5&#41; | &#40;r>>3&#41; &#41;;
&#125;
If you need to see the real thing, you can download a working binary here and full source code here.

Hope you guys can help me on this issue, so we can have another great emulator for the PS2 homebrew library! ;)

Thanks!

bootsector

Posted: Mon Feb 25, 2008 2:15 am
by Lukasz
Your issue is that the CLUT colors on the PS2 are not stored in order from 0 to 31, instead you should store it in this order (first 32 values, indices in HEX)

Code: Select all

00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17
08 | 09 | 0A | 0B | 0C | 0D | 0E | 0F | 18 | 19 | 1A | 1B | 1C | 1D | 1E | 1F 
...
See the GS Manual section "CLUT Storage Mode" for more details.

Nice job bringing the emulator code up to date :-)

Posted: Mon Feb 25, 2008 3:33 am
by bootsector_
You're right, Lukasz! I noted that ragnarok2040 did this on FCEUltra. I actually tried to do this in PSMS, but by the wrong way! Now I fixed the palette_sync function and it's now generating a valid CLUT! Thanks for your support!

Best regards,

bootsector

Posted: Mon Feb 25, 2008 4:35 am
by cosmito
Thanks bootsector_ for keeping the PS2 scene alive in these days. I recommend you a free SVN repository service (www.assembla.com) for keeping your sources. This way, you'll have a free repository for you files and for us an always up-to-date version.

Posted: Mon Feb 25, 2008 8:59 am
by bootsector_
Hi folks,

First public/playable version was just released:

http://psx-scene.com/forums/ps2-homebre ... oaded.html

Posted: Wed Feb 27, 2008 1:46 am
by Wraggster
thanks for the news/release, great to see the PS2 get some more loving these days :)