PSMS Reloaded v0.1 - Problems with GsKit/CLUT

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
User avatar
bootsector_
Posts: 15
Joined: Mon Feb 25, 2008 12:56 am

PSMS Reloaded v0.1 - Problems with GsKit/CLUT

Post 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
User avatar
Lukasz
Posts: 248
Joined: Mon Jan 19, 2004 8:37 pm
Location: Denmark
Contact:

Post 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 :-)
User avatar
bootsector_
Posts: 15
Joined: Mon Feb 25, 2008 12:56 am

Post 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
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post 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.
User avatar
bootsector_
Posts: 15
Joined: Mon Feb 25, 2008 12:56 am

Post by bootsector_ »

Hi folks,

First public/playable version was just released:

http://psx-scene.com/forums/ps2-homebre ... oaded.html
Wraggster
Posts: 121
Joined: Fri Aug 26, 2005 7:40 am
Contact:

Post by Wraggster »

thanks for the news/release, great to see the PS2 get some more loving these days :)
Webmaster of http://www.dcemu.co.uk

DCEMU The Worlds Only Homebrew & Gaming Network of Sites.
Post Reply