cscg22-gearboy

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

gb.h (57605B)


      1/** @file gb/gb.h
      2    Gameboy specific functions.
      3*/
      4#ifndef _GB_H
      5#define _GB_H
      6
      7#include <types.h>
      8#include <stdint.h>
      9#include <gbdk/version.h>
     10#include <gb/hardware.h>
     11
     12#define NINTENDO
     13#ifdef SEGA
     14#undef SEGA
     15#endif
     16#if defined(__TARGET_ap)
     17#define ANALOGUEPOCKET
     18#elif defined(__TARGET_gb)
     19#define GAMEBOY
     20#elif defined(__TARGET_duck)
     21#define MEGADUCK
     22#endif
     23
     24
     25/** Joypad bits.
     26    A logical OR of these is used in the wait_pad and joypad
     27    functions.  For example, to see if the B button is pressed
     28    try
     29
     30    uint8_t keys;
     31    keys = joypad();
     32    if (keys & J_B) {
     33    	...
     34    }
     35
     36    @see joypad
     37 */
     38#define	J_UP         0x04U
     39#define	J_DOWN       0x08U
     40#define	J_LEFT       0x02U
     41#define	J_RIGHT      0x01U
     42#define	J_A          0x10U
     43#define	J_B          0x20U
     44#define	J_SELECT     0x40U
     45#define	J_START      0x80U
     46
     47/** Screen modes.
     48    Normally used by internal functions only.
     49    @see mode()
     50 */
     51#define	M_DRAWING    0x01U
     52#define	M_TEXT_OUT   0x02U
     53#define	M_TEXT_INOUT 0x03U
     54/** Set this in addition to the others to disable scrolling
     55
     56    If scrolling is disabled, the cursor returns to (0,0)
     57    @see mode()
     58*/
     59#define M_NO_SCROLL  0x04U
     60/** Set this to disable interpretation
     61    @see mode()
     62*/
     63#define M_NO_INTERP  0x08U
     64
     65/** If this is set, sprite colours come from OBJ1PAL. Else
     66    they come from OBJ0PAL
     67    @see set_sprite_prop().
     68*/
     69#define S_PALETTE    0x10U
     70/** If set the sprite will be flipped horizontally.
     71    @see set_sprite_prop()
     72 */
     73#define S_FLIPX      0x20U
     74/** If set the sprite will be flipped vertically.
     75    @see set_sprite_prop()
     76 */
     77#define S_FLIPY      0x40U
     78/** If this bit is clear, then the sprite will be displayed
     79    on top of the background and window.
     80    @see set_sprite_prop()
     81*/
     82#define S_PRIORITY   0x80U
     83
     84/* Interrupt flags */
     85/** Disable calling of interrupt service routines
     86 */
     87#define EMPTY_IFLAG  0x00U
     88/** VBlank Interrupt occurs at the start of the vertical blank.
     89
     90    During this period the video ram may be freely accessed.
     91    @see set_interrupts(), @see add_VBL
     92 */
     93#define VBL_IFLAG    0x01U
     94/** LCD Interrupt when triggered by the STAT register.
     95    @see set_interrupts(), @see add_LCD
     96*/
     97#define LCD_IFLAG    0x02U
     98/** Timer Interrupt when the timer @ref TIMA_REG overflows.
     99    @see set_interrupts(), @see add_TIM
    100 */
    101#define TIM_IFLAG    0x04U
    102/** Serial Link Interrupt occurs when the serial transfer has completed.
    103    @see set_interrupts(), @see add_SIO
    104 */
    105#define SIO_IFLAG    0x08U
    106/** Joypad Interrupt occurs on a transition of the keypad.
    107    @see set_interrupts(), @see add_JOY
    108 */
    109#define JOY_IFLAG    0x10U
    110
    111
    112/* DMG Palettes */
    113#define DMG_BLACK     0x03
    114#define DMG_DARK_GRAY 0x02
    115#define DMG_LITE_GRAY 0x01
    116#define DMG_WHITE     0x00
    117/** Macro to create a DMG palette from 4 colors
    118
    119    @param C0    Color for Index 0
    120    @param C1    Color for Index 1
    121    @param C2    Color for Index 2
    122    @param C3    Color for Index 3
    123
    124    The resulting format is four greyscale colors
    125    packed into a single unsigned byte.
    126
    127    Example:
    128    \code{.c}
    129    REG_BGP = DMG_PALETTE(DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE);
    130    \endcode
    131
    132    @see OBP0_REG, OBP1_REG, BGP_REG
    133    @see DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE
    134
    135 */
    136#define DMG_PALETTE(C0, C1, C2, C3) ((uint8_t)((((C3) & 0x03) << 6) | (((C2) & 0x03) << 4) | (((C1) & 0x03) << 2) | ((C0) & 0x03)))
    137
    138/* Limits */
    139/** Width of the visible screen in pixels.
    140 */
    141#define SCREENWIDTH  DEVICE_SCREEN_PX_WIDTH
    142/** Height of the visible screen in pixels.
    143 */
    144#define SCREENHEIGHT DEVICE_SCREEN_PX_HEIGHT
    145/** The Minimum X position of the Window Layer (Left edge of screen) @see move_win()
    146 */
    147#define MINWNDPOSX   0x07U
    148/** The Minimum Y position of the Window Layer (Top edge of screen) @see move_win()
    149 */
    150#define MINWNDPOSY   0x00U
    151/** The Maximum X position of the Window Layer (Right edge of screen) @see move_win()
    152 */
    153#define MAXWNDPOSX   0xA6U
    154/** The Maximum Y position of the Window Layer (Bottom edge of screen) @see move_win()
    155 */
    156#define MAXWNDPOSY   0x8FU
    157
    158
    159/** Interrupt handlers
    160 */
    161typedef void (*int_handler)(void) NONBANKED;
    162
    163/** The remove functions will remove any interrupt handler.
    164
    165   A handler of NULL will cause bad things
    166   to happen if the given interrupt is enabled.
    167
    168   Removes the VBL interrupt handler. @see add_VBL()
    169*/
    170void remove_VBL(int_handler h) OLDCALL;
    171
    172/** Removes the LCD interrupt handler.
    173    @see add_LCD(), remove_VBL()
    174*/
    175void remove_LCD(int_handler h) OLDCALL;
    176
    177/** Removes the TIM interrupt handler.
    178    @see add_TIM(), remove_VBL()
    179*/
    180void remove_TIM(int_handler h) OLDCALL;
    181
    182/** Removes the Serial Link / SIO interrupt handler.
    183   @see add_SIO(), @see remove_VBL()
    184
    185    The default SIO ISR gets installed automatically if
    186    any of the standard SIO calls are used. These calls
    187    include @ref add_SIO(), @ref remove_SIO(),
    188    @ref send_byte(), @ref receive_byte().
    189
    190    The default SIO ISR cannot be removed once installed.
    191    Only secondary chained SIO ISRs (added with @ref add_SIO() )
    192    can be removed.
    193*/
    194void remove_SIO(int_handler h) OLDCALL;
    195
    196/** Removes the JOY interrupt handler.
    197    @see add_JOY(), remove_VBL()
    198*/
    199void remove_JOY(int_handler h) OLDCALL;
    200
    201/** Adds a V-blank interrupt handler.
    202
    203    @param h  The handler to be called whenever a V-blank
    204    interrupt occurs.
    205
    206    Up to 4 handlers may be added, with the last added being
    207    called last.  If the @ref remove_VBL function is to be called,
    208    only three may be added.
    209
    210    Do not use @ref CRITICAL and @ref INTERRUPT attributes for a
    211    function added via add_VBL() (or LCD, etc). The attributes
    212    are only required when constructing a bare jump from the
    213    interrupt vector itself.
    214
    215    Note: The default VBL is installed automatically.
    216*/
    217void add_VBL(int_handler h) OLDCALL;
    218
    219/** Adds a LCD interrupt handler.
    220
    221    Called when the LCD interrupt occurs, which is normally
    222    when @ref LY_REG == @ref LYC_REG.
    223
    224    There are various reasons for this interrupt to occur
    225    as described by the @ref STAT_REG register ($FF41). One very
    226    popular reason is to indicate to the user when the
    227    video hardware is about to redraw a given LCD line.
    228    This can be useful for dynamically controlling the
    229    @ref SCX_REG / @ref SCY_REG registers ($FF43/$FF42) to perform
    230    special video effects.
    231
    232    @see add_VBL
    233*/
    234void add_LCD(int_handler h) OLDCALL;
    235
    236/** Adds a timer interrupt handler.
    237
    238    Can not be used together with @ref add_low_priority_TIM
    239
    240    This interrupt occurs when the @ref TIMA_REG
    241    register ($FF05) changes from $FF to $00.
    242
    243    @see add_VBL
    244    @see set_interrupts() with TIM_IFLAG
    245*/
    246void add_TIM(int_handler h) OLDCALL;
    247
    248/** Adds a timer interrupt handler, that could be 
    249    interrupted by the other interrupts, 
    250    as well as itself, if it runs too slow.
    251
    252    Can not be used together with @ref add_TIM
    253
    254    This interrupt occurs when the @ref TIMA_REG
    255    register ($FF05) changes from $FF to $00.
    256
    257    @see add_VBL
    258    @see set_interrupts() with TIM_IFLAG
    259*/
    260void add_low_priority_TIM(int_handler h) OLDCALL;
    261
    262/** Adds a Serial Link transmit complete interrupt handler.
    263
    264    This interrupt occurs when a serial transfer has
    265    completed on the game link port.
    266
    267    @see send_byte, receive_byte(), add_VBL()
    268    @see set_interrupts() with SIO_IFLAG
    269*/
    270void add_SIO(int_handler h) OLDCALL;
    271
    272
    273/** Adds a joypad button change interrupt handler.
    274
    275    This interrupt occurs on a transition of any of the
    276    keypad input lines from high to low. Due to the fact
    277    that keypad "bounce" is virtually always present,
    278    software should expect this interrupt to occur one
    279    or more times for every button press and one or more
    280    times for every button release.
    281
    282    @see joypad(), add_VBL()
    283*/
    284void add_JOY(int_handler h) OLDCALL;
    285
    286
    287/** Interrupt handler chain terminator that does __not__ wait for .STAT
    288
    289    You must add this handler last in every interrupt handler
    290    chain if you want to change the default interrupt handler
    291    behaviour that waits for LCD controller mode to become 1 or 0
    292    before return from the interrupt.
    293
    294    Example:
    295    \code{.c}
    296    CRITICAL {
    297        add_SIO(nowait_int_handler); // Disable wait on VRAM state before returning from SIO interrupt
    298    }
    299    \endcode
    300    @see wait_int_handler()
    301*/
    302void nowait_int_handler();
    303
    304
    305/** Default Interrupt handler chain terminator that waits for
    306    @see STAT_REG and __only__ returns at the BEGINNING of
    307    either Mode 0 or Mode 1.
    308
    309    Used by default at the end of interrupt chains to help
    310    prevent graphical glitches. The glitches are caused when an
    311    ISR interrupts a graphics operation in one mode but returns
    312    in a different mode for which that graphics operation is not
    313    allowed.
    314
    315    @see nowait_int_handler()
    316*/
    317void wait_int_handler();
    318
    319/** Cancel pending interrupts
    320 */
    321inline uint8_t cancel_pending_interrupts() {
    322    return IF_REG = 0;
    323}
    324
    325/** Set the current screen mode - one of M_* modes
    326
    327    Normally used by internal functions only.
    328
    329    @see M_DRAWING, M_TEXT_OUT, M_TEXT_INOUT, M_NO_SCROLL, M_NO_INTERP
    330*/
    331void mode(uint8_t m) OLDCALL;
    332
    333/** Returns the current mode
    334
    335    @see M_DRAWING, M_TEXT_OUT, M_TEXT_INOUT, M_NO_SCROLL, M_NO_INTERP
    336*/
    337uint8_t get_mode() OLDCALL PRESERVES_REGS(b, c);
    338
    339/** GB CPU type
    340
    341    @see DMG_TYPE, MGB_TYPE, CGB_TYPE, cpu_fast(), cpu_slow(), _is_GBA
    342*/
    343extern uint8_t _cpu;
    344
    345/** Hardware Model: Original GB or Super GB. @see _cpu
    346*/
    347#define DMG_TYPE 0x01
    348/** Hardware Model: Pocket GB or Super GB 2. @see _cpu
    349*/
    350#define MGB_TYPE 0xFF
    351/** Hardware Model: Color GB. @see _cpu
    352*/
    353#define CGB_TYPE 0x11
    354
    355/** GBA detection
    356
    357    @see GBA_DETECTED, GBA_NOT_DETECTED, _cpu
    358*/
    359extern uint8_t _is_GBA;
    360
    361/** Hardware Model: DMG, CGB or MGB. @see _cpu, _is_GBA
    362*/
    363#define GBA_NOT_DETECTED 0x00
    364/** Hardware Model: GBA. @see _cpu, _is_GBA
    365*/
    366#define GBA_DETECTED 0x01
    367
    368/** Macro returns TRUE if device supports color
    369 */
    370#define DEVICE_SUPPORTS_COLOR (_cpu == CGB_TYPE)
    371
    372/** Global Time Counter in VBL periods (60Hz)
    373
    374    Increments once per Frame
    375
    376    Will wrap around every ~18 minutes (unsigned 16 bits = 65535 / 60 / 60 = 18.2)
    377*/
    378extern volatile uint16_t sys_time;
    379
    380
    381
    382/** Serial Link: Send the byte in @ref _io_out out through the serial port
    383
    384    Make sure to enable interrupts for the
    385    Serial Link before trying to transfer data.
    386    @see add_SIO(), remove_SIO()
    387    @see set_interrupts() with @ref SIO_IFLAG
    388*/
    389void send_byte();
    390
    391/** Serial Link: Receive a byte from the serial port into @ref _io_in
    392
    393    Make sure to enable interrupts for the
    394    Serial Link before trying to transfer data.
    395    @see add_SIO(), remove_SIO()
    396    @see set_interrupts() with @ref SIO_IFLAG
    397*/
    398void receive_byte();
    399
    400/** Serial Link: Current IO Status. An OR of IO_* */
    401extern volatile uint8_t _io_status;
    402
    403/** Serial Link: Byte just read after calling @ref receive_byte()
    404*/
    405extern volatile uint8_t _io_in;
    406
    407/** Serial Link: Write byte to send here before calling @ref send_byte()
    408*/
    409extern volatile uint8_t _io_out;
    410
    411/* Status codes */
    412/** Serial Link IO is completed */
    413#define IO_IDLE		0x00U
    414/** Serial Link Sending data */
    415#define IO_SENDING	0x01U
    416/** Serial Link Receiving data */
    417#define IO_RECEIVING	0x02U
    418/** Serial Link Error */
    419#define IO_ERROR	0x04U
    420
    421
    422
    423/** Tracks current active ROM bank @see SWITCH_ROM_MBC1(), SWITCH_ROM_MBC5()
    424    This variable is updated automatically when you call SWITCH_ROM_MBC1 or
    425    SWITCH_ROM_MBC5, or call a BANKED function.
    426*/
    427__REG _current_bank;
    428#define CURRENT_BANK _current_bank
    429
    430/** Obtains the __bank number__ of VARNAME
    431
    432    @param VARNAME Name of the variable which has a __bank_VARNAME companion symbol which is adjusted by bankpack
    433
    434    Use this to obtain the bank number from a bank reference
    435    created with @ref BANKREF().
    436
    437    @see BANKREF_EXTERN(), BANKREF()
    438*/
    439#ifndef BANK
    440#define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME )
    441#endif
    442
    443/** Creates a reference for retrieving the bank number of a variable or function
    444
    445    @param VARNAME Variable name to use, which may be an existing identifier
    446
    447    @see BANK() for obtaining the bank number of the included data.
    448
    449    More than one `BANKREF()` may be created per file, but each call should
    450    always use a unique VARNAME.
    451
    452    Use @ref BANKREF_EXTERN() within another source file
    453    to make the variable and it's data accesible there.
    454*/
    455#define BANKREF(VARNAME) void __func_ ## VARNAME() __banked __naked { \
    456__asm \
    457    .local b___func_ ## VARNAME \
    458    ___bank_ ## VARNAME = b___func_ ## VARNAME \
    459    .globl ___bank_ ## VARNAME \
    460__endasm; \
    461}
    462
    463/** Creates extern references for accessing a BANKREF() generated variable.
    464
    465    @param VARNAME Name of the variable used with @ref BANKREF()
    466
    467    This makes a @ref BANKREF() reference in another source
    468    file accessible in the current file for use with @ref BANK().
    469
    470    @see BANKREF(), BANK()
    471*/
    472#define BANKREF_EXTERN(VARNAME) extern const void __bank_ ## VARNAME;
    473
    474/** Makes MEGADUCK MBC switch the active ROM bank
    475    @param b   ROM bank to switch to
    476*/
    477#define SWITCH_ROM_MEGADUCK(b) \
    478  _current_bank = (b), *(uint8_t *)0x0001 = (b)
    479
    480
    481/** Makes MBC1 and other compatible MBCs switch the active ROM bank
    482    @param b   ROM bank to switch to
    483*/
    484#define SWITCH_ROM_MBC1(b) \
    485  _current_bank = (b), *(uint8_t *)0x2000 = (b)
    486
    487/** Makes default platform MBC switch the active ROM bank
    488    @param b   ROM bank to switch to (max 255)
    489
    490    @see SWITCH_ROM_MBC1, SWITCH_ROM_MBC5, SWITCH_ROM_MEGADUCK
    491*/
    492#if defined(__TARGET_duck)
    493#define SWITCH_ROM SWITCH_ROM_MEGADUCK
    494#else
    495#define SWITCH_ROM SWITCH_ROM_MBC1
    496#endif
    497
    498/** Switches SRAM bank on MBC1 and other compaticle MBCs
    499    @param b   SRAM bank to switch to
    500*/
    501#define SWITCH_RAM_MBC1(b) \
    502  *(uint8_t *)0x4000 = (b)
    503
    504/** Switches SRAM bank on MBC1 and other compaticle MBCs
    505    @param b   SRAM bank to switch to
    506
    507    @see SWITCH_RAM_MBC1, SWITCH_RAM_MBC5
    508*/
    509#define SWITCH_RAM SWITCH_RAM_MBC1
    510
    511/** Enables SRAM on MBC1
    512*/
    513#define ENABLE_RAM_MBC1 \
    514  *(uint8_t *)0x0000 = 0x0A
    515
    516#define ENABLE_RAM ENABLE_RAM_MBC1
    517
    518/** Disables SRAM on MBC1
    519*/
    520#define DISABLE_RAM_MBC1 \
    521  *(uint8_t *)0x0000 = 0x00
    522
    523#define DISABLE_RAM DISABLE_RAM_MBC1
    524
    525#define SWITCH_16_8_MODE_MBC1 \
    526  *(uint8_t *)0x6000 = 0x00
    527
    528#define SWITCH_4_32_MODE_MBC1 \
    529  *(uint8_t *)0x6000 = 0x01
    530
    531/** Makes MBC5 switch to the active ROM bank; only 4M roms are supported, @see SWITCH_ROM_MBC5_8M()
    532    @param b   ROM bank to switch to
    533
    534    Note the order used here. Writing the other way around on a MBC1 always selects bank 1
    535*/
    536#define SWITCH_ROM_MBC5(b) \
    537  _current_bank = (b), \
    538  *(uint8_t *)0x3000 = 0, \
    539  *(uint8_t *)0x2000 = (b)
    540
    541/** Makes MBC5 to switch the active ROM bank; active bank number is not tracked by _current_bank if you use this macro
    542    @see _current_bank
    543    @param b   ROM bank to switch to
    544
    545    Note the order used here. Writing the other way around on a MBC1 always selects bank 1
    546*/
    547#define SWITCH_ROM_MBC5_8M(b) \
    548  *(uint8_t *)0x3000 = ((uint16_t)(b) >> 8), \
    549  *(uint8_t *)0x2000 = (b)
    550
    551/** Switches SRAM bank on MBC5
    552    @param b   SRAM bank to switch to
    553*/
    554#define SWITCH_RAM_MBC5(b) \
    555  *(uint8_t *)0x4000 = (b)
    556
    557/** Enables SRAM on MBC5
    558*/
    559#define ENABLE_RAM_MBC5 \
    560  *(uint8_t *)0x0000 = 0x0A
    561
    562/** Disables SRAM on MBC5
    563*/
    564#define DISABLE_RAM_MBC5 \
    565  *(uint8_t *)0x0000 = 0x00
    566
    567
    568
    569/** Delays the given number of milliseconds.
    570    Uses no timers or interrupts, and can be called with
    571    interrupts disabled
    572 */
    573void delay(uint16_t d) OLDCALL;
    574
    575
    576
    577/** Reads and returns the current state of the joypad.
    578    Follows Nintendo's guidelines for reading the pad.
    579    Return value is an OR of J_*
    580
    581    When testing for multiple different buttons, it's
    582    best to read the joypad state *once* into a variable
    583    and then test using that variable.
    584
    585    @see J_START, J_SELECT, J_A, J_B, J_UP, J_DOWN, J_LEFT, J_RIGHT
    586*/
    587uint8_t joypad() OLDCALL PRESERVES_REGS(b, c, h, l);
    588
    589/** Waits until at least one of the buttons given in mask are pressed.
    590
    591    @param mask Bitmask indicating which buttons to wait for
    592
    593    Normally only used for checking one key, but it will
    594    support many, even J_LEFT at the same time as J_RIGHT. :)
    595
    596    Note: Checks in a loop that doesn't HALT at all, so the CPU
    597    will be maxed out until this call returns.
    598    @see joypad
    599    @see J_START, J_SELECT, J_A, J_B, J_UP, J_DOWN, J_LEFT, J_RIGHT
    600*/
    601uint8_t waitpad(uint8_t mask) OLDCALL PRESERVES_REGS(b, c);
    602
    603/** Waits for the directional pad and all buttons to be released.
    604
    605    Note: Checks in a loop that doesn't HALT at all, so the CPU
    606    will be maxed out until this call returns.
    607*/
    608void waitpadup() PRESERVES_REGS(a, b, c, d, e, h, l);
    609
    610/** Multiplayer joypad structure.
    611
    612    Must be initialized with @ref joypad_init() first then it
    613    may be used to poll all avaliable joypads with @ref joypad_ex()
    614*/
    615typedef struct {
    616    uint8_t npads;
    617    union {
    618        struct {
    619            uint8_t joy0, joy1, joy2, joy3;
    620        };
    621        uint8_t joypads[4];
    622    };
    623} joypads_t;
    624
    625/** Initializes joypads_t structure for polling multiple joypads
    626    (for the GB and ones connected via SGB)
    627    @param npads	number of joypads requested (1, 2 or 4)
    628    @param joypads	pointer to joypads_t structure to be initialized
    629
    630    Only required for @ref joypad_ex, not required for calls to regular @ref joypad()
    631    @returns number of joypads avaliable
    632    @see joypad_ex(), joypads_t
    633*/
    634uint8_t joypad_init(uint8_t npads, joypads_t * joypads) OLDCALL;
    635
    636/** Polls all avaliable joypads (for the GB and ones connected via SGB)
    637    @param joypads	pointer to joypads_t structure to be filled with joypad statuses,
    638    	   must be previously initialized with joypad_init()
    639
    640    @see joypad_init(), joypads_t
    641*/
    642void joypad_ex(joypads_t * joypads) OLDCALL PRESERVES_REGS(b, c);
    643
    644
    645
    646/** Enables unmasked interrupts
    647
    648    @note Use @ref CRITICAL {...} instead for creating a block of
    649          of code which should execute with interrupts  temporarily
    650          turned off.
    651
    652    @see disable_interrupts, set_interrupts, CRITICAL
    653*/
    654inline void enable_interrupts() PRESERVES_REGS(a, b, c, d, e, h, l) {
    655    __asm__("ei");
    656}
    657
    658/** Disables interrupts
    659
    660    @note Use @ref CRITICAL {...} instead for creating a block of
    661          of code which should execute with interrupts  temporarily
    662          turned off.
    663
    664    This function may be called as many times as you like;
    665    however the first call to @ref enable_interrupts will re-enable
    666    them.
    667
    668    @see enable_interrupts, set_interrupts, CRITICAL
    669*/
    670inline void disable_interrupts() PRESERVES_REGS(a, b, c, d, e, h, l) {
    671    __asm__("di");
    672}
    673
    674/** Clears any pending interrupts and sets the interrupt mask
    675    register IO to flags.
    676    @param flags	A logical OR of *_IFLAGS
    677
    678    @note: This disables and then re-enables interrupts so it
    679           must be used outside of a critical section.
    680
    681    @see enable_interrupts(), disable_interrupts()
    682    @see VBL_IFLAG, LCD_IFLAG, TIM_IFLAG, SIO_IFLAG, JOY_IFLAG
    683*/
    684void set_interrupts(uint8_t flags) OLDCALL PRESERVES_REGS(b, c, d, e);
    685
    686/** Performs a warm reset by reloading the CPU value
    687    then jumping to the start of crt0 (0x0150)
    688*/
    689void reset();
    690
    691/** HALTs the CPU and waits for the vertical blank interrupt (VBL) to finish.
    692
    693    This is often used in main loops to idle the CPU at low power
    694    until it's time to start the next frame. It's also useful for
    695    syncing animation with the screen re-draw.
    696
    697    Warning: If the VBL interrupt is disabled, this function will
    698    never return. If the screen is off this function returns
    699    immediately.
    700*/
    701void wait_vbl_done() PRESERVES_REGS(b, c, d, e, h, l);
    702
    703/** Turns the display off.
    704
    705    Waits until the VBL interrupt before turning the display off.
    706    @see DISPLAY_ON
    707*/
    708void display_off() PRESERVES_REGS(b, c, d, e, h, l);
    709
    710/** Copies data from shadow OAM to OAM
    711 */
    712void refresh_OAM() PRESERVES_REGS(b, c, d, e, h, l);
    713
    714
    715/** Copies data from somewhere in the lower address space to part of hi-ram.
    716    @param dst		Offset in high ram (0xFF00 and above) to copy to.
    717    @param src		Area to copy from
    718    @param n		Number of bytes to copy.
    719*/
    720void hiramcpy(uint8_t dst, const void *src, uint8_t n) OLDCALL PRESERVES_REGS(b, c);
    721
    722
    723/** Turns the display back on.
    724    @see display_off, DISPLAY_OFF
    725*/
    726#define DISPLAY_ON \
    727  LCDC_REG|=LCDCF_ON
    728
    729/** Turns the display off immediately.
    730    @see display_off, DISPLAY_ON
    731*/
    732#define DISPLAY_OFF \
    733  display_off();
    734
    735/** Does nothing for GB
    736 */
    737#define HIDE_LEFT_COLUMN
    738
    739/** Does nothing for GB
    740 */
    741#define SHOW_LEFT_COLUMN
    742
    743/** Turns on the background layer.
    744    Sets bit 0 of the LCDC register to 1.
    745*/
    746#define SHOW_BKG \
    747  LCDC_REG|=LCDCF_BGON
    748
    749/** Turns off the background layer.
    750    Sets bit 0 of the LCDC register to 0.
    751*/
    752#define HIDE_BKG \
    753  LCDC_REG&=~LCDCF_BGON
    754
    755/** Turns on the window layer
    756    Sets bit 5 of the LCDC register to 1.
    757*/
    758#define SHOW_WIN \
    759  LCDC_REG|=LCDCF_WINON
    760
    761/** Turns off the window layer.
    762    Clears bit 5 of the LCDC register to 0.
    763*/
    764#define HIDE_WIN \
    765  LCDC_REG&=~LCDCF_WINON
    766
    767/** Turns on the sprites layer.
    768    Sets bit 1 of the LCDC register to 1.
    769*/
    770#define SHOW_SPRITES \
    771  LCDC_REG|=LCDCF_OBJON
    772
    773/** Turns off the sprites layer.
    774    Clears bit 1 of the LCDC register to 0.
    775*/
    776#define HIDE_SPRITES \
    777  LCDC_REG&=~LCDCF_OBJON
    778
    779/** Sets sprite size to 8x16 pixels, two tiles one above the other.
    780    Sets bit 2 of the LCDC register to 1.
    781*/
    782#define SPRITES_8x16 \
    783  LCDC_REG|=LCDCF_OBJ16
    784
    785/** Sets sprite size to 8x8 pixels, one tile.
    786    Clears bit 2 of the LCDC register to 0.
    787*/
    788#define SPRITES_8x8 \
    789  LCDC_REG&=~LCDCF_OBJ16
    790
    791
    792
    793/**
    794 * Set byte in vram at given memory location
    795 *
    796 * @param addr address to write to
    797 * @param v value
    798 */
    799void set_vram_byte(uint8_t * addr, uint8_t v) OLDCALL PRESERVES_REGS(b, c);
    800
    801/**
    802 * Get byte from vram at given memory location
    803 *
    804 * @param addr address to read from
    805 * @return read value
    806 */
    807uint8_t get_vram_byte(uint8_t * addr) OLDCALL PRESERVES_REGS(b, c);
    808
    809
    810/**
    811 * Get address of X,Y tile of background map
    812 */
    813uint8_t * get_bkg_xy_addr(uint8_t x, uint8_t y) OLDCALL PRESERVES_REGS(b, c);
    814
    815#define COMPAT_PALETTE(C0,C1,C2,C3) ((uint8_t)(((C3) << 6) | ((C2) << 4) | ((C1) << 2) | (C0)))
    816
    817/** Sets palette for 2bpp color translation for GG/SMS, does nothing on GB
    818 */
    819inline void set_2bpp_palette(uint16_t palette) {
    820    palette;
    821}
    822
    823extern uint16_t _current_1bpp_colors;
    824void set_1bpp_colors_ex(uint8_t fgcolor, uint8_t bgcolor, uint8_t mode) OLDCALL;
    825inline void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor) {
    826    set_1bpp_colors_ex(fgcolor, bgcolor, 0);
    827}
    828
    829/** Sets VRAM Tile Pattern data for the Background / Window
    830
    831    @param first_tile  Index of the first tile to write
    832    @param nb_tiles    Number of tiles to write
    833    @param data        Pointer to (2 bpp) source tile data
    834
    835    Writes __nb_tiles__ tiles to VRAM starting at __first_tile__, tile data
    836    is sourced from __data__. Each Tile is 16 bytes in size (8x8 pixels, 2 bits-per-pixel).
    837
    838    Note: Sprite Tiles 128-255 share the same memory region as Background Tiles 128-255.
    839
    840    GBC only: @ref VBK_REG determines which bank of Background tile patterns are written to.
    841    \li VBK_REG=0 indicates the first bank
    842    \li VBK_REG=1 indicates the second
    843
    844    @see set_win_data, set_tile_data
    845*/
    846void set_bkg_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
    847#define set_bkg_2bpp_data set_bkg_data
    848
    849/** Sets VRAM Tile Pattern data for the Background / Window using 1bpp source data
    850
    851    @param first_tile  Index of the first Tile to write
    852    @param nb_tiles    Number of Tiles to write
    853    @param data        Pointer to (1bpp) source Tile Pattern data
    854
    855    Similar to @ref set_bkg_data, except source data is 1 bit-per-pixel
    856    which gets expanded into 2 bits-per-pixel.
    857
    858    For a given bit that represent a pixel:
    859    \li 0 will be expanded into color 0
    860    \li 1 will be expanded into color 1, 2 or 3 depending on color argument
    861
    862    @see SHOW_BKG, HIDE_BKG, set_bkg_tiles
    863*/
    864void set_bkg_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
    865
    866/** Copies from Background / Window VRAM Tile Pattern data into a buffer
    867
    868    @param first_tile  Index of the first Tile to read from
    869    @param nb_tiles    Number of Tiles to read
    870    @param data        Pointer to destination buffer for Tile Pattern data
    871
    872    Copies __nb_tiles__ tiles from VRAM starting at __first_tile__, Tile data
    873    is copied into __data__.
    874
    875    Each Tile is 16 bytes, so the buffer pointed to by __data__
    876    should be at least __nb_tiles__ x 16 bytes in size.
    877
    878    @see get_win_data, get_data
    879*/
    880void get_bkg_data(uint8_t first_tile, uint8_t nb_tiles, uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
    881
    882
    883/** Sets a rectangular region of Background Tile Map.
    884
    885    @param x      X Start position in Background Map tile coordinates. Range 0 - 31
    886    @param y      Y Start position in Background Map tile coordinates. Range 0 - 31
    887    @param w      Width of area to set in tiles. Range 1 - 32
    888    @param h      Height of area to set in tiles. Range 1 - 32
    889    @param tiles  Pointer to source tile map data
    890
    891    Entries are copied from map at __tiles__ to the Background Tile Map starting at
    892    __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
    893
    894    Use @ref set_bkg_submap() instead when:
    895    \li Source map is wider than 32 tiles.
    896    \li Writing a width that does not match the source map width __and__ more
    897    than one row high at a time.
    898
    899    One byte per source tile map entry.
    900
    901    Writes that exceed coordinate 31 on the x or y axis will wrap around to
    902    the Left and Top edges.
    903
    904    Note: Patterns 128-255 overlap with patterns 128-255 of the sprite Tile Pattern table.
    905
    906    GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
    907    \li VBK_REG=0 Tile Numbers are written
    908    \li VBK_REG=1 Tile Attributes are written
    909
    910    GBC Tile Attributes are defined as:
    911    \li Bit 7 - Priority flag. When this is set, it puts the tile above the sprites
    912              with colour 0 being transparent.
    913              \n 0: Below sprites
    914              \n 1: Above sprites
    915              \n Note: @ref SHOW_BKG needs to be set for these priorities to take place.
    916    \li Bit 6 - Vertical flip. Dictates which way up the tile is drawn vertically.
    917              \n 0: Normal
    918              \n 1: Flipped Vertically
    919    \li Bit 5 - Horizontal flip. Dictates which way up the tile is drawn horizontally.
    920              \n 0: Normal
    921              \n 1: Flipped Horizontally
    922    \li Bit 4 - Not used
    923    \li Bit 3 - Character Bank specification. Dictates from which bank of
    924              Background Tile Patterns the tile is taken.
    925              \n 0: Bank 0
    926              \n 1: Bank 1
    927    \li Bit 2 - See bit 0.
    928    \li Bit 1 - See bit 0.
    929    \li Bit 0 - Bits 0-2 indicate which of the 7 BKG colour palettes the tile is
    930              assigned.
    931
    932    @see SHOW_BKG
    933    @see set_bkg_data, set_bkg_submap, set_win_tiles, set_tiles
    934*/
    935void set_bkg_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
    936#define set_tile_map set_bkg_tiles
    937
    938
    939extern uint8_t _map_tile_offset;
    940
    941/** Sets a rectangular region of Background Tile Map.
    942    The offset value in __base_tile__ is added to
    943    the tile ID for each map entry.
    944
    945    @param x      X Start position in Background Map tile coordinates. Range 0 - 31
    946    @param y      Y Start position in Background Map tile coordinates. Range 0 - 31
    947    @param w      Width of area to set in tiles. Range 1 - 32
    948    @param h      Height of area to set in tiles. Range 1 - 32
    949    @param tiles  Pointer to source tile map data
    950    @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
    951
    952    This is identical to @ref set_bkg_tiles() except that it
    953    adds the __base_tile__ parameter for when a tile map's tiles don't
    954    start at index zero. (For example, the tiles used by the map
    955    range from 100 -> 120 in VRAM instead of 0 -> 20).
    956
    957    @see set_bkg_tiles for more details
    958*/
    959inline void set_bkg_based_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles, uint8_t base_tile) {
    960    _map_tile_offset = base_tile;
    961    set_bkg_tiles(x, y, w, h, tiles);
    962    _map_tile_offset = 0;
    963}
    964
    965
    966/** Sets a rectangular area of the Background Tile Map using a sub-region
    967    from a source tile map. Useful for scrolling implementations of maps
    968    larger than 32 x 32 tiles.
    969
    970    @param x      X Start position in Background Map tile coordinates. Range 0 - 31
    971    @param y      Y Start position in Background Map tile coordinates. Range 0 - 31
    972    @param w      Width of area to set in tiles. Range 1 - 255
    973    @param h      Height of area to set in tiles. Range 1 - 255
    974    @param map    Pointer to source tile map data
    975    @param map_w  Width of source tile map in tiles. Range 1 - 255
    976
    977    Entries are copied from __map__ to the Background Tile Map starting at
    978    __x__, __y__ writing across for __w__ tiles and down for __h__ tiles,
    979    using __map_w__ as the rowstride for the source tile map.
    980
    981    Use this instead of @ref set_bkg_tiles when the source map is wider than
    982    32 tiles or when writing a width that does not match the source map width.
    983
    984    One byte per source tile map entry.
    985
    986    Writes that exceed coordinate 31 on the x or y axis will wrap around to
    987    the Left and Top edges.
    988
    989    See @ref set_bkg_tiles for setting CGB attribute maps with @ref VBK_REG.
    990
    991    @see SHOW_BKG
    992    @see set_bkg_data, set_bkg_tiles, set_win_submap, set_tiles
    993*/
    994void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) OLDCALL;
    995#define set_tile_submap set_bkg_submap
    996
    997
    998extern uint8_t _submap_tile_offset;
    999
   1000/** Sets a rectangular area of the Background Tile Map using a sub-region
   1001    from a source tile map. The offset value in __base_tile__ is added to
   1002    the tile ID for each map entry.
   1003
   1004    @param x      X Start position in Background Map tile coordinates. Range 0 - 31
   1005    @param y      Y Start position in Background Map tile coordinates. Range 0 - 31
   1006    @param w      Width of area to set in tiles. Range 1 - 255
   1007    @param h      Height of area to set in tiles. Range 1 - 255
   1008    @param map    Pointer to source tile map data
   1009    @param map_w  Width of source tile map in tiles. Range 1 - 255
   1010    @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
   1011
   1012    This is identical to @ref set_bkg_based_submap() except that it
   1013    adds the __base_tile__ parameter for when a tile map's tiles don't
   1014    start at index zero. (For example, the tiles used by the map
   1015    range from 100 -> 120 in VRAM instead of 0 -> 20).
   1016
   1017    @see set_bkg_based_submap for more details
   1018*/
   1019inline void set_bkg_based_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w, uint8_t base_tile) {
   1020    _submap_tile_offset = base_tile;
   1021    set_bkg_submap(x, y, w, h, map, map_w);
   1022    _submap_tile_offset = 0;
   1023}
   1024
   1025
   1026/** Copies a rectangular region of Background Tile Map entries into a buffer.
   1027
   1028    @param x      X Start position in Background Map tile coordinates. Range 0 - 31
   1029    @param y      Y Start position in Background Map tile coordinates. Range 0 - 31
   1030    @param w      Width of area to copy in tiles. Range 0 - 31
   1031    @param h      Height of area to copy in tiles. Range 0 - 31
   1032    @param tiles  Pointer to destination buffer for Tile Map data
   1033
   1034
   1035    Entries are copied into __tiles__ from the Background Tile Map starting at
   1036    __x__, __y__ reading across for __w__ tiles and down for __h__ tiles.
   1037
   1038    One byte per tile.
   1039
   1040    The buffer pointed to by __tiles__ should be at least __x__ x __y__ bytes in size.
   1041
   1042    @see get_win_tiles, get_bkg_tile_xy, get_tiles, get_vram_byte
   1043*/
   1044void get_bkg_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
   1045
   1046
   1047/**
   1048 * Set single tile t on background layer at x,y
   1049 * @param x X-coordinate
   1050 * @param y Y-coordinate
   1051 * @param t tile index
   1052 * @return returns the address of tile, so you may use faster set_vram_byte() later
   1053 */
   1054uint8_t * set_bkg_tile_xy(uint8_t x, uint8_t y, uint8_t t) OLDCALL PRESERVES_REGS(b, c);
   1055#define set_tile_xy set_bkg_tile_xy
   1056
   1057/**
   1058 * Get single tile t on background layer at x,y
   1059 * @param x X-coordinate
   1060 * @param y Y-coordinate
   1061 * @return returns tile index
   1062 */
   1063uint8_t get_bkg_tile_xy(uint8_t x, uint8_t y) OLDCALL PRESERVES_REGS(b, c);
   1064
   1065
   1066/** Moves the Background Layer to the position specified in __x__ and __y__ in pixels.
   1067
   1068    @param x   X axis screen coordinate for Left edge of the Background
   1069    @param y   Y axis screen coordinate for Top edge of the Background
   1070
   1071    0,0 is the top left corner of the GB screen. The Background Layer wraps around the screen,
   1072    so when part of it goes off the screen it appears on the opposite side (factoring in the
   1073    larger size of the Background Layer versus the screen size).
   1074
   1075    The background layer is always under the Window Layer.
   1076
   1077    @see SHOW_BKG, HIDE_BKG
   1078*/
   1079inline void move_bkg(uint8_t x, uint8_t y) {
   1080    SCX_REG=x, SCY_REG=y;
   1081}
   1082
   1083
   1084/** Moves the Background relative to it's current position.
   1085
   1086    @param x   Number of pixels to move the Background on the __X axis__
   1087               \n Range: -128 - 127
   1088    @param y   Number of pixels to move the Background on the __Y axis__
   1089               \n Range: -128 - 127
   1090
   1091    @see move_bkg
   1092*/
   1093inline void scroll_bkg(int8_t x, int8_t y) {
   1094    SCX_REG+=x, SCY_REG+=y;
   1095}
   1096
   1097
   1098
   1099/**
   1100 * Get address of X,Y tile of window map
   1101 */
   1102uint8_t * get_win_xy_addr(uint8_t x, uint8_t y) OLDCALL PRESERVES_REGS(b, c);
   1103
   1104/** Sets VRAM Tile Pattern data for the Window / Background
   1105
   1106    @param first_tile  Index of the first tile to write
   1107    @param nb_tiles    Number of tiles to write
   1108    @param data        Pointer to (2 bpp) source Tile Pattern data.
   1109
   1110    This is the same as @ref set_bkg_data, since the Window Layer and
   1111    Background Layer share the same Tile pattern data.
   1112
   1113    @see set_bkg_data
   1114    @see set_win_tiles, set_bkg_data, set_data
   1115    @see SHOW_WIN, HIDE_WIN
   1116*/
   1117void set_win_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
   1118
   1119
   1120/** Sets VRAM Tile Pattern data for the Window / Background using 1bpp source data
   1121
   1122    @param first_tile  Index of the first tile to write
   1123    @param nb_tiles    Number of tiles to write
   1124    @param data        Pointer to (1bpp) source Tile Pattern data
   1125
   1126    This is the same as @ref set_bkg_1bpp_data, since the Window Layer and
   1127    Background Layer share the same Tile pattern data.
   1128
   1129    @see set_bkg_data, set_bkg_1bpp_data, set_win_data
   1130*/
   1131void set_win_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
   1132
   1133
   1134/** Copies from Window / Background VRAM Tile Pattern data into a buffer
   1135
   1136    @param first_tile  Index of the first Tile to read from
   1137    @param nb_tiles    Number of Tiles to read
   1138    @param data        Pointer to destination buffer for Tile Pattern Data
   1139
   1140    This is the same as @ref get_bkg_data, since the Window Layer and
   1141    Background Layer share the same Tile pattern data.
   1142
   1143    @see get_bkg_data, get_data
   1144*/
   1145void get_win_data(uint8_t first_tile, uint8_t nb_tiles, uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
   1146
   1147
   1148/** Sets a rectangular region of the Window Tile Map.
   1149
   1150    @param x      X Start position in Window Map tile coordinates. Range 0 - 31
   1151    @param y      Y Start position in Window Map tile coordinates. Range 0 - 31
   1152    @param w      Width of area to set in tiles. Range 1 - 32
   1153    @param h      Height of area to set in tiles. Range 1 - 32
   1154    @param tiles  Pointer to source tile map data
   1155
   1156    Entries are copied from map at __tiles__ to the Window Tile Map starting at
   1157    __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
   1158
   1159    Use @ref set_win_submap() instead when:
   1160    \li Source map is wider than 32 tiles.
   1161    \li Writing a width that does not match the source map width __and__ more
   1162    than one row high at a time.
   1163
   1164    One byte per source tile map entry.
   1165
   1166    Writes that exceed coordinate 31 on the x or y axis will wrap around to
   1167    the Left and Top edges.
   1168
   1169    Note: Patterns 128-255 overlap with patterns 128-255 of the sprite Tile Pattern table.
   1170
   1171    GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
   1172    \li VBK_REG=0 Tile Numbers are written
   1173    \li VBK_REG=1 Tile Attributes are written
   1174
   1175    For more details about GBC Tile Attributes see @ref set_bkg_tiles.
   1176
   1177    @see SHOW_WIN, HIDE_WIN, set_win_submap, set_bkg_tiles, set_bkg_data, set_tiles
   1178*/
   1179void set_win_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
   1180
   1181
   1182/** Sets a rectangular region of the Window Tile Map.
   1183    The offset value in __base_tile__ is added to
   1184    the tile ID for each map entry.
   1185
   1186    @param x      X Start position in Window Map tile coordinates. Range 0 - 31
   1187    @param y      Y Start position in Window Map tile coordinates. Range 0 - 31
   1188    @param w      Width of area to set in tiles. Range 1 - 32
   1189    @param h      Height of area to set in tiles. Range 1 - 32
   1190    @param tiles  Pointer to source tile map data
   1191    @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
   1192
   1193    This is identical to @ref set_win_tiles() except that it
   1194    adds the __base_tile__ parameter for when a tile map's tiles don't
   1195    start at index zero. (For example, the tiles used by the map
   1196    range from 100 -> 120 in VRAM instead of 0 -> 20).
   1197
   1198    @see set_win_tiles for more details
   1199*/
   1200inline void set_win_based_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles, uint8_t base_tile) {
   1201    _map_tile_offset = base_tile;
   1202    set_win_tiles(x, y, w, h, tiles);
   1203    _map_tile_offset = 0;
   1204}
   1205
   1206/** Sets a rectangular area of the Window Tile Map using a sub-region
   1207    from a source tile map.
   1208
   1209    @param x      X Start position in Window Map tile coordinates. Range 0 - 31
   1210    @param y      Y Start position in Wimdpw Map tile coordinates. Range 0 - 31
   1211    @param w      Width of area to set in tiles. Range 1 - 255
   1212    @param h      Height of area to set in tiles. Range 1 - 255
   1213    @param map    Pointer to source tile map data
   1214    @param map_w  Width of source tile map in tiles. Range 1 - 255
   1215
   1216    Entries are copied from __map__ to the Window Tile Map starting at
   1217    __x__, __y__ writing across for __w__ tiles and down for __h__ tiles,
   1218    using __map_w__ as the rowstride for the source tile map.
   1219
   1220    Use this instead of @ref set_win_tiles when the source map is wider than
   1221    32 tiles or when writing a width that does not match the source map width.
   1222
   1223    One byte per source tile map entry.
   1224
   1225    Writes that exceed coordinate 31 on the x or y axis will wrap around to
   1226    the Left and Top edges.
   1227
   1228    GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
   1229    \li VBK_REG=0 Tile Numbers are written
   1230    \li VBK_REG=1 Tile Attributes are written
   1231
   1232    See @ref set_bkg_tiles for details about CGB attribute maps with @ref VBK_REG.
   1233
   1234    @see SHOW_WIN, HIDE_WIN, set_win_tiles, set_bkg_submap, set_bkg_tiles, set_bkg_data, set_tiles
   1235**/
   1236void set_win_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) OLDCALL;
   1237
   1238
   1239/** Sets a rectangular area of the Window Tile Map using a sub-region
   1240    from a source tile map. The offset value in __base_tile__ is added
   1241    to the tile ID for each map entry.
   1242
   1243    @param x         X Start position in Window Map tile coordinates. Range 0 - 31
   1244    @param y         Y Start position in Wimdpw Map tile coordinates. Range 0 - 31
   1245    @param w         Width of area to set in tiles. Range 1 - 255
   1246    @param h         Height of area to set in tiles. Range 1 - 255
   1247    @param map       Pointer to source tile map data
   1248    @param map_w     Width of source tile map in tiles. Range 1 - 255
   1249    @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
   1250
   1251    This is identical to @ref set_win_submap() except that it
   1252    adds the __base_tile__ parameter for when a tile map's tiles don't
   1253    start at index zero. (For example, the tiles used by the map
   1254    range from 100 -> 120 in VRAM instead of 0 -> 20).
   1255
   1256    @see set_win_submap for more details
   1257**/
   1258inline void set_win_based_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w, uint8_t base_tile) {
   1259    _submap_tile_offset = base_tile;
   1260    set_win_submap(x, y, w, h, map, map_w);
   1261    _submap_tile_offset = 0;
   1262}
   1263
   1264
   1265/** Copies a rectangular region of Window Tile Map entries into a buffer.
   1266
   1267    @param x      X Start position in Window Map tile coordinates. Range 0 - 31
   1268    @param y      Y Start position in Window Map tile coordinates. Range 0 - 31
   1269    @param w      Width of area to copy in tiles. Range 0 - 31
   1270    @param h      Height of area to copy in tiles. Range 0 - 31
   1271    @param tiles  Pointer to destination buffer for Tile Map data
   1272
   1273    Entries are copied into __tiles__ from the Window Tile Map starting at
   1274    __x__, __y__ reading across for __w__ tiles and down for __h__ tiles.
   1275
   1276    One byte per tile.
   1277
   1278    The buffer pointed to by __tiles__ should be at least __x__ x __y__ bytes in size.
   1279
   1280    @see get_bkg_tiles, get_bkg_tile_xy, get_tiles, get_vram_byte
   1281*/
   1282void get_win_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
   1283
   1284
   1285/**
   1286 * Set single tile t on window layer at x,y
   1287 * @param x X-coordinate
   1288 * @param y Y-coordinate
   1289 * @param t tile index
   1290 * @return returns the address of tile, so you may use faster set_vram_byte() later
   1291 */
   1292uint8_t * set_win_tile_xy(uint8_t x, uint8_t y, uint8_t t) OLDCALL PRESERVES_REGS(b, c);
   1293
   1294
   1295/**
   1296 * Get single tile t on window layer at x,y
   1297 * @param x X-coordinate
   1298 * @param y Y-coordinate
   1299 * @return returns the tile index
   1300 */
   1301uint8_t get_win_tile_xy(uint8_t x, uint8_t y) OLDCALL PRESERVES_REGS(b, c);
   1302
   1303
   1304/** Moves the Window to the __x__, __y__ position on the screen.
   1305
   1306    @param x   X coordinate for Left edge of the Window (actual displayed location will be X - 7)
   1307    @param y   Y coordinate for Top edge of the Window
   1308
   1309    7,0 is the top left corner of the screen in Window coordinates. The Window is locked to the bottom right corner.
   1310
   1311    The Window is always over the Background layer.
   1312
   1313    @see SHOW_WIN, HIDE_WIN
   1314*/
   1315inline void move_win(uint8_t x, uint8_t y) {
   1316    WX_REG=x, WY_REG=y;
   1317}
   1318
   1319
   1320/** Move the Window relative to its current position.
   1321
   1322    @param x   Number of pixels to move the window on the __X axis__
   1323               \n Range: -128 - 127
   1324    @param y   Number of pixels to move the window on the __Y axis__
   1325               \n Range: -128 - 127
   1326
   1327    @see move_win
   1328*/
   1329inline void scroll_win(int8_t x, int8_t y) {
   1330    WX_REG+=x, WY_REG+=y;
   1331}
   1332
   1333
   1334
   1335/** Sets VRAM Tile Pattern data for Sprites
   1336
   1337    @param first_tile  Index of the first tile to write
   1338    @param nb_tiles    Number of tiles to write
   1339    @param data        Pointer to (2 bpp) source Tile Pattern data
   1340
   1341    Writes __nb_tiles__ tiles to VRAM starting at __first_tile__, tile data
   1342    is sourced from __data__. Each Tile is 16 bytes in size (8x8 pixels, 2 bits-per-pixel).
   1343
   1344    Note: Sprite Tiles 128-255 share the same memory region as Background Tiles 128-255.
   1345
   1346    GBC only: @ref VBK_REG determines which bank of Background tile patterns are written to.
   1347    \li VBK_REG=0 indicates the first bank
   1348    \li VBK_REG=1 indicates the second
   1349*/
   1350void set_sprite_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
   1351#define set_sprite_2bpp_data set_sprite_data
   1352
   1353/** Sets VRAM Tile Pattern data for Sprites using 1bpp source data
   1354
   1355    @param first_tile  Index of the first tile to write
   1356    @param nb_tiles    Number of tiles to write
   1357    @param data        Pointer to (1bpp) source Tile Pattern data
   1358
   1359    Similar to @ref set_sprite_data, except source data is 1 bit-per-pixel
   1360    which gets expanded into 2 bits-per-pixel.
   1361
   1362    For a given bit that represent a pixel:
   1363    \li 0 will be expanded into color 0
   1364    \li 1 will be expanded into color 3
   1365
   1366    @see SHOW_SPRITES, HIDE_SPRITES, set_sprite_tile
   1367*/
   1368void set_sprite_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
   1369
   1370/** Copies from Sprite VRAM Tile Pattern data into a buffer
   1371
   1372    @param first_tile  Index of the first tile to read from
   1373    @param nb_tiles    Number of tiles to read
   1374    @param data        Pointer to destination buffer for Tile Pattern data
   1375
   1376    Copies __nb_tiles__ tiles from VRAM starting at __first_tile__, tile data
   1377    is copied into __data__.
   1378
   1379    Each Tile is 16 bytes, so the buffer pointed to by __data__
   1380    should be at least __nb_tiles__ x 16 bytes in size.
   1381*/
   1382void get_sprite_data(uint8_t first_tile, uint8_t nb_tiles, uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
   1383
   1384
   1385/** Sprite Attributes structure
   1386    @param x     X Coordinate of the sprite on screen
   1387    @param y     Y Coordinate of the sprite on screen
   1388    @param tile  Sprite tile number (see @ref set_sprite_tile)
   1389    @param prop  OAM Property Flags (see @ref set_sprite_prop)
   1390*/
   1391typedef struct OAM_item_t {
   1392    uint8_t y, x;  //< X, Y Coordinates of the sprite on screen
   1393    uint8_t tile;  //< Sprite tile number
   1394    uint8_t prop;  //< OAM Property Flags
   1395} OAM_item_t;
   1396
   1397
   1398/** Shadow OAM array in WRAM, that is DMA-transferred into the real OAM each VBlank
   1399*/
   1400extern volatile struct OAM_item_t shadow_OAM[];
   1401
   1402/** MSB of shadow_OAM address is used by OAM DMA copying routine
   1403*/
   1404__REG _shadow_OAM_base;
   1405
   1406#define DISABLE_OAM_DMA \
   1407    _shadow_OAM_base = 0
   1408
   1409/** Disable OAM DMA copy each VBlank
   1410*/
   1411#define DISABLE_VBL_TRANSFER DISABLE_OAM_DMA
   1412
   1413#define ENABLE_OAM_DMA \
   1414    _shadow_OAM_base = (uint8_t)((uint16_t)&shadow_OAM >> 8)
   1415
   1416/** Enable OAM DMA copy each VBlank and set it to transfer default shadow_OAM array
   1417*/
   1418#define ENABLE_VBL_TRANSFER ENABLE_OAM_DMA
   1419
   1420/** Amount of hardware sprites in OAM
   1421*/
   1422#define MAX_HARDWARE_SPRITES 40
   1423
   1424/** Enable OAM DMA copy each VBlank and set it to transfer any 256-byte aligned array
   1425*/
   1426inline void SET_SHADOW_OAM_ADDRESS(void * address) {
   1427    _shadow_OAM_base = (uint8_t)((uint16_t)address >> 8);
   1428}
   1429
   1430/** Sets sprite number __nb__in the OAM to display tile number __tile__.
   1431
   1432    @param nb    Sprite number, range 0 - 39
   1433    @param tile  Selects a tile (0 - 255) from memory at 8000h - 8FFFh
   1434                 \n In CGB Mode this could be either in VRAM Bank
   1435                 \n 0 or 1, depending on Bit 3 of the OAM Attribute Flag
   1436                 \n (see @ref set_sprite_prop)
   1437
   1438    In 8x16 mode:
   1439    \li The sprite will also display the next tile (__tile__ + 1)
   1440        directly below (y + 8) the first tile.
   1441    \li The lower bit of the tile number is ignored:
   1442        the upper 8x8 tile is (__tile__ & 0xFE), and
   1443        the lower 8x8 tile is (__tile__ | 0x01).
   1444    \li See: @ref SPRITES_8x16
   1445*/
   1446inline void set_sprite_tile(uint8_t nb, uint8_t tile) {
   1447    shadow_OAM[nb].tile=tile;
   1448}
   1449
   1450
   1451/** Returns the tile number of sprite number __nb__ in the OAM.
   1452
   1453@param nb    Sprite number, range 0 - 39
   1454
   1455@see set_sprite_tile for more details
   1456*/
   1457inline uint8_t get_sprite_tile(uint8_t nb) {
   1458    return shadow_OAM[nb].tile;
   1459}
   1460
   1461
   1462/** Sets the OAM Property Flags of sprite number __nb__ to those defined in __prop__.
   1463
   1464    @param nb    Sprite number, range 0 - 39
   1465    @param prop  Property setting (see bitfield description)
   1466
   1467    The bits in __prop__ represent:
   1468    \li Bit 7 - Priority flag. When this is set the sprites appear behind the
   1469              background and window layer.
   1470              \n 0: infront
   1471              \n 1: behind
   1472    \li Bit 6 - Vertical flip. Dictates which way up the sprite is drawn
   1473              vertically.
   1474              \n 0: normal
   1475              \n 1:upside down
   1476    \li Bit 5 - Horizontal flip. Dictates which way up the sprite is
   1477              drawn horizontally.
   1478              \n 0: normal
   1479              \n  1:back to front
   1480    \li Bit 4 - DMG/Non-CGB Mode Only. Assigns either one of the two b/w palettes to the sprite.
   1481              \n 0: OBJ palette 0
   1482              \n 1: OBJ palette 1
   1483    \li Bit 3 - GBC only. Dictates from which bank of Sprite Tile Patterns the tile
   1484              is taken.
   1485              \n 0: Bank 0
   1486              \n 1: Bank 1
   1487    \li Bit 2 - See bit 0.
   1488    \li Bit 1 - See bit 0.
   1489    \li Bit 0 - GBC only. Bits 0-2 indicate which of the 7 OBJ colour palettes the
   1490              sprite is assigned.
   1491*/
   1492inline void set_sprite_prop(uint8_t nb, uint8_t prop) {
   1493    shadow_OAM[nb].prop=prop;
   1494}
   1495
   1496
   1497/** Returns the OAM Property Flags of sprite number __nb__.
   1498
   1499    @param nb    Sprite number, range 0 - 39
   1500    @see set_sprite_prop for property bitfield settings
   1501*/
   1502inline uint8_t get_sprite_prop(uint8_t nb) {
   1503    return shadow_OAM[nb].prop;
   1504}
   1505
   1506
   1507/** Moves sprite number __nb__ to the __x__, __y__ position on the screen.
   1508
   1509    @param nb  Sprite number, range 0 - 39
   1510    @param x   X Position. Specifies the sprites horizontal position on the screen (minus 8).
   1511               \n An offscreen value (X=0 or X>=168) hides the sprite, but the sprite
   1512               still affects the priority ordering - a better way to hide a sprite is to set
   1513               its Y-coordinate offscreen.
   1514    @param y   Y Position. Specifies the sprites vertical position on the screen (minus 16).
   1515               \n An offscreen value (for example, Y=0 or Y>=160) hides the sprite.
   1516
   1517    Moving the sprite to 0,0 (or similar off-screen location) will hide it.
   1518*/
   1519inline void move_sprite(uint8_t nb, uint8_t x, uint8_t y) {
   1520    OAM_item_t * itm = &shadow_OAM[nb];
   1521    itm->y=y, itm->x=x;
   1522}
   1523
   1524
   1525/** Moves sprite number __nb__ relative to its current position.
   1526
   1527    @param nb  Sprite number, range 0 - 39
   1528    @param x   Number of pixels to move the sprite on the __X axis__
   1529               \n Range: -128 - 127
   1530    @param y   Number of pixels to move the sprite on the __Y axis__
   1531               \n Range: -128 - 127
   1532
   1533    @see move_sprite for more details about the X and Y position
   1534 */
   1535inline void scroll_sprite(uint8_t nb, int8_t x, int8_t y) {
   1536    OAM_item_t * itm = &shadow_OAM[nb];
   1537    itm->y+=y, itm->x+=x;
   1538}
   1539
   1540
   1541/** Hides sprite number __nb__ by moving it to zero position by Y.
   1542
   1543    @param nb  Sprite number, range 0 - 39
   1544 */
   1545inline void hide_sprite(uint8_t nb) {
   1546    shadow_OAM[nb].y = 0;
   1547}
   1548
   1549
   1550
   1551/** Copies arbitrary data to an address in VRAM
   1552    without taking into account the state of LCDC bits 3 or 4.
   1553
   1554    @param vram_addr Pointer to destination VRAM Address
   1555    @param data      Pointer to source buffer
   1556    @param len       Number of bytes to copy
   1557
   1558    Copies __len__ bytes from a buffer at __data__ to VRAM starting at __vram_addr__.
   1559
   1560    GBC only: @ref VBK_REG determines which bank of Background tile patterns are written to.
   1561    \li VBK_REG=0 indicates the first bank
   1562    \li VBK_REG=1 indicates the second
   1563
   1564    @see set_bkg_data, set_win_data, set_bkg_tiles, set_win_tiles, set_tile_data, set_tiles
   1565*/
   1566void set_data(uint8_t *vram_addr, const uint8_t *data, uint16_t len) OLDCALL PRESERVES_REGS(b, c);
   1567
   1568
   1569/** Copies arbitrary data from an address in VRAM into a buffer
   1570    without taking into account the state of LCDC bits 3 or 4.
   1571
   1572    @param vram_addr Pointer to source VRAM Address
   1573    @param data      Pointer to destination buffer
   1574    @param len       Number of bytes to copy
   1575
   1576    Copies __len__ bytes from VRAM starting at __vram_addr__ into a buffer at __data__.
   1577
   1578    GBC only: @ref VBK_REG determines which bank of Background tile patterns are written to.
   1579    \li VBK_REG=0 indicates the first bank
   1580    \li VBK_REG=1 indicates the second
   1581
   1582    @see get_bkg_data, get_win_data, get_bkg_tiles, get_win_tiles, get_tiles
   1583*/
   1584void get_data(uint8_t *data, uint8_t *vram_addr, uint16_t len) OLDCALL PRESERVES_REGS(b, c);
   1585
   1586/** Copies arbitrary data from an address in VRAM into a buffer
   1587
   1588    @param dest      Pointer to destination buffer (may be in VRAM)
   1589    @param sour      Pointer to source buffer (may be in VRAM)
   1590    @param len       Number of bytes to copy
   1591
   1592    Copies __len__ bytes from or to VRAM starting at __sour__ into a buffer or to VRAM at __dest__.
   1593
   1594    GBC only: @ref VBK_REG determines which bank of Background tile patterns are written to.
   1595    \li VBK_REG=0 indicates the first bank
   1596    \li VBK_REG=1 indicates the second
   1597*/
   1598void vmemcpy(uint8_t *dest, uint8_t *sour, uint16_t len) OLDCALL PRESERVES_REGS(b, c);
   1599
   1600
   1601
   1602/** Sets a rectangular region of Tile Map entries at a given VRAM Address
   1603    without taking into account the state of LCDC bit 3.
   1604
   1605    @param x         X Start position in Map tile coordinates. Range 0 - 31
   1606    @param y         Y Start position in Map tile coordinates. Range 0 - 31
   1607    @param w         Width of area to set in tiles. Range 1 - 32
   1608    @param h         Height of area to set in tiles.   Range 1 - 32
   1609    @param vram_addr Pointer to destination VRAM Address
   1610    @param tiles     Pointer to source Tile Map data
   1611
   1612    Entries are copied from __tiles__ to Tile Map at address vram_addr starting at
   1613    __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
   1614
   1615    One byte per source tile map entry.
   1616
   1617    There are two 32x32 Tile Maps in VRAM at addresses 9800h-9BFFh and 9C00h-9FFFh.
   1618
   1619    GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
   1620    \li VBK_REG=0 Tile Numbers are written
   1621    \li VBK_REG=1 Tile Attributes are written
   1622
   1623    @see set_bkg_tiles, set_win_tiles
   1624*/
   1625void set_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *vram_addr, const uint8_t *tiles) OLDCALL;
   1626
   1627/** Sets VRAM Tile Pattern data starting from given base address
   1628    without taking into account the state of LCDC bit 4.
   1629
   1630    @param first_tile  Index of the first tile to write
   1631    @param nb_tiles    Number of tiles to write
   1632    @param data        Pointer to (2 bpp) source Tile Pattern data.
   1633	@param base        MSB of the destination address in VRAM (usually 0x80 or 0x90 which gives 0x8000 or 0x9000)
   1634
   1635    @see set_bkg_data, set_win_data, set_data
   1636*/
   1637void set_tile_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data, uint8_t base) OLDCALL PRESERVES_REGS(b, c);
   1638
   1639/** Copies a rectangular region of Tile Map entries from a given VRAM Address into a buffer
   1640    without taking into account the state of LCDC bit 3.
   1641
   1642    @param x         X Start position in Background Map tile coordinates. Range 0 - 31
   1643    @param y         Y Start position in Background Map tile coordinates. Range 0 - 31
   1644    @param w         Width of area to copy in tiles. Range 0 - 31
   1645    @param h         Height of area to copy in tiles. Range 0 - 31
   1646    @param vram_addr Pointer to source VRAM Address
   1647    @param tiles     Pointer to destination buffer for Tile Map data
   1648
   1649    Entries are copied into __tiles__ from the Background Tile Map starting at
   1650    __x__, __y__ reading across for __w__ tiles and down for __h__ tiles.
   1651
   1652    One byte per tile.
   1653
   1654    There are two 32x32 Tile Maps in VRAM at addresses 9800h - 9BFFh and 9C00h - 9FFFh.
   1655
   1656    The buffer pointed to by __tiles__ should be at least __x__ x __y__ bytes in size.
   1657
   1658    @see get_bkg_tiles, get_win_tiles
   1659*/
   1660void get_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *vram_addr, uint8_t *tiles) OLDCALL;
   1661
   1662
   1663/** Sets VRAM Tile Pattern data in the native format
   1664
   1665    @param first_tile  Index of the first tile to write (0 - 511)
   1666    @param nb_tiles    Number of tiles to write
   1667    @param data        Pointer to source Tile Pattern data.
   1668
   1669    When `first_tile` is larger than 256 on the GB/AP, it
   1670    will write to sprite data instead of background data.
   1671
   1672    The bit depth of the source Tile Pattern data depends
   1673    on which console is being used:
   1674    \li Game Boy/Analogue Pocket: loads 2bpp tiles data
   1675    \li SMS/GG: loads 4bpp tile data
   1676 */
   1677inline void set_native_tile_data(uint16_t first_tile, uint8_t nb_tiles, const uint8_t *data) {
   1678    if (first_tile < 256) {
   1679        set_bkg_data(first_tile, nb_tiles, data);
   1680    } else {
   1681        set_sprite_data(first_tile - 256, nb_tiles, data);
   1682    }
   1683}
   1684
   1685
   1686/** Initializes the entire Window Tile Map with Tile Number __c__
   1687    @param c   Tile number to fill with
   1688
   1689    Note: This function avoids writes during modes 2 & 3
   1690*/
   1691void init_win(uint8_t c) OLDCALL PRESERVES_REGS(b, c);
   1692
   1693/** Initializes the entire Background Tile Map with Tile Number __c__
   1694    @param c   Tile number to fill with
   1695
   1696    Note: This function avoids writes during modes 2 & 3
   1697*/
   1698void init_bkg(uint8_t c) OLDCALL PRESERVES_REGS(b, c);
   1699
   1700/** Fills the VRAM memory region __s__ of size __n__ with Tile Number __c__
   1701    @param s   Start address in VRAM
   1702    @param c   Tile number to fill with
   1703    @param n   Size of memory region (in bytes) to fill
   1704
   1705    Note: This function avoids writes during modes 2 & 3
   1706*/
   1707void vmemset (void *s, uint8_t c, size_t n) OLDCALL PRESERVES_REGS(b, c);
   1708
   1709
   1710
   1711/** Fills a rectangular region of Tile Map entries for the Background layer with tile.
   1712
   1713    @param x      X Start position in Background Map tile coordinates. Range 0 - 31
   1714    @param y      Y Start position in Background Map tile coordinates. Range 0 - 31
   1715    @param w      Width of area to set in tiles. Range 0 - 31
   1716    @param h      Height of area to set in tiles. Range 0 - 31
   1717    @param tile   Fill value
   1718*/
   1719void fill_bkg_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t tile) OLDCALL PRESERVES_REGS(b, c);
   1720#define fill_rect fill_bkg_rect
   1721
   1722/** Fills a rectangular region of Tile Map entries for the Window layer with tile.
   1723
   1724    @param x      X Start position in Window Map tile coordinates. Range 0 - 31
   1725    @param y      Y Start position in Window Map tile coordinates. Range 0 - 31
   1726    @param w      Width of area to set in tiles. Range 0 - 31
   1727    @param h      Height of area to set in tiles. Range 0 - 31
   1728    @param tile   Fill value
   1729*/
   1730void fill_win_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t tile) OLDCALL PRESERVES_REGS(b, c);
   1731
   1732#endif /* _GB_H */