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