]> git.sesse.net Git - pistorm/blob - config_file/config_file.c
Add Kickstart 3.2 + more
[pistorm] / config_file / config_file.c
1 // SPDX-License-Identifier: MIT
2
3 #include "platforms/platforms.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "rominfo.h"
9
10 #define M68K_CPU_TYPES M68K_CPU_TYPE_SCC68070
11
12 const char *cpu_types[M68K_CPU_TYPES] = {
13   "68000",
14   "68010",
15   "68EC020",
16   "68020",
17   "68EC030",
18   "68030",
19   "68EC040",
20   "68LC040",
21   "68040",
22   "SCC68070",
23 };
24
25 const char *map_type_names[MAPTYPE_NUM] = {
26   "NONE",
27   "rom",
28   "ram",
29   "register",
30   "ram (no alloc)",
31 };
32
33 const char *config_item_names[CONFITEM_NUM] = {
34   "NONE",
35   "cpu",
36   "map",
37   "loopcycles",
38   "mouse",
39   "keyboard",
40   "platform",
41   "setvar",
42   "kbfile",
43 };
44
45 const char *mapcmd_names[MAPCMD_NUM] = {
46   "NONE",
47   "type",
48   "address",
49   "size",
50   "range",
51   "file",
52   "ovl",
53   "id",
54 };
55
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) {
59       return i;
60     }
61   }
62
63   return CONFITEM_NONE;
64 }
65
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]);
70       return i + 1;
71     }
72   }
73
74   printf("[CFG] Invalid CPU type %s specified, defaulting to 68000.\n", name);
75   return M68K_CPU_TYPE_68000;
76 }
77
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) {
81       return i;
82     }
83   }
84
85   return MAPCMD_UNKNOWN;
86 }
87
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) {
91       return i;
92     }
93   }
94
95   return MAPTYPE_NONE;
96 }
97
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';
101   }
102 }
103
104 unsigned int get_int(char *str) {
105   if (strlen(str) == 0)
106     return -1;
107
108   int ret_int = 0;
109
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);
114       }
115       else {
116         switch(str[i]) {
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;
126           default:
127             printf("[CFG] Unknown character %c in hex value.\n", str[i]);
128             break;
129         }
130       }
131     }
132     return ret_int;
133   }
134   else {
135     ret_int = atoi(str);
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;
142
143     return ret_int;
144   }
145 }
146
147 void get_next_string(char *str, char *str_out, int *strpos, char separator) {
148   int str_pos = 0, out_pos = 0;
149
150   if (!str_out)
151     return;
152
153   if (strpos)
154     str_pos = *strpos;
155
156   while (str[str_pos] == ' ' && str[str_pos] == '\t' && str_pos < (int)strlen(str)) {
157     str_pos++;
158   }
159
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';
164       if (strpos) {
165         *strpos = i + 1;
166       }
167       break;
168     }
169     out_pos++;
170     if (i + 1 == (int)strlen(str) && strpos) {
171       *strpos = i + 1;
172       str_out[out_pos] = '\0';
173     }
174   }
175 }
176
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;
179   FILE *in = NULL;
180
181   while (index < MAX_NUM_MAPPED_ITEMS) {
182     if (cfg->map_type[index] == MAPTYPE_NONE)
183       break;
184     index++;
185   }
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);
188     return;
189   }
190
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);
199   }
200
201   switch(type) {
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;
205       break;
206     case MAPTYPE_RAM:
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");
211         goto mapping_failed;
212       }
213       memset(cfg->map_data[index], 0x00, size);
214       break;
215     case MAPTYPE_ROM:
216       in = fopen(filename, "rb");
217       if (!in) {
218         printf("[CFG] Failed to open file %s for ROM mapping. Using onboard ROM instead, if available.\n", filename);
219         goto mapping_failed;
220       }
221       fseek(in, 0, SEEK_END);
222       file_size = (int)ftell(in);
223       if (size == 0) {
224         cfg->map_size[index] = file_size;
225         cfg->map_high[index] = addr + cfg->map_size[index];
226       }
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");
232         goto mapping_failed;
233       }
234       memset(cfg->map_data[index], 0x00, cfg->map_size[index]);
235       fread(cfg->map_data[index], cfg->rom_size[index], 1, in);
236       fclose(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]);
240       break;
241     case MAPTYPE_REGISTER:
242     default:
243       break;
244   }
245
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");
247
248   return;
249
250   mapping_failed:;
251   cfg->map_type[index] = MAPTYPE_NONE;
252   if (in)
253     fclose(in);
254 }
255
256 void free_config_file(struct emulator_config *cfg) {
257   if (!cfg) {
258     printf("[CFG] Tried to free NULL config, aborting.\n");
259   }
260
261   if (cfg->platform) {
262     cfg->platform->shutdown(cfg);
263     free(cfg->platform);
264     cfg->platform = NULL;
265   }
266
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;
271     }
272     if (cfg->map_id[i]) {
273       free(cfg->map_id[i]);
274       cfg->map_id[i] = NULL;
275     }
276   }
277   if (cfg->mouse_file) {
278     free(cfg->mouse_file);
279     cfg->mouse_file = NULL;
280   }
281   if (cfg->keyboard_file) {
282     free(cfg->keyboard_file);
283     cfg->keyboard_file = NULL;
284   }
285
286   m68k_clear_ranges();
287
288   printf("[CFG] Config file freed. Maybe.\n");
289 }
290
291 struct emulator_config *load_config_file(char *filename) {
292   FILE *in = fopen(filename, "rb");
293   if (in == NULL) {
294     printf("[CFG] Failed to open config file %s for reading.\n", filename);
295     return NULL;
296   }
297
298   char *parse_line = NULL;
299   char cur_cmd[128];
300   struct emulator_config *cfg = NULL;
301   int cur_line = 1;
302
303   parse_line = (char *)calloc(1, 512);
304   if (!parse_line) {
305     printf("[CFG] Failed to allocate memory for config file line buffer.\n");
306     return NULL;
307   }
308   cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
309   if (!cfg) {
310     printf("[CFG] Failed to allocate memory for temporary emulator config.\n");
311     goto load_failed;
312   }
313
314   memset(cfg, 0x00, sizeof(struct emulator_config));
315   cfg->cpu_type = M68K_CPU_TYPE_68000;
316
317   while (!feof(in)) {
318     int str_pos = 0;
319     memset(parse_line, 0x00, 512);
320     fgets(parse_line, 512, in);
321
322     if (strlen(parse_line) <= 2 || parse_line[0] == '#' || parse_line[0] == '/')
323       goto skip_line;
324
325     trim_whitespace(parse_line);
326
327     get_next_string(parse_line, cur_cmd, &str_pos, ' ');
328
329     switch (get_config_item_type(cur_cmd)) {
330       case CONFITEM_CPUTYPE:
331         cfg->cpu_type = get_m68k_cpu_type(parse_line + str_pos);
332         break;
333       case CONFITEM_MAP: {
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);
339
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)) {
343             case MAPCMD_TYPE:
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]);
347               break;
348             case MAPCMD_ADDRESS:
349               get_next_string(parse_line, cur_cmd, &str_pos, ' ');
350               mapaddr = get_int(cur_cmd);
351               //printf("Address! %.8X\n", mapaddr);
352               break;
353             case MAPCMD_SIZE:
354               get_next_string(parse_line, cur_cmd, &str_pos, ' ');
355               mapsize = get_int(cur_cmd);
356               //printf("Size! %.8X\n", mapsize);
357               break;
358             case MAPCMD_RANGE:
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);
364               break;
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);
369               break;
370             case MAPCMD_MAP_ID:
371               get_next_string(parse_line, cur_cmd, &str_pos, ' ');
372               strcpy(mapid, cur_cmd);
373               //printf("File! %s\n", mapfile);
374               break;
375             case MAPCMD_OVL_REMAP:
376               get_next_string(parse_line, cur_cmd, &str_pos, ' ');
377               mirraddr = get_int(cur_cmd);
378               break;
379             default:
380               printf("[CFG] Unknown/unhandled map argument %s on line %d.\n", cur_cmd, cur_line);
381               break;
382           }
383         }
384         add_mapping(cfg, maptype, mapaddr, mapsize, mirraddr, mapfile, mapid);
385
386         break;
387       }
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);
391         break;
392       case CONFITEM_MOUSE:
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);
402         break;
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");
415         printf(".\n");
416         break;
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);
422         break;
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);
432         printf("\n");
433         cfg->platform = make_platform_config(platform_name, platform_sub);
434         break;
435       }
436       case CONFITEM_SETVAR: {
437         if (!cfg->platform) {
438           printf("[CFG] Warning: setvar used in config file with no platform specified.\n");
439           break;
440         }
441
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);
448
449         break;
450       }
451       case CONFITEM_NONE:
452       default:
453         printf("[CFG] Unknown config item %s on line %d.\n", cur_cmd, cur_line);
454         break;
455     }
456
457   skip_line:
458     cur_line++;
459   }
460   goto load_successful;
461
462   load_failed:;
463   if (cfg) {
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;
468     }
469     free(cfg);
470     cfg = NULL;
471   }
472   load_successful:;
473   if (parse_line)
474     free(parse_line);
475
476   return cfg;
477 }
478
479 int get_named_mapped_item(struct emulator_config *cfg, char *name) {
480   if (strlen(name) == 0)
481     return -1;
482
483   for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) {
484     if (cfg->map_type[i] == MAPTYPE_NONE || !cfg->map_id[i])
485       continue;
486     if (strcmp(name, cfg->map_id[i]) == 0)
487       return i;
488   }
489
490   return -1;
491 }
492
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])
496       continue;
497     if (address >= cfg->map_offset[i] && address < cfg->map_high[i])
498       return i;
499   }
500
501   return -1;
502 }