]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/debug.c
mcd_mem.c: use cmd_error(), z80_bus_request_or_exit()
[z180-stamp.git] / avr / debug.c
CommitLineData
35edb766 1/*
2d914b45 2 * (C) Copyright 2014,2016 Leo C. <erbl259-lmu@yahoo.de>
35edb766 3 *
8ed66016 4 * SPDX-License-Identifier: GPL-2.0
35edb766
L
5 */
6
8ed66016 7#include "debug.h"
92b46605 8#include "common.h"
2d914b45 9#include <stdlib.h> /* __malloc_margin */
f338df2a 10#include <string.h>
92b46605
L
11#include <ctype.h>
12#include <avr/eeprom.h>
13
14#include "command.h"
ad9bc17c 15#include "cli_readline.h"
2d914b45 16#include "eval_arg.h"
e39cd2a2 17#include "print-utils.h"
35edb766 18
92b46605
L
19/*
20 * Debugging
21 */
e39cd2a2 22
92b46605
L
23#ifdef DEBUG
24
92b46605 25/*
61b0cfe9 26 * Memory Display
92b46605
L
27 * md addr {len}
28 */
b35fcb2f 29command_ret_t do_dump_mem(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
92b46605 30{
b35fcb2f 31 ERRNUM (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count);
92b46605
L
32
33 if (argc < 2)
34 return CMD_RET_USAGE;
35
e39cd2a2
L
36 uint32_t addr;
37 uint32_t length = 128;
507d25e2 38
c748023e
L
39 switch (argv[0][3]) {
40 case 'r':
ad9bc17c 41 readwhat = ram_read_buf;
c748023e
L
42 break;
43 case 'e':
ad9bc17c 44 readwhat = eeprom_read_buf;
c748023e
L
45 break;
46 case 'f':
ad9bc17c 47 readwhat = flash_read_buf;
c748023e
L
48 break;
49 default:
f338df2a 50 return CMD_RET_USAGE;
c748023e 51 }
92b46605 52
f338df2a 53 /* Address is specified since argc > 1 */
2d914b45 54 addr = eval_arg(argv[1], NULL);
f338df2a
L
55
56 /* If another parameter, it is the length to display. */
57 if (argc > 2)
2d914b45 58 length = (uint16_t) eval_arg(argv[2], NULL);
92b46605
L
59
60 /* Print the lines. */
ad9bc17c 61 dump_mem(addr, addr, length, readwhat, NULL);
92b46605 62
d0581f88 63 return CMD_RET_SUCCESS;
92b46605
L
64}
65
fcf1d5b3 66command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
92b46605
L
67{
68 uint16_t src, dest, count;
69 int_fast8_t step;
70
71 (void) cmdtp;
72 (void) flag;
73
74 if (argc != 4)
75 return CMD_RET_USAGE;
76
2d914b45
L
77 src = (size_t) eval_arg(argv[1], NULL);
78 dest = (size_t) eval_arg(argv[2], NULL);
79 count = (size_t) eval_arg(argv[3], NULL);
92b46605
L
80
81 if (src > E2END) {
82 debug("src > EEPROM size: 0x%04x\n", src);
d0581f88 83 return CMD_RET_FAILURE;
92b46605
L
84 }
85 if (dest > E2END) {
86 debug("dest > EEPROM size: 0x%04x\n", dest);
d0581f88 87 return CMD_RET_FAILURE;
92b46605
L
88 }
89 if (count > E2END+1) {
90 debug("count > EEPROM size: 0x%04x\n", count);
d0581f88 91 return CMD_RET_FAILURE;
92b46605
L
92 }
93 if (count == 0) {
69988dc1 94 debug("Zero length?\n");
d0581f88 95 return CMD_RET_FAILURE;
92b46605
L
96 }
97
98 if (dest > src) {
99 src += count - 1;
100 dest += count - 1;
101 step = -1;
102 } else
103 step = 1;
104
105 while (count-- > 0) {
106 uint8_t data;
107 data = eeprom_read_byte((uint8_t *) src);
108 eeprom_write_byte((uint8_t *) dest, data);
109 src += step;
110 dest += step;
111
112 }
d0581f88 113 return CMD_RET_SUCCESS;
92b46605 114}
69988dc1 115
4565be9a 116
ad9bc17c
L
117/* Modify memory.
118 *
119 * Syntax:
120 * !mm {addr}
121 * !nm {addr}
122 */
123
124 static uint8_t *mm_last_addr;
125
126static command_ret_t
fcf1d5b3 127mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, uint_fast8_t flag, int argc, char * const argv[])
ad9bc17c
L
128{
129 uint8_t *addr;
ad9bc17c
L
130 int nbytes;
131
132 (void) cmdtp;
133
134 if (argc != 2)
135 return CMD_RET_USAGE;
136
137 /* We use the last specified parameters, unless new ones are
138 * entered.
139 */
140 addr = mm_last_addr;
141
142 if ((flag & CMD_FLAG_REPEAT) == 0) {
143 /* New command specified.
144 */
145
146 /* Address is specified since argc > 1
147 */
2d914b45 148 addr = (uint8_t *) (size_t) eval_arg(argv[1], NULL);
ad9bc17c
L
149 }
150
151 /* Print the address, followed by value. Then accept input for
152 * the next value. A non-converted value exits.
153 */
154 do {
fb9b17a9 155 uint8_t data = *addr;
ad9bc17c
L
156 printf_P(PSTR("%04x: %02x"), addr, data);
157
8ed66016 158 nbytes = cli_readline(PSTR(" ? "), 0);
ad9bc17c
L
159 if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
160 /* <CR> pressed as only input, don't modify current
161 * location and move to next. "-" pressed will go back.
162 */
163 if (incrflag)
164 addr += nbytes ? -1 : 1;
165 nbytes = 1;
166
167 } else {
168 char *endp;
2d914b45 169 data = eval_arg(console_buffer, &endp);
ad9bc17c
L
170 nbytes = endp - console_buffer;
171 if (nbytes) {
172 *addr = data;
173 if (incrflag)
174 addr++;
175 }
176 }
13e88ed5 177 } while (nbytes > 0);
ad9bc17c
L
178
179 mm_last_addr = addr;
180 return CMD_RET_SUCCESS;
181}
182
183
fcf1d5b3 184command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
ad9bc17c
L
185{
186 return mod_mem_avr (cmdtp, 1, flag, argc, argv);
187}
fcf1d5b3 188command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
ad9bc17c
L
189{
190 return mod_mem_avr (cmdtp, 0, flag, argc, argv);
191}
192
4565be9a
L
193/*------------------------------------------------------------------------------*/
194
69988dc1
L
195struct __freelist {
196 size_t sz;
197 struct __freelist *nx;
198};
199
b9aef06e
L
200extern char *__brkval; /* first location not yet allocated */
201extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
69988dc1
L
202
203#define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
204
b9aef06e
L
205size_t get_freemem(void)
206{
207 return (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
208}
209
69988dc1
L
210void
211printfreelist(const char * title)
212{
213 struct __freelist *fp1;
214 int i;
215 unsigned int freesum = 0;
216
217 if (!__flp) {
d649155c 218 printf_P(PSTR("%s no free list\n"), title ? title : "");
69988dc1 219 } else {
d649155c 220 printf_P(PSTR("Free list: %s\n"), title ? title : "");
69988dc1 221 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
d649155c 222 printf_P(PSTR(" entry %d @ %04x: size %4u, next "),
69988dc1
L
223 i, (size_t)fp1, fp1->sz);
224 if (fp1->nx)
d649155c 225 printf_P(PSTR("%04x\n"), (size_t)fp1->nx);
69988dc1 226 else
d649155c 227 printf_P(PSTR("NULL\n"));
69988dc1
L
228 freesum += fp1->sz;
229 }
230 }
507d25e2 231
b9aef06e 232 freesum += get_freemem();
69988dc1 233
d649155c 234 printf_P(PSTR("SP: %04x, __brkval: %04x, Total free: %04u\n"),
69988dc1
L
235 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
236}
237
fcf1d5b3 238command_ret_t do_pr_free_avr(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
fb9b17a9
L
239{
240 printfreelist(NULL);
241
242 return CMD_RET_SUCCESS;
243}
69988dc1 244
b9aef06e
L
245void dump_heap(void)
246{
247 //extern unsigned int __brkval;
248
249 dump_ram((uint8_t *)__malloc_heap_start, (size_t) __malloc_heap_start, __brkval - __malloc_heap_start,
250 "=== Heap:");
251}
252
253command_ret_t do_pr_heap_avr(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
254{
255 dump_heap();
256
257 return CMD_RET_SUCCESS;
258}
259
92b46605 260#endif /* DEBUG */