]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/cmd_mem.c
Z180 parameter checks
[z180-stamp.git] / avr / cmd_mem.c
index 53b18426fbcc1400718ab88f1b2581ab43cf3ed0..ded514669f1a6122f54c3b53bcd810d031cae4a4 100644 (file)
@@ -4,7 +4,7 @@
  * (C) Copyright 2000
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
- * SPDX-License-Identifier:    GPL-2.0+
+ * SPDX-License-Identifier:    GPL-2.0
  */
 
 /*
@@ -22,6 +22,7 @@
 #include "cli_readline.h"
 #include "print-utils.h"
 #include "con-utils.h"
+#include "getopt-min.h"
 #include "timer.h"
 #include "z80-if.h"
 #include "debug.h"
@@ -42,13 +43,14 @@ static      uint32_t        base_address = 0;
 
 /*--------------------------------------------------------------------------*/
 
-
-void z180_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
+int z180_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
 {
-               if (z80_bus_cmd(Request) & ZST_ACQUIRED) {
-                       z80_read_block (buf, addr, count);
-                       z80_bus_cmd(Release);
-               }
+       if (!(z80_bus_cmd(Request) & ZST_ACQUIRED))
+               return -1;
+
+       z80_read_block (buf, addr, count);
+       z80_bus_cmd(Release);
+       return 0;
 }
 
 /*--------------------------------------------------------------------------*/
@@ -92,10 +94,16 @@ command_ret_t do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
        }
 
        /* Print the lines. */
-       dump_mem(addr, addr, length, z180_read_buf, NULL);
+       int ret = dump_mem(addr, addr, length, z180_read_buf, NULL);
+       if (ret == -2)  { /* TODO: Error codes */
+               my_puts_P(PSTR("Bus timeout\n"));
+               return  CMD_RET_FAILURE;
+       }
 
-       dp_last_addr = addr + length;
-       dp_last_length = length;
+       if (ret >= 0) {
+               dp_last_addr = addr + length;
+               dp_last_length = length;
+       }
        return CMD_RET_SUCCESS;
 }
 
@@ -144,7 +152,7 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
                z80_bus_cmd(Release);
                printf_P(PSTR("%05lx: %02x"), addr, data);
 
-               nbytes = cli_readline(PSTR(" ? "));
+               nbytes = cli_readline(PSTR(" ? "), 0);
                if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
                        /* <CR> pressed as only input, don't modify current
                         * location and move to next. "-" pressed will go back.
@@ -152,8 +160,8 @@ mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
                        if (incrflag)
                                addr += nbytes ? -1 : 1;
                        nbytes = 1;
-               }
-               else {
+
+               else {
                        char *endp;
                        data = strtoul(console_buffer, &endp, 16);
                        nbytes = endp - console_buffer;
@@ -186,36 +194,60 @@ command_ret_t do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[
 
 command_ret_t do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-       uint8_t writeval;
-       uint32_t addr, count;
+       uint32_t writeval;
+       uint32_t addr;
+       uint32_t count = 1;
+       uint_fast8_t width = 1;
 
-       (void) cmdtp;
-       (void) flag;
+       (void) cmdtp; (void) flag;
+
+       /* reset getopt() */
+       optind = 0;
 
-       if ((argc < 3) || (argc > 4))
+       int opt;
+       while ((opt = getopt(argc, argv, PSTR("bwl"))) != -1) {
+               switch (opt) {
+               case 'b':
+                       width = 1;
+                       break;
+               case 'w':
+                       width = 2;
+                       break;
+               case 'l':
+                       width = 4;
+                       break;
+               default: /* '?' */
+                       return CMD_RET_USAGE;
+               }
+       }
+
+       /* remaining arguments */
+       argc -= optind;
+       if ((argc < 2) || (argc > 3))
                return CMD_RET_USAGE;
 
-       /* Address is specified since argc > 1
-       */
-       addr = strtoul(argv[1], NULL, 16);
+       /* Address and value are specified since (adjusted) argc >= 2 */
+       addr = strtoul(argv[optind++], NULL, 16);
        addr += base_address;
-
-       /* Get the value to write.
-       */
-       writeval = (uint8_t) strtoul(argv[2], NULL, 16);
+       writeval = strtoul(argv[optind++], NULL, 16);
 
        /* Count ? */
-       if (argc == 4) {
-               count = strtoul(argv[3], NULL, 16);
-       } else {
-               count = 1;
-       }
+       if (argc == 3)
+               count = strtoul(argv[optind], NULL, 16);
 
        if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
                my_puts_P(PSTR("Bus timeout\n"));
                return  CMD_RET_FAILURE;
        }
-       z80_memset(addr, writeval, count);
+
+       if (width == 1)
+               z80_memset(addr, writeval, count);
+       else {
+               while (count--) {
+                       z80_write_block((const uint8_t *) &writeval, addr, width);
+                       addr += width;
+               }
+       }
        z80_bus_cmd(Release);
 
        return CMD_RET_SUCCESS;