9595# define PEANUT_GB_12_COLOUR 1
9696#endif
9797
98+ /**
99+ * If PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE is enabled, the pixel colour data will be
100+ * duplicated into both nibbles. This allows for faster scaling when rendering to
101+ * a 4 bit per pixel destination.
102+ *
103+ * PEANUT_GB_USE_NIBBLE_FOR_PALETTE must also be defined for this to work.
104+ */
105+ #ifndef PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
106+ # define PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE 0
107+ #endif
108+
109+ /**
110+ * If PEANUT_GB_USE_NIBBLE_FOR_PALETTE is enabled, the pixel colour data will be
111+ * packed into the four least significant bits of a byte (or nibble). This
112+ * allows for a smaller look-up table (LUT) to be used directly, in comparison
113+ * to performing bit shifts or using a larger LUT.
114+ */
115+ #ifndef PEANUT_GB_USE_NIBBLE_FOR_PALETTE
116+ # define PEANUT_GB_USE_NIBBLE_FOR_PALETTE PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
117+ #endif
118+
98119/* Adds more code to improve LCD rendering accuracy. */
99120#ifndef PEANUT_GB_HIGH_LCD_ACCURACY
100121# define PEANUT_GB_HIGH_LCD_ACCURACY 1
@@ -501,20 +522,29 @@ struct count_s
501522 #define LCD_COLOUR 0x03
502523
503524# if PEANUT_GB_12_COLOUR
504- /**
505- * Bit mask for whether a pixel is OBJ0, OBJ1, or BG. Each may have a different
506- * palette when playing a DMG game on CGB.
507- */
508- #define LCD_PALETTE_OBJ 0x04
509- #define LCD_PALETTE_BG 0x08
510- /**
511- * Bit mask for the two bits listed above.
512- * LCD_PALETTE_ALL == 0b00 --> OBJ0
513- * LCD_PALETTE_ALL == 0b01 --> OBJ1
514- * LCD_PALETTE_ALL == 0b10 --> BG
515- * LCD_PALETTE_ALL == 0b11 --> NOT POSSIBLE
516- */
517- #define LCD_PALETTE_ALL 0x0c
525+ # if PEANUT_GB_USE_NIBBLE_FOR_PALETTE
526+ /**
527+ * Bit mask for whether a pixel is OBJ0, OBJ1, or BG. Each may have a different
528+ * palette when playing a DMG game on CGB.
529+ */
530+ #define LCD_PALETTE_OBJ 0x04
531+ #define LCD_PALETTE_BG 0x08
532+ /**
533+ * Bit mask for the two bits listed above.
534+ * LCD_PALETTE_ALL == 0b00 --> OBJ0
535+ * LCD_PALETTE_ALL == 0b01 --> OBJ1
536+ * LCD_PALETTE_ALL == 0b10 --> BG
537+ * LCD_PALETTE_ALL == 0b11 --> NOT POSSIBLE
538+ */
539+ #define LCD_PALETTE_ALL 0x0c
540+ # else
541+ # if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
542+ # error PEANUT_GB_USE_NIBBLE_FOR_PALETTE must be enabled for PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE to work
543+ # endif
544+ #define LCD_PALETTE_OBJ 0x10
545+ #define LCD_PALETTE_BG 0x20
546+ #define LCD_PALETTE_ALL 0x30
547+ # endif
518548# else
519549 #define LCD_PALETTE_OBJ 0
520550 #define LCD_PALETTE_BG 0
@@ -1211,11 +1241,12 @@ void __gb_write(struct gb_s *gb, uint_fast16_t addr, uint8_t val)
12111241 gb -> display .bg_palette [1 ] = ((gb -> hram_io [IO_BGP ] >> 2 ) & 0x03 ) | LCD_PALETTE_BG ;
12121242 gb -> display .bg_palette [2 ] = ((gb -> hram_io [IO_BGP ] >> 4 ) & 0x03 ) | LCD_PALETTE_BG ;
12131243 gb -> display .bg_palette [3 ] = ((gb -> hram_io [IO_BGP ] >> 6 ) & 0x03 ) | LCD_PALETTE_BG ;
1214-
1244+ #if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
12151245 gb -> display .bg_palette [0 ] |= gb -> display .bg_palette [0 ] << 4 ;
12161246 gb -> display .bg_palette [1 ] |= gb -> display .bg_palette [1 ] << 4 ;
12171247 gb -> display .bg_palette [2 ] |= gb -> display .bg_palette [2 ] << 4 ;
12181248 gb -> display .bg_palette [3 ] |= gb -> display .bg_palette [3 ] << 4 ;
1249+ #endif
12191250 return ;
12201251
12211252 case 0x48 :
@@ -1224,11 +1255,12 @@ void __gb_write(struct gb_s *gb, uint_fast16_t addr, uint8_t val)
12241255 gb -> display .sp_palette [1 ] = ((gb -> hram_io [IO_OBP0 ] >> 2 ) & 0x03 );
12251256 gb -> display .sp_palette [2 ] = ((gb -> hram_io [IO_OBP0 ] >> 4 ) & 0x03 );
12261257 gb -> display .sp_palette [3 ] = ((gb -> hram_io [IO_OBP0 ] >> 6 ) & 0x03 );
1227-
1258+ #if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
12281259 gb -> display .sp_palette [0 ] |= gb -> display .sp_palette [0 ] << 4 ;
12291260 gb -> display .sp_palette [1 ] |= gb -> display .sp_palette [1 ] << 4 ;
12301261 gb -> display .sp_palette [2 ] |= gb -> display .sp_palette [2 ] << 4 ;
12311262 gb -> display .sp_palette [3 ] |= gb -> display .sp_palette [3 ] << 4 ;
1263+ #endif
12321264 return ;
12331265
12341266 case 0x49 :
@@ -1237,11 +1269,12 @@ void __gb_write(struct gb_s *gb, uint_fast16_t addr, uint8_t val)
12371269 gb -> display .sp_palette [5 ] = ((gb -> hram_io [IO_OBP1 ] >> 2 ) & 0x03 ) | LCD_PALETTE_OBJ ;
12381270 gb -> display .sp_palette [6 ] = ((gb -> hram_io [IO_OBP1 ] >> 4 ) & 0x03 ) | LCD_PALETTE_OBJ ;
12391271 gb -> display .sp_palette [7 ] = ((gb -> hram_io [IO_OBP1 ] >> 6 ) & 0x03 ) | LCD_PALETTE_OBJ ;
1240-
1272+ #if PEANUT_GB_USE_DOUBLE_WIDTH_PALETTE
12411273 gb -> display .sp_palette [4 ] |= gb -> display .sp_palette [4 ] << 4 ;
12421274 gb -> display .sp_palette [5 ] |= gb -> display .sp_palette [5 ] << 4 ;
12431275 gb -> display .sp_palette [6 ] |= gb -> display .sp_palette [6 ] << 4 ;
12441276 gb -> display .sp_palette [7 ] |= gb -> display .sp_palette [7 ] << 4 ;
1277+ #endif
12451278 return ;
12461279
12471280 /* Window Position Registers */
0 commit comments