]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
21 #include "cli_readline.h"
22 #include "con-utils.h"
26 * TODO: printf() --> printf_P()
29 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
30 #define CONFIG_SYS_MEMTEST_SCRATCH 0
33 static int mod_mem(cmd_tbl_t
*, int, int, int, char * const []);
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 void print_blanks(uint_fast8_t count
)
52 int z180_dump_mem(uint32_t startaddr
, uint32_t len
, const char *title
)
56 uint8_t pre
= startaddr
% 16;
57 uint32_t addr
= startaddr
& ~0x0f;
62 printf_P(PSTR("%s\n"),title
);
69 for (i
= pre
; i
< llen
; i
++)
70 buf
[i
] = z80_read(addr
+ i
);
73 printf_P(PSTR("%.5lx:"), addr
);
75 print_blanks(3 * pre
);
77 /* Print hex values */
78 for (i
= pre
; i
< llen
; i
++)
79 printf_P(PSTR(" %.2x"), buf
[i
]);
81 for (i
= 0; i
< llen
; i
++) {
85 printf_P(PSTR(".. "));
87 printf_P(PSTR("%.2x "), buf
[i
]);
90 /* fill line with whitespace for nice ASCII print */
92 print_blanks(3 * (16u - i
) + (16u-i
)/8 + 1 + pre
);
96 /* Print data in ASCII characters */
97 for (i
= pre
; i
< llen
; i
++)
98 printf_P(PSTR("%c"), isprint(buf
[i
]) ? buf
[i
] : '.');
112 /*--------------------------------------------------------------------------*/
119 int do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
121 uint32_t addr
, length
;
126 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
127 for (int i
= 0; i
< argc
; i
++) {
128 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
133 /* We use the last specified parameters, unless new ones are
137 length
= dp_last_length
;
140 return CMD_RET_USAGE
;
142 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
143 /* Address is specified since argc > 1 */
144 addr
= strtoul(argv
[1], NULL
, 16);
145 addr
+= base_address
;
147 /* If another parameter, it is the length to display. */
149 length
= strtoul(argv
[2], NULL
, 16);
152 /* Print the lines. */
153 z180_dump_mem(addr
, length
, NULL
);
155 dp_last_addr
= addr
+ length
;
156 dp_last_length
= length
;
160 int do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
162 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
164 int do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
166 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
169 int do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
172 uint32_t addr
, count
;
177 if ((argc
< 3) || (argc
> 4))
178 return CMD_RET_USAGE
;
180 /* Address is specified since argc > 1
182 addr
= strtoul(argv
[1], NULL
, 16);
183 addr
+= base_address
;
185 /* Get the value to write.
187 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
191 count
= strtoul(argv
[3], NULL
, 16);
197 while (count
-- > 0) {
198 z80_write(addr
, writeval
);
206 #ifdef CONFIG_MX_CYCLIC
207 int do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
213 return CMD_RET_USAGE
;
215 count
= strtoul(argv
[3], NULL
, 10);
218 do_mem_md (NULL
, 0, 3, argv
);
220 /* delay for <count> ms... */
221 /* TODO: use timer */
222 for (i
=0; i
<count
; i
++)
225 /* check for ctrl-c to abort... */
235 int do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
241 return CMD_RET_USAGE
;
243 count
= strtoul(argv
[3], NULL
, 10);
246 do_mem_mw (NULL
, 0, 3, argv
);
248 /* delay for <count> ms... */
249 /* TODO: use timer */
250 for (i
=0; i
<count
; i
++)
253 /* check for ctrl-c to abort... */
262 #endif /* CONFIG_MX_CYCLIC */
264 int do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
266 uint32_t addr1
, addr2
, count
, ngood
;
268 uint8_t byte1
, byte2
;
274 return CMD_RET_USAGE
;
277 addr1
= strtoul(argv
[1], NULL
, 16);
278 addr1
+= base_address
;
279 addr2
= strtoul(argv
[2], NULL
, 16);
280 addr2
+= base_address
;
281 count
= strtoul(argv
[3], NULL
, 16);
283 for (ngood
= 0; ngood
< count
; ++ngood
) {
285 byte1
= z80_read(addr1
);
286 byte2
= z80_read(addr2
);
288 if (byte1
!= byte2
) {
289 printf( "byte at 0x%05lx (%#02x) != "
290 "byte at 0x%05lx (%#02x)\n",
291 addr1
, byte1
, addr2
, byte2
);
298 /* check for ctrl-c to abort... */
305 printf("Total of %ld byte(s) (0x%lx) were the same\n", ngood
, ngood
);
309 int do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
311 uint32_t src
, dest
, count
;
318 return CMD_RET_USAGE
;
320 src
= strtoul(argv
[1], NULL
, 16);
322 dest
= strtoul(argv
[2], NULL
, 16);
323 dest
+= base_address
;
324 count
= strtoul(argv
[3], NULL
, 16);
327 my_puts ("Zero length ???\n");
338 while (count
-- > 0) {
341 data
= z80_read(src
);
342 z80_write(dest
, data
);
347 /* check for ctrl-c to abort... */
356 int do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
363 /* Set new base address. */
364 base_address
= strtoul(argv
[1], NULL
, 16);
366 /* Print the current base address. */
367 printf("Base Address: 0x%05lx\n", base_address
);
371 int do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
374 uint32_t addr
, length
;
380 return CMD_RET_USAGE
;
382 /* Address is always specified. */
383 addr
= strtoul(argv
[1], NULL
, 16);
385 /* Length is the number of bytes. */
386 length
= strtoul(argv
[2], NULL
, 16);
389 /* We want to optimize the loops to run as fast as possible.
390 * If we have only one object, just run infinite loops.
412 int do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
414 uint32_t addr
, length
;
421 return CMD_RET_USAGE
;
423 /* Address is always specified. */
424 addr
= strtoul(argv
[1], NULL
, 16);
426 /* Length is the number of bytes. */
427 length
= strtoul(argv
[2], NULL
, 16);
429 data
= strtoul(argv
[3], NULL
, 16);
431 /* We want to optimize the loops to run as fast as possible.
432 * If we have only one object, just run infinite loops.
437 z80_write(addr
, data
);
444 z80_write(p
++, data
);
447 #endif /* CONFIG_LOOPW */
449 #ifdef CONFIG_CMD_MEMTEST
450 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
455 uint32_t val
, readback
;
461 vu_long anti_pattern
;
463 static const FLASH
uint32_t bitpattern
[] = {
464 0x00000001, /* single bit */
465 0x00000003, /* two adjacent bits */
466 0x00000007, /* three adjacent bits */
467 0x0000000F, /* four adjacent bits */
468 0x00000005, /* two non-adjacent bits */
469 0x00000015, /* three non-adjacent bits */
470 0x00000055, /* four non-adjacent bits */
471 0xaaaaaaaa, /* alternating 1/0 */
474 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
477 * Data line test: write a pattern to the first
478 * location, write the 1's complement to a 'parking'
479 * address (changes the state of the data bus so a
480 * floating bus doesn't give a false OK), and then
481 * read the value back. Note that we read it back
482 * into a variable because the next time we read it,
483 * it might be right (been there, tough to explain to
484 * the quality guys why it prints a failure when the
485 * "is" and "should be" are obviously the same in the
488 * Rather than exhaustively testing, we test some
489 * patterns by shifting '1' bits through a field of
490 * '0's and '0' bits through a field of '1's (i.e.
491 * pattern and ~pattern).
494 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
496 for (; val
!= 0; val
<<= 1) {
498 *dummy
= ~val
; /* clear the test data off the bus */
500 if (readback
!= val
) {
501 printf("FAILURE (data line): "
502 "expected %05lx, actual %05lx\n",
511 if (readback
!= ~val
) {
512 printf("FAILURE (data line): "
513 "Is %05lx, should be %05lx\n",
523 * Based on code whose Original Author and Copyright
524 * information follows: Copyright (c) 1998 by Michael
525 * Barr. This software is placed into the public
526 * domain and may be used for any purpose. However,
527 * this notice must not be changed or removed and no
528 * warranty is either expressed or implied by its
529 * publication or distribution.
535 * Description: Test the address bus wiring in a
536 * memory region by performing a walking
537 * 1's test on the relevant bits of the
538 * address and checking for aliasing.
539 * This test will find single-bit
540 * address failures such as stuck-high,
541 * stuck-low, and shorted pins. The base
542 * address and size of the region are
543 * selected by the caller.
545 * Notes: For best results, the selected base
546 * address should have enough LSB 0's to
547 * guarantee single address bit changes.
548 * For example, to test a 64-Kbyte
549 * region, select a base address on a
550 * 64-Kbyte boundary. Also, select the
551 * region size as a power-of-two if at
554 * Returns: 0 if the test succeeds, 1 if the test fails.
556 pattern
= (vu_long
) 0xaaaaaaaa;
557 anti_pattern
= (vu_long
) 0x55555555;
559 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
561 * Write the default pattern at each of the
562 * power-of-two offsets.
564 for (offset
= 1; offset
< num_words
; offset
<<= 1)
565 addr
[offset
] = pattern
;
568 * Check for address bits stuck high.
571 addr
[test_offset
] = anti_pattern
;
573 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
575 if (temp
!= pattern
) {
576 printf("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
577 " expected 0x%.5lx, actual 0x%.5lx\n",
578 start_addr
+ offset
*sizeof(vu_long
),
585 addr
[test_offset
] = pattern
;
588 * Check for addr bits stuck low or shorted.
590 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
591 addr
[test_offset
] = anti_pattern
;
593 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
595 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
596 printf("\nFAILURE: Address bit stuck low or"
597 " shorted @ 0x%.5lx: expected 0x%.5lx,"
599 start_addr
+ offset
*sizeof(vu_long
),
606 addr
[test_offset
] = pattern
;
610 * Description: Test the integrity of a physical
611 * memory device by performing an
612 * increment/decrement test over the
613 * entire region. In the process every
614 * storage bit in the device is tested
615 * as a zero and a one. The base address
616 * and the size of the region are
617 * selected by the caller.
619 * Returns: 0 if the test succeeds, 1 if the test fails.
624 * Fill memory with a known pattern.
626 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
627 addr
[offset
] = pattern
;
631 * Check each location and invert it for the second pass.
633 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
635 if (temp
!= pattern
) {
636 printf("\nFAILURE (read/write) @ 0x%.5lx:"
637 " expected 0x%.5lx, actual 0x%.5lx)\n",
638 start_addr
+ offset
*sizeof(vu_long
),
645 anti_pattern
= ~pattern
;
646 addr
[offset
] = anti_pattern
;
650 * Check each location for the inverted pattern and zero it.
652 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
654 anti_pattern
= ~pattern
;
656 if (temp
!= anti_pattern
) {
657 printf("\nFAILURE (read/write): @ 0x%.5lx:"
658 " expected 0x%.5lx, actual 0x%.5lx)\n",
659 start_addr
+ offset
*sizeof(vu_long
),
671 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
672 vu_long pattern
, int iteration
)
677 uint32_t incr
, length
;
678 uint32_t val
, readback
;
680 /* Alternate the pattern */
685 * Flip the pattern each time to make lots of zeros and
686 * then, the next time, lots of ones. We decrement
687 * the "negative" patterns and increment the "positive"
688 * patterns to preserve this feature.
690 if (pattern
& 0x80000000)
691 pattern
= -pattern
; /* complement & increment */
695 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
697 printf("\rPattern %08lX Writing..."
699 "\b\b\b\b\b\b\b\b\b\b",
702 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
707 my_puts("Reading...");
709 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
711 if (readback
!= val
) {
712 uint32_t offset
= addr
- buf
;
714 printf("\nMem error @ 0x%08X: "
715 "found %08lX, expected %08lX\n",
716 (unsigned int)(uintptr_t)(start_addr
+ offset
*sizeof(vu_long
)),
729 * Perform a memory test. A more complete alternative test can be
730 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
731 * interrupted by ctrl-c or by a failure of one of the sub-tests.
733 int do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
737 vu_long
*buf
, *dummy
;
740 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
743 #if defined(CONFIG_SYS_ALT_MEMTEST)
744 const int alt_test
= 1;
746 const int alt_test
= 0;
750 start
= strtoul(argv
[1], NULL
, 16);
752 start
= CONFIG_SYS_MEMTEST_START
;
755 end
= strtoul(argv
[2], NULL
, 16);
757 end
= CONFIG_SYS_MEMTEST_END
;
760 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
765 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
769 printf("Testing %08x ... %08x:\n", (unsigned int)start
, (unsigned int)end
);
770 debug("%s:%d: start %#05lx end %#05lx\n", __func__
, __LINE__
,
774 // buf = map_sysmem(start, end - start);
775 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
777 !iteration_limit
|| iteration
< iteration_limit
;
784 printf("Iteration: %6d\r", iteration
+ 1);
787 errs
= mem_test_alt(buf
, start
, end
, dummy
);
789 errs
= mem_test_quick(buf
, start
, end
, pattern
,
797 /* Memory test was aborted - write a newline to finish off */
801 printf("Tested %d iteration(s) with %lu errors.\n",
806 return ret
; /* not reached */
808 #endif /* CONFIG_CMD_MEMTEST */
817 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
826 return CMD_RET_USAGE
;
828 /* We use the last specified parameters, unless new ones are
833 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
834 /* New command specified.
837 /* Address is specified since argc > 1
839 addr
= strtoul(argv
[1], NULL
, 16);
840 addr
+= base_address
;
843 /* Print the address, followed by value. Then accept input for
844 * the next value. A non-converted value exits.
848 data
= z80_read(addr
);
849 printf("%05lx: %02x", addr
, data
);
852 nbytes
= cli_readline(PSTR(" ? "));
853 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
854 /* <CR> pressed as only input, don't modify current
855 * location and move to next. "-" pressed will go back.
858 addr
+= nbytes
? -1 : 1;
863 data
= strtoul(console_buffer
, &endp
, 16);
864 nbytes
= endp
- console_buffer
;
867 z80_write(addr
, data
);