]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
2ddeab79a52a6a9a1ec00f10b4f04db314c1b693
2 * (C) Copyright 2014 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 int 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
, int 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 int ret
= dump_mem(addr
, addr
, length
, z180_read_buf
, NULL
);
98 if (ret
== -2) { /* TODO: Error codes */
99 my_puts_P(PSTR("Bus timeout\n"));
100 return CMD_RET_FAILURE
;
104 dp_last_addr
= addr
+ length
;
105 dp_last_length
= length
;
107 return CMD_RET_SUCCESS
;
117 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
126 return CMD_RET_USAGE
;
128 /* We use the last specified parameters, unless new ones are
133 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
134 /* New command specified.
137 /* Address is specified since argc > 1
139 addr
= eval_arg(argv
[1], NULL
);
140 addr
+= base_address
;
143 /* Print the address, followed by value. Then accept input for
144 * the next value. A non-converted value exits.
147 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
148 my_puts_P(PSTR("Bus timeout\n"));
149 return CMD_RET_FAILURE
;
151 data
= z80_read(addr
);
152 z80_bus_cmd(Release
);
153 printf_P(PSTR("%05lx: %02x"), addr
, data
);
155 nbytes
= cli_readline(PSTR(" ? "), 0);
156 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
157 /* <CR> pressed as only input, don't modify current
158 * location and move to next. "-" pressed will go back.
161 addr
+= nbytes
? -1 : 1;
166 data
= eval_arg(console_buffer
, &endp
);
167 nbytes
= endp
- console_buffer
;
169 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
170 my_puts_P(PSTR("Bus timeout\n"));
171 return CMD_RET_FAILURE
;
173 z80_write(addr
, data
);
174 z80_bus_cmd(Release
);
179 } while (nbytes
> 0);
182 return CMD_RET_SUCCESS
;
186 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
188 return mod_mem (cmdtp
, 1, flag
, argc
, argv
);
190 command_ret_t
do_mem_nm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
192 return mod_mem (cmdtp
, 0, flag
, argc
, argv
);
195 command_ret_t
do_mem_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
200 uint_fast8_t width
= 1;
202 (void) cmdtp
; (void) flag
;
208 while ((opt
= getopt(argc
, argv
, PSTR("bwl"))) != -1) {
220 return CMD_RET_USAGE
;
224 /* remaining arguments */
226 if ((argc
< 2) || (argc
> 3))
227 return CMD_RET_USAGE
;
229 /* Address and value are specified since (adjusted) argc >= 2 */
230 addr
= eval_arg(argv
[optind
++], NULL
);
231 addr
+= base_address
;
232 writeval
= eval_arg(argv
[optind
++], NULL
);
236 count
= eval_arg(argv
[optind
], NULL
);
238 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
239 my_puts_P(PSTR("Bus timeout\n"));
240 return CMD_RET_FAILURE
;
244 z80_memset(addr
, writeval
, count
);
247 z80_write_block((const uint8_t *) &writeval
, addr
, width
);
251 z80_bus_cmd(Release
);
253 return CMD_RET_SUCCESS
;
256 #ifdef CONFIG_MX_CYCLIC
257 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
266 if (argv
[0][1] != 'd') {
268 while ((opt
= getopt(argc
, argv
, PSTR("bwl"))) != -1)
270 return CMD_RET_USAGE
;
274 printf_P(PSTR("# argc: %d, optind: %d, argv[optind+3]: '%s'\n"),
275 argc
, optind
, argv
[optind
+3]);
277 if (argc
-optind
!= 4)
278 return CMD_RET_USAGE
;
280 count
= eval_arg(argv
[optind
+ 3], NULL
);
282 clear_ctrlc(); /* forget any previous Control C */
285 if (argv
[0][1] == 'd')
286 do_mem_md (NULL
, 0, argc
-1, argv
); /* memory display */
288 do_mem_mw (NULL
, 0, argc
-1, argv
); /* memory write */
291 /* delay for <count> ms... */
294 /* check for ctrl-c to abort... */
295 if (had_ctrlc() || ctrlc()) {
296 my_puts_P(PSTR("Abort\n"));
297 return CMD_RET_SUCCESS
;
299 } while (get_timer(ts
) < count
);
302 return CMD_RET_SUCCESS
;
304 #endif /* CONFIG_MX_CYCLIC */
306 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
308 uint32_t addr1
, addr2
, count
, ngood
;
309 command_ret_t rcode
= CMD_RET_SUCCESS
;
310 uint8_t byte1
, byte2
;
316 return CMD_RET_USAGE
;
319 addr1
= eval_arg(argv
[1], NULL
);
320 addr1
+= base_address
;
321 addr2
= eval_arg(argv
[2], NULL
);
322 addr2
+= base_address
;
323 count
= eval_arg(argv
[3], NULL
);
325 for (ngood
= 0; ngood
< count
; ++ngood
) {
326 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
327 my_puts_P(PSTR("Bus timeout\n"));
328 rcode
= CMD_RET_FAILURE
;
331 byte1
= z80_read(addr1
);
332 byte2
= z80_read(addr2
);
333 z80_bus_cmd(Release
);
334 if (byte1
!= byte2
) {
335 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
336 "byte at 0x%05lx (%#02x)\n"),
337 addr1
, byte1
, addr2
, byte2
);
338 rcode
= CMD_RET_FAILURE
;
344 /* check for ctrl-c to abort... */
346 my_puts_P(PSTR("Abort\n"));
347 return CMD_RET_SUCCESS
;
351 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
355 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
357 uint32_t src
, dest
, count
;
364 return CMD_RET_USAGE
;
366 src
= eval_arg(argv
[1], NULL
);
368 dest
= eval_arg(argv
[2], NULL
);
369 dest
+= base_address
;
370 count
= eval_arg(argv
[3], NULL
);
373 my_puts_P(PSTR("Zero length?\n"));
374 return CMD_RET_FAILURE
;
384 while (count
-- > 0) {
386 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
387 my_puts_P(PSTR("Bus timeout\n"));
388 return CMD_RET_FAILURE
;
390 data
= z80_read(src
);
391 z80_write(dest
, data
);
392 z80_bus_cmd(Release
);
396 /* check for ctrl-c to abort... */
398 my_puts_P(PSTR("Abort\n"));
399 return CMD_RET_SUCCESS
;
402 return CMD_RET_SUCCESS
;
405 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
412 /* Set new base address. */
413 base_address
= eval_arg(argv
[1], NULL
);
415 /* Print the current base address. */
416 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
417 return CMD_RET_SUCCESS
;
420 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
423 uint32_t addr
, length
;
429 return CMD_RET_USAGE
;
431 /* Address is always specified. */
432 addr
= eval_arg(argv
[1], NULL
);
434 /* Length is the number of bytes. */
435 length
= eval_arg(argv
[2], NULL
);
438 /* We want to optimize the loops to run as fast as possible.
439 * If we have only one object, just run infinite loops.
442 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
443 my_puts_P(PSTR("Bus timeout\n"));
444 return CMD_RET_FAILURE
;
451 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
452 my_puts_P(PSTR("Bus timeout\n"));
453 return CMD_RET_FAILURE
;
463 return CMD_RET_SUCCESS
;
466 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
468 uint32_t addr
, length
;
475 return CMD_RET_USAGE
;
477 /* Address is always specified. */
478 addr
= eval_arg(argv
[1], NULL
);
480 /* Length is the number of bytes. */
481 length
= eval_arg(argv
[2], NULL
);
483 data
= eval_arg(argv
[3], NULL
);
486 * We want to optimize the loops to run as fast as possible.
487 * If we have only one object, just run infinite loops.
490 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
491 my_puts_P(PSTR("Bus timeout\n"));
492 return CMD_RET_FAILURE
;
496 z80_write(addr
, data
);
499 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
500 my_puts_P(PSTR("Bus timeout\n"));
501 return CMD_RET_FAILURE
;
508 z80_write(p
++, data
);
512 //#define CONFIG_SYS_ALT_MEMTEST
514 #ifdef CONFIG_CMD_MEMTEST
515 static uint32_t mem_test_alt(uint32_t start_addr
, uint32_t end_addr
)
521 uint32_t test_offset
;
523 uint8_t anti_pattern
;
527 static const FLASH
uint8_t bitpattern
[] = {
528 0x01, /* single bit */
529 0x03, /* two adjacent bits */
530 0x07, /* three adjacent bits */
531 0x0F, /* four adjacent bits */
532 0x05, /* two non-adjacent bits */
533 0x15, /* three non-adjacent bits */
534 0x55, /* four non-adjacent bits */
535 0xaa, /* alternating 1/0 */
539 * Data line test: write a pattern to the first
540 * location, write the 1's complement to a 'parking'
541 * address (changes the state of the data bus so a
542 * floating bus doesn't give a false OK), and then
543 * read the value back. Note that we read it back
544 * into a variable because the next time we read it,
545 * it might be right (been there, tough to explain to
546 * the quality guys why it prints a failure when the
547 * "is" and "should be" are obviously the same in the
550 * Rather than exhaustively testing, we test some
551 * patterns by shifting '1' bits through a field of
552 * '0's and '0' bits through a field of '1's (i.e.
553 * pattern and ~pattern).
556 dummy
= start_addr
+1;
557 for (unsigned int j
= 0; j
< ARRAY_SIZE(bitpattern
); j
++) {
558 pattern
= bitpattern
[j
];
559 for (; pattern
!= 0; pattern
<<= 1) {
560 anti_pattern
= ~pattern
;
561 z80_write(addr
, pattern
);
562 z80_write(dummy
, anti_pattern
); /* clear the test data off the bus */
563 temp
= z80_read(addr
);
564 if (temp
!= pattern
) {
565 printf_P(PSTR("FAILURE (data line): "
566 "expected %02x, actual %02x\n"),
570 z80_write(addr
, anti_pattern
);
571 z80_write(dummy
, pattern
); /* clear the test data off the bus */
572 temp
= z80_read(addr
);
573 if (temp
!= anti_pattern
) {
574 printf_P(PSTR("FAILURE (data line): "
575 "Is %02x, should be %02x\n"),
589 * Based on code whose Original Author and Copyright
590 * information follows: Copyright (c) 1998 by Michael
591 * Barr. This software is placed into the public
592 * domain and may be used for any purpose. However,
593 * this notice must not be changed or removed and no
594 * warranty is either expressed or implied by its
595 * publication or distribution.
601 * Description: Test the address bus wiring in a
602 * memory region by performing a walking
603 * 1's test on the relevant bits of the
604 * address and checking for aliasing.
605 * This test will find single-bit
606 * address failures such as stuck-high,
607 * stuck-low, and shorted pins. The base
608 * address and size of the region are
609 * selected by the caller.
611 * Notes: For best results, the selected base
612 * address should have enough LSB 0's to
613 * guarantee single address bit changes.
614 * For example, to test a 64-Kbyte
615 * region, select a base address on a
616 * 64-Kbyte boundary. Also, select the
617 * region size as a power-of-two if at
620 * Returns: 0 if the test succeeds, 1 if the test fails.
623 num_bytes
= (end_addr
- start_addr
) / sizeof(uint8_t);
628 // debug("## %s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_bytes);
630 * Write the default pattern at each of the
631 * power-of-two offsets.
633 for (offset
= 1; offset
< num_bytes
; offset
<<= 1)
634 z80_write(addr
+offset
, pattern
);
637 * Check for address bits stuck high.
639 z80_write(start_addr
, anti_pattern
);
641 for (offset
= 1; offset
< num_bytes
; offset
<<= 1) {
642 temp
= z80_read(start_addr
+ offset
);
643 if (temp
!= pattern
) {
644 printf_P(PSTR("FAILURE: Address bit stuck high @ 0x%.5lx:"
645 " expected 0x%.2x, actual 0x%.2x\n"),
646 start_addr
+ offset
, pattern
, temp
);
652 z80_write(start_addr
, pattern
);
655 * Check for addr bits stuck low or shorted.
657 for (test_offset
= 1; test_offset
< num_bytes
; test_offset
<<= 1) {
658 z80_write(start_addr
+ test_offset
, anti_pattern
);
660 for (offset
= 1; offset
< num_bytes
; offset
<<= 1) {
661 temp
= z80_read(start_addr
+ offset
);
662 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
663 printf_P(PSTR("FAILURE: Address bit stuck low or shorted"
664 " @ 0x%.5lx: expected 0x%.2x, actual 0x%.2x\n"),
665 start_addr
+ offset
, pattern
, temp
);
671 z80_write(start_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, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++)
695 z80_write(addr
, pattern
);
698 * Check each location and invert it for the second pass.
700 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++) {
701 temp
= z80_read(addr
);
702 if (temp
!= pattern
) {
703 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
704 " expected 0x%.2x, actual 0x%.2x)\n"),
705 addr
, pattern
, temp
);
711 anti_pattern
= ~pattern
;
712 z80_write(addr
, anti_pattern
);
716 * Check each location for the inverted pattern and zero it.
718 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++) {
719 anti_pattern
= ~pattern
;
720 temp
= z80_read(addr
);
721 if (temp
!= anti_pattern
) {
722 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
723 " expected 0x%.2x, actual 0x%.2x)\n"),
724 start_addr
, anti_pattern
, temp
);
736 * Perform a memory test. A more complete alternative test can be
737 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
738 * interrupted by ctrl-c or by a failure of one of the sub-tests.
740 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
745 unsigned int iteration_limit
= 0;
746 unsigned int iteration
;
747 uint32_t errs
= 0; /* number of errors */
754 start
= eval_arg(argv
[1], NULL
);
757 end
= eval_arg(argv
[2], NULL
);
759 end
= CONFIG_SYS_RAMSIZE_MAX
- 1;
762 iteration_limit
= (unsigned int) eval_arg(argv
[3], NULL
);
764 printf_P(PSTR("Testing %05lx ... %05lx:\n"), start
, end
);
765 // debug("## %s:%d: start %#05lx end %#05lx\n", __func__, __LINE__, start, end);
767 clear_ctrlc(); /* forget any previous Control C */
770 !iteration_limit
|| iteration
< iteration_limit
;
773 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
776 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
777 my_puts_P(PSTR("Bus timeout\n"));
778 return CMD_RET_FAILURE
;
780 errs
+= mem_test_alt(start
, end
);
781 z80_bus_cmd(Release
);
783 if (had_ctrlc() || ctrlc()) {
789 /* Memory test was aborted - write a newline to finish off */
791 ret
= CMD_RET_FAILURE
;
793 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
795 ret
= errs
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
800 #endif /* CONFIG_CMD_MEMTEST */