-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsmPass2.pas
547 lines (487 loc) · 17.1 KB
/
AsmPass2.pas
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
unit AsmPass2;
interface
uses AsmUtils,AsmTypes;
function SecondPassASMLine(s: string; sFile: string; iLine: integer;
var regPC: integer; var sOut: string;
Encoding: TCharEncoding): integer;
implementation
uses SysUtils, Assemb;
{ ParseAsmDirective
IN: sOp - OpCode, eg. "LD", "NOP", "JR", "BIT"
IN: sParam - Parameters, eg. "A,62", "", "print-a", "1,B"
IN: sFile - Name of file being processed (name only, not path)
IN: iLine - Line number being processed, within sFile.
IN/OUT: regPC - Current assembly address - modified as necessary after the
line has been parsed to point to the address of the next instr
OUT: sOut - Assembled Binary for the line
RETURNS: - 0 = no errors, 1 = Warning, 2 = Error, 3 = Internal failure
}
function ParseAsmDirective(sOp,sParam: string; sFile: string; iLine: integer;
var regPC: integer; var sOut: string;
Encoding: TCharEncoding): integer;
var
i: integer;
begin
// ORG
if (sOp = 'ORG') or (sOP = '.ORG') then
begin
if (sParam[1] = '$') or ( UpperCase(sParam[Length(sParam)]) = 'H') then
begin
if not HexToDecimal(sParam,i) then
begin
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Invalid origin. Unable to parse hex value.');
exit;
end;
end
else if sParam[1] = '%' then
begin
if not BinToDecimal(sParam,i) then
begin
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Invalid origin. Unable to parse binary value.');
exit;
end;
end
else
i := StrToIntDef(sParam,-1);
if regPC > 0 then
begin
if i < regPC then
begin
// Generate error: org value set lower than current assembly location
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Attempt to set origin lower than current assembly location.');
exit;
end;
end;
if (i < 0) or (i> 65535) then
begin
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Origin must be in the range 0 - 65535.');
exit;
end;
regPC := i;
Result := 0;
exit;
end;
// EQU
if (sOp = 'EQU') or (sOp = '.EQU') then
begin
if (sParam[1] = '$') or ( UpperCase(sParam[Length(sParam)]) = 'H') then
if not HexToDecimal(sParam,i) then
begin
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Invalid hex value.');
exit;
end
else if sParam[1] = '%' then
if not BinToDecimal(sParam,i) then
begin
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Invalid binary value.');
exit;
end
else
i := StrToIntDef(sParam,-32769);
if (i < -32768) or (i > 65535) then
begin
Result := 2;
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Value overflows word.');
end
else
begin
Result := ($08000000 Or i); // Special return code instructing the
// calling procedure to add
// label=(Result and $FFFF) rather than
// label=regPC
end;
exit;
end;
// DEFB/BYTE/DB
if (sOp = 'DEFB') or (sOp = '.DEFB') or (sOp = 'BYTE') or (sOp = '.BYTE') or
(sOp = 'DEFM') or (sOp = '.DEFM') then
begin
// sParam contains either a single byte or a comma separated list to add
// to sOut
Result := AsmDirDEFB(sParam,sFile,iLine,regPC,sOut,Encoding);
exit;
end;
// DEFW/WORD/DB
if (sOp = 'DEFW') or (sOp = '.DEFW') or (sOp = 'WORD') or (sOp = '.WORD') then
begin
// sParam contains either a single byte or a comma separated list to add
// to sOut
Result := AsmDirDEFW(sParam,sFile,iLine,regPC,sOut,Encoding);
exit;
end;
Result := 4; // 4 = Not a supported ASM directive
end;
function IsIndexedInstruction(sParam: string): boolean;
begin
if (Copy(sParam,1,3) = '(IX') or (Copy(sParam,1,3) = '(IY') or
(Copy(sParam,2,4) = ',(IX') or (Copy(sParam,2,4) = ',(IY') then
Result := true
else
Result := false;
end;
function ParseOpCode(sOp,sParam: string; sFile: string; iLine: integer;
var regPC: integer; var sOut: string;
Encoding: TCharEncoding): integer;
var
i: integer;
sParamU, sMid: string;
sEval: string;
iEval: integer;
begin
sParamU := UpperCase(sParam);
// Scan for static (no params) instructions
for i := 1 to Length(ASMInfo)-1 do
begin
if (sOp = ASMInfo[i].sOperator) and (sParamU = ASMInfo[i].sOperand) then
begin
// Static instruction matched
sOut := ASMInfo[i].sOut;
regPC := regPC + Length(sOut);
Result := 0;
exit;
end;
end;
// Scan for parametized instructions
for i := 1 to Length(ASMInfo)-1 do
begin
// Instruction contains a '?' parameter, which is a relative jump offset
if (sOp = ASMInfo[i].sOperator) and (Pos('?',ASMInfo[i].sOperand)>0) then
begin
if (Copy(sParamU,1,Pos('?',ASMInfo[i].sOperand)-1) =
Copy(ASMInfo[i].sOperand,1,Pos('?',ASMInfo[i].sOperand)-1)) and
(Copy(ASMInfo[i].sOperand,Pos('?',ASMInfo[i].sOperand)+1,512) =
Copy(sParamU,1+Length(sParamU)-Length(Copy(ASMInfo[i].sOperand,Pos('?',ASMInfo[i].sOperand)+1,512)),512)) then
begin
sOut := ASMInfo[i].sOut;
sEval := Copy(sParam,Pos('?',ASMInfo[i].sOperand),512);
sEval := Copy(sEval,
1,
Length(sEval)-(Length(ASMInfo[i].sOperand)-Pos('?',ASMInfo[i].sOperand)));
regPC := regPC + Length(sOut) + ASMInfo[i].iParamBytes;
if ASMInfo[i].iParamBytes <> 1 then
begin
AssemblerDisplay('[CPU Table Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Instruction for relative jump should only have 1 parameter byte.');
Result := 3;
exit;
end;
// Evaluate sEval and tag the appropriate value (1 byte offet)
// onto sOut
if IsIndexedInstruction(sParamU) then
begin
case Evaluate(true, sEval, 0, Encoding, iEval) of
0:
begin
if (iEval < -128) or (iEval > 127) then
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Index register offset too far.');
Result := 2;
exit;
end;
if iEval < 0 then Inc(iEval,256);
sOut := sOut + Chr(iEval and $FF);
if (Length(sOut) = 4) and ((Copy(sOut,1,2) = #$DD#$CB) or (Copy(sOut,1,2) = #$FD#$CB)) then
sOut := Copy(sOut,1,2) + sOut[4] + sOut[3];
Result := 0;
end;
1:
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Index register offset jump too far.');
Result := 2;
end;
2:
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Index reference to undefined label.');
Result := 2;
end;
else
Result := 3;
end;
end
else
begin
case Evaluate(true, sEval, regPC, Encoding, iEval) of
0:
begin
if (Copy(sParamU,1,3) <> '(IX') and (Copy(sParamU,1,3) <> '(IY') and
(Copy(sParamU,2,4) <> ',(IX') and (Copy(sParamU,2,4) <> ',(IY') and
((iEval - regPC) < -128) or ((iEval - regPC) > 127) then
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Relative jump too far.');
Result := 2;
exit;
end;
if (Copy(sParamU,1,3) <> '(IX') and (Copy(sParamU,1,3) <> '(IY') then
iEval := iEval - regPC;
if iEval < 0 then Inc(iEval,256);
sOut := sOut + Chr(iEval and $FF);
if (Length(sOut) = 4) and ((Copy(sOut,1,2) = #$DD#$CB) or (Copy(sOut,1,2) = #$FD#$CB)) then
sOut := Copy(sOut,1,2) + sOut[4] + sOut[3];
Result := 0;
end;
1:
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Relative jump too far.');
Result := 2;
end;
2:
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Relative jump to undefined label.');
Result := 2;
end;
else
Result := 3;
end;
end;
exit;
end;
if ( Copy(sParamU,1,Pos('?',ASMInfo[i].sOperand)-1) =
Copy(ASMInfo[i].sOperand,1,Pos('?',ASMInfo[i].sOperand)-1)) and
(ASMInfo[i].sOperand[Length(ASMInfo[i].sOperand)]='*') then
begin
// (IX?),*
sMid := Copy(ASMInfo[i].sOperand,Pos('?',ASMInfo[i].sOperand)+1,512);
sMid := Copy(sMid,1,Length(sMid)-1);
// ),
if Pos(sMid,sParamU) > Pos('?',ASMInfo[i].sOperand) then
begin
// (IX+$20),$32
sOut := ASMInfo[i].sOut;
Inc(regPC,Length(sOut) + ASMInfo[i].iParamBytes);
sEval := Copy(sParam,Pos('?',ASMInfo[i].sOperand),512);
// +$20),$32
sEval := Copy(sEval,1,Pos(sMid,sEval)-1);
// Evaluate sEval and tag the appropriate value (1 byte offet)
// onto sOut
case Evaluate(true, sEval, 0, Encoding, iEval) of
0:
begin
if ((iEval < -128) or (iEval > 127)) then
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Index offset too far.');
Result := 2;
exit;
end;
if iEval < 0 then Inc(iEval,256);
sOut := sOut + Chr(iEval and $FF);
end;
1:
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Index offset too far.');
Result := 2;
exit;
end;
2:
begin
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Relative jump to undefined label.');
Result := 2;
exit;
end;
else
begin
Result := 3;
exit;
end;
end;
sEval := Copy(sParam,Pos(sMid,sParamU)+Length(sMid),512);
case Evaluate(true, sEval, regPC, Encoding, iEval) of
0:
begin
sOut := sOut + Char(iEval and $FF);
Result := 0;
end;
else
Result := 3;
end;
exit;
end;
end;
end;
if (sOp = ASMInfo[i].sOperator) and (Pos('*',ASMInfo[i].sOperand)>0) then
begin
if (Copy(sParamU,1,Pos('*',ASMInfo[i].sOperand)-1) =
Copy(ASMInfo[i].sOperand,1,Pos('*',ASMInfo[i].sOperand)-1)) and
(Copy(ASMInfo[i].sOperand,Pos('*',ASMInfo[i].sOperand)+1,512) =
Copy(sParamU,1+Length(sParamU)-Length(Copy(ASMInfo[i].sOperand,Pos('*',ASMInfo[i].sOperand)+1,512)),512)) then
begin
sOut := ASMInfo[i].sOut;
sEval := Copy(sParam,Pos('*',ASMInfo[i].sOperand),512);
sEval := Copy(sEval,
1,
Length(sEval)-(Length(ASMInfo[i].sOperand)-Pos('*',ASMInfo[i].sOperand)));
// Evaluate sEval and tag the appropriate value (1 or 2 bytes)
// onto sOut
case Evaluate(true,sEval, regPC, Encoding, iEval) of
1:
begin
// Invalid parameter value
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Invalid parameter value.');
Result := 2;
exit;
end;
2:
begin
// Label not defined
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Label not defined.');
Result := 2;
exit;
end;
3:
begin
// Error evaluating parameters
AssemblerDisplay('[Error] ' + sFile + ', line ' + IntToStr(iLine) +
': Unable to evaluate parameter(s).');
Result := 2;
exit;
end;
end;
case ASMInfo[i].iParamBytes of
1:
sOut := sOut + Char(iEval and $FF);
2:
sOut := sOut + Char(iEval and $FF) + Char(iEval div 256);
end;
regPC := regPC + Length(sOut);
Result := 0;
exit;
end;
end;
end;
Result := 4;
end;
{ SecondPassASMLine
Second pass of processing a line of assembly. Procudes any binary output for
the line in sOut.
IN: s - Line of assembler to process
IN: sFile - Name of file being processed
IN: iLine - Number of line being processed.
OUT: regPC - Full name of output file to create.
OUT: sOut - Binary output for the instruction
RETURNS: - 0 = no errors, 1 = Warning, 2 = Error, 3 = Internal failure
}
{$WARNINGS OFF} // Stop D2007 ERRONEOUSLY emitting 'bInString might not be initialized' warning below
function SecondPassASMLine(s: string; sFile: string; iLine: integer;
var regPC: integer; var sOut: string;
Encoding: TCharEncoding): integer;
var
sOperator: string;
i: integer;
bInString, bInChar: boolean;
begin
// First, strip off any comment text
if Pos(';',Trim(s)) = 1 then
begin
// This is only a comment line, okay it
Result := 0;
exit;
end
else if (Pos(';',s)>1) then
// Cope with about semi-colons in strings and as character literals
// ("Hello;World" or ';')
bInString := false;
bInChar := false;
for i := 1 to Length(s) do
begin
if s[i] = '"' then
bInString := not bInString;
if s[i] = '''' then
bInChar := not bInChar;
if (s[i] = '''') and (i >= 10) and (UpperCase(Copy(s,i-5,6)) = 'AF,AF''') and bInChar then
bInChar := not bInChar; // Special case to deal with the quote char in ex af,af' which is NOT a char literal identifier
if (s[i] = ';') and (not bInString) and (not bInChar) then
begin
s:=Copy(s,1,i-1);
break;
end;
end;
if (Length(s)>0) and ((s[1]=' ') or (s[1]=#9)) then
begin
// No Label
s := Trim(s);
end
else
begin
// Label found
if (Pos(' ',s)>1) then
begin
// Label + Possibly an instruction
s := Trim(Copy(s,Pos(' ',s)+1,512));
end
else if (Pos(#9,s)>1) then
begin
s := Trim(Copy(s,Pos(#9,s)+1,512));
end
else
s := '';
end;
{ Right, we now have:-
s = Full instruction or directive if one is present on the line,
including any parameters.
}
if (Pos(' ',s)>0) then
begin
sOperator := UpperCase(Copy(s,1,Pos(' ',s)-1));
s := Trim(Copy(s,Pos(' ',s)+1,512));
end
else if Pos(#9,s)>0 then
begin
sOperator := UpperCase(Copy(s,1,Pos(#9,s)-1));
s := Trim(Copy(s,Pos(#9,s)+1,512));
end
else
begin
sOperator := UpperCase(s);
s := '';
end;
// If label exists, return an error
// Check Operator against assembler directives
i := ParseAsmDirective(sOperator,s,sFile,iLine,regPC,sOut,Encoding);
if (i And $08000000) > 0 then i := 0;
if (i < 4) then
begin
// Line contained an ASM directive which we've processed or an error
Result := i;
exit;
end;
// if there's no opcode then this is a label-only line, exit ok
if (sOperator = '') then
begin
Result := 0;
exit;
end;
// Now the label, if present, has been recorded, and we know the line doesn't
// contain an ASM directive, so it either contains an opcode or an error!
i := ParseOpCode(sOperator,s,sFile,iLine,regPC,sOut,Encoding);
if (i < 4) then
begin
Result := i;
exit;
end;
Result := 2; // ERROR not an asm directive or an opcode or a comment or a blank line!
end;
{$WARNINGS ON} // Turn warnings back on
end.