From 6a44a34e0ebb867753df26f1cb0a38f53420a606 Mon Sep 17 00:00:00 2001 From: Simponic Date: Fri, 7 Aug 2020 22:40:00 -0600 Subject: Added files --- include/input.h | 135 ++++++++++++++++++++++++++++++++++ include/memdef.h | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/memmap.h | 97 +++++++++++++++++++++++++ include/toolbox.h | 154 +++++++++++++++++++++++++++++++++++++++ include/types.h | 120 +++++++++++++++++++++++++++++++ 5 files changed, 718 insertions(+) create mode 100644 include/input.h create mode 100644 include/memdef.h create mode 100644 include/memmap.h create mode 100644 include/toolbox.h create mode 100644 include/types.h (limited to 'include') diff --git a/include/input.h b/include/input.h new file mode 100644 index 0000000..70b7a83 --- /dev/null +++ b/include/input.h @@ -0,0 +1,135 @@ +// +// Input header +// +//! \file tonc_input.h +//! \author J Vijn +//! \date 20060508 - 20060924 +// +// === NOTES === +// * This is a _small_ set of typedefs, #defines and inlines that can +// be found in tonclib, and might not represent the +// final forms. + + +#ifndef __INPUT__ +#define __INPUT__ + +#include "memmap.h" +#include "types.h" +#include "memdef.h" + + +INLINE int bit_tribool(u32 x, int plus, int minus); + +// === CONSTANTS ====================================================== + +typedef enum eKeyIndex +{ + KI_A=0, KI_B, KI_SELECT, KI_START, + KI_RIGHT, KI_LEFT, KI_UP, KI_DOWN, + KI_R, KI_L, KI_MAX +} eKeyIndex; + +// === MACROS ========================================================= + +// check which of the specified keys are down or up right now +#define KEY_DOWN_NOW(key) (~(REG_KEYINPUT) & key) +#define KEY_UP_NOW(key) ( (REG_KEYINPUT) & key) + +// test whether all keys are pressed, released, whatever. +// Example use: +// KEY_EQ(key_hit, KEY_L | KEY_R) +// will be true if and only if KEY_L and KEY_R are _both_ being pressed +#define KEY_EQ(key_fun, keys) ( key_fun(keys) == (keys) ) + +// === CLASSES ======================================================== +// === GLOBALS ======================================================== + +extern u16 __key_curr, __key_prev; // in tonc_core.c + +// === PROTOTYPES ===================================================== + +// --- synchronous key states --- +INLINE void key_poll(); +INLINE u32 key_curr_state(); +INLINE u32 key_prev_state(); + +INLINE u32 key_is_down(u32 key); // any of key currently down? +INLINE u32 key_is_up(u32 key); // any of key currently up? + +INLINE u32 key_was_down(u32 key); // any of key previously down? +INLINE u32 key_was_up(u32 key); // any of key previously up? + +INLINE u32 key_transit(u32 key); // any of key changing? +INLINE u32 key_held(u32 key); // any of key held down? +INLINE u32 key_hit(u32 key); // any of key being hit (going down)? +INLINE u32 key_released(u32 key); // any of key being released? + +// --- tribools --- +INLINE int key_tri_horz(); +INLINE int key_tri_vert(); +INLINE int key_tri_shoulder(); +INLINE int key_tri_fire(); + +// === INLINES========================================================= + +// --- keys ----------------------------------------------------------- + +//! Poll for keystates +INLINE void key_poll() +{ __key_prev= __key_curr; __key_curr= ~REG_KEYINPUT & KEY_MASK; } + +//! Get current keystate +INLINE u32 key_curr_state() { return __key_curr; } + +//! Get previous key state +INLINE u32 key_prev_state() { return __key_prev; } + +//! Gives the keys of \a key that are currently down +INLINE u32 key_is_down(u32 key) { return __key_curr & key; } + +//! Gives the keys of \a key that are currently up +INLINE u32 key_is_up(u32 key) { return ~__key_curr & key; } + +//! Gives the keys of \a key that were previously down +INLINE u32 key_was_down(u32 key) { return __key_prev & key; } + +//! Gives the keys of \a key that were previously down +INLINE u32 key_was_up(u32 key) { return ~__key_prev & key; } + +//! Gives the keys of \a key that are different from before +INLINE u32 key_transit(u32 key) +{ return (__key_curr^__key_prev) & key; } + +//! Gives the keys of \a key that are being held down +INLINE u32 key_held(u32 key) +{ return (__key_curr& __key_prev) & key; } + +//! Gives the keys of \a key that are pressed (down now but not before) +INLINE u32 key_hit(u32 key) +{ return (__key_curr&~__key_prev) & key; } + +//! Gives the keys of \a key that are being released +INLINE u32 key_released(u32 key) +{ return (~__key_curr&__key_prev) & key; } + +// --- tribools --- + +//! Horizontal tribool (right,left)=(+,-) +INLINE int key_tri_horz() +{ return bit_tribool(__key_curr, KI_RIGHT, KI_LEFT); } + +//! Vertical tribool (down,up)=(+,-) +INLINE int key_tri_vert() +{ return bit_tribool(__key_curr, KI_DOWN, KI_UP); } + +//! Shoulder-button tribool (R,L)=(+,-) +INLINE int key_tri_shoulder() +{ return bit_tribool(__key_curr, KI_R, KI_L); } + +//! Fire-button tribool (A,B)=(+,-) +INLINE int key_tri_fire() +{ return bit_tribool(__key_curr, KI_A, KI_B); } + + +#endif // __INPUT__ diff --git a/include/memdef.h b/include/memdef.h new file mode 100644 index 0000000..8fe432f --- /dev/null +++ b/include/memdef.h @@ -0,0 +1,212 @@ +// +// Memory map defines. All of them +// +//! \file tonc_memdef.h +//! \author J Vijn +//! \date 20060508 - 20060924 +// +// === NOTES === +// * This is a _small_ set of typedefs, #defines and inlines that can +// be found in tonclib, and might not represent the +// final forms. +// * '0'-defines are prefixed with '_', to indicate their zero-ness +// and presents a safety from things like doing `if(x&0)' + +#ifndef __MEMDEF__ +#define __MEMDEF__ + +// --- Prefixes --- +// REG_DISPCNT : DCNT +// REG_DISPSTAT : DSTAT + +// OAM attr 0 : OA0 +// OAM attr 1 : OA1 +// OAM attr 2 : OA2 + + +// --- REG_DISPCNT ----------------------------------------------------- + +#define DCNT_MODE0 0 //!< Mode 0; bg 0-4: reg +#define DCNT_MODE1 0x0001 //!< Mode 1; bg 0-1: reg; bg 2: affine +#define DCNT_MODE2 0x0002 //!< Mode 2; bg 2-2: affine +#define DCNT_MODE3 0x0003 //!< Mode 3; bg2: 240x160\@16 bitmap +#define DCNT_MODE4 0x0004 //!< Mode 4; bg2: 240x160\@8 bitmap +#define DCNT_MODE5 0x0005 //!< Mode 5; bg2: 160x128\@16 bitmap +#define DCNT_GB 0x0008 //!< (R) GBC indicator +#define DCNT_PAGE 0x0010 //!< Page indicator +#define DCNT_OAM_HBL 0x0020 //!< Allow OAM updates in HBlank +#define DCNT_OBJ_2D 0 //!< OBJ-VRAM as matrix +#define DCNT_OBJ_1D 0x0040 //!< OBJ-VRAM as array +#define DCNT_BLANK 0x0080 //!< Force screen blank +#define DCNT_BG0 0x0100 //!< Enable bg 0 +#define DCNT_BG1 0x0200 //!< Enable bg 1 +#define DCNT_BG2 0x0400 //!< Enable bg 2 +#define DCNT_BG3 0x0800 //!< Enable bg 3 +#define DCNT_OBJ 0x1000 //!< Enable objects +#define DCNT_WIN0 0x2000 //!< Enable window 0 +#define DCNT_WIN1 0x4000 //!< Enable window 1 +#define DCNT_WINOBJ 0x8000 //!< Enable object window + +#define DCNT_MODE_MASK 0x0007 +#define DCNT_MODE_SHIFT 0 +#define DCNT_MODE(n) ((n)<:) +// _x needs shifting +#define BFN_PREP(x, name) ( ((x)<>name##_SHIFT ) +#define BFN_SET(y, x, name) (y = ((y)&~name##_MASK) | BFN_PREP(x,name) ) + +// x already shifted +#define BFN_PREP2(x, name) ( (x) & name##_MASK ) +#define BFN_GET2(y, name) ( (y) & name##_MASK ) +#define BFN_SET2(y, x, name) (y = ((y)&~name##_MASK) | BFN_PREP2(x,name) ) + +//! Gives a tribool (-1, 0, or +1) depending on the state of some bits. +/*! Looks at the \a plus and \a minus bits of \a flags, and subtracts +* their status to give a +1, -1 or 0 result. Useful for direction flags. +* \param plus Bit number for positive result +* \param minus Bit number for negative result +* \return +1 if \a plus bit is set but \a minus bit isn't
+* -1 if \a minus bit is set and \a plus bit isn't
+* 0 if neither or both are set. +*/ +INLINE int bit_tribool(u32 flags, int plus, int minus) +{ return ((flags>>plus)&1) - ((flags>>minus)&1); } + + +// --- (tonc_video.h) ------------------------------------------------- + +//! Wait for next VBlank +INLINE void vid_vsync() +{ + while(REG_VCOUNT >= 160); // wait till VDraw + while(REG_VCOUNT < 160); // wait till VBlank +} + +//! Create a 15bit BGR color. +INLINE COLOR RGB15(u32 red, u32 green, u32 blue) +{ return red | (green<<5) | (blue<<10); } + + +// --- Objects --- + + +//! Set the attributes of an object. +INLINE OBJ_ATTR *obj_set_attr(OBJ_ATTR *obj, u16 a0, u16 a1, u16 a2) +{ + obj->attr0= a0; obj->attr1= a1; obj->attr2= a2; + return obj; +} + +//! Set the position of \a obj +INLINE void obj_set_pos(OBJ_ATTR *obj, int x, int y) +{ + BFN_SET(obj->attr0, y, ATTR0_Y); + BFN_SET(obj->attr1, x, ATTR1_X); +} + +//! Hide an object. +INLINE void obj_hide(OBJ_ATTR *obj) +{ BFN_SET2(obj->attr0, ATTR0_HIDE, ATTR0_MODE); } + +//! Unhide an object. +/*! \param obj Object to unhide. +* \param mode Object mode to unhide to. Necessary because this affects +* the affine-ness of the object. +*/ +INLINE void obj_unhide(OBJ_ATTR *obj, u16 mode) +{ BFN_SET2(obj->attr0, mode, ATTR0_MODE); } + + +#endif // TOOLBOX_H diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..5ad8121 --- /dev/null +++ b/include/types.h @@ -0,0 +1,120 @@ +// +// Basic structs and typedefs +// +//! \file tonc_types.h +//! \author J Vijn +//! \date 20060508 - 20060508 +// +// === NOTES === +// * This is a _small_ set of typedefs, #defines and inlines that can +// be found in tonclib, and might not represent the +// final forms. + + +#ifndef __TYPES__ +#define __TYPES__ + + +// === GCC ATTRIBUTES ================================================= + +// alignment boundary +#define ALIGN(_n) __attribute__((aligned(_n))) +#define ALIGN4 __attribute__((aligned(4))) + +// pack aggregate members +#define PACKED __attribute__((packed)) + + +// === TYPES: ========================================================= + +// --- primary typedefs ----------------------------------------------- +typedef unsigned char u8, byte; +typedef unsigned short u16, hword; +typedef unsigned int u32, word; +typedef unsigned long long u64; + +typedef signed char s8; +typedef signed short s16; +typedef signed int s32; +typedef signed long long s64; + +// and volatiles for registers 'n stuff +typedef volatile u8 vu8; +typedef volatile u16 vu16; +typedef volatile u32 vu32; +typedef volatile u64 vu64; + +typedef volatile s8 vs8; +typedef volatile s16 vs16; +typedef volatile s32 vs32; +typedef volatile s64 vs64; + +// and consts too for parameters *sigh* +typedef const u8 cu8; +typedef const u16 cu16; +typedef const u32 cu32; +typedef const u64 cu64; + +typedef const s8 cs8; +typedef const s16 cs16; +typedef const s32 cs32; +typedef const s64 cs64; + +typedef struct { u32 data[8]; } BLOCK; + +// --- secondary typedefs --------------------------------------------- + +typedef u16 COLOR; + +// NOTE, u32[]! +typedef struct { u32 data[8]; } TILE, TILE4; +typedef struct { u32 data[16]; } TILE8; + + +// --- memory map structs -------------------------------------------- + +// --- PAL types --- +typedef COLOR PALBANK[16]; + +// --- VRAM types --- + +typedef COLOR M3LINE[240]; +typedef u8 M4LINE[240]; // NOTE u8, not u16!! +typedef COLOR M5LINE[160]; + +typedef TILE CHARBLOCK[512]; +typedef TILE8 CHARBLOCK8[256]; + +// --- OAM structs --- +// NOTE: OATR and OAFF are interlaced; when using affine objs, +// struct/DMA/mem copies will give bad results +typedef struct OBJ_ATTR +{ + u16 attr0; + u16 attr1; + u16 attr2; + s16 fill; +} ALIGN4 OBJ_ATTR; + +typedef struct OBJ_AFFINE +{ + u16 fill0[3]; s16 pa; + u16 fill1[3]; s16 pb; + u16 fill2[3]; s16 pc; + u16 fill3[3]; s16 pd; +} ALIGN4 OBJ_AFFINE; + + +// === DEFINES ======================================================== + +#ifndef NULL +#define NULL 0 +#endif + +// `inline' inlines the function when -O > 0 when called, +// but also creates a body for the function itself +// `static' removes the body as well +#define INLINE static inline + + +#endif // __TYPES__ -- cgit v1.2.3-70-g09d2