Skip to content

Commit

Permalink
Fix cgb palette fading code
Browse files Browse the repository at this point in the history
You can't read/write the palette data registers during mode 3, same as
vram.
  • Loading branch information
binji committed Feb 10, 2023
1 parent ff4b8e2 commit 40645e6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 42 deletions.
72 changes: 30 additions & 42 deletions src/palette.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gb/gb.h>
#include <gb/cgb.h>
#include <string.h>

#include "common.h"
#include "palette.h"
Expand All @@ -18,7 +19,8 @@ extern const palette_color_t cgb_mob_key_flash_pals[];
extern const palette_color_t cgb_press_start_flash_pals[];

static u8 flash_pal_timer, flash_pal_index;
static u8 saved_bkg_pal[64], saved_obp_pal[64];
static u16 saved_bkg_pal[32], saved_obp_pal[32];
static u16 cur_bkg_pal[32], cur_obp_pal[32];

void pal_init(void) {
if (_cpu == CGB_TYPE) {
Expand Down Expand Up @@ -55,38 +57,31 @@ u16 next_fadeout_color(u16 color) NONBANKED {
return newcolor;
}

void get_bkg_palettes(u8* dest);
void get_obp_palettes(u8* dest);

void pal_fadeout(void) NONBANKED {
u8 i, j;
if (_cpu == CGB_TYPE) {
u16 color;

// Save original palette.
for (i = 0; i < 64; ++i) {
rBCPS = i;
saved_bkg_pal[i] = rBCPD;
rOCPS = i;
saved_obp_pal[i] = rOCPD;
}
get_bkg_palettes(saved_bkg_pal);
get_obp_palettes(saved_obp_pal);

// Update cur palette.
memcpy(cur_bkg_pal, saved_bkg_pal, sizeof(cur_bkg_pal));
memcpy(cur_obp_pal, saved_obp_pal, sizeof(cur_obp_pal));

i = FADE_FRAMES * 3;
do {
for (j = 0; j < 64; j += 2) {
rBCPS = j + 1;
color = rBCPD << 8;
rBCPS = j | BCPSF_AUTOINC;
color |= rBCPD;
color = next_fadeout_color(color);
rBCPD = color & 0xff;
rBCPD = color >> 8;

rOCPS = j + 1;
color = rOCPD << 8;
rOCPS = j | OCPSF_AUTOINC;
color |= rOCPD;
color = next_fadeout_color(color);
rOCPD = color & 0xff;
rOCPD = color >> 8;
// Calculate next palette.
for (j = 0; j < 32; ++j) {
cur_bkg_pal[j] = next_fadeout_color(cur_bkg_pal[j]);
cur_obp_pal[j] = next_fadeout_color(cur_obp_pal[j]);
}

// Write it.
set_bkg_palette(0, 8, cur_bkg_pal);
set_sprite_palette(0, 8, cur_obp_pal);
wait_vbl_done();
} while (i--);
} else {
Expand All @@ -109,26 +104,19 @@ u16 next_fadein_color(u16 src, u16 dst) NONBANKED {
void pal_fadein(void) NONBANKED {
u8 i, j;
if (_cpu == CGB_TYPE) {
u16 color;
i = FADE_FRAMES * 3;
do {
for (j = 0; j < 64; j += 2) {
rBCPS = j + 1;
color = rBCPD << 8;
rBCPS = j | BCPSF_AUTOINC;
color |= rBCPD;
color = next_fadein_color(color, *((u16 *)(saved_bkg_pal + j)));
rBCPD = color & 0xff;
rBCPD = color >> 8;

rOCPS = j + 1;
color = rOCPD << 8;
rOCPS = j | OCPSF_AUTOINC;
color |= rOCPD;
color = next_fadein_color(color, *((u16 *)(saved_obp_pal + j)));
rOCPD = color & 0xff;
rOCPD = color >> 8;
u16 *saved_bkg = saved_bkg_pal;
u16 *saved_obp = saved_obp_pal;
// Calculate next palette.
for (j = 0; j < 32; ++j) {
cur_bkg_pal[j] = next_fadein_color(cur_bkg_pal[j], saved_bkg[j]);
cur_obp_pal[j] = next_fadein_color(cur_obp_pal[j], saved_obp[j]);
}

// Write it.
set_bkg_palette(0, 8, cur_bkg_pal);
set_sprite_palette(0, 8, cur_obp_pal);
wait_vbl_done();
} while (i--);
} else {
Expand Down
50 changes: 50 additions & 0 deletions src/util.s
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,56 @@ _vram_copy::
jr nz, 1$
ret

; void get_bkg_palettes(u8* dest)
_get_bkg_palettes::
ldhl sp, #2
ld a, (hl+)
ld h, (hl)
ld l, a ; hl = dst

ld b, #0 ; byte index

1$:
ldh a, (.STAT) ; wait until VRAM is unlocked
and a, #STATF_BUSY
jr nz, 1$

ld a, b
ldh (0x68), a ; write index
ldh a, (0x69) ; read value
ld (hl+), a

inc b
ld a, b
cp #0x40
jr nz, 1$
ret

; void get_obp_palettes(u8* dest)
_get_obp_palettes::
ldhl sp, #2
ld a, (hl+)
ld h, (hl)
ld l, a ; hl = dst

ld b, #0 ; byte index

1$:
ldh a, (.STAT) ; wait until VRAM is unlocked
and a, #STATF_BUSY
jr nz, 1$

ld a, b
ldh (0x6a), a ; write index
ldh a, (0x6b) ; read value
ld (hl+), a

inc b
ld a, b
cp #0x40
jr nz, 1$
ret

; zero page vars
DIST=0x93
MAXDIST=0x94
Expand Down

0 comments on commit 40645e6

Please sign in to comment.