]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_mem.c
Enable monitor debugging commands only, if env var 'cmd' is set to 'debug'.
[z180-stamp.git] / avr / cmd_mem.c
CommitLineData
72f58822 1/*
35edb766
L
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
72f58822
L
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
8ed66016 7 * SPDX-License-Identifier: GPL-2.0
72f58822
L
8 */
9
10/*
11 * Memory Functions
12 *
13 * Copied from FADS ROM, Dan Malek (dmalek@jlc.net)
14 */
15
16#include "common.h"
17#include <stdlib.h>
18#include <ctype.h>
5480dc65 19#include <avr/interrupt.h>
72f58822 20
72f58822
L
21#include "command.h"
22#include "cli_readline.h"
8f23e84c 23#include "print-utils.h"
72f58822 24#include "con-utils.h"
32154e5a 25#include "getopt-min.h"
5480dc65 26#include "timer.h"
72f58822 27#include "z80-if.h"
5480dc65 28#include "debug.h"
72f58822 29
72f58822
L
30
31#ifndef CONFIG_SYS_MEMTEST_SCRATCH
32#define CONFIG_SYS_MEMTEST_SCRATCH 0
33#endif
34
72f58822
L
35/* Display values from last command.
36 * Memory modify remembered values are different from display memory.
37 */
38static uint32_t dp_last_addr;
39static uint32_t dp_last_length = 0x100;
40static uint32_t mm_last_addr;
41
42static uint32_t base_address = 0;
43
44/*--------------------------------------------------------------------------*/
45
d9d8de47 46int z180_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
e39cd2a2 47{
d9d8de47
L
48 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED))
49 return -1;
50
51 z80_read_block (buf, addr, count);
52 z80_bus_cmd(Release);
53 return 0;
72f58822
L
54}
55
72f58822
L
56/*--------------------------------------------------------------------------*/
57
58/* Memory Display
59 *
60 * Syntax:
61 * md {addr} {len}
62 */
d0581f88 63command_ret_t do_mem_md(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822
L
64{
65 uint32_t addr, length;
6035a17b 66
72f58822
L
67 (void) cmdtp;
68
6035a17b 69#if 0
72f58822
L
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>");
73 }
74 putchar('\n');
75#endif
76
77 /* We use the last specified parameters, unless new ones are
78 * entered.
79 */
80 addr = dp_last_addr;
81 length = dp_last_length;
82
83 if (argc < 2)
84 return CMD_RET_USAGE;
85
86 if ((flag & CMD_FLAG_REPEAT) == 0) {
87 /* Address is specified since argc > 1 */
88 addr = strtoul(argv[1], NULL, 16);
89 addr += base_address;
90
91 /* If another parameter, it is the length to display. */
92 if (argc > 2)
93 length = strtoul(argv[2], NULL, 16);
94 }
95
96 /* Print the lines. */
d9d8de47
L
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;
101 }
72f58822 102
d9d8de47
L
103 if (ret >= 0) {
104 dp_last_addr = addr + length;
105 dp_last_length = length;
106 }
d0581f88 107 return CMD_RET_SUCCESS;
72f58822
L
108}
109
d0581f88
L
110/* Modify memory.
111 *
112 * Syntax:
113 * mm {addr}
114 * nm {addr}
115 */
116static command_ret_t
117mod_mem(cmd_tbl_t *cmdtp, int incrflag, int flag, int argc, char * const argv[])
118{
119 uint32_t addr;
120 uint8_t data;
121 int nbytes;
122
123 (void) cmdtp;
124
125 if (argc != 2)
126 return CMD_RET_USAGE;
127
128 /* We use the last specified parameters, unless new ones are
129 * entered.
130 */
131 addr = mm_last_addr;
132
133 if ((flag & CMD_FLAG_REPEAT) == 0) {
134 /* New command specified.
135 */
136
137 /* Address is specified since argc > 1
138 */
139 addr = strtoul(argv[1], NULL, 16);
140 addr += base_address;
141 }
142
143 /* Print the address, followed by value. Then accept input for
144 * the next value. A non-converted value exits.
145 */
146 do {
ea6971b8
L
147 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
148 my_puts_P(PSTR("Bus timeout\n"));
149 return CMD_RET_FAILURE;
150 }
d0581f88 151 data = z80_read(addr);
d0581f88 152 z80_bus_cmd(Release);
ea6971b8 153 printf_P(PSTR("%05lx: %02x"), addr, data);
d0581f88 154
8ed66016 155 nbytes = cli_readline(PSTR(" ? "), 0);
d0581f88
L
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.
159 */
160 if (incrflag)
161 addr += nbytes ? -1 : 1;
162 nbytes = 1;
ad9bc17c
L
163
164 } else {
d0581f88
L
165 char *endp;
166 data = strtoul(console_buffer, &endp, 16);
167 nbytes = endp - console_buffer;
168 if (nbytes) {
ea6971b8
L
169 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
170 my_puts_P(PSTR("Bus timeout\n"));
171 return CMD_RET_FAILURE;
172 }
d0581f88
L
173 z80_write(addr, data);
174 z80_bus_cmd(Release);
175 if (incrflag)
176 addr++;
177 }
178 }
13e88ed5 179 } while (nbytes > 0);
d0581f88
L
180
181 mm_last_addr = addr;
182 return CMD_RET_SUCCESS;
183}
184
185
186command_ret_t do_mem_mm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822
L
187{
188 return mod_mem (cmdtp, 1, flag, argc, argv);
189}
d0581f88 190command_ret_t do_mem_nm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822
L
191{
192 return mod_mem (cmdtp, 0, flag, argc, argv);
193}
194
d0581f88 195command_ret_t do_mem_mw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822 196{
32154e5a
L
197 uint32_t writeval;
198 uint32_t addr;
199 uint32_t count = 1;
200 uint_fast8_t width = 1;
201
202 (void) cmdtp; (void) flag;
203
204 /* reset getopt() */
beafa6d6 205 optind = 0;
32154e5a
L
206
207 int opt;
208 while ((opt = getopt(argc, argv, PSTR("bwl"))) != -1) {
209 switch (opt) {
210 case 'b':
211 width = 1;
212 break;
213 case 'w':
214 width = 2;
215 break;
216 case 'l':
217 width = 4;
218 break;
219 default: /* '?' */
220 return CMD_RET_USAGE;
221 }
222 }
72f58822 223
32154e5a
L
224 /* remaining arguments */
225 argc -= optind;
226 if ((argc < 2) || (argc > 3))
72f58822
L
227 return CMD_RET_USAGE;
228
32154e5a
L
229 /* Address and value are specified since (adjusted) argc >= 2 */
230 addr = strtoul(argv[optind++], NULL, 16);
72f58822 231 addr += base_address;
32154e5a 232 writeval = strtoul(argv[optind++], NULL, 16);
72f58822
L
233
234 /* Count ? */
32154e5a
L
235 if (argc == 3)
236 count = strtoul(argv[optind], NULL, 16);
72f58822 237
ea6971b8
L
238 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
239 my_puts_P(PSTR("Bus timeout\n"));
240 return CMD_RET_FAILURE;
72f58822 241 }
32154e5a
L
242
243 if (width == 1)
244 z80_memset(addr, writeval, count);
245 else {
246 while (count--) {
247 z80_write_block((const uint8_t *) &writeval, addr, width);
248 addr += width;
249 }
250 }
62f624d3 251 z80_bus_cmd(Release);
72f58822 252
d0581f88 253 return CMD_RET_SUCCESS;
72f58822
L
254}
255
256#ifdef CONFIG_MX_CYCLIC
d0581f88 257command_ret_t do_mem_mdc ( cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822 258{
72f58822 259 uint32_t count;
5480dc65
L
260 uint32_t ts;
261
262 (void) cmdtp;
263 (void) flag;
72f58822
L
264
265 if (argc < 4)
266 return CMD_RET_USAGE;
267
268 count = strtoul(argv[3], NULL, 10);
269
5480dc65 270 clear_ctrlc(); /* forget any previous Control C */
72f58822 271 for (;;) {
72f58822 272
5480dc65
L
273 if (argv[0][1] == 'd')
274 do_mem_md (NULL, 0, 3, argv);
275 else
276 do_mem_mw (NULL, 0, 3, argv);
72f58822 277
72f58822
L
278
279 /* delay for <count> ms... */
5480dc65
L
280 ts = get_timer(0);
281 do {
282 /* check for ctrl-c to abort... */
283 if (had_ctrlc() || ctrlc()) {
284 my_puts_P(PSTR("Abort\n"));
285 return CMD_RET_SUCCESS;
286 }
287 } while (get_timer(ts) < count);
72f58822
L
288 }
289
d0581f88 290 return CMD_RET_SUCCESS;
72f58822
L
291}
292#endif /* CONFIG_MX_CYCLIC */
293
d0581f88 294command_ret_t do_mem_cmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822
L
295{
296 uint32_t addr1, addr2, count, ngood;
d0581f88 297 command_ret_t rcode = CMD_RET_SUCCESS;
72f58822
L
298 uint8_t byte1, byte2;
299
300 (void) cmdtp;
301 (void) flag;
302
303 if (argc != 4)
304 return CMD_RET_USAGE;
305
306
307 addr1 = strtoul(argv[1], NULL, 16);
308 addr1 += base_address;
309 addr2 = strtoul(argv[2], NULL, 16);
310 addr2 += base_address;
311 count = strtoul(argv[3], NULL, 16);
312
313 for (ngood = 0; ngood < count; ++ngood) {
ea6971b8
L
314 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
315 my_puts_P(PSTR("Bus timeout\n"));
316 rcode = CMD_RET_FAILURE;
317 break;
318 }
72f58822
L
319 byte1 = z80_read(addr1);
320 byte2 = z80_read(addr2);
62f624d3 321 z80_bus_cmd(Release);
72f58822 322 if (byte1 != byte2) {
6035a17b
L
323 printf_P(PSTR("byte at 0x%05lx (%#02x) != "
324 "byte at 0x%05lx (%#02x)\n"),
72f58822 325 addr1, byte1, addr2, byte2);
d0581f88 326 rcode = CMD_RET_FAILURE;
72f58822
L
327 break;
328 }
329 addr1++;
330 addr2++;
331
332 /* check for ctrl-c to abort... */
333 if (ctrlc()) {
69988dc1 334 my_puts_P(PSTR("Abort\n"));
d0581f88 335 return CMD_RET_SUCCESS;
72f58822
L
336 }
337 }
338
69988dc1 339 printf_P(PSTR("Total of %ld byte(s) (0x%lx) were the same\n"), ngood, ngood);
72f58822
L
340 return rcode;
341}
342
d0581f88 343command_ret_t do_mem_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822
L
344{
345 uint32_t src, dest, count;
346 int_fast8_t step;
347
348 (void) cmdtp;
349 (void) flag;
350
351 if (argc != 4)
352 return CMD_RET_USAGE;
353
354 src = strtoul(argv[1], NULL, 16);
355 src += base_address;
356 dest = strtoul(argv[2], NULL, 16);
357 dest += base_address;
358 count = strtoul(argv[3], NULL, 16);
359
360 if (count == 0) {
69988dc1 361 my_puts_P(PSTR("Zero length?\n"));
d0581f88 362 return CMD_RET_FAILURE;
72f58822 363 }
6035a17b 364
72f58822
L
365 if (dest > src) {
366 src += count - 1;
367 dest += count - 1;
368 step = -1;
369 } else
370 step = 1;
371
372 while (count-- > 0) {
373 uint8_t data;
ea6971b8
L
374 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
375 my_puts_P(PSTR("Bus timeout\n"));
376 return CMD_RET_FAILURE;
377 }
72f58822
L
378 data = z80_read(src);
379 z80_write(dest, data);
62f624d3 380 z80_bus_cmd(Release);
72f58822
L
381 src += step;
382 dest += step;
383
384 /* check for ctrl-c to abort... */
385 if (ctrlc()) {
69988dc1 386 my_puts_P(PSTR("Abort\n"));
d0581f88 387 return CMD_RET_SUCCESS;
72f58822
L
388 }
389 }
d0581f88 390 return CMD_RET_SUCCESS;
72f58822
L
391}
392
d0581f88 393command_ret_t do_mem_base(cmd_tbl_t *cmdtp, int flag, int argc,
72f58822
L
394 char * const argv[])
395{
396 (void) cmdtp;
397 (void) flag;
398
399 if (argc > 1) {
400 /* Set new base address. */
401 base_address = strtoul(argv[1], NULL, 16);
402 }
403 /* Print the current base address. */
69988dc1 404 printf_P(PSTR("Base Address: 0x%05lx\n"), base_address);
d0581f88 405 return CMD_RET_SUCCESS;
72f58822
L
406}
407
d0581f88 408command_ret_t do_mem_loop(cmd_tbl_t *cmdtp, int flag, int argc,
72f58822
L
409 char * const argv[])
410{
411 uint32_t addr, length;
412
413 (void) cmdtp;
414 (void) flag;
415
416 if (argc < 3)
417 return CMD_RET_USAGE;
418
419 /* Address is always specified. */
420 addr = strtoul(argv[1], NULL, 16);
421
422 /* Length is the number of bytes. */
423 length = strtoul(argv[2], NULL, 16);
424
425
426 /* We want to optimize the loops to run as fast as possible.
427 * If we have only one object, just run infinite loops.
428 */
429 if (length == 1) {
ea6971b8
L
430 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
431 my_puts_P(PSTR("Bus timeout\n"));
432 return CMD_RET_FAILURE;
433 }
5480dc65 434 cli();
72f58822
L
435 for (;;)
436 z80_read(addr);
72f58822
L
437 }
438
ea6971b8
L
439 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
440 my_puts_P(PSTR("Bus timeout\n"));
441 return CMD_RET_FAILURE;
442 }
5480dc65 443 cli();
72f58822
L
444 for (;;) {
445 uint32_t i = length;
446 uint32_t p = addr;
447 while (i-- > 0)
448 z80_read(p++);
449 }
72f58822 450
d0581f88 451 return CMD_RET_SUCCESS;
72f58822
L
452}
453
d0581f88 454command_ret_t do_mem_loopw (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
72f58822
L
455{
456 uint32_t addr, length;
457 uint8_t data;
458
459 (void) cmdtp;
460 (void) flag;
461
462 if (argc < 4)
463 return CMD_RET_USAGE;
464
465 /* Address is always specified. */
466 addr = strtoul(argv[1], NULL, 16);
467
468 /* Length is the number of bytes. */
469 length = strtoul(argv[2], NULL, 16);
470
471 data = strtoul(argv[3], NULL, 16);
472
5480dc65
L
473 /*
474 * We want to optimize the loops to run as fast as possible.
72f58822
L
475 * If we have only one object, just run infinite loops.
476 */
477 if (length == 1) {
ea6971b8
L
478 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
479 my_puts_P(PSTR("Bus timeout\n"));
480 return CMD_RET_FAILURE;
481 }
5480dc65 482 cli();
72f58822
L
483 for (;;)
484 z80_write(addr, data);
485 }
486
ea6971b8
L
487 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
488 my_puts_P(PSTR("Bus timeout\n"));
489 return CMD_RET_FAILURE;
490 }
5480dc65 491 cli();
72f58822
L
492 for (;;) {
493 uint32_t i = length;
494 uint32_t p = addr;
495 while (i-- > 0)
496 z80_write(p++, data);
497 }
498}
5480dc65
L
499
500//#define CONFIG_SYS_ALT_MEMTEST
72f58822
L
501
502#ifdef CONFIG_CMD_MEMTEST
5480dc65 503static uint32_t mem_test_alt(uint32_t start_addr, uint32_t end_addr)
72f58822 504{
5480dc65
L
505 uint32_t addr;
506 uint32_t dummy;
72f58822 507 uint32_t errs = 0;
5480dc65
L
508 uint32_t offset;
509 uint32_t test_offset;
510 uint8_t pattern;
511 uint8_t anti_pattern;
512 uint8_t temp;
513 uint32_t num_bytes;
514
515 static const FLASH uint8_t bitpattern[] = {
516 0x01, /* single bit */
517 0x03, /* two adjacent bits */
518 0x07, /* three adjacent bits */
519 0x0F, /* four adjacent bits */
520 0x05, /* two non-adjacent bits */
521 0x15, /* three non-adjacent bits */
522 0x55, /* four non-adjacent bits */
523 0xaa, /* alternating 1/0 */
72f58822
L
524 };
525
72f58822
L
526 /*
527 * Data line test: write a pattern to the first
528 * location, write the 1's complement to a 'parking'
529 * address (changes the state of the data bus so a
530 * floating bus doesn't give a false OK), and then
531 * read the value back. Note that we read it back
532 * into a variable because the next time we read it,
533 * it might be right (been there, tough to explain to
534 * the quality guys why it prints a failure when the
535 * "is" and "should be" are obviously the same in the
536 * error message).
537 *
538 * Rather than exhaustively testing, we test some
539 * patterns by shifting '1' bits through a field of
540 * '0's and '0' bits through a field of '1's (i.e.
541 * pattern and ~pattern).
542 */
5480dc65
L
543 addr = start_addr;
544 dummy = start_addr+1;
545 for (unsigned int j = 0; j < ARRAY_SIZE(bitpattern); j++) {
546 pattern = bitpattern[j];
547 for (; pattern != 0; pattern <<= 1) {
548 anti_pattern = ~pattern;
549 z80_write(addr, pattern);
550 z80_write(dummy, anti_pattern); /* clear the test data off the bus */
551 temp = z80_read(addr);
552 if (temp != pattern) {
69988dc1 553 printf_P(PSTR("FAILURE (data line): "
5480dc65
L
554 "expected %02x, actual %02x\n"),
555 pattern, temp);
72f58822 556 errs++;
72f58822 557 }
5480dc65
L
558 z80_write(addr, anti_pattern);
559 z80_write(dummy, pattern); /* clear the test data off the bus */
560 temp = z80_read(addr);
561 if (temp != anti_pattern) {
69988dc1 562 printf_P(PSTR("FAILURE (data line): "
5480dc65
L
563 "Is %02x, should be %02x\n"),
564 temp, anti_pattern);
72f58822 565 errs++;
72f58822
L
566 }
567 }
5480dc65
L
568
569 if (ctrlc())
570 return -1;
72f58822
L
571 }
572
5480dc65
L
573 if (errs)
574 return errs;
575
72f58822
L
576 /*
577 * Based on code whose Original Author and Copyright
578 * information follows: Copyright (c) 1998 by Michael
579 * Barr. This software is placed into the public
580 * domain and may be used for any purpose. However,
581 * this notice must not be changed or removed and no
582 * warranty is either expressed or implied by its
583 * publication or distribution.
584 */
585
586 /*
587 * Address line test
588
589 * Description: Test the address bus wiring in a
590 * memory region by performing a walking
591 * 1's test on the relevant bits of the
592 * address and checking for aliasing.
593 * This test will find single-bit
594 * address failures such as stuck-high,
595 * stuck-low, and shorted pins. The base
596 * address and size of the region are
597 * selected by the caller.
598
599 * Notes: For best results, the selected base
600 * address should have enough LSB 0's to
601 * guarantee single address bit changes.
602 * For example, to test a 64-Kbyte
603 * region, select a base address on a
604 * 64-Kbyte boundary. Also, select the
605 * region size as a power-of-two if at
606 * all possible.
607 *
608 * Returns: 0 if the test succeeds, 1 if the test fails.
609 */
72f58822 610
5480dc65
L
611 num_bytes = (end_addr - start_addr) / sizeof(uint8_t);
612
613 pattern = 0xaa;
614 anti_pattern = 0x55;
615
616// debug("## %s:%d: length = 0x%.5lx\n", __func__, __LINE__, num_bytes);
72f58822
L
617 /*
618 * Write the default pattern at each of the
619 * power-of-two offsets.
620 */
5480dc65
L
621 for (offset = 1; offset < num_bytes; offset <<= 1)
622 z80_write(addr+offset, pattern);
72f58822
L
623
624 /*
625 * Check for address bits stuck high.
626 */
5480dc65 627 z80_write(start_addr, anti_pattern);
72f58822 628
5480dc65
L
629 for (offset = 1; offset < num_bytes; offset <<= 1) {
630 temp = z80_read(start_addr + offset);
72f58822 631 if (temp != pattern) {
5480dc65
L
632 printf_P(PSTR("FAILURE: Address bit stuck high @ 0x%.5lx:"
633 " expected 0x%.2x, actual 0x%.2x\n"),
634 start_addr + offset, pattern, temp);
72f58822
L
635 errs++;
636 if (ctrlc())
637 return -1;
638 }
639 }
5480dc65 640 z80_write(start_addr, pattern);
72f58822
L
641
642 /*
643 * Check for addr bits stuck low or shorted.
644 */
5480dc65
L
645 for (test_offset = 1; test_offset < num_bytes; test_offset <<= 1) {
646 z80_write(start_addr + test_offset, anti_pattern);
72f58822 647
5480dc65
L
648 for (offset = 1; offset < num_bytes; offset <<= 1) {
649 temp = z80_read(start_addr + offset);
72f58822 650 if ((temp != pattern) && (offset != test_offset)) {
5480dc65
L
651 printf_P(PSTR("FAILURE: Address bit stuck low or shorted"
652 " @ 0x%.5lx: expected 0x%.2x, actual 0x%.2x\n"),
653 start_addr + offset, pattern, temp);
72f58822
L
654 errs++;
655 if (ctrlc())
656 return -1;
657 }
658 }
5480dc65 659 z80_write(start_addr + test_offset, pattern);
72f58822
L
660 }
661
5480dc65
L
662 if (errs)
663 return errs;
664
72f58822
L
665 /*
666 * Description: Test the integrity of a physical
667 * memory device by performing an
668 * increment/decrement test over the
669 * entire region. In the process every
670 * storage bit in the device is tested
671 * as a zero and a one. The base address
672 * and the size of the region are
673 * selected by the caller.
674 *
675 * Returns: 0 if the test succeeds, 1 if the test fails.
676 */
5480dc65 677 num_bytes++;
72f58822
L
678
679 /*
680 * Fill memory with a known pattern.
681 */
5480dc65
L
682 for (pattern = 1, addr = start_addr; addr <= end_addr; pattern++, addr++)
683 z80_write(addr, pattern);
72f58822
L
684
685 /*
686 * Check each location and invert it for the second pass.
687 */
5480dc65
L
688 for (pattern = 1, addr = start_addr; addr <= end_addr; pattern++, addr++) {
689 temp = z80_read(addr);
72f58822 690 if (temp != pattern) {
5480dc65
L
691 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
692 " expected 0x%.2x, actual 0x%.2x)\n"),
693 addr, pattern, temp);
72f58822
L
694 errs++;
695 if (ctrlc())
696 return -1;
697 }
698
699 anti_pattern = ~pattern;
5480dc65 700 z80_write(addr, anti_pattern);
72f58822
L
701 }
702
703 /*
704 * Check each location for the inverted pattern and zero it.
705 */
5480dc65 706 for (pattern = 1, addr = start_addr; addr <= end_addr; pattern++, addr++) {
72f58822 707 anti_pattern = ~pattern;
5480dc65 708 temp = z80_read(addr);
72f58822 709 if (temp != anti_pattern) {
5480dc65
L
710 printf_P(PSTR("FAILURE (read/write) @ 0x%.5lx:"
711 " expected 0x%.2x, actual 0x%.2x)\n"),
712 start_addr, anti_pattern, temp);
72f58822
L
713 errs++;
714 if (ctrlc())
715 return -1;
716 }
5480dc65 717 z80_write(addr, 0);
72f58822
L
718 }
719
5480dc65 720 return errs;
72f58822
L
721}
722
723/*
724 * Perform a memory test. A more complete alternative test can be
725 * configured using CONFIG_SYS_ALT_MEMTEST. The complete test loops until
726 * interrupted by ctrl-c or by a failure of one of the sub-tests.
727 */
d0581f88 728command_ret_t do_mem_mtest(cmd_tbl_t *cmdtp, int flag, int argc,
72f58822
L
729 char * const argv[])
730{
5480dc65
L
731 uint32_t start = 0;
732 uint32_t end;
733 unsigned int iteration_limit = 0;
734 unsigned int iteration;
735 uint32_t errs = 0; /* number of errors */
72f58822 736 int ret;
5480dc65
L
737
738 (void) cmdtp;
739 (void) flag;
72f58822
L
740
741 if (argc > 1)
742 start = strtoul(argv[1], NULL, 16);
72f58822
L
743
744 if (argc > 2)
745 end = strtoul(argv[2], NULL, 16);
746 else
5480dc65 747 end = CONFIG_SYS_RAMSIZE_MAX - 1;
72f58822
L
748
749 if (argc > 3)
5480dc65 750 iteration_limit = (unsigned int) strtoul(argv[3], NULL, 16);
72f58822 751
5480dc65
L
752 printf_P(PSTR("Testing %05lx ... %05lx:\n"), start, end);
753// debug("## %s:%d: start %#05lx end %#05lx\n", __func__, __LINE__, start, end);
72f58822 754
5480dc65 755 clear_ctrlc(); /* forget any previous Control C */
72f58822 756
72f58822
L
757 for (iteration = 0;
758 !iteration_limit || iteration < iteration_limit;
759 iteration++) {
72f58822 760
69988dc1 761 printf_P(PSTR("Iteration: %6d\r"), iteration + 1);
5480dc65
L
762// debug("\n");
763
764 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
765 my_puts_P(PSTR("Bus timeout\n"));
766 return CMD_RET_FAILURE;
72f58822 767 }
5480dc65
L
768 errs += mem_test_alt(start, end);
769 z80_bus_cmd(Release);
770
771 if (had_ctrlc() || ctrlc()) {
72f58822 772 break;
5480dc65 773 }
72f58822
L
774 }
775
5480dc65 776 if (had_ctrlc()) {
72f58822 777 /* Memory test was aborted - write a newline to finish off */
5480dc65
L
778 putchar('\n');
779 ret = CMD_RET_FAILURE;
72f58822 780 } else {
69988dc1 781 printf_P(PSTR("Tested %d iteration(s) with %lu errors.\n"),
72f58822 782 iteration, errs);
5480dc65 783 ret = errs ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
72f58822
L
784 }
785
5480dc65 786 return ret;
72f58822
L
787}
788#endif /* CONFIG_CMD_MEMTEST */