diff options
Diffstat (limited to 'avr/cmd_mem.c')
-rw-r--r-- | avr/cmd_mem.c | 774 |
1 files changed, 774 insertions, 0 deletions
diff --git a/avr/cmd_mem.c b/avr/cmd_mem.c new file mode 100644 index 0000000..3e18770 --- /dev/null +++ b/avr/cmd_mem.c @@ -0,0 +1,774 @@ +/* + * (C) Copyright 2014,2018 Leo C. <erbl259-lmu@yahoo.de> + * + * (C) Copyright 2000 + * Wolfgang Denk, DENX Software Engineering, wd@denx.de. + * + * SPDX-License-Identifier: GPL-2.0 + */ + +/* + * Memory Functions + * + * Copied from FADS ROM, Dan Malek (dmalek@jlc.net) + */ + +#include "cmd_mem.h" +#include <avr/interrupt.h> + +#include "cli_readline.h" +#include "print-utils.h" +#include "con-utils.h" +#include "getopt-min.h" +#include "eval_arg.h" +#include "timer.h" +#include "z80-if.h" +#include "debug.h" + + +#ifndef CONFIG_SYS_MEMTEST_SCRATCH +#define CONFIG_SYS_MEMTEST_SCRATCH 0 +#endif + +/* Display values from last command. + * Memory modify remembered values are different from display memory. + */ +static uint32_t dp_last_addr; +static uint32_t dp_last_length = 0x100; +static uint32_t mm_last_addr; + +static uint32_t base_address = 0; + +/*--------------------------------------------------------------------------*/ + +static ERRNUM z180_read_buf(uint8_t *buf, uint32_t addr, uint8_t count) +{ + if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) + return EBUSTO; + + z80_read_block (buf, addr, count); + z80_bus_cmd(Release); + return ESUCCESS; +} + +/*--------------------------------------------------------------------------*/ + +command_ret_t do_mem_size(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) +{ + int32_t ramsize = z80_memsize_detect(); + + if (ramsize < 0) + cmd_error(CMD_RET_FAILURE, (ERRNUM) -ramsize, PSTR("Couldn't access RAM")); + + printf_P(PSTR("Detected RAM: Start %.5lx, End: %.5lx, Size: %.5lx (%ld dec)\n"), + 0l, ramsize ? ramsize-1 : 0l, ramsize, ramsize); + + return CMD_RET_SUCCESS; +} + +/* Memory Display + * + * Syntax: + * md {addr} {len} + */ +command_ret_t do_mem_md(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t addr, length; + + (void) cmdtp; + +#if 0 + printf_P(PSTR("flag: %d, argc: %d"), flag, argc); + for (int i = 0; i < argc; i++) { + printf_P(PSTR(", argv[%d]: %s"), i, argv[i] ? argv[i] : "<NULL>"); + } + putchar('\n'); +#endif + + /* We use the last specified parameters, unless new ones are + * entered. + */ + addr = dp_last_addr; + length = dp_last_length; + + if (argc < 2) + return CMD_RET_USAGE; + + if ((flag & CMD_FLAG_REPEAT) == 0) { + /* Address is specified since argc > 1 */ + addr = eval_arg(argv[1], NULL); + addr += base_address; + + /* If another parameter, it is the length to display. */ + if (argc > 2) + length = eval_arg(argv[2], NULL); + } + + /* Print the lines. */ + ERRNUM ret = dump_mem(addr, addr, length, z180_read_buf, NULL); + if (ret == EBUSTO) + cmd_error(CMD_RET_FAILURE, ret, NULL); + + if (ret == ESUCCESS) { + dp_last_addr = addr + length; + dp_last_length = length; + } + return CMD_RET_SUCCESS; +} + +/* Modify memory. + * + * Syntax: + * mm {addr} + * nm {addr} + */ +static command_ret_t +mod_mem(cmd_tbl_t *cmdtp, int incrflag, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t addr; + uint8_t data; + int nbytes; + + (void) cmdtp; + + if (argc != 2) + return CMD_RET_USAGE; + + /* We use the last specified parameters, unless new ones are + * entered. + */ + addr = mm_last_addr; + + if ((flag & CMD_FLAG_REPEAT) == 0) { + /* New command specified. + */ + + /* Address is specified since argc > 1 + */ + addr = eval_arg(argv[1], NULL); + addr += base_address; + } + + /* Print the address, followed by value. Then accept input for + * the next value. A non-converted value exits. + */ + do { + z80_bus_request_or_exit(); + data = z80_read(addr); + z80_bus_cmd(Release); + printf_P(PSTR("%05lx: %02x"), addr, data); + + 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. + */ + if (incrflag) + addr += nbytes ? -1 : 1; + nbytes = 1; + + } else { + char *endp; + data = eval_arg(console_buffer, &endp); + nbytes = endp - console_buffer; + if (nbytes) { + z80_bus_request_or_exit(); + z80_write(addr, data); + z80_bus_cmd(Release); + if (incrflag) + addr++; + } + } + } while (nbytes > 0); + + mm_last_addr = addr; + return CMD_RET_SUCCESS; +} + + +command_ret_t do_mem_mm(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + return mod_mem (cmdtp, 1, flag, argc, argv); +} +command_ret_t do_mem_nm(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + return mod_mem (cmdtp, 0, flag, argc, argv); +} + +command_ret_t do_mem_mw(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t writeval; + uint32_t addr; + uint32_t count = 1; + uint_fast8_t width = 1; + + (void) cmdtp; (void) flag; + + 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 and value are specified since (adjusted) argc >= 2 */ + addr = eval_arg(argv[optind++], NULL); + addr += base_address; + writeval = eval_arg(argv[optind++], NULL); + + /* Count ? */ + if (argc == 3) + count = eval_arg(argv[optind], NULL); + + z80_bus_request_or_exit(); + 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; +} + +#ifdef CONFIG_MX_CYCLIC +command_ret_t do_mem_mdc ( cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t count; + uint32_t ts; + + (void) cmdtp; + (void) flag; + + if (argv[0][1] != 'd') { + int opt; + while ((opt = getopt(argc, argv, PSTR("bwl"))) != -1) + if (opt == '?') + return CMD_RET_USAGE; + --optind; + } + + if (argc-optind != 4) + return CMD_RET_USAGE; + + count = eval_arg(argv[optind + 3], NULL); + + clear_ctrlc(); /* forget any previous Control C */ + for (;;) { + + if (argv[0][1] == 'd') + do_mem_md (NULL, 0, argc-1, argv); /* memory display */ + else + do_mem_mw (NULL, 0, argc-1, argv); /* memory write */ + + + /* delay for <count> ms... */ + ts = get_timer(0); + do { + /* check for ctrl-c to abort... */ + if (had_ctrlc() || ctrlc()) { + my_puts_P(PSTR("Abort\n")); + return CMD_RET_SUCCESS; + } + } while (get_timer(ts) < count); + } + + return CMD_RET_SUCCESS; +} +#endif /* CONFIG_MX_CYCLIC */ + +command_ret_t do_mem_cmp(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t addr1, addr2, count, ngood; + command_ret_t rcode = CMD_RET_SUCCESS; + uint8_t byte1, byte2; + + (void) cmdtp; + (void) flag; + + if (argc != 4) + return CMD_RET_USAGE; + + + addr1 = eval_arg(argv[1], NULL); + addr1 += base_address; + addr2 = eval_arg(argv[2], NULL); + addr2 += base_address; + count = eval_arg(argv[3], NULL); + + for (ngood = 0; ngood < count; ++ngood) { + if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) { + my_puts_P(PSTR("Bus timeout\n")); + rcode = CMD_RET_FAILURE; + break; + } + byte1 = z80_read(addr1); + byte2 = z80_read(addr2); + z80_bus_cmd(Release); + if (byte1 != byte2) { + printf_P(PSTR("byte at 0x%05lx (%#02x) != " + "byte at 0x%05lx (%#02x)\n"), + addr1, byte1, addr2, byte2); + rcode = CMD_RET_FAILURE; + break; + } + addr1++; + addr2++; + + /* check for ctrl-c to abort... */ + if (ctrlc()) { + my_puts_P(PSTR("Abort\n")); + return CMD_RET_SUCCESS; + } + } + + printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood, ngood); + return rcode; +} + +command_ret_t do_mem_cp(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t src, dest, count; + int_fast8_t step; + + (void) cmdtp; + (void) flag; + + if (argc != 4) + return CMD_RET_USAGE; + + src = eval_arg(argv[1], NULL); + src += base_address; + dest = eval_arg(argv[2], NULL); + dest += base_address; + count = eval_arg(argv[3], NULL); + + if (count == 0) { + my_puts_P(PSTR("Zero length?\n")); + return CMD_RET_FAILURE; + } + + if (dest > src) { + src += count - 1; + dest += count - 1; + step = -1; + } else + step = 1; + + while (count-- > 0) { + uint8_t data; + z80_bus_request_or_exit(); + data = z80_read(src); + z80_write(dest, data); + z80_bus_cmd(Release); + src += step; + dest += step; + + /* check for ctrl-c to abort... */ + if (ctrlc()) { + my_puts_P(PSTR("Abort\n")); + return CMD_RET_SUCCESS; + } + } + return CMD_RET_SUCCESS; +} + +command_ret_t do_mem_base(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, + char * const argv[]) +{ + (void) cmdtp; + (void) flag; + + if (argc > 1) { + /* Set new base address. */ + base_address = eval_arg(argv[1], NULL); + } + /* Print the current base address. */ + printf_P(PSTR("Base Address: 0x%05lx\n"), base_address); + return CMD_RET_SUCCESS; +} + +command_ret_t do_mem_loop(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, + char * const argv[]) +{ + uint32_t addr, length; + + (void) cmdtp; + (void) flag; + + if (argc < 3) + return CMD_RET_USAGE; + + /* Address is always specified. */ + addr = eval_arg(argv[1], NULL); + + /* Length is the number of bytes. */ + length = eval_arg(argv[2], NULL); + + + /* We want to optimize the loops to run as fast as possible. + * If we have only one object, just run infinite loops. + */ + if (length == 1) { + z80_bus_request_or_exit(); + cli(); + for (;;) + z80_read(addr); + } + + z80_bus_request_or_exit(); + cli(); + for (;;) { + uint32_t i = length; + uint32_t p = addr; + while (i-- > 0) + z80_read(p++); + } + + return CMD_RET_SUCCESS; +} + +command_ret_t do_mem_loopw (cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[]) +{ + uint32_t addr, length; + uint8_t data; + + (void) cmdtp; + (void) flag; + + if (argc < 4) + return CMD_RET_USAGE; + + /* Address is always specified. */ + addr = eval_arg(argv[1], NULL); + + /* Length is the number of bytes. */ + length = eval_arg(argv[2], NULL); + + data = eval_arg(argv[3], NULL); + + /* + * We want to optimize the loops to run as fast as possible. + * If we have only one object, just run infinite loops. + */ + if (length == 1) { + z80_bus_request_or_exit(); + cli(); + for (;;) + z80_write(addr, data); + } + + z80_bus_request_or_exit(); + cli(); + for (;;) { + uint32_t i = length; + uint32_t p = addr; + while (i-- > 0) + z80_write(p++, data); + } +} + +//#define CONFIG_SYS_ALT_MEMTEST + +#ifdef CONFIG_CMD_MEMTEST +static uint32_t mem_test_alt(uint32_t start_addr, uint32_t end_addr) +{ + uint32_t addr; + uint32_t dummy; + uint32_t errs = 0; + uint32_t offset; + uint32_t test_offset; + uint8_t pattern; + uint8_t anti_pattern; + uint8_t temp; + uint32_t num_bytes; + + static const FLASH uint8_t bitpattern[] = { + 0x01, /* single bit */ + 0x03, /* two adjacent bits */ + 0x07, /* three adjacent bits */ + 0x0F, /* four adjacent bits */ + 0x05, /* two non-adjacent bits */ + 0x15, /* three non-adjacent bits */ + 0x55, /* four non-adjacent bits */ + 0xaa, /* alternating 1/0 */ + }; + + /* + * Data line test: write a pattern to the first + * location, write the 1's complement to a 'parking' + * address (changes the state of the data bus so a + * floating bus doesn't give a false OK), and then + * read the value back. Note that we read it back + * into a variable because the next time we read it, + * it might be right (been there, tough to explain to + * the quality guys why it prints a failure when the + * "is" and "should be" are obviously the same in the + * error message). + * + * Rather than exhaustively testing, we test some + * patterns by shifting '1' bits through a field of + * '0's and '0' bits through a field of '1's (i.e. + * pattern and ~pattern). + */ + addr = start_addr; + dummy = start_addr+1; + for (unsigned int j = 0; j < ARRAY_SIZE(bitpattern); j++) { + pattern = bitpattern[j]; + for (; pattern != 0; pattern <<= 1) { + anti_pattern = ~pattern; + z80_write(addr, pattern); + z80_write(dummy, anti_pattern); /* clear the test data off the bus */ + temp = z80_read(addr); + if (temp != pattern) { + printf_P(PSTR("FAILURE (data line): " + "expected %02x, actual %02x\n"), + pattern, temp); + errs++; + } + z80_write(addr, anti_pattern); + z80_write(dummy, pattern); /* clear the test data off the bus */ + temp = z80_read(addr); + if (temp != anti_pattern) { + printf_P(PSTR("FAILURE (data line): " + "Is %02x, should be %02x\n"), + temp, anti_pattern); + errs++; + } + } + + if (ctrlc()) + return -1; + } + + if (errs) + return errs; + + /* + * Based on code whose Original Author and Copyright + * information follows: Copyright (c) 1998 by Michael + * Barr. This software is placed into the public + * domain and may be used for any purpose. However, + * this notice must not be changed or removed and no + * warranty is either expressed or implied by its + * publication or distribution. + */ + + /* + * Address line test + + * Description: Test the address bus wiring in a + * memory region by performing a walking + * 1's test on the relevant bits of the + * address and checking for aliasing. + * This test will find single-bit + * address failures such as stuck-high, + * stuck-low, and shorted pins. The base + * address and size of the region are + * selected by the caller. + + * Notes: For best results, the selected base + * address should have enough LSB 0's to + * guarantee single address bit changes. + * For example, to test a 64-Kbyte + * region, select a base address on a + * 64-Kbyte boundary. Also, select the + * region size as a power-of-two if at + * all possible. + * + * Returns: 0 if the test succeeds, 1 if the test fails. + */ + + num_bytes = (end_addr - start_addr) / sizeof(uint8_t); + + pattern = 0xaa; + anti_pattern = 0x55; + +// debug("## %s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_bytes); + /* + * Write the default pattern at each of the + * power-of-two offsets. + */ + for (offset = 1; offset < num_bytes; offset <<= 1) + z80_write(addr+offset, pattern); + + /* + * Check for address bits stuck high. + */ + z80_write(start_addr, anti_pattern); + + for (offset = 1; offset < num_bytes; offset <<= 1) { + temp = z80_read(start_addr + offset); + if (temp != pattern) { + printf_P(PSTR("FAILURE: Address bit stuck high @ 0x%.5lx:" + " expected 0x%.2x, actual 0x%.2x\n"), + start_addr + offset, pattern, temp); + errs++; + if (ctrlc()) + return -1; + } + } + z80_write(start_addr, pattern); + + /* + * Check for addr bits stuck low or shorted. + */ + for (test_offset = 1; test_offset < num_bytes; test_offset <<= 1) { + z80_write(start_addr + test_offset, anti_pattern); + + for (offset = 1; offset < num_bytes; offset <<= 1) { + temp = z80_read(start_addr + offset); + if ((temp != pattern) && (offset != test_offset)) { + printf_P(PSTR("FAILURE: Address bit stuck low or shorted" + " @ 0x%.5lx: expected 0x%.2x, actual 0x%.2x\n"), + start_addr + offset, pattern, temp); + errs++; + if (ctrlc()) + return -1; + } + } + z80_write(start_addr + test_offset, pattern); + } + + if (errs) + return errs; + + /* + * Description: Test the integrity of a physical + * memory device by performing an + * increment/decrement test over the + * entire region. In the process every + * storage bit in the device is tested + * as a zero and a one. The base address + * and the size of the region are + * selected by the caller. + * + * Returns: 0 if the test succeeds, 1 if the test fails. + */ + num_bytes++; + + /* + * Fill memory with a known pattern. + */ + for (pattern = 1, addr = start_addr; addr <= end_addr; pattern++, addr++) + z80_write(addr, pattern); + + /* + * Check each location and invert it for the second pass. + */ + for (pattern = 1, addr = start_addr; addr <= end_addr; pattern++, addr++) { + temp = z80_read(addr); + if (temp != pattern) { + printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:" + " expected 0x%.2x, actual 0x%.2x)\n"), + addr, pattern, temp); + errs++; + if (ctrlc()) + return -1; + } + + anti_pattern = ~pattern; + z80_write(addr, anti_pattern); + } + + /* + * Check each location for the inverted pattern and zero it. + */ + for (pattern = 1, addr = start_addr; addr <= end_addr; pattern++, addr++) { + anti_pattern = ~pattern; + temp = z80_read(addr); + if (temp != anti_pattern) { + printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:" + " expected 0x%.2x, actual 0x%.2x)\n"), + start_addr, anti_pattern, temp); + errs++; + if (ctrlc()) + return -1; + } + z80_write(addr, 0); + } + + return errs; +} + +/* + * Perform a memory test. A more complete alternative test can be + * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until + * interrupted by ctrl-c or by a failure of one of the sub-tests. + */ +command_ret_t do_mem_mtest(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, + char * const argv[]) +{ + uint32_t start = 0; + uint32_t end; + unsigned int iteration_limit = 0; + unsigned int iteration; + uint32_t errs = 0; /* number of errors */ + int ret; + + (void) cmdtp; + (void) flag; + + if (argc > 1) + start = eval_arg(argv[1], NULL); + + if (argc > 2) + end = eval_arg(argv[2], NULL); + else + end = CONFIG_SYS_RAMSIZE_MAX - 1; + + if (argc > 3) + iteration_limit = (unsigned int) eval_arg(argv[3], NULL); + + printf_P(PSTR("Testing %05lx ... %05lx:\n"), start, end); +// debug("## %s:%d: start %#05lx end %#05lx\n", __func__, __LINE__, start, end); + + clear_ctrlc(); /* forget any previous Control C */ + + for (iteration = 0; + !iteration_limit || iteration < iteration_limit; + iteration++) { + + printf_P(PSTR("Iteration: %6d\r"), iteration + 1); +// debug("\n"); + + z80_bus_request_or_exit(); + errs += mem_test_alt(start, end); + z80_bus_cmd(Release); + + if (had_ctrlc() || ctrlc()) { + break; + } + } + + if (had_ctrlc()) { + /* Memory test was aborted - write a newline to finish off */ + putchar('\n'); + ret = CMD_RET_FAILURE; + } else { + printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"), + iteration, errs); + ret = errs ? CMD_RET_FAILURE : CMD_RET_SUCCESS; + } + + return ret; +} +#endif /* CONFIG_CMD_MEMTEST */ |