GBDK 2020 Docs  4.1.1
API Documentation for GBDK 2020
msx.h
Go to the documentation of this file.
1 
4 #ifndef _MSX_H
5 #define _MSX_H
6 
7 #include <types.h>
8 #include <stdint.h>
9 #include <gbdk/version.h>
10 #include <msx/hardware.h>
11 
12 #define MSX
13 #ifdef NINTENDO
14 #undef NINTENDO
15 #endif
16 #ifdef SEGA
17 #undef SEGA
18 #endif
19 #if defined(__TARGET_msxdos)
20 #define MSXDOS
21 #endif
22 
23 
24 #define VBK_REG VDP_ATTR_SHIFT
25 
39 #define J_UP 0b00100000
40 #define J_DOWN 0b01000000
41 #define J_LEFT 0b00010000
42 #define J_RIGHT 0b10000000
43 #define J_A 0b00000001
44 #define J_B 0b00000100
45 #define J_SELECT 0b00001000
46 #define J_START 0b00000010
47 
52 #define M_TEXT_OUT 0x02U
53 #define M_TEXT_INOUT 0x03U
59 #define M_NO_SCROLL 0x04U
63 #define M_NO_INTERP 0x08U
64 
67 #define S_FLIPX 0x02U
70 #define S_FLIPY 0x04U
73 #define S_PALETTE 0x08U
76 #define S_PRIORITY 0x10U
80 #define S_PAL(n) n
81 
82 // VDP helper macros
83 #define __WRITE_VDP_REG(REG, v) shadow_##REG=(v);__critical{VDP_CMD=(shadow_##REG),VDP_CMD=REG;}
84 #define __READ_VDP_REG(REG) shadow_##REG
85 
88 
96 
102 
103 /* Interrupt flags */
106 #define EMPTY_IFLAG 0x00U
112 #define VBL_IFLAG 0x01U
116 #define LCD_IFLAG 0x02U
119 #define TIM_IFLAG 0x04U
122 #define SIO_IFLAG 0x08U
125 #define JOY_IFLAG 0x10U
126 
128 
129 /* Limits */
132 #define SCREENWIDTH DEVICE_SCREEN_PX_WIDTH
135 #define SCREENHEIGHT DEVICE_SCREEN_PX_HEIGHT
138 #define MINWNDPOSX 0x00U
141 #define MINWNDPOSY 0x00U
144 #define MAXWNDPOSX 0x00U
147 #define MAXWNDPOSY 0x00U
148 
149 
152 typedef void (*int_handler)(void) NONBANKED;
153 
158 
163 
167 
171 
175 
179 
183 
187 
191  return VDP_STATUS;
192 }
193 
194 inline void move_bkg(uint8_t x, uint8_t y) {
197 }
198 
199 inline void scroll_bkg(int8_t x, int8_t y) {
201  int16_t tmp = __READ_VDP_REG(VDP_RSCY) + y;
202  __WRITE_VDP_REG(VDP_RSCY, (tmp < 0) ? 224 + tmp : tmp % 224u);
203 }
204 
215 void vsync(void) PRESERVES_REGS(b, c, d, e, h, l, iyh, iyl);
216 
219 void wait_vbl_done(void) PRESERVES_REGS(b, c, d, e, h, l, iyh, iyl);
220 
225 inline void display_off(void) {
227 }
228 
232 #define DISPLAY_ON \
233  __WRITE_VDP_REG(VDP_R1, __READ_VDP_REG(VDP_R1) |= R1_DISP_ON)
234 
238 #define DISPLAY_OFF \
239  display_off();
240 
243 void refresh_OAM(void);
244 
248 #define HIDE_LEFT_COLUMN \
249  __WRITE_VDP_REG(VDP_R0, __READ_VDP_REG(VDP_R0) |= R0_LCB)
250 
254 #define SHOW_LEFT_COLUMN \
255  __WRITE_VDP_REG(VDP_R0, __READ_VDP_REG(VDP_R0) &= (~R0_LCB))
256 
260 #define SHOW_BKG
261 
265 #define HIDE_BKG
266 
270 #define SHOW_WIN
271 
275 #define HIDE_WIN
276 
280 #define SHOW_SPRITES
281 
285 #define HIDE_SPRITES
286 
289 #define SPRITES_16x16 \
290  __WRITE_VDP_REG(VDP_R1, __READ_VDP_REG(VDP_R1) |= R1_SPR_16X16)
291 
294 #define SPRITES_8x8 \
295  __WRITE_VDP_REG(VDP_R1, __READ_VDP_REG(VDP_R1) &= (~R1_SPR_16X16))
296 
300 #define DEVICE_SUPPORTS_COLOR (TRUE)
301 
308 extern volatile uint16_t sys_time;
309 
312 extern volatile uint8_t _current_bank;
313 #define CURRENT_BANK _current_bank
314 
324 #ifndef BANK
325 #define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME )
326 #endif
327 
340 #define BANKREF(VARNAME) void __func_ ## VARNAME(void) __banked __naked { \
341 __asm \
342  .local b___func_ ## VARNAME \
343  ___bank_ ## VARNAME = b___func_ ## VARNAME \
344  .globl ___bank_ ## VARNAME \
345 __endasm; \
346 }
347 
357 #define BANKREF_EXTERN(VARNAME) extern const void __bank_ ## VARNAME;
358 
359 
365 #define SWITCH_ROM1 SWITCH_ROM
366 
371 #define SWITCH_ROM2(b) MAP_FRAME2=(b)
372 
377 #define SWITCH_RAM(b) RAM_CONTROL=((b)&1)?RAM_CONTROL|RAMCTL_BANK:RAM_CONTROL&(~RAMCTL_BANK)
378 
382 #define ENABLE_RAM RAM_CONTROL|=RAMCTL_RAM
383 
387 #define DISABLE_RAM RAM_CONTROL&=(~RAMCTL_RAM)
388 
389 
395 
396 
400 
404 
410 void waitpadup(void) PRESERVES_REGS(b, c, d, e, iyh, iyl);
411 
417 typedef struct {
418  uint8_t npads;
419  union {
420  struct {
421  uint8_t joy0, joy1, joy2, joy3;
422  };
423  uint8_t joypads[4];
424  };
425 } joypads_t;
426 
436 
444 
445 
446 #if defined(__TARGET_msxdos)
447 
448 #define RGB(r,g,b) ((r) | ((g) << 2) | ((b) << 4))
449 #define RGB8(r,g,b) (((r) >> 6) | (((g) >> 6) << 2) | (((b) >> 6) << 4))
450 #define RGBHTML(RGB24bit) (((RGB24bit) >> 22) | ((((RGB24bit) & 0xFFFF) >> 14) << 2) | ((((RGB24bit) & 0xFF) >> 6) << 4))
451 
454 #define RGB_RED RGB( 3, 0, 0)
455 #define RGB_DARKRED RGB( 2, 0, 0)
456 #define RGB_GREEN RGB( 0, 3, 0)
457 #define RGB_DARKGREEN RGB( 0, 2, 0)
458 #define RGB_BLUE RGB( 0, 0, 3)
459 #define RGB_DARKBLUE RGB( 0, 0, 2)
460 #define RGB_YELLOW RGB( 3, 3, 0)
461 #define RGB_DARKYELLOW RGB( 2, 2, 0)
462 #define RGB_CYAN RGB( 0, 3, 3)
463 #define RGB_AQUA RGB( 3, 1, 2)
464 #define RGB_PINK RGB( 3, 0, 3)
465 #define RGB_PURPLE RGB( 2, 0, 2)
466 #define RGB_BLACK RGB( 0, 0, 0)
467 #define RGB_DARKGRAY RGB( 1, 1, 1)
468 #define RGB_LIGHTGRAY RGB( 2, 2, 2)
469 #define RGB_WHITE RGB( 3, 3, 3)
470 
471 typedef uint8_t palette_color_t;
472 
473 #else
474 #error Unrecognized port
475 #endif
476 
478 inline void cpu_fast(void) {}
479 
481 #define set_bkg_palette_entry set_palette_entry
482 #define set_sprite_palette_entry(palette,entry,rgb_data) set_palette_entry(1,entry,rgb_data)
483 void set_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) Z88DK_CALLEE;
484 #define set_bkg_palette set_palette
485 #define set_sprite_palette(first_palette,nb_palettes,rgb_data) set_palette(1,1,rgb_data)
486 
487 void set_native_tile_data(uint16_t start, uint16_t ntiles, const void *src) Z88DK_CALLEE;
488 inline void set_bkg_4bpp_data(uint16_t start, uint16_t ntiles, const void *src) {
489  set_native_tile_data(start, ntiles, src);
490 }
491 void set_sprite_1bpp_data(uint16_t start, uint16_t ntiles, const void *src) Z88DK_CALLEE;
492 inline void set_native_sprite_data(uint16_t start, uint16_t ntiles, const void *src) {
493  set_sprite_1bpp_data(start, ntiles, src);
494 }
495 
496 #define COMPAT_PALETTE(C0,C1,C2,C3) (((uint16_t)(C3) << 12) | ((uint16_t)(C2) << 8) | ((uint16_t)(C1) << 4) | (uint16_t)(C0))
498 inline void set_2bpp_palette(uint16_t palette) {
499  _current_2bpp_palette = palette;
500 }
501 //void set_tile_2bpp_data(uint16_t start, uint16_t ntiles, const void *src, uint16_t palette) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl);
502 inline void set_bkg_data(uint16_t start, uint16_t ntiles, const void *src) {
503  set_native_tile_data(start, ntiles, src);
504 }
505 inline void set_sprite_data(uint16_t start, uint16_t ntiles, const void *src) {
506  set_sprite_1bpp_data(start, ntiles, src);
507 }
508 //inline void set_bkg_2bpp_data(uint16_t start, uint16_t ntiles, const void *src) {
509 // set_tile_2bpp_data(start, ntiles, src, _current_2bpp_palette);
510 //}
511 //inline void set_sprite_2bpp_data(uint16_t start, uint16_t ntiles, const void *src) {
512 // set_tile_2bpp_data((uint8_t)(start) + 0x100u, ntiles, src, _current_2bpp_palette);
513 //}
514 
516 inline void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor) {
517  _current_1bpp_colors = ((uint16_t)bgcolor << 8) | fgcolor;
518 }
519 void set_tile_1bpp_data(uint16_t start, uint16_t ntiles, const void *src, uint16_t colors) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl);
520 inline void set_bkg_1bpp_data(uint16_t start, uint16_t ntiles, const void *src) {
521  set_tile_1bpp_data(start, ntiles, src, _current_1bpp_colors);
522 }
523 
524 
533 void set_data(uint16_t dst, const void *src, uint16_t size) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl);
534 void vmemcpy(uint16_t dst, const void *src, uint16_t size) Z88DK_CALLEE PRESERVES_REGS(iyh, iyl);
535 
537 #define set_bkg_tiles set_tile_map
538 #define set_win_tiles set_tile_map
539 
541 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) {
542  _map_tile_offset = base_tile;
543  set_tile_map(x, y, w, h, tiles);
544  _map_tile_offset = 0;
545 }
546 inline 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) {
547  _map_tile_offset = base_tile;
548  set_tile_map(x, y, w, h, tiles);
549  _map_tile_offset = 0;
550 }
551 
554 inline void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) {
555  set_tile_submap_compat(x, y, w, h, map_w, map);
556 }
557 inline void set_win_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) {
558  set_tile_submap_compat(x, y, w, h, map_w, map);
559 }
560 
562 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) {
563  _submap_tile_offset = base_tile;
564  set_tile_submap_compat(x, y, w, h, map_w, map);
566 }
567 inline 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) {
568  _submap_tile_offset = base_tile;
569  set_tile_submap_compat(x, y, w, h, map_w, map);
571 }
572 
574 #define fill_bkg_rect fill_rect
575 #define fill_win_rect fill_rect
576 
583 typedef struct OAM_item_t {
584  uint8_t y, x; //< X, Y Coordinates of the sprite on screen
585  uint8_t tile; //< Sprite tile number
586  uint8_t prop; //< OAM Property Flags
588 
589 
592 extern volatile struct OAM_item_t shadow_OAM[];
593 
596 extern volatile uint8_t _shadow_OAM_base;
597 
622 extern volatile uint8_t _shadow_OAM_OFF;
623 
626 #define DISABLE_VBL_TRANSFER \
627  _shadow_OAM_base = 0
628 
631 #define ENABLE_VBL_TRANSFER \
632  _shadow_OAM_base = (uint8_t)((uint16_t)&shadow_OAM >> 8)
633 
636 #define MAX_HARDWARE_SPRITES 32
637 
640 #define HARDWARE_SPRITE_CAN_FLIP_X 0
641 
644 #define HARDWARE_SPRITE_CAN_FLIP_Y 0
645 
648 inline void SET_SHADOW_OAM_ADDRESS(void * address) {
649  _shadow_OAM_base = (uint8_t)((uint16_t)address >> 8);
650 }
651 
669  shadow_OAM[nb].tile=tile;
670 }
671 
672 
680  return shadow_OAM[nb].tile;
681 }
682 
684  shadow_OAM[nb].prop = prop;
685 }
686 
688  return shadow_OAM[nb].prop;
689 }
690 
703 inline void move_sprite(uint8_t nb, uint8_t x, uint8_t y) {
704  OAM_item_t * itm = &shadow_OAM[nb];
705  itm->y=y, itm->x=x;
706 }
707 
708 
719 inline void scroll_sprite(uint8_t nb, int8_t x, int8_t y) {
720  OAM_item_t * itm = &shadow_OAM[nb];
721  itm->y+=y, itm->x+=x;
722 }
723 
724 
729 inline void hide_sprite(uint8_t nb) {
730  shadow_OAM[nb].y = 0xC0;
731 }
732 
740 
749 
758 #define set_bkg_tile_xy set_tile_xy
759 #define set_win_tile_xy set_tile_xy
760 
765 #define get_win_xy_addr get_bkg_xy_addr
766 
767 #endif /* _MSX_H */
#define OLDCALL
Definition: types.h:21
#define NONBANKED
Definition: types.h:38
#define PRESERVES_REGS(...)
Definition: types.h:31
#define Z88DK_CALLEE
Definition: types.h:45
#define Z88DK_FASTCALL
Definition: types.h:46
uint16_t palette_color_t
Definition: cgb.h:85
void b
Definition: gb.h:665
#define VDP_RSCY
Definition: hardware.h:133
#define VDP_R1
Definition: hardware.h:73
#define VDP_RSCX
Definition: hardware.h:128
#define R1_DISP_ON
Definition: hardware.h:78
void set_sprite_tile(uint8_t nb, uint8_t tile)
Definition: msx.h:668
volatile uint8_t _current_bank
Definition: gb.h:474
void d
Definition: msx.h:86
void c
Definition: msx.h:86
void set_bkg_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w)
Definition: msx.h:554
void set_tile_map(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) Z88DK_CALLEE PRESERVES_REGS(iyh
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)
Definition: msx.h:567
void joypad_ex(joypads_t *joypads) Z88DK_FASTCALL PRESERVES_REGS(iyh
void h
Definition: msx.h:215
void scroll_bkg(int8_t x, int8_t y)
Definition: msx.h:199
void set_sprite_data(uint16_t start, uint16_t ntiles, const void *src)
Definition: msx.h:505
void delay(uint16_t d) Z88DK_FASTCALL
uint16_t _current_2bpp_palette
uint16_t _current_1bpp_colors
void set_default_palette(void)
void set_palette_entry(uint8_t palette, uint8_t entry, uint16_t rgb_data) Z88DK_CALLEE PRESERVES_REGS(iyh
void add_SIO(int_handler h) Z88DK_FASTCALL
void vmemcpy(uint16_t dst, const void *src, uint16_t size) Z88DK_CALLEE PRESERVES_REGS(iyh
void remove_JOY(int_handler h) Z88DK_FASTCALL
volatile struct OAM_item_t shadow_OAM[]
volatile uint8_t _shadow_OAM_OFF
void mode(uint8_t m) OLDCALL
void set_native_sprite_data(uint16_t start, uint16_t ntiles, const void *src)
Definition: msx.h:492
uint8_t * set_attributed_tile_xy(uint8_t x, uint8_t y, uint16_t t) Z88DK_CALLEE PRESERVES_REGS(iyh
void add_JOY(int_handler h) Z88DK_FASTCALL
void WRITE_VDP_CMD(uint16_t cmd) Z88DK_FASTCALL PRESERVES_REGS(b
void set_bkg_1bpp_data(uint16_t start, uint16_t ntiles, const void *src)
Definition: msx.h:520
void add_LCD(int_handler h) Z88DK_FASTCALL PRESERVES_REGS(b
volatile uint8_t _shadow_OAM_base
Definition: gb.h:1657
uint8_t * set_tile_xy(uint8_t x, uint8_t y, uint8_t t) Z88DK_CALLEE PRESERVES_REGS(iyh
void set_sprite_1bpp_data(uint16_t start, uint16_t ntiles, const void *src) Z88DK_CALLEE
Definition: sms.h:562
uint8_t joypad(void) OLDCALL PRESERVES_REGS(b
void move_bkg(uint8_t x, uint8_t y)
Definition: msx.h:194
uint8_t * get_bkg_xy_addr(uint8_t x, uint8_t y) Z88DK_CALLEE PRESERVES_REGS(iyh
uint8_t get_sprite_prop(uint8_t nb)
Definition: msx.h:687
void scroll_sprite(uint8_t nb, int8_t x, int8_t y)
Definition: msx.h:719
void add_TIM(int_handler h) Z88DK_FASTCALL
uint8_t get_mode(void) OLDCALL
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)
Definition: msx.h:546
void set_tile_1bpp_data(uint16_t start, uint16_t ntiles, const void *src, uint16_t colors) Z88DK_CALLEE PRESERVES_REGS(iyh
volatile uint16_t sys_time
uint8_t waitpad(uint8_t mask) Z88DK_FASTCALL PRESERVES_REGS(b
void SET_SHADOW_OAM_ADDRESS(void *address)
Definition: msx.h:648
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: msx.h:541
void set_bkg_data(uint16_t start, uint16_t ntiles, const void *src)
Definition: msx.h:502
void wait_vbl_done(void) PRESERVES_REGS(b
void set_tile_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE PRESERVES_REGS(iyh
void cpu_fast(void)
Definition: msx.h:478
struct OAM_item_t OAM_item_t
void set_data(uint16_t dst, const void *src, uint16_t size) Z88DK_CALLEE PRESERVES_REGS(iyh
void set_palette(uint8_t first_palette, uint8_t nb_palettes, palette_color_t *rgb_data) Z88DK_CALLEE
void(* int_handler)(void) NONBANKED
Definition: msx.h:152
#define __READ_VDP_REG(REG)
Definition: msx.h:84
void remove_TIM(int_handler h) Z88DK_FASTCALL
void remove_SIO(int_handler h) Z88DK_FASTCALL
void remove_VBL(int_handler h) Z88DK_FASTCALL PRESERVES_REGS(iyh
void set_sprite_prop(uint8_t nb, uint8_t prop)
Definition: msx.h:683
void set_2bpp_palette(uint16_t palette)
Definition: msx.h:498
void WRITE_VDP_DATA(uint16_t data) Z88DK_FASTCALL PRESERVES_REGS(b
#define __WRITE_VDP_REG(REG, v)
Definition: msx.h:83
uint8_t _map_tile_offset
void remove_LCD(int_handler h) Z88DK_FASTCALL PRESERVES_REGS(b
void iyh
Definition: msx.h:86
void move_sprite(uint8_t nb, uint8_t x, uint8_t y)
Definition: msx.h:703
uint8_t _submap_tile_offset
void set_tile_submap_compat(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t map_w, const uint8_t *map) Z88DK_CALLEE PRESERVES_REGS(iyh
void fill_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint16_t tile) Z88DK_CALLEE PRESERVES_REGS(iyh
void set_win_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w)
Definition: msx.h:557
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: msx.h:562
uint8_t joypad_init(uint8_t npads, joypads_t *joypads) Z88DK_CALLEE
void refresh_OAM(void)
void waitpadup(void) PRESERVES_REGS(b
void SWITCH_ROM(uint8_t bank) Z88DK_FASTCALL PRESERVES_REGS(b
void hide_sprite(uint8_t nb)
Definition: msx.h:729
void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor)
Definition: msx.h:516
uint8_t cancel_pending_interrupts()
Definition: msx.h:190
void add_VBL(int_handler h) Z88DK_FASTCALL PRESERVES_REGS(d
void set_native_tile_data(uint16_t start, uint16_t ntiles, const void *src) Z88DK_CALLEE
void e
Definition: msx.h:86
void set_interrupts(uint8_t flags) Z88DK_FASTCALL
void set_bkg_4bpp_data(uint16_t start, uint16_t ntiles, const void *src)
Definition: msx.h:488
void iyl
Definition: msx.h:86
void vsync(void) PRESERVES_REGS(b
uint8_t get_sprite_tile(uint8_t nb)
Definition: msx.h:679
void l
Definition: msx.h:215
void set_vram_byte(uint8_t *addr, uint8_t v) Z88DK_CALLEE PRESERVES_REGS(iyh
void display_off(void)
Definition: msx.h:225
short int int16_t
Definition: stdint.h:44
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