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] = {
32 const char *config_item_names[CONFITEM_NUM] = {
44 const char *mapcmd_names[MAPCMD_NUM] = {
55 int get_config_item_type(char *cmd) {
56 for (int i = 0; i < CONFITEM_NUM; i++) {
57 if (strcmp(cmd, config_item_names[i]) == 0) {
65 unsigned int get_m68k_cpu_type(char *name) {
66 for (int i = 0; i < M68K_CPU_TYPES; i++) {
67 if (strcmp(name, cpu_types[i]) == 0) {
68 printf("[CFG] Set CPU type to %s.\n", cpu_types[i]);
73 printf("[CFG] Invalid CPU type %s specified, defaulting to 68000.\n", name);
74 return M68K_CPU_TYPE_68000;
77 unsigned int get_map_cmd(char *name) {
78 for (int i = 1; i < MAPCMD_NUM; i++) {
79 if (strcmp(name, mapcmd_names[i]) == 0) {
84 return MAPCMD_UNKNOWN;
87 unsigned int get_map_type(char *name) {
88 for (int i = 1; i < MAPTYPE_NUM; i++) {
89 if (strcmp(name, map_type_names[i]) == 0) {
97 void trim_whitespace(char *str) {
98 while (strlen(str) != 0 && (str[strlen(str) - 1] == ' ' || str[strlen(str) - 1] == '\t' || str[strlen(str) - 1] == 0x0A || str[strlen(str) - 1] == 0x0D)) {
99 str[strlen(str) - 1] = '\0';
103 unsigned int get_int(char *str) {
104 if (strlen(str) == 0)
109 if (strlen(str) > 2 && str[0] == '0' && str[1] == 'x') {
110 for (int i = 2; i < (int)strlen(str); i++) {
111 if (str[i] >= '0' && str[i] <= '9') {
112 ret_int = (str[i] - '0') | (ret_int << 4);
116 case 'A': ret_int = 0xA | (ret_int << 4); break;
117 case 'B': ret_int = 0xB | (ret_int << 4); break;
118 case 'C': ret_int = 0xC | (ret_int << 4); break;
119 case 'D': ret_int = 0xD | (ret_int << 4); break;
120 case 'E': ret_int = 0xE | (ret_int << 4); break;
121 case 'F': ret_int = 0xF | (ret_int << 4); break;
122 case 'K': ret_int = ret_int * SIZE_KILO; break;
123 case 'M': ret_int = ret_int * SIZE_MEGA; break;
124 case 'G': ret_int = ret_int * SIZE_GIGA; break;
126 printf("[CFG] Unknown character %c in hex value.\n", str[i]);
135 if (str[strlen(str) - 1] == 'K')
136 ret_int = ret_int * SIZE_KILO;
137 else if (str[strlen(str) - 1] == 'M')
138 ret_int = ret_int * SIZE_MEGA;
139 else if (str[strlen(str) - 1] == 'G')
140 ret_int = ret_int * SIZE_GIGA;
146 void get_next_string(char *str, char *str_out, int *strpos, char separator) {
147 int str_pos = 0, out_pos = 0;
155 while (str[str_pos] == ' ' && str[str_pos] == '\t' && str_pos < (int)strlen(str)) {
159 for (int i = str_pos; i < (int)strlen(str); i++) {
160 str_out[out_pos] = str[i];
161 if ((separator == ' ' && (str[i] == ' ' || str[i] == '\t')) || str[i] == separator) {
162 str_out[out_pos] = '\0';
169 if (i + 1 == (int)strlen(str) && strpos) {
171 str_out[out_pos] = '\0';
176 void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id) {
177 unsigned int index = 0, file_size = 0;
180 while (index < MAX_NUM_MAPPED_ITEMS) {
181 if (cfg->map_type[index] == MAPTYPE_NONE)
185 if (index == MAX_NUM_MAPPED_ITEMS) {
186 printf("[CFG] Unable to map item, only %d items can be mapped with current binary.\n", MAX_NUM_MAPPED_ITEMS);
190 cfg->map_type[index] = type;
191 cfg->map_offset[index] = addr;
192 cfg->map_size[index] = size;
193 cfg->map_high[index] = addr + size;
194 cfg->map_mirror[index] = mirr_addr;
195 if (strlen(map_id)) {
196 cfg->map_id[index] = (char *)malloc(strlen(map_id) + 1);
197 strcpy(cfg->map_id[index], map_id);
202 printf("[CFG] Allocating %d bytes for RAM mapping (%d MB)...\n", size, size / 1024 / 1024);
203 cfg->map_data[index] = (unsigned char *)malloc(size);
204 if (!cfg->map_data[index]) {
205 printf("[CFG] ERROR: Unable to allocate memory for mapped RAM!\n");
208 memset(cfg->map_data[index], 0x00, size);
211 in = fopen(filename, "rb");
213 printf("[CFG] Failed to open file %s for ROM mapping. Using onboard ROM instead, if available.\n", filename);
216 fseek(in, 0, SEEK_END);
217 file_size = (int)ftell(in);
219 cfg->map_size[index] = file_size;
220 cfg->map_high[index] = addr + cfg->map_size[index];
222 fseek(in, 0, SEEK_SET);
223 cfg->map_data[index] = (unsigned char *)calloc(1, cfg->map_size[index]);
224 cfg->rom_size[index] = (cfg->map_size[index] <= file_size) ? cfg->map_size[index] : file_size;
225 if (!cfg->map_data[index]) {
226 printf("[CFG] ERROR: Unable to allocate memory for mapped ROM!\n");
229 memset(cfg->map_data[index], 0x00, cfg->map_size[index]);
230 fread(cfg->map_data[index], cfg->rom_size[index], 1, in);
232 displayRomInfo(cfg->map_data[index]);
234 case MAPTYPE_REGISTER:
239 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");
240 if (cfg->map_size[index] == cfg->rom_size[index])
241 m68k_add_rom_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]);
246 cfg->map_type[index] = MAPTYPE_NONE;
251 void free_config_file(struct emulator_config *cfg) {
253 printf("[CFG] Tried to free NULL config, aborting.\n");
257 cfg->platform->shutdown(cfg);
259 cfg->platform = NULL;
262 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
263 if (cfg->map_data[i]) {
264 free(cfg->map_data[i]);
265 cfg->map_data[i] = NULL;
267 if (cfg->map_id[i]) {
268 free(cfg->map_id[i]);
269 cfg->map_id[i] = NULL;
272 if (cfg->mouse_file) {
273 free(cfg->mouse_file);
274 cfg->mouse_file = NULL;
276 if (cfg->keyboard_file) {
277 free(cfg->keyboard_file);
278 cfg->keyboard_file = NULL;
283 printf("[CFG] Config file freed. Maybe.\n");
286 struct emulator_config *load_config_file(char *filename) {
287 FILE *in = fopen(filename, "rb");
289 printf("[CFG] Failed to open config file %s for reading.\n", filename);
293 char *parse_line = NULL;
295 struct emulator_config *cfg = NULL;
298 parse_line = (char *)calloc(1, 512);
300 printf("[CFG] Failed to allocate memory for config file line buffer.\n");
303 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
305 printf("[CFG] Failed to allocate memory for temporary emulator config.\n");
309 memset(cfg, 0x00, sizeof(struct emulator_config));
310 cfg->cpu_type = M68K_CPU_TYPE_68000;
314 memset(parse_line, 0x00, 512);
315 fgets(parse_line, 512, in);
317 if (strlen(parse_line) <= 2 || parse_line[0] == '#' || parse_line[0] == '/')
320 trim_whitespace(parse_line);
322 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
324 switch (get_config_item_type(cur_cmd)) {
325 case CONFITEM_CPUTYPE:
326 cfg->cpu_type = get_m68k_cpu_type(parse_line + str_pos);
329 unsigned int maptype = 0, mapsize = 0, mapaddr = 0;
330 unsigned int mirraddr = ((unsigned int)-1);
331 char mapfile[128], mapid[128];
332 memset(mapfile, 0x00, 128);
333 memset(mapid, 0x00, 128);
335 while (str_pos < (int)strlen(parse_line)) {
336 get_next_string(parse_line, cur_cmd, &str_pos, '=');
337 switch(get_map_cmd(cur_cmd)) {
339 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
340 maptype = get_map_type(cur_cmd);
341 //printf("Type! %s\n", map_type_names[maptype]);
344 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
345 mapaddr = get_int(cur_cmd);
346 //printf("Address! %.8X\n", mapaddr);
349 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
350 mapsize = get_int(cur_cmd);
351 //printf("Size! %.8X\n", mapsize);
354 get_next_string(parse_line, cur_cmd, &str_pos, '-');
355 mapaddr = get_int(cur_cmd);
356 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
357 mapsize = get_int(cur_cmd) - 1 - mapaddr;
358 //printf("Range! %d-%d\n", mapaddr, mapaddr + mapsize);
360 case MAPCMD_FILENAME:
361 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
362 strcpy(mapfile, cur_cmd);
363 //printf("File! %s\n", mapfile);
366 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
367 strcpy(mapid, cur_cmd);
368 //printf("File! %s\n", mapfile);
370 case MAPCMD_OVL_REMAP:
371 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
372 mirraddr = get_int(cur_cmd);
375 printf("[CFG] Unknown/unhandled map argument %s on line %d.\n", cur_cmd, cur_line);
379 add_mapping(cfg, maptype, mapaddr, mapsize, mirraddr, mapfile, mapid);
383 case CONFITEM_LOOPCYCLES:
384 cfg->loop_cycles = get_int(parse_line + str_pos);
385 printf("[CFG] Set CPU loop cycles to %d.\n", cfg->loop_cycles);
388 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
389 cfg->mouse_file = (char *)calloc(1, strlen(cur_cmd) + 1);
390 strcpy(cfg->mouse_file, cur_cmd);
391 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
392 cfg->mouse_toggle_key = cur_cmd[0];
393 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
394 cfg->mouse_autoconnect = (strcmp(cur_cmd, "autoconnect") == 0) ? 1 : 0;
395 cfg->mouse_enabled = 1;
396 printf("[CFG] Enabled mouse event forwarding from file %s, toggle key %c.\n", cfg->mouse_file, cfg->mouse_toggle_key);
398 case CONFITEM_KEYBOARD:
399 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
400 cfg->keyboard_toggle_key = cur_cmd[0];
401 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
402 cfg->keyboard_grab = (strcmp(cur_cmd, "grab") == 0) ? 1 : 0;
403 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
404 cfg->keyboard_autoconnect = (strcmp(cur_cmd, "autoconnect") == 0) ? 1 : 0;
405 printf("[CFG] Enabled keyboard event forwarding, toggle key %c", cfg->keyboard_toggle_key);
406 if (cfg->keyboard_grab)
407 printf(", locking from host when connected");
408 if (cfg->keyboard_autoconnect)
409 printf(", connected to guest at startup");
412 case CONFITEM_KBFILE:
413 get_next_string(parse_line, cur_cmd, &str_pos, ' ');
414 cfg->keyboard_file = (char *)calloc(1, strlen(cur_cmd) + 1);
415 strcpy(cfg->keyboard_file, cur_cmd);
416 printf("[CFG] Set keyboard event source file to %s.\n", cfg->keyboard_file);
418 case CONFITEM_PLATFORM: {
419 char platform_name[128], platform_sub[128];
420 memset(platform_name, 0x00, 128);
421 memset(platform_sub, 0x00, 128);
422 get_next_string(parse_line, platform_name, &str_pos, ' ');
423 printf("[CFG] Setting platform to %s", platform_name);
424 get_next_string(parse_line, platform_sub, &str_pos, ' ');
425 if (strlen(platform_sub))
426 printf(" (sub: %s)", platform_sub);
428 cfg->platform = make_platform_config(platform_name, platform_sub);
431 case CONFITEM_SETVAR: {
432 if (!cfg->platform) {
433 printf("[CFG] Warning: setvar used in config file with no platform specified.\n");
437 char var_name[128], var_value[128];
438 memset(var_name, 0x00, 128);
439 memset(var_value, 0x00, 128);
440 get_next_string(parse_line, var_name, &str_pos, ' ');
441 get_next_string(parse_line, var_value, &str_pos, ' ');
442 cfg->platform->setvar(cfg, var_name, var_value);
448 printf("[CFG] Unknown config item %s on line %d.\n", cur_cmd, cur_line);
455 goto load_successful;
459 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
460 if (cfg->map_data[i])
461 free(cfg->map_data[i]);
462 cfg->map_data[i] = NULL;
474 int get_named_mapped_item(struct emulator_config *cfg, char *name) {
475 if (strlen(name) == 0)
478 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
479 if (cfg->map_type[i] == MAPTYPE_NONE || !cfg->map_id[i])
481 if (strcmp(name, cfg->map_id[i]) == 0)
488 int get_mapped_item_by_address(struct emulator_config *cfg, uint32_t address) {
489 for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
490 if (cfg->map_type[i] == MAPTYPE_NONE || !cfg->map_data[i])
492 if (address >= cfg->map_offset[i] && address < cfg->map_high[i])