Main Page   Compound List   File List   Header Files   File Members  

gb.h File Reference

Gameboy specific functions. More...


Defines

#define J_START
Joypad bits. More...

#define M_DRAWING
Screen modes. More...

#define M_NO_SCROLL
Set this in addition to the others to disable scrolling If scrolling is disabled, the cursor returns to (0,0).

#define M_NO_INTERP
Set this to disable \n interpretation.

#define S_PALETTE
If this is set, sprite colours come from OBJ1PAL. More...

#define S_FLIPX
If set the sprite will be flipped horizontally.

#define S_FLIPY
If set the sprite will be flipped vertically.

#define S_PRIORITY
If this bit is clear, then the sprite will be displayed ontop of the background and window.

#define VBL_IFLAG
Vertical blank interrupt. More...

#define LCD_IFLAG
Interrupt when triggered by the STAT register. More...

#define TIM_IFLAG
Interrupt when the timer TIMA overflows.

#define SIO_IFLAG
Occurs when the serial transfer has completed.

#define JOY_IFLAG
Occurs on a transition of the keypad.

#define SCREENWIDTH
Width of the visible screen in pixels.

#define SCREENHEIGHT
Height of the visible screen in pixels.

#define DMG_TYPE
Original GB or Super GB.

#define MGB_TYPE
Pocket GB or Super GB 2.

#define CGB_TYPE
Color GB.

#define IO_IDLE
IO is completed.

#define IO_SENDING
Sending data.

#define IO_RECEIVING
Receiving data.

#define IO_ERROR
Error.

#define SWITCH_ROM_MBC1 (b)
Switches the upper 16k bank of the 32k rom to bank rombank using the MBC1 controller. More...

#define SWITCH_RAM_MBC1 (b)
#define SWITCH_ROM_MBC5 (b)
MBC5.

#define SWITCH_RAM_MBC5 (b)
#define DISPLAY_ON
Turns the display back on. More...

#define DISPLAY_OFF
Turns the display off immediatly. More...

#define SHOW_BKG
Turns on the background layer. More...

#define HIDE_BKG
Turns off the background layer. More...

#define SHOW_WIN
Turns on the window layer Sets bit 5 of the LCDC register to 1.

#define HIDE_WIN
Turns off the window layer. More...

#define SHOW_SPRITES
Turns on the sprites layer. More...

#define HIDE_SPRITES
Turns off the sprites layer. More...

#define SPRITES_8x16
Sets sprite size to 8x16 pixels, two tiles one above the other. More...

#define SPRITES_8x8
Sets sprite size to 8x8 pixels, one tile. More...


Typedefs

typedef void (* int_handler )(void)
Interrupt handlers.


Functions

void remove_VBL (int_handler h)
The remove functions will remove any interrupt handler. More...

void remove_LCD (int_handler h)
void remove_TIM (int_handler h)
void remove_SIO (int_handler h)
void remove_JOY (int_handler h)
void add_VBL (int_handler h)
Adds a V-blank interrupt handler. More...

void add_LCD (int_handler h)
Adds a LCD interrupt handler. More...

void add_TIM (int_handler h)
Adds a timer interrupt handler. More...

void mode (UINT8 m)
Set the current mode - one of M_* defined above.

UINT8 get_mode (void)
Returns the current mode.

void send_byte (void)
Send byte in _io_out to the serial port.

void receive_byte (void)
Receive byte from the serial port in _io_in.

void delay (UINT16 d)
Delays the given number of milliseconds. More...

UINT8 joypad (void)
Reads and returns the current state of the joypad. More...

UINT8 waitpad (UINT8 mask)
Waits until all the keys given in mask are pressed. More...

void waitpadup (void)
Waits for the pad and all buttons to be released.

void enable_interrupts (void)
Enables unmasked interrupts. More...

void disable_interrupts (void)
Disables interrupts. More...

void set_interrupts (UINT8 flags)
Clears any pending interrupts and sets the interrupt mask register IO to flags. More...

void reset (void)
Performs a warm reset by reloading the CPU value then jumping to the start of crt0 (0x0150).

void wait_vbl_done (void)
Waits for the vertical blank interrupt (VBL) to finish. More...

void display_off (void)
Turns the display off. More...

void hiramcpy (UINT8 dst, const void *src, UINT8 n)
Copies data from somewhere in the lower address space to part of hi-ram. More...

void set_bkg_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char *data)
Sets the tile patterns in the Background Tile Pattern table. More...

void set_bkg_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char *tiles)
Sets the tiles in the background tile table. More...

void get_bkg_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char *tiles)
void move_bkg (UINT8 x, UINT8 y)
Moves the background layer to the position specified in x and y in pixels. More...

void scroll_bkg (INT8 x, INT8 y)
Moves the background relative to it's current position. More...

void set_win_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char *data)
Sets the window tile data. More...

void set_win_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char *tiles)
Sets the tiles in the win tile table. More...

void get_win_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char *tiles)
void move_win (UINT8 x, UINT8 y)
Moves the window layer to the position specified in x and y in pixels. More...

void scroll_win (INT8 x, INT8 y)
Move the window relative to its current position. More...

void set_sprite_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char *data)
Sets the tile patterns in the Sprite Tile Pattern table. More...

void get_sprite_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char *data)
void set_sprite_tile (UINT8 nb, UINT8 tile)
Sets sprite n to display tile number t, from the sprite tile data. More...

UINT8 get_sprite_tile (UINT8 nb)
void set_sprite_prop (UINT8 nb, UINT8 prop)
Sets the property of sprite n to those defined in p. More...

UINT8 get_sprite_prop (UINT8 nb)
void move_sprite (UINT8 nb, UINT8 x, UINT8 y)
Moves the given sprite to the given position on the screen. More...

void scroll_sprite (INT8 nb, INT8 x, INT8 y)
Moves the given sprite relative to its current position.

void set_data (unsigned char *vram_addr, unsigned char *data, UINT16 len)
void get_data (unsigned char *data, unsigned char *vram_addr, UINT16 len)
void set_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char *vram_addr, unsigned char *tiles)
void get_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char *tiles, unsigned char *vram_addr)

Variables

UINT8 _cpu
GB type (GB, PGB, CGB).

UINT16 sys_time
Time in VBL periods (60Hz).

UINT8 _io_status
An OR of IO_*.

UINT8 _io_in
Byte just read.

UINT8 _io_out
Write the byte to send here before calling send_byte(). More...


Detailed Description

Gameboy specific functions.

Define Documentation

#define J_START ()

Joypad bits.

A logical OR of these is used in the wait_pad and joypad functions. For example, to see if the B button is pressed try

UINT8 keys; keys = joypad(); if (keys & J_B) { ... }

See also:
joypad()

#define M_DRAWING ()

Screen modes.

Normally used by internal functions only.

#define S_PALETTE ()

If this is set, sprite colours come from OBJ1PAL.

Else they come from OBJ0PAL.

#define VBL_IFLAG ()

Vertical blank interrupt.

Occurs at the start of the vertical blank. During this period the video ram may be freely accessed.

#define LCD_IFLAG ()

Interrupt when triggered by the STAT register.

See the Pan doc.

#define SWITCH_ROM_MBC1 (b)

Switches the upper 16k bank of the 32k rom to bank rombank using the MBC1 controller.

By default the upper 16k bank is 1. Make sure the rom you compile has more than just bank 0 and bank 1, a 32k rom. This is done by feeding lcc.exe the following switches:

-Wl-yt# where # is the type of cartridge. 1 for ROM+MBC1.

-Wl-yo# where # is the number of rom banks. 2,4,8,16,32.

#define DISPLAY_ON ()

Turns the display back on.

See also:
display_off(), DISPLAY_OFF()

#define DISPLAY_OFF ()

Turns the display off immediatly.

See also:
display_off(), DISPLAY_ON()

#define SHOW_BKG ()

Turns on the background layer.

Sets bit 0 of the LCDC register to 1.

#define HIDE_BKG ()

Turns off the background layer.

Sets bit 0 of the LCDC register to 0.

#define HIDE_WIN ()

Turns off the window layer.

Clears bit 5 of the LCDC register to 0.

#define SHOW_SPRITES ()

Turns on the sprites layer.

Sets bit 1 of the LCDC register to 1.

#define HIDE_SPRITES ()

Turns off the sprites layer.

Clears bit 1 of the LCDC register to 0.

#define SPRITES_8x16 ()

Sets sprite size to 8x16 pixels, two tiles one above the other.

Sets bit 2 of the LCDC register to 1.

#define SPRITES_8x8 ()

Sets sprite size to 8x8 pixels, one tile.

Clears bit 2 of the LCDC register to 0.


Function Documentation

void remove_VBL (int_handler h)

The remove functions will remove any interrupt handler.

A handler of NULL will cause bad things to happen.

void add_VBL (int_handler h)

Adds a V-blank interrupt handler.

The handler 'h' will be called whenever a V-blank interrupt occurs. Up to 4 handlers may be added, with the last added being called last. If the remove_VBL function is to be called, only three may be added.

See also:
remove_VBL()

void add_LCD (int_handler h)

Adds a LCD interrupt handler.

Called when the LCD interrupt occurs, which is normally when LY_REG == LYC_REG.

From pan/k0Pa: There are various reasons for this interrupt to occur as described by the STAT register ($FF40). One very popular reason is to indicate to the user when the video hardware is about to redraw a given LCD line. This can be useful for dynamically controlling the SCX/ SCY registers ($FF43/$FF42) to perform special video effects.

See also:
add_VBL()

void add_TIM (int_handler h)

Adds a timer interrupt handler.

From pan/k0Pa: This interrupt occurs when the TIMA register ($FF05) changes from $FF to $00.

See also:
add_VBL()

void delay (UINT16 d)

Delays the given number of milliseconds.

Uses no timers or interrupts, and can be called with interrupts disabled (why nobody knows :)

UINT8 joypad (void)

Reads and returns the current state of the joypad.

Follows Nintendo's guidelines for reading the pad. Return value is an OR of J_*

See also:
J_START()

UINT8 waitpad (UINT8 mask)

Waits until all the keys given in mask are pressed.

Normally only used for checking one key, but it will support many, even J_LEFT at the same time as J_RIGHT :)

See also:
joypad(), J_START()

void enable_interrupts (void)

Enables unmasked interrupts.

See also:
disable_interrupts()

void disable_interrupts (void)

Disables interrupts.

This function may be called as many times as you like; however the first call to enable_interrupts will re-enable them.

See also:
enable_interrupts()

void set_interrupts (UINT8 flags)

Clears any pending interrupts and sets the interrupt mask register IO to flags.

See also:
VBL_IFLAG()
Parameters:
flags   A logical OR of *_IFLAGS

</td><td>A logical OR of *_IFLAGS</td></tr></table></dl></div><aname="a73"doxytag="gb.h::wait_vbl_done"><p><tablewidth=100%%cellpadding=2cellspacing=0border=0><tr><tdclass="md"><b>void wait_vbl_done</td> (void)

Waits for the vertical blank interrupt (VBL) to finish.

This can be used to sync animation with the screen re-draw. If VBL interrupt is disabled, this function will never return. If the screen is off this function returns immediatly.

PP Switches the upper k bank of the k rom to bank rombank however the first call to enable_interrupts will re enable them PP fBSee this function will never return If the screen is off this function returns immediatly SS void display_off (void)

Turns the display off.

Waits until the VBL interrupt before turning the display off.

See also:
DISPLAY_ON()

void hiramcpy (UINT8 dst, const void * src, UINT8 n)

Copies data from somewhere in the lower address space to part of hi-ram.

Parameters:
dst   Offset in high ram (0xFF00 and above) to copy to.
src   Area to copy from
n   Number of bytes to copy.

void set_bkg_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char * data)

Sets the tile patterns in the Background Tile Pattern table.

Starting with the tile pattern x and carrying on for n number of tile patterns.Taking the values starting from the pointer data. Note that patterns 128-255 overlap with patterns 128-255 of the sprite Tile Pattern table.

GBC: Depending on the VBK_REG this determines which bank of Background tile patterns are written to. VBK_REG=0 indicates the first bank, and VBK_REG=1 indicates the second.

Parameters:
first_tile   Range 0 - 255
nb_tiles   Range 0 - 255

void set_bkg_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char * tiles)

Sets the tiles in the background tile table.

Starting at position x,y in tiles and writing across for w tiles and down for h tiles. Taking the values starting from the pointer data.

For the GBC, also see the pan/k00Pa section on VBK_REG.

Parameters:
x   Range 0 - 31
y   Range 0 - 31
w   Range 0 - 31
h   Range 0 - 31
data   Pointer to an unsigned char. Usually the first element in an array.

void move_bkg (UINT8 x, UINT8 y)

Moves the background layer to the position specified in x and y in pixels.

Where 0,0 is the top left corner of the GB screen. You'll notice the screen wraps around in all 4 directions, and is always under the window layer.

void scroll_bkg (INT8 x, INT8 y)

Moves the background relative to it's current position.

See also:
move_bkg()

void set_win_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char * data)

Sets the window tile data.

This is the same as set_bkg_data, as both the window layer and background layer share the same Tile Patterns.

See also:
set_bkg_data()

void set_win_tiles (UINT8 x, UINT8 y, UINT8 w, UINT8 h, unsigned char * tiles)

Sets the tiles in the win tile table.

Starting at position x,y in tiles and writing across for w tiles and down for h tiles. Taking the values starting from the pointer data. Note that patterns 128-255 overlap with patterns 128-255 of the sprite Tile Pattern table.

GBC only. Depending on the VBK_REG this determines if you're setting the tile numbers VBK_REG=0; or the attributes for those tiles VBK_REG=1;. The bits in the attributes are defined as: Bit 7 - Priority flag. When this is set, it puts the tile above the sprites with colour 0 being transparent. 0: below sprites, 1: above sprites Note SHOW_BKG needs to be set for these priorities to take place. Bit 6 - Vertical flip. Dictates which way up the tile is drawn vertically. 0: normal, 1: upside down. Bit 5 - Horizontal flip. Dictates which way up the tile is drawn horizontally. 0: normal, 1:back to front. Bit 4 - Not used. Bit 3 - Character Bank specification. Dictates from which bank of Background Tile Patterns the tile is taken. 0: Bank 0, 1: Bank 1 Bit 2 - See bit 0. Bit 1 - See bit 0. Bit 0 - Bits 0-2 indicate which of the 7 BKG colour palettes the tile is assigned.

Parameters:
x   Range 0 - 31
y   Range 0 - 31
w   Range 0 - 31
h   Range 0 - 31

void move_win (UINT8 x, UINT8 y)

Moves the window layer to the position specified in x and y in pixels.

Where 7,0 is the top left corner of the GB screen. The window is locked to the bottom right corner, and is always over the background layer.

See also:
SHOW_WIN(), HIDE_WIN()

void scroll_win (INT8 x, INT8 y)

Move the window relative to its current position.

See also:
move_win()

void set_sprite_data (UINT8 first_tile, UINT8 nb_tiles, unsigned char * data)

Sets the tile patterns in the Sprite Tile Pattern table.

Starting with the tile pattern x and carrying on for n number of tile patterns.Taking the values starting from the pointer data. Note that patterns 128-255 overlap with patterns 128-255 of the Background Tile Pattern table.

GBC only. Depending on the VBK_REG this determines which bank of Background tile patterns are written to. VBK_REG=0 indicates the first bank, and VBK_REG=1 indicates the second.

void set_sprite_tile (UINT8 nb, UINT8 tile)

Sets sprite n to display tile number t, from the sprite tile data.

If the GB is in 8x16 sprite mode then it will display the next tile, t+1, below the first tile.

Parameters:
nb   Sprite number, range 0 - 39

void set_sprite_prop (UINT8 nb, UINT8 prop)

Sets the property of sprite n to those defined in p.

Where the bits in p represent: Bit 7 - Priority flag. When this is set the sprites appear behind the background and window layer. 0: infront, 1: behind. Bit 6 - GBC only. Vertical flip. Dictates which way up the sprite is drawn vertically. 0: normal, 1:upside down. Bit 5 - GBC only. Horizontal flip. Dictates which way up the sprite is drawn horizontally. 0: normal, 1:back to front. Bit 4 - DMG only. Assigns either one of the two b/w palettes to the sprite. 0: OBJ palette 0, 1: OBJ palette 1. Bit 3 - GBC only. Dictates from which bank of Sprite Tile Patterns the tile is taken. 0: Bank 0, 1: Bank 1 Bit 2 - See bit 0. Bit 1 - See bit 0. Bit 0 - GBC only. Bits 0-2 indicate which of the 7 OBJ colour palettes the sprite is assigned.

Parameters:
nb   Sprite number, range 0 - 39

void move_sprite (UINT8 nb, UINT8 x, UINT8 y)

Moves the given sprite to the given position on the screen.

Dont forget that the top left visible pixel on the screen is at (8,16). To put sprite 0 at the top left, use move_sprite(0, 8, 16);


Variable Documentation

UINT8 _io_out

Write the byte to send here before calling send_byte().

See also:
send_byte()

Generated at Sun May 7 12:16:59 2000 for gbdk-lib by doxygen 1.0.0 written by Dimitri van Heesch, © 1997-1999