A little snippet for SDCC devs. Usually I'm not using devKitSMS lib from our friend Sverx but i've slightly edited some calls to work with ;)

This function will produce a "smooth" fade. At each vblank a color channel is updated on each palette entry (successively B => G => R) (32 colors ; full pal background & sprites)

You'll need to add few global vars

Global vars

__at(0xDF30) unsigned char fadePaltmp[8];
__at(0xDF40) unsigned char temp_pal32in[32];
__at(0xDF60) unsigned char temp_pal32out[32];

The function (see below)

And then copy your pal to temp ram var before calling the function

//only 1st time at init
memcpy((unsigned char *)temp_pal32out, (unsigned char *)0xDF80, 32); //can be any addr (ROM or RAM), just need 32 bytes full of {0} (=black), can also be done with memset() or if ram is fully cleared (depend on CRT0) no need to add this line

memcpy((unsigned char *)temp_pal32in, (unsigned char *)my_pal, 32); //copy your pal data in ram var before calling the function

SMS_displayOn(); //screen must be "on"
fade_function_ASM(); //call the function
...rest of your stuff here...

After the very first call of the function, you'll only need to change IN pal as OUT values had been already processed before.

Little hints :

memcpy((unsigned char *)temp_pal32in, (unsigned char *)my_pal, 32); //full fade pals bkg+spr
memcpy((unsigned char *)temp_pal32in, (unsigned char *)my_pal, 16); //fade only only bkg pal
memcpy((unsigned char *)temp_pal32in +16, (unsigned char *)my_pal, 16); //fade only spr pal

Cheers Ichigo.

void fade_function_ASM() __naked{
// will transform palout into palin, one color channel at a time
__asm
        push hl
        push bc
        push de

        xor a
        ld (#_fadePaltmp+6), a ;color var, start with blue channel (0)

_faderoutine_master_loop:
        xor a
        ld (#_fadePaltmp+7), a ; reset diff var

        ld hl, #_temp_pal32in
        ld de, #_temp_pal32out
        ; PALETTEs are aligned, so only inc or dec L / E
        ld b, #32 ; 32 colors (full pal bkg+spr) loop

_faderoutine_loop:
        push hl
        push de
        ld c, (hl) ; 1 IN
        ld hl, #_fadePaltmp ;Rin,Gin,Bin - 0,1,2
        call _fadeout_explode_channels

        ex de, hl
        ld c, (hl) ; 2 OUT
        ld hl, #_fadePaltmp+3 ;Rout,Gout,Bout - 3,4,5
        call _fadeout_explode_channels

        ld de, #_fadePaltmp+2 ;Bin
        ld hl, #_fadePaltmp+5 ;Bout
        xor a
        ld a, (#_fadePaltmp +6) ;which color index R(2), G(1), B(0)
        or a
        jp z, _fadeout_diff_rgb ;Blue
        dec l
        dec e
        dec a
        jp z, _fadeout_diff_rgb ;Green
        dec l
        dec e
        jp _fadeout_diff_rgb ;Red

_faderoutine_compute:
        pop de
        pop hl
        xor a
        ld a, (#_fadePaltmp+3) ;Rout
        ld c, a
        ld a, (#_fadePaltmp+4) ;Gout
        rla
        rla
        add c
        ld c, a
        ld a, (#_fadePaltmp+5) ;Bout
        rla
        rla
        rla
        rla
        add c
        ld (de), a
        inc l
        inc e
        djnz _faderoutine_loop

        ld a, (#_fadePaltmp+6) ;color
        cp #2
        jr nz, 3$
        ld a, #0xFF
3$:     inc a
        ld (#_fadePaltmp+6), a ;next channel B(0)=>G(1)=>R(2)

        call _SMS_waitForVBlank
        call _SMS_waitForVBlank
        ;add more _SMS_waitForVBlank calls to make fade effect "slower"

        call _load_palette_32c
        ld a, (#_fadePaltmp+7) ;diff==0?
        or a
        jp nz, _faderoutine_master_loop ;loop while diff!=0

        pop de
        pop bc
        pop hl
        ret

_load_palette_32c:
        xor a
        ld c, #0xBF
        out (c), a
        ld a, #0xC0
        out (c), a
        dec c ;=BE
        ld b, #32
        ld hl, #_temp_pal32out
        otir
        ret

_fadeout_diff_rgb:
        ld a, (de) ;inPal channel
        sub (hl) ;outPal channel
        jp z, _faderoutine_compute
        ld a, #1
        ld (#_fadePaltmp+7), a ;Set Diff to 1
        jr nc, _fadeout_diff_rgb_minus
        dec (hl)
        jp _faderoutine_compute
_fadeout_diff_rgb_minus:
        inc (hl)
        jp _faderoutine_compute

_fadeout_explode_channels:
        ld a, c
        and #0x3 ;R
        ld (hl), a
        ld a, c
        and #0xC ;G
        rra
        rra
        inc l
        ld (hl), a
        ld a, c
        and #0x30 ;B
        rra
        rra
        rra
        rra
        inc l
        ld (hl), a
        ret

__endasm;
}



Return to top
0.309s