1 // SPDX-License-Identifier: MIT
3 #include "platforms/platforms.h"
10 #define M68K_CPU_TYPES M68K_CPU_TYPE_SCC68070
12 const char *cpu_types[M68K_CPU_TYPES] = {
25 const char *map_type_names[MAPTYPE_NUM] = {
33 const char *config_item_names[CONFITEM_NUM] = {
45 const char *mapcmd_names[MAPCMD_NUM] = {
56 int get_config_item_type(char *cmd) {
57 for (int i = 0; i < CONFITEM_NUM; i++) {
58 if (strcmp(cmd, config_item_names[i]) == 0) {
66 unsigned int get_m68k_cpu_type(char *name) {
67 for (int i = 0; i < M68K_CPU_TYPES; i++) {
68 if (strcmp(name, cpu_types[i]) == 0) {
69 printf("[CFG] Set CPU type to %s.\n", cpu_types[i]);
74 printf("[CFG] Invalid CPU type %s specified, defaulting to 68000.\n", name);
75 return M68K_CPU_TYPE_68000;
78 unsigned int get_map_cmd(char *name) {
79 for (int i = 1; i < MAPCMD_NUM; i++) {
80 if (strcmp(name, mapcmd_names[i]) == 0) {
85 return MAPCMD_UNKNOWN;
88 unsigned int get_map_type(char *name) {
89 for (int i = 1; i < MAPTYPE_NUM; i++) {
90 if (strcmp(name, map_type_names[i]) == 0) {
98 void trim_whitespace(char *str) {
99 while (strlen(str) != 0 && (str[strlen(str) - 1] == ' ' || str[strlen(str) - 1] == '\t' || str[strlen(str) - 1] == 0x0A || str[strlen(str) - 1] == 0x0D)) {
100 str[strlen(str) - 1] = '\0';
104 unsigned int get_int(char *str) {
105 if (strlen(str) == 0)
110 if (strlen(str) > 2 && str[0] == '0' && str[1] == 'x') {
111 for (int i = 2; i < (int)strlen(str); i++) {
112 if (str[i] >= '0' && str[i] <= '9') {
113 ret_int = (str[i] - '0') | (ret_int << 4);
117 case 'A': ret_int = 0xA | (ret_int << 4); break;
118 case 'B': ret_int = 0xB | (ret_int << 4); break;
119 case 'C': ret_int = 0xC | (ret_int << 4); break;
120 case 'D': ret_int = 0xD | (ret_int << 4); break;
121 case 'E': ret_int = 0xE | (ret_int << 4); break;
122 case 'F': ret_int = 0xF | (ret_int << 4); break;
123 case 'K': ret_int = ret_int * SIZE_KILO; break;
124 case 'M': ret_int = ret_int * SIZE_MEGA; break;
125 case 'G': ret_int = ret_int * SIZE_GIGA; break;
127 printf("[CFG] Unknown character %c in hex value.\n", str[i]);
136 if (str[strlen(str) - 1] == 'K')
137 ret_int = ret_int * SIZE_KILO;
138 else if (str[strlen(str) - 1] == 'M')
139 ret_int = ret_int * SIZE_MEGA;
140 else if (str[strlen(str) - 1] == 'G')
141 ret_int = ret_int * SIZE_GIGA;
147 void get_next_string(char *str, char *str_out, int *strpos, char separator) {
148 int str_pos = 0, out_pos = 0;
156 while (str[str_pos] == ' ' && str[str_pos] == '\t' && str_pos < (int)strlen(str)) {
160 for (int i = str_pos; i < (int)strlen(str); i++) {
161 str_out[out_pos] = str[i];
162 if ((separator == ' ' && (str[i] == ' ' || str[i] == '\t')) || str[i] == separator) {
163 str_out[out_pos] = '\0';
170 if (i + 1 == (int)strlen(str) && strpos) {
172 str_out[out_pos] = '\0';
177 void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id) {
178 unsigned int index = 0, file_size = 0;
181 while (index < MAX_NUM_MAPPED_ITEMS) {
182 if (cfg->map_type[index] == MAPTYPE_NONE)
186 if (index == MAX_NUM_MAPPED_ITEMS) {
187 printf("[CFG] Unable to map item, only %d items can be mapped with current binary.\n", MAX_NUM_MAPPED_ITEMS);
191 cfg->map_type[index] = type;
192 cfg->map_offset[index] = addr;
193 cfg->map_size[index] = size;
194 cfg->map_high[index] = addr + size;
195 cfg->map_mirror[index] = mirr_addr;
196 if (strlen(map_id)) {
197 cfg->map_id[index] = (char *)malloc(strlen(map_id) + 1);
198 strcpy(cfg->map_id[index], map_id);
202 case MAPTYPE_RAM_NOALLOC:
203 printf("[CFG] Adding %d byte (%d MB) RAM mapping %s...\n", size, size / 1024 / 1024, map_id);
204 cfg->map_data[index] = (unsigned char *)filename;
207 printf("[CFG] Allocating %d bytes for RAM mapping (%d MB)...\n", size, size / 1024 / 1024);
208 cfg->map_data[index] = (unsigned char *)malloc(size);
209 if (!cfg->map_data[index]) {
210 printf("[CFG] ERROR: Unable to allocate memory for mapped RAM!\n");
213 memset(cfg->map_data[index], 0x00, size);
216 in = fopen(filename, "rb");
218 printf("[CFG] Failed to open file %s for ROM mapping. Using onboard ROM instead, if available.\n", filename);
221 fseek(in, 0, SEEK_END);
222 file_size = (int)ftell(in);
224 cfg->map_size[index] = file_size;
225 cfg->map_high[index] = addr + cfg->map_size[index];
227 fseek(in, 0, SEEK_SET);
228 cfg->map_data[index] = (unsigned char *)calloc(1, cfg->map_size[index]);
229 cfg->rom_size[index] = (cfg->map_size[index] <= file_size) ? cfg->map_size[index] : file_size;
230 if (!cfg->map_data[index]) {
231 printf("[CFG] ERROR: Unable to allocate memory for mapped ROM!\n");
234 memset(cfg->map_data[index], 0x00, cfg->map_size[index]);
235 fread(cfg->map_data[index], cfg->rom_size[index], 1, in);
237 displayRomInfo(cfg->map_data[index]);
238 if (cfg->map_size[index] == cfg->rom_size[index])
239 m68k_add_rom_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
241 case MAPTYPE_REGISTER:
246 printf("[CFG] [MAP %d] Added %s mapping for range %.8lX-%.8lX ID: %s\n", index, map_type_names[type], cfg->map_offset[index], cfg->map_high[index] - 1, cfg->map_id[index] ? cfg->map_id[index] : "None");
251 cfg->map_type[index] = MAPTYPE_NONE;
256 void free_config_file(struct emulator_config *cfg) {
258 printf("[CFG] Tried to free NULL config, aborting.\n");
262 cfg->platform->shutdown(cfg);
264 cfg->platform = NULL;
267 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
268 if (cfg->map_data[i]) {
269 free(cfg->map_data[i]);
270 cfg->map_data[i] = NULL;
272 if (cfg->map_id[i]) {
273 free(cfg->map_id[i]);
274 cfg->map_id[i] = NULL;
277 if (cfg->mouse_file) {
278 free(cfg->mouse_file);
279 cfg->mouse_file = NULL;
281 if (cfg->keyboard_file) {
282 free(cfg->keyboard_file);
283 cfg->keyboard_file = NULL;
288 printf("[CFG] Config file freed. Maybe.\n");
291 struct emulator_config *load_config_file(char *filename) {
292 FILE *in = fopen(filename, "rb");
294 printf("[CFG] Failed to open config file %s for reading.\n", filename);
298 char *parse_line = NULL;
300 struct emulator_config *cfg = NULL;
303 parse_line = (char *)calloc(1, 512);
305 printf("[CFG] Failed to allocate memory for config file line buffer.\n");
308 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
310 printf("[CFG] Failed to allocate memory for temporary emulator config.\n");
314 memset(cfg, 0x00, sizeof(struct emulator_config));
315 cfg->cpu_type = M68K_CPU_TYPE_68000;
319 memset(parse_line, 0x00, 512);
320 fgets(parse_line, 512, in);
322 if (strlen(parse_line) <= 2 || parse_line[0] == '#' || parse_line[0] == '/')
325 trim_whitespace(parse_line);
327 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
329 switch (get_config_item_type(cur_cmd)) {
330 case CONFITEM_CPUTYPE:
331 cfg->cpu_type = get_m68k_cpu_type(parse_line + str_pos);
334 unsigned int maptype = 0, mapsize = 0, mapaddr = 0;
335 unsigned int mirraddr = ((unsigned int)-1);
336 char mapfile[128], mapid[128];
337 memset(mapfile, 0x00, 128);
338 memset(mapid, 0x00, 128);
340 while (str_pos < (int)strlen(parse_line)) {
341 get_next_string(parse_line, cur_cmd, &str_pos, '=');
342 switch(get_map_cmd(cur_cmd)) {
344 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
345 maptype = get_map_type(cur_cmd);
346 //printf("Type! %s\n", map_type_names[maptype]);
349 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
350 mapaddr = get_int(cur_cmd);
351 //printf("Address! %.8X\n", mapaddr);
354 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
355 mapsize = get_int(cur_cmd);
356 //printf("Size! %.8X\n", mapsize);
359 get_next_string(parse_line, cur_cmd, &str_pos, '-');
360 mapaddr = get_int(cur_cmd);
361 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
362 mapsize = get_int(cur_cmd) - 1 - mapaddr;
363 //printf("Range! %d-%d\n", mapaddr, mapaddr + mapsize);
365 case MAPCMD_FILENAME:
366 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
367 strcpy(mapfile, cur_cmd);
368 //printf("File! %s\n", mapfile);
371 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
372 strcpy(mapid, cur_cmd);
373 //printf("File! %s\n", mapfile);
375 case MAPCMD_OVL_REMAP:
376 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
377 mirraddr = get_int(cur_cmd);
380 printf("[CFG] Unknown/unhandled map argument %s on line %d.\n", cur_cmd, cur_line);
384 add_mapping(cfg, maptype, mapaddr, mapsize, mirraddr, mapfile, mapid);
388 case CONFITEM_LOOPCYCLES:
389 cfg->loop_cycles = get_int(parse_line + str_pos);
390 printf("[CFG] Set CPU loop cycles to %d.\n", cfg->loop_cycles);
393 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
394 cfg->mouse_file = (char *)calloc(1, strlen(cur_cmd) + 1);
395 strcpy(cfg->mouse_file, cur_cmd);
396 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
397 cfg->mouse_toggle_key = cur_cmd[0];
398 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
399 cfg->mouse_autoconnect = (strcmp(cur_cmd, "autoconnect") == 0) ? 1 : 0;
400 cfg->mouse_enabled = 1;
401 printf("[CFG] Enabled mouse event forwarding from file %s, toggle key %c.\n", cfg->mouse_file, cfg->mouse_toggle_key);
403 case CONFITEM_KEYBOARD:
404 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
405 cfg->keyboard_toggle_key = cur_cmd[0];
406 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
407 cfg->keyboard_grab = (strcmp(cur_cmd, "grab") == 0) ? 1 : 0;
408 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
409 cfg->keyboard_autoconnect = (strcmp(cur_cmd, "autoconnect") == 0) ? 1 : 0;
410 printf("[CFG] Enabled keyboard event forwarding, toggle key %c", cfg->keyboard_toggle_key);
411 if (cfg->keyboard_grab)
412 printf(", locking from host when connected");
413 if (cfg->keyboard_autoconnect)
414 printf(", connected to guest at startup");
417 case CONFITEM_KBFILE:
418 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
419 cfg->keyboard_file = (char *)calloc(1, strlen(cur_cmd) + 1);
420 strcpy(cfg->keyboard_file, cur_cmd);
421 printf("[CFG] Set keyboard event source file to %s.\n", cfg->keyboard_file);
423 case CONFITEM_PLATFORM: {
424 char platform_name[128], platform_sub[128];
425 memset(platform_name, 0x00, 128);
426 memset(platform_sub, 0x00, 128);
427 get_next_string(parse_line, platform_name, &str_pos, ' ');
428 printf("[CFG] Setting platform to %s", platform_name);
429 get_next_string(parse_line, platform_sub, &str_pos, ' ');
430 if (strlen(platform_sub))
431 printf(" (sub: %s)", platform_sub);
433 cfg->platform = make_platform_config(platform_name, platform_sub);
436 case CONFITEM_SETVAR: {
437 if (!cfg->platform) {
438 printf("[CFG] Warning: setvar used in config file with no platform specified.\n");
442 char var_name[128], var_value[128];
443 memset(var_name, 0x00, 128);
444 memset(var_value, 0x00, 128);
445 get_next_string(parse_line, var_name, &str_pos, ' ');
446 get_next_string(parse_line, var_value, &str_pos, ' ');
447 cfg->platform->setvar(cfg, var_name, var_value);
453 printf("[CFG] Unknown config item %s on line %d.\n", cur_cmd, cur_line);
460 goto load_successful;
464 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
465 if (cfg->map_data[i])
466 free(cfg->map_data[i]);
467 cfg->map_data[i] = NULL;
479 int get_named_mapped_item(struct emulator_config *cfg, char *name) {
480 if (strlen(name) == 0)
483 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
484 if (cfg->map_type[i] == MAPTYPE_NONE || !cfg->map_id[i])
486 if (strcmp(name, cfg->map_id[i]) == 0)
493 int get_mapped_item_by_address(struct emulator_config *cfg, uint32_t address) {
494 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
495 if (cfg->map_type[i] == MAPTYPE_NONE || !cfg->map_data[i])
497 if (address >= cfg->map_offset[i] && address < cfg->map_high[i])