1 /* ======================================================================== */
\r
2 /* ========================= LICENSING & COPYRIGHT ======================== */
\r
3 /* ======================================================================== */
\r
8 * A portable Motorola M680x0 processor emulation engine.
\r
9 * Copyright 1998-2007 Karl Stenerud. All rights reserved.
\r
11 * This code may be freely used for non-commercial purposes as long as this
\r
12 * copyright notice remains unaltered in the source code and any binary files
\r
13 * containing this code in compiled form.
\r
15 * All other lisencing terms must be negotiated with the author
\r
18 * The latest version of this code can be obtained at:
\r
19 * http://kstenerud.cjb.net
\r
23 * Modified For OpenVMS By: Robert Alan Byer
\r
24 * byer@mail.ourservers.net
\r
28 /* ======================================================================== */
\r
29 /* ============================ CODE GENERATOR ============================ */
\r
30 /* ======================================================================== */
\r
32 * This is the code generator program which will generate the opcode table
\r
33 * and the final opcode handlers.
\r
35 * It requires an input file to function (default m68k_in.c), but you can
\r
36 * specify your own like so:
\r
38 * m68kmake <output path> <input file>
\r
40 * where output path is the path where the output files should be placed, and
\r
41 * input file is the file to use for input.
\r
43 * If you modify the input file greatly from its released form, you may have
\r
44 * to tweak the configuration section a bit since I'm using static allocation
\r
45 * to keep things simple.
\r
48 * TODO: - build a better code generator for the move instruction.
\r
49 * - Add callm and rtm instructions
\r
50 * - Fix RTE to handle other format words
\r
51 * - Add address error (and bus error?) handling
\r
55 static const char* g_version = "3.31";
\r
57 /* ======================================================================== */
\r
58 /* =============================== INCLUDES =============================== */
\r
59 /* ======================================================================== */
\r
69 /* ======================================================================== */
\r
70 /* ============================= CONFIGURATION ============================ */
\r
71 /* ======================================================================== */
\r
73 #define M68K_MAX_PATH 1024
\r
74 #define M68K_MAX_DIR 1024
\r
76 #define MAX_LINE_LENGTH 200 /* length of 1 line */
\r
77 #define MAX_BODY_LENGTH 300 /* Number of lines in 1 function */
\r
78 #define MAX_REPLACE_LENGTH 30 /* Max number of replace strings */
\r
79 #define MAX_INSERT_LENGTH 5000 /* Max size of insert piece */
\r
80 #define MAX_NAME_LENGTH 30 /* Max length of ophandler name */
\r
81 #define MAX_SPEC_PROC_LENGTH 4 /* Max length of special processing str */
\r
82 #define MAX_SPEC_EA_LENGTH 5 /* Max length of specified EA str */
\r
83 #define EA_ALLOWED_LENGTH 11 /* Max length of ea allowed str */
\r
84 #define MAX_OPCODE_INPUT_TABLE_LENGTH 1000 /* Max length of opcode handler tbl */
\r
85 #define MAX_OPCODE_OUTPUT_TABLE_LENGTH 3000 /* Max length of opcode handler tbl */
\r
87 /* Default filenames */
\r
88 #define FILENAME_INPUT "m68k_in.c"
\r
89 #define FILENAME_PROTOTYPE "m68kops.h"
\r
90 #define FILENAME_TABLE "m68kops.c"
\r
93 /* Identifier sequences recognized by this program */
\r
95 #define ID_INPUT_SEPARATOR "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
\r
97 #define ID_BASE "M68KMAKE"
\r
98 #define ID_PROTOTYPE_HEADER ID_BASE "_PROTOTYPE_HEADER"
\r
99 #define ID_PROTOTYPE_FOOTER ID_BASE "_PROTOTYPE_FOOTER"
\r
100 #define ID_TABLE_HEADER ID_BASE "_TABLE_HEADER"
\r
101 #define ID_TABLE_FOOTER ID_BASE "_TABLE_FOOTER"
\r
102 #define ID_TABLE_BODY ID_BASE "_TABLE_BODY"
\r
103 #define ID_TABLE_START ID_BASE "_TABLE_START"
\r
104 #define ID_OPHANDLER_HEADER ID_BASE "_OPCODE_HANDLER_HEADER"
\r
105 #define ID_OPHANDLER_FOOTER ID_BASE "_OPCODE_HANDLER_FOOTER"
\r
106 #define ID_OPHANDLER_BODY ID_BASE "_OPCODE_HANDLER_BODY"
\r
107 #define ID_END ID_BASE "_END"
\r
109 #define ID_OPHANDLER_NAME ID_BASE "_OP"
\r
110 #define ID_OPHANDLER_EA_AY_8 ID_BASE "_GET_EA_AY_8"
\r
111 #define ID_OPHANDLER_EA_AY_16 ID_BASE "_GET_EA_AY_16"
\r
112 #define ID_OPHANDLER_EA_AY_32 ID_BASE "_GET_EA_AY_32"
\r
113 #define ID_OPHANDLER_OPER_AY_8 ID_BASE "_GET_OPER_AY_8"
\r
114 #define ID_OPHANDLER_OPER_AY_16 ID_BASE "_GET_OPER_AY_16"
\r
115 #define ID_OPHANDLER_OPER_AY_32 ID_BASE "_GET_OPER_AY_32"
\r
116 #define ID_OPHANDLER_CC ID_BASE "_CC"
\r
117 #define ID_OPHANDLER_NOT_CC ID_BASE "_NOT_CC"
\r
122 #endif /* DECL_SPEC */
\r
126 /* ======================================================================== */
\r
127 /* ============================== PROTOTYPES ============================== */
\r
128 /* ======================================================================== */
\r
138 #define UNSPECIFIED "."
\r
139 #define UNSPECIFIED_CH '.'
\r
141 #define HAS_NO_EA_MODE(A) (strcmp(A, "..........") == 0)
\r
142 #define HAS_EA_AI(A) ((A)[0] == 'A')
\r
143 #define HAS_EA_PI(A) ((A)[1] == '+')
\r
144 #define HAS_EA_PD(A) ((A)[2] == '-')
\r
145 #define HAS_EA_DI(A) ((A)[3] == 'D')
\r
146 #define HAS_EA_IX(A) ((A)[4] == 'X')
\r
147 #define HAS_EA_AW(A) ((A)[5] == 'W')
\r
148 #define HAS_EA_AL(A) ((A)[6] == 'L')
\r
149 #define HAS_EA_PCDI(A) ((A)[7] == 'd')
\r
150 #define HAS_EA_PCIX(A) ((A)[8] == 'x')
\r
151 #define HAS_EA_I(A) ((A)[9] == 'I')
\r
155 EA_MODE_NONE, /* No special addressing mode */
\r
156 EA_MODE_AI, /* Address register indirect */
\r
157 EA_MODE_PI, /* Address register indirect with postincrement */
\r
158 EA_MODE_PI7, /* Address register 7 indirect with postincrement */
\r
159 EA_MODE_PD, /* Address register indirect with predecrement */
\r
160 EA_MODE_PD7, /* Address register 7 indirect with predecrement */
\r
161 EA_MODE_DI, /* Address register indirect with displacement */
\r
162 EA_MODE_IX, /* Address register indirect with index */
\r
163 EA_MODE_AW, /* Absolute word */
\r
164 EA_MODE_AL, /* Absolute long */
\r
165 EA_MODE_PCDI, /* Program counter indirect with displacement */
\r
166 EA_MODE_PCIX, /* Program counter indirect with index */
\r
167 EA_MODE_I /* Immediate */
\r
171 /* Everything we need to know about an opcode */
\r
174 char name[MAX_NAME_LENGTH]; /* opcode handler name */
\r
175 unsigned char size; /* Size of operation */
\r
176 char spec_proc[MAX_SPEC_PROC_LENGTH]; /* Special processing mode */
\r
177 char spec_ea[MAX_SPEC_EA_LENGTH]; /* Specified effective addressing mode */
\r
178 unsigned char bits; /* Number of significant bits (used for sorting the table) */
\r
179 unsigned short op_mask; /* Mask to apply for matching an opcode to a handler */
\r
180 unsigned short op_match; /* Value to match after masking */
\r
181 char ea_allowed[EA_ALLOWED_LENGTH]; /* Effective addressing modes allowed */
\r
182 char cpu_mode[NUM_CPUS]; /* User or supervisor mode */
\r
183 char cpus[NUM_CPUS+1]; /* Allowed CPUs */
\r
184 unsigned char cycles[NUM_CPUS]; /* cycles for 000, 010, 020 */
\r
188 /* All modifications necessary for a specific EA mode of an instruction */
\r
191 const char* fname_add;
\r
192 const char* ea_add;
\r
193 unsigned int mask_add;
\r
194 unsigned int match_add;
\r
198 /* Holds the body of a function */
\r
201 char body[MAX_BODY_LENGTH][MAX_LINE_LENGTH+1];
\r
206 /* Holds a sequence of search / replace strings */
\r
209 char replace[MAX_REPLACE_LENGTH][2][MAX_LINE_LENGTH+1];
\r
214 /* Function Prototypes */
\r
215 void error_exit(const char* fmt, ...);
\r
216 void perror_exit(const char* fmt, ...);
\r
217 int check_strsncpy(char* dst, char* src, int maxlength);
\r
218 int check_atoi(char* str, int *result);
\r
219 int skip_spaces(char* str);
\r
220 int num_bits(int value);
\r
221 int atoh(char* buff);
\r
222 int fgetline(char* buff, int nchars, FILE* file);
\r
223 int get_oper_cycles(opcode_struct* op, int ea_mode, int cpu_type);
\r
224 opcode_struct* find_opcode(char* name, int size, char* spec_proc, char* spec_ea);
\r
225 opcode_struct* find_illegal_opcode(void);
\r
226 int extract_opcode_info(char* src, char* name, int* size, char* spec_proc, char* spec_ea);
\r
227 void add_replace_string(replace_struct* replace, const char* search_str, const char* replace_str);
\r
228 void write_body(FILE* filep, body_struct* body, replace_struct* replace);
\r
229 void get_base_name(char* base_name, opcode_struct* op);
\r
230 void write_prototype(FILE* filep, char* base_name);
\r
231 void write_function_name(FILE* filep, char* base_name);
\r
232 void add_opcode_output_table_entry(opcode_struct* op, char* name);
\r
233 static int DECL_SPEC compare_nof_true_bits(const void* aptr, const void* bptr);
\r
234 void print_opcode_output_table(FILE* filep);
\r
235 void write_table_entry(FILE* filep, opcode_struct* op);
\r
236 void set_opcode_struct(opcode_struct* src, opcode_struct* dst, int ea_mode);
\r
237 void generate_opcode_handler(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* opinfo, int ea_mode);
\r
238 void generate_opcode_ea_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op);
\r
239 void generate_opcode_cc_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op_in, int offset);
\r
240 void process_opcode_handlers(FILE* filep);
\r
241 void populate_table(void);
\r
242 void read_insert(char* insert);
\r
246 /* ======================================================================== */
\r
247 /* ================================= DATA ================================= */
\r
248 /* ======================================================================== */
\r
250 /* Name of the input file */
\r
251 char g_input_filename[M68K_MAX_PATH] = FILENAME_INPUT;
\r
254 FILE* g_input_file = NULL;
\r
255 FILE* g_prototype_file = NULL;
\r
256 FILE* g_table_file = NULL;
\r
258 int g_num_functions = 0; /* Number of functions processed */
\r
259 int g_num_primitives = 0; /* Number of function primitives read */
\r
260 int g_line_number = 1; /* Current line number */
\r
262 /* Opcode handler table */
\r
263 opcode_struct g_opcode_input_table[MAX_OPCODE_INPUT_TABLE_LENGTH];
\r
265 opcode_struct g_opcode_output_table[MAX_OPCODE_OUTPUT_TABLE_LENGTH];
\r
266 int g_opcode_output_table_length = 0;
\r
268 ea_info_struct g_ea_info_table[13] =
\r
269 {/* fname ea mask match */
\r
270 {"", "", 0x00, 0x00}, /* EA_MODE_NONE */
\r
271 {"ai", "AY_AI", 0x38, 0x10}, /* EA_MODE_AI */
\r
272 {"pi", "AY_PI", 0x38, 0x18}, /* EA_MODE_PI */
\r
273 {"pi7", "A7_PI", 0x3f, 0x1f}, /* EA_MODE_PI7 */
\r
274 {"pd", "AY_PD", 0x38, 0x20}, /* EA_MODE_PD */
\r
275 {"pd7", "A7_PD", 0x3f, 0x27}, /* EA_MODE_PD7 */
\r
276 {"di", "AY_DI", 0x38, 0x28}, /* EA_MODE_DI */
\r
277 {"ix", "AY_IX", 0x38, 0x30}, /* EA_MODE_IX */
\r
278 {"aw", "AW", 0x3f, 0x38}, /* EA_MODE_AW */
\r
279 {"al", "AL", 0x3f, 0x39}, /* EA_MODE_AL */
\r
280 {"pcdi", "PCDI", 0x3f, 0x3a}, /* EA_MODE_PCDI */
\r
281 {"pcix", "PCIX", 0x3f, 0x3b}, /* EA_MODE_PCIX */
\r
282 {"i", "I", 0x3f, 0x3c}, /* EA_MODE_I */
\r
286 const char* g_cc_table[16][2] =
\r
288 { "t", "T"}, /* 0000 */
\r
289 { "f", "F"}, /* 0001 */
\r
290 {"hi", "HI"}, /* 0010 */
\r
291 {"ls", "LS"}, /* 0011 */
\r
292 {"cc", "CC"}, /* 0100 */
\r
293 {"cs", "CS"}, /* 0101 */
\r
294 {"ne", "NE"}, /* 0110 */
\r
295 {"eq", "EQ"}, /* 0111 */
\r
296 {"vc", "VC"}, /* 1000 */
\r
297 {"vs", "VS"}, /* 1001 */
\r
298 {"pl", "PL"}, /* 1010 */
\r
299 {"mi", "MI"}, /* 1011 */
\r
300 {"ge", "GE"}, /* 1100 */
\r
301 {"lt", "LT"}, /* 1101 */
\r
302 {"gt", "GT"}, /* 1110 */
\r
303 {"le", "LE"}, /* 1111 */
\r
306 /* size to index translator (0 -> 0, 8 and 16 -> 1, 32 -> 2) */
\r
307 int g_size_select_table[33] =
\r
310 0, 0, 0, 0, 0, 0, 0, 1, /* 8 */
\r
311 0, 0, 0, 0, 0, 0, 0, 1, /* 16 */
\r
312 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 /* 32 */
\r
315 /* Extra cycles required for certain EA modes */
\r
316 /* TODO: correct timings for 040 */
\r
317 int g_ea_cycle_table[13][NUM_CPUS][3] =
\r
318 {/* 000 010 020 040 */
\r
319 {{ 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}}, /* EA_MODE_NONE */
\r
320 {{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_AI */
\r
321 {{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_PI */
\r
322 {{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_PI7 */
\r
323 {{ 0, 6, 10}, { 0, 6, 10}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_PD */
\r
324 {{ 0, 6, 10}, { 0, 6, 10}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_PD7 */
\r
325 {{ 0, 8, 12}, { 0, 8, 12}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_DI */
\r
326 {{ 0, 10, 14}, { 0, 10, 14}, { 0, 7, 7}, { 0, 7, 7}}, /* EA_MODE_IX */
\r
327 {{ 0, 8, 12}, { 0, 8, 12}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_AW */
\r
328 {{ 0, 12, 16}, { 0, 12, 16}, { 0, 4, 4}, { 0, 4, 4}}, /* EA_MODE_AL */
\r
329 {{ 0, 8, 12}, { 0, 8, 12}, { 0, 5, 5}, { 0, 5, 5}}, /* EA_MODE_PCDI */
\r
330 {{ 0, 10, 14}, { 0, 10, 14}, { 0, 7, 7}, { 0, 7, 7}}, /* EA_MODE_PCIX */
\r
331 {{ 0, 4, 8}, { 0, 4, 8}, { 0, 2, 4}, { 0, 2, 4}}, /* EA_MODE_I */
\r
334 /* Extra cycles for JMP instruction (000, 010) */
\r
335 int g_jmp_cycle_table[13] =
\r
337 0, /* EA_MODE_NONE */
\r
338 4, /* EA_MODE_AI */
\r
339 0, /* EA_MODE_PI */
\r
340 0, /* EA_MODE_PI7 */
\r
341 0, /* EA_MODE_PD */
\r
342 0, /* EA_MODE_PD7 */
\r
343 6, /* EA_MODE_DI */
\r
344 10, /* EA_MODE_IX */
\r
345 6, /* EA_MODE_AW */
\r
346 8, /* EA_MODE_AL */
\r
347 6, /* EA_MODE_PCDI */
\r
348 10, /* EA_MODE_PCIX */
\r
352 /* Extra cycles for JSR instruction (000, 010) */
\r
353 int g_jsr_cycle_table[13] =
\r
355 0, /* EA_MODE_NONE */
\r
356 4, /* EA_MODE_AI */
\r
357 0, /* EA_MODE_PI */
\r
358 0, /* EA_MODE_PI7 */
\r
359 0, /* EA_MODE_PD */
\r
360 0, /* EA_MODE_PD7 */
\r
361 6, /* EA_MODE_DI */
\r
362 10, /* EA_MODE_IX */
\r
363 6, /* EA_MODE_AW */
\r
364 8, /* EA_MODE_AL */
\r
365 6, /* EA_MODE_PCDI */
\r
366 10, /* EA_MODE_PCIX */
\r
370 /* Extra cycles for LEA instruction (000, 010) */
\r
371 int g_lea_cycle_table[13] =
\r
373 0, /* EA_MODE_NONE */
\r
374 4, /* EA_MODE_AI */
\r
375 0, /* EA_MODE_PI */
\r
376 0, /* EA_MODE_PI7 */
\r
377 0, /* EA_MODE_PD */
\r
378 0, /* EA_MODE_PD7 */
\r
379 8, /* EA_MODE_DI */
\r
380 12, /* EA_MODE_IX */
\r
381 8, /* EA_MODE_AW */
\r
382 12, /* EA_MODE_AL */
\r
383 8, /* EA_MODE_PCDI */
\r
384 12, /* EA_MODE_PCIX */
\r
388 /* Extra cycles for PEA instruction (000, 010) */
\r
389 int g_pea_cycle_table[13] =
\r
391 0, /* EA_MODE_NONE */
\r
392 6, /* EA_MODE_AI */
\r
393 0, /* EA_MODE_PI */
\r
394 0, /* EA_MODE_PI7 */
\r
395 0, /* EA_MODE_PD */
\r
396 0, /* EA_MODE_PD7 */
\r
397 10, /* EA_MODE_DI */
\r
398 14, /* EA_MODE_IX */
\r
399 10, /* EA_MODE_AW */
\r
400 14, /* EA_MODE_AL */
\r
401 10, /* EA_MODE_PCDI */
\r
402 14, /* EA_MODE_PCIX */
\r
406 /* Extra cycles for MOVEM instruction (000, 010) */
\r
407 int g_movem_cycle_table[13] =
\r
409 0, /* EA_MODE_NONE */
\r
410 0, /* EA_MODE_AI */
\r
411 0, /* EA_MODE_PI */
\r
412 0, /* EA_MODE_PI7 */
\r
413 0, /* EA_MODE_PD */
\r
414 0, /* EA_MODE_PD7 */
\r
415 4, /* EA_MODE_DI */
\r
416 6, /* EA_MODE_IX */
\r
417 4, /* EA_MODE_AW */
\r
418 8, /* EA_MODE_AL */
\r
419 0, /* EA_MODE_PCDI */
\r
420 0, /* EA_MODE_PCIX */
\r
424 /* Extra cycles for MOVES instruction (010) */
\r
425 int g_moves_cycle_table[13][3] =
\r
427 { 0, 0, 0}, /* EA_MODE_NONE */
\r
428 { 0, 4, 6}, /* EA_MODE_AI */
\r
429 { 0, 4, 6}, /* EA_MODE_PI */
\r
430 { 0, 4, 6}, /* EA_MODE_PI7 */
\r
431 { 0, 6, 12}, /* EA_MODE_PD */
\r
432 { 0, 6, 12}, /* EA_MODE_PD7 */
\r
433 { 0, 12, 16}, /* EA_MODE_DI */
\r
434 { 0, 16, 20}, /* EA_MODE_IX */
\r
435 { 0, 12, 16}, /* EA_MODE_AW */
\r
436 { 0, 16, 20}, /* EA_MODE_AL */
\r
437 { 0, 0, 0}, /* EA_MODE_PCDI */
\r
438 { 0, 0, 0}, /* EA_MODE_PCIX */
\r
439 { 0, 0, 0}, /* EA_MODE_I */
\r
442 /* Extra cycles for CLR instruction (010) */
\r
443 int g_clr_cycle_table[13][3] =
\r
445 { 0, 0, 0}, /* EA_MODE_NONE */
\r
446 { 0, 4, 6}, /* EA_MODE_AI */
\r
447 { 0, 4, 6}, /* EA_MODE_PI */
\r
448 { 0, 4, 6}, /* EA_MODE_PI7 */
\r
449 { 0, 6, 8}, /* EA_MODE_PD */
\r
450 { 0, 6, 8}, /* EA_MODE_PD7 */
\r
451 { 0, 8, 10}, /* EA_MODE_DI */
\r
452 { 0, 10, 14}, /* EA_MODE_IX */
\r
453 { 0, 8, 10}, /* EA_MODE_AW */
\r
454 { 0, 10, 14}, /* EA_MODE_AL */
\r
455 { 0, 0, 0}, /* EA_MODE_PCDI */
\r
456 { 0, 0, 0}, /* EA_MODE_PCIX */
\r
457 { 0, 0, 0}, /* EA_MODE_I */
\r
462 /* ======================================================================== */
\r
463 /* =========================== UTILITY FUNCTIONS ========================== */
\r
464 /* ======================================================================== */
\r
466 /* Print an error message and exit with status error */
\r
467 void error_exit(const char* fmt, ...)
\r
470 fprintf(stderr, "In %s, near or on line %d:\n\t", g_input_filename, g_line_number);
\r
471 va_start(args, fmt);
\r
472 vfprintf(stderr, fmt, args);
\r
474 fprintf(stderr, "\n");
\r
476 if(g_prototype_file) fclose(g_prototype_file);
\r
477 if(g_table_file) fclose(g_table_file);
\r
478 if(g_input_file) fclose(g_input_file);
\r
480 exit(EXIT_FAILURE);
\r
483 /* Print an error message, call perror(), and exit with status error */
\r
484 void perror_exit(const char* fmt, ...)
\r
487 va_start(args, fmt);
\r
488 vfprintf(stderr, fmt, args);
\r
492 if(g_prototype_file) fclose(g_prototype_file);
\r
493 if(g_table_file) fclose(g_table_file);
\r
494 if(g_input_file) fclose(g_input_file);
\r
496 exit(EXIT_FAILURE);
\r
500 /* copy until 0 or space and exit with error if we read too far */
\r
501 int check_strsncpy(char* dst, char* src, int maxlength)
\r
504 while(*src && *src != ' ')
\r
507 if(p - dst > maxlength)
\r
508 error_exit("Field too long");
\r
514 /* copy until 0 or specified character and exit with error if we read too far */
\r
515 int check_strcncpy(char* dst, char* src, char delim, int maxlength)
\r
518 while(*src && *src != delim)
\r
521 if(p - dst > maxlength)
\r
522 error_exit("Field too long");
\r
528 /* convert ascii to integer and exit with error if we find invalid data */
\r
529 int check_atoi(char* str, int *result)
\r
533 while(*p >= '0' && *p <= '9')
\r
536 accum += *p++ - '0';
\r
538 if(*p != ' ' && *p != 0)
\r
539 error_exit("Malformed integer value (%c)", *p);
\r
544 /* Skip past spaces in a string */
\r
545 int skip_spaces(char* str)
\r
555 /* Count the number of set bits in a value */
\r
556 int num_bits(int value)
\r
558 value = ((value & 0xaaaa) >> 1) + (value & 0x5555);
\r
559 value = ((value & 0xcccc) >> 2) + (value & 0x3333);
\r
560 value = ((value & 0xf0f0) >> 4) + (value & 0x0f0f);
\r
561 value = ((value & 0xff00) >> 8) + (value & 0x00ff);
\r
565 /* Convert a hex value written in ASCII */
\r
566 int atoh(char* buff)
\r
572 if(*buff >= '0' && *buff <= '9')
\r
575 accum += *buff - '0';
\r
577 else if(*buff >= 'a' && *buff <= 'f')
\r
580 accum += *buff - 'a' + 10;
\r
587 /* Get a line of text from a file, discarding any end-of-line characters */
\r
588 int fgetline(char* buff, int nchars, FILE* file)
\r
592 if(fgets(buff, nchars, file) == NULL)
\r
594 if(buff[0] == '\r')
\r
595 memcpy(buff, buff + 1, nchars - 1);
\r
597 length = strlen(buff);
\r
598 while(length && (buff[length-1] == '\r' || buff[length-1] == '\n'))
\r
608 /* ======================================================================== */
\r
609 /* =========================== HELPER FUNCTIONS =========================== */
\r
610 /* ======================================================================== */
\r
612 /* Calculate the number of cycles an opcode requires */
\r
613 int get_oper_cycles(opcode_struct* op, int ea_mode, int cpu_type)
\r
615 int size = g_size_select_table[op->size];
\r
617 if(op->cpus[cpu_type] == '.')
\r
620 if(cpu_type < CPU_TYPE_020)
\r
622 if(cpu_type == CPU_TYPE_010)
\r
624 if(strcmp(op->name, "moves") == 0)
\r
625 return op->cycles[cpu_type] + g_moves_cycle_table[ea_mode][size];
\r
626 if(strcmp(op->name, "clr") == 0)
\r
627 return op->cycles[cpu_type] + g_clr_cycle_table[ea_mode][size];
\r
630 /* ASG: added these cases -- immediate modes take 2 extra cycles here */
\r
631 /* SV: but only when operating on long, and also on register direct mode */
\r
632 if(cpu_type == CPU_TYPE_000 && (ea_mode == EA_MODE_I || ea_mode == EA_MODE_NONE) && op->size == 32 &&
\r
633 ((strcmp(op->name, "add") == 0 && strcmp(op->spec_proc, "er") == 0) ||
\r
634 strcmp(op->name, "adda") == 0 ||
\r
635 (strcmp(op->name, "and") == 0 && strcmp(op->spec_proc, "er") == 0) ||
\r
636 (strcmp(op->name, "or") == 0 && strcmp(op->spec_proc, "er") == 0) ||
\r
637 (strcmp(op->name, "sub") == 0 && strcmp(op->spec_proc, "er") == 0) ||
\r
638 strcmp(op->name, "suba") == 0))
\r
639 return op->cycles[cpu_type] + g_ea_cycle_table[ea_mode][cpu_type][size] + 2;
\r
641 if(strcmp(op->name, "jmp") == 0)
\r
642 return op->cycles[cpu_type] + g_jmp_cycle_table[ea_mode];
\r
643 if(strcmp(op->name, "jsr") == 0)
\r
644 return op->cycles[cpu_type] + g_jsr_cycle_table[ea_mode];
\r
645 if(strcmp(op->name, "lea") == 0)
\r
646 return op->cycles[cpu_type] + g_lea_cycle_table[ea_mode];
\r
647 if(strcmp(op->name, "pea") == 0)
\r
648 return op->cycles[cpu_type] + g_pea_cycle_table[ea_mode];
\r
649 if(strcmp(op->name, "movem") == 0)
\r
650 return op->cycles[cpu_type] + g_movem_cycle_table[ea_mode];
\r
652 return op->cycles[cpu_type] + g_ea_cycle_table[ea_mode][cpu_type][size];
\r
655 /* Find an opcode in the opcode handler list */
\r
656 opcode_struct* find_opcode(char* name, int size, char* spec_proc, char* spec_ea)
\r
661 for(op = g_opcode_input_table;op->name != NULL;op++)
\r
663 if( strcmp(name, op->name) == 0 &&
\r
664 (size == op->size) &&
\r
665 strcmp(spec_proc, op->spec_proc) == 0 &&
\r
666 strcmp(spec_ea, op->spec_ea) == 0)
\r
672 /* Specifically find the illegal opcode in the list */
\r
673 opcode_struct* find_illegal_opcode(void)
\r
677 for(op = g_opcode_input_table;op->name != NULL;op++)
\r
679 if(strcmp(op->name, "illegal") == 0)
\r
685 /* Parse an opcode handler name */
\r
686 int extract_opcode_info(char* src, char* name, int* size, char* spec_proc, char* spec_ea)
\r
688 char* ptr = strstr(src, ID_OPHANDLER_NAME);
\r
693 ptr += strlen(ID_OPHANDLER_NAME) + 1;
\r
695 ptr += check_strcncpy(name, ptr, ',', MAX_NAME_LENGTH);
\r
696 if(*ptr != ',') return 0;
\r
698 ptr += skip_spaces(ptr);
\r
701 ptr = strstr(ptr, ",");
\r
702 if(ptr == NULL) return 0;
\r
704 ptr += skip_spaces(ptr);
\r
706 ptr += check_strcncpy(spec_proc, ptr, ',', MAX_SPEC_PROC_LENGTH);
\r
707 if(*ptr != ',') return 0;
\r
709 ptr += skip_spaces(ptr);
\r
711 ptr += check_strcncpy(spec_ea, ptr, ')', MAX_SPEC_EA_LENGTH);
\r
712 if(*ptr != ')') return 0;
\r
714 ptr += skip_spaces(ptr);
\r
720 /* Add a search/replace pair to a replace structure */
\r
721 void add_replace_string(replace_struct* replace, const char* search_str, const char* replace_str)
\r
723 if(replace->length >= MAX_REPLACE_LENGTH)
\r
724 error_exit("overflow in replace structure");
\r
726 strcpy(replace->replace[replace->length][0], search_str);
\r
727 strcpy(replace->replace[replace->length++][1], replace_str);
\r
730 /* Write a function body while replacing any selected strings */
\r
731 void write_body(FILE* filep, body_struct* body, replace_struct* replace)
\r
736 char output[MAX_LINE_LENGTH+1];
\r
737 char temp_buff[MAX_LINE_LENGTH+1];
\r
740 for(i=0;i<body->length;i++)
\r
742 strcpy(output, body->body[i]);
\r
743 /* Check for the base directive header */
\r
744 if(strstr(output, ID_BASE) != NULL)
\r
746 /* Search for any text we need to replace */
\r
748 for(j=0;j<replace->length;j++)
\r
750 ptr = strstr(output, replace->replace[j][0]);
\r
753 /* We found something to replace */
\r
755 strcpy(temp_buff, ptr+strlen(replace->replace[j][0]));
\r
756 strcpy(ptr, replace->replace[j][1]);
\r
757 strcat(ptr, temp_buff);
\r
760 /* Found a directive with no matching replace string */
\r
762 error_exit("Unknown " ID_BASE " directive");
\r
764 fprintf(filep, "%s\n", output);
\r
766 fprintf(filep, "\n\n");
\r
769 /* Generate a base function name from an opcode struct */
\r
770 void get_base_name(char* base_name, opcode_struct* op)
\r
772 sprintf(base_name, "m68k_op_%s", op->name);
\r
774 sprintf(base_name+strlen(base_name), "_%d", op->size);
\r
775 if(strcmp(op->spec_proc, UNSPECIFIED) != 0)
\r
776 sprintf(base_name+strlen(base_name), "_%s", op->spec_proc);
\r
777 if(strcmp(op->spec_ea, UNSPECIFIED) != 0)
\r
778 sprintf(base_name+strlen(base_name), "_%s", op->spec_ea);
\r
781 /* Write the prototype of an opcode handler function */
\r
782 void write_prototype(FILE* filep, char* base_name)
\r
784 fprintf(filep, "void %s(void);\n", base_name);
\r
787 /* Write the name of an opcode handler function */
\r
788 void write_function_name(FILE* filep, char* base_name)
\r
790 fprintf(filep, "void %s(void)\n", base_name);
\r
793 void add_opcode_output_table_entry(opcode_struct* op, char* name)
\r
795 opcode_struct* ptr;
\r
796 if(g_opcode_output_table_length > MAX_OPCODE_OUTPUT_TABLE_LENGTH)
\r
797 error_exit("Opcode output table overflow");
\r
799 ptr = g_opcode_output_table + g_opcode_output_table_length++;
\r
802 strcpy(ptr->name, name);
\r
803 ptr->bits = num_bits(ptr->op_mask);
\r
807 * Comparison function for qsort()
\r
808 * For entries with an equal number of set bits in
\r
809 * the mask compare the match values
\r
811 static int DECL_SPEC compare_nof_true_bits(const void* aptr, const void* bptr)
\r
813 const opcode_struct *a = aptr, *b = bptr;
\r
814 if(a->bits != b->bits)
\r
815 return a->bits - b->bits;
\r
816 if(a->op_mask != b->op_mask)
\r
817 return a->op_mask - b->op_mask;
\r
818 return a->op_match - b->op_match;
\r
821 void print_opcode_output_table(FILE* filep)
\r
824 qsort((void *)g_opcode_output_table, g_opcode_output_table_length, sizeof(g_opcode_output_table[0]), compare_nof_true_bits);
\r
826 for(i=0;i<g_opcode_output_table_length;i++)
\r
827 write_table_entry(filep, g_opcode_output_table+i);
\r
830 /* Write an entry in the opcode handler table */
\r
831 void write_table_entry(FILE* filep, opcode_struct* op)
\r
835 fprintf(filep, "\t{%-28s, 0x%04x, 0x%04x, {",
\r
836 op->name, op->op_mask, op->op_match);
\r
838 for(i=0;i<NUM_CPUS;i++)
\r
840 fprintf(filep, "%3d", op->cycles[i]);
\r
842 fprintf(filep, ", ");
\r
845 fprintf(filep, "}},\n");
\r
848 /* Fill out an opcode struct with a specific addressing mode of the source opcode struct */
\r
849 void set_opcode_struct(opcode_struct* src, opcode_struct* dst, int ea_mode)
\r
855 for(i=0;i<NUM_CPUS;i++)
\r
856 dst->cycles[i] = get_oper_cycles(dst, ea_mode, i);
\r
857 if(strcmp(dst->spec_ea, UNSPECIFIED) == 0 && ea_mode != EA_MODE_NONE)
\r
858 sprintf(dst->spec_ea, "%s", g_ea_info_table[ea_mode].fname_add);
\r
859 dst->op_mask |= g_ea_info_table[ea_mode].mask_add;
\r
860 dst->op_match |= g_ea_info_table[ea_mode].match_add;
\r
864 /* Generate a final opcode handler from the provided data */
\r
865 void generate_opcode_handler(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* opinfo, int ea_mode)
\r
867 char str[MAX_LINE_LENGTH+1];
\r
868 opcode_struct* op = malloc(sizeof(opcode_struct));
\r
870 /* Set the opcode structure and write the tables, prototypes, etc */
\r
871 set_opcode_struct(opinfo, op, ea_mode);
\r
872 get_base_name(str, op);
\r
873 write_prototype(g_prototype_file, str);
\r
874 add_opcode_output_table_entry(op, str);
\r
875 write_function_name(filep, str);
\r
877 /* Add any replace strings needed */
\r
878 if(ea_mode != EA_MODE_NONE)
\r
880 sprintf(str, "EA_%s_8()", g_ea_info_table[ea_mode].ea_add);
\r
881 add_replace_string(replace, ID_OPHANDLER_EA_AY_8, str);
\r
882 sprintf(str, "EA_%s_16()", g_ea_info_table[ea_mode].ea_add);
\r
883 add_replace_string(replace, ID_OPHANDLER_EA_AY_16, str);
\r
884 sprintf(str, "EA_%s_32()", g_ea_info_table[ea_mode].ea_add);
\r
885 add_replace_string(replace, ID_OPHANDLER_EA_AY_32, str);
\r
886 sprintf(str, "OPER_%s_8()", g_ea_info_table[ea_mode].ea_add);
\r
887 add_replace_string(replace, ID_OPHANDLER_OPER_AY_8, str);
\r
888 sprintf(str, "OPER_%s_16()", g_ea_info_table[ea_mode].ea_add);
\r
889 add_replace_string(replace, ID_OPHANDLER_OPER_AY_16, str);
\r
890 sprintf(str, "OPER_%s_32()", g_ea_info_table[ea_mode].ea_add);
\r
891 add_replace_string(replace, ID_OPHANDLER_OPER_AY_32, str);
\r
894 /* Now write the function body with the selected replace strings */
\r
895 write_body(filep, body, replace);
\r
900 /* Generate opcode variants based on available addressing modes */
\r
901 void generate_opcode_ea_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op)
\r
903 int old_length = replace->length;
\r
905 /* No ea modes available for this opcode */
\r
906 if(HAS_NO_EA_MODE(op->ea_allowed))
\r
908 generate_opcode_handler(filep, body, replace, op, EA_MODE_NONE);
\r
912 /* Check for and create specific opcodes for each available addressing mode */
\r
913 if(HAS_EA_AI(op->ea_allowed))
\r
914 generate_opcode_handler(filep, body, replace, op, EA_MODE_AI);
\r
915 replace->length = old_length;
\r
916 if(HAS_EA_PI(op->ea_allowed))
\r
918 generate_opcode_handler(filep, body, replace, op, EA_MODE_PI);
\r
919 replace->length = old_length;
\r
921 generate_opcode_handler(filep, body, replace, op, EA_MODE_PI7);
\r
923 replace->length = old_length;
\r
924 if(HAS_EA_PD(op->ea_allowed))
\r
926 generate_opcode_handler(filep, body, replace, op, EA_MODE_PD);
\r
927 replace->length = old_length;
\r
929 generate_opcode_handler(filep, body, replace, op, EA_MODE_PD7);
\r
931 replace->length = old_length;
\r
932 if(HAS_EA_DI(op->ea_allowed))
\r
933 generate_opcode_handler(filep, body, replace, op, EA_MODE_DI);
\r
934 replace->length = old_length;
\r
935 if(HAS_EA_IX(op->ea_allowed))
\r
936 generate_opcode_handler(filep, body, replace, op, EA_MODE_IX);
\r
937 replace->length = old_length;
\r
938 if(HAS_EA_AW(op->ea_allowed))
\r
939 generate_opcode_handler(filep, body, replace, op, EA_MODE_AW);
\r
940 replace->length = old_length;
\r
941 if(HAS_EA_AL(op->ea_allowed))
\r
942 generate_opcode_handler(filep, body, replace, op, EA_MODE_AL);
\r
943 replace->length = old_length;
\r
944 if(HAS_EA_PCDI(op->ea_allowed))
\r
945 generate_opcode_handler(filep, body, replace, op, EA_MODE_PCDI);
\r
946 replace->length = old_length;
\r
947 if(HAS_EA_PCIX(op->ea_allowed))
\r
948 generate_opcode_handler(filep, body, replace, op, EA_MODE_PCIX);
\r
949 replace->length = old_length;
\r
950 if(HAS_EA_I(op->ea_allowed))
\r
951 generate_opcode_handler(filep, body, replace, op, EA_MODE_I);
\r
952 replace->length = old_length;
\r
955 /* Generate variants of condition code opcodes */
\r
956 void generate_opcode_cc_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op_in, int offset)
\r
961 int old_length = replace->length;
\r
962 opcode_struct* op = malloc(sizeof(opcode_struct));
\r
966 op->op_mask |= 0x0f00;
\r
968 /* Do all condition codes except t and f */
\r
971 /* Add replace strings for this condition code */
\r
972 sprintf(repl, "COND_%s()", g_cc_table[i][1]);
\r
973 sprintf(replnot, "COND_NOT_%s()", g_cc_table[i][1]);
\r
975 add_replace_string(replace, ID_OPHANDLER_CC, repl);
\r
976 add_replace_string(replace, ID_OPHANDLER_NOT_CC, replnot);
\r
978 /* Set the new opcode info */
\r
979 strcpy(op->name+offset, g_cc_table[i][0]);
\r
981 op->op_match = (op->op_match & 0xf0ff) | (i<<8);
\r
983 /* Generate all opcode variants for this modified opcode */
\r
984 generate_opcode_ea_variants(filep, body, replace, op);
\r
985 /* Remove the above replace strings */
\r
986 replace->length = old_length;
\r
991 /* Process the opcode handlers section of the input file */
\r
992 void process_opcode_handlers(FILE* filep)
\r
994 FILE* input_file = g_input_file;
\r
995 char func_name[MAX_LINE_LENGTH+1];
\r
996 char oper_name[MAX_LINE_LENGTH+1];
\r
998 char oper_spec_proc[MAX_LINE_LENGTH+1];
\r
999 char oper_spec_ea[MAX_LINE_LENGTH+1];
\r
1000 opcode_struct* opinfo;
\r
1001 replace_struct* replace = malloc(sizeof(replace_struct));
\r
1002 body_struct* body = malloc(sizeof(body_struct));
\r
1006 /* Find the first line of the function */
\r
1008 while(strstr(func_name, ID_OPHANDLER_NAME) == NULL)
\r
1010 if(strcmp(func_name, ID_INPUT_SEPARATOR) == 0)
\r
1014 return; /* all done */
\r
1016 if(fgetline(func_name, MAX_LINE_LENGTH, input_file) < 0)
\r
1017 error_exit("Premature end of file when getting function name");
\r
1019 /* Get the rest of the function */
\r
1020 for(body->length=0;;body->length++)
\r
1022 if(body->length > MAX_BODY_LENGTH)
\r
1023 error_exit("Function too long");
\r
1025 if(fgetline(body->body[body->length], MAX_LINE_LENGTH, input_file) < 0)
\r
1026 error_exit("Premature end of file when getting function body");
\r
1028 if(body->body[body->length][0] == '}')
\r
1035 g_num_primitives++;
\r
1037 /* Extract the function name information */
\r
1038 if(!extract_opcode_info(func_name, oper_name, &oper_size, oper_spec_proc, oper_spec_ea))
\r
1039 error_exit("Invalid " ID_OPHANDLER_NAME " format");
\r
1041 /* Find the corresponding table entry */
\r
1042 opinfo = find_opcode(oper_name, oper_size, oper_spec_proc, oper_spec_ea);
\r
1043 if(opinfo == NULL)
\r
1044 error_exit("Unable to find matching table entry for %s", func_name);
\r
1046 replace->length = 0;
\r
1048 /* Generate opcode variants */
\r
1049 if(strcmp(opinfo->name, "bcc") == 0 || strcmp(opinfo->name, "scc") == 0)
\r
1050 generate_opcode_cc_variants(filep, body, replace, opinfo, 1);
\r
1051 else if(strcmp(opinfo->name, "dbcc") == 0)
\r
1052 generate_opcode_cc_variants(filep, body, replace, opinfo, 2);
\r
1053 else if(strcmp(opinfo->name, "trapcc") == 0)
\r
1054 generate_opcode_cc_variants(filep, body, replace, opinfo, 4);
\r
1056 generate_opcode_ea_variants(filep, body, replace, opinfo);
\r
1064 /* Populate the opcode handler table from the input file */
\r
1065 void populate_table(void)
\r
1068 char bitpattern[17];
\r
1069 opcode_struct* op;
\r
1070 char buff[MAX_LINE_LENGTH];
\r
1076 /* Find the start of the table */
\r
1077 while(strcmp(buff, ID_TABLE_START) != 0)
\r
1078 if(fgetline(buff, MAX_LINE_LENGTH, g_input_file) < 0)
\r
1079 error_exit("Premature EOF while reading table");
\r
1081 /* Process the entire table */
\r
1082 for(op = g_opcode_input_table;;op++)
\r
1084 if(fgetline(buff, MAX_LINE_LENGTH, g_input_file) < 0)
\r
1085 error_exit("Premature EOF while reading table");
\r
1086 if(strlen(buff) == 0)
\r
1088 /* We finish when we find an input separator */
\r
1089 if(strcmp(buff, ID_INPUT_SEPARATOR) == 0)
\r
1092 /* Extract the info from the table */
\r
1096 ptr += skip_spaces(ptr);
\r
1097 ptr += check_strsncpy(op->name, ptr, MAX_NAME_LENGTH);
\r
1100 ptr += skip_spaces(ptr);
\r
1101 ptr += check_atoi(ptr, &temp);
\r
1102 op->size = (unsigned char)temp;
\r
1104 /* Special processing */
\r
1105 ptr += skip_spaces(ptr);
\r
1106 ptr += check_strsncpy(op->spec_proc, ptr, MAX_SPEC_PROC_LENGTH);
\r
1108 /* Specified EA Mode */
\r
1109 ptr += skip_spaces(ptr);
\r
1110 ptr += check_strsncpy(op->spec_ea, ptr, MAX_SPEC_EA_LENGTH);
\r
1112 /* Bit Pattern (more processing later) */
\r
1113 ptr += skip_spaces(ptr);
\r
1114 ptr += check_strsncpy(bitpattern, ptr, 17);
\r
1116 /* Allowed Addressing Mode List */
\r
1117 ptr += skip_spaces(ptr);
\r
1118 ptr += check_strsncpy(op->ea_allowed, ptr, EA_ALLOWED_LENGTH);
\r
1120 /* CPU operating mode (U = user or supervisor, S = supervisor only */
\r
1121 ptr += skip_spaces(ptr);
\r
1122 for(i=0;i<NUM_CPUS;i++)
\r
1124 op->cpu_mode[i] = *ptr++;
\r
1125 ptr += skip_spaces(ptr);
\r
1128 /* Allowed CPUs for this instruction */
\r
1129 for(i=0;i<NUM_CPUS;i++)
\r
1131 ptr += skip_spaces(ptr);
\r
1132 if(*ptr == UNSPECIFIED_CH)
\r
1134 op->cpus[i] = UNSPECIFIED_CH;
\r
1135 op->cycles[i] = 0;
\r
1140 op->cpus[i] = '0' + i;
\r
1141 ptr += check_atoi(ptr, &temp);
\r
1142 op->cycles[i] = (unsigned char)temp;
\r
1146 /* generate mask and match from bitpattern */
\r
1151 op->op_mask |= (bitpattern[i] != '.') << (15-i);
\r
1152 op->op_match |= (bitpattern[i] == '1') << (15-i);
\r
1155 /* Terminate the list */
\r
1159 /* Read a header or footer insert from the input file */
\r
1160 void read_insert(char* insert)
\r
1162 char* ptr = insert;
\r
1163 char* overflow = insert + MAX_INSERT_LENGTH - MAX_LINE_LENGTH;
\r
1165 char* first_blank = NULL;
\r
1167 first_blank = NULL;
\r
1169 /* Skip any leading blank lines */
\r
1170 for(length = 0;length == 0;length = fgetline(ptr, MAX_LINE_LENGTH, g_input_file))
\r
1171 if(ptr >= overflow)
\r
1172 error_exit("Buffer overflow reading inserts");
\r
1174 error_exit("Premature EOF while reading inserts");
\r
1176 /* Advance and append newline */
\r
1178 strcpy(ptr++, "\n");
\r
1180 /* Read until next separator */
\r
1183 /* Read a new line */
\r
1184 if(ptr >= overflow)
\r
1185 error_exit("Buffer overflow reading inserts");
\r
1186 if((length = fgetline(ptr, MAX_LINE_LENGTH, g_input_file)) < 0)
\r
1187 error_exit("Premature EOF while reading inserts");
\r
1189 /* Stop if we read a separator */
\r
1190 if(strcmp(ptr, ID_INPUT_SEPARATOR) == 0)
\r
1193 /* keep track in case there are trailing blanks */
\r
1196 if(first_blank == NULL)
\r
1197 first_blank = ptr;
\r
1200 first_blank = NULL;
\r
1202 /* Advance and append newline */
\r
1204 strcpy(ptr++, "\n");
\r
1207 /* kill any trailing blank lines */
\r
1209 ptr = first_blank;
\r
1215 /* ======================================================================== */
\r
1216 /* ============================= MAIN FUNCTION ============================ */
\r
1217 /* ======================================================================== */
\r
1219 int main(int argc, char **argv)
\r
1222 char output_path[M68K_MAX_DIR] = "";
\r
1223 char filename[M68K_MAX_PATH];
\r
1224 /* Section identifier */
\r
1225 char section_id[MAX_LINE_LENGTH+1];
\r
1227 char temp_insert[MAX_INSERT_LENGTH+1];
\r
1228 char prototype_footer_insert[MAX_INSERT_LENGTH+1];
\r
1229 char table_header_insert[MAX_INSERT_LENGTH+1];
\r
1230 char table_footer_insert[MAX_INSERT_LENGTH+1];
\r
1231 char ophandler_header_insert[MAX_INSERT_LENGTH+1];
\r
1232 char ophandler_footer_insert[MAX_INSERT_LENGTH+1];
\r
1233 /* Flags if we've processed certain parts already */
\r
1234 int prototype_header_read = 0;
\r
1235 int prototype_footer_read = 0;
\r
1236 int table_header_read = 0;
\r
1237 int table_footer_read = 0;
\r
1238 int ophandler_header_read = 0;
\r
1239 int ophandler_footer_read = 0;
\r
1240 int table_body_read = 0;
\r
1241 int ophandler_body_read = 0;
\r
1243 printf("\n\tMusashi v%s 68000, 68008, 68010, 68EC020, 68020, 68040 emulator\n", g_version);
\r
1244 printf("\tCopyright 1998-2007 Karl Stenerud (karl@mame.net)\n\n");
\r
1246 /* Check if output path and source for the input file are given */
\r
1250 strcpy(output_path, argv[1]);
\r
1252 for(ptr = strchr(output_path, '\\'); ptr; ptr = strchr(ptr, '\\'))
\r
1255 #if !(defined(__DECC) && defined(VMS))
\r
1256 if(output_path[strlen(output_path)-1] != '/')
\r
1257 strcat(output_path, "/");
\r
1261 strcpy(g_input_filename, argv[2]);
\r
1265 #if defined(__DECC) && defined(VMS)
\r
1267 /* Open the files we need */
\r
1268 sprintf(filename, "%s%s", output_path, FILENAME_PROTOTYPE);
\r
1269 if((g_prototype_file = fopen(filename, "w")) == NULL)
\r
1270 perror_exit("Unable to create prototype file (%s)\n", filename);
\r
1272 sprintf(filename, "%s%s", output_path, FILENAME_TABLE);
\r
1273 if((g_table_file = fopen(filename, "w")) == NULL)
\r
1274 perror_exit("Unable to create table file (%s)\n", filename);
\r
1276 if((g_input_file=fopen(g_input_filename, "r")) == NULL)
\r
1277 perror_exit("can't open %s for input", g_input_filename);
\r
1282 /* Open the files we need */
\r
1283 sprintf(filename, "%s%s", output_path, FILENAME_PROTOTYPE);
\r
1284 if((g_prototype_file = fopen(filename, "wt")) == NULL)
\r
1285 perror_exit("Unable to create prototype file (%s)\n", filename);
\r
1287 sprintf(filename, "%s%s", output_path, FILENAME_TABLE);
\r
1288 if((g_table_file = fopen(filename, "wt")) == NULL)
\r
1289 perror_exit("Unable to create table file (%s)\n", filename);
\r
1291 if((g_input_file=fopen(g_input_filename, "rt")) == NULL)
\r
1292 perror_exit("can't open %s for input", g_input_filename);
\r
1296 /* Get to the first section of the input file */
\r
1297 section_id[0] = 0;
\r
1298 while(strcmp(section_id, ID_INPUT_SEPARATOR) != 0)
\r
1299 if(fgetline(section_id, MAX_LINE_LENGTH, g_input_file) < 0)
\r
1300 error_exit("Premature EOF while reading input file");
\r
1302 /* Now process all sections */
\r
1305 if(fgetline(section_id, MAX_LINE_LENGTH, g_input_file) < 0)
\r
1306 error_exit("Premature EOF while reading input file");
\r
1307 if(strcmp(section_id, ID_PROTOTYPE_HEADER) == 0)
\r
1309 if(prototype_header_read)
\r
1310 error_exit("Duplicate prototype header");
\r
1311 read_insert(temp_insert);
\r
1312 fprintf(g_prototype_file, "%s\n\n", temp_insert);
\r
1313 prototype_header_read = 1;
\r
1315 else if(strcmp(section_id, ID_TABLE_HEADER) == 0)
\r
1317 if(table_header_read)
\r
1318 error_exit("Duplicate table header");
\r
1319 read_insert(table_header_insert);
\r
1320 table_header_read = 1;
\r
1322 else if(strcmp(section_id, ID_OPHANDLER_HEADER) == 0)
\r
1324 if(ophandler_header_read)
\r
1325 error_exit("Duplicate opcode handler header");
\r
1326 read_insert(ophandler_header_insert);
\r
1327 ophandler_header_read = 1;
\r
1329 else if(strcmp(section_id, ID_PROTOTYPE_FOOTER) == 0)
\r
1331 if(prototype_footer_read)
\r
1332 error_exit("Duplicate prototype footer");
\r
1333 read_insert(prototype_footer_insert);
\r
1334 prototype_footer_read = 1;
\r
1336 else if(strcmp(section_id, ID_TABLE_FOOTER) == 0)
\r
1338 if(table_footer_read)
\r
1339 error_exit("Duplicate table footer");
\r
1340 read_insert(table_footer_insert);
\r
1341 table_footer_read = 1;
\r
1343 else if(strcmp(section_id, ID_OPHANDLER_FOOTER) == 0)
\r
1345 if(ophandler_footer_read)
\r
1346 error_exit("Duplicate opcode handler footer");
\r
1347 read_insert(ophandler_footer_insert);
\r
1348 ophandler_footer_read = 1;
\r
1350 else if(strcmp(section_id, ID_TABLE_BODY) == 0)
\r
1352 if(!prototype_header_read)
\r
1353 error_exit("Table body encountered before prototype header");
\r
1354 if(!table_header_read)
\r
1355 error_exit("Table body encountered before table header");
\r
1356 if(!ophandler_header_read)
\r
1357 error_exit("Table body encountered before opcode handler header");
\r
1359 if(table_body_read)
\r
1360 error_exit("Duplicate table body");
\r
1363 table_body_read = 1;
\r
1365 else if(strcmp(section_id, ID_OPHANDLER_BODY) == 0)
\r
1367 if(!prototype_header_read)
\r
1368 error_exit("Opcode handlers encountered before prototype header");
\r
1369 if(!table_header_read)
\r
1370 error_exit("Opcode handlers encountered before table header");
\r
1371 if(!ophandler_header_read)
\r
1372 error_exit("Opcode handlers encountered before opcode handler header");
\r
1373 if(!table_body_read)
\r
1374 error_exit("Opcode handlers encountered before table body");
\r
1376 if(ophandler_body_read)
\r
1377 error_exit("Duplicate opcode handler section");
\r
1379 fprintf(g_table_file, "%s\n\n", ophandler_header_insert);
\r
1380 process_opcode_handlers(g_table_file);
\r
1381 fprintf(g_table_file, "%s\n\n", ophandler_footer_insert);
\r
1383 ophandler_body_read = 1;
\r
1385 else if(strcmp(section_id, ID_END) == 0)
\r
1387 /* End of input file. Do a sanity check and then write footers */
\r
1388 if(!prototype_header_read)
\r
1389 error_exit("Missing prototype header");
\r
1390 if(!prototype_footer_read)
\r
1391 error_exit("Missing prototype footer");
\r
1392 if(!table_header_read)
\r
1393 error_exit("Missing table header");
\r
1394 if(!table_footer_read)
\r
1395 error_exit("Missing table footer");
\r
1396 if(!table_body_read)
\r
1397 error_exit("Missing table body");
\r
1398 if(!ophandler_header_read)
\r
1399 error_exit("Missing opcode handler header");
\r
1400 if(!ophandler_footer_read)
\r
1401 error_exit("Missing opcode handler footer");
\r
1402 if(!ophandler_body_read)
\r
1403 error_exit("Missing opcode handler body");
\r
1405 fprintf(g_table_file, "%s\n\n", table_header_insert);
\r
1406 print_opcode_output_table(g_table_file);
\r
1407 fprintf(g_table_file, "%s\n\n", table_footer_insert);
\r
1409 fprintf(g_prototype_file, "%s\n\n", prototype_footer_insert);
\r
1415 error_exit("Unknown section identifier: %s", section_id);
\r
1419 /* Close all files and exit */
\r
1420 fclose(g_prototype_file);
\r
1421 fclose(g_table_file);
\r
1422 fclose(g_input_file);
\r
1424 printf("Generated %d opcode handlers from %d primitives\n", g_num_functions, g_num_primitives);
\r
1431 /* ======================================================================== */
\r
1432 /* ============================== END OF FILE ============================= */
\r
1433 /* ======================================================================== */
\r