]>
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+
13 #include "con-utils.h"
18 uint32_t detect_ramsize(void)
21 uint8_t save_addr
, save_0
;
22 const uint8_t PATTERN_1
= 0x55;
23 const uint8_t PATTERN_2
= ~PATTERN_1
;
25 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
26 my_puts_P(PSTR("Bus timeout\n"));
31 z80_write(0, PATTERN_1
);
33 for (addr
=1; addr
< CONFIG_SYS_RAMSIZE_MAX
; addr
<<= 1) {
34 save_addr
= z80_read(addr
);
35 z80_write(addr
, PATTERN_2
);
36 if (z80_read(0) != PATTERN_1
|| z80_read(addr
) != PATTERN_2
)
38 z80_write(addr
, save_addr
);
62 int get_hexdigit(void) {
65 c
= toupper(my_getchar(1));
69 c
-= ('A' - '0' - 10);
76 int get_hexbyte(void) {
80 if ((i
= (uint8_t) get_hexdigit()) < 0x10)
81 if ((j
= (uint8_t) get_hexdigit()) < 0x10) {
90 int ihex_get_record(ihex_t
*rec
) {
95 rec
->status
= IHX_BROKEN
;
99 while ((c
= my_getchar(0)) != ':') {
101 return -1; /* Control-C */
103 rec
->status
= IHX_OK
;
104 return 0; /*Control-D, EOF */
108 if ((c
= get_hexbyte()) < 0) /* Start code */
112 if ((c
= get_hexbyte()) < 0) /* Byte Count */
115 rec
->address
= c
* 256;
116 if ((c
= get_hexbyte()) < 0) /* Address */
120 if ((c
= get_hexbyte()) < 0) /* Record type */
125 if (rec
->len
) { /* Record Data */
128 for (n
= 0; n
< rec
->len
; n
++) {
129 if ((c
= get_hexbyte()) < 0)
140 c
= get_hexbyte(); /* Check sum */
145 rec
->status
= IHX_OK
;
147 rec
->status
= IHX_CHKSUMERR
;
154 command_ret_t
do_loadihex(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
157 uint32_t base_address
= 0;
158 uint32_t address_max
= detect_ramsize();
159 uint32_t address_high
= 0;
160 uint32_t address_low
= address_max
;
161 command_ret_t rcode
= CMD_RET_FAILURE
;
163 bool firstrec
= true;
165 (void) cmdtp
; (void) flag
;
169 offset
= strtol(argv
[1], NULL
, 16);
171 printf_P(PSTR("Waiting for Intel Hex Records...\n"));
173 while (ihex_get_record(&rec
) > 0 &&
174 rec
.status
== IHX_OK
&&
178 case 0: /* Data record */
180 printf_P(PSTR("Loading: 0x....."));
184 uint32_t addr
= base_address
+ rec
.address
+ offset
;
185 if (addr
< address_low
)
187 if (addr
+rec
.len
> address_high
)
188 address_high
= addr
+ rec
.len
;
190 // debug("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n",
191 // address_low, address_high, address_max, addr, rec.len);
192 printf_P(PSTR("\b\b\b\b\b%.5lX"), addr
);
193 if (addr
< address_max
) {
194 uint32_t tmplen
= address_max
- addr
;
195 if (rec
.len
> tmplen
)
198 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
199 my_puts_P(PSTR("Bus timeout\n"));
200 return CMD_RET_FAILURE
;
202 z80_write_block(rec
.data
, /* src */
205 z80_bus_cmd(Release
);
211 case 1: /* EOF record */
214 case 2: /* Extended Segment Address Record */
215 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 4;
218 case 4: /* Extended Linear Address Record */
219 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 16;
222 case 3: /* Start Segment Address Record (ignored)*/
223 case 5: /* Start Linear Address Record (ignored)*/
229 switch (rec
.status
) {
231 rcode
= CMD_RET_SUCCESS
;
236 printf_P(PSTR("Broken Hex Record or loading interrupted!\n"));
241 for (uint_fast8_t i
=0; i
<100; ++i
) {
242 /* flush input buffer */
243 while (my_getchar(0) > 0)
249 printf_P(PSTR("\nData loaded: "));
250 if (address_low
>= MIN(address_high
, address_max
))
251 printf_P(PSTR("None.\n"));
253 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low
,
254 MIN(address_high
, address_max
) - 1);
256 if (address_high
> address_max
)
257 printf_P(PSTR("Data above highest RAM address "
258 "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max
, address_high
- 1);