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