-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_ihex.c
247 lines (209 loc) · 5.41 KB
/
output_ihex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "vasm.h"
#ifdef OUTIHEX
/* ihex formats */
#define I8HEX 0 /* supports 16-bit address space */
#define I16HEX 1 /* supports 20-bit address space */
#define I32HEX 2 /* supports 32-bit address space */
/* maximum address for I16HEX */
#define MEGABYTE (1 << 20) - 1
/* ihex record types */
#define REC_DAT 0 /* data */
#define REC_EOF 1 /* end of file */
#define REC_ESA 2 /* extended segment address */
#define REC_SSA 3 /* start segment address */
#define REC_ELA 4 /* extended linear address */
#define REC_SLA 5 /* start linear address */
static char *copyright = "vasm Intel HEX output module 0.1 (c) 2020 Rida Dzhaafar";
static int ihex_fmt = I8HEX; /* default ihex format */
static uint8_t *buffer; /* output buffer for data records */
static uint8_t buffer_s = 10; /* maximum buffer size */
static uint8_t buffer_i = 0; /* current index in buffer */
static uint32_t addr = 0; /* current output address */
static uint16_t ext_addr = 0; /* last written extended segment/linear address */
static void write_eof_record(FILE *f)
{
fprintf(f, ":00000001FF");
}
static void write_extended_record(FILE *f)
{
uint8_t csum;
uint16_t ext;
uint8_t type;
if (ihex_fmt == I16HEX) {
ext = ext_addr << 4;
type = REC_ESA;
}
if (ihex_fmt == I32HEX) {
ext = ext_addr;
type = REC_ELA;
}
csum = type;
csum += 2;
csum += ext >> 8;
csum += ext;
csum = (~csum) + 1;
fprintf(f, ":020000%02X%04X%02X\n", type, ext, csum);
}
static void write_data_record(FILE *f)
{
uint8_t csum;
uint8_t i;
uint16_t ext;
uint32_t start;
/* pre-flight checks */
if (buffer_i == 0)
return;
if (ihex_fmt == I8HEX && addr > UINT16_MAX)
output_error(11, addr);
if (ihex_fmt == I16HEX && addr > MEGABYTE)
output_error(11, addr);
start = addr - buffer_i;
ext = start >> 16;
start &= 0xFFFF;
/* set/reset extended address if needed */
if (ext != ext_addr) {
ext_addr = ext;
write_extended_record(f);
}
/* write data record */
fprintf(f, ":%02X%04X00", buffer_i, start);
csum = start;
csum += start >> 8;
csum += buffer_i;
for (i = 0; i < buffer_i; i++) {
csum += buffer[i];
fprintf(f, "%02X", buffer[i]);
}
csum = (~csum) + 1;
fprintf(f, "%02X\n", csum);
/* reset the buffer index */
buffer_i = 0;
}
static void buffer_data(FILE *f, uint8_t b)
{
buffer[buffer_i] = b;
buffer_i++;
addr++;
if (buffer_i == buffer_s)
write_data_record(f);
}
/* align the atom if necessary
adapted from output_srec.c */
static void align(FILE *f, section *sec, atom *a)
{
uint32_t align = balign(addr, a->align);
uint32_t i;
uint32_t len;
uint8_t *fill;
if (!align)
return;
if (a->type == SPACE && a->content.sb->space == 0) {
if (a->content.sb->maxalignbytes != 0 &&
align > a->content.sb->maxalignbytes)
return;
fill = a->content.sb->fill;
len = a->content.sb->size;
} else {
fill = sec->pad;
len = sec->padbytes;
}
while (align % len) {
buffer_data(f, 0);
align--;
}
while (align >= len) {
for(i = 0; i < len; i++) {
buffer_data(f, fill[i]);
align--;
}
}
while (len--) {
buffer_data(f, 0);
}
}
static void write_output(FILE *f, section *sec, symbol *sym)
{
uint32_t i, j;
atom *a;
section *s, *s2;
if (!sec)
return;
for (; sym; sym = sym->next)
if (sym->type == IMPORT)
output_error(6, sym->name); /* undefined symbol (sym->name) */
/* fail on overlapping sections
adapted from output_bin.c */
for (s = sec; s != NULL; s = s->next) {
for (s2 = s->next; s2; s2 = s2->next) {
if (((ULLTADDR(s2->org) >= ULLTADDR(s->org) &&
ULLTADDR(s2->org) < ULLTADDR(s->pc)) ||
(ULLTADDR(s2->pc) > ULLTADDR(s->org) &&
ULLTADDR(s2->pc) <= ULLTADDR(s->pc))))
output_error(0);
}
}
buffer = mymalloc(sizeof(uint8_t) * buffer_s);
for (s = sec; s; s = s->next) {
addr = ULLTADDR(s->org);
for (a = s->first; a; a = a->next) {
align(f, s, a);
if (a->type == DATA) {
for (i = 0; i < a->content.db->size; i++) {
buffer_data(f, a->content.db->data[i]);
}
} else if (a->type == SPACE) {
for (i = 0; i < a->content.sb->space; i++) {
for (j = 0; j < a->content.sb->size; j++) {
buffer_data(f, a->content.sb->fill[j]);
}
}
}
}
/* flush buffer before moving on to next section */
write_data_record(f);
}
write_eof_record(f);
myfree(buffer);
}
static int parse_args(char *arg)
{
uint8_t size;
if (!strcmp(arg, "-i8hex")) {
ihex_fmt = I8HEX;
return 1;
}
else if (!strcmp(arg, "-i16hex")) {
ihex_fmt = I16HEX;
return 1;
}
else if (!strcmp(arg, "-i32hex")) {
ihex_fmt = I32HEX;
return 1;
}
else if (!strncmp(arg, "-record-size=", 13)) {
size = atoi(arg + 13);
/* an IHEX record cannot be longer than FF bytes */
if (size < 1 || size > 255)
return 0;
buffer_s = size;
return 1;
}
return 0;
}
int init_output_ihex(char **cp, void (**wo)(FILE *, section *, symbol *), int (**oa)(char *))
{
if (sizeof(utaddr) > sizeof(uint32_t) || bitsperbyte != 8) {
output_error(1, cpuname); /* output module doesn't support (cpuname) */
return 0;
}
*cp = copyright;
*wo = write_output;
*oa = parse_args;
return 1;
}
#else
int init_output_ihex(char **cp, void (**wo)(FILE *, section *, symbol *), int (**oa)(char *))
{
return 0;
}
#endif