summaryrefslogtreecommitdiff
path: root/asmmon.h
blob: 633ed387ba27f922d93e4db15f701c4d0e8e5237 (plain)
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
#include "opcode.h"
#include <ctype.h>
#include <string.h>

#define MAX_TOK 0x1000

typedef struct tok  token      ;
typedef struct ln   line       ;
typedef struct sym  symbol     ;
typedef struct fix  fixup      ;
typedef struct inst instruction;


struct tok {
	token *next;	/* Pointer to the next token. */
	uint8_t id;	/* Token ID.      */
	uint8_t type;	/* Token type ID. */

	uint8_t tab;		/* Number of tabs.        */
	uint8_t space;		/* Number of spaces.      */

	uint8_t subtab;		/* Number of sub-token tabs.    */
	uint8_t subspace;	/* Number of sub-token spaces.  */

	uint8_t digits;		/* Number of digits. */

	/* Token value(s). */
	union {
		symbol *sym;
		char *str;
		uint8_t  byte ;
		uint16_t word ;
		uint32_t dword;
		uint64_t qword;
	};
};

struct ln {
	line  *next;		/* Pointer to the next line.   */
	token *tok;		/* The token(s) for this line. */
	uint16_t count;		/* Total tokens for this line. */
	uint16_t bline;		/* Number of blank lines.      */
	uint32_t linenum;	/* Line number.                */
	uint64_t addr;		/* The address of this line.   */
};



struct fix {
	fixup *next;
	symbol *s;
	token *t;
	uint64_t adr;
};

struct sym {
	symbol *next;
	symbol *local;
	uint16_t count;
	uint64_t val;
	uint8_t def;
	char *name;
	uint16_t id;
};

struct inst {
	uint16_t am;	/* Addressing modes. */
	uint8_t op;	/* Base value used to get the actual opcode. */
};


extern char lexeme[];
extern char *string[];
extern char *comment[];
extern uint16_t incl[];
extern line *lines;
extern line *last_line;
extern token *tokens;
extern token *last_tok;
extern symbol *symbols;
extern symbol *last_sym;
extern symbol *locals;
extern symbol *last_loc;
extern fixup *fixups;
extern fixup *last_fix;

extern uint8_t lex_type;

enum dir {
	DIR_ORG,
	DIR_BYTE,
	DIR_WORD,
	DIR_DWORD,
	DIR_QWORD,
	DIR_INCLUDE,
	DIR_RES
};

enum token {
	TOK_DIR,
	TOK_LOCAL,
	TOK_LABEL,
	TOK_SYM,
	TOK_EXPR,
	TOK_CSV,
	TOK_STRING,
	TOK_CHAR,
	TOK_IND,
	TOK_IMM,
	TOK_BREG,
	TOK_OPCODE,
	TOK_RS,
	TOK_OF,
	TOK_COMMENT,
	TOK_HEX,
	TOK_DEC,
	TOK_BIN,
	TOK_INCLUDE
};

enum pre_token {
	PTOK_DOT,
	PTOK_AT,
	PTOK_COLON,
	PTOK_EQU,
	PTOK_PLUS,
	PTOK_MINUS,
	PTOK_GT,
	PTOK_LT,
	PTOK_PIPE,
	PTOK_LBRACK,
	PTOK_RBRACK,
	PTOK_COMMA,
	PTOK_B,
	PTOK_X,
	PTOK_Y,
	PTOK_S,
	PTOK_P,
	PTOK_DQUOTE,
	PTOK_SQUOTE,
	PTOK_HASH,
	PTOK_SCOLON,
	PTOK_DOLLAR,
	PTOK_PERCENT,
	PTOK_NUMBER,
	PTOK_ALPHA,
	PTOK_OTHER
};

enum expr {
	EXPR_PLUS,
	EXPR_MINUS,
	EXPR_LOW,
	EXPR_HIGH,
	EXPR_OR,
	EXPR_LSHFT,
	EXPR_RSHFT,
	EXPR_NONE
};

enum addrmode {
	AM_IMM    = (1 <<  0),
	AM_ZM     = (1 <<  1),
	AM_ZMX    = (1 <<  2),
	AM_ZMY    = (1 <<  3),
	AM_IND    = (1 <<  4),
	AM_INDX   = (1 <<  5),
	AM_INDY   = (1 <<  6),
	AM_ABS    = (1 <<  7),
	AM_REL    = (1 <<  8),
	AM_BREG   = (1 <<  9),
	AM_IMPL   = (1 << 10),
	AM_INDX2  = (1 << 11),
	AM_ZM2    = (1 << 12)
};

enum ind {
	CMP_IND =  0,
	CMP_IDY =  1,
	CMP_IDX =  2,
	CPB_IND =  3,
	CPB_IDY =  4,
	CPB_IDX =  5,
	JMP_IND =  6,
	JSR_IND =  7,
	LDA_IND =  8,
	LDA_IDY =  9,
	LDB_IND = 10,
	LDB_IDY = 11,
	LDX_IND = 12,
	LDY_IND = 13,
	STA_IND = 14,
	STA_IDY = 15,
	STB_IND = 16,
	STB_IDY = 17,
	STX_IND = 18,
	STY_IND = 19
};

static const uint8_t ind_ops[20] = {
	[CMP_IND] = 0x8C,
	[CMP_IDY] = 0xF9,
	[CMP_IDX] = 0xAA,
	[CPB_IND] = 0x7C,
	[CPB_IDY] = 0xFA,
	[CPB_IDX] = 0xBA,
	[JMP_IND] = 0xEC,
	[JSR_IND] = 0xDC,
	[LDA_IND] = 0xC4,
	[LDA_IDY] = 0xD9,
	[LDB_IND] = 0xE4,
	[LDB_IDY] = 0xE9,
	[LDX_IND] = 0xBC,
	[LDY_IND] = 0xAC,
	[STA_IND] = 0xD4,
	[STA_IDY] = 0xCA,
	[STB_IND] = 0xF4,
	[STB_IDY] = 0xDA,
	[STX_IND] = 0xCC,
	[STY_IND] = 0x9C
};

static const instruction inst[OPNUM] = {
	[ADC] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x01},
	[AND] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x41},
	[ASR] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x62},
	[BCC] = {(AM_REL), 0xA0},
	[BCS] = {(AM_REL), 0x90},
	[BEQ] = {(AM_REL), 0xB0},
	[BNE] = {(AM_REL), 0xC0},
	[BNG] = {(AM_REL), 0x80},
	[BPO] = {(AM_REL), 0x70},
	[BRA] = {(AM_REL), 0xF0},
	[BRK] = {(AM_IMPL), 0x69},
	[BVC] = {(AM_REL), 0xE0},
	[BVS] = {(AM_REL), 0xD0},
	[CLC] = {(AM_IMPL), 0x09},
	[CLI] = {(AM_IMPL), 0x29},
	[CLV] = {(AM_IMPL), 0x49},
	[CMP] = {(AM_IMM|AM_ZM|AM_IND|AM_INDY|AM_ABS|AM_BREG|AM_INDX2), 0x82},
	[CPB] = {(AM_IMM|AM_ZM|AM_IND|AM_INDY|AM_ABS|AM_INDX2), 0x04},
	[CPS] = {(AM_IMPL), 0x00},
	[CPX] = {(AM_IMM|AM_ZM|AM_IND|AM_ABS), 0x24},
	[CPY] = {(AM_IMM|AM_ZM|AM_IND|AM_ABS), 0x44},
	[DEB] = {(AM_IMPL), 0x99},
	[DEC] = {(AM_IMPL|AM_ZM|AM_ABS), 0x84},
	[DEX] = {(AM_IMPL), 0xB9},
	[DEY] = {(AM_IMPL), 0x79},
	[DIV] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x42},
	[INB] = {(AM_IMPL), 0xA9},
	[INC] = {(AM_IMPL|AM_ZM|AM_ABS), 0xA4},
	[INX] = {(AM_IMPL), 0xC9},
	[INY] = {(AM_IMPL), 0x89},
	[JMP] = {(AM_ABS|AM_IND|AM_ZM2), 0x00},
	[JSR] = {(AM_ABS|AM_IND|AM_ZM2), 0x20},
	[LDA] = {(AM_IMM|AM_ZM|AM_ZMX|AM_ZMY|AM_IND|AM_INDX|AM_INDY|AM_ABS), 0xC2},
	[LDB] = {(AM_IMM|AM_ZM|AM_ZMX|AM_ZMY|AM_IND|AM_INDX|AM_INDY|AM_ABS), 0xE2},
	[LDX] = {(AM_IMM|AM_ZM|AM_IND|AM_ABS), 0x64},
	[LDY] = {(AM_IMM|AM_ZM|AM_IND|AM_ABS), 0xA2},
	[LSL] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0xA1},
	[LSR] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0xC1},
	[MUL] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x22},
	[NOP] = {(AM_IMPL), 0xEA},
	[ORA] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x61},
	[PHA] = {(AM_IMPL), 0x8E},
	[PHB] = {(AM_IMPL), 0xAE},
	[PHP] = {(AM_IMPL), 0x6E},
	[PHX] = {(AM_IMPL), 0xEE},
	[PHY] = {(AM_IMPL), 0xCE},
	[PLA] = {(AM_IMPL), 0x9E},
	[PLB] = {(AM_IMPL), 0xBE},
	[PLP] = {(AM_IMPL), 0x7E},
	[PLX] = {(AM_IMPL), 0xFE},
	[PLY] = {(AM_IMPL), 0xDE},
	[ROL] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0xE1},
	[ROR] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x02},
	[RTI] = {(AM_IMPL), 0x60},
	[RTS] = {(AM_IMPL), 0x50},
	[SBC] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x21},
	[SEC] = {(AM_IMPL), 0x19},
	[SEI] = {(AM_IMPL), 0x39},
	[STA] = {(AM_ZM|AM_ZMX|AM_ZMY|AM_IND|AM_INDX|AM_INDY|AM_ABS), 0x28},
	[STB] = {(AM_ZM|AM_ZMX|AM_ZMY|AM_IND|AM_INDX|AM_INDY|AM_ABS), 0x48},
	[STX] = {(AM_ZM|AM_IND|AM_ABS), 0x68},
	[STY] = {(AM_ZM|AM_IND|AM_ABS), 0x08},
	[TAB] = {(AM_IMPL), 0x0A},
	[TAX] = {(AM_IMPL), 0x4A},
	[TAY] = {(AM_IMPL), 0x2A},
	[TBA] = {(AM_IMPL), 0x1A},
	[TSX] = {(AM_IMPL), 0x8A},
	[TXA] = {(AM_IMPL), 0x5A},
	[TXS] = {(AM_IMPL|AM_IMM), 0x9A},
	[TXY] = {(AM_IMPL), 0x7A},
	[TYA] = {(AM_IMPL), 0x3A},
	[TYX] = {(AM_IMPL), 0x6A},
	[WAI] = {(AM_IMPL), 0x59},
	[XOR] = {(AM_IMM|AM_ZM|AM_ABS|AM_BREG), 0x81}
};

static const char *dir_t[7] = {
	[0] = "org",
	[1] = "byte",
	[2] = "word",
	[3] = "dword",
	[4] = "qword",
	[5] = "include",
	[6] = "res"
};

static const char *rs_t[4] = {
	[0] = "",
	[1] = ".w",
	[2] = ".d",
	[3] = ".q"
};

static const char *lex_tok[19] = {
	[TOK_DIR    ] = "TOK_DIR",
	[TOK_LOCAL  ] = "TOK_LOCAL",
	[TOK_LABEL  ] = "TOK_LABEL",
	[TOK_SYM    ] = "TOK_SYM",
	[TOK_EXPR   ] = "TOK_EXPR",
	[TOK_CSV    ] = "TOK_CSV",
	[TOK_STRING ] = "TOK_STRING",
	[TOK_CHAR   ] = "TOK_CHAR",
	[TOK_IND    ] = "TOK_IND",
	[TOK_IMM    ] = "TOK_IMM",
	[TOK_BREG   ] = "TOK_BREG",
	[TOK_OPCODE ] = "TOK_OPCODE",
	[TOK_RS     ] = "TOK_RS",
	[TOK_OF     ] = "TOK_OF",
	[TOK_COMMENT] = "TOK_COMMENT",
	[TOK_HEX    ] = "TOK_HEX",
	[TOK_DEC    ] = "TOK_DEC",
	[TOK_BIN    ] = "TOK_BIN",
	[TOK_INCLUDE] = "TOK_INCLUDE"
};

static const char *adrmode[11] = {
	[IMM ] = "IMM",
	[ZM  ] = "ZM",
	[ZMX ] = "ZMX",
	[ZMY ] = "ZMY",
	[IND ] = "IND",
	[INDX] = "INDX",
	[INDY] = "INDY",
	[ABS ] = "ABS",
	[REL ] = "REL",
	[BREG] = "BREG",
	[IMPL] = "IMPL"
};

static const char *mne[OPNUM] = {
	[ADC] = "ADC",
	[AND] = "AND",
	[ASR] = "ASR",
	[BCC] = "BCC",
	[BCS] = "BCS",
	[BEQ] = "BEQ",
	[BNE] = "BNE",
	[BNG] = "BNG",
	[BPO] = "BPO",
	[BRA] = "BRA",
	[BRK] = "BRK",
	[BVC] = "BVC",
	[BVS] = "BVS",
	[CLC] = "CLC",
	[CLI] = "CLI",
	[CLV] = "CLV",
	[CMP] = "CMP",
	[CPB] = "CPB",
	[CPS] = "CPS",
	[CPX] = "CPX",
	[CPY] = "CPY",
	[DEB] = "DEB",
	[DEC] = "DEC",
	[DEX] = "DEX",
	[DEY] = "DEY",
	[DIV] = "DIV",
	[INB] = "INB",
	[INC] = "INC",
	[INX] = "INX",
	[INY] = "INY",
	[JMP] = "JMP",
	[JSR] = "JSR",
	[LDA] = "LDA",
	[LDB] = "LDB",
	[LDX] = "LDX",
	[LDY] = "LDY",
	[LSL] = "LSL",
	[LSR] = "LSR",
	[MUL] = "MUL",
	[NOP] = "NOP",
	[ORA] = "ORA",
	[PHA] = "PHA",
	[PHB] = "PHB",
	[PHP] = "PHP",
	[PHX] = "PHX",
	[PHY] = "PHY",
	[PLA] = "PLA",
	[PLB] = "PLB",
	[PLP] = "PLP",
	[PLX] = "PLX",
	[PLY] = "PLY",
	[ROL] = "ROL",
	[ROR] = "ROR",
	[RTI] = "RTI",
	[RTS] = "RTS",
	[SBC] = "SBC",
	[SEC] = "SEC",
	[SEI] = "SEI",
	[STA] = "STA",
	[STB] = "STB",
	[STX] = "STX",
	[STY] = "STY",
	[TAB] = "TAB",
	[TAX] = "TAX",
	[TAY] = "TAY",
	[TBA] = "TBA",
	[TSX] = "TSX",
	[TXA] = "TXA",
	[TXS] = "TXS",
	[TXY] = "TXY",
	[TYA] = "TYA",
	[TYX] = "TYX",
	[WAI] = "WAI",
	[XOR] = "XOR"
};

static const char *instdesc[OPNUM] = {
	[ADC] = "ADd accumulator, with operand, Carry if needed.",
	[AND] = "Bitwise AND accumulator, with operand.",
	[ASR] = "Arithmetic Shift Right accumulator, with operand.",
	[BCC] = "Branch if the Carry flag has been Cleared.",
	[BCS] = "Branch if the Carry flag is Set.",
	[BEQ] = "Branch if EQual (the zero flag has been set).",
	[BNE] = "Branch if Not Equal (the zero flag has been cleared)",
	[BNG] = "Branch if NeGative.",
	[BPO] = "Branch if POsitive.",
	[BRA] = "BRanch Always.",
	[BRK] = "BReaKpoint",
	[BVC] = "Branch if the oVerflow flag has been Cleared.",
	[BVS] = "Branch if the oVerflow flag is Set.",
	[CLC] = "CLear the Carry flag.",
	[CLI] = "CLear the Interrupt flag.",
	[CLV] = "CLear the oVerflow flag.",
	[CMP] = "CoMPare acumulator, with operand.",
	[CPB] = "ComPare the B register, with operand.",
	[CPS] = "Clears the Processor Status register.",
	[CPX] = "ComPare the X register, with operand.",
	[CPY] = "ComPare the Y register, with operand.",
	[DEB] = "DEcrement the B register.",
	[DEC] = "DECrement accumulator, or memory.",
	[DEX] = "DEcrement the X register.",
	[DEY] = "DEcrement the Y register.",
	[DIV] = "DIVide accumulator, with operand, and put the remainder into the B register.",
	[INB] = "INcrement the B register.",
	[INC] = "INCrement accumulator, or memory.",
	[INX] = "INcrement the X register.",
	[INY] = "INcrement the Y register.",
	[JMP] = "JuMP to the address specified.",
	[JSR] = "Jump to a SubRoutine.",
	[LDA] = "LoaD the value from the operand, to the Accumulator.",
	[LDB] = "LoaD the value from the operand, to the B register.",
	[LDX] = "LoaD the value from the operand, to the X register.",
	[LDY] = "LoaD the value from the operand, to the Y register.",
	[LSL] = "Logical Shift Left accumulator, with operand.",
	[LSR] = "Logical Shift Right accumulator, with operand.",
	[MUL] = "MULtiply accumulator, with operand.",
	[NOP] = "NO oPeration",
	[ORA] = "Bitwise OR Accumulator, with operand.",
	[PHA] = "PusH the number of bytes specified, from the Accumulator to the stack.",
	[PHB] = "PusH the number of bytes specified, from the B register to the stack.",
	[PHP] = "PusH the number of bytes specified, from the Processor status register to the stack.",
	[PHX] = "PusH the number of bytes specified, from the X register to the stack.",
	[PHY] = "PusH the number of bytes specified, from the Y register to the stack.",
	[PLA] = "PuLl the number of bytes specified, from the stack, to the Accumulator.",
	[PLB] = "PuLl the number of bytes specified, from the stack, to the B register.",
	[PLP] = "PuLl the number of bytes specified, from the stack, to the Processor status register.",
	[PLX] = "PuLl the number of bytes specified, from the stack, to the X register.",
	[PLY] = "PuLl the number of bytes specified, from the stack, to the Y register.",
	[ROL] = "ROtate Left accumulator, with operand.",
	[ROR] = "ROtate Right accumulator, with operand.",
	[RTI] = "ReTurn from an Interrupt.",
	[RTS] = "ReTurn from a Subroutine.",
	[SBC] = "SuBtract accumulator, with operand, Carry if needed",
	[SEC] = "SEt the Carry flag.",
	[SEI] = "SEt the Interrupt flag.",
	[STA] = "STore the value from the Accumulator, in memory.",
	[STB] = "STore the value from the B register, in memory.",
	[STX] = "STore the value from the X register, in memory.",
	[STY] = "STore the value from the Y register, in memory.",
	[TAB] = "Transfer the value from the Accumulator, to the B register.",
	[TAX] = "Transfer the value from the Accumulator, to the X register.",
	[TAY] = "Transfer the value from the Accumulator, to the Y register.",
	[TBA] = "Transfer the value from the Y register, to the Accumulator.",
	[TSX] = "Transfer the value from the Stack pointer, to the X register.",
	[TXA] = "Transfer the value from the X register, to the Accumulator.",
	[TXS] = "Transfer the value from the X register, to the Stack pointer.",
	[TXY] = "Transfer the value from the X register, to the Y register.",
	[TYA] = "Transfer the value from the Y register, to the Accumulator.",
	[TYX] = "Transfer the value from the Y register, to the X register.",
	[WAI] = "WAIt for an interrupt",
	[XOR] = "Bitwise XOR Accumulator, with operand."
};

static const uint8_t bitsize[4] = {
	[0] = 0x07,
	[1] = 0x0F,
	[2] = 0x1F,
	[3] = 0x3F
};

static const uint8_t amp[8] = {
	[0] = 0x00,
	[1] = 0x00,
	[2] = 0x07,
	[4] = 0x07,
	[5] = 0x0B,
	[6] = 0x0B,
	[3] = 0x0F,
	[7] = 0x0F
};


extern uint16_t linenum;
extern uint16_t lineidx;
extern uint16_t stridx;
extern uint16_t comidx;
extern uint16_t inc_file; /* Number of included files. */
extern uint16_t inc_count;

struct bc {
	uint64_t progsize;
	uint64_t datasize;
};

typedef struct bc bytecount;

extern uint8_t defined;
extern uint8_t isfixup;

extern line *find_line(uint32_t ln, uint8_t dbg);
extern uint64_t lex(char *str, uint64_t address, uint16_t bline, uint8_t dbg);

extern uint64_t get_val(token *t, uint64_t addr, uint8_t size, uint8_t dbg);
extern token *skip_expr(token *t, uint8_t dbg);
extern uint64_t parse_tokens(token *tm, bytecount *bc, uint8_t isasm, uint64_t address, uint8_t dbg);
extern token *make_token(uint8_t id, uint8_t type, uint8_t space, uint8_t tab, uint64_t value, char *str, symbol *s);
extern void assemble(line *ln, bytecount *bc, uint8_t dbg);
extern void cleanup();