gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 7b94d3fe4553f9ef8e6d635edec22e381588c321 parent f6432035ca7acd7aa378340c6eeb30b2e022223c Author: Toxa <untoxa@mail.ru> Date: Sat, 24 Jul 2021 15:11:13 +0300 BGB_printf() funciton, replacement for BGB_MESSAGE_FMT() macro Diffstat:
| M | gbdk-lib/examples/gb/bgb_debug/bgb_debug.c | 5 | +++++ |
| M | gbdk-lib/include/gb/bgb_emu.h | 62 | +++++++++++++++++++++++--------------------------------------- |
| M | gbdk-lib/libc/gb/Makefile | 2 | +- |
| M | gbdk-lib/libc/gb/bgb_emu.s | 32 | +++++++++++++++----------------- |
| A | gbdk-lib/libc/gb/bgb_emu_printf.s | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 87 insertions(+), 57 deletions(-)
diff --git a/gbdk-lib/examples/gb/bgb_debug/bgb_debug.c b/gbdk-lib/examples/gb/bgb_debug/bgb_debug.c @@ -136,6 +136,11 @@ int main(void) // It's equivalent to: BGB_MESSAGE("PROFILE,%(SP+$0)%,%(SP+$1)%,%A%,%TOTALCLKS%,%ROMBANK%,%WRAMBANK%"); + uint8_t var0 = 16; + int16_t var1 = -10; + // + BGB_printf("var0: %hd; var1: %d; var0*var1=%d", (uint8_t)var0, var1, var0 * var1); + // The BGB_TEXT() macro will accept a non-quoted string BGB_TEXT("The End"); diff --git a/gbdk-lib/include/gb/bgb_emu.h b/gbdk-lib/include/gb/bgb_emu.h @@ -67,45 +67,6 @@ llbl: \ .ENDM \ name ^/message_text/, ^/message_suffix/ \ __endasm - -#define BGB_HASH # -#define BGB_ADD_HASH(x) x -#define BGB_MAKE_LABEL(a) BGB_ADD_HASH(BGB_HASH)a -/// \endcond DOXYGEN_DO_NOT_DOCUMENT - -/** Macro to display a sprintf formatted message in the BGB emulator debug message window - - @param buf Pointer to a globally defined char buffer - @param ... VA Args list of sprintf parameters - - To avoid buffer overflows __buf__ must be large - enough to store the entire printed message. - - Example: - \code{.c} - char mybuf[100]; // should be globally defined - - BGB_MESSAGE_FMT(mybuf, "An integer:%d, a string: %s", 12345, "hello bgb") - \endcode - - @see BGB_MESSAGE() - */ -#define BGB_MESSAGE_FMT(buf, ...) sprintf(buf, __VA_ARGS__);BGB_MESSAGE2(BGB_MACRONAME(__LINE__), BGB_MAKE_LABEL(_##buf)); -/// \cond DOXYGEN_DO_NOT_DOCUMENT -#define BGB_MESSAGE2(lbl, buf) \ -__asm \ -.MACRO name buf, ?llbl\ - ld d, d \ - jr llbl \ - .dw 0x6464 \ - .dw 0x0001 \ - .dw buf \ - .dw 0 \ -llbl: \ -.ENDM \ -name ^/buf/ \ -__endasm - /// \endcond DOXYGEN_DO_NOT_DOCUMENT /** Macro to __Start__ a profiling block for the BGB emulator @@ -157,6 +118,29 @@ __endasm */ void BGB_profiler_message(); +/** Print the string and arguments given by format to the BGB emulator debug message window + + @param format The format string as per printf + + Does not return the number of characters printed. + Result string MUST BE LESS THAN 64 BYTES LONG, INCLUDING THE TRAILIG ZERO BYTE! + + Currently supported: + \li \%hx (char as hex) + \li \%hu (unsigned char) + \li \%hd (signed char) + \li \%c (character) + \li \%u (unsigned int) + \li \%d (signed int) + \li \%x (unsigned int as hex) + \li \%s (string) + + Warning: to correctly pass chars for printing as chars, they *must* + be explicitly re-cast as such when calling the function. + See @ref docs_chars_varargs for more details. + */ +void BGB_printf(const char *format, ...) NONBANKED; + static void * __BGB_PROFILER_INIT = &BGB_profiler_message; #endif diff --git a/gbdk-lib/libc/gb/Makefile b/gbdk-lib/libc/gb/Makefile @@ -21,7 +21,7 @@ ASSRC = cgb.s cgb_palettes.s cgb_compat.s \ set_1bit_data.s \ sgb.s font.s delay.s \ rand.s arand.s \ - bgb_emu.s \ + bgb_emu.s bgb_emu_printf.s \ nowait.s far_ptr.s \ lcd.s joy.s tim.s \ crash_handler.s \ diff --git a/gbdk-lib/libc/gb/bgb_emu.s b/gbdk-lib/libc/gb/bgb_emu.s @@ -1,22 +1,20 @@ - .include "global.s" + .include "global.s" - ;; **************************************** - ;; Beginning of module - ;; BANKED: checked - .title "BGB_emu" - .module BGB_emu - .area _EMU_HEADER (ABS) + .title "BGB_emu" + .module BGB_emu + + .area _EMU_HEADER (ABS) - .org 0x08 - JP _BGB_profiler_message + .org 0x08 + JP _BGB_profiler_message - .area _HOME + .area _HOME - ;; BGB profiler message + ;; BGB profiler message _BGB_profiler_message:: - LD D, D - JR 1$ - .dw 0x6464 - .dw 0 - .ascii "PROFILE,%(SP+$0)%,%(SP+$1)%,%A%,%TOTALCLKS%,%ROMBANK%,%WRAMBANK%" -1$: RET + LD D, D + JR 1$ + .dw 0x6464 + .dw 0 + .ascii "PROFILE,%(SP+$0)%,%(SP+$1)%,%A%,%TOTALCLKS%,%ROMBANK%,%WRAMBANK%" +1$: RET diff --git a/gbdk-lib/libc/gb/bgb_emu_printf.s b/gbdk-lib/libc/gb/bgb_emu_printf.s @@ -0,0 +1,43 @@ + .include "global.s" + + .title "BGB_emu" + .module BGB_emu + + .globl _sprintf + + .area _DATA + +ret_save: + .ds 0x02 +printf_buffer: + .ds 0x80 + + .area _HOME + + ;; BGB_printf(fmt, ...) +_BGB_printf:: + di + pop de + ld hl, #ret_save + ld (hl), e + inc hl + ld (hl), d + + ld de, #printf_buffer + push de + call _sprintf + pop hl + + ld d,d + jr 1$ + .dw 0x6464 + .dw 0x0001 + .dw #printf_buffer + .dw 0 +1$: + ld hl, #ret_save + ld a, (hl+) + ld h, (hl) + ld l, a + ei + jp (hl)
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.