cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

cgb.h (6844B)


      1/** @file gb/cgb.h
      2    Support for the Color GameBoy (CGB).
      3
      4    __Enabling CGB features__
      5
      6    To unlock and use CGB features and registers you need to
      7    change byte 0143h in the cartridge header.  Otherwise, the CGB
      8    will operate in monochrome "Non CGB" compatibility mode.
      9    \li Use a value of __80h__ for games that support CGB and monochrome gameboys
     10        \n (with Lcc: __-Wm-yc__, or makebin directly: __-yc__)
     11    \li Use a value of __C0h__ for CGB only games.
     12        \n (with Lcc: __-Wm-yC__, or makebin directly: __-yC__)
     13
     14    See the Pan Docs for more information CGB features.
     15*/
     16
     17#ifndef _CGB_H
     18#define _CGB_H
     19
     20#include <types.h>
     21#include <stdint.h>
     22
     23/** Macro to create a CGB palette color entry out of 5-bit color components.
     24
     25    @param r   5-bit Red Component, range 0 - 31 (31 brightest)
     26    @param g   5-bit Green Component, range 0 - 31 (31 brightest)
     27    @param b   5-bit Blue Component, range 0 - 31 (31 brightest)
     28
     29    The resulting format is bitpacked BGR-555 in a uint16_t.
     30
     31    @see set_bkg_palette(), set_sprite_palette(), RGB8(), RGBHTML()
     32 */
     33#define RGB(r, g, b) ((((uint16_t)(b) & 0x1f) << 10) | (((uint16_t)(g) & 0x1f) << 5) | (((uint16_t)(r) & 0x1f) << 0))
     34
     35/** Macro to create a CGB palette color entry out of 8-bit color components.
     36
     37    @param r   8-bit Red Component, range 0 - 255 (255 brightest)
     38    @param g   8-bit Green Component, range 0 - 255 (255 brightest)
     39    @param b   8-bit Blue Component, range 0 - 255 (255 brightest)
     40
     41    The resulting format is bitpacked BGR-555 in a uint16_t.
     42
     43    The lowest 3 bits of each color component are dropped during conversion.
     44
     45    @see set_bkg_palette(), set_sprite_palette(), RGB(), RGBHTML()
     46 */
     47#define RGB8(r, g, b) ((uint16_t)((r) >> 3) | ((uint16_t)((g) >> 3) << 5) | ((uint16_t)((b) >> 3) << 10))
     48
     49/** Macro to convert a 24 Bit RGB color to a CGB palette color entry.
     50
     51    @param RGB24bit   Bit packed RGB-888 color (0-255 for each color component).
     52
     53    The resulting format is bitpacked BGR-555 in a uint16_t.
     54
     55    The lowest 3 bits of each color component are dropped during conversion.
     56
     57    @see set_bkg_palette(), set_sprite_palette(), RGB(), RGB8()
     58 */
     59#define RGBHTML(RGB24bit) (RGB8((((RGB24bit) >> 16) & 0xFF), (((RGB24bit) >> 8) & 0xFF), ((RGB24bit) & 0xFF)))
     60
     61/** Common colors based on the EGA default palette.
     62 */
     63#define RGB_RED        RGB(31,  0,  0)
     64#define RGB_DARKRED    RGB(15,  0,  0)
     65#define RGB_GREEN      RGB( 0, 31,  0)
     66#define RGB_DARKGREEN  RGB( 0, 15,  0)
     67#define RGB_BLUE       RGB( 0,  0, 31)
     68#define RGB_DARKBLUE   RGB( 0,  0, 15)
     69#define RGB_YELLOW     RGB(31, 31,  0)
     70#define RGB_DARKYELLOW RGB(21, 21,  0)
     71#define RGB_CYAN       RGB( 0, 31, 31)
     72#define RGB_AQUA       RGB(28,  5, 22)
     73#define RGB_PINK       RGB(31,  0, 31)
     74#define RGB_PURPLE     RGB(21,  0, 21)
     75#define RGB_BLACK      RGB( 0,  0,  0)
     76#define RGB_DARKGRAY   RGB(10, 10, 10)
     77#define RGB_LIGHTGRAY  RGB(21, 21, 21)
     78#define RGB_WHITE      RGB(31, 31, 31)
     79
     80#define RGB_LIGHTFLESH RGB(30, 20, 15)
     81#define RGB_BROWN      RGB(10, 10,  0)
     82#define RGB_ORANGE     RGB(30, 20,  0)
     83#define RGB_TEAL       RGB(15, 15,  0)
     84
     85typedef uint16_t palette_color_t;   /**< 16 bit color entry */
     86
     87/** Set CGB background palette(s).
     88
     89    @param first_palette  Index of the first palette to write (0-7)
     90    @param nb_palettes    Number of palettes to write (1-8, max depends on first_palette)
     91    @param rgb_data       Pointer to source palette data
     92
     93    Writes __nb_palettes__ to background palette data starting
     94    at __first_palette__, Palette data is sourced from __rgb_data__.
     95
     96    \li Each Palette is 8 bytes in size: 4 colors x 2 bytes per palette color entry.
     97    \li Each color (4 per palette) is packed as BGR-555 format (1:5:5:5, MSBit [15] is unused).
     98    \li Each component (R, G, B) may have values from 0 - 31 (5 bits), 31 is brightest.
     99
    100    @see RGB(), set_bkg_palette_entry()
    101 */
    102void set_bkg_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL;
    103
    104/** Set CGB sprite palette(s).
    105
    106    @param first_palette  Index of the first palette to write (0-7)
    107    @param nb_palettes    Number of palettes to write (1-8, max depends on first_palette)
    108    @param rgb_data       Pointer to source palette data
    109
    110    Writes __nb_palettes__ to sprite palette data starting
    111    at __first_palette__, Palette data is sourced from __rgb_data__.
    112
    113    \li Each Palette is 8 bytes in size: 4 colors x 2 bytes per palette color entry.
    114    \li Each color (4 per palette) is packed as BGR-555 format (1:5:5:5, MSBit [15] is unused).
    115    \li Each component (R, G, B) may have values from 0 - 31 (5 bits), 31 is brightest.
    116
    117    @see RGB(), set_sprite_palette_entry()
    118 */
    119void set_sprite_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL;
    120
    121/** Sets a single color in the specified CGB background palette.
    122
    123    @param palette  Index of the palette to modify (0-7)
    124    @param entry    Index of color in palette to modify (0-3)
    125    @param rgb_data New color data in BGR 15bpp format.
    126
    127    @see set_bkg_palette(), RGB()
    128 */
    129
    130void set_bkg_palette_entry(uint8_t palette, uint8_t entry, uint16_t rgb_data) OLDCALL;
    131
    132/** Sets a single color in the specified CGB sprite palette.
    133
    134    @param palette  Index of the palette to modify (0-7)
    135    @param entry    Index of color in palette to modify (0-3)
    136    @param rgb_data New color data in BGR 15bpp format.
    137
    138    @see set_sprite_palette(), RGB()
    139 */
    140void set_sprite_palette_entry(uint8_t palette, uint8_t entry, uint16_t rgb_data) OLDCALL;
    141
    142/** Set CPU speed to slow (Normal Speed) operation.
    143
    144    Interrupts are temporarily disabled and then re-enabled during this call.
    145
    146    In this mode the CGB operates at the same speed as the DMG/Pocket/SGB models.
    147
    148    \li You can check to see if @ref _cpu == @ref CGB_TYPE before using this function.
    149
    150    @see cpu_fast()
    151 */
    152void cpu_slow();
    153
    154/** Set CPU speed to fast (CGB Double Speed) operation.
    155
    156    On startup the CGB operates in Normal Speed Mode and can be switched
    157    into Double speed mode (faster processing but also higher power consumption).
    158    See the Pan Docs for more information about which hardware features
    159    operate faster and which remain at Normal Speed.
    160
    161    \li Interrupts are temporarily disabled and then re-enabled during this call.
    162    \li You can check to see if @ref _cpu == @ref CGB_TYPE before using this function.
    163
    164    @see cpu_slow(), _cpu
    165*/
    166void cpu_fast();
    167
    168/** Set palette, compatible with the DMG/GBP.
    169
    170    The default/first CGB palettes for sprites and backgrounds are
    171    set to a similar default appearance as on the DMG/Pocket/SGB models.
    172    (White, Light Gray, Dark Gray, Black)
    173
    174    \li You can check to see if @ref _cpu == @ref CGB_TYPE before using this function.
    175 */
    176void set_default_palette();
    177
    178/** This function is obsolete
    179 */
    180void cgb_compatibility();
    181
    182#endif /* _CGB_H */