]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_loadihex.c
cmd_loadihex.c: use cmd_error()
[z180-stamp.git] / avr / cmd_loadihex.c
1 /*
2 * (C) Copyright 2015 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7 #include "cmd_loadihex.h"
8 #include <ctype.h>
9
10 #include "con-utils.h"
11 #include "z80-if.h"
12 #include "debug.h"
13
14
15 uint32_t detect_ramsize(void)
16 {
17 uint32_t addr;
18 uint8_t save_addr, save_0;
19 const uint8_t PATTERN_1 = 0x55;
20 const uint8_t PATTERN_2 = ~PATTERN_1;
21
22 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
23 my_puts_P(PSTR("Bus timeout\n"));
24 return 0;
25 }
26
27 save_0 = z80_read(0);
28 z80_write(0, PATTERN_1);
29
30 for (addr=1; addr < CONFIG_SYS_RAMSIZE_MAX; addr <<= 1) {
31 save_addr = z80_read(addr);
32 z80_write(addr, PATTERN_2);
33 if (z80_read(0) != PATTERN_1 || z80_read(addr) != PATTERN_2)
34 break;
35 z80_write(addr, save_addr);
36 }
37 z80_write(0, save_0);
38 z80_bus_cmd(Release);
39
40 return addr;
41 }
42
43 typedef enum {
44 IHX_OK,
45 IHX_BROKEN,
46 IHX_CHKSUMERR
47 } ihx_rstat_t;
48
49 typedef struct {
50 ihx_rstat_t status;
51 int8_t type;
52 uint8_t len;
53 uint16_t address;
54 uint8_t data[256];
55 } ihex_t;
56
57
58 static int get_hexdigit(void)
59 {
60 int c;
61 c = toupper(my_getchar(1));
62 if (isxdigit(c)) {
63 c -= '0';
64 if (c > 9)
65 c -= ('A' - '0' - 10);
66 return c;
67 } else
68 return -1;
69 }
70
71 static int get_hexbyte(void)
72 {
73 uint8_t i,j;
74
75 if ((i = (uint8_t) get_hexdigit()) < 0x10)
76 if ((j = (uint8_t) get_hexdigit()) < 0x10) {
77 return (i<<4) + j;
78 }
79
80 return -1;
81 }
82
83
84 static int ihex_get_record(ihex_t *rec)
85 {
86 int c;
87 uint8_t sum;
88
89 rec->status = IHX_BROKEN;
90 rec->len = 0;
91 rec->type = 0xff;
92
93 while ((c = my_getchar(0)) != ':') {
94 if (c == 0x03)
95 return -1; /* Control-C */
96 if (c == 0x04) {
97 rec->status = IHX_OK;
98 return 0; /*Control-D, EOF */
99 }
100 }
101
102 if ((c = get_hexbyte()) < 0) /* Start code */
103 return -1;
104 sum = c;
105 rec->len = c;
106 if ((c = get_hexbyte()) < 0) /* Byte Count */
107 return -1;
108 sum += c;
109 rec->address = c * 256;
110 if ((c = get_hexbyte()) < 0) /* Address */
111 return -1;
112 sum += c;
113 rec->address += c;
114 if ((c = get_hexbyte()) < 0) /* Record type */
115 return -1;
116 sum += c;
117 rec->type = c;
118
119 if (rec->len) { /* Record Data */
120 uint8_t n;
121
122 for (n = 0; n < rec->len; n++) {
123 if ((c = get_hexbyte()) < 0)
124 break;
125 sum += c;
126 rec->data[n] = c;
127 }
128
129 if (n < rec->len) {
130 return -1;
131 }
132 }
133
134 c = get_hexbyte(); /* Check sum */
135
136 if (c >= 0) {
137 sum += c;
138 if (sum == 0)
139 rec->status = IHX_OK;
140 else
141 rec->status = IHX_CHKSUMERR;
142 }
143
144 return rec->len;
145 }
146
147
148 command_ret_t do_loadihex(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
149 {
150 long offset = 0;
151 uint32_t base_address = 0;
152 uint32_t address_max = detect_ramsize();
153 uint32_t address_high = 0;
154 uint32_t address_low = address_max;
155 bool firstrec = true;
156 ihex_t rec;
157
158 if (argc > 1)
159 offset = strtol(argv[1], NULL, 16);
160
161 my_puts_P(PSTR("Waiting for Intel Hex Records...\n"));
162
163 while (ihex_get_record(&rec) > 0 &&
164 rec.status == IHX_OK &&
165 rec.type != 1 ) {
166
167 switch (rec.type) {
168 case 0: /* Data record */
169 if (firstrec) {
170 my_puts_P(PSTR("Loading: 0x....."));
171 firstrec = false;
172 }
173 if (rec.len) {
174 uint32_t addr = base_address + rec.address + offset;
175 if (addr < address_low)
176 address_low = addr;
177 if (addr+rec.len > address_high)
178 address_high = addr + rec.len;
179
180 // debug("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n",
181 // address_low, address_high, address_max, addr, rec.len);
182 printf_P(PSTR("\b\b\b\b\b%.5lX"), addr);
183 if (addr < address_max) {
184 uint32_t tmplen = address_max - addr;
185 if (rec.len > tmplen)
186 rec.len = tmplen;
187
188 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
189 cmd_error(CMD_RET_FAILURE, EBUSTO, NULL);
190 //my_puts_P(PSTR("Bus timeout\n"));
191 //return CMD_RET_FAILURE;
192 }
193 z80_write_block(rec.data, /* src */
194 addr, /* dest */
195 rec.len); /* len */
196 z80_bus_cmd(Release);
197 }
198 }
199 break;
200
201 #if 0
202 case 1: /* EOF record */
203 break;
204 #endif
205 case 2: /* Extended Segment Address Record */
206 base_address = (uint32_t)((rec.data[0] << 8) + rec.data[1]) << 4;
207 break;
208
209 case 4: /* Extended Linear Address Record */
210 base_address = (uint32_t)((rec.data[0] << 8) + rec.data[1]) << 16;
211 break;
212
213 case 3: /* Start Segment Address Record (ignored)*/
214 case 5: /* Start Linear Address Record (ignored)*/
215 break;
216
217 }
218 }
219
220 if (rec.status != IHX_OK)
221 my_puts_P(PSTR("Broken Hex Record or loading interrupted!\n"));
222
223 for (uint_fast8_t i=0; i<100; ++i) {
224 /* flush input buffer */
225 while (my_getchar(0) > 0)
226 ;
227 udelay(1000);
228 }
229
230 my_puts_P(PSTR("\nData loaded: "));
231 if (address_low >= MIN(address_high, address_max))
232 my_puts_P(PSTR("None.\n"));
233 else
234 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low,
235 MIN(address_high, address_max) - 1);
236
237 if (address_high > address_max)
238 printf_P(PSTR("Data above highest RAM address "
239 "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max, address_high - 1);
240
241 return rec.status == IHX_OK ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
242 }