From 9e3744bd91f79916d7cae2faca0291de86400cd4 Mon Sep 17 00:00:00 2001 From: Patrick Mackinlay Date: Wed, 20 Mar 2024 13:56:30 +0700 Subject: [PATCH] debugger: add null-terminated string output to printf/logerror * also added left-justification option for numeric and string formats * removed duplication and made documentation more consistent --- docs/source/debugger/general.rst | 24 +++++---- src/emu/debug/debugcmd.cpp | 89 ++++++++++++++++++++++++++------ src/emu/debug/debughlp.cpp | 28 ++++------ 3 files changed, 98 insertions(+), 43 deletions(-) diff --git a/docs/source/debugger/general.rst b/docs/source/debugger/general.rst index 13060894aaec3..c01ef20dea8b6 100644 --- a/docs/source/debugger/general.rst +++ b/docs/source/debugger/general.rst @@ -190,18 +190,22 @@ available: %c Prints the corresponding argument as an 8-bit character. -%[0][]d +%[-][0][]d Prints the corresponding argument as a decimal number with optional - minimum field width and zero fill. -%[0][]o + left justification, zero fill and minimum field width. +%[-][0][]o Prints the corresponding argument as an octal number with optional - minimum field width and zero fill using lowercase letters. -%[0][]x - Prints the corresponding argument as a hexadecimal number with - optional minimum field width and zero fill using lowercase letters. -%[0][]X - Prints the corresponding argument as a hexadecimal number with - optional minimum field width and zero fill using uppercase letters. + left justification, zero fill and minimum field width. +%[-][0][]x + Prints the corresponding argument as a lowercase hexadecimal number + with optional left justification, zero fill and minimum field width. +%[-][0][]X + Prints the corresponding argument as an uppercase hexadecimal number + with optional left justification, zero fill and minimum field width. +%[-][][.[]]s + Prints a null-terminated string of 8-bit characters from the address + and address space given by the corresponding argument, with optional + left justification, minimum and maximum field widths. \%% Prints a literal percent symbol. \\n diff --git a/src/emu/debug/debugcmd.cpp b/src/emu/debug/debugcmd.cpp index 082324a6d9479..ad463ace500fc 100644 --- a/src/emu/debug/debugcmd.cpp +++ b/src/emu/debug/debugcmd.cpp @@ -471,7 +471,7 @@ void debugger_commands::execute_print(const std::vector ¶m bool debugger_commands::mini_printf(std::ostream &stream, const std::vector ¶ms) { - std::string_view format(params[0]); + std::string_view const format(params[0]); auto f = format.begin(); int param = 1; @@ -499,21 +499,48 @@ bool debugger_commands::mini_printf(std::ostream &stream, const std::vector= '0' && *f <= '9') + // parse optional left justification flag + if (f != format.end() && *f == '-') { - c = *f++; - if (c == '0' && width == 0) - zerofill = 1; - width = width * 10 + (c - '0'); + left_justify = true; + f++; + } + + // parse optional zero fill flag + if (f != format.end() && *f == '0') + { + zero_fill = true; + f++; + } + + // parse optional width + while (f != format.end() && isdigit(*f)) + width = width * 10 + (*f++ - '0'); + if (f == format.end()) + break; + + // apply left justification + if (left_justify) + width = -width; + + if ((c = *f++) == '.') + { + // parse optional precision + while (f != format.end() && isdigit(*f)) + precision = precision * 10 + (*f++ - '0'); + + // get the format + if (f != format.end()) + c = *f++; + else + break; } - if (f == format.end()) break; - // get the format - c = *f++; switch (c) { case '%': @@ -522,7 +549,7 @@ bool debugger_commands::mini_printf(std::ostream &stream, const std::vectordevice().memory().translate(space->spacenum(), device_memory_interface::TR_READ, taddress = address, tspace); address++) + { + u8 const data = tspace->read_byte(taddress); + + if (!data) + break; + + s += data; + + if (precision == 1) + break; + else if (precision) + precision--; + } + + util::stream_format(stream, "%*s", width, s); + } + else + { + m_console.printf("Not enough parameters for format!\n"); + return false; + } + } + break; } } diff --git a/src/emu/debug/debughlp.cpp b/src/emu/debug/debughlp.cpp index 5d71d81e0ba18..76102402bcbef 100644 --- a/src/emu/debug/debughlp.cpp +++ b/src/emu/debug/debughlp.cpp @@ -359,11 +359,12 @@ const help_item f_static_help_list[] = "The printf command performs a C-style printf to the debugger console. Only a very limited set of " "formatting options are available:\n" "\n" - " %c -- 8-bit character\n" - " %[0][]d -- decimal number with optional digit count and zero-fill\n" - " %[0][]o -- octal number with optional digit count and zero-fill\n" - " %[0][]x -- hexadecimal number with optional digit count and zero-fill (lowercase digits)\n" - " %[0][]X -- hexadecimal number with optional digit count and zero-fill (uppercase digits)\n" + " %c -- 8-bit character\n" + " %[-][0][]d -- decimal number with optional left justification, zero fill and minimum width\n" + " %[-][0][]o -- octal number with optional left justification, zero fill and minimum width\n" + " %[-][0][]x -- lowercase hexadecimal number with optional left justification, zero fill and minimum width\n" + " %[-][0][]X -- uppercase hexadecimal number with optional left justification, zero fill and minimum width\n" + " %[-][][.[]]s -- null-terminated string of 8-bit characters with optional left justification, minimum and maximum width\n" "\n" "All remaining formatting options are ignored. Use %% to output a % character. Multiple lines can be " "printed by embedding a \\n in the text.\n" @@ -371,7 +372,7 @@ const help_item f_static_help_list[] = "Examples:\n" "\n" "printf \"PC=%04X\",pc\n" - " Prints PC= where is displayed in hexadecimal with 4 digits with zero-fill.\n" + " Prints PC= where is displayed in uppercase hexadecimal with 4 digits and zero fill.\n" "\n" "printf \"A=%d, B=%d\\nC=%d\",a,b,a+b\n" " Prints A=, B= on one line, and C= on a second line.\n" @@ -382,21 +383,12 @@ const help_item f_static_help_list[] = " logerror [,[,...]]\n" "\n" "The logerror command performs a C-style printf to the error log. Only a very limited set of " - "formatting options are available:\n" - "\n" - " %c -- 8-bit character\n" - " %[0][]d -- decimal number with optional digit count and zero-fill\n" - " %[0][]o -- octal number with optional digit count and zero-fill\n" - " %[0][]x -- hexadecimal number with optional digit count and zero-fill (lowercase digits)\n" - " %[0][]X -- hexadecimal number with optional digit count and zero-fill (uppercase digits)\n" - "\n" - "All remaining formatting options are ignored. Use %% to output a % character. Multiple lines can be " - "printed by embedding a \\n in the text.\n" + "formatting options are available. See the 'printf' help for details.\n" "\n" "Examples:\n" "\n" - "logerror \"PC=%04X\",pc\n" - " Logs PC= where is displayed in hexadecimal with 4 digits with zero-fill.\n" + "logerror \"PC=%04x\",pc\n" + " Logs PC= where is displayed in lowercase hexadecimal with 4 digits and zero fill.\n" "\n" "logerror \"A=%d, B=%d\\nC=%d\",a,b,a+b\n" " Logs A=, B= on one line, and C= on a second line.\n"