-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathactions_6502.c
486 lines (448 loc) · 13.4 KB
/
actions_6502.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
/*
* Copyright (c) 1987 Fujitsu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
actions.c -- Actions associated with various machine instruction
classes (6502 version).
Chip Morningstar -- Lucasfilm Ltd.
16-November-1984
*/
#include "macrossTypes.h"
#include "macrossGlobals.h"
#include "actions.h"
#include "emitStuff.h"
#include "errorStuff.h"
#include "semanticMisc.h"
#define operand (evaluatedOperands[0])
#define address (evaluatedOperands[0])->value
#define class (evaluatedOperands[0])->addressMode
#define binary opcode->opcode
/*
These routines are vectored off of the opcode lookup table. Each
instruction is of a particular category that defines which address modes
it accepts its operands in and what size (one byte or two) the operands
are. There is one "actionsXXXX" routine for each of these categories that
grabs the operand, checks the address mode, and emits the binary opcode
and operand.
*/
void
actionsDir1(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define ZERO_PAGE_ADDRESS_BIT 0x00
#define NON_ZERO_PAGE_ADDRESS_BIT 0x08
if(class==EXPRESSION_OPND && isByteAddress(operand) &&
isDefined(operand)){
emitByte(binary | ZERO_PAGE_ADDRESS_BIT);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | NON_ZERO_PAGE_ADDRESS_BIT);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
void
actionsDir2(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
if (wordCheck(address)) {
emitByte(binary);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
void
actionsDirIndir(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define DIRECT_ADDRESS_BIT 0x00
#define INDIRECT_ADDRESS_BIT 0x20
if (wordCheck(address)) {
if (class == INDIRECT_OPND)
emitByte(binary | INDIRECT_ADDRESS_BIT);
else
emitByte(binary | DIRECT_ADDRESS_BIT);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
void
actionsDirX1(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_X1 0x04
#define A_REGISTER_BITS_X1 0x08
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_X1 0x0C
#define X_INDEXED_ZERO_PAGE_BITS_X1 0x14
#define X_INDEXED_NON_ZERO_PAGE_BITS_X1 0x1C
if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_X1);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_X1);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class==X_INDEXED_OPND || class==X_SELECTED_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | X_INDEXED_ZERO_PAGE_BITS_X1);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | X_INDEXED_NON_ZERO_PAGE_BITS_X1);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else {
emitByte(binary | A_REGISTER_BITS_X1);
}
}
void
actionsDirX2(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_X2 0x00
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_X2 0x08
#define X_INDEXED_ZERO_PAGE_BITS_X2 0x10
#define X_INDEXED_NON_ZERO_PAGE_BITS_X2 0x18
if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_X2);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_X2);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | X_INDEXED_ZERO_PAGE_BITS_X2);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | X_INDEXED_NON_ZERO_PAGE_BITS_X2);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
}
void
actionsDirX3(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_X2);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_X2);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else {
if (byteCheck(address)) {
emitByte(binary | X_INDEXED_ZERO_PAGE_BITS_X2);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
}
}
void
actionsDirY(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_Y 0x00
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_Y 0x08
#define Y_INDEXED_ZERO_PAGE_BITS_Y 0x10
if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_Y);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_Y);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else {
if (byteCheck(address)) {
emitByte(binary | Y_INDEXED_ZERO_PAGE_BITS_Y);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
}
}
void
actionsImmDir(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define IMMEDIATE_DATA_BITS_ID 0x00
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_ID 0x04
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_ID 0x0C
if (class == IMMEDIATE_OPND) {
if (byteCheck(address)) {
emitByte(binary | IMMEDIATE_DATA_BITS_ID);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
} else {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_ID);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_ID);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
}
void
actionsImmDirX(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define IMMEDIATE_DATA_BITS_IX 0x00
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_IX 0x04
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_IX 0x0C
#define X_INDEXED_ZERO_PAGE_BITS_IX 0x14
#define X_INDEXED_NON_ZERO_PAGE_BITS_IX 0x1C
if (class == IMMEDIATE_OPND) {
if (byteCheck(address)) {
emitByte(binary | IMMEDIATE_DATA_BITS_IX);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
} else if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_IX);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_IX);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | X_INDEXED_ZERO_PAGE_BITS_IX);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | X_INDEXED_NON_ZERO_PAGE_BITS_IX);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
}
void
actionsImmDirY(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define IMMEDIATE_DATA_BITS_IY 0x00
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_IY 0x04
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_IY 0x0C
#define Y_INDEXED_ZERO_PAGE_BITS_IY 0x14
#define Y_INDEXED_NON_ZERO_PAGE_BITS_IY 0x1C
if (class == IMMEDIATE_OPND) {
if (byteCheck(address)) {
emitByte(binary | IMMEDIATE_DATA_BITS_IY);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
} else if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_IY);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary|DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_IY);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | Y_INDEXED_ZERO_PAGE_BITS_IY);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | Y_INDEXED_NON_ZERO_PAGE_BITS_IY);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
}
}
void
actionsImmIndex(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
#define PRE_INDEXED_BITS_A 0x00
#define DIRECT_ADDRESS_ZERO_PAGE_BITS_A 0x04
#define IMMEDIATE_DATA_BITS_A 0x08
#define DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_A 0x0C
#define POST_INDEXED_BITS_A 0x10
#define X_INDEXED_ZERO_PAGE_BITS_A 0x14
#define Y_INDEXED_NON_ZERO_PAGE_BITS_A 0x18
#define X_INDEXED_NON_ZERO_PAGE_BITS_A 0x1C
if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_A);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary |DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_A);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class==X_INDEXED_OPND || class==X_SELECTED_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | X_INDEXED_ZERO_PAGE_BITS_A);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | X_INDEXED_NON_ZERO_PAGE_BITS_A);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class==Y_INDEXED_OPND || class==Y_SELECTED_OPND) {
if (wordCheck(address)) {
emitByte(binary | Y_INDEXED_NON_ZERO_PAGE_BITS_A);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class == IMMEDIATE_OPND) {
if (byteCheck(address)) {
emitByte(binary | IMMEDIATE_DATA_BITS_A);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
} else if (class == POST_INDEXED_Y_OPND) {
if (byteCheck(address)) {
emitByte(binary | POST_INDEXED_BITS_A);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
} else {
if (byteCheck(address)) {
emitByte(binary | PRE_INDEXED_BITS_A);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
}
}
void
actionsIndex(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
if (class == EXPRESSION_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | DIRECT_ADDRESS_ZERO_PAGE_BITS_A);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary |DIRECT_ADDRESS_NON_ZERO_PAGE_BITS_A);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class==X_INDEXED_OPND || class==X_SELECTED_OPND) {
if (isByteAddress(operand) && isDefined(operand)) {
emitByte(binary | X_INDEXED_ZERO_PAGE_BITS_A);
emitByte(address);
} else if (wordCheck(address)) {
emitByte(binary | X_INDEXED_NON_ZERO_PAGE_BITS_A);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class==Y_INDEXED_OPND || class==Y_SELECTED_OPND) {
if (wordCheck(address)) {
emitByte(binary | Y_INDEXED_NON_ZERO_PAGE_BITS_A);
putFixupsHere(WORD_FIXUP, 0);
emitWord(address);
}
} else if (class == POST_INDEXED_Y_OPND) {
if (byteCheck(address)) {
emitByte(binary | POST_INDEXED_BITS_A);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
} else {
if (byteCheck(address)) {
emitByte(binary | PRE_INDEXED_BITS_A);
putFixupsHere(BYTE_FIXUP, 0);
emitByte(address);
}
}
}
void
actionsNone(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
emitByte(binary);
}
void
actionsRelative(opcodeTableEntryType *opcode, int numberOfOperands, valueType **evaluatedOperands)
{
int offset;
if (operand->kindOfValue == UNDEFINED_VALUE || (currentCodeMode ==
RELOCATABLE_BUFFER && targetOffset == 0))
offset = 0;
else
offset = address - (currentLocationCounter.value - targetOffset) - 1;
if (offset < 0)
offset--;
if (isByteOffset(offset)) {
emitByte(binary);
putFixupsHere(BYTE_RELATIVE_FIXUP, 0);
emitByte(offset);
} else {
error(RELATIVE_OFFSET_TOO_LARGE_ERROR);
}
}
/*
Miscellaneous helper predicates.
*/
bool
isByte(int value)
{
return (-129<value && value<256);
}
bool
isByteOffset(int value)
{
return (-129<value && value<128);
}
bool
isWordOffset(int value)
{
return (-32769<value && value<32768);
}
bool
isByteAddress(valueType *value)
{
return(value->kindOfValue==ABSOLUTE_VALUE && isByte(value->value));
}
bool
isWord(int value)
{
return (-32769<value && value<65536);
}
bool
byteCheck(int value)
{
if (isByte(value)) {
return(TRUE);
} else {
error(BYTE_VALUE_TOO_LARGE_ERROR, value);
return(FALSE);
}
}
bool
wordCheck(int value)
{
if (isWord(value)) {
return(TRUE);
} else {
printf("word: %d\n", value);
error(WORD_VALUE_TOO_LARGE_ERROR, value);
return(FALSE);
}
}
bool
isDefined(valueType *value)
{
return(value!=NULL && value->kindOfValue!=UNDEFINED_VALUE);
}