This is an automated email from Gerrit.

"zapb <d...@zapb.de>" just uploaded a new patch set to Gerrit, which you can 
find at https://review.openocd.org/c/openocd/+/8635

-- gerrit

commit 67149606c8e9aea691c5fb39fe58762eb078a71f
Author: Marc Schink <d...@zapb.de>
Date:   Mon Dec 2 21:51:32 2024 +0000

    flash/nor/tcl: Rewrite 'flash mdX' as Tcl code
    
    Remove redundant C code by rewriting the 'flash mdX' commands as Tcl
    code which uses the 'flash read_memory' command internally.
    
    This replacement leads to a slight increase in runtime of around 0.5 ms
    and 37.2 ms to display 512 and 16384 flash memory words, respectively.
    Nonetheless, this replacement results in a cleaner code base as existing
    functions are reused instead of having duplicate implementations, and is
    therefore worth the slight performance loss in displaying memory
    contents.
    
    Change-Id: I7d239abec31416ce16238289e65f532ca22deecc
    Signed-off-by: Marc Schink <d...@zapb.de>

diff --git a/src/flash/nor/tcl.c b/src/flash/nor/tcl.c
index a41437213c..32880c180b 100644
--- a/src/flash/nor/tcl.c
+++ b/src/flash/nor/tcl.c
@@ -667,66 +667,6 @@ done:
        return retval;
 }
 
-COMMAND_HANDLER(handle_flash_md_command)
-{
-       int retval;
-
-       if (CMD_ARGC < 1 || CMD_ARGC > 2)
-               return ERROR_COMMAND_SYNTAX_ERROR;
-
-       target_addr_t address;
-       COMMAND_PARSE_ADDRESS(CMD_ARGV[0], address);
-
-       uint32_t count = 1;
-       if (CMD_ARGC == 2)
-               COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], count);
-
-       unsigned int wordsize;
-       switch (CMD_NAME[2]) {
-               case 'w':
-                       wordsize = 4;
-                       break;
-               case 'h':
-                       wordsize = 2;
-                       break;
-               case 'b':
-                       wordsize = 1;
-                       break;
-               default:
-                       return ERROR_COMMAND_SYNTAX_ERROR;
-       }
-
-       if (count == 0)
-               return ERROR_OK;
-
-       struct target *target = get_current_target(CMD_CTX);
-       struct flash_bank *bank;
-       retval = get_flash_bank_by_addr(target, address, true, &bank);
-       if (retval != ERROR_OK)
-               return retval;
-
-       uint32_t offset = address - bank->base;
-       uint32_t sizebytes = count * wordsize;
-       if (offset + sizebytes > bank->size) {
-               command_print(CMD, "Cannot cross flash bank borders");
-               return ERROR_FAIL;
-       }
-
-       uint8_t *buffer = calloc(count, wordsize);
-       if (!buffer) {
-               command_print(CMD, "No memory for flash read buffer");
-               return ERROR_FAIL;
-       }
-
-       retval = flash_driver_read(bank, buffer, offset, sizebytes);
-       if (retval == ERROR_OK)
-               target_handle_md_output(CMD, target, address, wordsize, count, 
buffer);
-
-       free(buffer);
-
-       return retval;
-}
-
 COMMAND_HANDLER(handle_flash_read_memory_command)
 {
        /*
@@ -1265,27 +1205,6 @@ static const struct command_registration 
flash_exec_command_handlers[] = {
                .help = "Fill n bytes with 8-bit value, starting at "
                        "word address.  (No autoerase.)",
        },
-       {
-               .name = "mdb",
-               .handler = handle_flash_md_command,
-               .mode = COMMAND_EXEC,
-               .usage = "address [count]",
-               .help = "Display bytes from flash.",
-       },
-       {
-               .name = "mdh",
-               .handler = handle_flash_md_command,
-               .mode = COMMAND_EXEC,
-               .usage = "address [count]",
-               .help = "Display half-words from flash.",
-       },
-       {
-               .name = "mdw",
-               .handler = handle_flash_md_command,
-               .mode = COMMAND_EXEC,
-               .usage = "address [count]",
-               .help = "Display words from flash.",
-       },
        {
                .name = "read_memory",
                .mode = COMMAND_EXEC,
diff --git a/src/flash/startup.tcl b/src/flash/startup.tcl
index 0dd84efacc..aa8add067b 100644
--- a/src/flash/startup.tcl
+++ b/src/flash/startup.tcl
@@ -104,6 +104,65 @@ proc program {filename args} {
 add_help_text program "write an image to flash, address is only required for 
binary images. preverify, verify, reset, exit are optional"
 add_usage_text program "<filename> \[address\] \[preverify\] \[verify\] 
\[reset\] \[exit\]"
 
+proc display_memory {memory_content size {offset 0}} {
+       switch $size {
+               8 {
+                       set value_fmt "%2.2x"
+               }
+               16 {
+                       set value_fmt "%4.4xx"
+               }
+               32 {
+                       set value_fmt "%8.8x"
+               }
+               64 {
+                       set value_fmt "%16.16x"
+               }
+       }
+
+       set output ""
+       set counter 0
+
+       foreach value $memory_content {
+               set value_output [format $value_fmt $value]
+
+               if {[expr {$counter % 32}] == 0} {
+                       if {$counter > 0} {
+                               append output "\n"
+                       }
+
+                       append output [format "0x%8.8x: " [expr {$offset + 
$counter}]]
+               }
+
+               append output "$value_output "
+
+               set counter [expr {$counter + ($size / 8)}]
+       }
+
+       return $output
+}
+
+proc "flash mdb" {address {count 1}} {
+       set memory_content [flash read_memory $address 8 $count]
+       display_memory $memory_content 8 $address
+}
+add_help_text "flash mdb" "Display bytes from flash memory"
+add_usage_text "flash mdb" "address \[count\]"
+
+proc "flash mdh" {address {count 1}} {
+       set memory_content [flash read_memory $address 16 $count]
+       display_memory $memory_content 16 $address
+}
+add_help_text "flash mdh" "Display half-words from flash memory"
+add_usage_text "flash mdh" "address \[count\]"
+
+proc "flash mdw" {address {count 1}} {
+       set memory_content [flash read_memory $address 32 $count]
+       display_memory $memory_content 32 $address
+}
+add_help_text "flash mdw" "Display words from flash memory"
+add_usage_text "flash mdw" "address \[count\]"
+
 # stm32[f0x|f3x] uses the same flash driver as the stm32f1x
 proc stm32f0x args { eval stm32f1x $args }
 proc stm32f3x args { eval stm32f1x $args }

-- 

Reply via email to