An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <dirent.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include "config.h"
  10. #include "logging.h"
  11. #include "util.h"
  12. #include "version.h"
  13. /*
  14. Print command-line help/usage.
  15. */
  16. static void print_help(const char *arg1)
  17. {
  18. printf("%s [-h] [-v] [-f] [-s <n>] [<rom_path>] ...\n"
  19. "\n"
  20. "basic options:\n"
  21. " -h, --help show this help message and exit\n"
  22. " -v, --version show crater's version number and exit\n"
  23. " -f, --fullscreen enable fullscreen mode\n"
  24. " -s, --scale <n> scale the game screen by an integer factor\n"
  25. " (applies to windowed mode only)\n"
  26. " <rom_path> path to the rom file to execute; if not given, will look\n"
  27. " in the roms/ directory and prompt the user\n"
  28. "\n"
  29. "advanced options:\n"
  30. " -g, --debug display information about emulation state while running,\n"
  31. " including register and memory values; can also pause\n"
  32. " emulation, set breakpoints, and change state\n"
  33. " -a, --assemble <in> [<out>] convert z80 assembly source code into a\n"
  34. " binary file that can be run by crater\n"
  35. " -d, --disassemble <in> [<out>] convert a binary file into z80 assembly\n"
  36. " source code\n"
  37. " -r, --overwrite allow crater to write assembler output to the same\n"
  38. " filename as the input\n",
  39. arg1);
  40. }
  41. /*
  42. Print crater's version.
  43. */
  44. static void print_version()
  45. {
  46. printf("crater %s\n", CRATER_VERSION);
  47. }
  48. /*
  49. Return whether the given string ends with the given suffix.
  50. */
  51. static bool ends_with(const char *input, const char *suffix)
  52. {
  53. size_t ilen = strlen(input), slen = strlen(suffix);
  54. if (ilen < slen)
  55. return false;
  56. return strcmp(input + (ilen - slen), suffix) == 0;
  57. }
  58. /*
  59. Load all potential ROM files in roms/ into a data structure.
  60. */
  61. static int get_rom_paths(char ***path_ptr)
  62. {
  63. DIR *dirp;
  64. struct dirent *entry;
  65. char **paths = NULL, *path;
  66. int psize = 8, npaths = 0;
  67. dirp = opendir(ROMS_DIR);
  68. if (dirp) {
  69. paths = cr_malloc(sizeof(char*) * psize);
  70. while ((entry = readdir(dirp))) {
  71. path = entry->d_name;
  72. if (ends_with(path, ".gg") || ends_with(path, ".bin")) {
  73. if (npaths >= psize) {
  74. paths = cr_realloc(paths, sizeof(char*) * (psize *= 2));
  75. }
  76. paths[npaths] = cr_malloc(sizeof(char*) *
  77. (strlen(path) + strlen(ROMS_DIR) + 1));
  78. strcpy(paths[npaths], ROMS_DIR "/");
  79. strcat(paths[npaths], path);
  80. npaths++;
  81. }
  82. }
  83. closedir(dirp);
  84. } else {
  85. WARN_ERRNO("couldn't open 'roms/'")
  86. }
  87. *path_ptr = paths;
  88. return npaths;
  89. }
  90. /*
  91. Find all potential ROM files in the roms/ directory, then ask the user
  92. which one they want to run.
  93. */
  94. static char* get_rom_path_from_user()
  95. {
  96. char **paths, *path, *input = NULL;
  97. int npaths, i;
  98. long int index;
  99. size_t size = 0;
  100. ssize_t len;
  101. npaths = get_rom_paths(&paths);
  102. for (i = 0; i < npaths; i++)
  103. printf("[%2d] %s\n", i + 1, paths[i]);
  104. if (npaths)
  105. printf("Enter a ROM number from above, or the path to a ROM image: ");
  106. else
  107. printf("Enter the path to a ROM image: ");
  108. len = getline(&input, &size, stdin);
  109. if (!input)
  110. OUT_OF_MEMORY()
  111. if (len > 0 && input[len - 1] == '\n')
  112. input[len - 1] = '\0';
  113. index = strtol(input, NULL, 10);
  114. if (index < 1 || index > npaths)
  115. path = input;
  116. else
  117. path = paths[index - 1];
  118. for (i = 0; i < npaths; i++) {
  119. if (paths[i] != path)
  120. free(paths[i]);
  121. }
  122. free(paths);
  123. return path;
  124. }
  125. /*
  126. Parse the command-line arguments for any special flags.
  127. */
  128. static int parse_args(Config *config, int argc, char *argv[])
  129. {
  130. char *arg, *path;
  131. int i, paths_read = 0;
  132. for (i = 1; i < argc; i++) {
  133. arg = argv[i];
  134. if (arg[0] != '-') {
  135. // Parsing a path or other variable:
  136. if (paths_read >= 2) {
  137. ERROR("too many arguments given - emulator mode accepts one "
  138. "ROM file,\nand assembler mode accepts one input file "
  139. "and one output file")
  140. return CONFIG_EXIT_FAILURE;
  141. }
  142. path = cr_malloc(sizeof(char) * (strlen(arg) + 1));
  143. strcpy(path, arg);
  144. if (paths_read == 1) {
  145. /* If this is the second path given, it can only be an output
  146. file for the assembler. If the assembler is not enabled by
  147. subsequent arguments, we'll throw an error. */
  148. config->dst_path = path;
  149. } else {
  150. /* Otherwise, put the argument in the expected place. If we put
  151. it in rom_path and the assembler is enabled by later
  152. arguments, we'll move it. */
  153. if (config->assemble || config->disassemble)
  154. config->src_path = path;
  155. else
  156. config->rom_path = path;
  157. }
  158. paths_read++;
  159. continue;
  160. }
  161. // Parsing an option:
  162. do
  163. arg++;
  164. while (arg[0] == '-');
  165. if (!strcmp(arg, "h") || !strcmp(arg, "help")) {
  166. print_help(argv[0]);
  167. return CONFIG_EXIT_SUCCESS;
  168. } else if (!strcmp(arg, "v") || !strcmp(arg, "version")) {
  169. print_version();
  170. return CONFIG_EXIT_SUCCESS;
  171. } else if (!strcmp(arg, "f") || !strcmp(arg, "fullscreen")) {
  172. config->fullscreen = true;
  173. } else if (!strcmp(arg, "s") || !strcmp(arg, "scale")) {
  174. if (i++ >= argc) {
  175. ERROR("the scale option requires an argument")
  176. return CONFIG_EXIT_FAILURE;
  177. }
  178. arg = argv[i];
  179. long scale = strtol(arg, NULL, 10);
  180. if (scale <= 0 || scale > SCALE_MAX) {
  181. ERROR("scale factor of %s is not an integer or is out of range", arg)
  182. return CONFIG_EXIT_FAILURE;
  183. }
  184. config->scale = scale;
  185. } else if (!strcmp(arg, "g") || !strcmp(arg, "debug")) {
  186. config->debug = true;
  187. } else if (!strcmp(arg, "a") || !strcmp(arg, "assemble")) {
  188. if (paths_read >= 1) {
  189. config->src_path = config->rom_path;
  190. config->rom_path = NULL;
  191. }
  192. config->assemble = true;
  193. } else if (!strcmp(arg, "d") || !strcmp(arg, "disassemble")) {
  194. if (paths_read >= 1) {
  195. config->src_path = config->rom_path;
  196. config->rom_path = NULL;
  197. }
  198. config->disassemble = true;
  199. } else if (!strcmp(arg, "r") || !strcmp(arg, "overwrite")) {
  200. config->overwrite = true;
  201. } else {
  202. ERROR("unknown argument: %s", argv[i])
  203. return CONFIG_EXIT_FAILURE;
  204. }
  205. }
  206. if (!config->assemble && !config->disassemble && paths_read >= 2) {
  207. ERROR("too many arguments given - emulator mode accepts one ROM file")
  208. return CONFIG_EXIT_FAILURE;
  209. }
  210. if (!config->assemble && !config->disassemble && paths_read == 0) {
  211. path = get_rom_path_from_user();
  212. if (path[0] == '\0') {
  213. ERROR("no ROM image given")
  214. return CONFIG_EXIT_FAILURE;
  215. }
  216. config->rom_path = path;
  217. }
  218. return CONFIG_OK;
  219. }
  220. /*
  221. If no output file is specified for the assembler, this function picks a
  222. filename based on the input file, replacing its extension with ".gg" or
  223. ".asm" (or adding it, if none is present).
  224. */
  225. static void guess_assembler_output_file(Config* config)
  226. {
  227. char *src = config->src_path, *ptr = src + strlen(src) - 1,
  228. *ext = config->assemble ? ".gg" : ".asm";
  229. size_t until_ext = ptr - src + 1;
  230. do {
  231. if (*ptr == '.') {
  232. until_ext = ptr - src;
  233. break;
  234. }
  235. } while (ptr-- >= src);
  236. config->dst_path = cr_malloc(sizeof(char) * (until_ext + 5));
  237. strcpy(stpncpy(config->dst_path, src, until_ext), ext);
  238. }
  239. /*
  240. Ensure that the combination of arguments in a config object are valid.
  241. Some modifications are made in the case of missing arguments, like guessing
  242. the filenames for assembler output files.
  243. */
  244. static bool sanity_check(Config* config)
  245. {
  246. bool assembler = config->assemble || config->disassemble;
  247. if (config->fullscreen && config->scale > 1) {
  248. ERROR("cannot specify a scale in fullscreen mode")
  249. return false;
  250. } else if (config->assemble && config->disassemble) {
  251. ERROR("cannot assemble and disassemble at the same time")
  252. return false;
  253. } else if (assembler && (config->debug || config->fullscreen || config->scale > 1)) {
  254. ERROR("cannot specify emulator options in assembler mode")
  255. return false;
  256. } else if (assembler && !config->src_path) {
  257. ERROR("assembler mode requires an input file")
  258. return false;
  259. }
  260. if (assembler && !config->dst_path) {
  261. guess_assembler_output_file(config);
  262. }
  263. if (assembler && !config->overwrite && !strcmp(config->src_path, config->dst_path)) {
  264. ERROR("refusing to overwrite the assembler input file; pass -r to override")
  265. return false;
  266. }
  267. return true;
  268. }
  269. /*
  270. Create a new config object and load default values into it.
  271. Return value is one of CONFIG_OK, CONFIG_EXIT_SUCCESS, CONFIG_EXIT_FAILURE
  272. and indicates how the caller should proceed. If the caller should exit,
  273. then the config object should *not* be freed; otherwise it should be freed
  274. with config_destroy() when the caller is ready.
  275. */
  276. int config_create(Config** config_ptr, int argc, char* argv[])
  277. {
  278. Config *config = cr_malloc(sizeof(Config));
  279. int retval;
  280. config->debug = false;
  281. config->assemble = false;
  282. config->disassemble = false;
  283. config->fullscreen = false;
  284. config->scale = 1;
  285. config->rom_path = NULL;
  286. config->src_path = NULL;
  287. config->dst_path = NULL;
  288. config->overwrite = false;
  289. retval = parse_args(config, argc, argv);
  290. if (retval == CONFIG_OK && !sanity_check(config))
  291. retval = CONFIG_EXIT_FAILURE;
  292. if (retval != CONFIG_OK) {
  293. config_destroy(config);
  294. return retval;
  295. }
  296. *config_ptr = config;
  297. return CONFIG_OK;
  298. }
  299. /*
  300. Destroy a config object previously created with config_create().
  301. */
  302. void config_destroy(Config *config)
  303. {
  304. free(config->rom_path);
  305. free(config->src_path);
  306. free(config->dst_path);
  307. free(config);
  308. }
  309. #ifdef DEBUG_MODE
  310. /*
  311. DEBUG FUNCTION: Print out all config arguments to stdout.
  312. */
  313. void config_dump_args(const Config* config)
  314. {
  315. DEBUG("Dumping arguments:")
  316. DEBUG("- fullscreen: %s", config->fullscreen ? "true" : "false")
  317. DEBUG("- scale: %d", config->scale)
  318. DEBUG("- debug: %s", config->debug ? "true" : "false")
  319. DEBUG("- assemble: %s", config->assemble ? "true" : "false")
  320. DEBUG("- disassemble: %s", config->disassemble ? "true" : "false")
  321. DEBUG("- rom_path: %s", config->rom_path ? config->rom_path : "(null)")
  322. DEBUG("- src_path: %s", config->src_path ? config->src_path : "(null)")
  323. DEBUG("- dst_path: %s", config->dst_path ? config->dst_path : "(null)")
  324. DEBUG("- overwrite: %s", config->overwrite ? "true" : "false")
  325. }
  326. #endif