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