]>
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 static 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
);
58 static int get_hexdigit(void)
61 c
= toupper(my_getchar(1));
65 c
-= ('A' - '0' - 10);
71 static int get_hexbyte(void)
75 if ((i
= (uint8_t) get_hexdigit()) < 0x10)
76 if ((j
= (uint8_t) get_hexdigit()) < 0x10) {
84 static int ihex_get_record(ihex_t
*rec
)
89 rec
->status
= IHX_BROKEN
;
93 while ((c
= my_getchar(0)) != ':') {
95 return -1; /* Control-C */
98 return 0; /*Control-D, EOF */
102 if ((c
= get_hexbyte()) < 0) /* Start code */
106 if ((c
= get_hexbyte()) < 0) /* Byte Count */
109 rec
->address
= c
* 256;
110 if ((c
= get_hexbyte()) < 0) /* Address */
114 if ((c
= get_hexbyte()) < 0) /* Record type */
119 if (rec
->len
) { /* Record Data */
122 for (n
= 0; n
< rec
->len
; n
++) {
123 if ((c
= get_hexbyte()) < 0)
134 c
= get_hexbyte(); /* Check sum */
139 rec
->status
= IHX_OK
;
141 rec
->status
= IHX_CHKSUMERR
;
148 command_ret_t
do_loadihex(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
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;
159 offset
= strtol(argv
[1], NULL
, 16);
161 my_puts_P(PSTR("Waiting for Intel Hex Records...\n"));
163 while (ihex_get_record(&rec
) > 0 &&
164 rec
.status
== IHX_OK
&&
168 case 0: /* Data record */
170 my_puts_P(PSTR("Loading: 0x....."));
174 uint32_t addr
= base_address
+ rec
.address
+ offset
;
175 if (addr
< address_low
)
177 if (addr
+rec
.len
> address_high
)
178 address_high
= addr
+ rec
.len
;
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
)
188 z80_bus_request_or_exit();
189 z80_write_block(rec
.data
, /* src */
192 z80_bus_cmd(Release
);
198 case 1: /* EOF record */
201 case 2: /* Extended Segment Address Record */
202 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 4;
205 case 4: /* Extended Linear Address Record */
206 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 16;
209 case 3: /* Start Segment Address Record (ignored)*/
210 case 5: /* Start Linear Address Record (ignored)*/
216 if (rec
.status
!= IHX_OK
)
217 my_puts_P(PSTR("Broken Hex Record or loading interrupted!\n"));
219 for (uint_fast8_t i
=0; i
<100; ++i
) {
220 /* flush input buffer */
221 while (my_getchar(0) > 0)
226 my_puts_P(PSTR("\nData loaded: "));
227 if (address_low
>= MIN(address_high
, address_max
))
228 my_puts_P(PSTR("None.\n"));
230 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low
,
231 MIN(address_high
, address_max
) - 1);
233 if (address_high
> address_max
)
234 printf_P(PSTR("Data above highest RAM address "
235 "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max
, address_high
- 1);
237 return rec
.status
== IHX_OK
? CMD_RET_SUCCESS
: CMD_RET_FAILURE
;