GBDK 2020 Docs  4.1.1
API Documentation for GBDK 2020
nes.h
Go to the documentation of this file.
1 
4 #ifndef _NES_H
5 #define _NES_H
6 
7 #include <types.h>
8 #include <stdint.h>
9 #include <gbdk/version.h>
10 #include <nes/hardware.h>
11 #include <nes/rgb_to_nes_macro.h>
12 
13 #define NINTENDO_ENTERTAINMENT_SYSTEM
14 #ifdef SEGA
15 #undef SEGA
16 #endif
17 
18 #define RGB(r,g,b) RGB_TO_NES(((r) | ((g) << 2) | ((b) << 4)))
19 #define RGB8(r,g,b) RGB_TO_NES((((r) >> 6) | (((g) >> 6) << 2) | (((b) >> 6) << 4)))
20 #define RGBHTML(RGB24bit) RGB_TO_NES((((RGB24bit) >> 22) | ((((RGB24bit) & 0xFFFF) >> 14) << 2) | ((((RGB24bit) & 0xFF) >> 6) << 4)))
21 
27 #define RGB_RED 0x16 // EGA12
28 #define RGB_DARKRED 0x06 // EGA4
29 #define RGB_GREEN 0x2A // EGA10
30 #define RGB_DARKGREEN 0x1A // EGA2
31 #define RGB_BLUE 0x12 // EGA9
32 #define RGB_DARKBLUE 0x02 // EGA1
33 #define RGB_YELLOW 0x28 // EGA14
34 #define RGB_DARKYELLOW 0x18 // EGA6
35 #define RGB_CYAN 0x2C // EGA11
36 #define RGB_AQUA 0x1C // EGA3
37 #define RGB_PINK 0x24 // EGA13
38 #define RGB_PURPLE 0x14 // EGA5
39 #define RGB_BLACK 0x0F // EGA0
40 #define RGB_DARKGRAY 0x00 // EGA8
41 #define RGB_LIGHTGRAY 0x10 // EGA7
42 #define RGB_WHITE 0x30 // EGA15
43 
45 
46 void set_bkg_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL;
47 
48 void set_sprite_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL;
49 
51 
53 
67 #define J_UP 0x10U
68 #define J_DOWN 0x20U
69 #define J_LEFT 0x40U
70 #define J_RIGHT 0x80U
71 #define J_A 0x01U
72 #define J_B 0x02U
73 #define J_SELECT 0x04U
74 #define J_START 0x08U
75 
80 #define M_DRAWING 0x01U
81 #define M_TEXT_OUT 0x02U
82 #define M_TEXT_INOUT 0x03U
88 #define M_NO_SCROLL 0x04U
92 #define M_NO_INTERP 0x08U
93 
98 #define S_PALETTE 0x10U
102 #define S_FLIPX 0x40U
106 #define S_FLIPY 0x80U
111 #define S_PRIORITY 0x20U
115 #define S_PAL(n) n
116 
117 /* DMG Palettes */
118 #define DMG_BLACK 0x03
119 #define DMG_DARK_GRAY 0x02
120 #define DMG_LITE_GRAY 0x01
121 #define DMG_WHITE 0x00
141 #define DMG_PALETTE(C0, C1, C2, C3) ((uint8_t)((((C3) & 0x03) << 6) | (((C2) & 0x03) << 4) | (((C1) & 0x03) << 2) | ((C0) & 0x03)))
142 
143 /* Limits */
146 #define SCREENWIDTH DEVICE_SCREEN_PX_WIDTH
149 #define SCREENHEIGHT DEVICE_SCREEN_PX_HEIGHT
150 
158 
164 
171 extern volatile uint16_t sys_time;
172 
183 extern volatile uint8_t _current_bank;
184 #define CURRENT_BANK _current_bank
185 
195 #ifndef BANK
196 #define BANK(VARNAME) 0
197 #endif
198 
211 #define BANKREF(VARNAME)
212 
222 #define BANKREF_EXTERN(VARNAME) extern const void __bank_ ## VARNAME;
223 
227 #define SWITCH_ROM_DUMMY(b)
228 
234 #define SWITCH_ROM SWITCH_ROM_DUMMY
235 
241 
252 
262 
266 void waitpadup(void);
267 
273 typedef struct {
274  uint8_t npads;
275  union {
276  struct {
277  uint8_t joy0, joy1, joy2, joy3;
278  };
279  uint8_t joypads[4];
280  };
281 } joypads_t;
282 
292 
297 void joypad_ex(joypads_t * joypads) OLDCALL;
298 
299 
300 
309 inline void enable_interrupts(void) {
310  __asm__("cli");
311 }
312 
325 inline void disable_interrupts(void) {
326  __asm__("sei");
327 }
328 
338 void vsync(void);
339 
342 void wait_vbl_done(void);
343 
349 void display_off(void);
350 
353 void refresh_OAM(void);
354 
358 #define DISPLAY_ON \
359  display_on();
360 
364 #define DISPLAY_OFF \
365  display_off();
366 
370 #define HIDE_LEFT_COLUMN \
371  shadow_PPUMASK &= ~(PPUMASK_SHOW_BG_LC | PPUMASK_SHOW_SPR_LC); \
372 
376 #define SHOW_LEFT_COLUMN \
377  shadow_PPUMASK |= (PPUMASK_SHOW_BG_LC | PPUMASK_SHOW_SPR_LC);
378 
382 #define SHOW_BKG \
383  shadow_PPUMASK |= PPUMASK_SHOW_BG;
384 
388 #define HIDE_BKG \
389  shadow_PPUMASK &= ~PPUMASK_SHOW_BG;
390 
394 #define SHOW_SPRITES \
395  shadow_PPUMASK |= PPUMASK_SHOW_SPR;
396 
400 #define HIDE_SPRITES \
401  shadow_PPUMASK &= ~PPUMASK_SHOW_SPR;
402 
406 #define SPRITES_8x16 \
407  shadow_PPUCTRL |= PPUCTRL_SPR_8X16;
408 
412 #define SPRITES_8x8 \
413  shadow_PPUCTRL &= ~PPUCTRL_SPR_8X16;
414 
415 
416 
424 
429 
430 #define COMPAT_PALETTE(C0,C1,C2,C3) ((uint8_t)(((C3) << 6) | ((C2) << 4) | ((C1) << 2) | (C0)))
431 
434 inline void set_2bpp_palette(uint16_t palette) {
435  palette;
436 }
437 
440 inline void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor) {
441  set_1bpp_colors_ex(fgcolor, bgcolor, 0);
442 }
443 
453 void set_bkg_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL;
454 #define set_bkg_2bpp_data set_bkg_data
455 
467 void set_bkg_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL;
468 
488 #define set_tile_map set_bkg_tiles
489 
514 
538 inline void set_bkg_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *attributes)
539 {
540  set_bkg_attributes_nes16x16(x >> 1, y >> 1, (w + 1) >> 1, (h + 1) >> 1, attributes);
541 }
588 
601 inline void set_bkg_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *attributes, uint8_t map_w)
602 {
603  set_bkg_submap_attributes_nes16x16(x >> 1, y >> 1, (w + 1) >> 1, (h + 1) >> 1, attributes, map_w >> 1);
604 }
605 
606 
608 
627 inline 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) {
628  _map_tile_offset = base_tile;
629  set_bkg_tiles(x, y, w, h, tiles);
630  _map_tile_offset = 0;
631 }
632 
633 
662 void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) OLDCALL;
663 #define set_tile_submap set_bkg_submap
664 
665 
667 
687 inline 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) {
688  _submap_tile_offset = base_tile;
689  set_bkg_submap(x, y, w, h, map, map_w);
691 }
692 
693 
706 
707 
716 #define set_tile_xy set_bkg_tile_xy
717 
725 
726 
740 inline void move_bkg(uint8_t x, uint8_t y) {
741  bkg_scroll_x = x, bkg_scroll_y = y;
742 }
743 
744 
754 inline void scroll_bkg(int8_t x, int8_t y) {
755  bkg_scroll_x += x, bkg_scroll_y += y;
756 }
757 
758 
770 void set_sprite_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL;
771 #define set_sprite_2bpp_data set_sprite_data
772 
784 void set_sprite_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL;
785 
792 typedef struct OAM_item_t {
793  uint8_t y; //< Y coordinate of the sprite on screen - 1
794  uint8_t tile; //< Sprite tile number
795  uint8_t prop; //< OAM Property Flags
796  uint8_t x; //< X coordinate of the sprite on screen
798 
799 
802 extern volatile struct OAM_item_t shadow_OAM[];
803 
807 
808 #define DISABLE_OAM_DMA \
809  _shadow_OAM_base = 0
810 
813 #define DISABLE_VBL_TRANSFER DISABLE_OAM_DMA
814 
815 #define ENABLE_OAM_DMA \
816  _shadow_OAM_base = (uint8_t)((uint16_t)&shadow_OAM >> 8)
817 
820 #define ENABLE_VBL_TRANSFER ENABLE_OAM_DMA
821 
824 #define MAX_HARDWARE_SPRITES 64
825 
828 #define HARDWARE_SPRITE_CAN_FLIP_X 1
829 
832 #define HARDWARE_SPRITE_CAN_FLIP_Y 1
833 
836 inline void SET_SHADOW_OAM_ADDRESS(void * address) {
837  _shadow_OAM_base = (uint8_t)((uint16_t)address >> 8);
838 }
839 
854  shadow_OAM[nb].tile=tile;
855 }
856 
857 
865  return shadow_OAM[nb].tile;
866 }
867 
868 
900  shadow_OAM[nb].prop=prop;
901 }
902 
903 
910  return shadow_OAM[nb].prop;
911 }
912 
913 
923 inline void move_sprite(uint8_t nb, uint8_t x, uint8_t y) {
924  OAM_item_t * itm = &shadow_OAM[nb];
925  itm->y=y, itm->x=x;
926 }
927 
928 
939 inline void scroll_sprite(uint8_t nb, int8_t x, int8_t y) {
940  OAM_item_t * itm = &shadow_OAM[nb];
941  itm->y+=y, itm->x+=x;
942 }
943 
944 
949 inline void hide_sprite(uint8_t nb) {
950  shadow_OAM[nb].y = 240;
951 }
952 
953 
954 
962 void set_data(uint8_t *vram_addr, const uint8_t *data, uint16_t len) OLDCALL;
963 
964 
983 void set_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *vram_addr, const uint8_t *tiles) OLDCALL;
984 
990 void set_tile_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data, uint8_t base) OLDCALL;
991 
992 
1006 inline void set_native_tile_data(uint16_t first_tile, uint8_t nb_tiles, const uint8_t *data) {
1007  if (first_tile < 256) {
1008  set_bkg_data(first_tile, nb_tiles, data);
1009  } else {
1010  set_sprite_data(first_tile - 256, nb_tiles, data);
1011  }
1012 }
1013 
1020 
1028 void vmemset (void *s, uint8_t c, size_t n) OLDCALL;
1029 
1039 #define fill_rect fill_bkg_rect
1040 
1041 #endif /* _NES_H */
char c
Definition: string.h:20
#define OLDCALL
Definition: types.h:21
uint16_t palette_color_t
Definition: cgb.h:85
void d
Definition: gb.h:665
uint8_t h
Definition: gb.h:644
__SHADOW_REG bkg_scroll_x
Definition: hardware.h:56
__SHADOW_REG bkg_scroll_y
Definition: hardware.h:57
void set_vram_byte(uint8_t *addr, uint8_t v) OLDCALL
void set_bkg_submap_attributes_nes16x16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) OLDCALL
void set_sprite_tile(uint8_t nb, uint8_t tile)
Definition: nes.h:853
volatile uint8_t _current_bank
Definition: gb.h:474
void set_bkg_attributes_nes16x16(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *attributes) OLDCALL
void waitpadup(void)
void set_1bpp_colors_ex(uint8_t fgcolor, uint8_t bgcolor, uint8_t mode) OLDCALL
void scroll_bkg(int8_t x, int8_t y)
Definition: nes.h:754
void wait_vbl_done(void)
void enable_interrupts(void)
Definition: nes.h:309
uint16_t _current_1bpp_colors
uint8_t joypad(void) OLDCALL
void delay(uint16_t d) OLDCALL
void disable_interrupts(void)
Definition: nes.h:325
void vmemset(void *s, uint8_t c, size_t n) OLDCALL
void set_bkg_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *attributes, uint8_t map_w)
Definition: nes.h:601
void set_bkg_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) OLDCALL
volatile struct OAM_item_t shadow_OAM[]
void set_data(uint8_t *vram_addr, const uint8_t *data, uint16_t len) OLDCALL
void mode(uint8_t m) OLDCALL
void vsync(void)
void set_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *vram_addr, const uint8_t *tiles) OLDCALL
void move_bkg(uint8_t x, uint8_t y)
Definition: nes.h:740
uint8_t _shadow_OAM_base
Definition: gb.h:1657
void set_native_tile_data(uint16_t first_tile, uint8_t nb_tiles, const uint8_t *data)
Definition: nes.h:1006
uint8_t get_sprite_prop(uint8_t nb)
Definition: nes.h:909
void scroll_sprite(uint8_t nb, int8_t x, int8_t y)
Definition: nes.h:939
uint8_t get_mode(void) OLDCALL
volatile uint16_t sys_time
void SET_SHADOW_OAM_ADDRESS(void *address)
Definition: nes.h:836
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)
Definition: nes.h:627
void set_bkg_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *attributes)
Definition: nes.h:538
void fill_bkg_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t tile) OLDCALL
uint8_t * set_bkg_tile_xy(uint8_t x, uint8_t y, uint8_t t) OLDCALL
struct OAM_item_t OAM_item_t
void set_sprite_prop(uint8_t nb, uint8_t prop)
Definition: nes.h:899
void set_sprite_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL
void set_sprite_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL
uint8_t palette_color_t
Definition: nes.h:44
void set_2bpp_palette(uint16_t palette)
Definition: nes.h:434
uint8_t _map_tile_offset
void set_bkg_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) OLDCALL
void joypad_ex(joypads_t *joypads) OLDCALL
void set_tile_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data, uint8_t base) OLDCALL
void set_sprite_palette_entry(uint8_t palette, uint8_t entry, palette_color_t rgb_data) OLDCALL
void move_sprite(uint8_t nb, uint8_t x, uint8_t y)
Definition: nes.h:923
uint8_t _submap_tile_offset
uint8_t joypad_init(uint8_t npads, joypads_t *joypads) OLDCALL
uint8_t * get_bkg_xy_addr(uint8_t x, uint8_t y) OLDCALL
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)
Definition: nes.h:687
void set_sprite_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL
void refresh_OAM(void)
uint8_t get_bkg_tile_xy(uint8_t x, uint8_t y) OLDCALL
void set_bkg_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL
void hide_sprite(uint8_t nb)
Definition: nes.h:949
void init_bkg(uint8_t c) OLDCALL
void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) OLDCALL
Definition: msx.h:554
void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor)
Definition: nes.h:440
void get_bkg_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *tiles) OLDCALL
void set_bkg_palette_entry(uint8_t palette, uint8_t entry, palette_color_t rgb_data) OLDCALL
uint8_t waitpad(uint8_t mask) OLDCALL
uint8_t get_sprite_tile(uint8_t nb)
Definition: nes.h:864
void set_bkg_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL
void display_off(void)
Definition: msx.h:225
unsigned char uint8_t
Definition: stdint.h:51
unsigned short int uint16_t
Definition: stdint.h:52
signed char int8_t
Definition: stdint.h:43
Definition: gb.h:1644
uint8_t x
Definition: gb.h:1645
uint8_t prop
Definition: gb.h:1647
uint8_t tile
Definition: gb.h:1646
uint8_t y
Definition: gb.h:1645
Definition: gb.h:672