]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_mem.c
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)
19 #include <avr/interrupt.h>
22 #include "cli_readline.h"
23 #include "print-utils.h"
24 #include "con-utils.h"
30 #ifndef CONFIG_SYS_MEMTEST_SCRATCH
31 #define CONFIG_SYS_MEMTEST_SCRATCH 0
34 /* Display values from last command.
35 * Memory modify remembered values are different from display memory.
37 static uint32_t dp_last_addr
;
38 static uint32_t dp_last_length
= 0x100;
39 static uint32_t mm_last_addr
;
41 static uint32_t base_address
= 0;
43 /*--------------------------------------------------------------------------*/
46 void z180_read_buf(uint8_t *buf
, uint32_t addr
, uint8_t count
)
48 if (z80_bus_cmd(Request
) & ZST_ACQUIRED
) {
49 z80_read_block (buf
, addr
, count
);
54 /*--------------------------------------------------------------------------*/
61 command_ret_t
do_mem_md(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
63 uint32_t addr
, length
;
68 printf_P(PSTR("flag: %d, argc: %d"), flag
, argc
);
69 for (int i
= 0; i
< argc
; i
++) {
70 printf_P(PSTR(", argv[%d]: %s"), i
, argv
[i
] ? argv
[i
] : "<NULL>");
75 /* We use the last specified parameters, unless new ones are
79 length
= dp_last_length
;
84 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
85 /* Address is specified since argc > 1 */
86 addr
= strtoul(argv
[1], NULL
, 16);
89 /* If another parameter, it is the length to display. */
91 length
= strtoul(argv
[2], NULL
, 16);
94 /* Print the lines. */
95 dump_mem(addr
, addr
, length
, z180_read_buf
, NULL
);
97 dp_last_addr
= addr
+ length
;
98 dp_last_length
= length
;
99 return CMD_RET_SUCCESS
;
109 mod_mem(cmd_tbl_t
*cmdtp
, int incrflag
, int flag
, int argc
, char * const argv
[])
118 return CMD_RET_USAGE
;
120 /* We use the last specified parameters, unless new ones are
125 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
126 /* New command specified.
129 /* Address is specified since argc > 1
131 addr
= strtoul(argv
[1], NULL
, 16);
132 addr
+= base_address
;
135 /* Print the address, followed by value. Then accept input for
136 * the next value. A non-converted value exits.
139 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
140 my_puts_P(PSTR("Bus timeout\n"));
141 return CMD_RET_FAILURE
;
143 data
= z80_read(addr
);
144 z80_bus_cmd(Release
);
145 printf_P(PSTR("%05lx: %02x"), addr
, data
);
147 nbytes
= cli_readline(PSTR(" ? "));
148 if (nbytes
== 0 || (nbytes
== 1 && console_buffer
[0] == '-')) {
149 /* <CR> pressed as only input, don't modify current
150 * location and move to next. "-" pressed will go back.
153 addr
+= nbytes
? -1 : 1;
158 data
= strtoul(console_buffer
, &endp
, 16);
159 nbytes
= endp
- console_buffer
;
161 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
162 my_puts_P(PSTR("Bus timeout\n"));
163 return CMD_RET_FAILURE
;
165 z80_write(addr
, data
);
166 z80_bus_cmd(Release
);
174 return CMD_RET_SUCCESS
;
178 command_ret_t
do_mem_mm(cmd_tbl_t
*cmdtp
, int 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
, int 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
, int flag
, int argc
, char * const argv
[])
190 uint32_t addr
, count
;
195 if ((argc
< 3) || (argc
> 4))
196 return CMD_RET_USAGE
;
198 /* Address is specified since argc > 1
200 addr
= strtoul(argv
[1], NULL
, 16);
201 addr
+= base_address
;
203 /* Get the value to write.
205 writeval
= (uint8_t) strtoul(argv
[2], NULL
, 16);
209 count
= strtoul(argv
[3], NULL
, 16);
214 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
215 my_puts_P(PSTR("Bus timeout\n"));
216 return CMD_RET_FAILURE
;
218 z80_memset(addr
, writeval
, count
);
219 z80_bus_cmd(Release
);
221 return CMD_RET_SUCCESS
;
224 #ifdef CONFIG_MX_CYCLIC
225 command_ret_t
do_mem_mdc ( cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
234 return CMD_RET_USAGE
;
236 count
= strtoul(argv
[3], NULL
, 10);
238 clear_ctrlc(); /* forget any previous Control C */
241 if (argv
[0][1] == 'd')
242 do_mem_md (NULL
, 0, 3, argv
);
244 do_mem_mw (NULL
, 0, 3, argv
);
247 /* delay for <count> ms... */
250 /* check for ctrl-c to abort... */
251 if (had_ctrlc() || ctrlc()) {
252 my_puts_P(PSTR("Abort\n"));
253 return CMD_RET_SUCCESS
;
255 } while (get_timer(ts
) < count
);
258 return CMD_RET_SUCCESS
;
260 #endif /* CONFIG_MX_CYCLIC */
262 command_ret_t
do_mem_cmp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
264 uint32_t addr1
, addr2
, count
, ngood
;
265 command_ret_t rcode
= CMD_RET_SUCCESS
;
266 uint8_t byte1
, byte2
;
272 return CMD_RET_USAGE
;
275 addr1
= strtoul(argv
[1], NULL
, 16);
276 addr1
+= base_address
;
277 addr2
= strtoul(argv
[2], NULL
, 16);
278 addr2
+= base_address
;
279 count
= strtoul(argv
[3], NULL
, 16);
281 for (ngood
= 0; ngood
< count
; ++ngood
) {
282 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
283 my_puts_P(PSTR("Bus timeout\n"));
284 rcode
= CMD_RET_FAILURE
;
287 byte1
= z80_read(addr1
);
288 byte2
= z80_read(addr2
);
289 z80_bus_cmd(Release
);
290 if (byte1
!= byte2
) {
291 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
292 "byte at 0x%05lx (%#02x)\n"),
293 addr1
, byte1
, addr2
, byte2
);
294 rcode
= CMD_RET_FAILURE
;
300 /* check for ctrl-c to abort... */
302 my_puts_P(PSTR("Abort\n"));
303 return CMD_RET_SUCCESS
;
307 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood
, ngood
);
311 command_ret_t
do_mem_cp(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
313 uint32_t src
, dest
, count
;
320 return CMD_RET_USAGE
;
322 src
= strtoul(argv
[1], NULL
, 16);
324 dest
= strtoul(argv
[2], NULL
, 16);
325 dest
+= base_address
;
326 count
= strtoul(argv
[3], NULL
, 16);
329 my_puts_P(PSTR("Zero length?\n"));
330 return CMD_RET_FAILURE
;
340 while (count
-- > 0) {
342 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
343 my_puts_P(PSTR("Bus timeout\n"));
344 return CMD_RET_FAILURE
;
346 data
= z80_read(src
);
347 z80_write(dest
, data
);
348 z80_bus_cmd(Release
);
352 /* check for ctrl-c to abort... */
354 my_puts_P(PSTR("Abort\n"));
355 return CMD_RET_SUCCESS
;
358 return CMD_RET_SUCCESS
;
361 command_ret_t
do_mem_base(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
368 /* Set new base address. */
369 base_address
= strtoul(argv
[1], NULL
, 16);
371 /* Print the current base address. */
372 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address
);
373 return CMD_RET_SUCCESS
;
376 command_ret_t
do_mem_loop(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
379 uint32_t addr
, length
;
385 return CMD_RET_USAGE
;
387 /* Address is always specified. */
388 addr
= strtoul(argv
[1], NULL
, 16);
390 /* Length is the number of bytes. */
391 length
= strtoul(argv
[2], NULL
, 16);
394 /* We want to optimize the loops to run as fast as possible.
395 * If we have only one object, just run infinite loops.
398 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
399 my_puts_P(PSTR("Bus timeout\n"));
400 return CMD_RET_FAILURE
;
407 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
408 my_puts_P(PSTR("Bus timeout\n"));
409 return CMD_RET_FAILURE
;
419 return CMD_RET_SUCCESS
;
422 command_ret_t
do_mem_loopw (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
424 uint32_t addr
, length
;
431 return CMD_RET_USAGE
;
433 /* Address is always specified. */
434 addr
= strtoul(argv
[1], NULL
, 16);
436 /* Length is the number of bytes. */
437 length
= strtoul(argv
[2], NULL
, 16);
439 data
= strtoul(argv
[3], NULL
, 16);
442 * We want to optimize the loops to run as fast as possible.
443 * If we have only one object, just run infinite loops.
446 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
447 my_puts_P(PSTR("Bus timeout\n"));
448 return CMD_RET_FAILURE
;
452 z80_write(addr
, data
);
455 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
456 my_puts_P(PSTR("Bus timeout\n"));
457 return CMD_RET_FAILURE
;
464 z80_write(p
++, data
);
468 //#define CONFIG_SYS_ALT_MEMTEST
470 #ifdef CONFIG_CMD_MEMTEST
471 static uint32_t mem_test_alt(uint32_t start_addr
, uint32_t end_addr
)
477 uint32_t test_offset
;
479 uint8_t anti_pattern
;
483 static const FLASH
uint8_t bitpattern
[] = {
484 0x01, /* single bit */
485 0x03, /* two adjacent bits */
486 0x07, /* three adjacent bits */
487 0x0F, /* four adjacent bits */
488 0x05, /* two non-adjacent bits */
489 0x15, /* three non-adjacent bits */
490 0x55, /* four non-adjacent bits */
491 0xaa, /* alternating 1/0 */
495 * Data line test: write a pattern to the first
496 * location, write the 1's complement to a 'parking'
497 * address (changes the state of the data bus so a
498 * floating bus doesn't give a false OK), and then
499 * read the value back. Note that we read it back
500 * into a variable because the next time we read it,
501 * it might be right (been there, tough to explain to
502 * the quality guys why it prints a failure when the
503 * "is" and "should be" are obviously the same in the
506 * Rather than exhaustively testing, we test some
507 * patterns by shifting '1' bits through a field of
508 * '0's and '0' bits through a field of '1's (i.e.
509 * pattern and ~pattern).
512 dummy
= start_addr
+1;
513 for (unsigned int j
= 0; j
< ARRAY_SIZE(bitpattern
); j
++) {
514 pattern
= bitpattern
[j
];
515 for (; pattern
!= 0; pattern
<<= 1) {
516 anti_pattern
= ~pattern
;
517 z80_write(addr
, pattern
);
518 z80_write(dummy
, anti_pattern
); /* clear the test data off the bus */
519 temp
= z80_read(addr
);
520 if (temp
!= pattern
) {
521 printf_P(PSTR("FAILURE (data line): "
522 "expected %02x, actual %02x\n"),
526 z80_write(addr
, anti_pattern
);
527 z80_write(dummy
, pattern
); /* clear the test data off the bus */
528 temp
= z80_read(addr
);
529 if (temp
!= anti_pattern
) {
530 printf_P(PSTR("FAILURE (data line): "
531 "Is %02x, should be %02x\n"),
545 * Based on code whose Original Author and Copyright
546 * information follows: Copyright (c) 1998 by Michael
547 * Barr. This software is placed into the public
548 * domain and may be used for any purpose. However,
549 * this notice must not be changed or removed and no
550 * warranty is either expressed or implied by its
551 * publication or distribution.
557 * Description: Test the address bus wiring in a
558 * memory region by performing a walking
559 * 1's test on the relevant bits of the
560 * address and checking for aliasing.
561 * This test will find single-bit
562 * address failures such as stuck-high,
563 * stuck-low, and shorted pins. The base
564 * address and size of the region are
565 * selected by the caller.
567 * Notes: For best results, the selected base
568 * address should have enough LSB 0's to
569 * guarantee single address bit changes.
570 * For example, to test a 64-Kbyte
571 * region, select a base address on a
572 * 64-Kbyte boundary. Also, select the
573 * region size as a power-of-two if at
576 * Returns: 0 if the test succeeds, 1 if the test fails.
579 num_bytes
= (end_addr
- start_addr
) / sizeof(uint8_t);
584 // debug("## %s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_bytes);
586 * Write the default pattern at each of the
587 * power-of-two offsets.
589 for (offset
= 1; offset
< num_bytes
; offset
<<= 1)
590 z80_write(addr
+offset
, pattern
);
593 * Check for address bits stuck high.
595 z80_write(start_addr
, anti_pattern
);
597 for (offset
= 1; offset
< num_bytes
; offset
<<= 1) {
598 temp
= z80_read(start_addr
+ offset
);
599 if (temp
!= pattern
) {
600 printf_P(PSTR("FAILURE: Address bit stuck high @ 0x%.5lx:"
601 " expected 0x%.2x, actual 0x%.2x\n"),
602 start_addr
+ offset
, pattern
, temp
);
608 z80_write(start_addr
, pattern
);
611 * Check for addr bits stuck low or shorted.
613 for (test_offset
= 1; test_offset
< num_bytes
; test_offset
<<= 1) {
614 z80_write(start_addr
+ test_offset
, anti_pattern
);
616 for (offset
= 1; offset
< num_bytes
; offset
<<= 1) {
617 temp
= z80_read(start_addr
+ offset
);
618 if ((temp
!= pattern
) && (offset
!= test_offset
)) {
619 printf_P(PSTR("FAILURE: Address bit stuck low or shorted"
620 " @ 0x%.5lx: expected 0x%.2x, actual 0x%.2x\n"),
621 start_addr
+ offset
, pattern
, temp
);
627 z80_write(start_addr
+ test_offset
, pattern
);
634 * Description: Test the integrity of a physical
635 * memory device by performing an
636 * increment/decrement test over the
637 * entire region. In the process every
638 * storage bit in the device is tested
639 * as a zero and a one. The base address
640 * and the size of the region are
641 * selected by the caller.
643 * Returns: 0 if the test succeeds, 1 if the test fails.
648 * Fill memory with a known pattern.
650 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++)
651 z80_write(addr
, pattern
);
654 * Check each location and invert it for the second pass.
656 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++) {
657 temp
= z80_read(addr
);
658 if (temp
!= pattern
) {
659 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
660 " expected 0x%.2x, actual 0x%.2x)\n"),
661 addr
, pattern
, temp
);
667 anti_pattern
= ~pattern
;
668 z80_write(addr
, anti_pattern
);
672 * Check each location for the inverted pattern and zero it.
674 for (pattern
= 1, addr
= start_addr
; addr
<= end_addr
; pattern
++, addr
++) {
675 anti_pattern
= ~pattern
;
676 temp
= z80_read(addr
);
677 if (temp
!= anti_pattern
) {
678 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
679 " expected 0x%.2x, actual 0x%.2x)\n"),
680 start_addr
, anti_pattern
, temp
);
692 * Perform a memory test. A more complete alternative test can be
693 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
694 * interrupted by ctrl-c or by a failure of one of the sub-tests.
696 command_ret_t
do_mem_mtest(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
701 unsigned int iteration_limit
= 0;
702 unsigned int iteration
;
703 uint32_t errs
= 0; /* number of errors */
710 start
= strtoul(argv
[1], NULL
, 16);
713 end
= strtoul(argv
[2], NULL
, 16);
715 end
= CONFIG_SYS_RAMSIZE_MAX
- 1;
718 iteration_limit
= (unsigned int) strtoul(argv
[3], NULL
, 16);
720 printf_P(PSTR("Testing %05lx ... %05lx:\n"), start
, end
);
721 // debug("## %s:%d: start %#05lx end %#05lx\n", __func__, __LINE__, start, end);
723 clear_ctrlc(); /* forget any previous Control C */
726 !iteration_limit
|| iteration
< iteration_limit
;
729 printf_P(PSTR("Iteration: %6d\r"), iteration
+ 1);
732 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
733 my_puts_P(PSTR("Bus timeout\n"));
734 return CMD_RET_FAILURE
;
736 errs
+= mem_test_alt(start
, end
);
737 z80_bus_cmd(Release
);
739 if (had_ctrlc() || ctrlc()) {
745 /* Memory test was aborted - write a newline to finish off */
747 ret
= CMD_RET_FAILURE
;
749 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
751 ret
= errs
? CMD_RET_FAILURE
: CMD_RET_SUCCESS
;
756 #endif /* CONFIG_CMD_MEMTEST */