Docs: EMU_printf: Note %c vars should not be cast when passing

- Improve EMU_printf in example with more detailed version
This commit is contained in:
bbbbbr
2025-01-23 01:48:47 -08:00
parent 4ff8549fda
commit 459dbfbff4
2 changed files with 42 additions and 8 deletions

View File

@@ -153,9 +153,34 @@ int main(void)
EMU_MESSAGE("PROFILE,%(SP+$0)%,%(SP+$1)%,%A%,%TOTALCLKS%,%ROMBANK%,%WRAMBANK%");
#endif
uint8_t var0 = 16;
int16_t var1 = -10;
EMU_printf("var0: %hd; var1: %d; var0*var1=%d\n", (uint8_t)var0, var1, var0 * var1);
int16_t var_s16 = -1234;
uint16_t var_u16 = 31002;
uint16_t var_u16h = 0xA50Fu;
int8_t var_s8 = -56;
uint8_t var_u8 = 224;
uint8_t var_u8h = 0xF8;
char var_chr = 'A';
char * var_string = "Hello Emu Printf";
// For EMU_printf:
// Note how all the 8 bit arguments must be explicitly cast EXCEPT for %c
// In partifcular, %c MUST NOT be cast or the output will be incorrect
EMU_printf("signed int: %d\n"
"unsigned int: %u\n"
"hex int: 0x%X\n"
"signed byte: %hd\n"
"unsigned byte: %hu\n"
"hex byte: 0x%hX\n"
"Character: %c\n"
"String: %s\n",
(int16_t) var_s16,
(uint16_t) var_u16,
(uint16_t) var_u16h,
(int8_t)var_s8,
(uint8_t)var_u8,
(uint8_t)var_u8h,
var_chr, // Do not explicitly cast %c vars for EMU_printf, it expects them auto-promoted to 2 bytes (casting would prevent that)
var_string);
// The EMU_TEXT() macro will accept a non-quoted string
EMU_TEXT("The End");

View File

@@ -149,17 +149,26 @@ void EMU_profiler_message(void);
\li \%hx (char as hex)
\li \%hu (unsigned char)
\li \%hd (signed char)
\li \%c (character)
\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.
Currently supported in the Emulicious emulator
@note
Variables for the following 8-bit formats __MUST__ be cast to their type when passed to EMU_printf()
\li \%hx (char)
\li \%hu (unsigned char)
\li \%hd (signed char)
However variables for the following 8-bit format __MUST NOT__ be cast to their type when passed to EMU_printf()
\li \%c (char)
This behavior is __different__ than for @ref sprintf(), which does require \%c format char variables to be explicitly cast.
Currently supported in the Emulicious emulator, may be supported by bgb
*/
void EMU_printf(const char *format, ...) PRESERVES_REGS(a, b, c);
#define BGB_printf(...) EMU_printf(__VA_ARGS__)