ref: 0be19c0dfa3a9b752905f8002b2018aabfe810cd
parent: 1599df1dd34f5ec106d00ac0da0182c31480d1f7
author: allkern <lisandroaalarcon@gmail.com>
date: Sun Jun 18 19:32:27 EDT 2023
Keep working on CLI, TOML
--- /dev/null
+++ b/frontend/config.c
@@ -1,0 +1,261 @@
+#include <string.h>
+#include <stdlib.h>
+
+#include "config.h"
+
+#ifndef OS_INFO
+#define OS_INFO unknown
+#endif
+#ifndef REP_VERSION
+#define REP_VERSION latest
+#endif
+#ifndef REP_COMMIT_HASH
+#define REP_COMMIT_HASH latest
+#endif
+
+#define STR1(m) #m
+#define STR(m) STR1(m)
+
+static const char* g_version_text =
+#ifdef _WIN32
+ "psxe.exe (" STR(OS_INFO) ") " STR(REP_VERSION) "-" STR(REP_COMMIT_HASH) "\n"+#elif __linux__
+ "psxe (" STR(OS_INFO) ") " STR(REP_VERSION) "-" STR(REP_COMMIT_HASH) "\n"+#else
+ "psxe (" STR(OS_INFO) ") " STR(REP_VERSION) "-" STR(REP_COMMIT_HASH) "\n"+#endif
+ "\nPSXE - A simple, fast and portable Sony PlayStation emulator.\n\n"
+ "Copyright (C) 2023 Allkern (Lisandro Alarcon)\n"
+ "This is free software; see the source for copying conditions. There is NO\n"
+ "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
+
+static const char g_default_settings[] =
+ "# Settings file generated by PSXE\n"
+ "\n"
+ "# Don't change please, reserved for future use\n"
+ "psxe_version = \"" STR(REP_VERSION) "\"\n"
+ "\n"
+ "# BIOS-related settings:\n"
+ "[bios]\n"
+ " search_path = \"bios\"\n"
+ " preferred_model = \"SCPH-1001\"\n"
+ " override_file = \"\"\n"
+ "\n"
+ "# Console settings\n"
+ "[console]\n"
+ " region = \"auto\"\n";
+
+
+static const char* g_models_text =
+ "Available console models:\n"
+ "\"scph1000\" (SCPH-1000) [NTSC-J]\n"
+ "\"scph1001\" (SCPH-1001) [NTSC-U/C]\n"
+ "\"scph1002\" (SCPH-1002) [PAL]\n"
+ "\"scph3000\" (SCPH-3000) [NTSC-J]\n"
+ "\"scph3500\" (SCPH-3500) [NTSC-J]\n"
+ "\"scph5000\" (SCPH-5000) [NTSC-U/C]\n"
+ "\"scph5501\" (SCPH-5501) [NTSC-U/C]\n"
+ "\"scph5500\" (SCPH-5500) [NTSC-J]\n"
+ "\"scph5502\" (SCPH-5502) [PAL]\n"
+ "\"scph5552\" (SCPH-5552) [PAL]\n"
+ "\"scph7000\" (SCPH-7000) [NTSC-J]\n"
+ "\"scph7001\" (SCPH-7001) [NTSC-U/C]\n"
+ "\"scph7002\" (SCPH-7002) [PAL]\n"
+ "\"scph7003\" (SCPH-7003) [NTSC-J]\n"
+ "\"scph7501\" (SCPH-7501) [NTSC]\n"
+ "\"scph7502\" (SCPH-7502) [PAL]\n"
+ "\"scph9002\" (SCPH-9002) [PAL]\n"
+ "\"scph100\" (SCPH-100) [NTSC-J]\n"
+ "\"scph101\" (SCPH-101) [NTSC-U/C]\n"
+ "\"scph102a\" (SCPH-102A) [PAL]\n"
+ "\"scph102b\" (SCPH-102B) [PAL]\n"
+ "\"scph102c\" (SCPH-102C) [?]\n";
+
+static const char* g_regions_text =
+ "Available region options: \"ntsc\", \"pal\", \"auto\"\n";
+
+static const char* g_desc_text =
+ "\nPlease report any bugs to <https://github.com/allkern/psxe/issues>\n";
+
+
+psxe_config_t* psxe_cfg_create() {+ return (psxe_config_t*)malloc(sizeof(psxe_config_t));
+}
+
+void psxe_cfg_init(psxe_config_t* cfg) {+ memset(cfg, 0, sizeof(psxe_config_t));
+}
+
+void psxe_cfg_load_defaults(psxe_config_t* cfg) {+ cfg->bios = NULL;
+ cfg->bios_search = "bios";
+ cfg->exe = NULL;
+ cfg->help_model = 0;
+ cfg->help_region = 0;
+ cfg->model = "scph1001";
+ cfg->psxe_version = STR(REP_VERSION);
+ cfg->region = "ntsc";
+ cfg->settings_path = NULL;
+ cfg->use_args = 0;
+ cfg->version = 0;
+}
+
+void psxe_cfg_load(psxe_config_t* cfg, int argc, const char* argv) {+ log_set_level(LOG_INFO);
+
+ int use_args = 0;
+ int version = 0;
+ int help_model = 0;
+ int help_region = 0;
+ const char* settings_path = NULL;
+ const char* bios = NULL;
+ const char* bios_search = NULL;
+ const char* model = NULL;
+ const char* exe = NULL;
+ const char* region = NULL;
+ const char* psxe_version = NULL;
+
+ static const char *const usages[] = {+ "psxe [options]",
+ NULL,
+ };
+
+ struct argparse_option options[] = {+ OPT_BOOLEAN ('h', "help" , NULL , "Display this information", argparse_help_cb, 0, 0),+ OPT_BOOLEAN (0 , "help-model" , &help_model , "Display available console models", NULL, 0, 0),
+ OPT_BOOLEAN (0 , "help-region" , &help_region , "Display available region options", NULL, 0, 0),
+ OPT_BOOLEAN ('v', "version" , &version , "Display version and build information", NULL, 0, 0),+ OPT_BOOLEAN ('a', "use-args" , &use_args , "Ignore settings file, use CLI args instead", NULL, 0, 0),+ OPT_STRING ('S', "settings-file", &settings_path, "Specify settings file path", NULL, 0, 0),+ OPT_BOOLEAN ('b', "bios" , &bios , "Use this BIOS file (ignores -B, -M)", NULL, 0, 0),+ OPT_BOOLEAN ('B', "bios-folder" , &bios_search , "Specify a BIOS search folder", NULL, 0, 0),+ OPT_STRING ('M', "model" , &model , "Specify console model (SPCH-XXXX)", NULL, 0, 0),+ OPT_STRING ('r', "region" , ®ion , "Specify console region"),+ OPT_STRING ('x', "exe" , &exe , "Boot this PS-X EXE file (ignores CDROM image)"),+ OPT_END()
+ };
+
+ struct argparse argparse;
+
+ argparse_init(&argparse, options, usages, 0);
+ argparse_describe(&argparse, NULL, g_desc_text);
+
+ argc = argparse_parse(&argparse, argc, argv);
+
+ if (help_model) {+ printf("%s\n", g_models_text);+
+ exit(0);
+ }
+
+ if (help_region) {+ printf("%s\n", g_regions_text);+
+ exit(0);
+ }
+
+ if (version) {+ printf("%s\n", g_version_text);+
+ exit(0);
+ }
+
+ if (!use_args) {+ if (!settings_path)
+ settings_path = "settings.toml";
+
+ FILE* settings = fopen(settings_path, "rb");
+
+ char error[0x100];
+
+ if (!settings) {+ settings = fopen("settings.toml", "w+b");+
+ fwrite(g_default_settings, 1, sizeof(g_default_settings) - 1, settings);
+
+ fseek(settings, 0, 0);
+ }
+
+ log_info("Parsing settings file...");+
+ toml_table_t* conf = toml_parse_file(settings, error, sizeof(error));
+
+ if (!conf) {+ log_error("Couldn't parse settings file");+
+ exit(1);
+ }
+
+ toml_datum_t s_version = toml_string_in(conf, "psxe_version");
+
+ if (!s_version.ok) {+ log_error("Settings file lacking version number");+
+ exit(1);
+ }
+
+ toml_table_t* s_bios_table = toml_table_in(conf, "bios");
+
+ if (s_bios_table) {+ toml_datum_t s_bios_search_path = toml_string_in(s_bios_table, "search_path");
+
+ if (s_bios_search_path.ok)
+ bios_search = s_bios_search_path.u.s;
+
+ toml_datum_t s_bios_preferred_model = toml_string_in(s_bios_table, "preferred_model");
+
+ if (s_bios_preferred_model.ok)
+ model = s_bios_preferred_model.u.s;
+
+ toml_datum_t s_bios_override_file = toml_string_in(s_bios_table, "override_file");
+
+ if (s_bios_override_file.ok)
+ bios = s_bios_override_file.u.s;
+ }
+
+ toml_table_t* s_console_table = toml_table_in(conf, "console");
+
+ if (s_console_table) {+ toml_datum_t s_console_region = toml_string_in(s_console_table, "region");
+
+ if (s_console_region.ok)
+ region = s_console_region.u.s;
+ }
+
+ psxe_version = s_version.u.s;
+
+ log_info("Settings file parsed. PSXE version: %s", psxe_version);+
+ fclose(settings);
+ }
+
+ if (bios)
+ cfg->bios = bios;
+
+ if (bios_search)
+ cfg->bios_search = bios_search;
+
+ if (model)
+ cfg->model = model;
+
+ if (exe)
+ cfg->exe = exe;
+
+ if (region)
+ cfg->region = region;
+
+ if (psxe_version)
+ cfg->psxe_version = psxe_version;
+}
+
+char* psxe_cfg_get_bios_path(psxe_config_t* cfg) {+ if (cfg->bios) {+ return cfg->bios;
+ }
+
+ if (!cfg->bios_search)
+ return NULL;
+}
+
+#undef STR1
+#undef STR
\ No newline at end of file
--- /dev/null
+++ b/frontend/config.h
@@ -1,0 +1,31 @@
+#ifndef CONFIG_H
+#define CONFIG_H
+
+#include <stdlib.h>
+
+#include "argparse.h"
+#include "toml.h"
+#include "psx/log.h"
+
+typedef struct {+ int use_args;
+ int version;
+ int help_model;
+ int help_region;
+ const char* settings_path;
+ const char* bios;
+ const char* bios_search;
+ const char* model;
+ const char* exe;
+ const char* region;
+ const char* psxe_version;
+} psxe_config_t;
+
+psxe_config_t* psxe_cfg_create();
+void psxe_cfg_init(psxe_config_t*);
+void psxe_cfg_load_defaults(psxe_config_t*);
+void psxe_cfg_load(psxe_config_t*, int, const char**);
+char* psxe_cfg_get_bios_path(psxe_config_t*);
+void psxe_cfg_destroy(psxe_config_t*);
+
+#endif
\ No newline at end of file
--- a/frontend/main.c
+++ b/frontend/main.c
@@ -1,88 +1,12 @@
#include "psx/psx.h"
-#include "psx/config.h"
#include "screen.h"
-#include "argparse.h"
-#include "toml.h"
+#include "config.h"
#include <signal.h>
#undef main
-#ifndef OS_INFO
-#define OS_INFO unknown
-#endif
-#ifndef REP_VERSION
-#define REP_VERSION latest
-#endif
-#ifndef REP_COMMIT_HASH
-#define REP_COMMIT_HASH latest
-#endif
-
-#define STR1(m) #m
-#define STR(m) STR1(m)
-
-static const char* g_version_text =
-#ifdef _WIN32
- "psxe.exe (" STR(OS_INFO) ") " STR(REP_VERSION) "-" STR(REP_COMMIT_HASH) "\n"-#elif __linux__
- "psxe (" STR(OS_INFO) ") " STR(REP_VERSION) "-" STR(REP_COMMIT_HASH) "\n"-#else
- "psxe (" STR(OS_INFO) ") " STR(REP_VERSION) "-" STR(REP_COMMIT_HASH) "\n"-#endif
- "\nPSXE - A simple, fast and portable Sony PlayStation emulator.\n\n"
- "Copyright (C) 2023 Allkern (Lisandro Alarcon)\n"
- "This is free software; see the source for copying conditions. There is NO\n"
- "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
-
-static const char g_default_settings[] =
- "# Settings file generated by PSXE\n"
- "\n"
- "# Don't change please, reserved for future use\n"
- "psxe_version = \"" STR(REP_VERSION) "\"\n"
- "\n"
- "# BIOS-related settings:\n"
- "[bios]\n"
- " search_path = \"bios\"\n"
- " preferred_model = \"SCPH-1001\"\n"
- " override_file = \"\"\n"
- "\n"
- "# Console settings\n"
- "[console]\n"
- " region = \"auto\"\n";
-
-
-static const char* g_models_text =
- "Available console models:\n"
- "\"scph1000\" (SCPH-1000) [NTSC-J]\n"
- "\"scph1001\" (SCPH-1001) [NTSC-U/C]\n"
- "\"scph1002\" (SCPH-1002) [PAL]\n"
- "\"scph3000\" (SCPH-3000) [NTSC-J]\n"
- "\"scph3500\" (SCPH-3500) [NTSC-J]\n"
- "\"scph5000\" (SCPH-5000) [NTSC-U/C]\n"
- "\"scph5501\" (SCPH-5501) [NTSC-U/C]\n"
- "\"scph5500\" (SCPH-5500) [NTSC-J]\n"
- "\"scph5502\" (SCPH-5502) [PAL]\n"
- "\"scph5552\" (SCPH-5552) [PAL]\n"
- "\"scph7000\" (SCPH-7000) [NTSC-J]\n"
- "\"scph7001\" (SCPH-7001) [NTSC-U/C]\n"
- "\"scph7002\" (SCPH-7002) [PAL]\n"
- "\"scph7003\" (SCPH-7003) [NTSC-J]\n"
- "\"scph7501\" (SCPH-7501) [NTSC]\n"
- "\"scph7502\" (SCPH-7502) [PAL]\n"
- "\"scph9002\" (SCPH-9002) [PAL]\n"
- "\"scph100\" (SCPH-100) [NTSC-J]\n"
- "\"scph101\" (SCPH-101) [NTSC-U/C]\n"
- "\"scph102a\" (SCPH-102A) [PAL]\n"
- "\"scph102b\" (SCPH-102B) [PAL]\n"
- "\"scph102c\" (SCPH-102C) [?]\n";
-
-static const char* g_regions_text =
- "Available region options: \"ntsc\", \"pal\", \"auto\"\n";
-
-static const char* g_desc_text =
- "\nPlease report any bugs to <https://github.com/allkern/psxe/issues>\n";
-
psx_cpu_t* g_cpu;
void sigint_handler(int sig) {@@ -100,135 +24,12 @@
}
int main(int argc, const char* argv[]) {- log_set_level(LOG_INFO);
+ psxe_config_t* cfg = psxe_cfg_create();
- int use_args = 0;
- int version = 0;
- int help_model = 0;
- int help_region = 0;
- const char* settings_path = NULL;
- const char* bios = NULL;
- const char* bios_search = NULL;
- const char* model = NULL;
- const char* exe = NULL;
- const char* region = NULL;
- const char* psxe_version = NULL;
-
- static const char *const usages[] = {- "basic [options]",
- NULL,
- };
-
- struct argparse_option options[] = {- OPT_BOOLEAN ('h', "help" , NULL , "Display this information", argparse_help_cb, 0, 0),- OPT_BOOLEAN (0 , "help-model" , &help_model , "Display available console models", NULL, 0, 0),
- OPT_BOOLEAN (0 , "help-region" , &help_region , "Display available region options", NULL, 0, 0),
- OPT_BOOLEAN ('v', "version" , &version , "Display version and build information", NULL, 0, 0),- OPT_BOOLEAN ('a', "use-args" , &use_args , "Ignore settings file, use CLI args instead", NULL, 0, 0),- OPT_STRING ('S', "settings-file", &settings_path, "Specify settings file path", NULL, 0, 0),- OPT_GROUP("BIOS options"),- OPT_BOOLEAN ('b', "bios" , &bios , "Use this BIOS file (ignores -B, -M)", NULL, 0, 0),- OPT_BOOLEAN ('B', "bios-folder" , &bios_search , "Specify a BIOS search folder", NULL, 0, 0),- OPT_GROUP("Console options"),- OPT_STRING ('M', "model" , &model , "Specify console model (SPCH-XXXX)", NULL, 0, 0),- OPT_STRING ('r', "region" , ®ion , "Specify console region"),- OPT_STRING ('x', "exe" , &exe , "Boot this PS-X EXE file (ignores CDROM image)"),- OPT_END()
- };
-
- struct argparse argparse;
-
- argparse_init(&argparse, options, usages, 0);
- argparse_describe(&argparse, NULL, g_desc_text);
-
- argc = argparse_parse(&argparse, argc, argv);
-
- if (help_model) {- printf("%s\n", g_models_text);-
- exit(0);
- }
-
- if (help_region) {- printf("%s\n", g_regions_text);-
- exit(0);
- }
-
- if (version) {- printf("%s\n", g_version_text);-
- exit(0);
- }
-
- if (!use_args) {- if (!settings_path)
- settings_path = "settings.toml";
-
- FILE* settings = fopen(settings_path, "rb");
-
- char error[0x100];
-
- if (!settings) {- settings = fopen("settings.toml", "w+b");-
- fwrite(g_default_settings, 1, sizeof(g_default_settings) - 1, settings);
-
- fseek(settings, 0, 0);
- }
-
- log_info("Parsing settings file...");-
- toml_table_t* conf = toml_parse_file(settings, error, sizeof(error));
-
- if (!conf) {- log_error("Couldn't parse settings file");-
- exit(1);
- }
-
- toml_datum_t s_version = toml_string_in(conf, "psxe_version");
-
- if (!s_version.ok) {- log_error("Settings file lacking version number");-
- exit(1);
- }
-
- toml_table_t* s_bios_table = toml_table_in(conf, "bios");
-
- if (s_bios_table) {- toml_datum_t s_bios_search_path = toml_string_in(s_bios_table, "search_path");
-
- if (s_bios_search_path.ok)
- bios_search = s_bios_search_path.u.s;
-
- toml_datum_t s_bios_preferred_model = toml_string_in(s_bios_table, "preferred_model");
-
- if (s_bios_preferred_model.ok)
- model = s_bios_preferred_model.u.s;
-
- toml_datum_t s_bios_override_file = toml_string_in(s_bios_table, "override_file");
-
- if (s_bios_override_file.ok)
- bios = s_bios_override_file.u.s;
- }
-
- toml_table_t* s_console_table = toml_table_in(conf, "console");
-
- if (s_console_table) {- toml_datum_t s_console_region = toml_string_in(s_console_table, "region");
-
- if (s_console_region.ok)
- region = s_console_region.u.s;
- }
-
- psxe_version = s_version.u.s;
-
- log_info("Settings file parsed. PSXE version: %s", psxe_version);-
- fclose(settings);
- }
+ psxe_cfg_init(cfg);
+ psxe_cfg_load_defaults(cfg);
+ psxe_cfg_load(cfg, argc, argv);
+
log_set_level(LOG_FATAL);
--- a/frontend/settings.h
+++ /dev/null
@@ -1,25 +1,0 @@
-#ifndef SETTINGS_H
-#define SETTINGS_H
-
-#include <stdlib.h>
-
-typedef struct {- int use_args;
- int version;
- int help_model;
- int help_region;
- const char* settings_path;
- const char* bios;
- const char* bios_search;
- const char* model;
- const char* exe;
- const char* region;
- const char* psxe_version;
-} psxe_settings_t;
-
-psxe_settings_t* psxe_settings_create();
-void psxe_settings_init(psxe_settings_t*);
-void psxe_settings_load_defaults(psxe_settings_t*);
-char* psxe_settings_get_bios_path(psxe_settings_t*);
-
-#endif
\ No newline at end of file
--
⑨