-
Notifications
You must be signed in to change notification settings - Fork 30
/
cmd2mot.c
342 lines (297 loc) · 9.09 KB
/
cmd2mot.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*****************************************************************************/
/* cmd2mot - converts FLEX CMD file to Motorola hex */
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef TYPES
#define TYPES
typedef unsigned char byte;
typedef unsigned short word;
#endif
#ifndef NULL
#define NULL 0
#endif
#ifndef FALSE
#define TRUE (1==1)
#define FALSE (!TRUE)
#endif
/*****************************************************************************/
/* Global Data */
/*****************************************************************************/
byte *memory = NULL;
byte *label = NULL;
char **lblnames = NULL;
char **commentlines = NULL;
static char *Options[]=
{
"out",
NULL
};
#define OPCODE(address) memory[address&0xffff]
#define ARGBYTE(address) memory[address&0xffff]
#define ARGWORD(address) (word)((memory[address&0xffff]<<8)|memory[(address+1)&0xffff])
#define ATTRBYTE(address) (label[(address) & 0xffff])
#define LABELTYPE(address) (ATTRBYTE(address) & 0x03)
#define LABELTYBE_ISLABEL (0x02)
#define LABEL_REFERENCED(address) ((ATTRBYTE(address) & 0x04) >> 2)
#define USEDBYTE(address) ((ATTRBYTE(address) & 0x80) >> 7)
#define AREATYPE(address) ((ATTRBYTE(address) & 0x78) >> 3)
#define AREATYPE_CODE 0x40
#define AREATYPE_DATA 0x20
#define AREATYPE_BINARY (AREATYPE_DATA | 0x10)
#define AREATYPE_WORD (AREATYPE_DATA | 0x08)
/*****************************************************************************/
/* usage : displays usage */
/*****************************************************************************/
void usage(void)
{
printf("* Usage: cmd2mot [options] <filename>\n"
"* Available options are:\n"
"* -out - output file [stdout]\n");
exit(1);
}
/*****************************************************************************/
/* SFlexRecord : a record in a binary FLEX9 disk file */
/*****************************************************************************/
#pragma pack(1)
struct SFlexRecord
{
byte bSOI; /* start of record indicator */
byte bLoadAddress[2]; /* Hi/Lo byte of load address */
byte bDataLen; /* length of data record */
byte bData[255]; /* data record */
};
#pragma pack()
int IsTransferAddress(struct SFlexRecord *pRec)
{ return (pRec->bSOI == 0x16); }
int IsRecord(struct SFlexRecord *pRec)
{ return (pRec->bSOI == 0x02); }
int GetSize(struct SFlexRecord *pRec)
{ return pRec->bDataLen; }
int GetLoadAddress(struct SFlexRecord *pRec)
{ return (((int)(pRec->bLoadAddress[0])) << 8) | pRec->bLoadAddress[1]; }
byte * GetData(struct SFlexRecord *pRec)
{ return pRec->bData; }
/*****************************************************************************/
/* ReadFlexRecord : read one record of a FLEX9 binary */
/*****************************************************************************/
int ReadFlexRecord(FILE *f, struct SFlexRecord *pRecord)
{
int nCurPos = ftell(f);
byte bCur = 0;
int i;
while (!bCur) /* read 1st byte, skipping 0 bytes */
if (!fread(&bCur, 1, 1, f))
return FALSE;
switch (bCur) /* OK, so what's that? */
{
case 0x02 : /* Start of Record Indicator ? */
{
pRecord->bSOI = bCur;
if ((!fread(pRecord->bLoadAddress, 2, 1, f)) ||
(!fread(&pRecord->bDataLen, 1, 1, f)))
return FALSE;
for (i = 0; i < pRecord->bDataLen; i++)
if (!fread(&pRecord->bData[i], 1, 1, f))
return FALSE;
}
break;
case 0x16 : /* Transfer Address ? */
pRecord->bSOI = bCur;
if (!fread(pRecord->bLoadAddress, 2, 1, f))
return FALSE;
break;
default :
fseek(f, nCurPos, SEEK_SET); /* seek back */
return FALSE;
}
return TRUE;
}
/*****************************************************************************/
/* IsFlex : returns whether this is a FLEX binary */
/*****************************************************************************/
int IsFlex(FILE *f, byte *memory, unsigned *pbegin, unsigned *pend, long *load)
{
struct SFlexRecord rec;
int nCurPos = ftell(f);
int nRecs = 0;
int begin = 0x10000;
int end = 0;
int i;
while (ReadFlexRecord(f, &rec))
{
int nStart = GetLoadAddress(&rec);
int nEnd = nStart + GetSize(&rec) - 1;
nRecs++;
if (nStart < begin)
begin = nStart;
if (nEnd > end)
end = nEnd;
if (IsRecord(&rec) && GetSize(&rec))
{
for (i = nStart; i <= nEnd; i++) /* mark area as used */
ATTRBYTE(i) |= 0x80; /* mark as used byte */
memcpy(memory + nStart,
GetData(&rec),
GetSize(&rec));
}
else if (IsTransferAddress(&rec))
*load = GetLoadAddress(&rec);
}
if (fgetc(f) != EOF) /* if not read through the whole file*/
{
for (i = begin; i <= end; i++) /* mark area as UNused */
ATTRBYTE(i) &= ~0x80; /* mark as used byte */
nRecs = 0; /* this ain't no valid FLEX file */
}
fseek(f, nCurPos, SEEK_SET);
if (nRecs > 0)
{
if (begin < (int)(*pbegin))
*pbegin = begin;
if (end > (int)*pend)
*pend = end;
}
return (nRecs > 0);
}
/*****************************************************************************/
/* writeS1rec : write Motorola S1 record */
/*****************************************************************************/
void writeS1rec(FILE *objfile, unsigned hexaddr, int hexcount)
{
int i;
int chksum = 0;
fprintf(objfile, "S1%02X%04X", (hexcount + 3) & 0xff, hexaddr & 0xffff);
for (i = 0; i < hexcount; i++)
{
byte b = memory[hexaddr + i];
chksum += b;
fprintf(objfile, "%02X", b);
}
chksum += (hexaddr & 0xff) + ((hexaddr >> 8) & 0xff) + hexcount + 3;
fprintf(objfile, "%02X\r\n", 0xff - (chksum & 0xff));
}
/*****************************************************************************/
/* writeS9rec : write Motorola S9 record */
/*****************************************************************************/
void writeS9rec(FILE *objfile, unsigned tfraddr)
{
int chksum = (tfraddr & 0xff) + ((tfraddr >> 8) & 0xff) + 3;
fprintf(objfile, "S903%04X%02X\n", tfraddr, 0xff - (chksum & 0xff));
}
/*****************************************************************************/
/* writeS1range : writes out a memory range as Motorola S1 records */
/*****************************************************************************/
void writeS1range(FILE *objfile, unsigned hexaddr, int hexcount)
{
while (hexcount > 16)
{
writeS1rec(objfile, hexaddr, 16);
hexaddr += 16;
hexcount -= 16;
}
writeS1rec(objfile, hexaddr, hexcount);
}
/*****************************************************************************/
/* main : main program function */
/*****************************************************************************/
int main(int argc, char *argv[])
{
unsigned begin = 0x10000,end = 0, offset = 0;
long load = -1;
char *fname = NULL, *outname = NULL;
int i, j, n;
int off;
FILE *f;
FILE *out = stdout;
printf("cmd2mot: convert FLEX Binary to Motorola .HEX\n");
for (i = 1, n = 0; i < argc; ++i)
{
if (argv[i][0] != '-')
{
switch (++n)
{
case 1:
fname = argv[i];
break;
default:
usage();
}
}
else
{
for (j = 0; Options[j]; ++j)
if (!strcmp(argv[i] + 1, Options[j]))
break;
switch (j)
{
case 0:
++i;
if (i > argc)
usage();
outname = argv[i];
break;
default:
usage();
}
}
}
if (!fname)
usage();
f = fopen(fname,"rb");
if (!f)
usage();
if (!end)
{
fseek(f,0,SEEK_END);
off = ftell(f);
end = (offset+off)-1;
rewind(f);
}
memory = (byte *)malloc(0x10000);
label = (byte *)malloc(0x10000);
if ((!memory) || (!label))
{
printf("no mem buffer\n");
goto exit;
}
memset(memory, 0x01, 0x10000);
memset(label, 0x00, 0x10000);
if (outname)
{
out = fopen(outname, "wb");
if (!out)
{
printf("can't open %s \n",outname);
out = stdout;
}
}
if ((IsFlex(f, memory, &begin, &end, &load)) &&
(out != stdout))
{
unsigned long blkstrt = begin;
while (blkstrt <= end)
{
unsigned long after = blkstrt;
while (after <= end && (ATTRBYTE(after) & 0x80))
after++;
writeS1range(out, blkstrt, after - blkstrt);
blkstrt = after;
while (blkstrt <= end && !(ATTRBYTE(blkstrt) & 0x80))
blkstrt++;
}
if (load >= 0 && load <= 0xffff)
writeS9rec(out, (unsigned int)load);
}
exit:
if (f)
fclose(f);
if (outname)
if(out)
fclose(out);
if (memory)
free(memory);
return 0;
}