-
Notifications
You must be signed in to change notification settings - Fork 105
/
m68kmake.c
1408 lines (1208 loc) · 42.9 KB
/
m68kmake.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
/* ======================================================================== */
/* ========================= LICENSING & COPYRIGHT ======================== */
/* ======================================================================== */
/*
* MUSASHI
* Version 4.60
*
* A portable Motorola M680x0 processor emulation engine.
* Copyright Karl Stenerud. All rights reserved.
* FPU and MMU by R. Belmont.
*
* 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.
*/
/* ======================================================================== */
/* ============================ CODE GENERATOR ============================ */
/* ======================================================================== */
/*
* This is the code generator program which will generate the opcode table
* and the final opcode handlers.
*
* It requires an input file to function (default m68k_in.c), but you can
* specify your own like so:
*
* m68kmake <output path> <input file>
*
* where output path is the path where the output files should be placed, and
* input file is the file to use for input.
*
* If you modify the input file greatly from its released form, you may have
* to tweak the configuration section a bit since I'm using static allocation
* to keep things simple.
*
*
* TODO: - build a better code generator for the move instruction.
* - Add callm and rtm instructions
* - Fix RTE to handle other format words
* - Add address error (and bus error?) handling
*/
static const char g_version[] = "4.60";
/* ======================================================================== */
/* =============================== INCLUDES =============================== */
/* ======================================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
/* ======================================================================== */
/* ============================= CONFIGURATION ============================ */
/* ======================================================================== */
#define M68K_MAX_PATH 1024
#define M68K_MAX_DIR 1024
#define MAX_LINE_LENGTH 200 /* length of 1 line */
#define MAX_BODY_LENGTH 300 /* Number of lines in 1 function */
#define MAX_REPLACE_LENGTH 30 /* Max number of replace strings */
#define MAX_INSERT_LENGTH 5000 /* Max size of insert piece */
#define MAX_NAME_LENGTH 30 /* Max length of ophandler name */
#define MAX_SPEC_PROC_LENGTH 4 /* Max length of special processing str */
#define MAX_SPEC_EA_LENGTH 5 /* Max length of specified EA str */
#define EA_ALLOWED_LENGTH 11 /* Max length of ea allowed str */
#define MAX_OPCODE_INPUT_TABLE_LENGTH 1000 /* Max length of opcode handler tbl */
#define MAX_OPCODE_OUTPUT_TABLE_LENGTH 3000 /* Max length of opcode handler tbl */
/* Default filenames */
#define FILENAME_INPUT "m68k_in.c"
#define FILENAME_PROTOTYPE "m68kops.h"
#define FILENAME_TABLE "m68kops.c"
/* Identifier sequences recognized by this program */
#define ID_INPUT_SEPARATOR "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
#define ID_BASE "M68KMAKE"
#define ID_PROTOTYPE_HEADER ID_BASE "_PROTOTYPE_HEADER"
#define ID_PROTOTYPE_FOOTER ID_BASE "_PROTOTYPE_FOOTER"
#define ID_TABLE_HEADER ID_BASE "_TABLE_HEADER"
#define ID_TABLE_FOOTER ID_BASE "_TABLE_FOOTER"
#define ID_TABLE_BODY ID_BASE "_TABLE_BODY"
#define ID_TABLE_START ID_BASE "_TABLE_START"
#define ID_OPHANDLER_HEADER ID_BASE "_OPCODE_HANDLER_HEADER"
#define ID_OPHANDLER_FOOTER ID_BASE "_OPCODE_HANDLER_FOOTER"
#define ID_OPHANDLER_BODY ID_BASE "_OPCODE_HANDLER_BODY"
#define ID_END ID_BASE "_END"
#define ID_OPHANDLER_NAME ID_BASE "_OP"
#define ID_OPHANDLER_EA_AY_8 ID_BASE "_GET_EA_AY_8"
#define ID_OPHANDLER_EA_AY_16 ID_BASE "_GET_EA_AY_16"
#define ID_OPHANDLER_EA_AY_32 ID_BASE "_GET_EA_AY_32"
#define ID_OPHANDLER_OPER_AY_8 ID_BASE "_GET_OPER_AY_8"
#define ID_OPHANDLER_OPER_AY_16 ID_BASE "_GET_OPER_AY_16"
#define ID_OPHANDLER_OPER_AY_32 ID_BASE "_GET_OPER_AY_32"
#define ID_OPHANDLER_CC ID_BASE "_CC"
#define ID_OPHANDLER_NOT_CC ID_BASE "_NOT_CC"
#ifndef DECL_SPEC
#define DECL_SPEC
#endif /* DECL_SPEC */
/* ======================================================================== */
/* ============================== PROTOTYPES ============================== */
/* ======================================================================== */
enum {
CPU_TYPE_000=0,
CPU_TYPE_010,
CPU_TYPE_020,
CPU_TYPE_030,
CPU_TYPE_040,
NUM_CPUS
};
#define UNSPECIFIED "."
#define UNSPECIFIED_CH '.'
#define HAS_NO_EA_MODE(A) (strcmp(A, "..........") == 0)
#define HAS_EA_AI(A) ((A)[0] == 'A')
#define HAS_EA_PI(A) ((A)[1] == '+')
#define HAS_EA_PD(A) ((A)[2] == '-')
#define HAS_EA_DI(A) ((A)[3] == 'D')
#define HAS_EA_IX(A) ((A)[4] == 'X')
#define HAS_EA_AW(A) ((A)[5] == 'W')
#define HAS_EA_AL(A) ((A)[6] == 'L')
#define HAS_EA_PCDI(A) ((A)[7] == 'd')
#define HAS_EA_PCIX(A) ((A)[8] == 'x')
#define HAS_EA_I(A) ((A)[9] == 'I')
enum
{
EA_MODE_NONE, /* No special addressing mode */
EA_MODE_AI, /* Address register indirect */
EA_MODE_PI, /* Address register indirect with postincrement */
EA_MODE_PI7, /* Address register 7 indirect with postincrement */
EA_MODE_PD, /* Address register indirect with predecrement */
EA_MODE_PD7, /* Address register 7 indirect with predecrement */
EA_MODE_DI, /* Address register indirect with displacement */
EA_MODE_IX, /* Address register indirect with index */
EA_MODE_AW, /* Absolute word */
EA_MODE_AL, /* Absolute long */
EA_MODE_PCDI, /* Program counter indirect with displacement */
EA_MODE_PCIX, /* Program counter indirect with index */
EA_MODE_I /* Immediate */
};
/* Everything we need to know about an opcode */
typedef struct
{
char name[MAX_NAME_LENGTH]; /* opcode handler name */
unsigned char size; /* Size of operation */
char spec_proc[MAX_SPEC_PROC_LENGTH]; /* Special processing mode */
char spec_ea[MAX_SPEC_EA_LENGTH]; /* Specified effective addressing mode */
unsigned char bits; /* Number of significant bits (used for sorting the table) */
unsigned short op_mask; /* Mask to apply for matching an opcode to a handler */
unsigned short op_match; /* Value to match after masking */
char ea_allowed[EA_ALLOWED_LENGTH]; /* Effective addressing modes allowed */
char cpu_mode[NUM_CPUS]; /* User or supervisor mode */
char cpus[NUM_CPUS+1]; /* Allowed CPUs */
unsigned char cycles[NUM_CPUS]; /* cycles for 000, 010, 020, 030, 040 */
} opcode_struct;
/* All modifications necessary for a specific EA mode of an instruction */
typedef struct
{
char* fname_add;
char* ea_add;
unsigned int mask_add;
unsigned int match_add;
} ea_info_struct;
/* Holds the body of a function */
typedef struct
{
char body[MAX_BODY_LENGTH][MAX_LINE_LENGTH+1];
int length;
} body_struct;
/* Holds a sequence of search / replace strings */
typedef struct
{
char replace[MAX_REPLACE_LENGTH][2][MAX_LINE_LENGTH+1];
int length;
} replace_struct;
/* Function Prototypes */
void error_exit(char* fmt, ...);
void perror_exit(char* fmt, ...);
int check_strsncpy(char* dst, char* src, int maxlength);
int check_atoi(char* str, int *result);
int skip_spaces(char* str);
int num_bits(int value);
int atoh(char* buff);
int fgetline(char* buff, int nchars, FILE* file);
int get_oper_cycles(opcode_struct* op, int ea_mode, int cpu_type);
opcode_struct* find_opcode(char* name, int size, char* spec_proc, char* spec_ea);
opcode_struct* find_illegal_opcode(void);
int extract_opcode_info(char* src, char* name, int* size, char* spec_proc, char* spec_ea);
void add_replace_string(replace_struct* replace, char* search_str, char* replace_str);
void write_body(FILE* filep, body_struct* body, replace_struct* replace);
void get_base_name(char* base_name, opcode_struct* op);
void write_function_name(FILE* filep, char* base_name);
void add_opcode_output_table_entry(opcode_struct* op, char* name);
static int DECL_SPEC compare_nof_true_bits(const void* aptr, const void* bptr);
void print_opcode_output_table(FILE* filep);
void write_table_entry(FILE* filep, opcode_struct* op);
void set_opcode_struct(opcode_struct* src, opcode_struct* dst, int ea_mode);
void generate_opcode_handler(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* opinfo, int ea_mode);
void generate_opcode_ea_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op);
void generate_opcode_cc_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op_in, int offset);
void process_opcode_handlers(FILE* filep);
void populate_table(void);
void read_insert(char* insert);
/* ======================================================================== */
/* ================================= DATA ================================= */
/* ======================================================================== */
/* Name of the input file */
char g_input_filename[M68K_MAX_PATH] = FILENAME_INPUT;
/* File handles */
FILE* g_input_file = NULL;
FILE* g_prototype_file = NULL;
FILE* g_table_file = NULL;
int g_num_functions = 0; /* Number of functions processed */
int g_num_primitives = 0; /* Number of function primitives read */
int g_line_number = 1; /* Current line number */
/* Opcode handler table */
opcode_struct g_opcode_input_table[MAX_OPCODE_INPUT_TABLE_LENGTH];
opcode_struct g_opcode_output_table[MAX_OPCODE_OUTPUT_TABLE_LENGTH];
int g_opcode_output_table_length = 0;
const ea_info_struct g_ea_info_table[13] =
{/* fname ea mask match */
{"", "", 0x00, 0x00}, /* EA_MODE_NONE */
{"ai", "AY_AI", 0x38, 0x10}, /* EA_MODE_AI */
{"pi", "AY_PI", 0x38, 0x18}, /* EA_MODE_PI */
{"pi7", "A7_PI", 0x3f, 0x1f}, /* EA_MODE_PI7 */
{"pd", "AY_PD", 0x38, 0x20}, /* EA_MODE_PD */
{"pd7", "A7_PD", 0x3f, 0x27}, /* EA_MODE_PD7 */
{"di", "AY_DI", 0x38, 0x28}, /* EA_MODE_DI */
{"ix", "AY_IX", 0x38, 0x30}, /* EA_MODE_IX */
{"aw", "AW", 0x3f, 0x38}, /* EA_MODE_AW */
{"al", "AL", 0x3f, 0x39}, /* EA_MODE_AL */
{"pcdi", "PCDI", 0x3f, 0x3a}, /* EA_MODE_PCDI */
{"pcix", "PCIX", 0x3f, 0x3b}, /* EA_MODE_PCIX */
{"i", "I", 0x3f, 0x3c}, /* EA_MODE_I */
};
const char *const g_cc_table[16][2] =
{
{ "t", "T"}, /* 0000 */
{ "f", "F"}, /* 0001 */
{"hi", "HI"}, /* 0010 */
{"ls", "LS"}, /* 0011 */
{"cc", "CC"}, /* 0100 */
{"cs", "CS"}, /* 0101 */
{"ne", "NE"}, /* 0110 */
{"eq", "EQ"}, /* 0111 */
{"vc", "VC"}, /* 1000 */
{"vs", "VS"}, /* 1001 */
{"pl", "PL"}, /* 1010 */
{"mi", "MI"}, /* 1011 */
{"ge", "GE"}, /* 1100 */
{"lt", "LT"}, /* 1101 */
{"gt", "GT"}, /* 1110 */
{"le", "LE"}, /* 1111 */
};
/* size to index translator (0 -> 0, 8 and 16 -> 1, 32 -> 2) */
const int g_size_select_table[33] =
{
0, /* unsized */
0, 0, 0, 0, 0, 0, 0, 1, /* 8 */
0, 0, 0, 0, 0, 0, 0, 1, /* 16 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 /* 32 */
};
/* Extra cycles required for certain EA modes */
/* TODO: correct timings for 030, 040 */
const int g_ea_cycle_table[13][NUM_CPUS][3] =
{/* 000 010 020 030 040 */
{{ 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}}, /* EA_MODE_NONE */
{{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_AI */
{{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_PI */
{{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_PI7 */
{{ 0, 6, 10}, { 0, 6, 10}, { 0, 5, 5}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_PD */
{{ 0, 6, 10}, { 0, 6, 10}, { 0, 5, 5}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_PD7 */
{{ 0, 8, 12}, { 0, 8, 12}, { 0, 5, 5}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_DI */
{{ 0, 10, 14}, { 0, 10, 14}, { 0, 7, 7}, { 0, 7, 7}, { 0, 7, 7}}, /* EA_MODE_IX */
{{ 0, 8, 12}, { 0, 8, 12}, { 0, 4, 4}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_AW */
{{ 0, 12, 16}, { 0, 12, 16}, { 0, 4, 4}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_AL */
{{ 0, 8, 12}, { 0, 8, 12}, { 0, 5, 5}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_PCDI */
{{ 0, 10, 14}, { 0, 10, 14}, { 0, 7, 7}, { 0, 7, 7}, { 0, 7, 7}}, /* EA_MODE_PCIX */
{{ 0, 4, 8}, { 0, 4, 8}, { 0, 2, 4}, { 0, 2, 4}, { 0, 2, 4}}, /* EA_MODE_I */
};
/* Extra cycles for JMP instruction (000, 010) */
const int g_jmp_cycle_table[13] =
{
0, /* EA_MODE_NONE */
4, /* EA_MODE_AI */
0, /* EA_MODE_PI */
0, /* EA_MODE_PI7 */
0, /* EA_MODE_PD */
0, /* EA_MODE_PD7 */
6, /* EA_MODE_DI */
10, /* EA_MODE_IX */
6, /* EA_MODE_AW */
8, /* EA_MODE_AL */
6, /* EA_MODE_PCDI */
10, /* EA_MODE_PCIX */
0, /* EA_MODE_I */
};
/* Extra cycles for JSR instruction (000, 010) */
const int g_jsr_cycle_table[13] =
{
0, /* EA_MODE_NONE */
4, /* EA_MODE_AI */
0, /* EA_MODE_PI */
0, /* EA_MODE_PI7 */
0, /* EA_MODE_PD */
0, /* EA_MODE_PD7 */
6, /* EA_MODE_DI */
10, /* EA_MODE_IX */
6, /* EA_MODE_AW */
8, /* EA_MODE_AL */
6, /* EA_MODE_PCDI */
10, /* EA_MODE_PCIX */
0, /* EA_MODE_I */
};
/* Extra cycles for LEA instruction (000, 010) */
const int g_lea_cycle_table[13] =
{
0, /* EA_MODE_NONE */
4, /* EA_MODE_AI */
0, /* EA_MODE_PI */
0, /* EA_MODE_PI7 */
0, /* EA_MODE_PD */
0, /* EA_MODE_PD7 */
8, /* EA_MODE_DI */
12, /* EA_MODE_IX */
8, /* EA_MODE_AW */
12, /* EA_MODE_AL */
8, /* EA_MODE_PCDI */
12, /* EA_MODE_PCIX */
0, /* EA_MODE_I */
};
/* Extra cycles for PEA instruction (000, 010) */
const int g_pea_cycle_table[13] =
{
0, /* EA_MODE_NONE */
6, /* EA_MODE_AI */
0, /* EA_MODE_PI */
0, /* EA_MODE_PI7 */
0, /* EA_MODE_PD */
0, /* EA_MODE_PD7 */
10, /* EA_MODE_DI */
14, /* EA_MODE_IX */
10, /* EA_MODE_AW */
14, /* EA_MODE_AL */
10, /* EA_MODE_PCDI */
14, /* EA_MODE_PCIX */
0, /* EA_MODE_I */
};
/* Extra cycles for MOVEM instruction (000, 010) */
const int g_movem_cycle_table[13] =
{
0, /* EA_MODE_NONE */
0, /* EA_MODE_AI */
0, /* EA_MODE_PI */
0, /* EA_MODE_PI7 */
0, /* EA_MODE_PD */
0, /* EA_MODE_PD7 */
4, /* EA_MODE_DI */
6, /* EA_MODE_IX */
4, /* EA_MODE_AW */
8, /* EA_MODE_AL */
0, /* EA_MODE_PCDI */
0, /* EA_MODE_PCIX */
0, /* EA_MODE_I */
};
/* Extra cycles for MOVES instruction (010) */
const int g_moves_cycle_table[13][3] =
{
{ 0, 0, 0}, /* EA_MODE_NONE */
{ 0, 4, 6}, /* EA_MODE_AI */
{ 0, 4, 6}, /* EA_MODE_PI */
{ 0, 4, 6}, /* EA_MODE_PI7 */
{ 0, 6, 12}, /* EA_MODE_PD */
{ 0, 6, 12}, /* EA_MODE_PD7 */
{ 0, 12, 16}, /* EA_MODE_DI */
{ 0, 16, 20}, /* EA_MODE_IX */
{ 0, 12, 16}, /* EA_MODE_AW */
{ 0, 16, 20}, /* EA_MODE_AL */
{ 0, 0, 0}, /* EA_MODE_PCDI */
{ 0, 0, 0}, /* EA_MODE_PCIX */
{ 0, 0, 0}, /* EA_MODE_I */
};
/* Extra cycles for CLR instruction (010) */
const int g_clr_cycle_table[13][3] =
{
{ 0, 0, 0}, /* EA_MODE_NONE */
{ 0, 4, 6}, /* EA_MODE_AI */
{ 0, 4, 6}, /* EA_MODE_PI */
{ 0, 4, 6}, /* EA_MODE_PI7 */
{ 0, 6, 8}, /* EA_MODE_PD */
{ 0, 6, 8}, /* EA_MODE_PD7 */
{ 0, 8, 10}, /* EA_MODE_DI */
{ 0, 10, 14}, /* EA_MODE_IX */
{ 0, 8, 10}, /* EA_MODE_AW */
{ 0, 10, 14}, /* EA_MODE_AL */
{ 0, 0, 0}, /* EA_MODE_PCDI */
{ 0, 0, 0}, /* EA_MODE_PCIX */
{ 0, 0, 0}, /* EA_MODE_I */
};
/* ======================================================================== */
/* =========================== UTILITY FUNCTIONS ========================== */
/* ======================================================================== */
/* Print an error message and exit with status error */
void error_exit(char* fmt, ...)
{
va_list args;
fprintf(stderr, "In %s, near or on line %d:\n\t", g_input_filename, g_line_number);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
if(g_prototype_file) fclose(g_prototype_file);
if(g_table_file) fclose(g_table_file);
if(g_input_file) fclose(g_input_file);
exit(EXIT_FAILURE);
}
/* Print an error message, call perror(), and exit with status error */
void perror_exit(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
perror("");
if(g_prototype_file) fclose(g_prototype_file);
if(g_table_file) fclose(g_table_file);
if(g_input_file) fclose(g_input_file);
exit(EXIT_FAILURE);
}
/* copy until 0 or space and exit with error if we read too far */
int check_strsncpy(char* dst, char* src, int maxlength)
{
char* p = dst;
while(*src && *src != ' ')
{
*p++ = *src++;
if(p - dst > maxlength)
error_exit("Field too long");
}
*p = 0;
return p - dst;
}
/* copy until 0 or specified character and exit with error if we read too far */
int check_strcncpy(char* dst, char* src, char delim, int maxlength)
{
char* p = dst;
while(*src && *src != delim)
{
*p++ = *src++;
if(p - dst > maxlength)
error_exit("Field too long");
}
*p = 0;
return p - dst;
}
/* convert ascii to integer and exit with error if we find invalid data */
int check_atoi(char* str, int *result)
{
int accum = 0;
char* p = str;
while(*p >= '0' && *p <= '9')
{
accum *= 10;
accum += *p++ - '0';
}
if(*p != ' ' && *p != 0)
error_exit("Malformed integer value (%c)", *p);
*result = accum;
return p - str;
}
/* Skip past spaces in a string */
int skip_spaces(char* str)
{
char* p = str;
while(*p == ' ')
p++;
return p - str;
}
/* Count the number of set bits in a value */
int num_bits(int value)
{
value = ((value & 0xaaaa) >> 1) + (value & 0x5555);
value = ((value & 0xcccc) >> 2) + (value & 0x3333);
value = ((value & 0xf0f0) >> 4) + (value & 0x0f0f);
value = ((value & 0xff00) >> 8) + (value & 0x00ff);
return value;
}
/* Convert a hex value written in ASCII */
int atoh(char* buff)
{
int accum = 0;
for(;;buff++)
{
if(*buff >= '0' && *buff <= '9')
{
accum <<= 4;
accum += *buff - '0';
}
else if(*buff >= 'a' && *buff <= 'f')
{
accum <<= 4;
accum += *buff - 'a' + 10;
}
else break;
}
return accum;
}
/* Get a line of text from a file, discarding any end-of-line characters */
int fgetline(char* buff, int nchars, FILE* file)
{
int length;
if(fgets(buff, nchars, file) == NULL)
return -1;
if(buff[0] == '\r')
memmove(buff, buff + 1, nchars - 1);
length = strlen(buff);
while(length && (buff[length-1] == '\r' || buff[length-1] == '\n'))
length--;
buff[length] = 0;
g_line_number++;
return length;
}
/* ======================================================================== */
/* =========================== HELPER FUNCTIONS =========================== */
/* ======================================================================== */
/* Calculate the number of cycles an opcode requires */
int get_oper_cycles(opcode_struct* op, int ea_mode, int cpu_type)
{
int size = g_size_select_table[op->size];
if(op->cpus[cpu_type] == '.')
return 0;
if(cpu_type < CPU_TYPE_020)
{
if(cpu_type == CPU_TYPE_010)
{
if(strcmp(op->name, "moves") == 0)
return op->cycles[cpu_type] + g_moves_cycle_table[ea_mode][size];
if(strcmp(op->name, "clr") == 0)
return op->cycles[cpu_type] + g_clr_cycle_table[ea_mode][size];
}
/* ASG: added these cases -- immediate modes take 2 extra cycles here */
if(cpu_type == CPU_TYPE_000 && ea_mode == EA_MODE_I &&
((strcmp(op->name, "add") == 0 && strcmp(op->spec_proc, "er") == 0) ||
strcmp(op->name, "adda") == 0 ||
(strcmp(op->name, "and") == 0 && strcmp(op->spec_proc, "er") == 0) ||
(strcmp(op->name, "or") == 0 && strcmp(op->spec_proc, "er") == 0) ||
(strcmp(op->name, "sub") == 0 && strcmp(op->spec_proc, "er") == 0) ||
strcmp(op->name, "suba") == 0))
return op->cycles[cpu_type] + g_ea_cycle_table[ea_mode][cpu_type][size] + 2;
if(strcmp(op->name, "jmp") == 0)
return op->cycles[cpu_type] + g_jmp_cycle_table[ea_mode];
if(strcmp(op->name, "jsr") == 0)
return op->cycles[cpu_type] + g_jsr_cycle_table[ea_mode];
if(strcmp(op->name, "lea") == 0)
return op->cycles[cpu_type] + g_lea_cycle_table[ea_mode];
if(strcmp(op->name, "pea") == 0)
return op->cycles[cpu_type] + g_pea_cycle_table[ea_mode];
if(strcmp(op->name, "movem") == 0)
return op->cycles[cpu_type] + g_movem_cycle_table[ea_mode];
}
return op->cycles[cpu_type] + g_ea_cycle_table[ea_mode][cpu_type][size];
}
/* Find an opcode in the opcode handler list */
opcode_struct* find_opcode(char* name, int size, char* spec_proc, char* spec_ea)
{
opcode_struct* op;
for(op = g_opcode_input_table;op < &g_opcode_input_table[MAX_OPCODE_INPUT_TABLE_LENGTH];op++)
{
if( strcmp(name, op->name) == 0 &&
(size == op->size) &&
strcmp(spec_proc, op->spec_proc) == 0 &&
strcmp(spec_ea, op->spec_ea) == 0)
return op;
}
return NULL;
}
/* Specifically find the illegal opcode in the list */
opcode_struct* find_illegal_opcode(void)
{
opcode_struct* op;
for(op = g_opcode_input_table;op < &g_opcode_input_table[MAX_OPCODE_INPUT_TABLE_LENGTH];op++)
{
if(strcmp(op->name, "illegal") == 0)
return op;
}
return NULL;
}
/* Parse an opcode handler name */
int extract_opcode_info(char* src, char* name, int* size, char* spec_proc, char* spec_ea)
{
char* ptr = strstr(src, ID_OPHANDLER_NAME);
if(ptr == NULL)
return 0;
ptr += strlen(ID_OPHANDLER_NAME) + 1;
ptr += check_strcncpy(name, ptr, ',', MAX_NAME_LENGTH);
if(*ptr != ',') return 0;
ptr++;
ptr += skip_spaces(ptr);
*size = atoi(ptr);
ptr = strstr(ptr, ",");
if(ptr == NULL) return 0;
ptr++;
ptr += skip_spaces(ptr);
ptr += check_strcncpy(spec_proc, ptr, ',', MAX_SPEC_PROC_LENGTH);
if(*ptr != ',') return 0;
ptr++;
ptr += skip_spaces(ptr);
ptr += check_strcncpy(spec_ea, ptr, ')', MAX_SPEC_EA_LENGTH);
if(*ptr != ')') return 0;
ptr++;
ptr += skip_spaces(ptr);
return 1;
}
/* Add a search/replace pair to a replace structure */
void add_replace_string(replace_struct* replace, char* search_str, char* replace_str)
{
if(replace->length >= MAX_REPLACE_LENGTH)
error_exit("overflow in replace structure");
strcpy(replace->replace[replace->length][0], search_str);
strcpy(replace->replace[replace->length++][1], replace_str);
}
/* Write a function body while replacing any selected strings */
void write_body(FILE* filep, body_struct* body, replace_struct* replace)
{
int i;
int j;
char* ptr;
char output[MAX_LINE_LENGTH+1];
char temp_buff[MAX_LINE_LENGTH+1];
int found;
for(i=0;i<body->length;i++)
{
strcpy(output, body->body[i]);
/* Check for the base directive header */
if(strstr(output, ID_BASE) != NULL)
{
/* Search for any text we need to replace */
found = 0;
for(j=0;j<replace->length;j++)
{
ptr = strstr(output, replace->replace[j][0]);
if(ptr)
{
/* We found something to replace */
found = 1;
strcpy(temp_buff, ptr+strlen(replace->replace[j][0]));
strcpy(ptr, replace->replace[j][1]);
strcat(ptr, temp_buff);
}
}
/* Found a directive with no matching replace string */
if(!found)
error_exit("Unknown " ID_BASE " directive [%s]", output);
}
fprintf(filep, "%s\n", output);
}
fprintf(filep, "\n\n");
}
/* Generate a base function name from an opcode struct */
void get_base_name(char* base_name, opcode_struct* op)
{
sprintf(base_name, "m68k_op_%s", op->name);
if(op->size > 0)
sprintf(base_name+strlen(base_name), "_%d", op->size);
if(strcmp(op->spec_proc, UNSPECIFIED) != 0)
sprintf(base_name+strlen(base_name), "_%s", op->spec_proc);
if(strcmp(op->spec_ea, UNSPECIFIED) != 0)
sprintf(base_name+strlen(base_name), "_%s", op->spec_ea);
}
/* Write the name of an opcode handler function */
void write_function_name(FILE* filep, char* base_name)
{
fprintf(filep, "static void %s(m68ki_cpu_core *state)\n", base_name);
}
void add_opcode_output_table_entry(opcode_struct* op, char* name)
{
opcode_struct* ptr;
if(g_opcode_output_table_length > MAX_OPCODE_OUTPUT_TABLE_LENGTH)
error_exit("Opcode output table overflow");
ptr = g_opcode_output_table + g_opcode_output_table_length++;
*ptr = *op;
strcpy(ptr->name, name);
ptr->bits = num_bits(ptr->op_mask);
}
/*
* Comparison function for qsort()
* For entries with an equal number of set bits in
* the mask compare the match values
*/
static int DECL_SPEC compare_nof_true_bits(const void* aptr, const void* bptr)
{
const opcode_struct *a = aptr, *b = bptr;
if(a->bits != b->bits)
return a->bits - b->bits;
if(a->op_mask != b->op_mask)
return a->op_mask - b->op_mask;
return a->op_match - b->op_match;
}
void print_opcode_output_table(FILE* filep)
{
int i;
qsort((void *)g_opcode_output_table, g_opcode_output_table_length, sizeof(g_opcode_output_table[0]), compare_nof_true_bits);
for(i=0;i<g_opcode_output_table_length;i++)
write_table_entry(filep, g_opcode_output_table+i);
}
/* Write an entry in the opcode handler table */
void write_table_entry(FILE* filep, opcode_struct* op)
{
int i;
fprintf(filep, "\t{%-28s, 0x%04x, 0x%04x, {",
op->name, op->op_mask, op->op_match);
for(i=0;i<NUM_CPUS;i++)
{
fprintf(filep, "%3d", op->cycles[i]);
if(i < NUM_CPUS-1)
fprintf(filep, ", ");
}
fprintf(filep, "}},\n");
}
/* Fill out an opcode struct with a specific addressing mode of the source opcode struct */
void set_opcode_struct(opcode_struct* src, opcode_struct* dst, int ea_mode)
{
int i;
*dst = *src;
for(i=0;i<NUM_CPUS;i++)
dst->cycles[i] = get_oper_cycles(dst, ea_mode, i);
if(strcmp(dst->spec_ea, UNSPECIFIED) == 0 && ea_mode != EA_MODE_NONE)
sprintf(dst->spec_ea, "%s", g_ea_info_table[ea_mode].fname_add);
dst->op_mask |= g_ea_info_table[ea_mode].mask_add;
dst->op_match |= g_ea_info_table[ea_mode].match_add;
}
/* Generate a final opcode handler from the provided data */
void generate_opcode_handler(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* opinfo, int ea_mode)
{
char str[MAX_LINE_LENGTH+1];
opcode_struct* op = malloc(sizeof(opcode_struct));
/* Set the opcode structure and write the tables, prototypes, etc */
set_opcode_struct(opinfo, op, ea_mode);
get_base_name(str, op);
add_opcode_output_table_entry(op, str);
write_function_name(filep, str);
/* Add any replace strings needed */
if(ea_mode != EA_MODE_NONE)
{
sprintf(str, "EA_%s_8()", g_ea_info_table[ea_mode].ea_add);
add_replace_string(replace, ID_OPHANDLER_EA_AY_8, str);
sprintf(str, "EA_%s_16()", g_ea_info_table[ea_mode].ea_add);
add_replace_string(replace, ID_OPHANDLER_EA_AY_16, str);
sprintf(str, "EA_%s_32()", g_ea_info_table[ea_mode].ea_add);
add_replace_string(replace, ID_OPHANDLER_EA_AY_32, str);
sprintf(str, "OPER_%s_8(state)", g_ea_info_table[ea_mode].ea_add);
add_replace_string(replace, ID_OPHANDLER_OPER_AY_8, str);
sprintf(str, "OPER_%s_16(state)", g_ea_info_table[ea_mode].ea_add);
add_replace_string(replace, ID_OPHANDLER_OPER_AY_16, str);
sprintf(str, "OPER_%s_32(state)", g_ea_info_table[ea_mode].ea_add);
add_replace_string(replace, ID_OPHANDLER_OPER_AY_32, str);
}
/* Now write the function body with the selected replace strings */
write_body(filep, body, replace);
g_num_functions++;
free(op);
}
/* Generate opcode variants based on available addressing modes */
void generate_opcode_ea_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op)
{
int old_length = replace->length;
/* No ea modes available for this opcode */
if(HAS_NO_EA_MODE(op->ea_allowed))
{
generate_opcode_handler(filep, body, replace, op, EA_MODE_NONE);
return;
}
/* Check for and create specific opcodes for each available addressing mode */
if(HAS_EA_AI(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_AI);
replace->length = old_length;
if(HAS_EA_PI(op->ea_allowed))
{
generate_opcode_handler(filep, body, replace, op, EA_MODE_PI);
replace->length = old_length;
if(op->size == 8)
generate_opcode_handler(filep, body, replace, op, EA_MODE_PI7);
}
replace->length = old_length;
if(HAS_EA_PD(op->ea_allowed))
{
generate_opcode_handler(filep, body, replace, op, EA_MODE_PD);
replace->length = old_length;
if(op->size == 8)
generate_opcode_handler(filep, body, replace, op, EA_MODE_PD7);
}
replace->length = old_length;
if(HAS_EA_DI(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_DI);
replace->length = old_length;
if(HAS_EA_IX(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_IX);
replace->length = old_length;
if(HAS_EA_AW(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_AW);
replace->length = old_length;
if(HAS_EA_AL(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_AL);
replace->length = old_length;
if(HAS_EA_PCDI(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_PCDI);
replace->length = old_length;
if(HAS_EA_PCIX(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_PCIX);
replace->length = old_length;
if(HAS_EA_I(op->ea_allowed))
generate_opcode_handler(filep, body, replace, op, EA_MODE_I);
replace->length = old_length;
}
/* Generate variants of condition code opcodes */
void generate_opcode_cc_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op_in, int offset)
{
char repl[20];
char replnot[20];
int i;
int old_length = replace->length;
opcode_struct* op = malloc(sizeof(opcode_struct));
*op = *op_in;
op->op_mask |= 0x0f00;
/* Do all condition codes except t and f */
for(i=2;i<16;i++)
{
/* Add replace strings for this condition code */
sprintf(repl, "COND_%s()", g_cc_table[i][1]);
sprintf(replnot, "COND_NOT_%s()", g_cc_table[i][1]);
add_replace_string(replace, ID_OPHANDLER_CC, repl);
add_replace_string(replace, ID_OPHANDLER_NOT_CC, replnot);
/* Set the new opcode info */
strcpy(op->name+offset, g_cc_table[i][0]);
op->op_match = (op->op_match & 0xf0ff) | (i<<8);
/* Generate all opcode variants for this modified opcode */
generate_opcode_ea_variants(filep, body, replace, op);
/* Remove the above replace strings */
replace->length = old_length;
}
free(op);
}
/* Process the opcode handlers section of the input file */
void process_opcode_handlers(FILE* filep)
{
FILE* input_file = g_input_file;
char func_name[MAX_LINE_LENGTH+1];
char oper_name[MAX_LINE_LENGTH+1];
int oper_size;
char oper_spec_proc[MAX_LINE_LENGTH+1];
char oper_spec_ea[MAX_LINE_LENGTH+1];
opcode_struct* opinfo;
replace_struct* replace = malloc(sizeof(replace_struct));
body_struct* body = malloc(sizeof(body_struct));