]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/debug.c
d4ae1f4aa7c09c68835f0a80d5597bc00c933449
[z180-stamp.git] / avr / debug.c
1 /*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include "common.h"
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <avr/eeprom.h>
12
13 #include "command.h"
14 #include "print-utils.h"
15 #include "debug.h"
16
17 /*
18 * Debugging
19 */
20
21 #ifdef DEBUG
22
23
24 #if 0
25 void dump_heap(void)
26 {
27 extern unsigned int __brkval;
28
29 dump_ram(__malloc_heap_start,
30 __brkval - (unsigned int) __malloc_heap_start,
31 "=== Heap:");
32 }
33 #endif
34
35
36 /*
37 * Memory Display
38 * md addr {len}
39 */
40 command_ret_t do_dump_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
41 {
42 void (*readhow)(uint8_t *buf, uint32_t addr, uint8_t count);
43
44 (void) cmdtp; (void) flag;
45
46 if (argc < 2)
47 return CMD_RET_USAGE;
48
49 uint32_t addr;
50 uint32_t length = 128;
51
52 switch (argv[0][3]) {
53 case 'r':
54 readhow = ram_read_buf;
55 break;
56 case 'e':
57 readhow = eeprom_read_buf;
58 break;
59 case 'f':
60 readhow = flash_read_buf;
61 break;
62 default:
63 return CMD_RET_USAGE;
64 }
65
66 /* Address is specified since argc > 1 */
67 addr = strtoul(argv[1], NULL, 16);
68
69 /* If another parameter, it is the length to display. */
70 if (argc > 2)
71 length = (uint16_t) strtoul(argv[2], NULL, 16);
72
73 /* Print the lines. */
74 dump_mem(addr, addr, length, readhow, NULL);
75
76 return CMD_RET_SUCCESS;
77 }
78
79 command_ret_t do_eep_cp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
80 {
81 uint16_t src, dest, count;
82 int_fast8_t step;
83
84 (void) cmdtp;
85 (void) flag;
86
87 if (argc != 4)
88 return CMD_RET_USAGE;
89
90 src = (size_t) strtoul(argv[1], NULL, 16);
91 dest = (size_t) strtoul(argv[2], NULL, 16);
92 count = (size_t) strtoul(argv[3], NULL, 16);
93
94 if (src > E2END) {
95 debug("src > EEPROM size: 0x%04x\n", src);
96 return CMD_RET_FAILURE;
97 }
98 if (dest > E2END) {
99 debug("dest > EEPROM size: 0x%04x\n", dest);
100 return CMD_RET_FAILURE;
101 }
102 if (count > E2END+1) {
103 debug("count > EEPROM size: 0x%04x\n", count);
104 return CMD_RET_FAILURE;
105 }
106 if (count == 0) {
107 debug("Zero length?\n");
108 return CMD_RET_FAILURE;
109 }
110
111 if (dest > src) {
112 src += count - 1;
113 dest += count - 1;
114 step = -1;
115 } else
116 step = 1;
117
118 while (count-- > 0) {
119 uint8_t data;
120 data = eeprom_read_byte((uint8_t *) src);
121 eeprom_write_byte((uint8_t *) dest, data);
122 src += step;
123 dest += step;
124
125 }
126 return CMD_RET_SUCCESS;
127 }
128
129
130 /*------------------------------------------------------------------------------*/
131
132 #if 1
133
134 struct __freelist {
135 size_t sz;
136 struct __freelist *nx;
137 };
138
139 extern char *__brkval; /* first location not yet allocated */
140 extern struct __freelist *__flp; /* freelist pointer (head of freelist) */
141
142 #define STACK_POINTER() ((char *)AVR_STACK_POINTER_REG)
143
144 void
145 printfreelist(const char * title)
146 {
147 struct __freelist *fp1;
148 int i;
149 unsigned int freesum = 0;
150
151 /* TODO: printf_P */
152
153 if (!__flp) {
154 printf("%s no free list\n", title ? title : "");
155 } else {
156 printf("Free list: %s\n", title ? title : "");
157 for (i = 0, fp1 = __flp; fp1; i++, fp1 = fp1->nx) {
158 printf(" entry %d @ %04x: size %4u, next ",
159 i, (size_t)fp1, fp1->sz);
160 if (fp1->nx)
161 printf("%04x\n", (size_t)fp1->nx);
162 else
163 printf("NULL\n");
164 freesum += fp1->sz;
165 }
166 }
167
168 freesum += (size_t) STACK_POINTER() - __malloc_margin - (size_t) __brkval;
169
170 printf("SP: %04x, __brkval: %04x, Total free: %04u\n",
171 (size_t) STACK_POINTER(), (size_t) __brkval, freesum);
172 }
173
174 #endif
175
176 #endif /* DEBUG */