]>
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
;
98 while ((c
= my_getchar(0)) != ':')
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
, int flag
, 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 command_ret_t rcode
= CMD_RET_FAILURE
;
158 (void) cmdtp
; (void) flag
;
162 offset
= strtol(argv
[1], NULL
, 16);
164 while (ihex_get_record(&rec
) > 0 &&
165 rec
.status
== IHX_OK
&&
169 case 0: /* Data record */
171 uint32_t addr
= base_address
+ rec
.address
+ offset
;
172 if (addr
< address_low
)
174 if (addr
+rec
.len
> address_high
)
175 address_high
= addr
+ rec
.len
;
177 // debug("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n",
178 // address_low, address_high, address_max, addr, rec.len);
180 if (addr
< address_max
) {
181 uint32_t tmplen
= address_max
- addr
;
182 if (rec
.len
> tmplen
)
185 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
186 my_puts_P(PSTR("Bus timeout\n"));
187 return CMD_RET_FAILURE
;
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 switch (rec
.status
) {
218 rcode
= CMD_RET_SUCCESS
;
223 printf_P(PSTR("Broken Hex Record or loading interrupted!\n"));
227 printf_P(PSTR("Data loaded: "));
228 if (address_low
>= MIN(address_high
, address_max
))
229 printf_P(PSTR("None.\n"));
231 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low
,
232 MIN(address_high
, address_max
) - 1);
234 if (address_high
> address_max
)
235 printf_P(PSTR("Data above highest RAM address "
236 "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max
, address_high
- 1);