gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-lib/include/gb/gb.h
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 // Here NINTENDO means Game Boy & related clones
13 #define NINTENDO
14
15 #ifdef SEGA
16 #undef SEGA
17 #endif
18
19 #ifdef NINTENDO_NES
20 #undef NINTENDO_NES
21 #endif
22
23 #ifdef MSX
24 #undef MSX
25 #endif
26
27 #define SYSTEM_60HZ 0x00
28 #define SYSTEM_50HZ 0x01
29
30 #if defined(__TARGET_ap)
31 #define ANALOGUEPOCKET
32 #elif defined(__TARGET_gb)
33 #define GAMEBOY
34 #elif defined(__TARGET_duck)
35 #define MEGADUCK
36 #endif
37
38
39 /** Joypad bits.
40 A logical OR of these is used in the wait_pad and joypad
41 functions. For example, to see if the B button is pressed
42 try
43
44 uint8_t keys;
45 keys = joypad();
46 if (keys & J_B) {
47 ...
48 }
49
50 @see joypad
51 */
52 #define J_UP 0x04U
53 #define J_DOWN 0x08U
54 #define J_LEFT 0x02U
55 #define J_RIGHT 0x01U
56 #define J_A 0x10U
57 #define J_B 0x20U
58 #define J_SELECT 0x40U
59 #define J_START 0x80U
60
61 /** Screen modes.
62 Normally used by internal functions only.
63 @see mode()
64 */
65 #define M_DRAWING 0x01U
66 #define M_TEXT_OUT 0x02U
67 #define M_TEXT_INOUT 0x03U
68 /** Set this in addition to the others to disable scrolling
69
70 If scrolling is disabled, the cursor returns to (0,0)
71 @see mode()
72 */
73 #define M_NO_SCROLL 0x04U
74 /** Set this to disable interpretation
75 @see mode()
76 */
77 #define M_NO_INTERP 0x08U
78
79 /** If this bit set clear, the tile from the second
80 VRAM bank is used
81 @see set_sprite_prop()
82 */
83 #define S_BANK 0x08U
84 /** If this is set, sprite colours come from OBJ1PAL. Else
85 they come from OBJ0PAL
86 @see set_sprite_prop().
87 */
88 #define S_PALETTE 0x10U
89 /** If set the sprite will be flipped horizontally.
90 @see set_sprite_prop()
91 */
92 #define S_FLIPX 0x20U
93 /** If set the sprite will be flipped vertically.
94 @see set_sprite_prop()
95 */
96 #define S_FLIPY 0x40U
97 /** If this bit is clear, then the sprite will be displayed
98 on top of the background and window.
99 @see set_sprite_prop()
100 */
101 #define S_PRIORITY 0x80U
102 /** Defines how palette number is encoded in OAM.
103 Required for the png2asset tool's metasprite output.
104 */
105 #define S_PAL(n) n
106
107 /* Interrupt flags */
108 /** Disable calling of interrupt service routines
109 */
110 #define EMPTY_IFLAG 0x00U
111 /** VBlank Interrupt occurs at the start of the vertical blank.
112
113 During this period the video ram may be freely accessed.
114 @see set_interrupts(), @see add_VBL
115 */
116 #define VBL_IFLAG 0x01U
117 /** LCD Interrupt when triggered by the STAT register.
118 @see set_interrupts(), @see add_LCD
119 */
120 #define LCD_IFLAG 0x02U
121 /** Timer Interrupt when the timer @ref TIMA_REG overflows.
122 @see set_interrupts(), @see add_TIM
123 */
124 #define TIM_IFLAG 0x04U
125 /** Serial Link Interrupt occurs when the serial transfer has completed.
126 @see set_interrupts(), @see add_SIO
127 */
128 #define SIO_IFLAG 0x08U
129 /** Joypad Interrupt occurs on a transition of the keypad.
130 @see set_interrupts(), @see add_JOY
131 */
132 #define JOY_IFLAG 0x10U
133
134
135 /* DMG Palettes */
136 #define DMG_BLACK 0x03
137 #define DMG_DARK_GRAY 0x02
138 #define DMG_LITE_GRAY 0x01
139 #define DMG_WHITE 0x00
140 /** Macro to create a DMG palette from 4 colors
141
142 @param C0 Color for Index 0
143 @param C1 Color for Index 1
144 @param C2 Color for Index 2
145 @param C3 Color for Index 3
146
147 The resulting format is four greyscale colors
148 packed into a single unsigned byte.
149
150 Example:
151 \code{.c}
152 BGP_REG = DMG_PALETTE(DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE);
153 \endcode
154
155 @see OBP0_REG, OBP1_REG, BGP_REG
156 @see DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE
157
158 */
159 #define DMG_PALETTE(C0, C1, C2, C3) ((uint8_t)((((C3) & 0x03) << 6) | (((C2) & 0x03) << 4) | (((C1) & 0x03) << 2) | ((C0) & 0x03)))
160
161 /* Limits */
162 /** Width of the visible screen in pixels.
163 */
164 #define SCREENWIDTH DEVICE_SCREEN_PX_WIDTH
165 /** Height of the visible screen in pixels.
166 */
167 #define SCREENHEIGHT DEVICE_SCREEN_PX_HEIGHT
168 /** The Minimum X position of the Window Layer (Left edge of screen) @see move_win()
169 */
170 #define MINWNDPOSX DEVICE_WINDOW_PX_OFFSET_X
171 /** The Minimum Y position of the Window Layer (Top edge of screen) @see move_win()
172 */
173 #define MINWNDPOSY DEVICE_WINDOW_PX_OFFSET_Y
174 /** The Maximum X position of the Window Layer (Right edge of screen) @see move_win()
175 */
176 #define MAXWNDPOSX (DEVICE_WINDOW_PX_OFFSET_X + DEVICE_SCREEN_PX_WIDTH - 1)
177 /** The Maximum Y position of the Window Layer (Bottom edge of screen) @see move_win()
178 */
179 #define MAXWNDPOSY (DEVICE_WINDOW_PX_OFFSET_Y + DEVICE_SCREEN_PX_HEIGHT - 1)
180
181
182 /** Interrupt handlers
183 */
184 typedef void (*int_handler)(void) NONBANKED;
185
186 /** The remove functions will remove any interrupt handler.
187
188 A handler of NULL will cause bad things
189 to happen if the given interrupt is enabled.
190
191 Removes the VBL interrupt handler. @see add_VBL()
192 */
193 void remove_VBL(int_handler h);
194
195 /** Removes the LCD interrupt handler.
196 @see add_LCD(), remove_VBL()
197 */
198 void remove_LCD(int_handler h);
199
200 /** Removes the TIM interrupt handler.
201 @see add_TIM(), remove_VBL()
202 */
203 void remove_TIM(int_handler h);
204
205 /** Removes the Serial Link / SIO interrupt handler.
206 @see add_SIO(), @see remove_VBL()
207
208 The default SIO ISR gets installed automatically if
209 any of the standard SIO calls are used
210 (@ref send_byte(), @ref receive_byte()).
211
212 Once installed the default SIO ISR cannot be removed.
213 Only secondary chained SIO ISRs (added with add_SIO())
214 can be removed.
215 */
216 void remove_SIO(int_handler h);
217
218 /** Removes the JOY interrupt handler.
219 @see add_JOY(), remove_VBL()
220 */
221 void remove_JOY(int_handler h);
222
223 /** Adds a Vertical Blanking interrupt handler.
224
225 @param h The handler to be called whenever a V-blank
226 interrupt occurs.
227
228 Up to 4 handlers may be added, with the last added
229 being called last.
230
231 __Do not__ use the function definition attributes
232 @ref CRITICAL and @ref INTERRUPT when declaring
233 ISR functions added via add_VBL() (or LCD, etc).
234 Those attributes are only required when constructing
235 a bare jump from the interrupt vector itself (such as
236 with @ref ISR_VECTOR()).
237
238 ISR handlers added using add_VBL()/etc are instead
239 called via the GBDK ISR dispatcher which makes
240 the extra function attributes unecessary.
241
242 @note The default GBDK VBL is installed automatically.
243
244 @see ISR_VECTOR()
245 */
246 void add_VBL(int_handler h);
247
248 /** Adds a LCD interrupt handler.
249
250 Called when the LCD interrupt occurs.
251
252 Up to 3 handlers may be added, with the last added
253 being called last.
254
255 There are various sources controlled by the
256 @ref STAT_REG register ($FF41) which can trigger
257 this interrupt. Common examples include triggering
258 on specific scanlines using @ref LY_REG == @ref LYC_REG.
259 Another is applying graphics effects on a per-scanline
260 basis such as modifying the X and Y scroll registers
261 (@ref SCX_REG / @ref SCY_REG registers).
262
263 @note LYC may not trigger with scanline 0 in the same
264 way as other scanlines due to particular behavior
265 with scanlines 153 and 0. Instead, using an add_VBL()
266 interrupt handler for start of frame behavior may be
267 more suitable.
268
269 __Do not__ use the function definition attributes
270 @ref CRITICAL and @ref INTERRUPT when declaring
271 ISR functions added via add_VBL() (or LCD, etc).
272 Those attributes are only required when constructing
273 a bare jump from the interrupt vector itself (such as
274 with @ref ISR_VECTOR()).
275
276 ISR handlers added using add_VBL/LCD/etc are instead
277 called via the GBDK ISR dispatcher which makes
278 the extra function attributes unecessary.
279
280 If this ISR is to be called once per each scanline then
281 make sure that the time it takes to execute is less
282 than the duration of a scanline.
283
284 @see add_VBL, nowait_int_handler, ISR_VECTOR()
285 */
286 void add_LCD(int_handler h);
287
288 /** Adds a timer interrupt handler.
289
290 Can not be used together with @ref add_low_priority_TIM
291
292 This interrupt occurs when the @ref TIMA_REG
293 register ($FF05) changes from $FF to $00.
294
295 Up to 4 handlers may be added, with the last added
296 being called last.
297
298 @note
299 For NES make sure to wrap TIM interrupt handlers
300 with a nooverlay pragma.
301 For more details see @ref docs_nes_tim_overlay
302
303 @see add_VBL
304 @see set_interrupts() with TIM_IFLAG, ISR_VECTOR()
305 */
306 void add_TIM(int_handler h);
307
308 /** Adds a timer interrupt handler, that could be
309 interrupted by the other interrupts,
310 as well as itself, if it runs too slow.
311
312 Can not be used together with @ref add_TIM
313
314 This interrupt occurs when the @ref TIMA_REG
315 register ($FF05) changes from $FF to $00.
316
317 Up to 4 handlers may be added, with the last added
318 being called last.
319
320 @see add_VBL
321 @see set_interrupts() with TIM_IFLAG, ISR_VECTOR()
322 */
323 void add_low_priority_TIM(int_handler h);
324
325 /** Adds a Serial Link transmit complete interrupt handler.
326
327 This interrupt occurs when a serial transfer has
328 completed on the game link port.
329
330 Up to 4 handlers may be added, with the last added
331 being called last.
332
333 The default SIO ISR gets installed automatically if
334 any of the standard SIO calls are used
335 (@ref send_byte(), @ref receive_byte()).
336
337 @see send_byte, receive_byte(), add_VBL()
338 @see set_interrupts() with SIO_IFLAG
339 */
340 void add_SIO(int_handler h);
341
342
343 /** Adds a joypad button change interrupt handler.
344
345 This interrupt occurs on a transition of any of the
346 keypad input lines from high to low, if the relevant
347 @ref P1_REG bits 4 or 5 are set.
348
349 For details about configuring flags or reading the data see:
350 https://gbdev.io/pandocs/Interrupt_Sources.html#int-60--joypad-interrupt
351 https://gbdev.io/pandocs/Joypad_Input.html#ff00--p1joyp-joypad
352
353 Due to the fact that keypad "bounce" is virtually always
354 present, software should expect this interrupt to occur
355 one or more times for every button press and one or more
356 times for every button release.
357
358 Up to 4 handlers may be added, with the last added
359 being called last.
360
361 An example use of this is allowing the user to trigger an
362 exit from the lower-power STOP cpu state.
363
364 @see joypad(), add_VBL(), IEF_HILO, P1F_5, P1F_4, P1F_3, P1F_2, P1F_1, P1F_0, P1F_GET_DPAD, P1F_GET_BTN, P1F_GET_NONE
365 */
366 void add_JOY(int_handler h);
367
368
369 /** Interrupt handler chain terminator that does __not__ wait for .STAT
370
371 You must add this handler last in every interrupt handler
372 chain if you want to change the default interrupt handler
373 behaviour that waits for LCD controller mode to become 1 or 0
374 before return from the interrupt.
375
376 Example:
377 \code{.c}
378 CRITICAL {
379 add_SIO(nowait_int_handler); // Disable wait on VRAM state before returning from SIO interrupt
380 }
381 \endcode
382 @see wait_int_handler()
383 */
384 void nowait_int_handler(void);
385
386
387 /** Default Interrupt handler chain terminator that waits for
388 @see STAT_REG and __only__ returns at the BEGINNING of
389 either Mode 0 or Mode 1.
390
391 Used by default at the end of interrupt chains to help
392 prevent graphical glitches. The glitches are caused when an
393 ISR interrupts a graphics operation in one mode but returns
394 in a different mode for which that graphics operation is not
395 allowed.
396
397 @see nowait_int_handler()
398 */
399 void wait_int_handler(void);
400
401 /** Cancel pending interrupts
402 */
403 inline uint8_t cancel_pending_interrupts(void) {
404 return IF_REG = 0;
405 }
406
407 /** Set the current screen mode - one of M_* modes
408
409 Normally used by internal functions only.
410
411 @see M_DRAWING, M_TEXT_OUT, M_TEXT_INOUT, M_NO_SCROLL, M_NO_INTERP
412 */
413 void mode(uint8_t m);
414
415 /** Returns the current mode
416
417 @see M_DRAWING, M_TEXT_OUT, M_TEXT_INOUT, M_NO_SCROLL, M_NO_INTERP
418 */
419 uint8_t get_mode(void) PRESERVES_REGS(b, c, d, e, h, l);
420
421 /** Returns the system gbdk is running on.
422
423 @see SYSTEM_50HZ, SYSTEM_60HZ, SYSTEM_BITS_DENDY, SYSTEM_BITS_NTSC, SYSTEM_BITS_PAL, SYSTEM_NTSC
424 SYSTEM_PAL
425 */
426 inline uint8_t get_system(void) {
427 return SYSTEM_60HZ;
428 }
429
430 /** GB CPU type
431
432 @see DMG_TYPE, MGB_TYPE, CGB_TYPE, cpu_fast(), cpu_slow(), _is_GBA
433 */
434 extern uint8_t _cpu;
435
436 /** Hardware Model: Original GB or Super GB. @see _cpu
437 */
438 #define DMG_TYPE 0x01
439 /** Hardware Model: Pocket GB or Super GB 2. @see _cpu
440 */
441 #define MGB_TYPE 0xFF
442 /** Hardware Model: Color GB. @see _cpu
443 */
444 #define CGB_TYPE 0x11
445
446 /** GBA detection
447
448 @see GBA_DETECTED, GBA_NOT_DETECTED, _cpu
449 */
450 extern uint8_t _is_GBA;
451
452 /** Hardware Model: DMG, CGB or MGB. @see _cpu, _is_GBA
453 */
454 #define GBA_NOT_DETECTED 0x00
455 /** Hardware Model: GBA. @see _cpu, _is_GBA
456 */
457 #define GBA_DETECTED 0x01
458
459 /** Macro returns TRUE if device supports color
460 */
461 #define DEVICE_SUPPORTS_COLOR (_cpu == CGB_TYPE)
462
463 /** Macro returns TRUE if device supports window layer
464 */
465 #define DEVICE_SUPPORTS_WINDOW (TRUE)
466
467 /** Macro returns TRUE if device supports reading from VRAM
468 */
469 #define DEVICE_SUPPORTS_VRAM_READ (TRUE)
470
471 /** Global Time Counter in VBL periods (60Hz)
472
473 Increments once per Frame
474
475 Will wrap around every ~18 minutes (unsigned 16 bits = 65535 / 60 / 60 = 18.2)
476 */
477 extern volatile uint16_t sys_time;
478
479 /** Flag indicating the VBlank ISR has run
480
481 Flag gets cleared at the start of @ref vsync() / @ref wait_vbl_done()
482 and set in the default VBlank ISR handler.
483 */
484 __REG _vbl_done;
485 #define VBL_DONE _vbl_done
486
487
488 /** Serial Link: Send the byte in @ref _io_out out through the serial port
489
490 Make sure to enable interrupts for the
491 Serial Link before trying to transfer data.
492 @see add_SIO(), remove_SIO()
493 @see set_interrupts() with @ref SIO_IFLAG
494 */
495 void send_byte(void);
496
497 /** Serial Link: Receive a byte from the serial port into @ref _io_in
498
499 Make sure to enable interrupts for the
500 Serial Link before trying to transfer data.
501 @see add_SIO(), remove_SIO()
502 @see set_interrupts() with @ref SIO_IFLAG
503 */
504 void receive_byte(void);
505
506 /** Serial Link: Current IO Status. An OR of IO_* */
507 extern volatile uint8_t _io_status;
508
509 /** Serial Link: Byte just read after calling @ref receive_byte()
510 */
511 extern volatile uint8_t _io_in;
512
513 /** Serial Link: Write byte to send here before calling @ref send_byte()
514 */
515 extern volatile uint8_t _io_out;
516
517 /* Status codes */
518 /** Serial Link IO is completed */
519 #define IO_IDLE 0x00U
520 /** Serial Link Sending data */
521 #define IO_SENDING 0x01U
522 /** Serial Link Receiving data */
523 #define IO_RECEIVING 0x02U
524 /** Serial Link Error */
525 #define IO_ERROR 0x04U
526
527
528
529 /** Tracks current active ROM bank
530
531 In most cases the @ref CURRENT_BANK macro for this variable
532 is recommended for use instead of the variable itself.
533
534 The active bank number is not tracked by @ref _current_bank when
535 @ref SWITCH_ROM_MBC5_8M is used.
536
537 This variable is updated automatically when you call SWITCH_ROM_MBC1 or
538 SWITCH_ROM_MBC5, SWITCH_ROM(), or call a BANKED function.
539
540 @see SWITCH_ROM_MBC1(), SWITCH_ROM_MBC5(), SWITCH_ROM()
541 */
542 __REG _current_bank;
543 #define CURRENT_BANK _current_bank
544
545 /** Obtains the __bank number__ of VARNAME
546
547 @param VARNAME Name of the variable which has a __bank_VARNAME companion symbol which is adjusted by bankpack
548
549 Use this to obtain the bank number from a bank reference
550 created with @ref BANKREF().
551
552 @see BANKREF_EXTERN(), BANKREF()
553 */
554 #ifndef BANK
555 #define BANK(VARNAME) ( (uint8_t) & __bank_ ## VARNAME )
556 #endif
557
558 /** Creates a reference for retrieving the bank number of a variable or function
559
560 @param VARNAME Variable name to use, which may be an existing identifier
561
562 @see BANK() for obtaining the bank number of the included data.
563
564 More than one `BANKREF()` may be created per file, but each call should
565 always use a unique VARNAME.
566
567 Use @ref BANKREF_EXTERN() within another source file
568 to make the variable and it's data accesible there.
569 */
570 #define BANKREF(VARNAME) void __func_ ## VARNAME(void) __banked __naked { \
571 __asm \
572 .local b___func_ ## VARNAME \
573 ___bank_ ## VARNAME = b___func_ ## VARNAME \
574 .globl ___bank_ ## VARNAME \
575 __endasm; \
576 }
577
578 /** Creates extern references for accessing a BANKREF() generated variable.
579
580 @param VARNAME Name of the variable used with @ref BANKREF()
581
582 This makes a @ref BANKREF() reference in another source
583 file accessible in the current file for use with @ref BANK().
584
585 @see BANKREF(), BANK()
586 */
587 #define BANKREF_EXTERN(VARNAME) extern const void __bank_ ## VARNAME;
588
589 /** Makes default platform MBC switch the active ROM bank
590 @param b ROM bank to switch to (max 255)
591
592 \li When used with MBC1 the max bank is Bank 31 (512K).
593 \li When used with MBC5 the max bank is Bank 255 (4MB).
594 \li To use the full 8MB size of MBC5 see @ref SWITCH_ROM_MBC5_8M().
595
596 \li For MBC1 some banks in it's range are unavailable
597 (typically 0x20, 0x40, 0x60).
598
599 @note Using @ref SWITCH_ROM_MBC5_8M() should not be mixed with using
600 @ref SWITCH_ROM_MBC5() and @ref SWITCH_ROM().
601
602 @see SWITCH_ROM_MBC1, SWITCH_ROM_MBC5, SWITCH_ROM_MEGADUCK
603 */
604 #define SWITCH_ROM(b) (_current_bank = (b), rROMB0 = (b))
605
606 #if defined(__TARGET_duck)
607
608 #define SWITCH_RAM(b) (0)
609
610 #define ENABLE_RAM
611
612 #define DISABLE_RAM
613
614 #else
615
616 /** Switches SRAM bank on MBC1 and other compatible MBCs
617 @param b SRAM bank to switch to
618
619 Before switching SRAM banks enable it using @ref ENABLE_RAM
620
621 @see SWITCH_RAM_MBC1, SWITCH_RAM_MBC5
622 */
623 #define SWITCH_RAM(b) (rRAMB = (b))
624
625 /** Enables SRAM on MBC1 and other compatible MBCs
626 */
627 #define ENABLE_RAM (rRAMG = 0x0A)
628
629 /** Disables SRAM on MBC1 and other compatible MBCs
630 */
631 #define DISABLE_RAM (rRAMG = 0x00)
632
633 #endif
634
635 /** Makes MEGADUCK MBC switch the active ROM bank
636 @param b ROM bank to switch to (max `3` for 64K, or `7` for 128K)
637 */
638 #define SWITCH_ROM_MEGADUCK(b) SWITCH_ROM(b)
639
640 /** Makes MBC1 and other compatible MBCs switch the active ROM bank
641 @param b ROM bank to switch to
642
643 For MBC1 some banks in it's range are unavailable
644 (typically 0x20, 0x40, 0x60).
645
646 See pandocs for more details https://gbdev.io/pandocs/MBC1
647 */
648 #define SWITCH_ROM_MBC1(b) SWITCH_ROM(b)
649
650 /** Switches SRAM bank on MBC1 and other compatible MBCs
651 @param b SRAM bank to switch to
652
653 Before switching SRAM banks enable it using @ref ENABLE_RAM
654
655 @see SWITCH_RAM, SWITCH_RAM_MBC5
656 */
657 #define SWITCH_RAM_MBC1(b) SWITCH_RAM(b)
658
659 /** Enables SRAM on MBC1
660 */
661 #define ENABLE_RAM_MBC1 ENABLE_RAM
662
663 /** Disables SRAM on MBC1
664 */
665 #define DISABLE_RAM_MBC1 DISABLE_RAM
666
667 #define SWITCH_16_8_MODE_MBC1 (*(volatile uint8_t *)0x6000 = 0x00)
668
669 #define SWITCH_4_32_MODE_MBC1 (*(volatile uint8_t *)0x6000 = 0x01)
670
671 /** Makes MBC5 switch to the active ROM bank
672 @param b ROM bank to switch to (max 255)
673
674 Supports up to ROM bank 255 (4 MB).
675
676 @ref SWITCH_ROM_MBC5_8M may be used if the full 8MB size is needed.
677
678 @note Using @ref SWITCH_ROM_MBC5_8M() should not be mixed with using
679 @ref SWITCH_ROM_MBC5() and @ref SWITCH_ROM().
680
681 Note the order used here. Writing the other way around on a MBC1 always selects bank 1
682 */
683 #define SWITCH_ROM_MBC5(b) (_current_bank = (b), rROMB1 = 0, rROMB0 = (b))
684
685 /** Makes MBC5 to switch the active ROM bank using the full 8MB size.
686 @see CURRENT_BANK
687 @param b ROM bank to switch to
688
689 This is an alternate to @ref SWITCH_ROM_MBC5 which is limited to 4MB.
690
691 Note:
692 \li Banked SDCC calls are not supported if you use this macro.
693 \li The active bank number is not tracked by @ref CURRENT_BANK if you use this macro.
694 \li Using @ref SWITCH_ROM_MBC5_8M() should not be mixed with using @ref SWITCH_ROM_MBC5() and @ref SWITCH_ROM().
695
696 Note the order used here. Writing the other way around on a MBC1 always selects bank 1
697 */
698 #define SWITCH_ROM_MBC5_8M(b) (rROMB1 = ((uint16_t)(b) >> 8), rROMB0 = (b))
699
700 /** Switches SRAM bank on MBC5
701 @param b SRAM bank to switch to
702
703 Before switching SRAM banks enable it using @ref ENABLE_RAM
704 */
705 #define SWITCH_RAM_MBC5(b) SWITCH_RAM(b)
706
707 /** Enables SRAM on MBC5
708 */
709 #define ENABLE_RAM_MBC5 ENABLE_RAM
710
711 /** Disables SRAM on MBC5
712 */
713 #define DISABLE_RAM_MBC5 DISABLE_RAM
714
715
716
717 /** Delays the given number of milliseconds.
718 Uses no timers or interrupts, and can be called with
719 interrupts disabled
720 */
721 void delay(uint16_t d) PRESERVES_REGS(h, l);
722
723
724
725 /** Reads and returns the current state of the joypad.
726 Follows Nintendo's guidelines for reading the pad.
727 Return value is an OR of J_*
728
729 When testing for multiple different buttons, it's
730 best to read the joypad state *once* into a variable
731 and then test using that variable.
732
733 @see J_START, J_SELECT, J_A, J_B, J_UP, J_DOWN, J_LEFT, J_RIGHT
734 */
735 uint8_t joypad(void) PRESERVES_REGS(b, c, h, l);
736
737 /** Waits until at least one of the buttons given in mask are pressed.
738
739 @param mask Bitmask indicating which buttons to wait for
740
741 Normally only used for checking one key, but it will
742 support many, even J_LEFT at the same time as J_RIGHT. :)
743
744 @note Checks in a loop that doesn't HALT at all, so the CPU
745 will be maxed out until this call returns.
746 @see joypad
747 @see J_START, J_SELECT, J_A, J_B, J_UP, J_DOWN, J_LEFT, J_RIGHT
748 */
749 uint8_t waitpad(uint8_t mask) PRESERVES_REGS(b, c, h, l);
750
751 /** Waits for the directional pad and all buttons to be released.
752
753 @note Checks in a loop that doesn't HALT at all, so the CPU
754 will be maxed out until this call returns.
755 */
756 void waitpadup(void) PRESERVES_REGS(a, b, c, d, e, h, l);
757
758 /** Multiplayer joypad structure.
759
760 Must be initialized with @ref joypad_init() first then it
761 may be used to poll all avaliable joypads with @ref joypad_ex()
762 */
763 typedef struct {
764 uint8_t npads;
765 union {
766 struct {
767 uint8_t joy0, joy1, joy2, joy3;
768 };
769 uint8_t joypads[4];
770 };
771 } joypads_t;
772
773 /** Initializes joypads_t structure for polling multiple joypads
774 (for the GB and ones connected via SGB)
775 @param npads number of joypads requested (1, 2 or 4)
776 @param joypads pointer to joypads_t structure to be initialized
777
778 Only required for @ref joypad_ex, not required for calls to regular @ref joypad()
779 @returns number of joypads avaliable
780 @see joypad_ex(), joypads_t
781 */
782 uint8_t joypad_init(uint8_t npads, joypads_t * joypads) OLDCALL;
783
784 /** Polls all avaliable joypads (for the GB and ones connected via SGB)
785 @param joypads pointer to joypads_t structure to be filled with joypad statuses,
786 must be previously initialized with joypad_init()
787
788 @see joypad_init(), joypads_t
789 */
790 void joypad_ex(joypads_t * joypads) PRESERVES_REGS(b, c);
791
792
793
794 /** Enables unmasked interrupts
795
796 @note Use @ref CRITICAL {...} instead for creating a block of
797 of code which should execute with interrupts temporarily
798 turned off.
799
800 @see disable_interrupts, set_interrupts, CRITICAL
801 */
802 inline void enable_interrupts(void) PRESERVES_REGS(a, b, c, d, e, h, l) {
803 __asm__("ei");
804 }
805
806 /** Disables interrupts
807
808 @note Use @ref CRITICAL {...} instead for creating a block of
809 of code which should execute with interrupts temporarily
810 turned off.
811
812 This function may be called as many times as you like;
813 however the first call to @ref enable_interrupts will re-enable
814 them.
815
816 @see enable_interrupts, set_interrupts, CRITICAL
817 */
818 inline void disable_interrupts(void) PRESERVES_REGS(a, b, c, d, e, h, l) {
819 __asm__("di");
820 }
821
822 /** Clears any pending interrupts and sets the interrupt mask
823 register IO to flags.
824 @param flags A logical OR of *_IFLAGS
825
826 @note This disables and then re-enables interrupts so it
827 must be used outside of a critical section.
828
829 @see enable_interrupts(), disable_interrupts()
830 @see VBL_IFLAG, LCD_IFLAG, TIM_IFLAG, SIO_IFLAG, JOY_IFLAG
831 */
832 void set_interrupts(uint8_t flags) PRESERVES_REGS(b, c, d, e, h, l);
833
834 /** Performs a soft reset.
835
836 For the Game Boy and related it does this by jumping to address 0x0150
837 which is in crt0.s (the c-runtime that executes before main() is called).
838
839 This performs various startup steps such as resetting the stack,
840 clearing WRAM and OAM, resetting initialized variables and some
841 display registers (scroll, window, LCDC), etc.
842
843 This is not the same a hard power reset.
844 */
845 void reset(void);
846
847 /** HALTs the CPU and waits for the vertical blank interrupt and then
848 returns when all registered VBL ISRs have completed.
849
850 This is often used in main loops to idle the CPU at low power
851 until it's time to start the next frame. It's also useful for
852 syncing animation with the screen re-draw.
853
854 Warning: If the VBL interrupt is disabled, this function will
855 never return. If the screen is off this function returns
856 immediately.
857 */
858 void vsync(void) PRESERVES_REGS(b, c, d, e, h, l);
859
860 /** Obsolete. This function has been replaced by vsync(), which has identical behavior.
861
862 */
863 void wait_vbl_done(void) PRESERVES_REGS(b, c, d, e, h, l);
864
865 /** Turns the display off.
866
867 Waits until the VBL before turning the display off.
868 @see DISPLAY_ON
869 */
870 void display_off(void) PRESERVES_REGS(b, c, d, e, h, l);
871
872 /** Copies data from shadow OAM to OAM
873 */
874 void refresh_OAM(void) PRESERVES_REGS(b, c, d, e, h, l);
875
876
877 /** Copies data from somewhere in the lower address space to part of hi-ram.
878 @param dst Offset in high ram (0xFF00 and above) to copy to.
879 @param src Area to copy from
880 @param n Number of bytes to copy.
881 */
882 void hiramcpy(uint8_t dst, const void *src, uint8_t n) OLDCALL PRESERVES_REGS(b, c);
883
884
885 /** Turns the display back on.
886 @see display_off, DISPLAY_OFF
887 */
888 #define DISPLAY_ON \
889 LCDC_REG|=LCDCF_ON
890
891 /** Turns the display off
892
893 Waits until the VBL before turning the display off.
894 @see display_off, DISPLAY_ON
895 */
896 #define DISPLAY_OFF \
897 display_off();
898
899 /** Does nothing for GB
900 */
901 #define HIDE_LEFT_COLUMN
902
903 /** Does nothing for GB
904 */
905 #define SHOW_LEFT_COLUMN
906
907 /** Does nothing for GB
908 */
909 #define SET_BORDER_COLOR(C)
910
911 /** Turns on the background layer.
912 Sets bit 0 of the LCDC register to 1.
913
914 Doesn't work in CGB mode - the bit is reused to control sprite priority
915 over background and window layers instead.
916 \li If 1 (SHOW_BKG), everything works as usual.
917 \li If 0 (HIDE_BKG), all sprites are always drawn over background and window,
918 ignoring any other priority settings.
919 */
920 #define SHOW_BKG \
921 LCDC_REG|=LCDCF_BGON
922
923 /** Turns off the background layer.
924 Sets bit 0 of the LCDC register to 0.
925
926 Doesn't work in CGB mode - the bit is reused to control sprite priority
927 over background and window layers instead.
928 \li If 1 (SHOW_BKG), everything works as usual.
929 \li If 0 (HIDE_BKG), all sprites are always drawn over background and window,
930 ignoring any other priority settings.
931 */
932 #define HIDE_BKG \
933 LCDC_REG&=~LCDCF_BGON
934
935 /** Turns on the Window layer
936 Sets bit 5 of the LCDC register to 1.
937
938 This only controls Window visibility. If either
939 the Background layer (which the window is part of)
940 or the Display are not turned then the Window contents
941 will not be visible. Those can be turned on using
942 @ref SHOW_BKG and @ref DISPLAY_ON.
943 */
944 #define SHOW_WIN \
945 LCDC_REG|=LCDCF_WINON
946
947 /** Turns off the window layer.
948 Clears bit 5 of the LCDC register to 0.
949 */
950 #define HIDE_WIN \
951 LCDC_REG&=~LCDCF_WINON
952
953 /** Turns on the sprites layer.
954 Sets bit 1 of the LCDC register to 1.
955 */
956 #define SHOW_SPRITES \
957 LCDC_REG|=LCDCF_OBJON
958
959 /** Turns off the sprites layer.
960 Clears bit 1 of the LCDC register to 0.
961
962 @see hide_sprite, hide_sprites_range
963 */
964 #define HIDE_SPRITES \
965 LCDC_REG&=~LCDCF_OBJON
966
967 /** Sets sprite size to 8x16 pixels, two tiles one above the other.
968 Sets bit 2 of the LCDC register to 1.
969 */
970 #define SPRITES_8x16 \
971 LCDC_REG|=LCDCF_OBJ16
972
973 /** Sets sprite size to 8x8 pixels, one tile.
974 Clears bit 2 of the LCDC register to 0.
975 */
976 #define SPRITES_8x8 \
977 LCDC_REG&=~LCDCF_OBJ16
978
979
980
981 /**
982 * Set byte in vram at given memory location
983 *
984 * @param addr address to write to
985 * @param v value
986 */
987 void set_vram_byte(uint8_t * addr, uint8_t v) PRESERVES_REGS(b, c);
988
989 /**
990 * Get byte from vram at given memory location
991 * @param addr address to read from
992 * @return read value
993
994 @note In general **avoid reading from VRAM**
995 since that memory is not accessible at all times.
996 It is also not supported by GBDK on the NES platform.
997 See @ref best_practice_dont_read_vram "coding guidelines"
998 for more details.
999 */
1000 uint8_t get_vram_byte(uint8_t * addr) PRESERVES_REGS(b, c, h, l);
1001
1002
1003 /**
1004 * Get address of X,Y tile of background map
1005 */
1006 uint8_t * get_bkg_xy_addr(uint8_t x, uint8_t y) PRESERVES_REGS(h, l);
1007
1008 #define COMPAT_PALETTE(C0,C1,C2,C3) ((uint8_t)(((C3) << 6) | ((C2) << 4) | ((C1) << 2) | (C0)))
1009
1010 /** Sets palette for 2bpp color translation for GG/SMS, does nothing on GB
1011 */
1012 inline void set_2bpp_palette(uint16_t palette) {
1013 palette;
1014 }
1015
1016 extern uint16_t _current_1bpp_colors;
1017
1018 /** Sets the Foreground and Background colors used by the set_*_1bpp_*() functions
1019 @param fgcolor Foreground color
1020 @param bgcolor Background color
1021 @param mode Draw Mode
1022
1023 See @ref set_1bpp_colors for details.
1024 */
1025 void set_1bpp_colors_ex(uint8_t fgcolor, uint8_t bgcolor, uint8_t mode) OLDCALL;
1026
1027 /** Sets the Foreground and Background colors used by the set_*_1bpp_*() functions
1028 @param fgcolor Foreground color to use
1029 @param bgcolor Background color to use
1030
1031 The default colors are:
1032 \li Foreground: DMG_BLACK
1033 \li Background: DMG_WHITE
1034
1035 Example:
1036 \code{.c}
1037 // Use DMG_BLACK as the Foreground color and DMG_LITE_GRAY
1038 // as the Background color when loading 1bpp tile data.
1039 set_1bpp_colors(DMG_BLACK, DMG_LITE_GRAY);
1040 \endcode
1041
1042
1043 @see DMG_BLACK, DMG_DARK_GRAY, DMG_LITE_GRAY, DMG_WHITE
1044 @see set_bkg_1bpp_data, set_win_1bpp_data, set_sprite_1bpp_data
1045 */
1046 inline void set_1bpp_colors(uint8_t fgcolor, uint8_t bgcolor) {
1047 set_1bpp_colors_ex(fgcolor, bgcolor, 0);
1048 }
1049
1050 /** Sets VRAM Tile Pattern data for the Background / Window
1051
1052 @param first_tile Index of the first tile to write
1053 @param nb_tiles Number of tiles to write
1054 @param data Pointer to (2 bpp) source tile data
1055
1056 Writes __nb_tiles__ tiles to VRAM starting at __first_tile__, tile data
1057 is sourced from __data__. Each Tile is 16 bytes in size (8x8 pixels, 2 bits-per-pixel).
1058
1059 @note Sprite Tiles 128-255 share the same memory region as Background Tiles 128-255.
1060
1061 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
1062 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
1063 \li VBK_REG = @ref VBK_BANK_1 indicates the second
1064
1065 @see set_win_data, set_tile_data
1066 */
1067 void set_bkg_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1068 #define set_bkg_2bpp_data set_bkg_data
1069
1070 /** Sets VRAM Tile Pattern data for the Background / Window using 1bpp source data
1071
1072 @param first_tile Index of the first Tile to write
1073 @param nb_tiles Number of Tiles to write
1074 @param data Pointer to (1bpp) source Tile Pattern data
1075
1076 Similar to @ref set_bkg_data, except source data is 1 bit-per-pixel
1077 which gets expanded into 2 bits-per-pixel.
1078
1079 For a given bit that represent a pixel:
1080 \li 0 will be expanded into the Background color
1081 \li 1 will be expanded into the Foreground color
1082
1083 See @ref set_1bpp_colors for details about setting the Foreground and Background colors.
1084
1085 @see SHOW_BKG, HIDE_BKG, set_bkg_tiles
1086 @see set_win_1bpp_data, set_sprite_1bpp_data
1087 */
1088 void set_bkg_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1089
1090 /** Copies from Background / Window VRAM Tile Pattern data into a buffer
1091
1092 @param first_tile Index of the first Tile to read from
1093 @param nb_tiles Number of Tiles to read
1094 @param data Pointer to destination buffer for Tile Pattern data
1095
1096 @note In general **avoid reading from VRAM**
1097 since that memory is not accessible at all times.
1098 It is also not supported by GBDK on the NES platform.
1099 See @ref best_practice_dont_read_vram "coding guidelines"
1100 for more details.
1101
1102 Copies __nb_tiles__ tiles from VRAM starting at __first_tile__, Tile data
1103 is copied into __data__.
1104
1105 Each Tile is 16 bytes, so the buffer pointed to by __data__
1106 should be at least __nb_tiles__ x 16 bytes in size.
1107
1108 @see get_win_data, get_data
1109 */
1110 void get_bkg_data(uint8_t first_tile, uint8_t nb_tiles, uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1111
1112
1113 /** Sets a rectangular region of Background Tile Map.
1114
1115 @param x X Start position in Background Map tile coordinates. Range 0 - 31
1116 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
1117 @param w Width of area to set in tiles. Range 1 - 32
1118 @param h Height of area to set in tiles. Range 1 - 32
1119 @param tiles Pointer to source tile map data
1120
1121 Entries are copied from map at __tiles__ to the Background Tile Map starting at
1122 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
1123
1124 Use @ref set_bkg_submap() instead when:
1125 \li Source map is wider than 32 tiles.
1126 \li Writing a width that does not match the source map width __and__ more
1127 than one row high at a time.
1128
1129 One byte per source tile map entry.
1130
1131 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1132 the Left and Top edges.
1133
1134 @note Patterns 128-255 overlap with patterns 128-255 of the sprite Tile Pattern table.
1135
1136 GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
1137 \li VBK_REG = @ref VBK_TILES Tile Numbers are written
1138 \li VBK_REG = @ref VBK_ATTRIBUTES Tile Attributes are written
1139
1140 GBC Tile Attributes are defined as:
1141 \li Bit 7 - Priority flag. When this is set, it puts the tile above the sprites
1142 with colour 0 being transparent.
1143 \n 0: Below sprites
1144 \n 1: Above sprites
1145 \n Note: @ref SHOW_BKG needs to be set for these priorities to take place.
1146 \li Bit 6 - Vertical flip. Dictates which way up the tile is drawn vertically.
1147 \n 0: Normal
1148 \n 1: Flipped Vertically
1149 \li Bit 5 - Horizontal flip. Dictates which way up the tile is drawn horizontally.
1150 \n 0: Normal
1151 \n 1: Flipped Horizontally
1152 \li Bit 4 - Not used
1153 \li Bit 3 - Character Bank specification. Dictates from which bank of
1154 Background Tile Patterns the tile is taken.
1155 \n 0: Bank 0
1156 \n 1: Bank 1
1157 \li Bit 2 - See bit 0.
1158 \li Bit 1 - See bit 0.
1159 \li Bit 0 - Bits 0-2 indicate which of the 7 BKG colour palettes the tile is
1160 assigned.
1161
1162 @see SHOW_BKG
1163 @see set_bkg_data, set_bkg_submap, set_win_tiles, set_tiles
1164 */
1165 void set_bkg_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
1166 #define set_tile_map set_bkg_tiles
1167
1168
1169 extern uint8_t _map_tile_offset;
1170
1171 /** Sets a rectangular region of Background Tile Map.
1172 The offset value in __base_tile__ is added to
1173 the tile ID for each map entry.
1174
1175 @param x X Start position in Background Map tile coordinates. Range 0 - 31
1176 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
1177 @param w Width of area to set in tiles. Range 1 - 32
1178 @param h Height of area to set in tiles. Range 1 - 32
1179 @param tiles Pointer to source tile map data
1180 @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
1181
1182 This is identical to @ref set_bkg_tiles() except that it
1183 adds the __base_tile__ parameter for when a tile map's tiles don't
1184 start at index zero. (For example, the tiles used by the map
1185 range from 100 -> 120 in VRAM instead of 0 -> 20).
1186
1187 @see set_bkg_tiles for more details
1188 */
1189 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) {
1190 _map_tile_offset = base_tile;
1191 set_bkg_tiles(x, y, w, h, tiles);
1192 _map_tile_offset = 0;
1193 }
1194
1195
1196 /** Sets a rectangular region of Background Tile Map Attributes.
1197
1198 @param x X Start position in Background Map tile coordinates. Range 0 - 31
1199 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
1200 @param w Width of area to set in tiles. Range 1 - 32
1201 @param h Height of area to set in tiles. Range 1 - 32
1202 @param tiles Pointer to source tile map attribute data
1203
1204 Entries are copied from map at __tiles__ to the Background Tile Map starting at
1205 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
1206
1207 Use @ref set_bkg_submap_attributes() instead when:
1208 \li Source map is wider than 32 tiles.
1209 \li Writing a width that does not match the source map width __and__ more
1210 than one row high at a time.
1211
1212 One byte per source tile map entry.
1213
1214 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1215 the Left and Top edges.
1216
1217 GBC Tile Attributes are defined as:
1218 \li Bit 7 - Priority flag. When this is set, it puts the tile above the sprites
1219 with colour 0 being transparent.
1220 \n 0: Below sprites
1221 \n 1: Above sprites
1222 \n Note: @ref SHOW_BKG needs to be set for these priorities to take place.
1223 \li Bit 6 - Vertical flip. Dictates which way up the tile is drawn vertically.
1224 \n 0: Normal
1225 \n 1: Flipped Vertically
1226 \li Bit 5 - Horizontal flip. Dictates which way up the tile is drawn horizontally.
1227 \n 0: Normal
1228 \n 1: Flipped Horizontally
1229 \li Bit 4 - Not used
1230 \li Bit 3 - Character Bank specification. Dictates from which bank of
1231 Background Tile Patterns the tile is taken.
1232 \n 0: Bank 0
1233 \n 1: Bank 1
1234 \li Bit 2 - See bit 0.
1235 \li Bit 1 - See bit 0.
1236 \li Bit 0 - Bits 0-2 indicate which of the 7 BKG colour palettes the tile is
1237 assigned.
1238
1239 @see SHOW_BKG
1240 @see set_bkg_data, set_bkg_submap_attributes, set_win_tiles, set_tiles
1241
1242 @note On the Game Boy this is only usable in Game Boy Color mode
1243 */
1244 inline void set_bkg_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles)
1245 {
1246 VBK_REG = VBK_ATTRIBUTES;
1247 set_bkg_tiles(x, y, w, h, tiles);
1248 VBK_REG = VBK_TILES;
1249 }
1250
1251
1252 /** Sets a rectangular area of the Background Tile Map using a sub-region
1253 from a source tile map. Useful for scrolling implementations of maps
1254 larger than 32 x 32 tiles.
1255
1256 @param x X Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255
1257 @param y Y Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255
1258 @param w Width of area to set in tiles. Range 1 - 255
1259 @param h Height of area to set in tiles. Range 1 - 255
1260 @param map Pointer to source tile map data
1261 @param map_w Width of source tile map in tiles. Range 1 - 255
1262
1263 Entries are copied from __map__ to the Background Tile Map starting at
1264 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles,
1265 using __map_w__ as the rowstride for the source tile map.
1266
1267 The __x__ and __y__ parameters are in Source Tile Map tile
1268 coordinates. The location tiles will be written to on the
1269 hardware Background Map is derived from those, but only uses
1270 the lower 5 bits of each axis, for range of 0-31 (they are
1271 bit-masked: `x & 0x1F` and `y & 0x1F`). As a result the two
1272 coordinate systems are aligned together.
1273
1274 In order to transfer tile map data in a way where the
1275 coordinate systems are not aligned, an offset from the
1276 Source Tile Map pointer can be passed in:
1277 `(map_ptr + x + (y * map_width))`.
1278
1279 For example, if you want the tile id at `1,2` from the source map to
1280 show up at `0,0` on the hardware Background Map (instead of at `1,2`)
1281 then modify the pointer address that is passed in:
1282 `map_ptr + 1 + (2 * map_width)`
1283
1284 Use this instead of @ref set_bkg_tiles when the source map is wider than
1285 32 tiles or when writing a width that does not match the source map width.
1286
1287 One byte per source tile map entry.
1288
1289 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1290 the Left and Top edges.
1291
1292 See @ref set_bkg_tiles for setting CGB attribute maps with @ref VBK_REG.
1293
1294 @see SHOW_BKG
1295 @see set_bkg_data, set_bkg_tiles, set_win_submap, set_tiles
1296 */
1297 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;
1298 #define set_tile_submap set_bkg_submap
1299
1300
1301 extern uint8_t _submap_tile_offset;
1302
1303 /** Sets a rectangular area of the Background Tile Map using a sub-region
1304 from a source tile map. The offset value in __base_tile__ is added to
1305 the tile ID for each map entry.
1306
1307 @param x X Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255
1308 @param y Y Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255
1309 @param w Width of area to set in tiles. Range 1 - 255
1310 @param h Height of area to set in tiles. Range 1 - 255
1311 @param map Pointer to source tile map data
1312 @param map_w Width of source tile map in tiles. Range 1 - 255
1313 @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
1314
1315 This is identical to @ref set_bkg_submap() except that it
1316 adds the __base_tile__ parameter for when a tile map's tiles don't
1317 start at index zero. (For example, the tiles used by the map
1318 range from 100 -> 120 in VRAM instead of 0 -> 20).
1319
1320 @see set_bkg_submap for more details
1321 */
1322 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) {
1323 _submap_tile_offset = base_tile;
1324 set_bkg_submap(x, y, w, h, map, map_w);
1325 _submap_tile_offset = 0;
1326 }
1327
1328
1329 /** Sets a rectangular area of the Background Tile Map Attributes using a sub-region
1330 from a source tile attribute map. Useful for scrolling implementations of maps
1331 larger than 32 x 32 tiles.
1332
1333 @param x X Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255
1334 @param y Y Start position in both the Source Tile Map and hardware Background Map tile coordinates. Range 0 - 255
1335 @param w Width of area to set in tiles. Range 1 - 255
1336 @param h Height of area to set in tiles. Range 1 - 255
1337 @param map Pointer to source tile map attribute data
1338 @param map_w Width of source tile map in tiles. Range 1 - 255
1339
1340 Entries are copied from __map__ to the Background Tile Map starting at
1341 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles,
1342 using __map_w__ as the rowstride for the source tile map.
1343
1344 The __x__ and __y__ parameters are in Source Tile Map tile
1345 coordinates. The location tiles will be written to on the
1346 hardware Background Map is derived from those, but only uses
1347 the lower 5 bits of each axis, for range of 0-31 (they are
1348 bit-masked: `x & 0x1F` and `y & 0x1F`). As a result the two
1349 coordinate systems are aligned together.
1350
1351 In order to transfer tile map data in a way where the
1352 coordinate systems are not aligned, an offset from the
1353 Source Tile Map pointer can be passed in:
1354 `(map_ptr + x + (y * map_width))`.
1355
1356 For example, if you want the tile id at `1,2` from the source map to
1357 show up at `0,0` on the hardware Background Map (instead of at `1,2`)
1358 then modify the pointer address that is passed in:
1359 `map_ptr + 1 + (2 * map_width)`
1360
1361 Use this instead of @ref set_bkg_tiles when the source map is wider than
1362 32 tiles or when writing a width that does not match the source map width.
1363
1364 One byte per source tile map entry.
1365
1366 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1367 the Left and Top edges.
1368
1369 See @ref set_bkg_tiles for setting CGB attribute maps with @ref VBK_REG.
1370
1371 @see SHOW_BKG
1372 @see set_bkg_data, set_bkg_attributes, set_win_submap, set_tiles
1373
1374 @note On the Game Boy this is only usable in Game Boy Color mode
1375 */
1376 inline void set_bkg_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w)
1377 {
1378 VBK_REG = VBK_ATTRIBUTES;
1379 set_bkg_submap(x, y, w, h, map, map_w);
1380 VBK_REG = VBK_TILES;
1381 }
1382
1383
1384 /** Copies a rectangular region of Background Tile Map entries into a buffer.
1385
1386 @param x X Start position in Background Map tile coordinates. Range 0 - 31
1387 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
1388 @param w Width of area to copy in tiles. Range 0 - 31
1389 @param h Height of area to copy in tiles. Range 0 - 31
1390 @param tiles Pointer to destination buffer for Tile Map data
1391
1392 @note In general **avoid reading from VRAM**
1393 since that memory is not accessible at all times.
1394 It is also not supported by GBDK on the NES platform.
1395 See @ref best_practice_dont_read_vram "coding guidelines"
1396 for more details.
1397
1398 Entries are copied into __tiles__ from the Background Tile Map starting at
1399 __x__, __y__ reading across for __w__ tiles and down for __h__ tiles.
1400
1401 One byte per tile.
1402
1403 The buffer pointed to by __tiles__ should be at least __x__ x __y__ bytes in size.
1404
1405 @see get_win_tiles, get_bkg_tile_xy, get_tiles, get_vram_byte
1406 */
1407 void get_bkg_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
1408
1409
1410 /** Set single tile t on background layer at x,y
1411 @param x X-coordinate
1412 @param y Y-coordinate
1413 @param t tile index
1414
1415 @return returns the address of tile, so you may use faster set_vram_byte() later
1416 */
1417 uint8_t * set_bkg_tile_xy(uint8_t x, uint8_t y, uint8_t t);
1418 #define set_tile_xy set_bkg_tile_xy
1419
1420 /** Set single tile attribute a on background layer at x,y
1421 @param x X-coordinate
1422 @param y Y-coordinate
1423 @param a tile attributes
1424 @return returns the address of tile attribute, so you may use faster set_vram_byte() later
1425
1426 @note On the Game Boy this is only usable in Game Boy Color mode
1427 */
1428 inline uint8_t * set_bkg_attribute_xy(uint8_t x, uint8_t y, uint8_t a)
1429 {
1430 uint8_t* addr;
1431 VBK_REG = VBK_ATTRIBUTES;
1432 addr = set_bkg_tile_xy(x, y, a);
1433 VBK_REG = VBK_TILES;
1434 return addr;
1435 }
1436 #define set_attribute_xy set_bkg_attribute_xy
1437
1438 /**
1439 Get single tile t on background layer at x,y
1440 @param x X-coordinate
1441 @param y Y-coordinate
1442
1443 @return returns tile index
1444
1445 @note In general **avoid reading from VRAM**
1446 since that memory is not accessible at all times.
1447 It is also not supported by GBDK on the NES platform.
1448 See @ref best_practice_dont_read_vram "coding guidelines"
1449 for more details.
1450
1451 */
1452 uint8_t get_bkg_tile_xy(uint8_t x, uint8_t y) OLDCALL PRESERVES_REGS(b, c);
1453
1454
1455 /** Moves the Background Layer to the position specified in __x__ and __y__ in pixels.
1456
1457 @param x X axis screen coordinate for Left edge of the Background
1458 @param y Y axis screen coordinate for Top edge of the Background
1459
1460 0,0 is the top left corner of the GB screen. The Background Layer wraps around the screen,
1461 so when part of it goes off the screen it appears on the opposite side (factoring in the
1462 larger size of the Background Layer versus the screen size).
1463
1464 The background layer is always under the Window Layer.
1465
1466 @see SHOW_BKG, HIDE_BKG
1467 */
1468 inline void move_bkg(uint8_t x, uint8_t y) {
1469 SCX_REG=x, SCY_REG=y;
1470 }
1471
1472
1473 /** Moves the Background relative to it's current position.
1474
1475 @param x Number of pixels to move the Background on the __X axis__
1476 \n Range: -128 - 127
1477 @param y Number of pixels to move the Background on the __Y axis__
1478 \n Range: -128 - 127
1479
1480 @see move_bkg
1481 */
1482 inline void scroll_bkg(int8_t x, int8_t y) {
1483 SCX_REG+=x, SCY_REG+=y;
1484 }
1485
1486
1487
1488 /**
1489 * Get address of X,Y tile of window map
1490 */
1491 uint8_t * get_win_xy_addr(uint8_t x, uint8_t y) PRESERVES_REGS(h, l);
1492
1493 /** Sets VRAM Tile Pattern data for the Window / Background
1494
1495 @param first_tile Index of the first tile to write
1496 @param nb_tiles Number of tiles to write
1497 @param data Pointer to (2 bpp) source Tile Pattern data.
1498
1499 This is the same as @ref set_bkg_data, since the Window Layer and
1500 Background Layer share the same Tile pattern data.
1501
1502 @see set_bkg_data
1503 @see set_win_tiles, set_bkg_data, set_data
1504 @see SHOW_WIN, HIDE_WIN
1505 */
1506 void set_win_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1507
1508
1509 /** Sets VRAM Tile Pattern data for the Window / Background using 1bpp source data
1510
1511 @param first_tile Index of the first tile to write
1512 @param nb_tiles Number of tiles to write
1513 @param data Pointer to (1bpp) source Tile Pattern data
1514
1515 This is the same as @ref set_bkg_1bpp_data, since the Window Layer and
1516 Background Layer share the same Tile pattern data.
1517
1518 For a given bit that represent a pixel:
1519 \li 0 will be expanded into the Background color
1520 \li 1 will be expanded into the Foreground color
1521
1522 See @ref set_1bpp_colors for details about setting the Foreground and Background colors.
1523
1524 @see set_bkg_data, set_win_data, set_1bpp_colors
1525 @see set_bkg_1bpp_data, set_sprite_1bpp_data
1526 */
1527 void set_win_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1528
1529
1530 /** Copies from Window / Background VRAM Tile Pattern data into a buffer
1531
1532 @param first_tile Index of the first Tile to read from
1533 @param nb_tiles Number of Tiles to read
1534 @param data Pointer to destination buffer for Tile Pattern Data
1535
1536 @note In general **avoid reading from VRAM**
1537 since that memory is not accessible at all times.
1538 It is also not supported by GBDK on the NES platform.
1539 See @ref best_practice_dont_read_vram "coding guidelines"
1540 for more details.
1541
1542 This is the same as @ref get_bkg_data, since the Window Layer and
1543 Background Layer share the same Tile pattern data.
1544
1545 @see get_bkg_data, get_data
1546 */
1547 void get_win_data(uint8_t first_tile, uint8_t nb_tiles, uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1548
1549
1550 /** Sets a rectangular region of the Window Tile Map.
1551
1552 @param x X Start position in Window Map tile coordinates. Range 0 - 31
1553 @param y Y Start position in Window Map tile coordinates. Range 0 - 31
1554 @param w Width of area to set in tiles. Range 1 - 32
1555 @param h Height of area to set in tiles. Range 1 - 32
1556 @param tiles Pointer to source tile map data
1557
1558 Entries are copied from map at __tiles__ to the Window Tile Map starting at
1559 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
1560
1561 Use @ref set_win_submap() instead when:
1562 \li Source map is wider than 32 tiles.
1563 \li Writing a width that does not match the source map width __and__ more
1564 than one row high at a time.
1565
1566 One byte per source tile map entry.
1567
1568 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1569 the Left and Top edges.
1570
1571 @note Patterns 128-255 overlap with patterns 128-255 of the sprite Tile Pattern table.
1572
1573 GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
1574 \li VBK_REG = @ref VBK_TILES Tile Numbers are written
1575 \li VBK_REG = @ref VBK_ATTRIBUTES Tile Attributes are written
1576
1577 For more details about GBC Tile Attributes see @ref set_bkg_tiles.
1578
1579 @see SHOW_WIN, HIDE_WIN, set_win_submap, set_bkg_tiles, set_bkg_data, set_tiles
1580 */
1581 void set_win_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
1582
1583
1584 /** Sets a rectangular region of the Window Tile Map.
1585 The offset value in __base_tile__ is added to
1586 the tile ID for each map entry.
1587
1588 @param x X Start position in Window Map tile coordinates. Range 0 - 31
1589 @param y Y Start position in Window Map tile coordinates. Range 0 - 31
1590 @param w Width of area to set in tiles. Range 1 - 32
1591 @param h Height of area to set in tiles. Range 1 - 32
1592 @param tiles Pointer to source tile map data
1593 @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
1594
1595 This is identical to @ref set_win_tiles() except that it
1596 adds the __base_tile__ parameter for when a tile map's tiles don't
1597 start at index zero. (For example, the tiles used by the map
1598 range from 100 -> 120 in VRAM instead of 0 -> 20).
1599
1600 @see set_win_tiles for more details
1601 */
1602 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) {
1603 _map_tile_offset = base_tile;
1604 set_win_tiles(x, y, w, h, tiles);
1605 _map_tile_offset = 0;
1606 }
1607
1608 /** Sets a rectangular area of the Window Tile Map using a sub-region
1609 from a source tile map.
1610
1611 @param x X Start position in both the Source Tile Map and hardware Window Map tile coordinates. Range 0 - 255
1612 @param y Y Start position in both the Source Tile Map and hardware Window Map tile coordinates. Range 0 - 255
1613 @param w Width of area to set in tiles. Range 1 - 255
1614 @param h Height of area to set in tiles. Range 1 - 255
1615 @param map Pointer to source tile map data
1616 @param map_w Width of source tile map in tiles. Range 1 - 255
1617
1618 Entries are copied from __map__ to the Window Tile Map starting at
1619 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles,
1620 using __map_w__ as the rowstride for the source tile map.
1621
1622 The __x__ and __y__ parameters are in Source Tile Map tile
1623 coordinates. The location tiles will be written to on the
1624 hardware Background Map is derived from those, but only uses
1625 the lower 5 bits of each axis, for range of 0-31 (they are
1626 bit-masked: `x & 0x1F` and `y & 0x1F`). As a result the two
1627 coordinate systems are aligned together.
1628
1629 In order to transfer tile map data in a way where the
1630 coordinate systems are not aligned, an offset from the
1631 Source Tile Map pointer can be passed in:
1632 `(map_ptr + x + (y * map_width))`.
1633
1634 For example, if you want the tile id at `1,2` from the source map to
1635 show up at `0,0` on the hardware Background Map (instead of at `1,2`)
1636 then modify the pointer address that is passed in:
1637 `map_ptr + 1 + (2 * map_width)`
1638
1639 Use this instead of @ref set_win_tiles when the source map is wider than
1640 32 tiles or when writing a width that does not match the source map width.
1641
1642 One byte per source tile map entry.
1643
1644 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1645 the Left and Top edges.
1646
1647 GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
1648 \li VBK_REG = @ref VBK_TILES Tile Numbers are written
1649 \li VBK_REG = @ref VBK_ATTRIBUTES Tile Attributes are written
1650
1651 See @ref set_bkg_tiles for details about CGB attribute maps with @ref VBK_REG.
1652
1653 @see SHOW_WIN, HIDE_WIN, set_win_tiles, set_bkg_submap, set_bkg_tiles, set_bkg_data, set_tiles
1654 **/
1655 void set_win_submap(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w) OLDCALL;
1656
1657
1658 /** Sets a rectangular area of the Window Tile Map Attributes using a sub-region
1659 from a source tile attribute map. Useful for scrolling implementations of maps
1660 larger than 32 x 32 tiles.
1661
1662 @param x X Start position in both the Source Tile Map and hardware Window Map tile coordinates. Range 0 - 255
1663 @param y Y Start position in both the Source Tile Map and hardware Window Map tile coordinates. Range 0 - 255
1664 @param w Width of area to set in tiles. Range 1 - 255
1665 @param h Height of area to set in tiles. Range 1 - 255
1666 @param map Pointer to source tile map attribute data
1667 @param map_w Width of source tile map in tiles. Range 1 - 255
1668
1669 Entries are copied from __map__ to the Window Tile Map starting at
1670 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles,
1671 using __map_w__ as the rowstride for the source tile map.
1672
1673 The __x__ and __y__ parameters are in Source Tile Map tile
1674 coordinates. The location tiles will be written to on the
1675 hardware Window Map is derived from those, but only uses
1676 the lower 5 bits of each axis, for range of 0-31 (they are
1677 bit-masked: `x & 0x1F` and `y & 0x1F`). As a result the two
1678 coordinate systems are aligned together.
1679
1680 In order to transfer tile map data in a way where the
1681 coordinate systems are not aligned, an offset from the
1682 Source Tile Map pointer can be passed in:
1683 `(map_ptr + x + (y * map_width))`.
1684
1685 For example, if you want the tile id at `1,2` from the source map to
1686 show up at `0,0` on the hardware Window Map (instead of at `1,2`)
1687 then modify the pointer address that is passed in:
1688 `map_ptr + 1 + (2 * map_width)`
1689
1690 Use this instead of @ref set_win_tiles when the source map is wider than
1691 32 tiles or when writing a width that does not match the source map width.
1692
1693 One byte per source tile map entry.
1694
1695 Writes that exceed coordinate 31 on the x or y axis will wrap around to
1696 the Left and Top edges.
1697
1698 See @ref set_win_tiles for setting CGB attribute maps with @ref VBK_REG.
1699
1700 @see SHOW_BKG
1701 @see set_win_data, set_win_attributes, set_bkg_submap, set_tiles
1702
1703 @note On the Game Boy this is only usable in Game Boy Color mode
1704 */
1705 inline void set_win_submap_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t *map, uint8_t map_w)
1706 {
1707 VBK_REG = VBK_ATTRIBUTES;
1708 set_win_submap(x, y, w, h, map, map_w);
1709 VBK_REG = VBK_TILES;
1710 }
1711
1712
1713 /** Sets a rectangular area of the Window Tile Map using a sub-region
1714 from a source tile map. The offset value in __base_tile__ is added
1715 to the tile ID for each map entry.
1716
1717 @param x X Start position in both the Source Tile Map and hardware Window Map tile coordinates. Range 0 - 255
1718 @param y Y Start position in both the Source Tile Map and hardware Window Map tile coordinates. Range 0 - 255
1719 @param w Width of area to set in tiles. Range 1 - 255
1720 @param h Height of area to set in tiles. Range 1 - 255
1721 @param map Pointer to source tile map data
1722 @param map_w Width of source tile map in tiles. Range 1 - 255
1723 @param base_tile Offset each tile ID entry of the source map by this value. Range 1 - 255
1724
1725 This is identical to @ref set_win_submap() except that it
1726 adds the __base_tile__ parameter for when a tile map's tiles don't
1727 start at index zero. (For example, the tiles used by the map
1728 range from 100 -> 120 in VRAM instead of 0 -> 20).
1729
1730 @see set_win_submap for more details
1731 **/
1732 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) {
1733 _submap_tile_offset = base_tile;
1734 set_win_submap(x, y, w, h, map, map_w);
1735 _submap_tile_offset = 0;
1736 }
1737
1738
1739 /** Copies a rectangular region of Window Tile Map entries into a buffer.
1740
1741 @param x X Start position in Window Map tile coordinates. Range 0 - 31
1742 @param y Y Start position in Window Map tile coordinates. Range 0 - 31
1743 @param w Width of area to copy in tiles. Range 0 - 31
1744 @param h Height of area to copy in tiles. Range 0 - 31
1745 @param tiles Pointer to destination buffer for Tile Map data
1746
1747 @note In general **avoid reading from VRAM**
1748 since that memory is not accessible at all times.
1749 It is also not supported by GBDK on the NES platform.
1750 See @ref best_practice_dont_read_vram "coding guidelines"
1751 for more details.
1752
1753 Entries are copied into __tiles__ from the Window Tile Map starting at
1754 __x__, __y__ reading across for __w__ tiles and down for __h__ tiles.
1755
1756 One byte per tile.
1757
1758 The buffer pointed to by __tiles__ should be at least __x__ x __y__ bytes in size.
1759
1760 @see get_bkg_tiles, get_bkg_tile_xy, get_tiles, get_vram_byte
1761 */
1762 void get_win_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *tiles) OLDCALL PRESERVES_REGS(b, c);
1763
1764
1765 /**
1766 * Set single tile t on window layer at x,y
1767 * @param x X-coordinate
1768 * @param y Y-coordinate
1769 * @param t tile index
1770 * @return returns the address of tile, so you may use faster set_vram_byte() later
1771 */
1772 uint8_t * set_win_tile_xy(uint8_t x, uint8_t y, uint8_t t);
1773
1774 /** Set single tile attribute a on window layer at x,y
1775 @param x X-coordinate
1776 @param y Y-coordinate
1777 @param a tile attributes
1778 @return returns the address of tile attribute, so you may use faster set_vram_byte() later
1779
1780 @note On the Game Boy this is only usable in Game Boy Color mode
1781 */
1782 inline uint8_t * set_win_attribute_xy(uint8_t x, uint8_t y, uint8_t a)
1783 {
1784 uint8_t* addr;
1785 VBK_REG = VBK_ATTRIBUTES;
1786 addr = set_win_tile_xy(x, y, a);
1787 VBK_REG = VBK_TILES;
1788 return addr;
1789 }
1790
1791 /**
1792 * Get single tile t on window layer at x,y
1793 * @param x X-coordinate
1794 * @param y Y-coordinate
1795 * @return returns the tile index
1796
1797 @note In general **avoid reading from VRAM**
1798 since that memory is not accessible at all times.
1799 It is also not supported by GBDK on the NES platform.
1800 See @ref best_practice_dont_read_vram "coding guidelines"
1801 for more details.
1802 */
1803 uint8_t get_win_tile_xy(uint8_t x, uint8_t y) OLDCALL PRESERVES_REGS(b, c);
1804
1805
1806 /** Moves the Window to the __x__, __y__ position on the screen.
1807
1808 @param x X coordinate for Left edge of the Window (actual displayed location will be X - 7)
1809 @param y Y coordinate for Top edge of the Window
1810
1811 7,0 is the top left corner of the screen in Window coordinates. The Window is locked to the bottom right corner.
1812
1813 The Window is always over the Background layer.
1814
1815 @see SHOW_WIN, HIDE_WIN
1816 */
1817 inline void move_win(uint8_t x, uint8_t y) {
1818 WX_REG=x, WY_REG=y;
1819 }
1820
1821
1822 /** Move the Window relative to its current position.
1823
1824 @param x Number of pixels to move the window on the __X axis__
1825 \n Range: -128 - 127
1826 @param y Number of pixels to move the window on the __Y axis__
1827 \n Range: -128 - 127
1828
1829 @see move_win
1830 */
1831 inline void scroll_win(int8_t x, int8_t y) {
1832 WX_REG+=x, WY_REG+=y;
1833 }
1834
1835
1836
1837 /** Sets VRAM Tile Pattern data for Sprites
1838
1839 @param first_tile Index of the first tile to write
1840 @param nb_tiles Number of tiles to write
1841 @param data Pointer to (2 bpp) source Tile Pattern data
1842
1843 Writes __nb_tiles__ tiles to VRAM starting at __first_tile__, tile data
1844 is sourced from __data__. Each Tile is 16 bytes in size (8x8 pixels, 2 bits-per-pixel).
1845
1846 @note Sprite Tiles 128-255 share the same memory region as Background Tiles 128-255.
1847
1848 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
1849 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
1850 \li VBK_REG = @ref VBK_BANK_1 indicates the second
1851 */
1852 void set_sprite_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1853 #define set_sprite_2bpp_data set_sprite_data
1854
1855 /** Sets VRAM Tile Pattern data for Sprites using 1bpp source data
1856
1857 @param first_tile Index of the first tile to write
1858 @param nb_tiles Number of tiles to write
1859 @param data Pointer to (1bpp) source Tile Pattern data
1860
1861 Similar to @ref set_sprite_data, except source data is 1 bit-per-pixel
1862 which gets expanded into 2 bits-per-pixel.
1863
1864 For a given bit that represent a pixel:
1865 \li 0 will be expanded into the Background color
1866 \li 1 will be expanded into the Foreground color
1867
1868 See @ref set_1bpp_colors for details about setting the Foreground and Background colors.
1869
1870 @see SHOW_SPRITES, HIDE_SPRITES, set_sprite_tile
1871 @see set_bkg_1bpp_data, set_win_1bpp_data
1872 */
1873 void set_sprite_1bpp_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1874
1875 /** Copies from Sprite VRAM Tile Pattern data into a buffer
1876
1877 @param first_tile Index of the first tile to read from
1878 @param nb_tiles Number of tiles to read
1879 @param data Pointer to destination buffer for Tile Pattern data
1880
1881 @note In general **avoid reading from VRAM**
1882 since that memory is not accessible at all times.
1883 It is also not supported by GBDK on the NES platform.
1884 See @ref best_practice_dont_read_vram "coding guidelines"
1885 for more details.
1886
1887 Copies __nb_tiles__ tiles from VRAM starting at __first_tile__, tile data
1888 is copied into __data__.
1889
1890 Each Tile is 16 bytes, so the buffer pointed to by __data__
1891 should be at least __nb_tiles__ x 16 bytes in size.
1892 */
1893 void get_sprite_data(uint8_t first_tile, uint8_t nb_tiles, uint8_t *data) OLDCALL PRESERVES_REGS(b, c);
1894
1895
1896 /** Sprite Attributes structure
1897 @param x X Coordinate of the sprite on screen
1898 @param y Y Coordinate of the sprite on screen
1899 @param tile Sprite tile number (see @ref set_sprite_tile)
1900 @param prop OAM Property Flags (see @ref set_sprite_prop)
1901 */
1902 typedef struct OAM_item_t {
1903 uint8_t y, x; //< X, Y Coordinates of the sprite on screen
1904 uint8_t tile; //< Sprite tile number
1905 uint8_t prop; //< OAM Property Flags
1906 } OAM_item_t;
1907
1908
1909 /** Shadow OAM array in WRAM, that is DMA-transferred into the real OAM each VBlank
1910 */
1911 extern volatile struct OAM_item_t shadow_OAM[];
1912
1913 /** MSB of shadow_OAM address is used by OAM DMA copying routine
1914 */
1915 __REG _shadow_OAM_base;
1916
1917 #define DISABLE_OAM_DMA \
1918 _shadow_OAM_base = 0
1919
1920 /** Disable OAM DMA copy each VBlank
1921 */
1922 #define DISABLE_VBL_TRANSFER DISABLE_OAM_DMA
1923
1924 #define ENABLE_OAM_DMA \
1925 _shadow_OAM_base = (uint8_t)((uint16_t)&shadow_OAM >> 8)
1926
1927 /** Enable OAM DMA copy each VBlank and set it to transfer default shadow_OAM array
1928 */
1929 #define ENABLE_VBL_TRANSFER ENABLE_OAM_DMA
1930
1931 /** Amount of hardware sprites in OAM
1932 */
1933 #define MAX_HARDWARE_SPRITES 40
1934
1935 /** True if sprite hardware can flip sprites by X (horizontally)
1936 */
1937 #define HARDWARE_SPRITE_CAN_FLIP_X 1
1938
1939 /** True if sprite hardware can flip sprites by Y (vertically)
1940 */
1941 #define HARDWARE_SPRITE_CAN_FLIP_Y 1
1942
1943 /** Enable OAM DMA copy each VBlank and set it to transfer any 256-byte aligned array
1944 */
1945 inline void SET_SHADOW_OAM_ADDRESS(void * address) {
1946 _shadow_OAM_base = (uint8_t)((uint16_t)address >> 8);
1947 }
1948
1949 /** Sets sprite number __nb__in the OAM to display tile number __tile__.
1950
1951 @param nb Sprite number, range 0 - 39
1952 @param tile Selects a tile (0 - 255) from memory at 8000h - 8FFFh
1953 \n In CGB Mode this could be either in VRAM Bank
1954 \n 0 or 1, depending on Bit 3 of the OAM Attribute Flag
1955 \n (see @ref set_sprite_prop)
1956
1957 In 8x16 mode:
1958 \li The sprite will also display the next tile (__tile__ + 1)
1959 directly below (y + 8) the first tile.
1960 \li The lower bit of the tile number is ignored:
1961 the upper 8x8 tile is (__tile__ & 0xFE), and
1962 the lower 8x8 tile is (__tile__ | 0x01).
1963 \li See: @ref SPRITES_8x16
1964 */
1965 inline void set_sprite_tile(uint8_t nb, uint8_t tile) {
1966 shadow_OAM[nb].tile=tile;
1967 }
1968
1969
1970 /** Returns the tile number of sprite number __nb__ in the OAM.
1971
1972 @param nb Sprite number, range 0 - 39
1973
1974 @see set_sprite_tile for more details
1975 */
1976 inline uint8_t get_sprite_tile(uint8_t nb) {
1977 return shadow_OAM[nb].tile;
1978 }
1979
1980
1981 /** Sets the OAM Property Flags of sprite number __nb__ to those defined in __prop__.
1982
1983 @param nb Sprite number, range 0 - 39
1984 @param prop Property setting (see bitfield description)
1985
1986 The bits in __prop__ represent:
1987 \li Bit 7 - Priority flag. When this is set the sprites appear behind the
1988 background and window layer.
1989 \n 0: infront
1990 \n 1: behind
1991 \li Bit 6 - Vertical flip. Dictates which way up the sprite is drawn
1992 vertically.
1993 \n 0: normal
1994 \n 1:upside down
1995 \li Bit 5 - Horizontal flip. Dictates which way up the sprite is
1996 drawn horizontally.
1997 \n 0: normal
1998 \n 1:back to front
1999 \li Bit 4 - DMG/Non-CGB Mode Only. Assigns either one of the two b/w palettes to the sprite.
2000 \n 0: OBJ palette 0
2001 \n 1: OBJ palette 1
2002 \li Bit 3 - GBC only. Dictates from which bank of Sprite Tile Patterns the tile
2003 is taken.
2004 \n 0: Bank 0
2005 \n 1: Bank 1
2006 \li Bit 2 - See bit 0.
2007 \li Bit 1 - See bit 0.
2008 \li Bit 0 - GBC only. Bits 0-2 indicate which of the 7 OBJ colour palettes the
2009 sprite is assigned.
2010
2011 It's recommended to use GBDK constants (eg: S_FLIPY) to configure sprite properties as these are crossplatform.
2012
2013 \code{.c}
2014 // Load palette data into the first palette
2015 set_sprite_palette(4, 1, exampleSprite_palettes)
2016
2017 // Set the OAM value for the sprite
2018 // These flags tell the sprite to flip both vertically and horizontally.
2019 set_sprite_prop(0, S_FLIPY | S_FLIPX);
2020 \endcode
2021
2022 @see S_PALETTE, S_FLIPX, S_FLIPY, S_PRIORITY
2023 */
2024 inline void set_sprite_prop(uint8_t nb, uint8_t prop) {
2025 shadow_OAM[nb].prop=prop;
2026 }
2027
2028
2029 /** Returns the OAM Property Flags of sprite number __nb__.
2030
2031 @param nb Sprite number, range 0 - 39
2032 @see set_sprite_prop for property bitfield settings
2033 */
2034 inline uint8_t get_sprite_prop(uint8_t nb) {
2035 return shadow_OAM[nb].prop;
2036 }
2037
2038
2039 /** Moves sprite number __nb__ to the __x__, __y__ position on the screen.
2040
2041 @param nb Sprite number, range 0 - 39
2042 @param x X Position. Specifies the sprites horizontal position on the screen (minus 8).
2043 \n An offscreen value (X=0 or X>=168) hides the sprite, but the sprite
2044 still affects the priority ordering - a better way to hide a sprite is to set
2045 its Y-coordinate offscreen.
2046 @param y Y Position. Specifies the sprites vertical position on the screen (minus 16).
2047 \n An offscreen value (for example, Y=0 or Y>=160) hides the sprite.
2048
2049 Moving the sprite to 0,0 (or similar off-screen location) will hide it.
2050 */
2051 inline void move_sprite(uint8_t nb, uint8_t x, uint8_t y) {
2052 OAM_item_t * itm = &shadow_OAM[nb];
2053 itm->y=y, itm->x=x;
2054 }
2055
2056
2057 /** Moves sprite number __nb__ relative to its current position.
2058
2059 @param nb Sprite number, range 0 - 39
2060 @param x Number of pixels to move the sprite on the __X axis__
2061 \n Range: -128 - 127
2062 @param y Number of pixels to move the sprite on the __Y axis__
2063 \n Range: -128 - 127
2064
2065 @see move_sprite for more details about the X and Y position
2066 */
2067 inline void scroll_sprite(uint8_t nb, int8_t x, int8_t y) {
2068 OAM_item_t * itm = &shadow_OAM[nb];
2069 itm->y+=y, itm->x+=x;
2070 }
2071
2072
2073 /** Hides sprite number __nb__ by moving it to zero position by Y.
2074
2075 @param nb Sprite number, range 0 - 39
2076
2077 @see hide_sprites_range, HIDE_SPRITES
2078 */
2079 inline void hide_sprite(uint8_t nb) {
2080 shadow_OAM[nb].y = 0;
2081 }
2082
2083
2084
2085 /** Copies arbitrary data to an address in VRAM
2086 without taking into account the state of LCDC bits 3 or 4.
2087
2088 @param vram_addr Pointer to destination VRAM Address
2089 @param data Pointer to source buffer
2090 @param len Number of bytes to copy
2091
2092 Copies __len__ bytes from a buffer at __data__ to VRAM starting at __vram_addr__.
2093
2094 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
2095 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
2096 \li VBK_REG = @ref VBK_BANK_1 indicates the second
2097
2098 @see set_bkg_data, set_win_data, set_bkg_tiles, set_win_tiles, set_tile_data, set_tiles
2099 */
2100 void set_data(uint8_t *vram_addr, const uint8_t *data, uint16_t len);
2101
2102
2103 /** Copies arbitrary data from an address in VRAM into a buffer
2104 without taking into account the state of LCDC bits 3 or 4.
2105
2106 @param vram_addr Pointer to source VRAM Address
2107 @param data Pointer to destination buffer
2108 @param len Number of bytes to copy
2109
2110 @note In general **avoid reading from VRAM**
2111 since that memory is not accessible at all times.
2112 It is also not supported by GBDK on the NES platform.
2113 See @ref best_practice_dont_read_vram "coding guidelines"
2114 for more details.
2115
2116 Copies __len__ bytes from VRAM starting at __vram_addr__ into a buffer at __data__.
2117
2118 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
2119 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
2120 \li VBK_REG = @ref VBK_BANK_1 indicates the second
2121
2122 @see get_bkg_data, get_win_data, get_bkg_tiles, get_win_tiles, get_tiles
2123 */
2124 void get_data(uint8_t *data, uint8_t *vram_addr, uint16_t len);
2125
2126 /** Copies arbitrary data from an address in VRAM into a buffer
2127
2128 @param dest Pointer to destination buffer (may be in VRAM)
2129 @param sour Pointer to source buffer (may be in VRAM)
2130 @param len Number of bytes to copy
2131
2132 Copies __len__ bytes from or to VRAM starting at __sour__ into a buffer or to VRAM at __dest__.
2133
2134 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
2135 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
2136 \li VBK_REG = @ref VBK_BANK_1 indicates the second
2137 */
2138 void vmemcpy(uint8_t *dest, uint8_t *sour, uint16_t len);
2139
2140
2141
2142 /** Sets a rectangular region of Tile Map entries at a given VRAM Address
2143 without taking into account the state of LCDC bit 3.
2144
2145 @param x X Start position in Map tile coordinates. Range 0 - 31
2146 @param y Y Start position in Map tile coordinates. Range 0 - 31
2147 @param w Width of area to set in tiles. Range 1 - 32
2148 @param h Height of area to set in tiles. Range 1 - 32
2149 @param vram_addr Pointer to destination VRAM Address
2150 @param tiles Pointer to source Tile Map data
2151
2152 Entries are copied from __tiles__ to Tile Map at address vram_addr starting at
2153 __x__, __y__ writing across for __w__ tiles and down for __h__ tiles.
2154
2155 One byte per source tile map entry.
2156
2157 There are two 32x32 Tile Maps in VRAM at addresses 9800h-9BFFh and 9C00h-9FFFh.
2158
2159 GBC only: @ref VBK_REG determines whether Tile Numbers or Tile Attributes get set.
2160 \li VBK_REG = @ref VBK_TILES Tile Numbers are written
2161 \li VBK_REG = @ref VBK_ATTRIBUTES Tile Attributes are written
2162
2163 @see set_bkg_tiles, set_win_tiles
2164 */
2165 void set_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *vram_addr, const uint8_t *tiles) OLDCALL;
2166
2167 /** Sets VRAM Tile Pattern data starting from given base address
2168 without taking into account the state of LCDC bit 4.
2169
2170 @param first_tile Index of the first tile to write
2171 @param nb_tiles Number of tiles to write
2172 @param data Pointer to (2 bpp) source Tile Pattern data.
2173 @param base MSB of the destination address in VRAM (usually 0x80 or 0x90 which gives 0x8000 or 0x9000)
2174
2175 @see set_bkg_data, set_win_data, set_data
2176 */
2177 void set_tile_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data, uint8_t base) OLDCALL PRESERVES_REGS(b, c);
2178
2179 /** Copies a rectangular region of Tile Map entries from a given VRAM Address into a buffer
2180 without taking into account the state of LCDC bit 3.
2181
2182 @param x X Start position in Background Map tile coordinates. Range 0 - 31
2183 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
2184 @param w Width of area to copy in tiles. Range 0 - 31
2185 @param h Height of area to copy in tiles. Range 0 - 31
2186 @param vram_addr Pointer to source VRAM Address
2187 @param tiles Pointer to destination buffer for Tile Map data
2188
2189 @note In general **avoid reading from VRAM**
2190 since that memory is not accessible at all times.
2191 It is also not supported by GBDK on the NES platform.
2192 See @ref best_practice_dont_read_vram "coding guidelines"
2193 for more details.
2194
2195 Entries are copied into __tiles__ from the Background Tile Map starting at
2196 __x__, __y__ reading across for __w__ tiles and down for __h__ tiles.
2197
2198 One byte per tile.
2199
2200 There are two 32x32 Tile Maps in VRAM at addresses 9800h - 9BFFh and 9C00h - 9FFFh.
2201
2202 The buffer pointed to by __tiles__ should be at least __x__ x __y__ bytes in size.
2203
2204 @see get_bkg_tiles, get_win_tiles
2205 */
2206 void get_tiles(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t *vram_addr, uint8_t *tiles) OLDCALL;
2207
2208
2209 /** Sets VRAM Tile Pattern data in the native format
2210
2211 @param first_tile Index of the first tile to write (0 - 511)
2212 @param nb_tiles Number of tiles to write
2213 @param data Pointer to source Tile Pattern data.
2214
2215 When `first_tile` is larger than 256 on the GB/AP, it
2216 will write to sprite data instead of background data.
2217
2218 The bit depth of the source Tile Pattern data depends
2219 on which console is being used:
2220 \li Game Boy/Analogue Pocket: loads 2bpp tiles data
2221 \li SMS/GG: loads 4bpp tile data
2222 */
2223 inline void set_native_tile_data(uint16_t first_tile, uint8_t nb_tiles, const uint8_t *data) {
2224 if (first_tile < 256) {
2225 set_bkg_data(first_tile, nb_tiles, data);
2226 } else {
2227 set_sprite_data(first_tile - 256, nb_tiles, data);
2228 }
2229 }
2230
2231 /** Sets VRAM Tile Pattern data for the Background / Window in the native format
2232
2233 @param first_tile Index of the first tile to write
2234 @param nb_tiles Number of tiles to write
2235 @param data Pointer to source tile data
2236
2237 Writes __nb_tiles__ tiles to VRAM starting at __first_tile__, tile data
2238 is sourced from __data__.
2239
2240 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
2241 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
2242 \li VBK_REG = @ref VBK_BANK_1 indicates the second
2243
2244 @see set_win_data, set_tile_data
2245 */
2246 inline void set_bkg_native_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) {
2247 set_bkg_data(first_tile, nb_tiles, data);
2248 }
2249
2250 /** Sets VRAM Tile Pattern data for Sprites in the native format
2251
2252 @param first_tile Index of the first tile to write
2253 @param nb_tiles Number of tiles to write
2254 @param data Pointer to source tile data
2255
2256 Writes __nb_tiles__ tiles to VRAM starting at __first_tile__, tile data
2257 is sourced from __data__.
2258
2259 GBC only: @ref VBK_REG determines which bank of tile patterns are written to.
2260 \li VBK_REG = @ref VBK_BANK_0 indicates the first bank
2261 \li VBK_REG = @ref VBK_BANK_1 indicates the second
2262 */
2263 inline void set_sprite_native_data(uint8_t first_tile, uint8_t nb_tiles, const uint8_t *data) {
2264 set_sprite_data(first_tile, nb_tiles, data);
2265 }
2266
2267 /** Initializes the entire Window Tile Map with Tile Number __c__
2268 @param c Tile number to fill with
2269
2270 @note This function avoids writes during modes 2 & 3
2271 */
2272 void init_win(uint8_t c) OLDCALL PRESERVES_REGS(b, c);
2273
2274 /** Initializes the entire Background Tile Map with Tile Number __c__
2275 @param c Tile number to fill with
2276
2277 @note This function avoids writes during modes 2 & 3
2278 */
2279 void init_bkg(uint8_t c) OLDCALL PRESERVES_REGS(b, c);
2280
2281 /** Fills the VRAM memory region __s__ of size __n__ with Tile Number __c__
2282 @param s Start address in VRAM
2283 @param c Tile number to fill with
2284 @param n Size of memory region (in bytes) to fill
2285
2286 @note This function avoids writes during modes 2 & 3
2287 */
2288 void vmemset (void *s, uint8_t c, size_t n) OLDCALL PRESERVES_REGS(b, c);
2289
2290
2291
2292 /** Fills a rectangular region of Tile Map entries for the Background layer with tile.
2293
2294 @param x X Start position in Background Map tile coordinates. Range 0 - 31
2295 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
2296 @param w Width of area to set in tiles. Range 1 - 32
2297 @param h Height of area to set in tiles. Range 1 - 32
2298 @param tile Fill value
2299 */
2300 void fill_bkg_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t tile) OLDCALL PRESERVES_REGS(b, c);
2301 #define fill_rect fill_bkg_rect
2302
2303 /** Fills a rectangular region of Tile Map Attribute entries for the Background layer with attribute.
2304
2305 @param x X Start position in Background Map tile coordinates. Range 0 - 31
2306 @param y Y Start position in Background Map tile coordinates. Range 0 - 31
2307 @param w Width of area to set in tiles. Range 1 - 32
2308 @param h Height of area to set in tiles. Range 1 - 32
2309 @param tile Fill value
2310 */
2311 inline void fill_bkg_rect_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t attribute)
2312 {
2313 VBK_REG = VBK_ATTRIBUTES;
2314 fill_bkg_rect(x, y, w, h, attribute);
2315 VBK_REG = VBK_TILES;
2316 }
2317
2318 /** Fills a rectangular region of Tile Map entries for the Window layer with tile.
2319
2320 @param x X Start position in Window Map tile coordinates. Range 0 - 31
2321 @param y Y Start position in Window Map tile coordinates. Range 0 - 31
2322 @param w Width of area to set in tiles. Range 1 - 32
2323 @param h Height of area to set in tiles. Range 1 - 32
2324 @param tile Fill value
2325 */
2326 void fill_win_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t tile) OLDCALL PRESERVES_REGS(b, c);
2327
2328 /** Fills a rectangular region of Tile Map Attribute entries for the Window layer with attribute.
2329
2330 @param x X Start position in Window Map tile coordinates. Range 0 - 31
2331 @param y Y Start position in Window Map tile coordinates. Range 0 - 31
2332 @param w Width of area to set in tiles. Range 1 - 32
2333 @param h Height of area to set in tiles. Range 1 - 32
2334 @param tile Fill value
2335 */
2336 inline void fill_win_rect_attributes(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint8_t attribute)
2337 {
2338 VBK_REG = VBK_ATTRIBUTES;
2339 fill_win_rect(x, y, w, h, attribute);
2340 VBK_REG = VBK_TILES;
2341 }
2342
2343 #endif /* _GB_H */
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.