Skip to content

Commit

Permalink
Fix .gb/.pocket to not include gbc code
Browse files Browse the repository at this point in the history
This way you can run the .gb file on a CGB. Thanks Tobias V. Langhoff
for mentioning this issue!
  • Loading branch information
binji committed Apr 23, 2023
1 parent 40645e6 commit 9c59fd6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ TARGETS=gbc gb pocket # megaduck sms gg
LCCFLAGS_gb = -Wm-ys #
LCCFLAGS_pocket = -Wm-ys # Usually the same as required for .gb
LCCFLAGS_duck = -Wm-ys # Usually the same as required for .gb
LCCFLAGS_gbc = -Wm-ys -Wm-yc # Same as .gb with: -Wm-yc (gb & gbc) or Wm-yC (gbc exclusive)
LCCFLAGS_gbc = -DCGB_SUPPORT -Wm-ys -Wm-yc # Same as .gb with: -Wm-yc (gb & gbc) or Wm-yC (gbc exclusive)
LCCFLAGS_sms =
LCCFLAGS_gg =

Expand Down
2 changes: 2 additions & 0 deletions src/animate.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ void animate_end(void) {
dirtymap[pos] = 0;
}

#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE && dirty_saw_ptr != dirty_saw) {
// CGB needs to update the palette data too, but just for saws: animated
// saws use pal 1, and broken saws use pal 0.
Expand Down Expand Up @@ -154,6 +155,7 @@ void animate_end(void) {
*code++ = 0xe0; // ldh 0xff4f, a (set vram bank to 0)
*code++ = 0x4f; //
}
#endif

*code++ = 0xf1; // pop af
*code++ = 0xc9; // ret
Expand Down
2 changes: 2 additions & 0 deletions src/gameover.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void gameover_update(void) {
counter_out(&st_steps, STATS_STEPS_ADDR);
counter_out(&st_kills, STATS_KILLS_ADDR);

#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
VBK_REG = 1;
// Set the palette for the text rows.
Expand All @@ -97,6 +98,7 @@ void gameover_update(void) {
PRESS_A_HEIGHT, 4);
VBK_REG = 0;
}
#endif

gameover_state = GAME_OVER_WAIT;
IE_REG |= VBL_IFLAG;
Expand Down
2 changes: 2 additions & 0 deletions src/gameplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ u8 wurstchain;

void clear_bkg(void) {
init_bkg(0);
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
VBK_REG = 1;
init_bkg(0);
VBK_REG = 0;
}
#endif
}

void gameplay_init(void) {
Expand Down
2 changes: 2 additions & 0 deletions src/inventory.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ void inv_init(void) {
inv_msg_update = 1;
memset(equip_type, PICKUP_TYPE_NONE, sizeof(equip_type));
set_win_tiles(0, 0, INV_WIDTH, INV_HEIGHT, inventory_map);
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
VBK_REG = 1;
fill_win_rect(0, 0, INV_WIDTH, INV_HEIGHT, 3);
set_vram_byte((u8 *)(INV_KEYS_ADDR - 1), 0); // make key yellow
VBK_REG = 0;
}
#endif
}

void inv_update(void) {
Expand Down
20 changes: 16 additions & 4 deletions src/palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ static u16 saved_bkg_pal[32], saved_obp_pal[32];
static u16 cur_bkg_pal[32], cur_obp_pal[32];

void pal_init(void) {
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
cpu_fast();
set_bkg_palette(0, 5, cgb_bkg_pals);
set_sprite_palette(0, 7, cgb_obp_pals);
} else {
} else
#endif
{
// 0:Black 1:Black 2:Black 3:Black
BGP_REG = OBP0_REG = OBP1_REG = 0b11111111;
}
Expand All @@ -38,12 +41,15 @@ void pal_update(void) {
if (--flash_pal_timer == 0) {
flash_pal_timer = FLASH_PAL_FRAMES;
flash_pal_index = (flash_pal_index + 1) & 7;
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
set_sprite_palette(2, 1,
&cgb_player_dmg_flash_pals[flash_pal_index << 2]);
set_sprite_palette(4, 1, &cgb_mob_key_flash_pals[flash_pal_index << 2]);
set_bkg_palette(4, 1, &cgb_press_start_flash_pals[flash_pal_index << 2]);
} else {
} else
#endif
{
OBP1_REG = obj_pal1[flash_pal_index];
}
}
Expand All @@ -62,6 +68,7 @@ void get_obp_palettes(u8* dest);

void pal_fadeout(void) NONBANKED {
u8 i, j;
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
// Save original palette.
get_bkg_palettes(saved_bkg_pal);
Expand All @@ -84,7 +91,9 @@ void pal_fadeout(void) NONBANKED {
set_sprite_palette(0, 8, cur_obp_pal);
wait_vbl_done();
} while (i--);
} else {
} else
#endif
{
i = 3;
do {
for (j = 0; j < FADE_FRAMES; ++j) { wait_vbl_done(); }
Expand All @@ -103,6 +112,7 @@ u16 next_fadein_color(u16 src, u16 dst) NONBANKED {

void pal_fadein(void) NONBANKED {
u8 i, j;
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
i = FADE_FRAMES * 3;
do {
Expand All @@ -119,7 +129,9 @@ void pal_fadein(void) NONBANKED {
set_sprite_palette(0, 8, cur_obp_pal);
wait_vbl_done();
} while (i--);
} else {
} else
#endif
{
i = 0;
do {
for (j = 0; j < FADE_FRAMES; ++j) { wait_vbl_done(); }
Expand Down
5 changes: 4 additions & 1 deletion src/sound.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ void soundinit(void) {

// set up timer for sound
TAC_REG = 0b100; // timer enabled, clock / 1024
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
TMA_REG = 0xbc; // double speed clock / 1024 / 68 = ~120Hz
} else {
} else
#endif
{
TMA_REG = 0xde; // clock / 1024 / 34 = ~120Hz
}
IE_REG |= TIM_IFLAG; // timer interrupt enabled
Expand Down
7 changes: 6 additions & 1 deletion src/title.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ void titlescreen(void) {
SCX_REG = 232;
SCY_REG = titletop_bounce[0];
set_bkg_tiles(0, 0, TITLETOP_WIDTH, TITLETOP_HEIGHT, titletop_map);
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
VBK_REG = 1;
fill_bkg_rect(0, 0, TITLETOP_WIDTH, TITLETOP_HEIGHT, 1);
VBK_REG = 0;
} else {
} else
#endif
{
// 0:LightGray 1:DarkGray 2:Black 3:White
BGP_REG = OBP0_REG = OBP1_REG = 0b00111001;
}
Expand Down Expand Up @@ -163,11 +166,13 @@ void titlescreen(void) {
LCDC_REG = LCDCF_ON | LCDCF_WINON | LCDCF_BGON | LCDCF_OBJON | LCDCF_WIN9C00;
init_win(0x8e);
set_win_tiles(0, 0, TITLEBOT_WIDTH, TITLEBOT_HEIGHT, titlebot_map);
#ifdef CGB_SUPPORT
if (_cpu == CGB_TYPE) {
VBK_REG = 1;
fill_win_rect(0, 0, TITLEBOT_WIDTH, TITLEBOT_HEIGHT, 3);
VBK_REG = 0;
}
#endif

// slide up title, gameboy, and credits
for (i = 0; i < sizeof(cubic_up) + SLIDE_LAG; ++i) {
Expand Down

0 comments on commit 9c59fd6

Please sign in to comment.