Skip to content

Commit

Permalink
Disable packed/double width palettes by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ccawley2011 committed May 30, 2024
1 parent df4c609 commit c9d428e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/debug/peanut-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "SDL.h"

#define PEANUT_GB_USE_NIBBLE_FOR_PALETTE 1
#define ENABLE_LCD 1
#define ENABLE_SOUND 0

Expand Down
2 changes: 2 additions & 0 deletions examples/sdl2/peanut_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# include "minigb_apu/minigb_apu.h"
#endif

#define PEANUT_GB_USE_NIBBLE_FOR_PALETTE 1

#include "../../peanut_gb.h"

enum {
Expand Down
67 changes: 50 additions & 17 deletions peanut_gb.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@
# define PEANUT_GB_12_COLOUR 1
#endif

/**
* If PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE is enabled, the pixel colour data will be
* duplicated into both nibbles. This allows for faster scaling when rendering to
* a 4 bit per pixel destination.
*
* PEANUT_GB_USE_NIBBLE_FOR_PALETTE must also be defined for this to work.
*/
#ifndef PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
# define PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE 0
#endif

/**
* If PEANUT_GB_USE_NIBBLE_FOR_PALETTE is enabled, the pixel colour data will be
* packed into the four least significant bits of a byte (or nibble). This
* allows for a smaller look-up table (LUT) to be used directly, in comparison
* to performing bit shifts or using a larger LUT.
*/
#ifndef PEANUT_GB_USE_NIBBLE_FOR_PALETTE
# define PEANUT_GB_USE_NIBBLE_FOR_PALETTE PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
#endif

/* Adds more code to improve LCD rendering accuracy. */
#ifndef PEANUT_GB_HIGH_LCD_ACCURACY
# define PEANUT_GB_HIGH_LCD_ACCURACY 1
Expand Down Expand Up @@ -464,20 +485,29 @@ struct count_s
#define LCD_COLOUR 0x03

# if PEANUT_GB_12_COLOUR
/**
* Bit mask for whether a pixel is OBJ0, OBJ1, or BG. Each may have a different
* palette when playing a DMG game on CGB.
*/
#define LCD_PALETTE_OBJ 0x04
#define LCD_PALETTE_BG 0x08
/**
* Bit mask for the two bits listed above.
* LCD_PALETTE_ALL == 0b00 --> OBJ0
* LCD_PALETTE_ALL == 0b01 --> OBJ1
* LCD_PALETTE_ALL == 0b10 --> BG
* LCD_PALETTE_ALL == 0b11 --> NOT POSSIBLE
*/
#define LCD_PALETTE_ALL 0x0c
# if PEANUT_GB_USE_NIBBLE_FOR_PALETTE
/**
* Bit mask for whether a pixel is OBJ0, OBJ1, or BG. Each may have a different
* palette when playing a DMG game on CGB.
*/
#define LCD_PALETTE_OBJ 0x04
#define LCD_PALETTE_BG 0x08
/**
* Bit mask for the two bits listed above.
* LCD_PALETTE_ALL == 0b00 --> OBJ0
* LCD_PALETTE_ALL == 0b01 --> OBJ1
* LCD_PALETTE_ALL == 0b10 --> BG
* LCD_PALETTE_ALL == 0b11 --> NOT POSSIBLE
*/
#define LCD_PALETTE_ALL 0x0c
# else
# if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
# error PEANUT_GB_USE_NIBBLE_FOR_PALETTE must be enabled for PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE to work
# endif
#define LCD_PALETTE_OBJ 0x10
#define LCD_PALETTE_BG 0x20
#define LCD_PALETTE_ALL 0x30
# endif
# else
#define LCD_PALETTE_OBJ 0
#define LCD_PALETTE_BG 0
Expand Down Expand Up @@ -1140,11 +1170,12 @@ void __gb_write(struct gb_s *gb, uint_fast16_t addr, uint8_t val)
gb->display.bg_palette[1] = ((gb->hram_io[IO_BGP] >> 2) & 0x03) | LCD_PALETTE_BG;
gb->display.bg_palette[2] = ((gb->hram_io[IO_BGP] >> 4) & 0x03) | LCD_PALETTE_BG;
gb->display.bg_palette[3] = ((gb->hram_io[IO_BGP] >> 6) & 0x03) | LCD_PALETTE_BG;

#if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
gb->display.bg_palette[0] |= gb->display.bg_palette[0] << 4;
gb->display.bg_palette[1] |= gb->display.bg_palette[1] << 4;
gb->display.bg_palette[2] |= gb->display.bg_palette[2] << 4;
gb->display.bg_palette[3] |= gb->display.bg_palette[3] << 4;
#endif
return;

case 0x48:
Expand All @@ -1153,11 +1184,12 @@ void __gb_write(struct gb_s *gb, uint_fast16_t addr, uint8_t val)
gb->display.sp_palette[1] = ((gb->hram_io[IO_OBP0] >> 2) & 0x03);
gb->display.sp_palette[2] = ((gb->hram_io[IO_OBP0] >> 4) & 0x03);
gb->display.sp_palette[3] = ((gb->hram_io[IO_OBP0] >> 6) & 0x03);

#if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
gb->display.sp_palette[0] |= gb->display.sp_palette[0] << 4;
gb->display.sp_palette[1] |= gb->display.sp_palette[1] << 4;
gb->display.sp_palette[2] |= gb->display.sp_palette[2] << 4;
gb->display.sp_palette[3] |= gb->display.sp_palette[3] << 4;
#endif
return;

case 0x49:
Expand All @@ -1166,11 +1198,12 @@ void __gb_write(struct gb_s *gb, uint_fast16_t addr, uint8_t val)
gb->display.sp_palette[5] = ((gb->hram_io[IO_OBP1] >> 2) & 0x03) | LCD_PALETTE_OBJ;
gb->display.sp_palette[6] = ((gb->hram_io[IO_OBP1] >> 4) & 0x03) | LCD_PALETTE_OBJ;
gb->display.sp_palette[7] = ((gb->hram_io[IO_OBP1] >> 6) & 0x03) | LCD_PALETTE_OBJ;

#if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
gb->display.sp_palette[4] |= gb->display.sp_palette[4] << 4;
gb->display.sp_palette[5] |= gb->display.sp_palette[5] << 4;
gb->display.sp_palette[6] |= gb->display.sp_palette[6] << 4;
gb->display.sp_palette[7] |= gb->display.sp_palette[7] << 4;
#endif
return;

/* Window Position Registers */
Expand Down

0 comments on commit c9d428e

Please sign in to comment.