]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_loadihex.c
2 * (C) Copyright 2015 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0
7 #include "cmd_loadihex.h"
10 #include "con-utils.h"
15 uint32_t detect_ramsize(void)
18 uint8_t save_addr
, save_0
;
19 const uint8_t PATTERN_1
= 0x55;
20 const uint8_t PATTERN_2
= ~PATTERN_1
;
22 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
23 my_puts_P(PSTR("Bus timeout\n"));
28 z80_write(0, PATTERN_1
);
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
)
35 z80_write(addr
, save_addr
);
59 int get_hexdigit(void) {
62 c
= toupper(my_getchar(1));
66 c
-= ('A' - '0' - 10);
73 int get_hexbyte(void) {
77 if ((i
= (uint8_t) get_hexdigit()) < 0x10)
78 if ((j
= (uint8_t) get_hexdigit()) < 0x10) {
87 int ihex_get_record(ihex_t
*rec
) {
92 rec
->status
= IHX_BROKEN
;
96 while ((c
= my_getchar(0)) != ':') {
98 return -1; /* Control-C */
100 rec
->status
= IHX_OK
;
101 return 0; /*Control-D, EOF */
105 if ((c
= get_hexbyte()) < 0) /* Start code */
109 if ((c
= get_hexbyte()) < 0) /* Byte Count */
112 rec
->address
= c
* 256;
113 if ((c
= get_hexbyte()) < 0) /* Address */
117 if ((c
= get_hexbyte()) < 0) /* Record type */
122 if (rec
->len
) { /* Record Data */
125 for (n
= 0; n
< rec
->len
; n
++) {
126 if ((c
= get_hexbyte()) < 0)
137 c
= get_hexbyte(); /* Check sum */
142 rec
->status
= IHX_OK
;
144 rec
->status
= IHX_CHKSUMERR
;
151 command_ret_t
do_loadihex(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
154 uint32_t base_address
= 0;
155 uint32_t address_max
= detect_ramsize();
156 uint32_t address_high
= 0;
157 uint32_t address_low
= address_max
;
158 command_ret_t rcode
= CMD_RET_FAILURE
;
160 bool firstrec
= true;
162 (void) cmdtp
; (void) flag
;
166 offset
= strtol(argv
[1], NULL
, 16);
168 printf_P(PSTR("Waiting for Intel Hex Records...\n"));
170 while (ihex_get_record(&rec
) > 0 &&
171 rec
.status
== IHX_OK
&&
175 case 0: /* Data record */
177 printf_P(PSTR("Loading: 0x....."));
181 uint32_t addr
= base_address
+ rec
.address
+ offset
;
182 if (addr
< address_low
)
184 if (addr
+rec
.len
> address_high
)
185 address_high
= addr
+ rec
.len
;
187 // debug("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n",
188 // address_low, address_high, address_max, addr, rec.len);
189 printf_P(PSTR("\b\b\b\b\b%.5lX"), addr
);
190 if (addr
< address_max
) {
191 uint32_t tmplen
= address_max
- addr
;
192 if (rec
.len
> tmplen
)
195 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
196 my_puts_P(PSTR("Bus timeout\n"));
197 return CMD_RET_FAILURE
;
199 z80_write_block(rec
.data
, /* src */
202 z80_bus_cmd(Release
);
208 case 1: /* EOF record */
211 case 2: /* Extended Segment Address Record */
212 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 4;
215 case 4: /* Extended Linear Address Record */
216 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 16;
219 case 3: /* Start Segment Address Record (ignored)*/
220 case 5: /* Start Linear Address Record (ignored)*/
226 switch (rec
.status
) {
228 rcode
= CMD_RET_SUCCESS
;
233 printf_P(PSTR("Broken Hex Record or loading interrupted!\n"));
238 for (uint_fast8_t i
=0; i
<100; ++i
) {
239 /* flush input buffer */
240 while (my_getchar(0) > 0)
246 printf_P(PSTR("\nData loaded: "));
247 if (address_low
>= MIN(address_high
, address_max
))
248 printf_P(PSTR("None.\n"));
250 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low
,
251 MIN(address_high
, address_max
) - 1);
253 if (address_high
> address_max
)
254 printf_P(PSTR("Data above highest RAM address "
255 "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max
, address_high
- 1);