-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupport.c
564 lines (532 loc) · 12.7 KB
/
support.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright (C) 1999-2012 Core Technologies.
//
// This file is part of tpasm.
//
// tpasm is free software; you can redistribute it and/or modify
// it under the terms of the tpasm LICENSE AGREEMENT.
//
// tpasm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// tpasm LICENSE AGREEMENT for more details.
//
// You should have received a copy of the tpasm LICENSE AGREEMENT
// along with tpasm; see the file "LICENSE.TXT".
// Support routines used by numerous processors to help keep the
// amount of duplicated code to a minimum
#include "include.h"
void ReportDisallowedLabel(const PARSED_LABEL *lineLabel)
// complain that a label is not allowed on this op
{
AssemblyComplaint(NULL,false,"Label '%s' not allowed here\n",lineLabel->name);
}
void ReportBadOperands()
// complain that the operands are bad
{
AssemblyComplaint(NULL,true,"Invalid operands\n");
}
bool CheckSignedByteRange(int value,bool generateMessage,bool isError)
// see if value looks like a signed byte
// if not, return false, and generate a warning or error if told to
{
if(value>=-128&&value<128)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Signed byte value (0x%X) out of range\n",value);
}
return(false);
}
bool CheckUnsignedByteRange(int value,bool generateMessage,bool isError)
// see if value looks like an unsigned byte
// if not, return false, and generate a warning or error if told to
{
if(value>=0&&value<256)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Unsigned byte value (0x%X) out of range\n",value);
}
return(false);
}
bool CheckByteRange(int value,bool generateMessage,bool isError)
// see if value looks like a signed byte, or an unsigned byte
// if not, return false, and generate a warning or error if told to
{
if(value>=-128&&value<256)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Byte value (0x%X) out of range\n",value);
}
return(false);
}
bool CheckSignedWordRange(int value,bool generateMessage,bool isError)
// see if value looks like a signed word
// if not, return false, and generate a warning or error if told to
{
if(value>=-32768&&value<32768)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Signed word value (0x%X) out of range\n",value);
}
return(false);
}
bool CheckUnsignedWordRange(int value,bool generateMessage,bool isError)
// see if value looks like an unsigned word
// if not, return false, and generate a warning or error if told to
{
if(value>=0&&value<65536)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Unsigned word value (0x%X) out of range\n",value);
}
return(false);
}
bool CheckWordRange(int value,bool generateMessage,bool isError)
// see if value looks like a signed word, or an unsigned word
// if not, return false, and generate a warning or error if told to
{
if(value>=-32768&&value<65536)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Word value (0x%X) out of range\n",value);
}
return(false);
}
bool Check8BitIndexRange(int value,bool generateMessage,bool isError)
// check value to see if it looks good as 8 bit index
// complain if not
{
if(value>=0&&value<8)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Bit index (0x%X) out of range\n",value);
}
return(false);
}
bool Check16BitIndexRange(int value,bool generateMessage,bool isError)
// check value to see if it looks good as 16 bit index
// complain if not
{
if(value>=0&&value<16)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Bit index (0x%X) out of range\n",value);
}
return(false);
}
bool Check32BitIndexRange(int value,bool generateMessage,bool isError)
// check value to see if it looks good as 32 bit index
// complain if not
{
if(value>=0&&value<32)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Bit index (0x%X) out of range\n",value);
}
return(false);
}
bool Check8RelativeRange(int value,bool generateMessage,bool isError)
// check value to see if it looks good as 8 bit relative offset
// complain if not
{
if(value>=-128&&value<128)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Relative offset (0x%X) out of range\n",value);
}
return(false);
}
bool Check16RelativeRange(int value,bool generateMessage,bool isError)
// check value to see if it looks good as 16 bit relative offset
// complain if not
{
if(value>=-32768&&value<32768)
{
return(true);
}
if(generateMessage)
{
AssemblyComplaint(NULL,isError,"Relative offset (0x%X) out of range\n",value);
}
return(false);
}
bool GenerateByte(unsigned char value,LISTING_RECORD *listingRecord)
// output the value to the current segment
// This will return false only if a "hard" error occurs
{
bool
fail;
unsigned int
length;
fail=false;
if(currentSegment)
{
if(!intermediatePass) // only do the real work if necessary
{
length=strlen(listingRecord->listObjectString);
if(length+4<MAX_STRING)
{
sprintf(&listingRecord->listObjectString[length],"%02X ",value); // create list file output
}
fail=!AddBytesToSegment(currentSegment,currentSegment->currentPC,&value,1);
}
currentSegment->currentPC++;
}
else
{
AssemblyComplaint(NULL,true,"Code cannot occur outside of a segment\n");
}
return(!fail);
}
bool GenerateWord(unsigned int value,LISTING_RECORD *listingRecord,bool bigEndian)
// output the value to the current segment as a word in the given endian.
// This will return false only if a "hard" error occurs
{
if(bigEndian)
{
if(GenerateByte(value>>8,listingRecord))
{
return(GenerateByte(value&0xFF,listingRecord));
}
}
else
{
if(GenerateByte(value&0xFF,listingRecord))
{
return(GenerateByte(value>>8,listingRecord));
}
}
return(false);
}
bool HandleDB(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord)
// declaring bytes of data
{
int
value;
bool
unresolved;
char
outputString[MAX_STRING];
unsigned int
stringLength;
bool
done,
fail;
unsigned int
i;
done=false;
fail=!ProcessLineLocationLabel(lineLabel); // deal with any label on the line
while(!done&&!fail)
{
if(ParseQuotedString(line,lineIndex,'"','"',outputString,&stringLength))
{
i=0;
while(!fail&&i<stringLength)
{
fail=!GenerateByte(outputString[i],listingRecord);
i++;
}
}
else if(ParseExpression(line,lineIndex,&value,&unresolved))
{
CheckByteRange(value,true,true);
fail=!GenerateByte(value&0xFF,listingRecord);
}
else
{
ReportBadOperands();
done=true;
}
if(!done&&!fail) // look for separator, or comment
{
if(ParseComment(line,lineIndex))
{
done=true;
}
else if(ParseCommaSeparator(line,lineIndex))
{
}
else
{
ReportBadOperands();
done=true;
}
}
}
return(!fail);
}
static bool HandleDW(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord,bool bigEndian)
// Declare words of data in the given endian
{
int
value;
bool
unresolved;
char
outputString[MAX_STRING];
unsigned int
stringLength;
bool
done,
fail;
unsigned int
i;
done=false;
fail=!ProcessLineLocationLabel(lineLabel); // deal with any label on the line
while(!done&&!fail)
{
if(ParseQuotedString(line,lineIndex,'"','"',outputString,&stringLength))
{
i=0;
while(!fail&&i<stringLength)
{
fail=!GenerateWord(outputString[i],listingRecord,bigEndian);
i++;
}
}
else if(ParseExpression(line,lineIndex,&value,&unresolved))
{
CheckWordRange(value,true,true);
fail=!GenerateWord(value,listingRecord,bigEndian);
}
else
{
ReportBadOperands();
done=true;
}
if(!done&&!fail) // look for separator, or comment
{
if(ParseComment(line,lineIndex))
{
done=true;
}
else if(ParseCommaSeparator(line,lineIndex))
{
}
else
{
ReportBadOperands();
done=true;
}
}
}
return(!fail);
}
bool HandleLEDW(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord)
// declaring words of data in little endian
{
return(HandleDW(opcodeName,line,lineIndex,lineLabel,listingRecord,false));
}
bool HandleBEDW(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord)
// declaring words of data in big endian
{
return(HandleDW(opcodeName,line,lineIndex,lineLabel,listingRecord,true));
}
bool HandleDS(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord)
// move the PC forward by the given amount
{
bool
fail;
int
value;
bool
unresolved;
fail=!ProcessLineLocationLabel(lineLabel); // deal with any label on the line
if(!fail)
{
if(ParseExpression(line,lineIndex,&value,&unresolved))
{
if(ParseComment(line,lineIndex))
{
if(currentSegment)
{
if(!unresolved)
{
if(!intermediatePass) // only do the real work if necessary
{
fail=!AddSpaceToSegment(currentSegment,currentSegment->currentPC,value);
}
currentSegment->currentPC+=value;
}
}
else
{
AssemblyComplaint(NULL,true,"'%s' outside of segment\n",opcodeName);
}
}
else
{
ReportBadOperands();
}
}
else
{
ReportBadOperands();
}
}
return(!fail);
}
bool HandleDSW(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord)
// move the PC forward by the given amount times two (do as if skipping this many words)
{
bool
fail;
int
value;
bool
unresolved;
fail=!ProcessLineLocationLabel(lineLabel); // deal with any label on the line
if(!fail)
{
if(ParseExpression(line,lineIndex,&value,&unresolved))
{
if(ParseComment(line,lineIndex))
{
value*=2;
if(currentSegment)
{
if(!unresolved)
{
if(!intermediatePass) // only do the real work if necessary
{
fail=!AddSpaceToSegment(currentSegment,currentSegment->currentPC,value);
}
currentSegment->currentPC+=value;
}
}
else
{
AssemblyComplaint(NULL,true,"'%s' outside of segment\n",opcodeName);
}
}
else
{
ReportBadOperands();
}
}
else
{
ReportBadOperands();
}
}
return(!fail);
}
bool HandleIncbin(const char *opcodeName,const char *line,unsigned int *lineIndex,const PARSED_LABEL *lineLabel,LISTING_RECORD *listingRecord)
// include the data of the given file into the input stream
// If there is a problem, report it and return false
// NOTE: on _large_ incbins, the assembly can be slowed by
// trying to read through all the bytes each pass of the assembly.
// So, on all passes but the last, this simply gets the length of the
// file and pushes the PC forward.
{
bool
fail,
done;
char
fileName[MAX_STRING];
unsigned int
stringLength;
FILE
*file;
unsigned char
inputDataBuffer[1024];
int
bytesRead;
fail=!ProcessLineLocationLabel(lineLabel); // deal with any label on the line
if(!fail)
{
if(ParseQuotedString(line,lineIndex,'"','"',fileName,&stringLength))
{
if(ParseComment(line,lineIndex)) // make sure there's nothing else on the line
{
if(currentSegment)
{
if((file=fopen(fileName,"rb")))
{
if(intermediatePass) // only do the real work if necessary
{
if(fseek(file,0,SEEK_END)==0)
{
currentSegment->currentPC+=ftell(file);
}
else
{
AssemblyComplaint(NULL,true,"Failed seeking '%s' to the end. %s\n",fileName,strerror(errno));
fail=true;
}
}
else
{
done=false;
while(!fail&&!done)
{
bytesRead=fread(inputDataBuffer,1,1024,file);
if(bytesRead>0)
{
fail=!AddBytesToSegment(currentSegment,currentSegment->currentPC,inputDataBuffer,bytesRead);
currentSegment->currentPC+=bytesRead;
}
else if(bytesRead==0)
{
done=true;
}
else
{
AssemblyComplaint(NULL,true,"Failed reading file '%s'. %s\n",fileName,strerror(errno));
fail=true;
}
}
}
fclose(file);
}
else
{
AssemblyComplaint(NULL,true,"Failed to open file '%s'. %s\n",fileName,strerror(errno));
}
}
else
{
AssemblyComplaint(NULL,true,"Code cannot occur outside of a segment\n");
}
}
else
{
AssemblyComplaint(NULL,true,"Missing or incorrectly formatted file name\n");
}
}
else
{
AssemblyComplaint(NULL,true,"Missing or incorrectly formatted file name\n");
}
}
return(!fail);
}