--- nono/lib/mainapp.cpp 2026/04/29 17:05:22 1.1.1.16 +++ nono/lib/mainapp.cpp 2026/04/29 17:05:26 1.1.1.17 @@ -27,6 +27,7 @@ #include #include #include +#include #if defined(__linux__) #include #endif @@ -52,6 +53,8 @@ MainApp::~MainApp() pMonitorManager.reset(); gMonitorManager = NULL; + hostcpu.reset(); + #if 0 // 確認用 if (objects.empty() == false) { printf("~MainApp: undead objects are:"); @@ -103,7 +106,6 @@ MainApp::ShowHelp(bool all) const p("-b [,][,]"); p(" set breakpoint. is either 'main'(default) or 'xp'"); p("--bi-exg set instruction breakpoint on 'exg sp,sp'"); - p("-B benchmark mode"); p("-C output log to console"); p("-d debugger prompt on startup"); p("-D same as '-V debugger-driver=stdio'"); @@ -216,10 +218,11 @@ MainApp::Init1a(int ac, char *av[]) // c2. VM ディレクトリに設定ファイルがあれば読み込んで設定を更新。 // vmfile が存在するかどうかはここで調べるまで分からない。 ConfigFile cfgfile(vmfile, ConfigItem::FromConfig); - if (cfgfile.Load()) { - if (gConfig->Update(cfgfile) == false) { - return EXIT_FAILURE; - } + if (cfgfile.Load() == false) { + return EXIT_FAILURE; + } + if (gConfig->Update(cfgfile) == false) { + return EXIT_FAILURE; } // c3. 最後にコマンドライン引数で更新 @@ -240,9 +243,13 @@ MainApp::Init1a(int ac, char *av[]) const ConfigItem& itemarg = gConfig->Find("exec-arg"); exec_arg = itemarg.AsString(); - // MSX-DOS モードならここでいくつかパラメータを強制的に変更。 + // Human68k/MSX-DOS モードならここでいくつかパラメータを強制的に変更。 + if (human_mode) { + // X68030 最小構成でいい。 + gConfig->Update("vmtype=x68030", "-H"); + } if (msxdos_mode) { - // LUNA-I 最小構成でいい + // LUNA-I 最小構成でいい。 gConfig->Update("vmtype=luna", "-S"); gConfig->Update("ram-size=16", "-S"); gConfig->Update("luna-video-plane=1", "-S"); @@ -251,6 +258,9 @@ MainApp::Init1a(int ac, char *av[]) gConfig->Update("hostnet-driver=none", "-S"); } + // ログ表示のためだけのオブジェクト + hostcpu.reset(new Object(OBJ_HOSTCPU)); + // ホスト CPU の機能チェック。 if (CheckCPU() == false) { return EXIT_FAILURE; @@ -268,7 +278,7 @@ MainApp::Init1a(int ac, char *av[]) vmtype = VMType::LUNA88K; } else if (vmstr == "news") { vmtype = VMType::NEWS; - } else if (vmstr == "virt68k") { + } else if (vmstr == "virt-m68k" || vmstr == "virt68k") { vmtype = VMType::VIRT68K; } else { if (item.GetFrom() == ConfigItem::FromInitial) { @@ -433,32 +443,22 @@ MainApp::ParseOpt(int ac, char *av[]) { struct stat st; const char *cpath; - int b; int c; - cpath = "."; + cpath = NULL; - while ((c = getopt_long(ac, av, "b:B:c:CdDfhHL:M:s:SvV:X:", + while ((c = getopt_long(ac, av, "b:c:CdDfhHL:M:s:SvV:X:", longopts, NULL)) != -1) { switch (c) { case 'b': debug_breakaddr.push_back(optarg); break; - case 'B': - b = atoi(optarg); - if (b < 2 || b > 6) { - fprintf(stderr, "-B : 2..6\n"); - return EXIT_FAILURE; - } - benchmark_mode = b; - break; - case 'c': cpath = optarg; // 空文字列なら再び初期値に if (cpath[0] == '\0') { - cpath = "."; + cpath = NULL; } break; @@ -571,30 +571,10 @@ MainApp::ParseOpt(int ac, char *av[]) } case OPT_perf: - { - // パフォーマンス確認用。 - // -Vprom-image=PROM.DAT はパスの問題があるので各自で追加指定 - // する必要がある。 - std::string where = "--perf -V"; - config_options.emplace_back(".rtc-force-fixed=1", where); - config_options.emplace_back("clock-sync=virtual", where); - config_options.emplace_back("ethernet-macaddr=02:00:00:00:00:01", - where); - config_options.emplace_back("hostcom-driver=none", where); - config_options.emplace_back("hostnet-driver=none", where); - config_options.emplace_back("luna-dipsw1=11110111", where); - config_options.emplace_back("spc0-id0-writeignore=1", where); - config_options.emplace_back("spc0-id1-writeignore=1", where); - config_options.emplace_back("spc0-id2-writeignore=1", where); - config_options.emplace_back("spc0-id3-writeignore=1", where); - config_options.emplace_back("spc0-id4-writeignore=1", where); - config_options.emplace_back("spc0-id5-writeignore=1", where); - config_options.emplace_back("spc0-id6-writeignore=1", where); - config_options.emplace_back("fast-mode=1", where); - // -C + // パフォーマンス確認用。(Config::Update() 側で展開する) + config_options.emplace_back("--perf", ""); log_to_console = true; break; - } case OPT_show_config: show_config = 1; @@ -618,33 +598,45 @@ MainApp::ParseOpt(int ac, char *av[]) } } - // 引数がディレクトリなら、それを VM ディレクトリとし、その中の - // nono.cfg を設定ファイルとする。 - // 引数がファイルなら、それを設定ファイルとし、そのファイルがある - // ディレクトリを VM ディレクトリとする。 - // -c DIR => vmdir = DIR, vmfile = DIR/nono.cfg - // -c DIR/FILE => vmdir = DIR, vmfile = FILE - if (stat(cpath, &st) < 0) { - warn("stat %s", cpath); - return EXIT_FAILURE; + if (human_mode) { + // Human モードで -c ありなら、VM ディレクトリ指定。 + // Human モードで -c 省略なら、VM ディレクトリなし (cpath==NULL)。 + } else { + // Human モードでなければ、-c 省略は -c . と同義。 + if (cpath == NULL) { + cpath = "."; + } } - if (S_ISDIR(st.st_mode)) { - vmdir = std::string(cpath); - if (vmdir.back() != '/') { - vmdir += '/'; - } - vmfile = vmdir + "nono.cfg"; - } else if (S_ISREG(st.st_mode)) { - vmfile = std::string(cpath); - auto pos = vmfile.rfind('/'); - if (pos != std::string::npos) { - vmdir = vmfile.substr(0, pos + 1); + + if (cpath != NULL) { + // 引数がディレクトリなら、それを VM ディレクトリとし、その中の + // nono.cfg を設定ファイルとする。 + // 引数がファイルなら、それを設定ファイルとし、そのファイルがある + // ディレクトリを VM ディレクトリとする。 + // -c DIR => vmdir = DIR, vmfile = DIR/nono.cfg + // -c DIR/FILE => vmdir = DIR, vmfile = FILE + if (stat(cpath, &st) < 0) { + warn("stat %s", cpath); + return EXIT_FAILURE; + } + if (S_ISDIR(st.st_mode)) { + vmdir = std::string(cpath); + if (vmdir.back() != '/') { + vmdir += '/'; + } + vmfile = vmdir + "nono.cfg"; + } else if (S_ISREG(st.st_mode)) { + vmfile = std::string(cpath); + auto pos = vmfile.rfind('/'); + if (pos != std::string::npos) { + vmdir = vmfile.substr(0, pos + 1); + } else { + vmdir = "./"; + } } else { - vmdir = "./"; + warnx("-c %s: path must be file or directory", cpath); + return EXIT_FAILURE; } - } else { - warnx("-c %s: path must be file or directory", cpath); - return EXIT_FAILURE; } // CLI 版で -Mhelp つけても黙って起動するのはさすがにどうかと思う。 @@ -669,6 +661,49 @@ MainApp::ParseOpt(int ac, char *av[]) bool MainApp::CheckCPU() { + const ConfigItem& item_aff = gConfig->Find(".host-cpu-affinity"); + std::string cfg_aff = item_aff.AsString(); + + if (cfg_aff.empty() || cfg_aff == "auto") { + // 指定なし + // (auto も今の所指定なしと同じ) + cpu_affinity = CPUAffinity::None; + } else if (cfg_aff.find(':') == std::string::npos) { + item_aff.Err(); + } else { + // 書式は ":"。 + // は "low", "high", "perf" のいずれか。 + // は "0,3-5,7" みたいな感じ。 + std::vector vals = string_split(cfg_aff, ':'); + std::string key = string_trim(vals[0]); + std::string csv = vals[1]; + if (key == "low") { + cpu_affinity = CPUAffinity::Low; + } else if (key == "high") { + cpu_affinity = CPUAffinity::High; + } else if (key == "perf") { + cpu_affinity = CPUAffinity::Perf; + } else { + item_aff.Err("Invalid affinity keyword"); + } + cpu_list.resize(std::thread::hardware_concurrency()); + auto errmsg = ParseCPUList(cpu_list, csv); + if (errmsg.empty() == false) { + item_aff.Err(errmsg); + return false; + } + + // 全部 true/false なら、これは意味がないのでクリアする。 + uint num = std::count(cpu_list.begin(), cpu_list.end(), true); + if (num == 0) { + item_aff.Err("No valid cores specified (Ignoring it)"); + cpu_affinity = CPUAffinity::None; + } else if (num == cpu_list.size()) { + item_aff.Err("All cores specified (Ignoring it)"); + cpu_affinity = CPUAffinity::None; + } + } + #if defined(HAVE_AVX2) // AVX2 detect_avx2 = ::DetectAVX2(); @@ -685,10 +720,114 @@ MainApp::CheckCPU() return false; } #endif - // AVX2 以外は何もしない。設定のチェックもしないほうがいい。 + +#if defined(HAVE_NEON) + // NEON は aarch64 なら必ずある? + detect_neon = true; + + const ConfigItem& item_neon = gConfig->Find("host-neon"); + std::string cfg_neon = item_neon.AsString(); + + if (cfg_neon == "auto") { + enable_neon = detect_neon; + } else if (cfg_neon == "no") { + enable_neon = false; + } else { + item_neon.Err("must be either \"auto\" or \"no\"."); + return false; + } +#endif + + // 何もしない。設定のチェックもしないほうがいい。 return true; } +// CPU 番号リストの文字列からビット配列を作って返す。 +// cpulist は CPU コア数で初期化(resize)しておくこと。 +// 成功すれば string.empty を返す。 +// 失敗すればエラーメッセージを返す。 +std::string +MainApp::ParseCPUList(std::vector& cpulist, const std::string& input) +{ + if (input.empty()) { + return " must be specified"; + } + + // 設定は "0,3-5,7,10-" のような書式 + std::vector csv = string_split(input, ','); + for (auto& r : csv) { + const char *str; + char *endp; + uint start, last; + + if (r.empty()) { + // 空なら無視する? + continue; + } else if (r.find('-') == std::string::npos) { + // '-' がなければ単独 + str = &r[0]; + errno = 0; + start = strtoul(str, &endp, 10); + if (endp == str || *endp != '\0' || errno == ERANGE) { + return "syntax error"; + } + last = start; + } else if (r[0] == '-') { + // "-a" なら範囲 [0, a] + str = &r[1]; + errno = 0; + last = strtoul(str, &endp, 10); + if (endp == str || *endp != '\0' || errno == ERANGE) { + return "syntax error"; + } + start = 0; + } else if (r[r.size() - 1] == '-') { + // "a-" なら範囲 [a, MAX] + str = &r[0]; + errno = 0; + start = strtoul(str, &endp, 10); + if (endp == str || endp != &r[r.size() - 1] || errno == ERANGE) { + return "syntax error"; + } + last = cpulist.size(); + } else { + // "a-b" なら範囲 [a, b] + str = &r[0]; + errno = 0; + start = strtoul(str, &endp, 10); + if (endp == str || *endp != '-' || errno == ERANGE) { + return "syntax error"; + } + + str = endp + 1; + last = strtoul(str, &endp, 10); + if (endp == str || *endp != '\0' || errno == ERANGE) { + return "syntax error"; + } + + if (start > last) { + uint tmp = start; + start = last; + last = tmp; + } + } + + if (start >= cpulist.size()) { + return string_format("cpu number %u exceeds number of cores(%zu)", + start, cpulist.size()); + } + if (last >= cpulist.size()) { + return string_format("cpu number %u exceeds number of cores(%zu)", + start, cpulist.size()); + } + for (uint n = start; n <= last; n++) { + cpulist[n] = true; + } + } + + return ""; +} + // バージョンを表示 void MainApp::ShowVersion() const @@ -1133,7 +1272,7 @@ MainApp::UnregistObject(Object *obj) // 指定された id を持つオブジェクトを返す。なければ NULL を返す。 Object * -MainApp::FindObject(int id) const +MainApp::FindObject(uint id) const { // NONE はここで検索しない (SCSI デバイスなど、そっちで検索する) if (id == OBJ_NONE) { @@ -1149,7 +1288,7 @@ MainApp::FindObject(int id) const // 指定された id を持つオブジェクトを探す。なければ assert する。 Object * -MainApp::GetObject(int id) const +MainApp::GetObject(uint id) const { Object *obj = FindObject(id); if (__predict_false(obj == NULL)) {