]>
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"
29 static int get_hexdigit(void)
32 c
= toupper(my_getchar(1));
36 c
-= ('A' - '0' - 10);
42 static int get_hexbyte(void)
46 if ((i
= (uint8_t) get_hexdigit()) < 0x10)
47 if ((j
= (uint8_t) get_hexdigit()) < 0x10) {
55 static int ihex_get_record(ihex_t
*rec
)
60 rec
->status
= IHX_BROKEN
;
64 while ((c
= my_getchar(0)) != ':') {
66 return -1; /* Control-C */
69 return 0; /*Control-D, EOF */
73 if ((c
= get_hexbyte()) < 0) /* Start code */
77 if ((c
= get_hexbyte()) < 0) /* Byte Count */
80 rec
->address
= c
* 256;
81 if ((c
= get_hexbyte()) < 0) /* Address */
85 if ((c
= get_hexbyte()) < 0) /* Record type */
90 if (rec
->len
) { /* Record Data */
93 for (n
= 0; n
< rec
->len
; n
++) {
94 if ((c
= get_hexbyte()) < 0)
105 c
= get_hexbyte(); /* Check sum */
110 rec
->status
= IHX_OK
;
112 rec
->status
= IHX_CHKSUMERR
;
119 command_ret_t
do_loadihex(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
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;
130 offset
= strtol(argv
[1], NULL
, 16);
132 int32_t ram
= z80_memsize_detect();
134 cmd_error(CMD_RET_FAILURE
, (ERRNUM
) -ram
, NULL
);
137 address_low
= address_max
;
139 my_puts_P(PSTR("Waiting for Intel Hex Records...\n"));
141 while (ihex_get_record(&rec
) > 0 &&
142 rec
.status
== IHX_OK
&&
146 case 0: /* Data record */
148 my_puts_P(PSTR("Loading: 0x....."));
152 uint32_t addr
= base_address
+ rec
.address
+ offset
;
153 if (addr
< address_low
)
155 if (addr
+rec
.len
> address_high
)
156 address_high
= addr
+ rec
.len
;
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
)
166 z80_bus_request_or_exit();
167 z80_write_block(rec
.data
, /* src */
170 z80_bus_cmd(Release
);
176 case 1: /* EOF record */
179 case 2: /* Extended Segment Address Record */
180 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 4;
183 case 4: /* Extended Linear Address Record */
184 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 16;
187 case 3: /* Start Segment Address Record (ignored)*/
188 case 5: /* Start Linear Address Record (ignored)*/
194 if (rec
.status
!= IHX_OK
)
195 my_puts_P(PSTR("Broken Hex Record or loading interrupted!\n"));
197 for (uint_fast8_t i
=0; i
<100; ++i
) {
198 /* flush input buffer */
199 while (my_getchar(0) > 0)
204 my_puts_P(PSTR("\nData loaded: "));
205 if (address_low
>= MIN(address_high
, address_max
))
206 my_puts_P(PSTR("None.\n"));
208 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low
,
209 MIN(address_high
, address_max
) - 1);
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);
215 return rec
.status
== IHX_OK
? CMD_RET_SUCCESS
: CMD_RET_FAILURE
;