]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
2 * (C) Copyright 2014,2018 Leo C. <erbl259-lmu@yahoo.de>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0
13 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
18 #include <avr/interrupt.h>
21 #include "cli_readline.h"
22 #include "print-utils.h"
23 #include "con-utils.h"
24 #include "getopt-min.h"
31 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
32 #define CONFIG_SYS_MEMTEST_SCRATCH 0
35 /* Display values from last command.
36 * Memory modify remembered values are different from display memory.
38 static uint32_t dp_last_addr
;
39 static uint32_t dp_last_length
= 0x100;
40 static uint32_t mm_last_addr
;
42 static uint32_t base_address
= 0;
44 /*--------------------------------------------------------------------------*/
46 static ERRNUM
z180_read_buf(uint8_t *buf
, uint32_t addr
, uint8_t count
)
48 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
))
51 z80_read_block (buf
, addr
, count
);
56 /*--------------------------------------------------------------------------*/
63 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
65 uint32_t addr
, length
;
70 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
71 for (int i
= 0; i
< argc
; i
++) {
72 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
77 /* We use the last specified parameters, unless new ones are
81 length
= dp_last_length
;
86 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
87 /* Address is specified since argc > 1 */
88 addr
= eval_arg(argv
[1], NULL
);
91 /* If another parameter, it is the length to display. */
93 length
= eval_arg(argv
[2], NULL
);
96 /* Print the lines. */
97 ERRNUM ret
= dump_mem(addr
, addr
, length
, z180_read_buf
, NULL
);
99 cmd_error(CMD_RET_FAILURE
, ret
, NULL
);
101 if (ret
== ESUCCESS
) {
102 dp_last_addr
= addr
+ length
;
103 dp_last_length
= length
;
105 return CMD_RET_SUCCESS
;
115 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, uint_fast8_t flag
, int argc
, char * const argv
[])
124 return CMD_RET_USAGE
;
126 /* We use the last specified parameters, unless new ones are
131 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
132 /* New command specified.
135 /* Address is specified since argc > 1
137 addr
= eval_arg(argv
[1], NULL
);
138 addr
+= base_address
;
141 /* Print the address, followed by value. Then accept input for
142 * the next value. A non-converted value exits.
145 z80_bus_request_or_exit();
146 data
= z80_read(addr
);
147 z80_bus_cmd(Release
);
148 printf_P(PSTR("%05lx: %02x"), addr
, data
);
150 nbytes
= cli_readline(PSTR(" ? "), 0);
151 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
152 /* <CR> pressed as only input, don't modify current
153 * location and move to next. "-" pressed will go back.
156 addr
+= nbytes
? -1 : 1;
161 data
= eval_arg(console_buffer
, &endp
);
162 nbytes
= endp
- console_buffer
;
164 z80_bus_request_or_exit();
165 z80_write(addr
, data
);
166 z80_bus_cmd(Release
);
171 } while (nbytes
> 0);
174 return CMD_RET_SUCCESS
;
178 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
180 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
182 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
184 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
187 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
192 uint_fast8_t width
= 1;
194 (void) cmdtp
; (void) flag
;
200 while ((opt
= getopt(argc
, argv
, PSTR("bwl"))) != -1) {
212 return CMD_RET_USAGE
;
216 /* remaining arguments */
218 if ((argc
< 2) || (argc
> 3))
219 return CMD_RET_USAGE
;
221 /* Address and value are specified since (adjusted) argc >= 2 */
222 addr
= eval_arg(argv
[optind
++], NULL
);
223 addr
+= base_address
;
224 writeval
= eval_arg(argv
[optind
++], NULL
);
228 count
= eval_arg(argv
[optind
], NULL
);
230 z80_bus_request_or_exit();
232 z80_memset(addr
, writeval
, count
);
235 z80_write_block((const uint8_t *) &writeval
, addr
, width
);
239 z80_bus_cmd(Release
);
241 return CMD_RET_SUCCESS
;
244 #ifdef CONFIG_MX_CYCLIC
245 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
254 if (argv
[0][1] != 'd') {
256 while ((opt
= getopt(argc
, argv
, PSTR("bwl"))) != -1)
258 return CMD_RET_USAGE
;
262 if (argc
-optind
!= 4)
263 return CMD_RET_USAGE
;
265 count
= eval_arg(argv
[optind
+ 3], NULL
);
267 clear_ctrlc(); /* forget any previous Control C */
270 if (argv
[0][1] == 'd')
271 do_mem_md (NULL
, 0, argc
-1, argv
); /* memory display */
273 do_mem_mw (NULL
, 0, argc
-1, argv
); /* memory write */
276 /* delay for <count> ms... */
279 /* check for ctrl-c to abort... */
280 if (had_ctrlc() || ctrlc()) {
281 my_puts_P(PSTR("Abort\n"));
282 return CMD_RET_SUCCESS
;
284 } while (get_timer(ts
) < count
);
287 return CMD_RET_SUCCESS
;
289 #endif /* CONFIG_MX_CYCLIC */
291 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
293 uint32_t addr1
, addr2
, count
, ngood
;
294 command_ret_t rcode
= CMD_RET_SUCCESS
;
295 uint8_t byte1
, byte2
;
301 return CMD_RET_USAGE
;
304 addr1
= eval_arg(argv
[1], NULL
);
305 addr1
+= base_address
;
306 addr2
= eval_arg(argv
[2], NULL
);
307 addr2
+= base_address
;
308 count
= eval_arg(argv
[3], NULL
);
310 for (ngood
= 0; ngood
< count
; ++ngood
) {
311 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
312 my_puts_P(PSTR("Bus timeout\n"));
313 rcode
= CMD_RET_FAILURE
;
316 byte1
= z80_read(addr1
);
317 byte2
= z80_read(addr2
);
318 z80_bus_cmd(Release
);
319 if (byte1
!= byte2
) {
320 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
321 "byte at 0x%05lx (%#02x)\n"),
322 addr1
, byte1
, addr2
, byte2
);
323 rcode
= CMD_RET_FAILURE
;
329 /* check for ctrl-c to abort... */
331 my_puts_P(PSTR("Abort\n"));
332 return CMD_RET_SUCCESS
;
336 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
340 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
342 uint32_t src
, dest
, count
;
349 return CMD_RET_USAGE
;
351 src
= eval_arg(argv
[1], NULL
);
353 dest
= eval_arg(argv
[2], NULL
);
354 dest
+= base_address
;
355 count
= eval_arg(argv
[3], NULL
);
358 my_puts_P(PSTR("Zero length?\n"));
359 return CMD_RET_FAILURE
;
369 while (count
-- > 0) {
371 z80_bus_request_or_exit();
372 data
= z80_read(src
);
373 z80_write(dest
, data
);
374 z80_bus_cmd(Release
);
378 /* check for ctrl-c to abort... */
380 my_puts_P(PSTR("Abort\n"));
381 return CMD_RET_SUCCESS
;
384 return CMD_RET_SUCCESS
;
387 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
,
394 /* Set new base address. */
395 base_address
= eval_arg(argv
[1], NULL
);
397 /* Print the current base address. */
398 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
399 return CMD_RET_SUCCESS
;
402 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
,
405 uint32_t addr
, length
;
411 return CMD_RET_USAGE
;
413 /* Address is always specified. */
414 addr
= eval_arg(argv
[1], NULL
);
416 /* Length is the number of bytes. */
417 length
= eval_arg(argv
[2], NULL
);
420 /* We want to optimize the loops to run as fast as possible.
421 * If we have only one object, just run infinite loops.
424 z80_bus_request_or_exit();
430 z80_bus_request_or_exit();
439 return CMD_RET_SUCCESS
;
442 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
444 uint32_t addr
, length
;
451 return CMD_RET_USAGE
;
453 /* Address is always specified. */
454 addr
= eval_arg(argv
[1], NULL
);
456 /* Length is the number of bytes. */
457 length
= eval_arg(argv
[2], NULL
);
459 data
= eval_arg(argv
[3], NULL
);
462 * We want to optimize the loops to run as fast as possible.
463 * If we have only one object, just run infinite loops.
466 z80_bus_request_or_exit();
469 z80_write(addr
, data
);
472 z80_bus_request_or_exit();
478 z80_write(p
++, data
);
482 //#define CONFIG_SYS_ALT_MEMTEST
484 #ifdef CONFIG_CMD_MEMTEST
485 static uint32_t mem_test_alt(uint32_t start_addr
, uint32_t end_addr
)
491 uint32_t test_offset
;
493 uint8_t anti_pattern
;
497 static const FLASH
uint8_t bitpattern
[] = {
498 0x01, /* single bit */
499 0x03, /* two adjacent bits */
500 0x07, /* three adjacent bits */
501 0x0F, /* four adjacent bits */
502 0x05, /* two non-adjacent bits */
503 0x15, /* three non-adjacent bits */
504 0x55, /* four non-adjacent bits */
505 0xaa, /* alternating 1/0 */
509 * Data line test: write a pattern to the first
510 * location, write the 1's complement to a 'parking'
511 * address (changes the state of the data bus so a
512 * floating bus doesn't give a false OK), and then
513 * read the value back. Note that we read it back
514 * into a variable because the next time we read it,
515 * it might be right (been there, tough to explain to
516 * the quality guys why it prints a failure when the
517 * "is" and "should be" are obviously the same in the
520 * Rather than exhaustively testing, we test some
521 * patterns by shifting '1' bits through a field of
522 * '0's and '0' bits through a field of '1's (i.e.
523 * pattern and ~pattern).
526 dummy
= start_addr
+1;
527 for (unsigned int j
= 0; j
< ARRAY_SIZE(bitpattern
); j
++) {
528 pattern
= bitpattern
[j
];
529 for (; pattern
!= 0; pattern
<<= 1) {
530 anti_pattern
= ~pattern
;
531 z80_write(addr
, pattern
);
532 z80_write(dummy
, anti_pattern
); /* clear the test data off the bus */
533 temp
= z80_read(addr
);
534 if (temp
!= pattern
) {
535 printf_P(PSTR("FAILURE (data line): "
536 "expected %02x, actual %02x\n"),
540 z80_write(addr
, anti_pattern
);
541 z80_write(dummy
, pattern
); /* clear the test data off the bus */
542 temp
= z80_read(addr
);
543 if (temp
!= anti_pattern
) {
544 printf_P(PSTR("FAILURE (data line): "
545 "Is %02x, should be %02x\n"),
559 * Based on code whose Original Author and Copyright
560 * information follows: Copyright (c) 1998 by Michael
561 * Barr. This software is placed into the public
562 * domain and may be used for any purpose. However,
563 * this notice must not be changed or removed and no
564 * warranty is either expressed or implied by its
565 * publication or distribution.
571 * Description: Test the address bus wiring in a
572 * memory region by performing a walking
573 * 1's test on the relevant bits of the
574 * address and checking for aliasing.
575 * This test will find single-bit
576 * address failures such as stuck-high,
577 * stuck-low, and shorted pins. The base
578 * address and size of the region are
579 * selected by the caller.
581 * Notes: For best results, the selected base
582 * address should have enough LSB 0's to
583 * guarantee single address bit changes.
584 * For example, to test a 64-Kbyte
585 * region, select a base address on a
586 * 64-Kbyte boundary. Also, select the
587 * region size as a power-of-two if at
590 * Returns: 0 if the test succeeds, 1 if the test fails.
593 num_bytes
= (end_addr
- start_addr
) / sizeof(uint8_t);
598 // debug("## %s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_bytes);
600 * Write the default pattern at each of the
601 * power-of-two offsets.
603 for (offset
= 1; offset
< num_bytes
; offset
<<= 1)
604 z80_write(addr
+offset
, pattern
);
607 * Check for address bits stuck high.
609 z80_write(start_addr
, anti_pattern
);
611 for (offset
= 1; offset
< num_bytes
; offset
<<= 1) {
612 temp
= z80_read(start_addr
+ offset
);
613 if (temp
!= pattern
) {
614 printf_P(PSTR("FAILURE: Address bit stuck high @ 0x%.5lx:"
615 " expected 0x%.2x, actual 0x%.2x\n"),
616 start_addr
+ offset
, pattern
, temp
);
622 z80_write(start_addr
, pattern
);
625 * Check for addr bits stuck low or shorted.
627 for (test_offset
= 1; test_offset
< num_bytes
; test_offset
<<= 1) {
628 z80_write(start_addr
+ test_offset
, anti_pattern
);
630 for (offset
= 1; offset
< num_bytes
; offset
<<= 1) {
631 temp
= z80_read(start_addr
+ offset
);
632 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
633 printf_P(PSTR("FAILURE: Address bit stuck low or shorted"
634 " @ 0x%.5lx: expected 0x%.2x, actual 0x%.2x\n"),
635 start_addr
+ offset
, pattern
, temp
);
641 z80_write(start_addr
+ test_offset
, pattern
);
648 * Description: Test the integrity of a physical
649 * memory device by performing an
650 * increment/decrement test over the
651 * entire region. In the process every
652 * storage bit in the device is tested
653 * as a zero and a one. The base address
654 * and the size of the region are
655 * selected by the caller.
657 * Returns: 0 if the test succeeds, 1 if the test fails.
662 * Fill memory with a known pattern.
664 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++)
665 z80_write(addr
, pattern
);
668 * Check each location and invert it for the second pass.
670 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++) {
671 temp
= z80_read(addr
);
672 if (temp
!= pattern
) {
673 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
674 " expected 0x%.2x, actual 0x%.2x)\n"),
675 addr
, pattern
, temp
);
681 anti_pattern
= ~pattern
;
682 z80_write(addr
, anti_pattern
);
686 * Check each location for the inverted pattern and zero it.
688 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++) {
689 anti_pattern
= ~pattern
;
690 temp
= z80_read(addr
);
691 if (temp
!= anti_pattern
) {
692 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
693 " expected 0x%.2x, actual 0x%.2x)\n"),
694 start_addr
, anti_pattern
, temp
);
706 * Perform a memory test. A more complete alternative test can be
707 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
708 * interrupted by ctrl-c or by a failure of one of the sub-tests.
710 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
,
715 unsigned int iteration_limit
= 0;
716 unsigned int iteration
;
717 uint32_t errs
= 0; /* number of errors */
724 start
= eval_arg(argv
[1], NULL
);
727 end
= eval_arg(argv
[2], NULL
);
729 end
= CONFIG_SYS_RAMSIZE_MAX
- 1;
732 iteration_limit
= (unsigned int) eval_arg(argv
[3], NULL
);
734 printf_P(PSTR("Testing %05lx ... %05lx:\n"), start
, end
);
735 // debug("## %s:%d: start %#05lx end %#05lx\n", __func__, __LINE__, start, end);
737 clear_ctrlc(); /* forget any previous Control C */
740 !iteration_limit
|| iteration
< iteration_limit
;
743 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
746 z80_bus_request_or_exit();
747 errs
+= mem_test_alt(start
, end
);
748 z80_bus_cmd(Release
);
750 if (had_ctrlc() || ctrlc()) {
756 /* Memory test was aborted - write a newline to finish off */
758 ret
= CMD_RET_FAILURE
;
760 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
762 ret
= errs
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
767 #endif /* CONFIG_CMD_MEMTEST */