1.1 diff -r 000000000000 -r 8bf1bf91a36d src/musashi/m68kmake.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/src/musashi/m68kmake.c Sat Nov 27 01:13:12 2010 +0000 1.4 @@ -0,0 +1,1414 @@ 1.5 +/* ======================================================================== */ 1.6 +/* ========================= LICENSING & COPYRIGHT ======================== */ 1.7 +/* ======================================================================== */ 1.8 +/* 1.9 + * MUSASHI 1.10 + * Version 3.3 1.11 + * 1.12 + * A portable Motorola M680x0 processor emulation engine. 1.13 + * Copyright 1998-2001 Karl Stenerud. All rights reserved. 1.14 + * 1.15 + * This code may be freely used for non-commercial purposes as long as this 1.16 + * copyright notice remains unaltered in the source code and any binary files 1.17 + * containing this code in compiled form. 1.18 + * 1.19 + * All other lisencing terms must be negotiated with the author 1.20 + * (Karl Stenerud). 1.21 + * 1.22 + * The latest version of this code can be obtained at: 1.23 + * http://kstenerud.cjb.net 1.24 + */ 1.25 + 1.26 + 1.27 + 1.28 +/* ======================================================================== */ 1.29 +/* ============================ CODE GENERATOR ============================ */ 1.30 +/* ======================================================================== */ 1.31 +/* 1.32 + * This is the code generator program which will generate the opcode table 1.33 + * and the final opcode handlers. 1.34 + * 1.35 + * It requires an input file to function (default m68k_in.c), but you can 1.36 + * specify your own like so: 1.37 + * 1.38 + * m68kmake <output path> <input file> 1.39 + * 1.40 + * where output path is the path where the output files should be placed, and 1.41 + * input file is the file to use for input. 1.42 + * 1.43 + * If you modify the input file greatly from its released form, you may have 1.44 + * to tweak the configuration section a bit since I'm using static allocation 1.45 + * to keep things simple. 1.46 + * 1.47 + * 1.48 + * TODO: - build a better code generator for the move instruction. 1.49 + * - Add callm and rtm instructions 1.50 + * - Fix RTE to handle other format words 1.51 + * - Add address error (and bus error?) handling 1.52 + */ 1.53 + 1.54 + 1.55 +char* g_version = "3.3"; 1.56 + 1.57 +/* ======================================================================== */ 1.58 +/* =============================== INCLUDES =============================== */ 1.59 +/* ======================================================================== */ 1.60 + 1.61 +#include <stdio.h> 1.62 +#include <stdlib.h> 1.63 +#include <string.h> 1.64 +#include <ctype.h> 1.65 +#include <stdarg.h> 1.66 + 1.67 + 1.68 + 1.69 +/* ======================================================================== */ 1.70 +/* ============================= CONFIGURATION ============================ */ 1.71 +/* ======================================================================== */ 1.72 + 1.73 +#define MAX_PATH 1024 1.74 +#define MAX_DIR 1024 1.75 + 1.76 +#define NUM_CPUS 3 /* 000, 010, 020 */ 1.77 +#define MAX_LINE_LENGTH 200 /* length of 1 line */ 1.78 +#define MAX_BODY_LENGTH 300 /* Number of lines in 1 function */ 1.79 +#define MAX_REPLACE_LENGTH 30 /* Max number of replace strings */ 1.80 +#define MAX_INSERT_LENGTH 5000 /* Max size of insert piece */ 1.81 +#define MAX_NAME_LENGTH 30 /* Max length of ophandler name */ 1.82 +#define MAX_SPEC_PROC_LENGTH 4 /* Max length of special processing str */ 1.83 +#define MAX_SPEC_EA_LENGTH 5 /* Max length of specified EA str */ 1.84 +#define EA_ALLOWED_LENGTH 11 /* Max length of ea allowed str */ 1.85 +#define MAX_OPCODE_INPUT_TABLE_LENGTH 1000 /* Max length of opcode handler tbl */ 1.86 +#define MAX_OPCODE_OUTPUT_TABLE_LENGTH 3000 /* Max length of opcode handler tbl */ 1.87 + 1.88 +/* Default filenames */ 1.89 +#define FILENAME_INPUT "m68k_in.c" 1.90 +#define FILENAME_PROTOTYPE "m68kops.h" 1.91 +#define FILENAME_TABLE "m68kops.c" 1.92 +#define FILENAME_OPS_AC "m68kopac.c" 1.93 +#define FILENAME_OPS_DM "m68kopdm.c" 1.94 +#define FILENAME_OPS_NZ "m68kopnz.c" 1.95 + 1.96 + 1.97 +/* Identifier sequences recognized by this program */ 1.98 + 1.99 +#define ID_INPUT_SEPARATOR "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" 1.100 + 1.101 +#define ID_BASE "M68KMAKE" 1.102 +#define ID_PROTOTYPE_HEADER ID_BASE "_PROTOTYPE_HEADER" 1.103 +#define ID_PROTOTYPE_FOOTER ID_BASE "_PROTOTYPE_FOOTER" 1.104 +#define ID_TABLE_HEADER ID_BASE "_TABLE_HEADER" 1.105 +#define ID_TABLE_FOOTER ID_BASE "_TABLE_FOOTER" 1.106 +#define ID_TABLE_BODY ID_BASE "_TABLE_BODY" 1.107 +#define ID_TABLE_START ID_BASE "_TABLE_START" 1.108 +#define ID_OPHANDLER_HEADER ID_BASE "_OPCODE_HANDLER_HEADER" 1.109 +#define ID_OPHANDLER_FOOTER ID_BASE "_OPCODE_HANDLER_FOOTER" 1.110 +#define ID_OPHANDLER_BODY ID_BASE "_OPCODE_HANDLER_BODY" 1.111 +#define ID_END ID_BASE "_END" 1.112 + 1.113 +#define ID_OPHANDLER_NAME ID_BASE "_OP" 1.114 +#define ID_OPHANDLER_EA_AY_8 ID_BASE "_GET_EA_AY_8" 1.115 +#define ID_OPHANDLER_EA_AY_16 ID_BASE "_GET_EA_AY_16" 1.116 +#define ID_OPHANDLER_EA_AY_32 ID_BASE "_GET_EA_AY_32" 1.117 +#define ID_OPHANDLER_OPER_AY_8 ID_BASE "_GET_OPER_AY_8" 1.118 +#define ID_OPHANDLER_OPER_AY_16 ID_BASE "_GET_OPER_AY_16" 1.119 +#define ID_OPHANDLER_OPER_AY_32 ID_BASE "_GET_OPER_AY_32" 1.120 +#define ID_OPHANDLER_CC ID_BASE "_CC" 1.121 +#define ID_OPHANDLER_NOT_CC ID_BASE "_NOT_CC" 1.122 + 1.123 + 1.124 +#ifndef DECL_SPEC 1.125 +#define DECL_SPEC 1.126 +#endif /* DECL_SPEC */ 1.127 + 1.128 + 1.129 + 1.130 +/* ======================================================================== */ 1.131 +/* ============================== PROTOTYPES ============================== */ 1.132 +/* ======================================================================== */ 1.133 + 1.134 +#define CPU_TYPE_000 0 1.135 +#define CPU_TYPE_010 1 1.136 +#define CPU_TYPE_020 2 1.137 + 1.138 +#define UNSPECIFIED "." 1.139 +#define UNSPECIFIED_CH '.' 1.140 + 1.141 +#define HAS_NO_EA_MODE(A) (strcmp(A, "..........") == 0) 1.142 +#define HAS_EA_AI(A) ((A)[0] == 'A') 1.143 +#define HAS_EA_PI(A) ((A)[1] == '+') 1.144 +#define HAS_EA_PD(A) ((A)[2] == '-') 1.145 +#define HAS_EA_DI(A) ((A)[3] == 'D') 1.146 +#define HAS_EA_IX(A) ((A)[4] == 'X') 1.147 +#define HAS_EA_AW(A) ((A)[5] == 'W') 1.148 +#define HAS_EA_AL(A) ((A)[6] == 'L') 1.149 +#define HAS_EA_PCDI(A) ((A)[7] == 'd') 1.150 +#define HAS_EA_PCIX(A) ((A)[8] == 'x') 1.151 +#define HAS_EA_I(A) ((A)[9] == 'I') 1.152 + 1.153 +enum 1.154 +{ 1.155 + EA_MODE_NONE, /* No special addressing mode */ 1.156 + EA_MODE_AI, /* Address register indirect */ 1.157 + EA_MODE_PI, /* Address register indirect with postincrement */ 1.158 + EA_MODE_PI7, /* Address register 7 indirect with postincrement */ 1.159 + EA_MODE_PD, /* Address register indirect with predecrement */ 1.160 + EA_MODE_PD7, /* Address register 7 indirect with predecrement */ 1.161 + EA_MODE_DI, /* Address register indirect with displacement */ 1.162 + EA_MODE_IX, /* Address register indirect with index */ 1.163 + EA_MODE_AW, /* Absolute word */ 1.164 + EA_MODE_AL, /* Absolute long */ 1.165 + EA_MODE_PCDI, /* Program counter indirect with displacement */ 1.166 + EA_MODE_PCIX, /* Program counter indirect with index */ 1.167 + EA_MODE_I /* Immediate */ 1.168 +}; 1.169 + 1.170 + 1.171 +/* Everything we need to know about an opcode */ 1.172 +typedef struct 1.173 +{ 1.174 + char name[MAX_NAME_LENGTH]; /* opcode handler name */ 1.175 + unsigned int size; /* Size of operation */ 1.176 + char spec_proc[MAX_SPEC_PROC_LENGTH]; /* Special processing mode */ 1.177 + char spec_ea[MAX_SPEC_EA_LENGTH]; /* Specified effective addressing mode */ 1.178 + unsigned int bits; /* Number of significant bits (used for sorting the table) */ 1.179 + unsigned int op_mask; /* Mask to apply for matching an opcode to a handler */ 1.180 + unsigned int op_match; /* Value to match after masking */ 1.181 + char ea_allowed[EA_ALLOWED_LENGTH]; /* Effective addressing modes allowed */ 1.182 + char cpu_mode[NUM_CPUS]; /* User or supervisor mode */ 1.183 + char cpus[NUM_CPUS+1]; /* Allowed CPUs */ 1.184 + unsigned int cycles[NUM_CPUS]; /* cycles for 000, 010, 020 */ 1.185 +} opcode_struct; 1.186 + 1.187 + 1.188 +/* All modifications necessary for a specific EA mode of an instruction */ 1.189 +typedef struct 1.190 +{ 1.191 + char* fname_add; 1.192 + char* ea_add; 1.193 + unsigned int mask_add; 1.194 + unsigned int match_add; 1.195 +} ea_info_struct; 1.196 + 1.197 + 1.198 +/* Holds the body of a function */ 1.199 +typedef struct 1.200 +{ 1.201 + char body[MAX_BODY_LENGTH][MAX_LINE_LENGTH+1]; 1.202 + int length; 1.203 +} body_struct; 1.204 + 1.205 + 1.206 +/* Holds a sequence of search / replace strings */ 1.207 +typedef struct 1.208 +{ 1.209 + char replace[MAX_REPLACE_LENGTH][2][MAX_LINE_LENGTH+1]; 1.210 + int length; 1.211 +} replace_struct; 1.212 + 1.213 + 1.214 +/* Function Prototypes */ 1.215 +void error_exit(char* fmt, ...); 1.216 +void perror_exit(char* fmt, ...); 1.217 +int check_strsncpy(char* dst, char* src, int maxlength); 1.218 +int check_atoi(char* str, int *result); 1.219 +int skip_spaces(char* str); 1.220 +int num_bits(int value); 1.221 +int atoh(char* buff); 1.222 +int fgetline(char* buff, int nchars, FILE* file); 1.223 +int get_oper_cycles(opcode_struct* op, int ea_mode, int cpu_type); 1.224 +opcode_struct* find_opcode(char* name, int size, char* spec_proc, char* spec_ea); 1.225 +opcode_struct* find_illegal_opcode(void); 1.226 +int extract_opcode_info(char* src, char* name, int* size, char* spec_proc, char* spec_ea); 1.227 +void add_replace_string(replace_struct* replace, char* search_str, char* replace_str); 1.228 +void write_body(FILE* filep, body_struct* body, replace_struct* replace); 1.229 +void get_base_name(char* base_name, opcode_struct* op); 1.230 +void write_prototype(FILE* filep, char* base_name); 1.231 +void write_function_name(FILE* filep, char* base_name); 1.232 +void add_opcode_output_table_entry(opcode_struct* op, char* name); 1.233 +static int DECL_SPEC compare_nof_true_bits(const void* aptr, const void* bptr); 1.234 +void print_opcode_output_table(FILE* filep); 1.235 +void write_table_entry(FILE* filep, opcode_struct* op); 1.236 +void set_opcode_struct(opcode_struct* src, opcode_struct* dst, int ea_mode); 1.237 +void generate_opcode_handler(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* opinfo, int ea_mode); 1.238 +void generate_opcode_ea_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op); 1.239 +void generate_opcode_cc_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op_in, int offset); 1.240 +void process_opcode_handlers(void); 1.241 +void populate_table(void); 1.242 +void read_insert(char* insert); 1.243 + 1.244 + 1.245 + 1.246 +/* ======================================================================== */ 1.247 +/* ================================= DATA ================================= */ 1.248 +/* ======================================================================== */ 1.249 + 1.250 +/* Name of the input file */ 1.251 +char g_input_filename[MAX_PATH] = FILENAME_INPUT; 1.252 + 1.253 +/* File handles */ 1.254 +FILE* g_input_file = NULL; 1.255 +FILE* g_prototype_file = NULL; 1.256 +FILE* g_table_file = NULL; 1.257 +FILE* g_ops_ac_file = NULL; 1.258 +FILE* g_ops_dm_file = NULL; 1.259 +FILE* g_ops_nz_file = NULL; 1.260 + 1.261 +int g_num_functions = 0; /* Number of functions processed */ 1.262 +int g_num_primitives = 0; /* Number of function primitives read */ 1.263 +int g_line_number = 1; /* Current line number */ 1.264 + 1.265 +/* Opcode handler table */ 1.266 +opcode_struct g_opcode_input_table[MAX_OPCODE_INPUT_TABLE_LENGTH]; 1.267 + 1.268 +opcode_struct g_opcode_output_table[MAX_OPCODE_OUTPUT_TABLE_LENGTH]; 1.269 +int g_opcode_output_table_length = 0; 1.270 + 1.271 +ea_info_struct g_ea_info_table[13] = 1.272 +{/* fname ea mask match */ 1.273 + {"", "", 0x00, 0x00}, /* EA_MODE_NONE */ 1.274 + {"ai", "AY_AI", 0x38, 0x10}, /* EA_MODE_AI */ 1.275 + {"pi", "AY_PI", 0x38, 0x18}, /* EA_MODE_PI */ 1.276 + {"pi7", "A7_PI", 0x3f, 0x1f}, /* EA_MODE_PI7 */ 1.277 + {"pd", "AY_PD", 0x38, 0x20}, /* EA_MODE_PD */ 1.278 + {"pd7", "A7_PD", 0x3f, 0x27}, /* EA_MODE_PD7 */ 1.279 + {"di", "AY_DI", 0x38, 0x28}, /* EA_MODE_DI */ 1.280 + {"ix", "AY_IX", 0x38, 0x30}, /* EA_MODE_IX */ 1.281 + {"aw", "AW", 0x3f, 0x38}, /* EA_MODE_AW */ 1.282 + {"al", "AL", 0x3f, 0x39}, /* EA_MODE_AL */ 1.283 + {"pcdi", "PCDI", 0x3f, 0x3a}, /* EA_MODE_PCDI */ 1.284 + {"pcix", "PCIX", 0x3f, 0x3b}, /* EA_MODE_PCIX */ 1.285 + {"i", "I", 0x3f, 0x3c}, /* EA_MODE_I */ 1.286 +}; 1.287 + 1.288 + 1.289 +char* g_cc_table[16][2] = 1.290 +{ 1.291 + { "t", "T"}, /* 0000 */ 1.292 + { "f", "F"}, /* 0001 */ 1.293 + {"hi", "HI"}, /* 0010 */ 1.294 + {"ls", "LS"}, /* 0011 */ 1.295 + {"cc", "CC"}, /* 0100 */ 1.296 + {"cs", "CS"}, /* 0101 */ 1.297 + {"ne", "NE"}, /* 0110 */ 1.298 + {"eq", "EQ"}, /* 0111 */ 1.299 + {"vc", "VC"}, /* 1000 */ 1.300 + {"vs", "VS"}, /* 1001 */ 1.301 + {"pl", "PL"}, /* 1010 */ 1.302 + {"mi", "MI"}, /* 1011 */ 1.303 + {"ge", "GE"}, /* 1100 */ 1.304 + {"lt", "LT"}, /* 1101 */ 1.305 + {"gt", "GT"}, /* 1110 */ 1.306 + {"le", "LE"}, /* 1111 */ 1.307 +}; 1.308 + 1.309 +/* size to index translator (0 -> 0, 8 and 16 -> 1, 32 -> 2) */ 1.310 +int g_size_select_table[33] = 1.311 +{ 1.312 + 0, /* unsized */ 1.313 + 0, 0, 0, 0, 0, 0, 0, 1, /* 8 */ 1.314 + 0, 0, 0, 0, 0, 0, 0, 1, /* 16 */ 1.315 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 /* 32 */ 1.316 +}; 1.317 + 1.318 +/* Extra cycles required for certain EA modes */ 1.319 +int g_ea_cycle_table[13][NUM_CPUS][3] = 1.320 +{/* 000 010 020 */ 1.321 + {{ 0, 0, 0}, { 0, 0, 0}, { 0, 0, 0}}, /* EA_MODE_NONE */ 1.322 + {{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}}, /* EA_MODE_AI */ 1.323 + {{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}}, /* EA_MODE_PI */ 1.324 + {{ 0, 4, 8}, { 0, 4, 8}, { 0, 4, 4}}, /* EA_MODE_PI7 */ 1.325 + {{ 0, 6, 10}, { 0, 6, 10}, { 0, 5, 5}}, /* EA_MODE_PD */ 1.326 + {{ 0, 6, 10}, { 0, 6, 10}, { 0, 5, 5}}, /* EA_MODE_PD7 */ 1.327 + {{ 0, 8, 12}, { 0, 8, 12}, { 0, 5, 5}}, /* EA_MODE_DI */ 1.328 + {{ 0, 10, 14}, { 0, 10, 14}, { 0, 7, 7}}, /* EA_MODE_IX */ 1.329 + {{ 0, 8, 12}, { 0, 8, 12}, { 0, 4, 4}}, /* EA_MODE_AW */ 1.330 + {{ 0, 12, 16}, { 0, 12, 16}, { 0, 4, 4}}, /* EA_MODE_AL */ 1.331 + {{ 0, 8, 12}, { 0, 8, 12}, { 0, 5, 5}}, /* EA_MODE_PCDI */ 1.332 + {{ 0, 10, 14}, { 0, 10, 14}, { 0, 7, 7}}, /* EA_MODE_PCIX */ 1.333 + {{ 0, 4, 8}, { 0, 4, 8}, { 0, 2, 4}}, /* EA_MODE_I */ 1.334 +}; 1.335 + 1.336 +/* Extra cycles for JMP instruction (000, 010) */ 1.337 +int g_jmp_cycle_table[13] = 1.338 +{ 1.339 + 0, /* EA_MODE_NONE */ 1.340 + 4, /* EA_MODE_AI */ 1.341 + 0, /* EA_MODE_PI */ 1.342 + 0, /* EA_MODE_PI7 */ 1.343 + 0, /* EA_MODE_PD */ 1.344 + 0, /* EA_MODE_PD7 */ 1.345 + 6, /* EA_MODE_DI */ 1.346 + 8, /* EA_MODE_IX */ 1.347 + 6, /* EA_MODE_AW */ 1.348 + 8, /* EA_MODE_AL */ 1.349 + 6, /* EA_MODE_PCDI */ 1.350 + 10, /* EA_MODE_PCIX */ 1.351 + 0, /* EA_MODE_I */ 1.352 +}; 1.353 + 1.354 +/* Extra cycles for JSR instruction (000, 010) */ 1.355 +int g_jsr_cycle_table[13] = 1.356 +{ 1.357 + 0, /* EA_MODE_NONE */ 1.358 + 4, /* EA_MODE_AI */ 1.359 + 0, /* EA_MODE_PI */ 1.360 + 0, /* EA_MODE_PI7 */ 1.361 + 0, /* EA_MODE_PD */ 1.362 + 0, /* EA_MODE_PD7 */ 1.363 + 6, /* EA_MODE_DI */ 1.364 + 10, /* EA_MODE_IX */ 1.365 + 6, /* EA_MODE_AW */ 1.366 + 8, /* EA_MODE_AL */ 1.367 + 6, /* EA_MODE_PCDI */ 1.368 + 10, /* EA_MODE_PCIX */ 1.369 + 0, /* EA_MODE_I */ 1.370 +}; 1.371 + 1.372 +/* Extra cycles for LEA instruction (000, 010) */ 1.373 +int g_lea_cycle_table[13] = 1.374 +{ 1.375 + 0, /* EA_MODE_NONE */ 1.376 + 4, /* EA_MODE_AI */ 1.377 + 0, /* EA_MODE_PI */ 1.378 + 0, /* EA_MODE_PI7 */ 1.379 + 0, /* EA_MODE_PD */ 1.380 + 0, /* EA_MODE_PD7 */ 1.381 + 8, /* EA_MODE_DI */ 1.382 + 12, /* EA_MODE_IX */ 1.383 + 8, /* EA_MODE_AW */ 1.384 + 12, /* EA_MODE_AL */ 1.385 + 8, /* EA_MODE_PCDI */ 1.386 + 12, /* EA_MODE_PCIX */ 1.387 + 0, /* EA_MODE_I */ 1.388 +}; 1.389 + 1.390 +/* Extra cycles for PEA instruction (000, 010) */ 1.391 +int g_pea_cycle_table[13] = 1.392 +{ 1.393 + 0, /* EA_MODE_NONE */ 1.394 + 4, /* EA_MODE_AI */ 1.395 + 0, /* EA_MODE_PI */ 1.396 + 0, /* EA_MODE_PI7 */ 1.397 + 0, /* EA_MODE_PD */ 1.398 + 0, /* EA_MODE_PD7 */ 1.399 + 10, /* EA_MODE_DI */ 1.400 + 14, /* EA_MODE_IX */ 1.401 + 10, /* EA_MODE_AW */ 1.402 + 14, /* EA_MODE_AL */ 1.403 + 10, /* EA_MODE_PCDI */ 1.404 + 14, /* EA_MODE_PCIX */ 1.405 + 0, /* EA_MODE_I */ 1.406 +}; 1.407 + 1.408 +/* Extra cycles for MOVES instruction (010) */ 1.409 +int g_moves_cycle_table[13][3] = 1.410 +{ 1.411 + { 0, 0, 0}, /* EA_MODE_NONE */ 1.412 + { 0, 4, 6}, /* EA_MODE_AI */ 1.413 + { 0, 4, 6}, /* EA_MODE_PI */ 1.414 + { 0, 4, 6}, /* EA_MODE_PI7 */ 1.415 + { 0, 6, 12}, /* EA_MODE_PD */ 1.416 + { 0, 6, 12}, /* EA_MODE_PD7 */ 1.417 + { 0, 12, 16}, /* EA_MODE_DI */ 1.418 + { 0, 16, 20}, /* EA_MODE_IX */ 1.419 + { 0, 12, 16}, /* EA_MODE_AW */ 1.420 + { 0, 16, 20}, /* EA_MODE_AL */ 1.421 + { 0, 0, 0}, /* EA_MODE_PCDI */ 1.422 + { 0, 0, 0}, /* EA_MODE_PCIX */ 1.423 + { 0, 0, 0}, /* EA_MODE_I */ 1.424 +}; 1.425 + 1.426 +/* Extra cycles for CLR instruction (010) */ 1.427 +int g_clr_cycle_table[13][3] = 1.428 +{ 1.429 + { 0, 0, 0}, /* EA_MODE_NONE */ 1.430 + { 0, 4, 6}, /* EA_MODE_AI */ 1.431 + { 0, 4, 6}, /* EA_MODE_PI */ 1.432 + { 0, 4, 6}, /* EA_MODE_PI7 */ 1.433 + { 0, 6, 8}, /* EA_MODE_PD */ 1.434 + { 0, 6, 8}, /* EA_MODE_PD7 */ 1.435 + { 0, 8, 10}, /* EA_MODE_DI */ 1.436 + { 0, 10, 14}, /* EA_MODE_IX */ 1.437 + { 0, 8, 10}, /* EA_MODE_AW */ 1.438 + { 0, 10, 14}, /* EA_MODE_AL */ 1.439 + { 0, 0, 0}, /* EA_MODE_PCDI */ 1.440 + { 0, 0, 0}, /* EA_MODE_PCIX */ 1.441 + { 0, 0, 0}, /* EA_MODE_I */ 1.442 +}; 1.443 + 1.444 + 1.445 + 1.446 +/* ======================================================================== */ 1.447 +/* =========================== UTILITY FUNCTIONS ========================== */ 1.448 +/* ======================================================================== */ 1.449 + 1.450 +/* Print an error message and exit with status error */ 1.451 +void error_exit(char* fmt, ...) 1.452 +{ 1.453 + va_list args; 1.454 + fprintf(stderr, "In %s, near or on line %d:\n\t", g_input_filename, g_line_number); 1.455 + va_start(args, fmt); 1.456 + vfprintf(stderr, fmt, args); 1.457 + va_end(args); 1.458 + fprintf(stderr, "\n"); 1.459 + 1.460 + if(g_prototype_file) fclose(g_prototype_file); 1.461 + if(g_table_file) fclose(g_table_file); 1.462 + if(g_ops_ac_file) fclose(g_ops_ac_file); 1.463 + if(g_ops_dm_file) fclose(g_ops_dm_file); 1.464 + if(g_ops_nz_file) fclose(g_ops_nz_file); 1.465 + if(g_input_file) fclose(g_input_file); 1.466 + 1.467 + exit(EXIT_FAILURE); 1.468 +} 1.469 + 1.470 +/* Print an error message, call perror(), and exit with status error */ 1.471 +void perror_exit(char* fmt, ...) 1.472 +{ 1.473 + va_list args; 1.474 + va_start(args, fmt); 1.475 + vfprintf(stderr, fmt, args); 1.476 + va_end(args); 1.477 + perror(""); 1.478 + 1.479 + if(g_prototype_file) fclose(g_prototype_file); 1.480 + if(g_table_file) fclose(g_table_file); 1.481 + if(g_ops_ac_file) fclose(g_ops_ac_file); 1.482 + if(g_ops_dm_file) fclose(g_ops_dm_file); 1.483 + if(g_ops_nz_file) fclose(g_ops_nz_file); 1.484 + if(g_input_file) fclose(g_input_file); 1.485 + 1.486 + exit(EXIT_FAILURE); 1.487 +} 1.488 + 1.489 + 1.490 +/* copy until 0 or space and exit with error if we read too far */ 1.491 +int check_strsncpy(char* dst, char* src, int maxlength) 1.492 +{ 1.493 + char* p = dst; 1.494 + while(*src && *src != ' ') 1.495 + { 1.496 + *p++ = *src++; 1.497 + if(p - dst > maxlength) 1.498 + error_exit("Field too long"); 1.499 + } 1.500 + *p = 0; 1.501 + return p - dst; 1.502 +} 1.503 + 1.504 +/* copy until 0 or specified character and exit with error if we read too far */ 1.505 +int check_strcncpy(char* dst, char* src, char delim, int maxlength) 1.506 +{ 1.507 + char* p = dst; 1.508 + while(*src && *src != delim) 1.509 + { 1.510 + *p++ = *src++; 1.511 + if(p - dst > maxlength) 1.512 + error_exit("Field too long"); 1.513 + } 1.514 + *p = 0; 1.515 + return p - dst; 1.516 +} 1.517 + 1.518 +/* convert ascii to integer and exit with error if we find invalid data */ 1.519 +int check_atoi(char* str, int *result) 1.520 +{ 1.521 + int accum = 0; 1.522 + char* p = str; 1.523 + while(*p >= '0' && *p <= '9') 1.524 + { 1.525 + accum *= 10; 1.526 + accum += *p++ - '0'; 1.527 + } 1.528 + if(*p != ' ' && *p != 0) 1.529 + error_exit("Malformed integer value (%c)", *p); 1.530 + *result = accum; 1.531 + return p - str; 1.532 +} 1.533 + 1.534 +/* Skip past spaces in a string */ 1.535 +int skip_spaces(char* str) 1.536 +{ 1.537 + char* p = str; 1.538 + 1.539 + while(*p == ' ') 1.540 + p++; 1.541 + 1.542 + return p - str; 1.543 +} 1.544 + 1.545 +/* Count the number of set bits in a value */ 1.546 +int num_bits(int value) 1.547 +{ 1.548 + value = ((value & 0xaaaa) >> 1) + (value & 0x5555); 1.549 + value = ((value & 0xcccc) >> 2) + (value & 0x3333); 1.550 + value = ((value & 0xf0f0) >> 4) + (value & 0x0f0f); 1.551 + value = ((value & 0xff00) >> 8) + (value & 0x00ff); 1.552 + return value; 1.553 +} 1.554 + 1.555 +/* Convert a hex value written in ASCII */ 1.556 +int atoh(char* buff) 1.557 +{ 1.558 + int accum = 0; 1.559 + 1.560 + for(;;buff++) 1.561 + { 1.562 + if(*buff >= '0' && *buff <= '9') 1.563 + { 1.564 + accum <<= 4; 1.565 + accum += *buff - '0'; 1.566 + } 1.567 + else if(*buff >= 'a' && *buff <= 'f') 1.568 + { 1.569 + accum <<= 4; 1.570 + accum += *buff - 'a' + 10; 1.571 + } 1.572 + else break; 1.573 + } 1.574 + return accum; 1.575 +} 1.576 + 1.577 +/* Get a line of text from a file, discarding any end-of-line characters */ 1.578 +int fgetline(char* buff, int nchars, FILE* file) 1.579 +{ 1.580 + int length; 1.581 + 1.582 + if(fgets(buff, nchars, file) == NULL) 1.583 + return -1; 1.584 + if(buff[0] == '\r') 1.585 + memcpy(buff, buff + 1, nchars - 1); 1.586 + 1.587 + length = strlen(buff); 1.588 + while(length && (buff[length-1] == '\r' || buff[length-1] == '\n')) 1.589 + length--; 1.590 + buff[length] = 0; 1.591 + g_line_number++; 1.592 + 1.593 + return length; 1.594 +} 1.595 + 1.596 + 1.597 + 1.598 +/* ======================================================================== */ 1.599 +/* =========================== HELPER FUNCTIONS =========================== */ 1.600 +/* ======================================================================== */ 1.601 + 1.602 +/* Calculate the number of cycles an opcode requires */ 1.603 +int get_oper_cycles(opcode_struct* op, int ea_mode, int cpu_type) 1.604 +{ 1.605 + int size = g_size_select_table[op->size]; 1.606 + 1.607 + if(op->cpus[cpu_type] == '.') 1.608 + return 0; 1.609 + 1.610 + if(cpu_type < CPU_TYPE_020) 1.611 + { 1.612 + if(cpu_type == CPU_TYPE_010) 1.613 + { 1.614 + if(strcmp(op->name, "moves") == 0) 1.615 + return op->cycles[cpu_type] + g_moves_cycle_table[ea_mode][size]; 1.616 + if(strcmp(op->name, "clr") == 0) 1.617 + return op->cycles[cpu_type] + g_clr_cycle_table[ea_mode][size]; 1.618 + } 1.619 + 1.620 + /* ASG: added these cases -- immediate modes take 2 extra cycles here */ 1.621 + if(cpu_type == CPU_TYPE_000 && ea_mode == EA_MODE_I && 1.622 + ((strcmp(op->name, "add") == 0 && strcmp(op->spec_proc, "er") == 0) || 1.623 + strcmp(op->name, "adda") == 0 || 1.624 + (strcmp(op->name, "and") == 0 && strcmp(op->spec_proc, "er") == 0) || 1.625 + (strcmp(op->name, "or") == 0 && strcmp(op->spec_proc, "er") == 0) || 1.626 + (strcmp(op->name, "sub") == 0 && strcmp(op->spec_proc, "er") == 0) || 1.627 + strcmp(op->name, "suba") == 0)) 1.628 + return op->cycles[cpu_type] + g_ea_cycle_table[ea_mode][cpu_type][size] + 2; 1.629 + 1.630 + if(strcmp(op->name, "jmp") == 0) 1.631 + return op->cycles[cpu_type] + g_jmp_cycle_table[ea_mode]; 1.632 + if(strcmp(op->name, "jsr") == 0) 1.633 + return op->cycles[cpu_type] + g_jsr_cycle_table[ea_mode]; 1.634 + if(strcmp(op->name, "lea") == 0) 1.635 + return op->cycles[cpu_type] + g_lea_cycle_table[ea_mode]; 1.636 + if(strcmp(op->name, "pea") == 0) 1.637 + return op->cycles[cpu_type] + g_pea_cycle_table[ea_mode]; 1.638 + } 1.639 + return op->cycles[cpu_type] + g_ea_cycle_table[ea_mode][cpu_type][size]; 1.640 +} 1.641 + 1.642 +/* Find an opcode in the opcode handler list */ 1.643 +opcode_struct* find_opcode(char* name, int size, char* spec_proc, char* spec_ea) 1.644 +{ 1.645 + opcode_struct* op; 1.646 + 1.647 + 1.648 + for(op = g_opcode_input_table;op->name != NULL;op++) 1.649 + { 1.650 + if( strcmp(name, op->name) == 0 && 1.651 + (size == (int)op->size) && 1.652 + strcmp(spec_proc, op->spec_proc) == 0 && 1.653 + strcmp(spec_ea, op->spec_ea) == 0) 1.654 + return op; 1.655 + } 1.656 + return NULL; 1.657 +} 1.658 + 1.659 +/* Specifically find the illegal opcode in the list */ 1.660 +opcode_struct* find_illegal_opcode(void) 1.661 +{ 1.662 + opcode_struct* op; 1.663 + 1.664 + for(op = g_opcode_input_table;op->name != NULL;op++) 1.665 + { 1.666 + if(strcmp(op->name, "illegal") == 0) 1.667 + return op; 1.668 + } 1.669 + return NULL; 1.670 +} 1.671 + 1.672 +/* Parse an opcode handler name */ 1.673 +int extract_opcode_info(char* src, char* name, int* size, char* spec_proc, char* spec_ea) 1.674 +{ 1.675 + char* ptr = strstr(src, ID_OPHANDLER_NAME); 1.676 + 1.677 + if(ptr == NULL) 1.678 + return 0; 1.679 + 1.680 + ptr += strlen(ID_OPHANDLER_NAME) + 1; 1.681 + 1.682 + ptr += check_strcncpy(name, ptr, ',', MAX_NAME_LENGTH); 1.683 + if(*ptr != ',') return 0; 1.684 + ptr++; 1.685 + ptr += skip_spaces(ptr); 1.686 + 1.687 + *size = atoi(ptr); 1.688 + ptr = strstr(ptr, ","); 1.689 + if(ptr == NULL) return 0; 1.690 + ptr++; 1.691 + ptr += skip_spaces(ptr); 1.692 + 1.693 + ptr += check_strcncpy(spec_proc, ptr, ',', MAX_SPEC_PROC_LENGTH); 1.694 + if(*ptr != ',') return 0; 1.695 + ptr++; 1.696 + ptr += skip_spaces(ptr); 1.697 + 1.698 + ptr += check_strcncpy(spec_ea, ptr, ')', MAX_SPEC_EA_LENGTH); 1.699 + if(*ptr != ')') return 0; 1.700 + 1.701 + return 1; 1.702 +} 1.703 + 1.704 + 1.705 +/* Add a search/replace pair to a replace structure */ 1.706 +void add_replace_string(replace_struct* replace, char* search_str, char* replace_str) 1.707 +{ 1.708 + if(replace->length >= MAX_REPLACE_LENGTH) 1.709 + error_exit("overflow in replace structure"); 1.710 + 1.711 + strcpy(replace->replace[replace->length][0], search_str); 1.712 + strcpy(replace->replace[replace->length++][1], replace_str); 1.713 +} 1.714 + 1.715 +/* Write a function body while replacing any selected strings */ 1.716 +void write_body(FILE* filep, body_struct* body, replace_struct* replace) 1.717 +{ 1.718 + int i; 1.719 + int j; 1.720 + char* ptr; 1.721 + char output[MAX_LINE_LENGTH+1]; 1.722 + char temp_buff[MAX_LINE_LENGTH+1]; 1.723 + int found; 1.724 + 1.725 + for(i=0;i<body->length;i++) 1.726 + { 1.727 + strcpy(output, body->body[i]); 1.728 + /* Check for the base directive header */ 1.729 + if(strstr(output, ID_BASE) != NULL) 1.730 + { 1.731 + /* Search for any text we need to replace */ 1.732 + found = 0; 1.733 + for(j=0;j<replace->length;j++) 1.734 + { 1.735 + ptr = strstr(output, replace->replace[j][0]); 1.736 + if(ptr) 1.737 + { 1.738 + /* We found something to replace */ 1.739 + found = 1; 1.740 + strcpy(temp_buff, ptr+strlen(replace->replace[j][0])); 1.741 + strcpy(ptr, replace->replace[j][1]); 1.742 + strcat(ptr, temp_buff); 1.743 + } 1.744 + } 1.745 + /* Found a directive with no matching replace string */ 1.746 + if(!found) 1.747 + error_exit("Unknown " ID_BASE " directive"); 1.748 + } 1.749 + fprintf(filep, "%s\n", output); 1.750 + } 1.751 + fprintf(filep, "\n\n"); 1.752 +} 1.753 + 1.754 +/* Generate a base function name from an opcode struct */ 1.755 +void get_base_name(char* base_name, opcode_struct* op) 1.756 +{ 1.757 + sprintf(base_name, "m68k_op_%s", op->name); 1.758 + if(op->size > 0) 1.759 + sprintf(base_name+strlen(base_name), "_%d", op->size); 1.760 + if(strcmp(op->spec_proc, UNSPECIFIED) != 0) 1.761 + sprintf(base_name+strlen(base_name), "_%s", op->spec_proc); 1.762 + if(strcmp(op->spec_ea, UNSPECIFIED) != 0) 1.763 + sprintf(base_name+strlen(base_name), "_%s", op->spec_ea); 1.764 +} 1.765 + 1.766 +/* Write the prototype of an opcode handler function */ 1.767 +void write_prototype(FILE* filep, char* base_name) 1.768 +{ 1.769 + fprintf(filep, "void %s(void);\n", base_name); 1.770 +} 1.771 + 1.772 +/* Write the name of an opcode handler function */ 1.773 +void write_function_name(FILE* filep, char* base_name) 1.774 +{ 1.775 + fprintf(filep, "void %s(void)\n", base_name); 1.776 +} 1.777 + 1.778 +void add_opcode_output_table_entry(opcode_struct* op, char* name) 1.779 +{ 1.780 + opcode_struct* ptr; 1.781 + if(g_opcode_output_table_length > MAX_OPCODE_OUTPUT_TABLE_LENGTH) 1.782 + error_exit("Opcode output table overflow"); 1.783 + 1.784 + ptr = g_opcode_output_table + g_opcode_output_table_length++; 1.785 + 1.786 + *ptr = *op; 1.787 + strcpy(ptr->name, name); 1.788 + ptr->bits = num_bits(ptr->op_mask); 1.789 +} 1.790 + 1.791 +/* 1.792 + * Comparison function for qsort() 1.793 + * For entries with an equal number of set bits in 1.794 + * the mask compare the match values 1.795 + */ 1.796 +static int DECL_SPEC compare_nof_true_bits(const void* aptr, const void* bptr) 1.797 +{ 1.798 + const opcode_struct *a = aptr, *b = bptr; 1.799 + if(a->bits != b->bits) 1.800 + return a->bits - b->bits; 1.801 + if(a->op_mask != b->op_mask) 1.802 + return a->op_mask - b->op_mask; 1.803 + return a->op_match - b->op_match; 1.804 +} 1.805 + 1.806 +void print_opcode_output_table(FILE* filep) 1.807 +{ 1.808 + int i; 1.809 + qsort((void *)g_opcode_output_table, g_opcode_output_table_length, sizeof(g_opcode_output_table[0]), compare_nof_true_bits); 1.810 + 1.811 + for(i=0;i<g_opcode_output_table_length;i++) 1.812 + write_table_entry(filep, g_opcode_output_table+i); 1.813 +} 1.814 + 1.815 +/* Write an entry in the opcode handler table */ 1.816 +void write_table_entry(FILE* filep, opcode_struct* op) 1.817 +{ 1.818 + int i; 1.819 + 1.820 + fprintf(filep, "\t{%-28s, 0x%04x, 0x%04x, {", 1.821 + op->name, op->op_mask, op->op_match); 1.822 + 1.823 + for(i=0;i<NUM_CPUS;i++) 1.824 + { 1.825 + fprintf(filep, "%3d", op->cycles[i]); 1.826 + if(i < NUM_CPUS-1) 1.827 + fprintf(filep, ", "); 1.828 + } 1.829 + 1.830 + fprintf(filep, "}},\n"); 1.831 +} 1.832 + 1.833 +/* Fill out an opcode struct with a specific addressing mode of the source opcode struct */ 1.834 +void set_opcode_struct(opcode_struct* src, opcode_struct* dst, int ea_mode) 1.835 +{ 1.836 + int i; 1.837 + 1.838 + *dst = *src; 1.839 + 1.840 + for(i=0;i<NUM_CPUS;i++) 1.841 + dst->cycles[i] = get_oper_cycles(dst, ea_mode, i); 1.842 + if(strcmp(dst->spec_ea, UNSPECIFIED) == 0 && ea_mode != EA_MODE_NONE) 1.843 + sprintf(dst->spec_ea, "%s", g_ea_info_table[ea_mode].fname_add); 1.844 + dst->op_mask |= g_ea_info_table[ea_mode].mask_add; 1.845 + dst->op_match |= g_ea_info_table[ea_mode].match_add; 1.846 +} 1.847 + 1.848 + 1.849 +/* Generate a final opcode handler from the provided data */ 1.850 +void generate_opcode_handler(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* opinfo, int ea_mode) 1.851 +{ 1.852 + char str[MAX_LINE_LENGTH+1]; 1.853 + opcode_struct* op = malloc(sizeof(opcode_struct)); 1.854 + 1.855 + /* Set the opcode structure and write the tables, prototypes, etc */ 1.856 + set_opcode_struct(opinfo, op, ea_mode); 1.857 + get_base_name(str, op); 1.858 + write_prototype(g_prototype_file, str); 1.859 + add_opcode_output_table_entry(op, str); 1.860 + write_function_name(filep, str); 1.861 + 1.862 + /* Add any replace strings needed */ 1.863 + if(ea_mode != EA_MODE_NONE) 1.864 + { 1.865 + sprintf(str, "EA_%s_8()", g_ea_info_table[ea_mode].ea_add); 1.866 + add_replace_string(replace, ID_OPHANDLER_EA_AY_8, str); 1.867 + sprintf(str, "EA_%s_16()", g_ea_info_table[ea_mode].ea_add); 1.868 + add_replace_string(replace, ID_OPHANDLER_EA_AY_16, str); 1.869 + sprintf(str, "EA_%s_32()", g_ea_info_table[ea_mode].ea_add); 1.870 + add_replace_string(replace, ID_OPHANDLER_EA_AY_32, str); 1.871 + sprintf(str, "OPER_%s_8()", g_ea_info_table[ea_mode].ea_add); 1.872 + add_replace_string(replace, ID_OPHANDLER_OPER_AY_8, str); 1.873 + sprintf(str, "OPER_%s_16()", g_ea_info_table[ea_mode].ea_add); 1.874 + add_replace_string(replace, ID_OPHANDLER_OPER_AY_16, str); 1.875 + sprintf(str, "OPER_%s_32()", g_ea_info_table[ea_mode].ea_add); 1.876 + add_replace_string(replace, ID_OPHANDLER_OPER_AY_32, str); 1.877 + } 1.878 + 1.879 + /* Now write the function body with the selected replace strings */ 1.880 + write_body(filep, body, replace); 1.881 + g_num_functions++; 1.882 + free(op); 1.883 +} 1.884 + 1.885 +/* Generate opcode variants based on available addressing modes */ 1.886 +void generate_opcode_ea_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op) 1.887 +{ 1.888 + int old_length = replace->length; 1.889 + 1.890 + /* No ea modes available for this opcode */ 1.891 + if(HAS_NO_EA_MODE(op->ea_allowed)) 1.892 + { 1.893 + generate_opcode_handler(filep, body, replace, op, EA_MODE_NONE); 1.894 + return; 1.895 + } 1.896 + 1.897 + /* Check for and create specific opcodes for each available addressing mode */ 1.898 + if(HAS_EA_AI(op->ea_allowed)) 1.899 + generate_opcode_handler(filep, body, replace, op, EA_MODE_AI); 1.900 + replace->length = old_length; 1.901 + if(HAS_EA_PI(op->ea_allowed)) 1.902 + { 1.903 + generate_opcode_handler(filep, body, replace, op, EA_MODE_PI); 1.904 + replace->length = old_length; 1.905 + if(op->size == 8) 1.906 + generate_opcode_handler(filep, body, replace, op, EA_MODE_PI7); 1.907 + } 1.908 + replace->length = old_length; 1.909 + if(HAS_EA_PD(op->ea_allowed)) 1.910 + { 1.911 + generate_opcode_handler(filep, body, replace, op, EA_MODE_PD); 1.912 + replace->length = old_length; 1.913 + if(op->size == 8) 1.914 + generate_opcode_handler(filep, body, replace, op, EA_MODE_PD7); 1.915 + } 1.916 + replace->length = old_length; 1.917 + if(HAS_EA_DI(op->ea_allowed)) 1.918 + generate_opcode_handler(filep, body, replace, op, EA_MODE_DI); 1.919 + replace->length = old_length; 1.920 + if(HAS_EA_IX(op->ea_allowed)) 1.921 + generate_opcode_handler(filep, body, replace, op, EA_MODE_IX); 1.922 + replace->length = old_length; 1.923 + if(HAS_EA_AW(op->ea_allowed)) 1.924 + generate_opcode_handler(filep, body, replace, op, EA_MODE_AW); 1.925 + replace->length = old_length; 1.926 + if(HAS_EA_AL(op->ea_allowed)) 1.927 + generate_opcode_handler(filep, body, replace, op, EA_MODE_AL); 1.928 + replace->length = old_length; 1.929 + if(HAS_EA_PCDI(op->ea_allowed)) 1.930 + generate_opcode_handler(filep, body, replace, op, EA_MODE_PCDI); 1.931 + replace->length = old_length; 1.932 + if(HAS_EA_PCIX(op->ea_allowed)) 1.933 + generate_opcode_handler(filep, body, replace, op, EA_MODE_PCIX); 1.934 + replace->length = old_length; 1.935 + if(HAS_EA_I(op->ea_allowed)) 1.936 + generate_opcode_handler(filep, body, replace, op, EA_MODE_I); 1.937 + replace->length = old_length; 1.938 +} 1.939 + 1.940 +/* Generate variants of condition code opcodes */ 1.941 +void generate_opcode_cc_variants(FILE* filep, body_struct* body, replace_struct* replace, opcode_struct* op_in, int offset) 1.942 +{ 1.943 + char repl[20]; 1.944 + char replnot[20]; 1.945 + int i; 1.946 + int old_length = replace->length; 1.947 + opcode_struct* op = malloc(sizeof(opcode_struct)); 1.948 + 1.949 + *op = *op_in; 1.950 + 1.951 + op->op_mask |= 0x0f00; 1.952 + 1.953 + /* Do all condition codes except t and f */ 1.954 + for(i=2;i<16;i++) 1.955 + { 1.956 + /* Add replace strings for this condition code */ 1.957 + sprintf(repl, "COND_%s()", g_cc_table[i][1]); 1.958 + sprintf(replnot, "COND_NOT_%s()", g_cc_table[i][1]); 1.959 + 1.960 + add_replace_string(replace, ID_OPHANDLER_CC, repl); 1.961 + add_replace_string(replace, ID_OPHANDLER_NOT_CC, replnot); 1.962 + 1.963 + /* Set the new opcode info */ 1.964 + strcpy(op->name+offset, g_cc_table[i][0]); 1.965 + 1.966 + op->op_match = (op->op_match & 0xf0ff) | (i<<8); 1.967 + 1.968 + /* Generate all opcode variants for this modified opcode */ 1.969 + generate_opcode_ea_variants(filep, body, replace, op); 1.970 + /* Remove the above replace strings */ 1.971 + replace->length = old_length; 1.972 + } 1.973 + free(op); 1.974 +} 1.975 + 1.976 +/* Process the opcode handlers section of the input file */ 1.977 +void process_opcode_handlers(void) 1.978 +{ 1.979 + FILE* input_file = g_input_file; 1.980 + FILE* output_file; 1.981 + char func_name[MAX_LINE_LENGTH+1]; 1.982 + char oper_name[MAX_LINE_LENGTH+1]; 1.983 + int oper_size; 1.984 + char oper_spec_proc[MAX_LINE_LENGTH+1]; 1.985 + char oper_spec_ea[MAX_LINE_LENGTH+1]; 1.986 + opcode_struct* opinfo; 1.987 + replace_struct* replace = malloc(sizeof(replace_struct)); 1.988 + body_struct* body = malloc(sizeof(body_struct)); 1.989 + 1.990 + 1.991 + output_file = g_ops_ac_file; 1.992 + 1.993 + for(;;) 1.994 + { 1.995 + /* Find the first line of the function */ 1.996 + func_name[0] = 0; 1.997 + while(strstr(func_name, ID_OPHANDLER_NAME) == NULL) 1.998 + { 1.999 + if(strcmp(func_name, ID_INPUT_SEPARATOR) == 0) 1.1000 + { 1.1001 + free(replace); 1.1002 + free(body); 1.1003 + return; /* all done */ 1.1004 + } 1.1005 + if(fgetline(func_name, MAX_LINE_LENGTH, input_file) < 0) 1.1006 + error_exit("Premature end of file when getting function name"); 1.1007 + } 1.1008 + /* Get the rest of the function */ 1.1009 + for(body->length=0;;body->length++) 1.1010 + { 1.1011 + if(body->length > MAX_BODY_LENGTH) 1.1012 + error_exit("Function too long"); 1.1013 + 1.1014 + if(fgetline(body->body[body->length], MAX_LINE_LENGTH, input_file) < 0) 1.1015 + error_exit("Premature end of file when getting function body"); 1.1016 + 1.1017 + if(body->body[body->length][0] == '}') 1.1018 + { 1.1019 + body->length++; 1.1020 + break; 1.1021 + } 1.1022 + } 1.1023 + 1.1024 + g_num_primitives++; 1.1025 + 1.1026 + /* Extract the function name information */ 1.1027 + if(!extract_opcode_info(func_name, oper_name, &oper_size, oper_spec_proc, oper_spec_ea)) 1.1028 + error_exit("Invalid " ID_OPHANDLER_NAME " format"); 1.1029 + 1.1030 + /* Find the corresponding table entry */ 1.1031 + opinfo = find_opcode(oper_name, oper_size, oper_spec_proc, oper_spec_ea); 1.1032 + if(opinfo == NULL) 1.1033 + error_exit("Unable to find matching table entry for %s", func_name); 1.1034 + 1.1035 + /* Change output files if we pass 'c' or 'n' */ 1.1036 + if(output_file == g_ops_ac_file && oper_name[0] > 'c') 1.1037 + output_file = g_ops_dm_file; 1.1038 + else if(output_file == g_ops_dm_file && oper_name[0] > 'm') 1.1039 + output_file = g_ops_nz_file; 1.1040 + 1.1041 + replace->length = 0; 1.1042 + 1.1043 + /* Generate opcode variants */ 1.1044 + if(strcmp(opinfo->name, "bcc") == 0 || strcmp(opinfo->name, "scc") == 0) 1.1045 + generate_opcode_cc_variants(output_file, body, replace, opinfo, 1); 1.1046 + else if(strcmp(opinfo->name, "dbcc") == 0) 1.1047 + generate_opcode_cc_variants(output_file, body, replace, opinfo, 2); 1.1048 + else if(strcmp(opinfo->name, "trapcc") == 0) 1.1049 + generate_opcode_cc_variants(output_file, body, replace, opinfo, 4); 1.1050 + else 1.1051 + generate_opcode_ea_variants(output_file, body, replace, opinfo); 1.1052 + } 1.1053 +} 1.1054 + 1.1055 + 1.1056 +/* Populate the opcode handler table from the input file */ 1.1057 +void populate_table(void) 1.1058 +{ 1.1059 + char* ptr; 1.1060 + char bitpattern[17]; 1.1061 + opcode_struct* op; 1.1062 + char buff[MAX_LINE_LENGTH]; 1.1063 + int i; 1.1064 + int temp; 1.1065 + 1.1066 + buff[0] = 0; 1.1067 + 1.1068 + /* Find the start of the table */ 1.1069 + while(strcmp(buff, ID_TABLE_START) != 0) 1.1070 + if(fgetline(buff, MAX_LINE_LENGTH, g_input_file) < 0) 1.1071 + error_exit("Premature EOF while reading table"); 1.1072 + 1.1073 + /* Process the entire table */ 1.1074 + for(op = g_opcode_input_table;;op++) 1.1075 + { 1.1076 + if(fgetline(buff, MAX_LINE_LENGTH, g_input_file) < 0) 1.1077 + error_exit("Premature EOF while reading table"); 1.1078 + if(strlen(buff) == 0) 1.1079 + continue; 1.1080 + /* We finish when we find an input separator */ 1.1081 + if(strcmp(buff, ID_INPUT_SEPARATOR) == 0) 1.1082 + break; 1.1083 + 1.1084 + /* Extract the info from the table */ 1.1085 + ptr = buff; 1.1086 + 1.1087 + /* Name */ 1.1088 + ptr += skip_spaces(ptr); 1.1089 + ptr += check_strsncpy(op->name, ptr, MAX_NAME_LENGTH); 1.1090 + 1.1091 + /* Size */ 1.1092 + ptr += skip_spaces(ptr); 1.1093 + ptr += check_atoi(ptr, &temp); 1.1094 + op->size = (unsigned char)temp; 1.1095 + 1.1096 + /* Special processing */ 1.1097 + ptr += skip_spaces(ptr); 1.1098 + ptr += check_strsncpy(op->spec_proc, ptr, MAX_SPEC_PROC_LENGTH); 1.1099 + 1.1100 + /* Specified EA Mode */ 1.1101 + ptr += skip_spaces(ptr); 1.1102 + ptr += check_strsncpy(op->spec_ea, ptr, MAX_SPEC_EA_LENGTH); 1.1103 + 1.1104 + /* Bit Pattern (more processing later) */ 1.1105 + ptr += skip_spaces(ptr); 1.1106 + ptr += check_strsncpy(bitpattern, ptr, 17); 1.1107 + 1.1108 + /* Allowed Addressing Mode List */ 1.1109 + ptr += skip_spaces(ptr); 1.1110 + ptr += check_strsncpy(op->ea_allowed, ptr, EA_ALLOWED_LENGTH); 1.1111 + 1.1112 + /* CPU operating mode (U = user or supervisor, S = supervisor only */ 1.1113 + ptr += skip_spaces(ptr); 1.1114 + for(i=0;i<NUM_CPUS;i++) 1.1115 + { 1.1116 + op->cpu_mode[i] = *ptr++; 1.1117 + ptr += skip_spaces(ptr); 1.1118 + } 1.1119 + 1.1120 + /* Allowed CPUs for this instruction */ 1.1121 + for(i=0;i<NUM_CPUS;i++) 1.1122 + { 1.1123 + ptr += skip_spaces(ptr); 1.1124 + if(*ptr == UNSPECIFIED_CH) 1.1125 + { 1.1126 + op->cpus[i] = UNSPECIFIED_CH; 1.1127 + op->cycles[i] = 0; 1.1128 + ptr++; 1.1129 + } 1.1130 + else 1.1131 + { 1.1132 + op->cpus[i] = (char)('0' + i); 1.1133 + ptr += check_atoi(ptr, &temp); 1.1134 + op->cycles[i] = (unsigned char)temp; 1.1135 + } 1.1136 + } 1.1137 + 1.1138 + /* generate mask and match from bitpattern */ 1.1139 + op->op_mask = 0; 1.1140 + op->op_match = 0; 1.1141 + for(i=0;i<16;i++) 1.1142 + { 1.1143 + op->op_mask |= (bitpattern[i] != '.') << (15-i); 1.1144 + op->op_match |= (bitpattern[i] == '1') << (15-i); 1.1145 + } 1.1146 + } 1.1147 + /* Terminate the list */ 1.1148 + op->name[0] = 0; 1.1149 +} 1.1150 + 1.1151 +/* Read a header or footer insert from the input file */ 1.1152 +void read_insert(char* insert) 1.1153 +{ 1.1154 + char* ptr = insert; 1.1155 + char* overflow = insert + MAX_INSERT_LENGTH - MAX_LINE_LENGTH; 1.1156 + int length; 1.1157 + char* first_blank = NULL; 1.1158 + 1.1159 + /* Skip any leading blank lines */ 1.1160 + for(length = 0;length == 0;length = fgetline(ptr, MAX_LINE_LENGTH, g_input_file)) 1.1161 + if(ptr >= overflow) 1.1162 + error_exit("Buffer overflow reading inserts"); 1.1163 + if(length < 0) 1.1164 + error_exit("Premature EOF while reading inserts"); 1.1165 + 1.1166 + /* Advance and append newline */ 1.1167 + ptr += length; 1.1168 + strcpy(ptr++, "\n"); 1.1169 + 1.1170 + /* Read until next separator */ 1.1171 + for(;;) 1.1172 + { 1.1173 + /* Read a new line */ 1.1174 + if(ptr >= overflow) 1.1175 + error_exit("Buffer overflow reading inserts"); 1.1176 + if((length = fgetline(ptr, MAX_LINE_LENGTH, g_input_file)) < 0) 1.1177 + error_exit("Premature EOF while reading inserts"); 1.1178 + 1.1179 + /* Stop if we read a separator */ 1.1180 + if(strcmp(ptr, ID_INPUT_SEPARATOR) == 0) 1.1181 + break; 1.1182 + 1.1183 + /* keep track in case there are trailing blanks */ 1.1184 + if(length == 0) 1.1185 + { 1.1186 + if(first_blank == NULL) 1.1187 + first_blank = ptr; 1.1188 + } 1.1189 + else 1.1190 + first_blank = NULL; 1.1191 + 1.1192 + /* Advance and append newline */ 1.1193 + ptr += length; 1.1194 + strcpy(ptr++, "\n"); 1.1195 + } 1.1196 + 1.1197 + /* kill any trailing blank lines */ 1.1198 + if(first_blank) 1.1199 + ptr = first_blank; 1.1200 + *ptr = 0; 1.1201 +} 1.1202 + 1.1203 + 1.1204 + 1.1205 +/* ======================================================================== */ 1.1206 +/* ============================= MAIN FUNCTION ============================ */ 1.1207 +/* ======================================================================== */ 1.1208 + 1.1209 +int main(int argc, char **argv) 1.1210 +{ 1.1211 + /* File stuff */ 1.1212 + char output_path[MAX_DIR] = ""; 1.1213 + char filename[MAX_PATH]; 1.1214 + /* Section identifier */ 1.1215 + char section_id[MAX_LINE_LENGTH+1]; 1.1216 + /* Inserts */ 1.1217 + char temp_insert[MAX_INSERT_LENGTH+1]; 1.1218 + char prototype_footer_insert[MAX_INSERT_LENGTH+1]; 1.1219 + char table_footer_insert[MAX_INSERT_LENGTH+1]; 1.1220 + char ophandler_footer_insert[MAX_INSERT_LENGTH+1]; 1.1221 + /* Flags if we've processed certain parts already */ 1.1222 + int prototype_header_read = 0; 1.1223 + int prototype_footer_read = 0; 1.1224 + int table_header_read = 0; 1.1225 + int table_footer_read = 0; 1.1226 + int ophandler_header_read = 0; 1.1227 + int ophandler_footer_read = 0; 1.1228 + int table_body_read = 0; 1.1229 + int ophandler_body_read = 0; 1.1230 + 1.1231 + printf("\n\t\tMusashi v%s 68000, 68010, 68EC020, 68020 emulator\n", g_version); 1.1232 + printf("\t\tCopyright 1998-2000 Karl Stenerud (karl@mame.net)\n\n"); 1.1233 + 1.1234 + /* Check if output path and source for the input file are given */ 1.1235 + if(argc > 1) 1.1236 + { 1.1237 + char *ptr; 1.1238 + strcpy(output_path, argv[1]); 1.1239 + 1.1240 + for(ptr = strchr(output_path, '\\'); ptr; ptr = strchr(ptr, '\\')) 1.1241 + *ptr = '/'; 1.1242 + if(output_path[strlen(output_path)-1] != '/') 1.1243 + strcat(output_path, "/"); 1.1244 + if(argc > 2) 1.1245 + strcpy(g_input_filename, argv[2]); 1.1246 + } 1.1247 + 1.1248 + 1.1249 + /* Open the files we need */ 1.1250 + sprintf(filename, "%s%s", output_path, FILENAME_PROTOTYPE); 1.1251 + if((g_prototype_file = fopen(filename, "wt")) == NULL) 1.1252 + perror_exit("Unable to create prototype file (%s)\n", filename); 1.1253 + 1.1254 + sprintf(filename, "%s%s", output_path, FILENAME_TABLE); 1.1255 + if((g_table_file = fopen(filename, "wt")) == NULL) 1.1256 + perror_exit("Unable to create table file (%s)\n", filename); 1.1257 + 1.1258 + sprintf(filename, "%s%s", output_path, FILENAME_OPS_AC); 1.1259 + if((g_ops_ac_file = fopen(filename, "wt")) == NULL) 1.1260 + perror_exit("Unable to create ops ac file (%s)\n", filename); 1.1261 + 1.1262 + sprintf(filename, "%s%s", output_path, FILENAME_OPS_DM); 1.1263 + if((g_ops_dm_file = fopen(filename, "wt")) == NULL) 1.1264 + perror_exit("Unable to create ops dm file (%s)\n", filename); 1.1265 + 1.1266 + sprintf(filename, "%s%s", output_path, FILENAME_OPS_NZ); 1.1267 + if((g_ops_nz_file = fopen(filename, "wt")) == NULL) 1.1268 + perror_exit("Unable to create ops nz file (%s)\n", filename); 1.1269 + 1.1270 + if((g_input_file=fopen(g_input_filename, "rt")) == NULL) 1.1271 + perror_exit("can't open %s for input", g_input_filename); 1.1272 + 1.1273 + 1.1274 + /* Get to the first section of the input file */ 1.1275 + section_id[0] = 0; 1.1276 + while(strcmp(section_id, ID_INPUT_SEPARATOR) != 0) 1.1277 + if(fgetline(section_id, MAX_LINE_LENGTH, g_input_file) < 0) 1.1278 + error_exit("Premature EOF while reading input file"); 1.1279 + 1.1280 + /* Now process all sections */ 1.1281 + for(;;) 1.1282 + { 1.1283 + if(fgetline(section_id, MAX_LINE_LENGTH, g_input_file) < 0) 1.1284 + error_exit("Premature EOF while reading input file"); 1.1285 + if(strcmp(section_id, ID_PROTOTYPE_HEADER) == 0) 1.1286 + { 1.1287 + if(prototype_header_read) 1.1288 + error_exit("Duplicate prototype header"); 1.1289 + read_insert(temp_insert); 1.1290 + fprintf(g_prototype_file, "%s\n\n", temp_insert); 1.1291 + prototype_header_read = 1; 1.1292 + } 1.1293 + else if(strcmp(section_id, ID_TABLE_HEADER) == 0) 1.1294 + { 1.1295 + if(table_header_read) 1.1296 + error_exit("Duplicate table header"); 1.1297 + read_insert(temp_insert); 1.1298 + fprintf(g_table_file, "%s", temp_insert); 1.1299 + table_header_read = 1; 1.1300 + } 1.1301 + else if(strcmp(section_id, ID_OPHANDLER_HEADER) == 0) 1.1302 + { 1.1303 + if(ophandler_header_read) 1.1304 + error_exit("Duplicate opcode handler header"); 1.1305 + read_insert(temp_insert); 1.1306 + fprintf(g_ops_ac_file, "%s\n\n", temp_insert); 1.1307 + fprintf(g_ops_dm_file, "%s\n\n", temp_insert); 1.1308 + fprintf(g_ops_nz_file, "%s\n\n", temp_insert); 1.1309 + ophandler_header_read = 1; 1.1310 + } 1.1311 + else if(strcmp(section_id, ID_PROTOTYPE_FOOTER) == 0) 1.1312 + { 1.1313 + if(prototype_footer_read) 1.1314 + error_exit("Duplicate prototype footer"); 1.1315 + read_insert(prototype_footer_insert); 1.1316 + prototype_footer_read = 1; 1.1317 + } 1.1318 + else if(strcmp(section_id, ID_TABLE_FOOTER) == 0) 1.1319 + { 1.1320 + if(table_footer_read) 1.1321 + error_exit("Duplicate table footer"); 1.1322 + read_insert(table_footer_insert); 1.1323 + table_footer_read = 1; 1.1324 + } 1.1325 + else if(strcmp(section_id, ID_OPHANDLER_FOOTER) == 0) 1.1326 + { 1.1327 + if(ophandler_footer_read) 1.1328 + error_exit("Duplicate opcode handler footer"); 1.1329 + read_insert(ophandler_footer_insert); 1.1330 + ophandler_footer_read = 1; 1.1331 + } 1.1332 + else if(strcmp(section_id, ID_TABLE_BODY) == 0) 1.1333 + { 1.1334 + if(!prototype_header_read) 1.1335 + error_exit("Table body encountered before prototype header"); 1.1336 + if(!table_header_read) 1.1337 + error_exit("Table body encountered before table header"); 1.1338 + if(!ophandler_header_read) 1.1339 + error_exit("Table body encountered before opcode handler header"); 1.1340 + 1.1341 + if(table_body_read) 1.1342 + error_exit("Duplicate table body"); 1.1343 + 1.1344 + populate_table(); 1.1345 + table_body_read = 1; 1.1346 + } 1.1347 + else if(strcmp(section_id, ID_OPHANDLER_BODY) == 0) 1.1348 + { 1.1349 + if(!prototype_header_read) 1.1350 + error_exit("Opcode handlers encountered before prototype header"); 1.1351 + if(!table_header_read) 1.1352 + error_exit("Opcode handlers encountered before table header"); 1.1353 + if(!ophandler_header_read) 1.1354 + error_exit("Opcode handlers encountered before opcode handler header"); 1.1355 + if(!table_body_read) 1.1356 + error_exit("Opcode handlers encountered before table body"); 1.1357 + 1.1358 + if(ophandler_body_read) 1.1359 + error_exit("Duplicate opcode handler section"); 1.1360 + 1.1361 + process_opcode_handlers(); 1.1362 + 1.1363 + ophandler_body_read = 1; 1.1364 + } 1.1365 + else if(strcmp(section_id, ID_END) == 0) 1.1366 + { 1.1367 + /* End of input file. Do a sanity check and then write footers */ 1.1368 + if(!prototype_header_read) 1.1369 + error_exit("Missing prototype header"); 1.1370 + if(!prototype_footer_read) 1.1371 + error_exit("Missing prototype footer"); 1.1372 + if(!table_header_read) 1.1373 + error_exit("Missing table header"); 1.1374 + if(!table_footer_read) 1.1375 + error_exit("Missing table footer"); 1.1376 + if(!table_body_read) 1.1377 + error_exit("Missing table body"); 1.1378 + if(!ophandler_header_read) 1.1379 + error_exit("Missing opcode handler header"); 1.1380 + if(!ophandler_footer_read) 1.1381 + error_exit("Missing opcode handler footer"); 1.1382 + if(!ophandler_body_read) 1.1383 + error_exit("Missing opcode handler body"); 1.1384 + 1.1385 + print_opcode_output_table(g_table_file); 1.1386 + 1.1387 + fprintf(g_prototype_file, "%s\n\n", prototype_footer_insert); 1.1388 + fprintf(g_table_file, "%s\n\n", table_footer_insert); 1.1389 + fprintf(g_ops_ac_file, "%s\n\n", ophandler_footer_insert); 1.1390 + fprintf(g_ops_dm_file, "%s\n\n", ophandler_footer_insert); 1.1391 + fprintf(g_ops_nz_file, "%s\n\n", ophandler_footer_insert); 1.1392 + 1.1393 + break; 1.1394 + } 1.1395 + else 1.1396 + { 1.1397 + error_exit("Unknown section identifier: %s", section_id); 1.1398 + } 1.1399 + } 1.1400 + 1.1401 + /* Close all files and exit */ 1.1402 + fclose(g_prototype_file); 1.1403 + fclose(g_table_file); 1.1404 + fclose(g_ops_ac_file); 1.1405 + fclose(g_ops_dm_file); 1.1406 + fclose(g_ops_nz_file); 1.1407 + fclose(g_input_file); 1.1408 + 1.1409 + printf("Generated %d opcode handlers from %d primitives\n", g_num_functions, g_num_primitives); 1.1410 + 1.1411 + return 0; 1.1412 +} 1.1413 + 1.1414 + 1.1415 + 1.1416 +/* ======================================================================== */ 1.1417 +/* ============================== END OF FILE ============================= */ 1.1418 +/* ======================================================================== */