]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/ihex.c
2 * (C) Copyright 2015 Leo C. <erbl259-lmu@yahoo.de>
4 * SPDX-License-Identifier: GPL-2.0+
13 #include "con-utils.h"
34 int get_hexdigit(void) {
37 c
= toupper(my_getchar(1));
41 c
-= ('A' - '0' - 10);
48 int get_hexbyte(void) {
52 if ((i
= (uint8_t) get_hexdigit()) < 0x10)
53 if ((j
= (uint8_t) get_hexdigit()) < 0x10) {
61 ihex_t
ihex_get_record() {
65 ihex_t rec
= { IHX_BROKEN
, 0, 0, 0, 0 };
68 while ((c
= my_getchar(0)) != ':')
72 if ((i
= get_hexbyte()) < 0) /* Start code */
76 if ((i
= get_hexbyte()) < 0) /* Byte Count */
79 rec
.address
= i
* 256;
80 if ((i
= get_hexbyte()) < 0) /* Address */
84 if ((i
= get_hexbyte()) < 0) /* Record type */
89 if (rec
.len
) { /* Record Data */
91 if ((rec
.data
= malloc(rec
.len
)) == 0)
94 for (p
=rec
.data
, k
=rec
.len
; k
; k
--) {
95 if ((i
= get_hexbyte()) < 0)
102 free(rec
.data
); rec
.data
= 0;
107 i
= get_hexbyte(); /* Check sum */
114 rec
.status
= IHX_CHKSUMERR
;
117 if (rec
.status
!= IHX_OK
) {
126 command_ret_t
do_loadihex(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
129 uint32_t base_address
= 0;
130 uint32_t address_low
= 0xffffffff;
131 uint32_t address_high
= 0;
133 command_ret_t rcode
= CMD_RET_FAILURE
;
140 offset
= strtol(argv
[1], NULL
, 16);
143 rec
= ihex_get_record();
145 if (rec
.status
== IHX_OK
) {
147 case 0: /* Data record */
149 uint32_t addr
= base_address
+ rec
.address
+ offset
;
150 if (addr
< address_low
)
152 if (addr
+rec
.len
> address_high
)
153 address_high
= addr
+ rec
.len
;
155 z80_bus_cmd(Request
);
156 z80_write_block(rec
.data
, /* src */
159 z80_bus_cmd(Release
);
164 case 1: /* EOF record */
169 case 2: /* Extended Segment Address Record */
170 base_address
= ((rec
.data
[0] << 8) + rec
.data
[1]) << 4;
174 case 4: /* Extended Linear Address Record */
175 base_address
= (uint32_t)((rec
.data
[0] << 8) + rec
.data
[1]) << 16;
179 case 3: /* Start Segment Address Record (ignored)*/
180 case 5: /* Start Linear Address Record (ignored)*/
194 switch (rec
.status
) {
196 rcode
= CMD_RET_SUCCESS
;
201 printf_P(PSTR("Broken Hex Record or loading interrupted!\n"));
205 printf_P(PSTR("Data loaded - "));
206 if (address_low
>= address_high
)
207 printf_P(PSTR("None.\n"));
209 printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low
, address_high
);