-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
as09.tab.c
5083 lines (4128 loc) · 138 KB
/
as09.tab.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* A Bison parser, made by GNU Bison 2.3. */
/* Skeleton implementation for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* C LALR(1) parser skeleton written by Richard Stallman, by
simplifying the original so-called "semantic" parser. */
/* All symbols defined below should begin with yy or YY, to avoid
infringing on user name space. This should be done even for local
variables, as they might otherwise be expanded by user macros.
There are some unavoidable exceptions within include files to
define necessary library symbols; they are noted "INFRINGES ON
USER NAME SPACE" below. */
/* Identify Bison output. */
#define YYBISON 1
/* Bison version. */
#define YYBISON_VERSION "2.3"
/* Skeleton name. */
#define YYSKELETON_NAME "yacc.c"
/* Pure parsers. */
#define YYPURE 0
/* Using locations. */
#define YYLSP_NEEDED 0
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
EQU = 258,
INCLUDE = 259,
SET = 260,
ID = 261,
STRING = 262,
CHAR = 263,
ABX = 264,
ASLA = 265,
ASLB = 266,
ASRA = 267,
ASRB = 268,
CLRA = 269,
CLRB = 270,
COMA = 271,
COMB = 272,
CWAI = 273,
DAA = 274,
DECA = 275,
DECB = 276,
INCA = 277,
INCB = 278,
LSLA = 279,
LSLB = 280,
LSRA = 281,
LSRB = 282,
MUL = 283,
NEGA = 284,
NEGB = 285,
NOP = 286,
ROLA = 287,
ROLB = 288,
RORA = 289,
RORB = 290,
RTI = 291,
RTS = 292,
SEX = 293,
SWI = 294,
SWI2 = 295,
SWI3 = 296,
SYNC = 297,
TSTA = 298,
TSTB = 299,
TST = 300,
ADCA = 301,
ADCB = 302,
ADDA = 303,
ADDB = 304,
ADDD = 305,
ANDA = 306,
ANDB = 307,
ANDCC = 308,
ASL = 309,
ASR = 310,
BCC = 311,
BCS = 312,
BEQ = 313,
BGE = 314,
BGT = 315,
BHI = 316,
BHS = 317,
BITA = 318,
BITB = 319,
BLE = 320,
BLO = 321,
BLS = 322,
BLT = 323,
BMI = 324,
BNE = 325,
BPL = 326,
BRA = 327,
BRN = 328,
BSR = 329,
BVC = 330,
BVS = 331,
CLR = 332,
CMPA = 333,
CMPB = 334,
CMPD = 335,
CMPS = 336,
CMPU = 337,
CMPX = 338,
CMPY = 339,
COM = 340,
DEC = 341,
EORA = 342,
EORB = 343,
EXG = 344,
INC = 345,
JMP = 346,
JSR = 347,
TFR = 348,
LBCC = 349,
LBCS = 350,
LBEQ = 351,
LBGE = 352,
LBGT = 353,
LBHI = 354,
LBHS = 355,
LBLE = 356,
LBLO = 357,
LBLS = 358,
LBLT = 359,
LBMI = 360,
LBNE = 361,
LBPL = 362,
LBRA = 363,
LBRN = 364,
LBSR = 365,
LBVC = 366,
LBVS = 367,
LDA = 368,
LDB = 369,
LDD = 370,
LDS = 371,
LDU = 372,
LDX = 373,
LDY = 374,
LEAX = 375,
LEAY = 376,
LEAS = 377,
LEAU = 378,
LSL = 379,
LSR = 380,
NEG = 381,
ORA = 382,
ORB = 383,
ORCC = 384,
PSHS = 385,
PSHU = 386,
PULS = 387,
PULU = 388,
SBCA = 389,
SBCB = 390,
ROL = 391,
ROR = 392,
STA = 393,
STB = 394,
STD = 395,
STX = 396,
STY = 397,
STS = 398,
STU = 399,
SUBA = 400,
SUBB = 401,
SUBD = 402,
NUMBER = 403,
A = 404,
B = 405,
D = 406,
X = 407,
Y = 408,
U = 409,
S = 410,
PC = 411,
CC = 412,
DP = 413,
PCR = 414,
SETDP = 415,
ORG = 416,
FCB = 417,
FDB = 418,
FCC = 419,
RMB = 420,
END = 421,
FCZ = 422,
SETC = 423,
CLRC = 424,
SETZ = 425,
CLRZ = 426,
CLRD = 427,
ASLD = 428,
ASRD = 429
};
#endif
/* Tokens. */
#define EQU 258
#define INCLUDE 259
#define SET 260
#define ID 261
#define STRING 262
#define CHAR 263
#define ABX 264
#define ASLA 265
#define ASLB 266
#define ASRA 267
#define ASRB 268
#define CLRA 269
#define CLRB 270
#define COMA 271
#define COMB 272
#define CWAI 273
#define DAA 274
#define DECA 275
#define DECB 276
#define INCA 277
#define INCB 278
#define LSLA 279
#define LSLB 280
#define LSRA 281
#define LSRB 282
#define MUL 283
#define NEGA 284
#define NEGB 285
#define NOP 286
#define ROLA 287
#define ROLB 288
#define RORA 289
#define RORB 290
#define RTI 291
#define RTS 292
#define SEX 293
#define SWI 294
#define SWI2 295
#define SWI3 296
#define SYNC 297
#define TSTA 298
#define TSTB 299
#define TST 300
#define ADCA 301
#define ADCB 302
#define ADDA 303
#define ADDB 304
#define ADDD 305
#define ANDA 306
#define ANDB 307
#define ANDCC 308
#define ASL 309
#define ASR 310
#define BCC 311
#define BCS 312
#define BEQ 313
#define BGE 314
#define BGT 315
#define BHI 316
#define BHS 317
#define BITA 318
#define BITB 319
#define BLE 320
#define BLO 321
#define BLS 322
#define BLT 323
#define BMI 324
#define BNE 325
#define BPL 326
#define BRA 327
#define BRN 328
#define BSR 329
#define BVC 330
#define BVS 331
#define CLR 332
#define CMPA 333
#define CMPB 334
#define CMPD 335
#define CMPS 336
#define CMPU 337
#define CMPX 338
#define CMPY 339
#define COM 340
#define DEC 341
#define EORA 342
#define EORB 343
#define EXG 344
#define INC 345
#define JMP 346
#define JSR 347
#define TFR 348
#define LBCC 349
#define LBCS 350
#define LBEQ 351
#define LBGE 352
#define LBGT 353
#define LBHI 354
#define LBHS 355
#define LBLE 356
#define LBLO 357
#define LBLS 358
#define LBLT 359
#define LBMI 360
#define LBNE 361
#define LBPL 362
#define LBRA 363
#define LBRN 364
#define LBSR 365
#define LBVC 366
#define LBVS 367
#define LDA 368
#define LDB 369
#define LDD 370
#define LDS 371
#define LDU 372
#define LDX 373
#define LDY 374
#define LEAX 375
#define LEAY 376
#define LEAS 377
#define LEAU 378
#define LSL 379
#define LSR 380
#define NEG 381
#define ORA 382
#define ORB 383
#define ORCC 384
#define PSHS 385
#define PSHU 386
#define PULS 387
#define PULU 388
#define SBCA 389
#define SBCB 390
#define ROL 391
#define ROR 392
#define STA 393
#define STB 394
#define STD 395
#define STX 396
#define STY 397
#define STS 398
#define STU 399
#define SUBA 400
#define SUBB 401
#define SUBD 402
#define NUMBER 403
#define A 404
#define B 405
#define D 406
#define X 407
#define Y 408
#define U 409
#define S 410
#define PC 411
#define CC 412
#define DP 413
#define PCR 414
#define SETDP 415
#define ORG 416
#define FCB 417
#define FDB 418
#define FCC 419
#define RMB 420
#define END 421
#define FCZ 422
#define SETC 423
#define CLRC 424
#define SETZ 425
#define CLRZ 426
#define CLRD 427
#define ASLD 428
#define ASRD 429
/* Copy the first part of user declarations. */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "as09.h"
#include "decb.h"
// command line switches
const char * g_szOutputFilename = "a.out";
int g_bDebug = FALSE;
int g_bSyncROM = TRUE;
int g_bROM = FALSE;
int g_bSystemV = FALSE;
int g_bBinaryRom = FALSE;
int g_bCompactFile = FALSE;
int g_bHexFile = FALSE;
int g_bSymbols = FALSE;
int g_bUnreferenced = FALSE;
// parser tracking
int lineno = 1;
int err_count = 0;
int warn_count = 0;
// assembler input and output files
FILE *yyin = NULL;
FILE *fout = NULL;
// code segment
uint16_t addr = 0;
uint8_t code[MAX_CODE];
uint16_t direct_page_addr = 0;
uint16_t origin_addr = 0;
uint16_t start_addr = 0;
// symbol table
Symbol_t symbols[MAX_SYMBOLS];
int symbol_count = 0;
// fixups
Fixup_t fixups[MAX_FIXUPS]; // list of address fixups
int fixup_count = 0; // count of address fixups
int fixup_pending_index = FP_NONE; // index of currently pending fixup
// instruction buffer
uint8_t inst_buf[INB_SIZE];
int inst_ptr = 0;
// file descriptor stack
#define MAX_FILE_STACK 50
int file_stack_ptr = 0;
FileNode file_nodes[MAX_FILE_STACK];
#define LAST_SYMBOL symbols[symbol_count - 1]
#define CURRENT_FILE file_nodes[file_stack_ptr].filename
#define CURRENT_LINENO file_nodes[file_stack_ptr].yylineno
const char *fixup_names[] =
{
"FIXUP_NOCHANGE",
"FIXUP_IMM8",
"FIXUP_IMM16",
"FIXUP_REL8",
"FIXUP_REL16",
};
Opcodes opcodes[] =
{
// Immediate, Direct, Indexed, Extended
{0x89, 0x99, 0xA9, 0xB9}, // ADCA
{0xC9, 0xD9, 0xE9, 0xF9}, // ADCB
{0x8B, 0x9B, 0xAB, 0xBB}, // ADDA
{0xCB, 0xDB, 0xEB, 0xFB}, // ADDB
{0xC3, 0xD3, 0xE3, 0xF3}, // ADDD
{0x84, 0x94, 0xA4, 0xB4}, // ANDA
{0xC4, 0xD4, 0xE4, 0xF4}, // ANDB
{0x00, 0x08, 0x68, 0x78}, // ASL
{0x00, 0x07, 0x67, 0x77}, // ASR
{0x85, 0x95, 0xA5, 0xB5}, // BITA
{0xC5, 0xD5, 0xE5, 0xF5}, // BITB
{0x00, 0x0F, 0x6F, 0x7F}, // CLR
{0x81, 0x91, 0xA1, 0xB1}, // CMPA
{0xC1, 0xD1, 0xE1, 0xF1}, // CMPB
{0x83, 0x93, 0xA3, 0xB3}, // CMPD
{0x8C, 0x9C, 0xAC, 0xBC}, // CMPS
{0x00, 0x03, 0x63, 0x73}, // COM
{0x00, 0x0A, 0x6A, 0x7A}, // DEC
{0x88, 0x98, 0xA8, 0xB8}, // EORA
{0xC8, 0xD8, 0xE8, 0xF8}, // EORB
{0x00, 0x0C, 0x6C, 0x7C}, // INC
{0x00, 0x0E, 0x6E, 0x7E}, // JMP
{0x00, 0x9D, 0xAD, 0xBD}, // JSR
{0x86, 0x96, 0xA6, 0xB6}, // LDA
{0xC6, 0xD6, 0xE6, 0xF6}, // LDB
{0xCC, 0xDC, 0xEc, 0xFC}, // LDD
{0xCE, 0xDE, 0xEE, 0xFE}, // LDU
{0x8E, 0x9E, 0xAE, 0xBE}, // LDX
{0x00, 0x00, 0x30, 0x00}, // LEAX
{0x00, 0x00, 0x31, 0x00}, // LEAY
{0x00, 0x00, 0x32, 0x00}, // LEAS
{0x00, 0x00, 0x33, 0x00}, // LEAU
{0x00, 0x08, 0x68, 0x78}, // LSL
{0x00, 0x04, 0x64, 0x74}, // LSR
{0x00, 0x00, 0x60, 0x70}, // NEG
{0x8A, 0x9A, 0xAA, 0xBA}, // ORA
{0xCA, 0xDA, 0xEA, 0xFA}, // ORB
{0x00, 0x09, 0x69, 0x79}, // ROL
{0x00, 0x06, 0x66, 0x76}, // ROR
{0x82, 0x92, 0xA2, 0xB2}, // SBCA
{0xC2, 0xD2, 0xE2, 0xF2}, // SBCB
{0x00, 0x97, 0xA7, 0xB7}, // STA
{0x00, 0xD7, 0xE7, 0xF7}, // STB
{0x00, 0xDD, 0xED, 0xFD}, // STD
{0x00, 0xDF, 0xEF, 0xFF}, // STS
{0x00, 0xDF, 0xEF, 0xFF}, // STU
{0x00, 0x9F, 0xAF, 0xBF}, // STX
{0x00, 0x9F, 0xAF, 0xBF}, // STY
{0x80, 0x90, 0xA0, 0xB0}, // SUBA
{0xC0, 0xD0, 0xE0, 0xF0}, // SUBB
{0x83, 0x93, 0xA3, 0xB3}, // SUBD
{0x00, 0x0D, 0x6D, 0x7D} // TST
};
//-----------------------------------
// Verilog vs. SystemVerilog strings
//-----------------------------------
char *input_wire = "input wire";
char *output_reg = "output reg";
char *always_ff = "always @(posedge clk)";
char *always_comb = "always @*";
char *reg = "reg";
//------------------------
// error routine
//------------------------
void yyerror(char *s)
{
fprintf(stderr, "ERROR: '%s' in file '%s' near line %d\n", s, CURRENT_FILE, CURRENT_LINENO);
err_count++;
}
//------------------------
// error routine
//------------------------
void yywarning(char *s)
{
fprintf(stderr, "WARNING: '%s' in file '%s' near line %d\n", s, CURRENT_FILE, CURRENT_LINENO);
warn_count++;
}
//-----------------------------------
// logging function
//-----------------------------------
void LOG(const char *fmt, ...)
{
if (!g_bDebug)
{
return;
}
char buf[BUF_SIZE];
va_list valist;
va_start(valist, fmt);
vsprintf(buf, fmt, valist);
va_end(valist);
fputs(buf, stdout);
}
//-----------------------------------
// push new input file onto the stack
//-----------------------------------
void push_file_stack(const char *filename)
{
if (file_stack_ptr + 1 > MAX_FILE_STACK)
yyerror("too many includes!");
FILE *fptr = fopen(filename, "rt");
if (!fptr)
yyerror("include file not found");
file_stack_ptr++;
file_nodes[file_stack_ptr].fptr = fptr;
file_nodes[file_stack_ptr].filename = strdup(filename);
file_nodes[file_stack_ptr].yylineno = 1;
file_nodes[file_stack_ptr].column = 1;
yyin = fptr;
LOG("Push '%s' onto parser input stack (depth = %d)\n", filename, file_stack_ptr);
}
//-----------------------------------
// pop input file off the stack
//-----------------------------------
void pop_file_stack()
{
if (file_stack_ptr <= 0)
yyerror("file stack underflow!");
LOG("Popping '%s' from parser input stack (depth = %d)\n", file_nodes[file_stack_ptr].filename, file_stack_ptr-1);
free(file_nodes[file_stack_ptr].filename);
fclose(file_nodes[file_stack_ptr].fptr);
file_stack_ptr--;
yyin = file_nodes[file_stack_ptr].fptr;
}
//-----------------------------------
// next next char from input stream
//-----------------------------------
int getch()
{
return fgetc(yyin);
}
//-----------------------------------
// put char back to input stream
//-----------------------------------
int ungetch(int c)
{
return ungetc(c, yyin);
}
//----------------------------------------------
// helper function to accumulate generated code
//----------------------------------------------
void emit(uint8_t v)
{
code[addr] = v;
addr++;
}
//----------------------------------------------
// helper function to accumulate word values
//----------------------------------------------
void emit_word(uint16_t v)
{
emit(HIBYTE(v));
emit(LOBYTE(v));
}
//----------------------------------------------
// helper function to queue generated code
//----------------------------------------------
void emit_buf(uint8_t v)
{
inst_buf[inst_ptr++] = v;
assert(inst_ptr < INB_SIZE);
}
//----------------------------------------------
// helper function to queue a code word
//----------------------------------------------
void emit_buf_word(uint16_t v)
{
emit_buf(HIBYTE(v));
emit_buf(LOBYTE(v));
}
//----------------------------------------------
// helper function to emit a string
//----------------------------------------------
void emit_str(const char *s)
{
for (int i = 0; i < strlen(s); i++)
emit(s[i]);
}
//----------------------------------------------
// helper function to emit queued code
//----------------------------------------------
void write_inb()
{
// emit queued instruction codes
for (int i = 0; i < inst_ptr; i++)
emit(inst_buf[i]);
// mark instruction queue as empty
inst_ptr = 0;
}
//------------------------
// convert int to int5
//------------------------
int8_t to_int5(int val)
{
int8_t i5 = val & 0xF;
if (val < 0)
i5 |= 0x10;
return i5;
}
//------------------------
// convert int to int8
//------------------------
int8_t to_int8(int val)
{
int8_t i8 = val & 0x7F;
if (val < 0)
i8 |= 0x80;
return i8;
}
//------------------------
// convert int to int8
//------------------------
int16_t to_int16(int val)
{
int16_t i16 = val & 0x7FFF;
if (val < 0)
i16 |= 0x8000;
return i16;
}
//----------------------------------------------------
// compute postbyte for direct addressing with offset
//----------------------------------------------------
void constant_offset_direct(int16_t offset, int index_reg)
{
if (offset == 0)
{
assert(fixup_pending_index == FP_NONE);
emit_buf(0x84 | (index_reg << 5));
}
else if (offset >= -16 && offset <= 15)
{
// compute 5-bit signed value
char byte_offset = offset & 0xF;
if (offset < 0)
byte_offset |= 0x10;
assert(fixup_pending_index == FP_NONE);
emit_buf(byte_offset | (index_reg << 5));
}
else if (offset >= -128 && offset <= 127)
{
// compute 8-bit signed value
char byte_offset = offset & 0x7F;
if (offset < 0)
byte_offset |= 0x80;
emit_buf(0x88 | (index_reg << 5));
emit_buf(byte_offset);
}
else
{
emit_buf(0x89 | (index_reg << 5));
emit_buf_word(offset);
}
}
//------------------------------------------------------
// compute postbyte for indirect addressing with offset
//------------------------------------------------------
void constant_offset_indirect(int16_t offset, int index_reg)
{
if (offset == 0)
{
assert(fixup_pending_index == FP_NONE);
emit_buf(0x94 | (index_reg << 5));
}
else if (offset >= -128 && offset <= 127)
{
// compute 8-bit signed value
char byte_offset = offset & 0x7F;
if (offset < 0)
byte_offset |= 0x80;
emit_buf(0x98 | (index_reg << 5));
emit_buf(byte_offset);
}
else
{
emit_buf(0x99 | (index_reg << 5));
emit_buf_word(offset);
}
}
//----------------------------------------------------------------
// compute postbyte for direct addressing offset from accumulator
//----------------------------------------------------------------
void accumulator_offset_direct(int accumulator, int index_reg)
{
switch(accumulator)
{
case 0: // A
emit_buf(0x86 | (index_reg << 5));
break;
case 1: // B
emit_buf(0x85 | (index_reg << 5));
break;
case 2: // D
emit_buf(0x8B | (index_reg << 5));
break;
default:
yyerror("invalid accumulator value");
}
}
//------------------------------------------------------------------
// compute postbyte for indirect addressing offset from accumulator
//------------------------------------------------------------------
void accumulator_offset_indirect(int accumulator, int index_reg)
{
switch(accumulator)
{
case 0: // A
emit_buf(0x96 | (index_reg << 5));
break;
case 1: // B
emit_buf(0x95 | (index_reg << 5));
break;
case 2: // D
emit_buf(0x9B | (index_reg << 5));
break;
default:
yyerror("invalid accumulator value");
}
}
//----------------------------------------------------
// compute postbyte for PC relative direct addressing
//----------------------------------------------------
void pcr_direct(int16_t num)
{
int16_t offset = num - (addr + origin_addr + 3);
if (offset >= -128 && offset <= 127)
{
// compute 8-bit signed value
char byte_offset = offset & 0x7F;
if (offset < 0)
byte_offset |= 0x80;
emit_buf(0x8C);
emit_buf(byte_offset);
}
else
{
emit_buf(0x8D);
emit_buf_word(offset - 1);
}
}
//------------------------------------------------------
// compute postbyte for PC relative indirect addressing
//------------------------------------------------------
void pcr_indirect(int16_t num)
{
int16_t offset = num - (addr + origin_addr + 3);
if (offset >= -128 && offset <= 127)
{
// compute 8-bit signed value
char byte_offset = offset & 0x7F;
if (offset < 0)
byte_offset |= 0x80;
emit_buf(0x9C);
emit_buf(byte_offset);
}
else
{
emit_buf(0x9D);
emit_buf_word(offset - 1);
}
}
//----------------------------------------------------
// alter the type of fixup and potentially adjust addr
//----------------------------------------------------
void adjust_fixup(FIXUP_TYPE fixup_type, int adjustment)
{
// see if we need to adjust a fixup based on address mode
if (fixup_pending_index != FP_NONE)
{
if (fixup_type != FIXUP_NOCHANGE)
fixups[fixup_pending_index].type = fixup_type;
fixups[fixup_pending_index].addr += adjustment;
LOG("Updating fixup type to %s and addr by %d to %d\n", fixup_names[fixup_type], adjustment, fixups[fixup_pending_index].addr);
}
}
//----------------------------------------------------
// compute relative branch offset
//----------------------------------------------------
void rel_branch(BRANCH_TYPE branch_dist, int op, int dest_addr)
{
int rel_offset = dest_addr - (addr + origin_addr);
if (branch_dist == BR_SHORT)
{
if (fixup_pending_index != FP_NONE)
{
adjust_fixup(FIXUP_REL8, 0);
rel_offset = 0;
}
else
{
rel_offset -= 2;
}
if (rel_offset < -128 || rel_offset > 127)
{
printf("offset is %d\n", rel_offset);
yyerror("BYTE OVERFLOW");
return;
} else
{
// compute 8-bit signed value
char byte_offset = rel_offset & 0x7F;
if (rel_offset < 0)
byte_offset |= 0x80;
emit(op);
emit(LOBYTE(byte_offset));
}
} else // Long branch
{
if (fixup_pending_index != FP_NONE)
{
adjust_fixup(FIXUP_REL16, 0 + ((branch_dist != BR_LONG_NOPREFIX) ? 1 : 0));
rel_offset = 0;
}
else
{
rel_offset -=4;
}
if (branch_dist != BR_LONG_NOPREFIX)
emit(0x10);
else
rel_offset++;
emit(op);
emit_word(rel_offset);
}
}
/* Enabling traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
/* Enabling verbose error messages. */
#ifdef YYERROR_VERBOSE
# undef YYERROR_VERBOSE
# define YYERROR_VERBOSE 1
#else
# define YYERROR_VERBOSE 0
#endif
/* Enabling the token table. */
#ifndef YYTOKEN_TABLE
# define YYTOKEN_TABLE 0
#endif
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED