]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/debug.c
New debug command: dump heap
[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 */
93ea25f2 29command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
92b46605 30{
d9d8de47 31 int (*readwhat)(uint8_t *buf, uint32_t addr, uint8_t count);
507d25e2 32
f338df2a 33 (void) cmdtp; (void) flag;
92b46605
L
34
35 if (argc < 2)
36 return CMD_RET_USAGE;
37
e39cd2a2
L
38 uint32_t addr;
39 uint32_t length = 128;
507d25e2 40
c748023e
L
41 switch (argv[0][3]) {
42 case 'r':
ad9bc17c 43 readwhat = ram_read_buf;
c748023e
L
44 break;
45 case 'e':
ad9bc17c 46 readwhat = eeprom_read_buf;
c748023e
L
47 break;
48 case 'f':
ad9bc17c 49 readwhat = flash_read_buf;
c748023e
L
50 break;
51 default:
f338df2a 52 return CMD_RET_USAGE;
c748023e 53 }
92b46605 54
f338df2a 55 /* Address is specified since argc > 1 */
2d914b45 56 addr = eval_arg(argv[1], NULL);
f338df2a
L
57
58 /* If another parameter, it is the length to display. */
59 if (argc > 2)
2d914b45 60 length = (uint16_t) eval_arg(argv[2], NULL);
92b46605
L
61
62 /* Print the lines. */
ad9bc17c 63 dump_mem(addr, addr, length, readwhat, NULL);
92b46605 64
d0581f88 65 return CMD_RET_SUCCESS;
92b46605
L
66}
67
93ea25f2 68command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
92b46605
L
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
2d914b45
L
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);
92b46605
L
82
83 if (src > E2END) {
84 debug("src > EEPROM size: 0x%04x\n", src);
d0581f88 85 return CMD_RET_FAILURE;
92b46605
L
86 }
87 if (dest > E2END) {
88 debug("dest > EEPROM size: 0x%04x\n", dest);
d0581f88 89 return CMD_RET_FAILURE;
92b46605
L
90 }
91 if (count > E2END+1) {
92 debug("count > EEPROM size: 0x%04x\n", count);
d0581f88 93 return CMD_RET_FAILURE;
92b46605
L
94 }
95 if (count == 0) {
69988dc1 96 debug("Zero length?\n");
d0581f88 97 return CMD_RET_FAILURE;
92b46605
L
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 }
d0581f88 115 return CMD_RET_SUCCESS;
92b46605 116}
69988dc1 117
4565be9a 118
ad9bc17c
L
119/* Modify memory.
120 *
121 * Syntax:
122 * !mm {addr}
123 * !nm {addr}
124 */
125
126 static uint8_t *mm_last_addr;
127
128static command_ret_t
93ea25f2 129mod_mem_avr(cmd_tbl_t *cmdtp, int incrflag, uint_fast8_t flag, int argc, char * const argv[])
ad9bc17c
L
130{
131 uint8_t *addr;
ad9bc17c
L
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 */
2d914b45 150 addr = (uint8_t *) (size_t) eval_arg(argv[1], NULL);
ad9bc17c
L
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 {
c6a1984b 157 uint8_t data = *addr;
ad9bc17c
L
158 printf_P(PSTR("%04x: %02x"), addr, data);
159
8ed66016 160 nbytes = cli_readline(PSTR(" ? "), 0);
ad9bc17c
L
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;
2d914b45 171 data = eval_arg(console_buffer, &endp);
ad9bc17c
L
172 nbytes = endp - console_buffer;
173 if (nbytes) {
174 *addr = data;
175 if (incrflag)
176 addr++;
177 }
178 }
13e88ed5 179 } while (nbytes > 0);
ad9bc17c
L
180
181 mm_last_addr = addr;
182 return CMD_RET_SUCCESS;
183}
184
185
93ea25f2 186command_ret_t do_mem_mm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
ad9bc17c
L
187{
188 return mod_mem_avr (cmdtp, 1, flag, argc, argv);
189}
93ea25f2 190command_ret_t do_mem_nm_avr(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
ad9bc17c
L
191{
192 return mod_mem_avr (cmdtp, 0, flag, argc, argv);
193}
194
4565be9a
L
195/*------------------------------------------------------------------------------*/
196
69988dc1
L
197struct __freelist {
198 size_t sz;
199 struct __freelist *nx;
200};
201
8315e7f4
L
202extern char *__brkval; /* first location not yet allocated */
203extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
69988dc1
L
204
205#define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
206
8315e7f4
L
207size_t get_freemem(void)
208{
209 return (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
210}
211
69988dc1
L
212void
213printfreelist(const char * title)
214{
215 struct __freelist *fp1;
216 int i;
217 unsigned int freesum = 0;
218
507d25e2
L
219/* TODO: printf_P */
220
69988dc1
L
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 }
507d25e2 235
8315e7f4 236 freesum += get_freemem();
69988dc1
L
237
238 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
239 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
240}
241
93ea25f2 242command_ret_t do_pr_free_avr(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
c6a1984b
L
243{
244 printfreelist(NULL);
245
246 return CMD_RET_SUCCESS;
247}
69988dc1 248
8315e7f4
L
249void 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
257command_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
92b46605 264#endif /* DEBUG */