GBDK 2020 Docs  4.1.1
API Documentation for GBDK 2020
cgb.h File Reference
#include <types.h>
#include <stdint.h>

Go to the source code of this file.

Macros

#define RGB(r, g, b)   ((uint16_t)((((b) & 0x1f) << 10) | ((uint16_t)(((g) & 0x1f) << 5)) | (((r) & 0x1f) << 0)))
 
#define RGB8(r, g, b)   (((uint16_t)((((b) >> 3) & 0x1f) << 10)) | ((uint16_t)((((g) >> 3) & 0x1f) << 5)) | (((r) >> 3) & 0x1f))
 
#define RGBHTML(RGB24bit)   (RGB8((((RGB24bit) >> 16) & 0xff), (((RGB24bit) >> 8) & 0xff), ((RGB24bit) & 0xff)))
 
#define RGB_RED   RGB(31, 0, 0)
 
#define RGB_DARKRED   RGB(15, 0, 0)
 
#define RGB_GREEN   RGB( 0, 31, 0)
 
#define RGB_DARKGREEN   RGB( 0, 15, 0)
 
#define RGB_BLUE   RGB( 0, 0, 31)
 
#define RGB_DARKBLUE   RGB( 0, 0, 15)
 
#define RGB_YELLOW   RGB(31, 31, 0)
 
#define RGB_DARKYELLOW   RGB(21, 21, 0)
 
#define RGB_CYAN   RGB( 0, 31, 31)
 
#define RGB_AQUA   RGB(28, 5, 22)
 
#define RGB_PINK   RGB(31, 0, 31)
 
#define RGB_PURPLE   RGB(21, 0, 21)
 
#define RGB_BLACK   RGB( 0, 0, 0)
 
#define RGB_DARKGRAY   RGB(10, 10, 10)
 
#define RGB_LIGHTGRAY   RGB(21, 21, 21)
 
#define RGB_WHITE   RGB(31, 31, 31)
 
#define RGB_LIGHTFLESH   RGB(30, 20, 15)
 
#define RGB_BROWN   RGB(10, 10, 0)
 
#define RGB_ORANGE   RGB(30, 20, 0)
 
#define RGB_TEAL   RGB(15, 15, 0)
 

Typedefs

typedef uint16_t palette_color_t
 

Functions

void set_bkg_palette (uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL
 
void set_sprite_palette (uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL
 
void set_bkg_palette_entry (uint8_t palette, uint8_t entry, uint16_t rgb_data) OLDCALL
 
void set_sprite_palette_entry (uint8_t palette, uint8_t entry, uint16_t rgb_data) OLDCALL
 
void cpu_slow (void)
 
void cpu_fast (void)
 
void set_default_palette (void)
 
void cgb_compatibility (void)
 

Detailed Description

Support for the Color GameBoy (CGB).

Enabling CGB features

To unlock and use CGB features and registers you need to change byte 0143h in the cartridge header. Otherwise, the CGB will operate in monochrome "Non CGB" compatibility mode.

  • Use a value of 80h for games that support CGB and monochrome gameboys
    (with Lcc: -Wm-yc, or makebin directly: -yc)
  • Use a value of C0h for CGB only games.
    (with Lcc: -Wm-yC, or makebin directly: -yC)

See the Pan Docs for more information CGB features.

Macro Definition Documentation

◆ RGB

#define RGB (   r,
  g,
  b 
)    ((uint16_t)((((b) & 0x1f) << 10) | ((uint16_t)(((g) & 0x1f) << 5)) | (((r) & 0x1f) << 0)))

Macro to create a CGB palette color entry out of 5-bit color components.

Parameters
r5-bit Red Component, range 0 - 31 (31 brightest)
g5-bit Green Component, range 0 - 31 (31 brightest)
b5-bit Blue Component, range 0 - 31 (31 brightest)

The resulting format is bitpacked BGR-555 in a uint16_t.

See also
set_bkg_palette(), set_sprite_palette(), RGB8(), RGBHTML()

◆ RGB8

#define RGB8 (   r,
  g,
  b 
)    (((uint16_t)((((b) >> 3) & 0x1f) << 10)) | ((uint16_t)((((g) >> 3) & 0x1f) << 5)) | (((r) >> 3) & 0x1f))

Macro to create a CGB palette color entry out of 8-bit color components.

Parameters
r8-bit Red Component, range 0 - 255 (255 brightest)
g8-bit Green Component, range 0 - 255 (255 brightest)
b8-bit Blue Component, range 0 - 255 (255 brightest)

The resulting format is bitpacked BGR-555 in a uint16_t.

The lowest 3 bits of each color component are dropped during conversion.

See also
set_bkg_palette(), set_sprite_palette(), RGB(), RGBHTML()

◆ RGBHTML

#define RGBHTML (   RGB24bit)    (RGB8((((RGB24bit) >> 16) & 0xff), (((RGB24bit) >> 8) & 0xff), ((RGB24bit) & 0xff)))

Macro to convert a 24 Bit RGB color to a CGB palette color entry.

Parameters
RGB24bitBit packed RGB-888 color (0-255 for each color component).

The resulting format is bitpacked BGR-555 in a uint16_t.

The lowest 3 bits of each color component are dropped during conversion.

See also
set_bkg_palette(), set_sprite_palette(), RGB(), RGB8()

◆ RGB_RED

#define RGB_RED   RGB(31, 0, 0)

Common colors based on the EGA default palette.

◆ RGB_DARKRED

#define RGB_DARKRED   RGB(15, 0, 0)

◆ RGB_GREEN

#define RGB_GREEN   RGB( 0, 31, 0)

◆ RGB_DARKGREEN

#define RGB_DARKGREEN   RGB( 0, 15, 0)

◆ RGB_BLUE

#define RGB_BLUE   RGB( 0, 0, 31)

◆ RGB_DARKBLUE

#define RGB_DARKBLUE   RGB( 0, 0, 15)

◆ RGB_YELLOW

#define RGB_YELLOW   RGB(31, 31, 0)

◆ RGB_DARKYELLOW

#define RGB_DARKYELLOW   RGB(21, 21, 0)

◆ RGB_CYAN

#define RGB_CYAN   RGB( 0, 31, 31)

◆ RGB_AQUA

#define RGB_AQUA   RGB(28, 5, 22)

◆ RGB_PINK

#define RGB_PINK   RGB(31, 0, 31)

◆ RGB_PURPLE

#define RGB_PURPLE   RGB(21, 0, 21)

◆ RGB_BLACK

#define RGB_BLACK   RGB( 0, 0, 0)

◆ RGB_DARKGRAY

#define RGB_DARKGRAY   RGB(10, 10, 10)

◆ RGB_LIGHTGRAY

#define RGB_LIGHTGRAY   RGB(21, 21, 21)

◆ RGB_WHITE

#define RGB_WHITE   RGB(31, 31, 31)

◆ RGB_LIGHTFLESH

#define RGB_LIGHTFLESH   RGB(30, 20, 15)

◆ RGB_BROWN

#define RGB_BROWN   RGB(10, 10, 0)

◆ RGB_ORANGE

#define RGB_ORANGE   RGB(30, 20, 0)

◆ RGB_TEAL

#define RGB_TEAL   RGB(15, 15, 0)

Typedef Documentation

◆ palette_color_t

16 bit color entry

Function Documentation

◆ set_bkg_palette()

void set_bkg_palette ( uint8_t  first_palette,
uint8_t  nb_palettes,
palette_color_t rgb_data 
)

Set CGB background palette(s).

Parameters
first_paletteIndex of the first palette to write (0-7)
nb_palettesNumber of palettes to write (1-8, max depends on first_palette)
rgb_dataPointer to source palette data

Writes nb_palettes to background palette data starting at first_palette, Palette data is sourced from rgb_data.

  • Each Palette is 8 bytes in size: 4 colors x 2 bytes per palette color entry.
  • Each color (4 per palette) is packed as BGR-555 format (1:5:5:5, MSBit [15] is unused).
  • Each component (R, G, B) may have values from 0 - 31 (5 bits), 31 is brightest.
See also
RGB(), set_bkg_palette_entry()
BKGF_CGB_PAL0, BKGF_CGB_PAL1, BKGF_CGB_PAL2, BKGF_CGB_PAL3
BKGF_CGB_PAL4, BKGF_CGB_PAL5, BKGF_CGB_PAL6, BKGF_CGB_PAL7

◆ set_sprite_palette()

void set_sprite_palette ( uint8_t  first_palette,
uint8_t  nb_palettes,
palette_color_t rgb_data 
)

Set CGB sprite palette(s).

Parameters
first_paletteIndex of the first palette to write (0-7)
nb_palettesNumber of palettes to write (1-8, max depends on first_palette)
rgb_dataPointer to source palette data

Writes nb_palettes to sprite palette data starting at first_palette, Palette data is sourced from rgb_data.

  • Each Palette is 8 bytes in size: 4 colors x 2 bytes per palette color entry.
  • Each color (4 per palette) is packed as BGR-555 format (1:5:5:5, MSBit [15] is unused).
  • Each component (R, G, B) may have values from 0 - 31 (5 bits), 31 is brightest.
See also
RGB(), set_sprite_palette_entry()
OAMF_CGB_PAL0, OAMF_CGB_PAL1, OAMF_CGB_PAL2, OAMF_CGB_PAL3
OAMF_CGB_PAL4, OAMF_CGB_PAL5, OAMF_CGB_PAL6, OAMF_CGB_PAL7

◆ set_bkg_palette_entry()

void set_bkg_palette_entry ( uint8_t  palette,
uint8_t  entry,
uint16_t  rgb_data 
)

Sets a single color in the specified CGB background palette.

Parameters
paletteIndex of the palette to modify (0-7)
entryIndex of color in palette to modify (0-3)
rgb_dataNew color data in BGR 15bpp format.
See also
set_bkg_palette(), RGB()
BKGF_CGB_PAL0, BKGF_CGB_PAL1, BKGF_CGB_PAL2, BKGF_CGB_PAL3
BKGF_CGB_PAL4, BKGF_CGB_PAL5, BKGF_CGB_PAL6, BKGF_CGB_PAL7

◆ set_sprite_palette_entry()

void set_sprite_palette_entry ( uint8_t  palette,
uint8_t  entry,
uint16_t  rgb_data 
)

Sets a single color in the specified CGB sprite palette.

Parameters
paletteIndex of the palette to modify (0-7)
entryIndex of color in palette to modify (0-3)
rgb_dataNew color data in BGR 15bpp format.
See also
set_sprite_palette(), RGB()
OAMF_CGB_PAL0, OAMF_CGB_PAL1, OAMF_CGB_PAL2, OAMF_CGB_PAL3
OAMF_CGB_PAL4, OAMF_CGB_PAL5, OAMF_CGB_PAL6, OAMF_CGB_PAL7

◆ cpu_slow()

void cpu_slow ( void  )

Set CPU speed to slow (Normal Speed) operation.

Interrupts are temporarily disabled and then re-enabled during this call.

In this mode the CGB operates at the same speed as the DMG/Pocket/SGB models.

  • You can check to see if _cpu == CGB_TYPE before using this function.
See also
cpu_fast()

◆ cpu_fast()

void cpu_fast ( void  )
inline

Set CPU speed to fast (CGB Double Speed) operation.

On startup the CGB operates in Normal Speed Mode and can be switched into Double speed mode (faster processing but also higher power consumption). See the Pan Docs for more information about which hardware features operate faster and which remain at Normal Speed.

  • Interrupts are temporarily disabled and then re-enabled during this call.
  • You can check to see if _cpu == CGB_TYPE before using this function.
See also
cpu_slow(), _cpu

◆ set_default_palette()

void set_default_palette ( void  )

Sets CGB palette 0 to be compatible with the DMG/GBP.

The default/first CGB palettes for sprites and backgrounds are set to a similar default appearance as on the DMG/Pocket/SGB models. (White, Light Gray, Dark Gray, Black)

  • You can check to see if _cpu == CGB_TYPE before using this function.

◆ cgb_compatibility()

void cgb_compatibility ( void  )
inline

Obsolete. This function has been replaced by set_default_palette(), which has identical behavior.