]>
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 /* Display values from last command.
34 * Memory modify remembered values are different from display memory.
36 static uint32_t dp_last_addr
;
37 static uint32_t dp_last_length
= 0x100;
38 static uint32_t mm_last_addr
;
40 static uint32_t base_address
= 0;
42 /*--------------------------------------------------------------------------*/
44 static void print_blanks(uint_fast8_t count
)
50 int z180_dump_mem(uint32_t startaddr
, uint32_t len
, const char *title
)
54 uint8_t pre
= startaddr
% 16;
55 uint32_t addr
= startaddr
& ~0x0f;
60 printf_P(PSTR("%s\n"),title
);
67 for (i
= pre
; i
< llen
; i
++)
68 buf
[i
] = z80_read(addr
+ i
);
71 printf_P(PSTR("%.5lx:"), addr
);
73 print_blanks(3 * pre
);
75 /* Print hex values */
76 for (i
= pre
; i
< llen
; i
++)
77 printf_P(PSTR(" %.2x"), buf
[i
]);
79 for (i
= 0; i
< llen
; i
++) {
83 printf_P(PSTR(".. "));
85 printf_P(PSTR("%.2x "), buf
[i
]);
88 /* fill line with whitespace for nice ASCII print */
90 print_blanks(3 * (16u - i
) + (16u-i
)/8 + 1 + pre
);
94 /* Print data in ASCII characters */
95 for (i
= pre
; i
< llen
; i
++)
96 printf_P(PSTR("%c"), isprint(buf
[i
]) ? buf
[i
] : '.');
110 /*--------------------------------------------------------------------------*/
117 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
119 uint32_t addr
, length
;
124 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
125 for (int i
= 0; i
< argc
; i
++) {
126 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
131 /* We use the last specified parameters, unless new ones are
135 length
= dp_last_length
;
138 return CMD_RET_USAGE
;
140 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
141 /* Address is specified since argc > 1 */
142 addr
= strtoul(argv
[1], NULL
, 16);
143 addr
+= base_address
;
145 /* If another parameter, it is the length to display. */
147 length
= strtoul(argv
[2], NULL
, 16);
150 /* Print the lines. */
151 z180_dump_mem(addr
, length
, NULL
);
153 dp_last_addr
= addr
+ length
;
154 dp_last_length
= length
;
155 return CMD_RET_SUCCESS
;
165 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
174 return CMD_RET_USAGE
;
176 /* We use the last specified parameters, unless new ones are
181 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
182 /* New command specified.
185 /* Address is specified since argc > 1
187 addr
= strtoul(argv
[1], NULL
, 16);
188 addr
+= base_address
;
191 /* Print the address, followed by value. Then accept input for
192 * the next value. A non-converted value exits.
195 z80_bus_cmd(Request
);
196 data
= z80_read(addr
);
197 printf_P(PSTR("%05lx: %02x"), addr
, data
);
198 z80_bus_cmd(Release
);
200 nbytes
= cli_readline(PSTR(" ? "));
201 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
202 /* <CR> pressed as only input, don't modify current
203 * location and move to next. "-" pressed will go back.
206 addr
+= nbytes
? -1 : 1;
211 data
= strtoul(console_buffer
, &endp
, 16);
212 nbytes
= endp
- console_buffer
;
214 z80_bus_cmd(Request
);
215 z80_write(addr
, data
);
216 z80_bus_cmd(Release
);
224 return CMD_RET_SUCCESS
;
228 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
230 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
232 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
234 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
237 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
240 uint32_t addr
, count
;
245 if ((argc
< 3) || (argc
> 4))
246 return CMD_RET_USAGE
;
248 /* Address is specified since argc > 1
250 addr
= strtoul(argv
[1], NULL
, 16);
251 addr
+= base_address
;
253 /* Get the value to write.
255 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
259 count
= strtoul(argv
[3], NULL
, 16);
264 z80_bus_cmd(Request
);
265 while (count
-- > 0) {
266 z80_write(addr
, writeval
);
269 z80_bus_cmd(Release
);
271 return CMD_RET_SUCCESS
;
274 #ifdef CONFIG_MX_CYCLIC
275 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
281 return CMD_RET_USAGE
;
283 count
= strtoul(argv
[3], NULL
, 10);
286 do_mem_md (NULL
, 0, 3, argv
);
288 /* delay for <count> ms... */
289 /* TODO: use timer */
290 for (i
=0; i
<count
; i
++)
293 /* check for ctrl-c to abort... */
295 my_puts_P(PSTR("Abort\n"));
296 return CMD_RET_SUCCESS
;
300 return CMD_RET_SUCCESS
;
303 command_ret_t
do_mem_mwc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
309 return CMD_RET_USAGE
;
311 count
= strtoul(argv
[3], NULL
, 10);
314 do_mem_mw (NULL
, 0, 3, argv
);
316 /* delay for <count> ms... */
317 /* TODO: use timer */
318 for (i
=0; i
<count
; i
++)
321 /* check for ctrl-c to abort... */
323 my_puts_P(PSTR("Abort\n"));
324 return CMD_RET_SUCCESS
;
328 return CMD_RET_SUCCESS
;
330 #endif /* CONFIG_MX_CYCLIC */
332 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
334 uint32_t addr1
, addr2
, count
, ngood
;
335 command_ret_t rcode
= CMD_RET_SUCCESS
;
336 uint8_t byte1
, byte2
;
342 return CMD_RET_USAGE
;
345 addr1
= strtoul(argv
[1], NULL
, 16);
346 addr1
+= base_address
;
347 addr2
= strtoul(argv
[2], NULL
, 16);
348 addr2
+= base_address
;
349 count
= strtoul(argv
[3], NULL
, 16);
351 for (ngood
= 0; ngood
< count
; ++ngood
) {
352 z80_bus_cmd(Request
);
353 byte1
= z80_read(addr1
);
354 byte2
= z80_read(addr2
);
355 z80_bus_cmd(Release
);
356 if (byte1
!= byte2
) {
357 printf( "byte at 0x%05lx (%#02x) != "
358 "byte at 0x%05lx (%#02x)\n",
359 addr1
, byte1
, addr2
, byte2
);
360 rcode
= CMD_RET_FAILURE
;
366 /* check for ctrl-c to abort... */
368 my_puts_P(PSTR("Abort\n"));
369 return CMD_RET_SUCCESS
;
373 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
377 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
379 uint32_t src
, dest
, count
;
386 return CMD_RET_USAGE
;
388 src
= strtoul(argv
[1], NULL
, 16);
390 dest
= strtoul(argv
[2], NULL
, 16);
391 dest
+= base_address
;
392 count
= strtoul(argv
[3], NULL
, 16);
395 my_puts_P(PSTR("Zero length?\n"));
396 return CMD_RET_FAILURE
;
406 while (count
-- > 0) {
408 z80_bus_cmd(Request
);
409 data
= z80_read(src
);
410 z80_write(dest
, data
);
411 z80_bus_cmd(Release
);
415 /* check for ctrl-c to abort... */
417 my_puts_P(PSTR("Abort\n"));
418 return CMD_RET_SUCCESS
;
421 return CMD_RET_SUCCESS
;
424 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
431 /* Set new base address. */
432 base_address
= strtoul(argv
[1], NULL
, 16);
434 /* Print the current base address. */
435 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
436 return CMD_RET_SUCCESS
;
439 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
442 uint32_t addr
, length
;
448 return CMD_RET_USAGE
;
450 /* Address is always specified. */
451 addr
= strtoul(argv
[1], NULL
, 16);
453 /* Length is the number of bytes. */
454 length
= strtoul(argv
[2], NULL
, 16);
457 /* We want to optimize the loops to run as fast as possible.
458 * If we have only one object, just run infinite loops.
461 z80_bus_cmd(Request
);
464 z80_bus_cmd(Release
);
467 z80_bus_cmd(Request
);
474 z80_bus_cmd(Release
);
476 return CMD_RET_SUCCESS
;
480 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
482 uint32_t addr
, length
;
489 return CMD_RET_USAGE
;
491 /* Address is always specified. */
492 addr
= strtoul(argv
[1], NULL
, 16);
494 /* Length is the number of bytes. */
495 length
= strtoul(argv
[2], NULL
, 16);
497 data
= strtoul(argv
[3], NULL
, 16);
499 /* We want to optimize the loops to run as fast as possible.
500 * If we have only one object, just run infinite loops.
503 z80_bus_cmd(Request
);
505 z80_write(addr
, data
);
512 z80_write(p
++, data
);
515 #endif /* CONFIG_LOOPW */
517 #ifdef CONFIG_CMD_MEMTEST
518 static uint32_t mem_test_alt(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
523 uint32_t val
, readback
;
529 vu_long anti_pattern
;
531 static const FLASH
uint32_t bitpattern
[] = {
532 0x00000001, /* single bit */
533 0x00000003, /* two adjacent bits */
534 0x00000007, /* three adjacent bits */
535 0x0000000F, /* four adjacent bits */
536 0x00000005, /* two non-adjacent bits */
537 0x00000015, /* three non-adjacent bits */
538 0x00000055, /* four non-adjacent bits */
539 0xaaaaaaaa, /* alternating 1/0 */
542 num_words
= (end_addr
- start_addr
) / sizeof(vu_long
);
545 * Data line test: write a pattern to the first
546 * location, write the 1's complement to a 'parking'
547 * address (changes the state of the data bus so a
548 * floating bus doesn't give a false OK), and then
549 * read the value back. Note that we read it back
550 * into a variable because the next time we read it,
551 * it might be right (been there, tough to explain to
552 * the quality guys why it prints a failure when the
553 * "is" and "should be" are obviously the same in the
556 * Rather than exhaustively testing, we test some
557 * patterns by shifting '1' bits through a field of
558 * '0's and '0' bits through a field of '1's (i.e.
559 * pattern and ~pattern).
562 for (j
= 0; j
< sizeof(bitpattern
) / sizeof(bitpattern
[0]); j
++) {
564 for (; val
!= 0; val
<<= 1) {
566 *dummy
= ~val
; /* clear the test data off the bus */
568 if (readback
!= val
) {
569 printf_P(PSTR("FAILURE (data line): "
570 "expected %05lx, actual %05lx\n"),
579 if (readback
!= ~val
) {
580 printf_P(PSTR("FAILURE (data line): "
581 "Is %05lx, should be %05lx\n"),
591 * Based on code whose Original Author and Copyright
592 * information follows: Copyright (c) 1998 by Michael
593 * Barr. This software is placed into the public
594 * domain and may be used for any purpose. However,
595 * this notice must not be changed or removed and no
596 * warranty is either expressed or implied by its
597 * publication or distribution.
603 * Description: Test the address bus wiring in a
604 * memory region by performing a walking
605 * 1's test on the relevant bits of the
606 * address and checking for aliasing.
607 * This test will find single-bit
608 * address failures such as stuck-high,
609 * stuck-low, and shorted pins. The base
610 * address and size of the region are
611 * selected by the caller.
613 * Notes: For best results, the selected base
614 * address should have enough LSB 0's to
615 * guarantee single address bit changes.
616 * For example, to test a 64-Kbyte
617 * region, select a base address on a
618 * 64-Kbyte boundary. Also, select the
619 * region size as a power-of-two if at
622 * Returns: 0 if the test succeeds, 1 if the test fails.
624 pattern
= (vu_long
) 0xaaaaaaaa;
625 anti_pattern
= (vu_long
) 0x55555555;
627 debug("%s:%d: length = 0x%.5lx\n", __func__
, __LINE__
, num_words
);
629 * Write the default pattern at each of the
630 * power-of-two offsets.
632 for (offset
= 1; offset
< num_words
; offset
<<= 1)
633 addr
[offset
] = pattern
;
636 * Check for address bits stuck high.
639 addr
[test_offset
] = anti_pattern
;
641 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
643 if (temp
!= pattern
) {
644 printf_P(PSTR("\nFAILURE: Address bit stuck high @ 0x%.5lx:"
645 " expected 0x%.5lx, actual 0x%.5lx\n"),
646 start_addr
+ offset
*sizeof(vu_long
),
653 addr
[test_offset
] = pattern
;
656 * Check for addr bits stuck low or shorted.
658 for (test_offset
= 1; test_offset
< num_words
; test_offset
<<= 1) {
659 addr
[test_offset
] = anti_pattern
;
661 for (offset
= 1; offset
< num_words
; offset
<<= 1) {
663 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
664 printf_P(PSTR("\nFAILURE: Address bit stuck low or"
665 " shorted @ 0x%.5lx: expected 0x%.5lx,"
666 " actual 0x%.5lx\n"),
667 start_addr
+ offset
*sizeof(vu_long
),
674 addr
[test_offset
] = pattern
;
678 * Description: Test the integrity of a physical
679 * memory device by performing an
680 * increment/decrement test over the
681 * entire region. In the process every
682 * storage bit in the device is tested
683 * as a zero and a one. The base address
684 * and the size of the region are
685 * selected by the caller.
687 * Returns: 0 if the test succeeds, 1 if the test fails.
692 * Fill memory with a known pattern.
694 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
695 addr
[offset
] = pattern
;
699 * Check each location and invert it for the second pass.
701 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
703 if (temp
!= pattern
) {
704 printf_P(PSTR("\nFAILURE (read/write) @ 0x%.5lx:"
705 " expected 0x%.5lx, actual 0x%.5lx)\n"),
706 start_addr
+ offset
*sizeof(vu_long
),
713 anti_pattern
= ~pattern
;
714 addr
[offset
] = anti_pattern
;
718 * Check each location for the inverted pattern and zero it.
720 for (pattern
= 1, offset
= 0; offset
< num_words
; pattern
++, offset
++) {
722 anti_pattern
= ~pattern
;
724 if (temp
!= anti_pattern
) {
725 printf_P(PSTR("\nFAILURE (read/write): @ 0x%.5lx:"
726 " expected 0x%.5lx, actual 0x%.5lx)\n"),
727 start_addr
+ offset
*sizeof(vu_long
),
739 static uint32_t mem_test_quick(vu_long
*buf
, uint32_t start_addr
, uint32_t end_addr
,
740 vu_long pattern
, int iteration
)
745 uint32_t incr
, length
;
746 uint32_t val
, readback
;
748 /* Alternate the pattern */
753 * Flip the pattern each time to make lots of zeros and
754 * then, the next time, lots of ones. We decrement
755 * the "negative" patterns and increment the "positive"
756 * patterns to preserve this feature.
758 if (pattern
& 0x80000000)
759 pattern
= -pattern
; /* complement & increment */
763 length
= (end_addr
- start_addr
) / sizeof(uint32_t);
765 printf_P(PSTR("\rPattern %08lX Writing..."
767 "\b\b\b\b\b\b\b\b\b\b"),
770 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
775 my_puts_P(PSTR("Reading..."));
777 for (addr
= buf
, val
= pattern
; addr
< end
; addr
++) {
779 if (readback
!= val
) {
780 uint32_t offset
= addr
- buf
;
782 printf_P(PSTR("\nMem error @ 0x%08X: "
783 "found %08lX, expected %08lX\n"),
784 (unsigned int)(uintptr_t)(start_addr
+ offset
*sizeof(vu_long
)),
797 * Perform a memory test. A more complete alternative test can be
798 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
799 * interrupted by ctrl-c or by a failure of one of the sub-tests.
801 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
805 vu_long
*buf
, *dummy
;
807 /* TODO: command_ret_t */
809 uint32_t errs
= 0; /* number of errors, or -1 if interrupted */
812 #if defined(CONFIG_SYS_ALT_MEMTEST)
813 const int alt_test
= 1;
815 const int alt_test
= 0;
819 start
= strtoul(argv
[1], NULL
, 16);
821 start
= CONFIG_SYS_MEMTEST_START
;
824 end
= strtoul(argv
[2], NULL
, 16);
826 end
= CONFIG_SYS_MEMTEST_END
;
829 pattern
= (uint32_t)strtoul(argv
[3], NULL
, 16);
834 iteration_limit
= (uint32_t)strtoul(argv
[4], NULL
, 16);
838 printf_P(PSTR("Testing %08x ... %08x:\n"), (unsigned int)start
, (unsigned int)end
);
839 debug("%s:%d: start %#05lx end %#05lx\n", __func__
, __LINE__
,
843 // buf = map_sysmem(start, end - start);
844 // dummy = map_sysmem(CONFIG_SYS_MEMTEST_SCRATCH, sizeof(vu_long));
846 !iteration_limit
|| iteration
< iteration_limit
;
853 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
856 errs
= mem_test_alt(buf
, start
, end
, dummy
);
858 errs
= mem_test_quick(buf
, start
, end
, pattern
,
866 /* Memory test was aborted - write a newline to finish off */
870 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
875 return ret
; /* not reached */
877 #endif /* CONFIG_CMD_MEMTEST */