1 #include "../platforms/platforms.h"
6 #define M68K_CPU_TYPES M68K_CPU_TYPE_SCC68070
8 const char *cpu_types[M68K_CPU_TYPES] = {
21 const char *map_type_names[MAPTYPE_NUM] = {
28 const char *config_item_names[CONFITEM_NUM] = {
39 const char *mapcmd_names[MAPCMD_NUM] = {
50 int get_config_item_type(char *cmd) {
51 for (int i = 0; i < CONFITEM_NUM; i++) {
52 if (strcmp(cmd, config_item_names[i]) == 0) {
60 unsigned int get_m68k_cpu_type(char *name) {
61 for (int i = 0; i < M68K_CPU_TYPES; i++) {
62 if (strcmp(name, cpu_types[i]) == 0) {
63 printf("Set CPU type to %s.\n", cpu_types[i]);
68 printf ("Invalid CPU type %s specified, defaulting to 68000.\n", name);
69 return M68K_CPU_TYPE_68000;
72 unsigned int get_map_cmd(char *name) {
73 for (int i = 1; i < MAPCMD_NUM; i++) {
74 if (strcmp(name, mapcmd_names[i]) == 0) {
79 return MAPCMD_UNKNOWN;
82 unsigned int get_map_type(char *name) {
83 for (int i = 1; i < MAPTYPE_NUM; i++) {
84 if (strcmp(name, map_type_names[i]) == 0) {
92 void trim_whitespace(char *str) {
93 while (strlen(str) != 0 && (str[strlen(str) - 1] == ' ' || str[strlen(str) - 1] == '\t' || str[strlen(str) - 1] == 0x0A || str[strlen(str) - 1] == 0x0D)) {
94 str[strlen(str) - 1] = '\0';
98 unsigned int get_int(char *str) {
104 if (strlen(str) > 2 && str[0] == '0' && str[1] == 'x') {
105 for (int i = 2; i < (int)strlen(str); i++) {
106 if (str[i] >= '0' && str[i] <= '9') {
107 ret_int = (str[i] - '0') | (ret_int << 4);
111 case 'A': ret_int = 0xA | (ret_int << 4); break;
112 case 'B': ret_int = 0xB | (ret_int << 4); break;
113 case 'C': ret_int = 0xC | (ret_int << 4); break;
114 case 'D': ret_int = 0xD | (ret_int << 4); break;
115 case 'E': ret_int = 0xE | (ret_int << 4); break;
116 case 'F': ret_int = 0xF | (ret_int << 4); break;
117 case 'K': ret_int = ret_int * SIZE_KILO; break;
118 case 'M': ret_int = ret_int * SIZE_MEGA; break;
119 case 'G': ret_int = ret_int * SIZE_GIGA; break;
121 printf("Unknown character %c in hex value.\n", str[i]);
130 if (str[strlen(str) - 1] == 'K')
131 ret_int = ret_int * SIZE_KILO;
132 else if (str[strlen(str) - 1] == 'M')
133 ret_int = ret_int * SIZE_MEGA;
134 else if (str[strlen(str) - 1] == 'G')
135 ret_int = ret_int * SIZE_GIGA;
141 void get_next_string(char *str, char *str_out, int *strpos, char separator) {
142 int str_pos = 0, out_pos = 0;
150 while (str[str_pos] == ' ' && str[str_pos] == '\t' && str_pos < (int)strlen(str)) {
154 for (int i = str_pos; i < (int)strlen(str); i++) {
155 str_out[out_pos] = str[i];
156 if ((separator == ' ' && (str[i] == ' ' || str[i] == '\t')) || str[i] == separator) {
157 str_out[out_pos] = '\0';
164 if (i + 1 == (int)strlen(str) && strpos) {
166 str_out[out_pos] = '\0';
171 void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id) {
172 unsigned int index = 0, file_size = 0;
175 while (index < MAX_NUM_MAPPED_ITEMS) {
176 if (cfg->map_type[index] == MAPTYPE_NONE)
180 if (index == MAX_NUM_MAPPED_ITEMS) {
181 printf("Unable to map item, only %d items can be mapped with current binary.\n", MAX_NUM_MAPPED_ITEMS);
185 cfg->map_type[index] = type;
186 cfg->map_offset[index] = addr;
187 cfg->map_size[index] = size;
188 cfg->map_mirror[index] = mirr_addr;
189 if (strlen(map_id)) {
190 cfg->map_id[index] = (char *)malloc(strlen(map_id) + 1);
191 strcpy(cfg->map_id[index], map_id);
196 printf("Allocating %d bytes for RAM mapping (%d MB)...\n", size, size / 1024 / 1024);
197 cfg->map_data[index] = (unsigned char *)malloc(size);
198 if (!cfg->map_data[index]) {
199 printf("ERROR: Unable to allocate memory for mapped RAM!\n");
202 memset(cfg->map_data[index], 0x00, size);
205 in = fopen(filename, "rb");
207 printf("Failed to open file %s for ROM mapping.\n", filename);
210 fseek(in, 0, SEEK_END);
211 file_size = (int)ftell(in);
213 cfg->map_size[index] = file_size;
215 fseek(in, 0, SEEK_SET);
216 cfg->map_data[index] = (unsigned char *)calloc(1, cfg->map_size[index]);
217 cfg->rom_size[index] = (cfg->map_size[index] <= file_size) ? cfg->map_size[index] : file_size;
218 if (!cfg->map_data[index]) {
219 printf("ERROR: Unable to allocate memory for mapped ROM!\n");
222 memset(cfg->map_data[index], 0x00, cfg->map_size[index]);
223 fread(cfg->map_data[index], cfg->rom_size[index], 1, in);
226 case MAPTYPE_REGISTER:
232 printf("[MAP %d] Added %s mapping for range %.8lX-%.8lX ID: %s\n", index, map_type_names[type], cfg->map_offset[index], cfg->map_offset[index] + cfg->map_size[index] - 1, cfg->map_id[index] ? cfg->map_id[index] : "None");
237 cfg->map_type[index] = MAPTYPE_NONE;
242 struct emulator_config *load_config_file(char *filename) {
243 FILE *in = fopen(filename, "rb");
245 printf("Failed to open config file %s for reading.\n", filename);
249 char *parse_line = NULL;
251 struct emulator_config *cfg = NULL;
254 parse_line = (char *)calloc(1, 512);
256 printf("Failed to allocate memory for config file line buffer.\n");
259 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
261 printf("Failed to allocate memory for temporary emulator config.\n");
265 memset(cfg, 0x00, sizeof(struct emulator_config));
266 cfg->cpu_type = M68K_CPU_TYPE_68000;
270 memset(parse_line, 0x00, 512);
271 fgets(parse_line, 512, in);
273 if (strlen(parse_line) <= 2 || parse_line[0] == '#' || parse_line[0] == '/')
276 trim_whitespace(parse_line);
278 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
280 switch (get_config_item_type(cur_cmd)) {
281 case CONFITEM_CPUTYPE:
282 cfg->cpu_type = get_m68k_cpu_type(parse_line + str_pos);
285 unsigned int maptype = 0, mapsize = 0, mapaddr = 0;
287 char mapfile[128], mapid[128];
288 memset(mapfile, 0x00, 128);
289 memset(mapid, 0x00, 128);
291 while (str_pos < (int)strlen(parse_line)) {
292 get_next_string(parse_line, cur_cmd, &str_pos, '=');
293 switch(get_map_cmd(cur_cmd)) {
295 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
296 maptype = get_map_type(cur_cmd);
297 //printf("Type! %s\n", map_type_names[maptype]);
300 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
301 mapaddr = get_int(cur_cmd);
302 //printf("Address! %.8X\n", mapaddr);
305 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
306 mapsize = get_int(cur_cmd);
307 //printf("Size! %.8X\n", mapsize);
310 get_next_string(parse_line, cur_cmd, &str_pos, '-');
311 mapaddr = get_int(cur_cmd);
312 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
313 mapsize = get_int(cur_cmd) - 1 - mapaddr;
314 //printf("Range! %d-%d\n", mapaddr, mapaddr + mapsize);
316 case MAPCMD_FILENAME:
317 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
318 strcpy(mapfile, cur_cmd);
319 //printf("File! %s\n", mapfile);
322 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
323 strcpy(mapid, cur_cmd);
324 //printf("File! %s\n", mapfile);
326 case MAPCMD_OVL_REMAP:
327 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
328 mirraddr = get_int(cur_cmd);
331 printf("Unknown/unhandled map argument %s on line %d.\n", cur_cmd, cur_line);
335 add_mapping(cfg, maptype, mapaddr, mapsize, mirraddr, mapfile, mapid);
339 case CONFITEM_LOOPCYCLES:
340 cfg->loop_cycles = get_int(parse_line + str_pos);
341 printf("Set CPU loop cycles to %d.\n", cfg->loop_cycles);
344 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
345 cfg->mouse_file = (char *)calloc(1, strlen(cur_cmd) + 1);
346 strcpy(cfg->mouse_file, cur_cmd);
347 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
348 cfg->mouse_toggle_key = cur_cmd[0];
349 cfg->mouse_enabled = 1;
350 printf("Enabled mouse event forwarding from file %s, toggle key %c.\n", cfg->mouse_file, cfg->mouse_toggle_key);
352 case CONFITEM_KEYBOARD:
353 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
354 cfg->keyboard_toggle_key = cur_cmd[0];
355 printf("Enabled keyboard event forwarding, toggle key %c.\n", cfg->keyboard_toggle_key);
357 case CONFITEM_PLATFORM: {
358 char platform_name[128], platform_sub[128];
359 memset(platform_name, 0x00, 128);
360 memset(platform_sub, 0x00, 128);
361 get_next_string(parse_line, platform_name, &str_pos, ' ');
362 printf("Setting platform to %s", platform_name);
363 get_next_string(parse_line, platform_sub, &str_pos, ' ');
364 if (strlen(platform_sub))
365 printf(" (sub: %s)", platform_sub);
367 cfg->platform = make_platform_config(platform_name, platform_sub);
370 case CONFITEM_SETVAR: {
371 if (!cfg->platform) {
372 printf("Warning: esetvar used in config file with no platform specified.\n");
376 char var_name[128], var_value[128];
377 memset(var_name, 0x00, 128);
378 memset(var_value, 0x00, 128);
379 get_next_string(parse_line, var_name, &str_pos, ' ');
380 get_next_string(parse_line, var_value, &str_pos, ' ');
381 cfg->platform->setvar(var_name, var_value);
387 printf("Unknown config item %s on line %d.\n", cur_cmd, cur_line);
394 goto load_successful;
398 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
399 if (cfg->map_data[i])
400 free(cfg->map_data[i]);
401 cfg->map_data[i] = NULL;
413 int get_named_mapped_item(struct emulator_config *cfg, char *name) {
414 if (strlen(name) == 0)
417 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
418 if (cfg->map_type[i] == MAPTYPE_NONE || !cfg->map_id[i])
420 if (strcmp(name, cfg->map_id[i]) == 0)