]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
f1f34c05a030519ec152a458ac61706093222888
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 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
27 #define CONFIG_SYS_MEMTEST_SCRATCH 0
30 /* Display values from last command.
31 * Memory modify remembered values are different from display memory.
33 static uint32_t dp_last_addr
;
34 static uint32_t dp_last_length
= 0x100;
35 static uint32_t mm_last_addr
;
37 static uint32_t base_address
= 0;
39 /*--------------------------------------------------------------------------*/
41 static void print_blanks(uint_fast8_t count
)
47 int z180_dump_mem(uint32_t startaddr
, uint32_t len
, const char *title
)
51 uint8_t pre
= startaddr
% 16;
52 uint32_t addr
= startaddr
& ~0x0f;
57 printf_P(PSTR("%s\n"),title
);
64 for (i
= pre
; i
< llen
; i
++)
65 buf
[i
] = z80_read(addr
+ i
);
68 printf_P(PSTR("%.5lx:"), addr
);
70 print_blanks(3 * pre
);
72 /* Print hex values */
73 for (i
= pre
; i
< llen
; i
++)
74 printf_P(PSTR(" %.2x"), buf
[i
]);
76 for (i
= 0; i
< llen
; i
++) {
80 printf_P(PSTR(".. "));
82 printf_P(PSTR("%.2x "), buf
[i
]);
85 /* fill line with whitespace for nice ASCII print */
87 print_blanks(3 * (16u - i
) + (16u-i
)/8 + 1 + pre
);
91 /* Print data in ASCII characters */
92 for (i
= pre
; i
< llen
; i
++)
93 printf_P(PSTR("%c"), isprint(buf
[i
]) ? buf
[i
] : '.');
107 /*--------------------------------------------------------------------------*/
114 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
116 uint32_t addr
, length
;
121 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
122 for (int i
= 0; i
< argc
; i
++) {
123 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
128 /* We use the last specified parameters, unless new ones are
132 length
= dp_last_length
;
135 return CMD_RET_USAGE
;
137 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
138 /* Address is specified since argc > 1 */
139 addr
= strtoul(argv
[1], NULL
, 16);
140 addr
+= base_address
;
142 /* If another parameter, it is the length to display. */
144 length
= strtoul(argv
[2], NULL
, 16);
147 /* Print the lines. */
148 z180_dump_mem(addr
, length
, NULL
);
150 dp_last_addr
= addr
+ length
;
151 dp_last_length
= length
;
152 return CMD_RET_SUCCESS
;
162 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
171 return CMD_RET_USAGE
;
173 /* We use the last specified parameters, unless new ones are
178 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
179 /* New command specified.
182 /* Address is specified since argc > 1
184 addr
= strtoul(argv
[1], NULL
, 16);
185 addr
+= base_address
;
188 /* Print the address, followed by value. Then accept input for
189 * the next value. A non-converted value exits.
192 z80_bus_cmd(Request
);
193 data
= z80_read(addr
);
194 printf_P(PSTR("%05lx: %02x"), addr
, data
);
195 z80_bus_cmd(Release
);
197 nbytes
= cli_readline(PSTR(" ? "));
198 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
199 /* <CR> pressed as only input, don't modify current
200 * location and move to next. "-" pressed will go back.
203 addr
+= nbytes
? -1 : 1;
208 data
= strtoul(console_buffer
, &endp
, 16);
209 nbytes
= endp
- console_buffer
;
211 z80_bus_cmd(Request
);
212 z80_write(addr
, data
);
213 z80_bus_cmd(Release
);
221 return CMD_RET_SUCCESS
;
225 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
227 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
229 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
231 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
234 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
237 uint32_t addr
, count
;
242 if ((argc
< 3) || (argc
> 4))
243 return CMD_RET_USAGE
;
245 /* Address is specified since argc > 1
247 addr
= strtoul(argv
[1], NULL
, 16);
248 addr
+= base_address
;
250 /* Get the value to write.
252 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
256 count
= strtoul(argv
[3], NULL
, 16);
261 z80_bus_cmd(Request
);
262 while (count
-- > 0) {
263 z80_write(addr
, writeval
);
266 z80_bus_cmd(Release
);
268 return CMD_RET_SUCCESS
;
271 #ifdef CONFIG_MX_CYCLIC
272 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
278 return CMD_RET_USAGE
;
280 count
= strtoul(argv
[3], NULL
, 10);
283 do_mem_md (NULL
, 0, 3, argv
);
285 /* delay for <count> ms... */
286 /* TODO: use timer */
287 for (i
=0; i
<count
; i
++)
290 /* check for ctrl-c to abort... */
292 my_puts_P(PSTR("Abort\n"));
293 return CMD_RET_SUCCESS
;
297 return CMD_RET_SUCCESS
;
300 command_ret_t
do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
306 return CMD_RET_USAGE
;
308 count
= strtoul(argv
[3], NULL
, 10);
311 do_mem_mw (NULL
, 0, 3, argv
);
313 /* delay for <count> ms... */
314 /* TODO: use timer */
315 for (i
=0; i
<count
; i
++)
318 /* check for ctrl-c to abort... */
320 my_puts_P(PSTR("Abort\n"));
321 return CMD_RET_SUCCESS
;
325 return CMD_RET_SUCCESS
;
327 #endif /* CONFIG_MX_CYCLIC */
329 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
331 uint32_t addr1
, addr2
, count
, ngood
;
332 command_ret_t rcode
= CMD_RET_SUCCESS
;
333 uint8_t byte1
, byte2
;
339 return CMD_RET_USAGE
;
342 addr1
= strtoul(argv
[1], NULL
, 16);
343 addr1
+= base_address
;
344 addr2
= strtoul(argv
[2], NULL
, 16);
345 addr2
+= base_address
;
346 count
= strtoul(argv
[3], NULL
, 16);
348 for (ngood
= 0; ngood
< count
; ++ngood
) {
349 z80_bus_cmd(Request
);
350 byte1
= z80_read(addr1
);
351 byte2
= z80_read(addr2
);
352 z80_bus_cmd(Release
);
353 if (byte1
!= byte2
) {
354 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
355 "byte at 0x%05lx (%#02x)\n"),
356 addr1
, byte1
, addr2
, byte2
);
357 rcode
= CMD_RET_FAILURE
;
363 /* check for ctrl-c to abort... */
365 my_puts_P(PSTR("Abort\n"));
366 return CMD_RET_SUCCESS
;
370 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
374 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
376 uint32_t src
, dest
, count
;
383 return CMD_RET_USAGE
;
385 src
= strtoul(argv
[1], NULL
, 16);
387 dest
= strtoul(argv
[2], NULL
, 16);
388 dest
+= base_address
;
389 count
= strtoul(argv
[3], NULL
, 16);
392 my_puts_P(PSTR("Zero length?\n"));
393 return CMD_RET_FAILURE
;
403 while (count
-- > 0) {
405 z80_bus_cmd(Request
);
406 data
= z80_read(src
);
407 z80_write(dest
, data
);
408 z80_bus_cmd(Release
);
412 /* check for ctrl-c to abort... */
414 my_puts_P(PSTR("Abort\n"));
415 return CMD_RET_SUCCESS
;
418 return CMD_RET_SUCCESS
;
421 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
428 /* Set new base address. */
429 base_address
= strtoul(argv
[1], NULL
, 16);
431 /* Print the current base address. */
432 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
433 return CMD_RET_SUCCESS
;
436 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
439 uint32_t addr
, length
;
445 return CMD_RET_USAGE
;
447 /* Address is always specified. */
448 addr
= strtoul(argv
[1], NULL
, 16);
450 /* Length is the number of bytes. */
451 length
= strtoul(argv
[2], NULL
, 16);
454 /* We want to optimize the loops to run as fast as possible.
455 * If we have only one object, just run infinite loops.
458 z80_bus_cmd(Request
);
461 z80_bus_cmd(Release
);
464 z80_bus_cmd(Request
);
471 z80_bus_cmd(Release
);
473 return CMD_RET_SUCCESS
;
477 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
479 uint32_t addr
, length
;
486 return CMD_RET_USAGE
;
488 /* Address is always specified. */
489 addr
= strtoul(argv
[1], NULL
, 16);
491 /* Length is the number of bytes. */
492 length
= strtoul(argv
[2], NULL
, 16);
494 data
= strtoul(argv
[3], NULL
, 16);
496 /* We want to optimize the loops to run as fast as possible.
497 * If we have only one object, just run infinite loops.
500 z80_bus_cmd(Request
);
502 z80_write(addr
, data
);
509 z80_write(p
++, data
);
512 #endif /* CONFIG_LOOPW */
514 #ifdef CONFIG_CMD_MEMTEST
515 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
520 uint32_t val
, readback
;
526 vu_long anti_pattern
;
528 static const FLASH
uint32_t bitpattern
[] = {
529 0x00000001, /* single bit */
530 0x00000003, /* two adjacent bits */
531 0x00000007, /* three adjacent bits */
532 0x0000000F, /* four adjacent bits */
533 0x00000005, /* two non-adjacent bits */
534 0x00000015, /* three non-adjacent bits */
535 0x00000055, /* four non-adjacent bits */
536 0xaaaaaaaa, /* alternating 1/0 */
539 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
542 * Data line test: write a pattern to the first
543 * location, write the 1's complement to a 'parking'
544 * address (changes the state of the data bus so a
545 * floating bus doesn't give a false OK), and then
546 * read the value back. Note that we read it back
547 * into a variable because the next time we read it,
548 * it might be right (been there, tough to explain to
549 * the quality guys why it prints a failure when the
550 * "is" and "should be" are obviously the same in the
553 * Rather than exhaustively testing, we test some
554 * patterns by shifting '1' bits through a field of
555 * '0's and '0' bits through a field of '1's (i.e.
556 * pattern and ~pattern).
559 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
561 for (; val
!= 0; val
<<= 1) {
563 *dummy
= ~val
; /* clear the test data off the bus */
565 if (readback
!= val
) {
566 printf_P(PSTR("FAILURE (data line): "
567 "expected %05lx, actual %05lx\n"),
576 if (readback
!= ~val
) {
577 printf_P(PSTR("FAILURE (data line): "
578 "Is %05lx, should be %05lx\n"),
588 * Based on code whose Original Author and Copyright
589 * information follows: Copyright (c) 1998 by Michael
590 * Barr. This software is placed into the public
591 * domain and may be used for any purpose. However,
592 * this notice must not be changed or removed and no
593 * warranty is either expressed or implied by its
594 * publication or distribution.
600 * Description: Test the address bus wiring in a
601 * memory region by performing a walking
602 * 1's test on the relevant bits of the
603 * address and checking for aliasing.
604 * This test will find single-bit
605 * address failures such as stuck-high,
606 * stuck-low, and shorted pins. The base
607 * address and size of the region are
608 * selected by the caller.
610 * Notes: For best results, the selected base
611 * address should have enough LSB 0's to
612 * guarantee single address bit changes.
613 * For example, to test a 64-Kbyte
614 * region, select a base address on a
615 * 64-Kbyte boundary. Also, select the
616 * region size as a power-of-two if at
619 * Returns: 0 if the test succeeds, 1 if the test fails.
621 pattern
= (vu_long
) 0xaaaaaaaa;
622 anti_pattern
= (vu_long
) 0x55555555;
624 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
626 * Write the default pattern at each of the
627 * power-of-two offsets.
629 for (offset
= 1; offset
< num_words
; offset
<<= 1)
630 addr
[offset
] = pattern
;
633 * Check for address bits stuck high.
636 addr
[test_offset
] = anti_pattern
;
638 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
640 if (temp
!= pattern
) {
641 printf_P(PSTR("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
642 " expected 0x%.5lx, actual 0x%.5lx\n"),
643 start_addr
+ offset
*sizeof(vu_long
),
650 addr
[test_offset
] = pattern
;
653 * Check for addr bits stuck low or shorted.
655 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
656 addr
[test_offset
] = anti_pattern
;
658 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
660 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
661 printf_P(PSTR("\nFAILURE: Address bit stuck low or"
662 " shorted @ 0x%.5lx: expected 0x%.5lx,"
663 " actual 0x%.5lx\n"),
664 start_addr
+ offset
*sizeof(vu_long
),
671 addr
[test_offset
] = pattern
;
675 * Description: Test the integrity of a physical
676 * memory device by performing an
677 * increment/decrement test over the
678 * entire region. In the process every
679 * storage bit in the device is tested
680 * as a zero and a one. The base address
681 * and the size of the region are
682 * selected by the caller.
684 * Returns: 0 if the test succeeds, 1 if the test fails.
689 * Fill memory with a known pattern.
691 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
692 addr
[offset
] = pattern
;
696 * Check each location and invert it for the second pass.
698 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
700 if (temp
!= pattern
) {
701 printf_P(PSTR("\nFAILURE (read/write) @ 0x%.5lx:"
702 " expected 0x%.5lx, actual 0x%.5lx)\n"),
703 start_addr
+ offset
*sizeof(vu_long
),
710 anti_pattern
= ~pattern
;
711 addr
[offset
] = anti_pattern
;
715 * Check each location for the inverted pattern and zero it.
717 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
719 anti_pattern
= ~pattern
;
721 if (temp
!= anti_pattern
) {
722 printf_P(PSTR("\nFAILURE (read/write): @ 0x%.5lx:"
723 " expected 0x%.5lx, actual 0x%.5lx)\n"),
724 start_addr
+ offset
*sizeof(vu_long
),
736 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
737 vu_long pattern
, int iteration
)
742 uint32_t incr
, length
;
743 uint32_t val
, readback
;
745 /* Alternate the pattern */
750 * Flip the pattern each time to make lots of zeros and
751 * then, the next time, lots of ones. We decrement
752 * the "negative" patterns and increment the "positive"
753 * patterns to preserve this feature.
755 if (pattern
& 0x80000000)
756 pattern
= -pattern
; /* complement & increment */
760 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
762 printf_P(PSTR("\rPattern %08lX Writing..."
764 "\b\b\b\b\b\b\b\b\b\b"),
767 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
772 my_puts_P(PSTR("Reading..."));
774 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
776 if (readback
!= val
) {
777 uint32_t offset
= addr
- buf
;
779 printf_P(PSTR("\nMem error @ 0x%08X: "
780 "found %08lX, expected %08lX\n"),
781 (unsigned int)(uintptr_t)(start_addr
+ offset
*sizeof(vu_long
)),
794 * Perform a memory test. A more complete alternative test can be
795 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
796 * interrupted by ctrl-c or by a failure of one of the sub-tests.
798 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
802 vu_long
*buf
, *dummy
;
804 /* TODO: command_ret_t */
806 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
809 #if defined(CONFIG_SYS_ALT_MEMTEST)
810 const int alt_test
= 1;
812 const int alt_test
= 0;
816 start
= strtoul(argv
[1], NULL
, 16);
818 start
= CONFIG_SYS_MEMTEST_START
;
821 end
= strtoul(argv
[2], NULL
, 16);
823 end
= CONFIG_SYS_MEMTEST_END
;
826 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
831 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
835 printf_P(PSTR("Testing %08x ... %08x:\n"), (unsigned int)start
, (unsigned int)end
);
836 debug("%s:%d: start %#05lx end %#05lx\n", __func__
, __LINE__
,
840 // buf = map_sysmem(start, end - start);
841 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
843 !iteration_limit
|| iteration
< iteration_limit
;
850 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
853 errs
= mem_test_alt(buf
, start
, end
, dummy
);
855 errs
= mem_test_quick(buf
, start
, end
, pattern
,
863 /* Memory test was aborted - write a newline to finish off */
867 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
872 return ret
; /* not reached */
874 #endif /* CONFIG_CMD_MEMTEST */