]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2015 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0 | |
5 | */ | |
6 | ||
7 | #include "cmd_loadihex.h" | |
8 | #include <ctype.h> | |
9 | ||
10 | #include "con-utils.h" | |
11 | #include "z80-if.h" | |
12 | #include "debug.h" | |
13 | ||
14 | ||
15 | typedef enum { | |
16 | IHX_OK, | |
17 | IHX_BROKEN, | |
18 | IHX_CHKSUMERR | |
19 | } ihx_rstat_t; | |
20 | ||
21 | typedef struct { | |
22 | ihx_rstat_t status; | |
23 | int8_t type; | |
24 | uint8_t len; | |
25 | uint16_t address; | |
26 | uint8_t data[256]; | |
27 | } ihex_t; | |
28 | ||
29 | ||
30 | static int get_hexdigit(void) | |
31 | { | |
32 | int c; | |
33 | c = toupper(my_getchar(1)); | |
34 | if (isxdigit(c)) { | |
35 | c -= '0'; | |
36 | if (c > 9) | |
37 | c -= ('A' - '0' - 10); | |
38 | return c; | |
39 | } else | |
40 | return -1; | |
41 | } | |
42 | ||
43 | static int get_hexbyte(void) | |
44 | { | |
45 | uint8_t i,j; | |
46 | ||
47 | if ((i = (uint8_t) get_hexdigit()) < 0x10) | |
48 | if ((j = (uint8_t) get_hexdigit()) < 0x10) { | |
49 | return (i<<4) + j; | |
50 | } | |
51 | ||
52 | return -1; | |
53 | } | |
54 | ||
55 | ||
56 | static int ihex_get_record(ihex_t *rec) | |
57 | { | |
58 | int c; | |
59 | uint8_t sum; | |
60 | ||
61 | rec->status = IHX_BROKEN; | |
62 | rec->len = 0; | |
63 | rec->type = 0xff; | |
64 | ||
65 | while ((c = my_getchar(0)) != ':') { | |
66 | if (c == 0x03) | |
67 | return -1; /* Control-C */ | |
68 | if (c == 0x04) { | |
69 | rec->status = IHX_OK; | |
70 | return 0; /*Control-D, EOF */ | |
71 | } | |
72 | } | |
73 | ||
74 | if ((c = get_hexbyte()) < 0) /* Start code */ | |
75 | return -1; | |
76 | sum = c; | |
77 | rec->len = c; | |
78 | if ((c = get_hexbyte()) < 0) /* Byte Count */ | |
79 | return -1; | |
80 | sum += c; | |
81 | rec->address = c * 256; | |
82 | if ((c = get_hexbyte()) < 0) /* Address */ | |
83 | return -1; | |
84 | sum += c; | |
85 | rec->address += c; | |
86 | if ((c = get_hexbyte()) < 0) /* Record type */ | |
87 | return -1; | |
88 | sum += c; | |
89 | rec->type = c; | |
90 | ||
91 | if (rec->len) { /* Record Data */ | |
92 | uint8_t n; | |
93 | ||
94 | for (n = 0; n < rec->len; n++) { | |
95 | if ((c = get_hexbyte()) < 0) | |
96 | break; | |
97 | sum += c; | |
98 | rec->data[n] = c; | |
99 | } | |
100 | ||
101 | if (n < rec->len) { | |
102 | return -1; | |
103 | } | |
104 | } | |
105 | ||
106 | c = get_hexbyte(); /* Check sum */ | |
107 | ||
108 | if (c >= 0) { | |
109 | sum += c; | |
110 | if (sum == 0) | |
111 | rec->status = IHX_OK; | |
112 | else | |
113 | rec->status = IHX_CHKSUMERR; | |
114 | } | |
115 | ||
116 | return rec->len; | |
117 | } | |
118 | ||
119 | ||
120 | command_ret_t do_loadihex(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) | |
121 | { | |
122 | long offset = 0; | |
123 | uint32_t base_address = 0; | |
124 | uint32_t address_high = 0; | |
125 | uint32_t address_max; | |
126 | uint32_t address_low; | |
127 | bool firstrec = true; | |
128 | ihex_t rec; | |
129 | ||
130 | if (argc > 1) | |
131 | offset = strtol(argv[1], NULL, 16); | |
132 | ||
133 | int32_t ram = z80_memsize_detect(); | |
134 | if (ram < 0) | |
135 | cmd_error(CMD_RET_FAILURE, (ERRNUM) -ram, NULL); | |
136 | ||
137 | address_max = ram; | |
138 | address_low = address_max; | |
139 | ||
140 | my_puts_P(PSTR("Waiting for Intel Hex Records...\n")); | |
141 | ||
142 | while (ihex_get_record(&rec) > 0 && | |
143 | rec.status == IHX_OK && | |
144 | rec.type != 1 ) { | |
145 | ||
146 | switch (rec.type) { | |
147 | case 0: /* Data record */ | |
148 | if (firstrec) { | |
149 | my_puts_P(PSTR("Loading: 0x.....")); | |
150 | firstrec = false; | |
151 | } | |
152 | if (rec.len) { | |
153 | uint32_t addr = base_address + rec.address + offset; | |
154 | if (addr < address_low) | |
155 | address_low = addr; | |
156 | if (addr+rec.len > address_high) | |
157 | address_high = addr + rec.len; | |
158 | ||
159 | // debug("low: 0x%.5lX, high: 0x%.5lX, max: 0x%.5lX, addr: 0x%.5lX, len: %d\n", | |
160 | // address_low, address_high, address_max, addr, rec.len); | |
161 | printf_P(PSTR("\b\b\b\b\b%.5lX"), addr); | |
162 | if (addr < address_max) { | |
163 | uint32_t tmplen = address_max - addr; | |
164 | if (rec.len > tmplen) | |
165 | rec.len = tmplen; | |
166 | ||
167 | z80_bus_request_or_exit(); | |
168 | z80_write_block(rec.data, /* src */ | |
169 | addr, /* dest */ | |
170 | rec.len); /* len */ | |
171 | z80_bus_cmd(Release); | |
172 | } | |
173 | } | |
174 | break; | |
175 | ||
176 | #if 0 | |
177 | case 1: /* EOF record */ | |
178 | break; | |
179 | #endif | |
180 | case 2: /* Extended Segment Address Record */ | |
181 | base_address = (uint32_t)((rec.data[0] << 8) + rec.data[1]) << 4; | |
182 | break; | |
183 | ||
184 | case 4: /* Extended Linear Address Record */ | |
185 | base_address = (uint32_t)((rec.data[0] << 8) + rec.data[1]) << 16; | |
186 | break; | |
187 | ||
188 | case 3: /* Start Segment Address Record (ignored)*/ | |
189 | case 5: /* Start Linear Address Record (ignored)*/ | |
190 | break; | |
191 | ||
192 | } | |
193 | } | |
194 | ||
195 | if (rec.status != IHX_OK) | |
196 | my_puts_P(PSTR("Broken Hex Record or loading interrupted!\n")); | |
197 | ||
198 | for (uint_fast8_t i=0; i<100; ++i) { | |
199 | /* flush input buffer */ | |
200 | while (my_getchar(0) > 0) | |
201 | ; | |
202 | udelay(1000); | |
203 | } | |
204 | ||
205 | my_puts_P(PSTR("\nData loaded: ")); | |
206 | if (address_low >= MIN(address_high, address_max)) | |
207 | my_puts_P(PSTR("None.\n")); | |
208 | else | |
209 | printf_P(PSTR("low: 0x%.5lX high: 0x%.5lX\n"), address_low, | |
210 | MIN(address_high, address_max) - 1); | |
211 | ||
212 | if (address_high > address_max) | |
213 | printf_P(PSTR("Data above highest RAM address " | |
214 | "(in range 0x%.5lX - 0x%.5lX) ignored!\n"), address_max, address_high - 1); | |
215 | ||
216 | return rec.status == IHX_OK ? CMD_RET_SUCCESS : CMD_RET_FAILURE; | |
217 | } |