Subversion Repositories pentevo

Rev

Rev 1124 | Rev 1140 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "std.h"
  2.  
  3. #include <io.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6.  
  7. #include "emul.h"
  8. #include "vars.h"
  9. #include "memory.h"
  10. #include "debug.h"
  11. #include "dbglabls.h"
  12. #include "draw.h"
  13. #include "dx.h"
  14. #include "fontatm2.h"
  15. #include "snapshot.h"
  16. #include "sndrender/sndcounter.h"
  17. #include "sndrender/dev_moonsound.h"
  18. #include "sound.h"
  19. #include "sdcard.h"
  20. #include "gs.h"
  21. #include "zc.h"
  22. #include "util.h"
  23. #include "init.h"
  24. #include "config.h"
  25. #include "zxusbnet.h"
  26.  
  27. char load_errors;
  28.  
  29. void loadkeys(action*);
  30. void loadzxkeys(CONFIG*);
  31. void load_arch(const char*);
  32.  
  33. static unsigned load_rom(const char *path, unsigned char *bank, unsigned max_banks = 1)
  34. {
  35.    if (!*path) { norom: memset(bank, 0xFF, max_banks*PAGE); return 0; }
  36.    char tmp[FILENAME_MAX]; strcpy(tmp, path);
  37.    char *x = strrchr(tmp+2, ':');
  38.    unsigned page = 0;
  39.    if (x) { *x = 0; page = unsigned(atoi(x+1)); }
  40.    if (max_banks == 16) page *= 16; // bank for scorp prof.rom
  41.  
  42.    FILE *ff = fopen(tmp, "rb");
  43.  
  44.    if (!ff) {
  45.       errmsg("ROM file %s not found", tmp);
  46.    err:
  47.       load_errors = 1;
  48.       goto norom;
  49.    }
  50.  
  51.    if (fseek(ff, long(page*PAGE), SEEK_SET)) {
  52. badrom:
  53.       fclose(ff);
  54.       errmsg("Incorrect ROM file: %s", path);
  55.       goto err;
  56.    }
  57.  
  58.    size_t size = fread(bank, 1, max_banks*PAGE, ff);
  59.    if (!size || (size & (PAGE-1))) goto badrom;
  60.  
  61.    fclose(ff);
  62.    return unsigned(size / 1024);
  63. }
  64.  
  65. void load_atm_font()
  66. {
  67.    FILE *ff = fopen("SGEN.ROM", "rb");
  68.    if (!ff)
  69.    {
  70.        memcpy(fontatm2, fontatm2_default, sizeof(fontatm2));
  71.        return;
  72.    }
  73.    unsigned char font[0x800];
  74.    size_t sz = fread(font, 1, 0x800, ff);
  75.    if (sz == 0x800) {
  76.       color(CONSCLR_INFO);
  77.       printf("using ATM font from external SGEN.ROM\n");
  78.       for (unsigned chr = 0; chr < 0x100; chr++)
  79.          for (unsigned l = 0; l < 8; l++)
  80.             fontatm2[chr+l*0x100] = font[chr*8+l];
  81.    }
  82.    fclose(ff);
  83. }
  84.  
  85. static void load_atariset()
  86. {
  87.    memset(temp.ataricolors, 0, sizeof temp.ataricolors);
  88.    if (!conf.atariset[0])
  89.        return;
  90.    char defs[4000]; *defs = 0; // =12*256, strlen("07:aabbccdd,")=12
  91.    char keyname[80];
  92.    sprintf(keyname, "atari.%s", conf.atariset);
  93.    GetPrivateProfileString("COLORS", keyname, nil, defs, sizeof defs, ininame);
  94.    if (!*defs)
  95.        conf.atariset[0] = 0;
  96.    for (char *ptr = defs; *ptr; )
  97.    {
  98.       if (ptr[2] != ':')
  99.           return;
  100.       for (int i = 0; i < 11; i++)
  101.          if (i != 2 && !ishex(ptr[i]))
  102.              return;
  103.       unsigned index, val;
  104.       sscanf(ptr, "%02X:%08X", &index, &val);
  105.       temp.ataricolors[index] = val;
  106.       // temp.ataricolors[(index*16 + index/16) & 0xFF] = val; // swap ink-paper
  107.       ptr += 12;
  108.       if (ptr [-1] != ',')
  109.           return;
  110.    }
  111. }
  112.  
  113. void addpath(char *dst, const char *fname)
  114. {
  115.    if (!fname)
  116.        fname = dst;
  117.    else
  118.        strcpy(dst, fname);
  119.    if (!*fname)
  120.        return; // empty filenames have special meaning
  121.    if (fname[1] == ':' || (fname[0] == '\\' || fname[1] == '\\'))
  122.        return; // already full name
  123.  
  124.    char tmp[FILENAME_MAX];
  125.    GetModuleFileName(nullptr, tmp, sizeof tmp);
  126.    char *xx = strrchr(tmp, '\\');
  127.    if (*fname == '?')
  128.        *xx = 0; // "?" to get exe directory
  129.    else
  130.        strcpy(xx+1, fname);
  131.    strcpy(dst, tmp);
  132. }
  133.  
  134. void save_nv()
  135. {
  136.    char line[FILENAME_MAX]; addpath(line, "CMOS");
  137.    FILE *f0 = fopen(line, "wb");
  138.    if(f0)
  139.    {
  140.        fwrite(cmos, 1, sizeof cmos, f0);
  141.        fclose(f0);
  142.    }
  143.  
  144.    addpath(line, "NVRAM");
  145.    if((f0 = fopen(line, "wb")))
  146.    {
  147.        fwrite(nvram, 1, sizeof nvram, f0);
  148.        fclose(f0);
  149.    }
  150. }
  151.  
  152. void load_romset(CONFIG *conf, const char *romset)
  153. {
  154.    char sec[0x200];
  155.    sprintf(sec, "ROM.%s", romset);
  156.    GetPrivateProfileString(sec, "sos", nil, conf->sos_rom_path, sizeof conf->sos_rom_path, ininame);
  157.    GetPrivateProfileString(sec, "dos", nil, conf->dos_rom_path, sizeof conf->dos_rom_path, ininame);
  158.    GetPrivateProfileString(sec, "128", nil, conf->zx128_rom_path, sizeof conf->zx128_rom_path, ininame);
  159.    GetPrivateProfileString(sec, "sys", nil, conf->sys_rom_path, sizeof conf->sys_rom_path, ininame);
  160.    addpath(conf->sos_rom_path);
  161.    addpath(conf->dos_rom_path);
  162.    addpath(conf->zx128_rom_path);
  163.    addpath(conf->sys_rom_path);
  164. }
  165.  
  166. static void add_presets(const char *section, const char *prefix0, unsigned *num, char **tab, unsigned char *curr)
  167. {
  168.    *num = 0;
  169.    char buf[0x7F00], defval[64];
  170.    GetPrivateProfileSection(section, buf, sizeof buf, ininame);
  171.    GetPrivateProfileString(section, prefix0, "none", defval, sizeof defval, ininame);
  172.    char *p = strchr(defval, ';');
  173.    if (p) *p = 0;
  174.  
  175.    for (p = defval+strlen(defval)-1; p>=defval && *p == ' '; *p-- = 0);
  176.  
  177.    char prefix[0x200];
  178.    strcpy(prefix, prefix0);
  179.    strcat(prefix, ".");
  180.    size_t plen = strlen(prefix);
  181.    for (char *ptr = buf; *ptr; )
  182.    {
  183.       if (!strnicmp(ptr, prefix, plen))
  184.       {
  185.          ptr += plen;
  186.          tab[*num] = setptr;
  187.          while (*ptr && *ptr != '=')
  188.              *setptr++ = *ptr++;
  189.          *setptr++ = 0;
  190.  
  191.          if (!stricmp(tab[*num], defval))
  192.              *curr = (unsigned char)*num;
  193.          (*num)++;
  194.       }
  195.       while (*ptr) ptr++;
  196.       ptr++;
  197.    }
  198. }
  199.  
  200. void load_ula_preset()
  201. {
  202.    if (conf.ula_preset >= num_ula) return;
  203.    char line[128], name[64];
  204.    sprintf(name, "PRESET.%s", ulapreset[conf.ula_preset]);
  205.    static char defaults[] = "71680,17989,224,50,32,0,0,0,0,0,320,240,24,32,384,288,48,64";
  206.    GetPrivateProfileString("ULA", name, defaults, line, sizeof line, ininame);
  207.    unsigned t1, t2, t3, t4, t5;
  208.    sscanf(line, "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u", &/*conf.frame*/frametime/*Alone Coder*/, &conf.paper,
  209.        &conf.t_line, &conf.intfq, &conf.intlen, &t1, &t2, &t3, &t4, &t5,
  210.        &conf.mcx_small, &conf.mcy_small, &conf.b_top_small, &conf.b_left_small,
  211.        &conf.mcx_full, &conf.mcy_full, &conf.b_top_full, &conf.b_left_full);
  212.    conf.even_M1 = (unsigned char)t1; conf.border_4T = (unsigned char)t2;
  213.    conf.floatbus = (unsigned char)t3; conf.floatdos = (unsigned char)t4;
  214.    conf.portff = t5 & 1;
  215.  
  216.    if(conf.mcx_small < 256)
  217.        conf.mcx_small = 256;
  218.    if(conf.mcy_small < 192)
  219.        conf.mcy_small = 192;
  220.  
  221.    if(conf.mcx_full < 256)
  222.        conf.mcx_full = 256;
  223.    if(conf.mcy_full < 192)
  224.        conf.mcy_full = 192;
  225.  
  226.    if(conf.b_left_full & 7)
  227.    {
  228.        err_printf("PRESET.%s:b_left_full not multiple of 8\n", ulapreset[conf.ula_preset]);
  229.        exit();
  230.    }
  231. }
  232.  
  233. void load_ay_stereo()
  234. {
  235.    char line[128], name[64]; sprintf(name, "STEREO.%s", aystereo[conf.sound.ay_stereo]);
  236.    GetPrivateProfileString("AY", name, "100,10,66,66,10,100", line, sizeof line, ininame);
  237.    unsigned *stereo = conf.sound.ay_stereo_tab;
  238.    sscanf(line, "%u,%u,%u,%u,%u,%u", stereo+0, stereo+1, stereo+2, stereo+3, stereo+4, stereo+5);
  239. }
  240.  
  241. void load_ay_vols()
  242. {
  243.    char line[512] = { 0 };
  244.    static char defaults[] = "0000,0340,04C0,06F2,0A44,0F13,1510,227E,289F,414E,5B21,7258,905E,B550,D7A0,FFFF";
  245.    char name[64]; sprintf(name, "VOLTAB.%s", ayvols[conf.sound.ay_vols]);
  246.    GetPrivateProfileString("AY", name, defaults, line, sizeof line, ininame);
  247.    if (line[74] != ',') strcpy(line, defaults);
  248.    if (line[79] == ',') { // YM
  249.       for (int i = 0; i < 32; i++)
  250.          sscanf(line+i*5, "%X", &conf.sound.ay_voltab[i]);
  251.    } else { // AY
  252.        for(int i = 0; i < 16; i++)
  253.        {
  254.            sscanf(line + i * 5, "%X", &conf.sound.ay_voltab[2 * i]);
  255.            conf.sound.ay_voltab[2 * i + 1] = conf.sound.ay_voltab[2 * i];
  256.        }
  257.    }
  258. }
  259.  
  260. #if (defined _MSC_VER)
  261. int strcasecmp(char const *s1, char const *s2)
  262. {
  263.         return ::_stricmp(s1, s2);
  264. }
  265. #endif
  266.  
  267.  
  268. void load_config(const char *fname)
  269. {
  270.    char line[FILENAME_MAX];
  271.    load_errors = 0;
  272.  
  273.    GetModuleFileName(nullptr, ininame, sizeof(ininame));
  274.  
  275.    //strlwr(ininame); // WHAT'S THIS FOR???
  276.    
  277.    // this was a flawed code since strstr searches FIRST occurence!
  278.    //*(unsigned*)(strstr(ininame, ".exe")+1) = WORD4('i','n','i',0);
  279.  
  280.    // make default config name: for EXENAME64.exe or EXENAME.exe that will be EXENAME.ini
  281.    size_t ininame_len = strlen(ininame);
  282.    const char const_64_exe[] = "64.exe";
  283.    const char const_exe[] = ".exe";
  284.    const char const_ini[] = ".ini";
  285.    //
  286.    if( !strcasecmp(ininame + ininame_len - sizeof(const_64_exe) + 1, const_64_exe) )
  287.         memcpy(ininame + ininame_len - sizeof(const_64_exe) + 1, const_ini, sizeof(const_ini)); // fix last "64.exe" to ".ini"
  288.    else if( !strcasecmp(ininame + ininame_len - sizeof(const_exe) + 1, const_exe) )
  289.         memcpy(ininame + ininame_len - sizeof(const_exe) + 1, const_ini, sizeof(const_ini)); // fix last ".exe" to ".ini"
  290.  
  291.    if (fname && *fname) {
  292.       char *dst = strrchr(ininame, '\\');
  293.       if (strchr(fname, '/') || strchr(fname, '\\')) dst = ininame; else dst++;
  294.       strcpy(dst, fname);
  295.    }
  296.    color(CONSCLR_DEFAULT); printf("ini: ");
  297.    color(CONSCLR_INFO);    printf("%s\n", ininame);
  298.  
  299.    if (GetFileAttributes(ininame) == INVALID_FILE_ATTRIBUTES) errexit("config file not found");
  300.  
  301.    static const char* misc = "MISC";
  302.    static const char* video = "VIDEO";
  303.    static const char* ula = "ULA";
  304.    static const char* beta128 = "Beta128";
  305.    static const char* USBZXNET = "USBZXNET";
  306.    static const char* leds = "LEDS";
  307.    static const char* sound = "SOUND";
  308.    static const char* input = "INPUT";
  309.    static const char* colors = "COLORS";
  310.    static const char* ay = "AY";
  311.    static const char* saa1099 = "SAA1099";
  312.    static const char* hdd = "HDD";
  313.    static const char* rom = "ROM";
  314.    static const char* ngs = "NGS";
  315.    static const char* zc = "ZC";
  316.  
  317.    #ifdef MOD_MONITOR
  318.    addpath(conf.sos_labels_path, "sos.l");
  319.    #endif
  320.  
  321.    GetPrivateProfileString("*", "UNREAL", nil, line, sizeof line, ininame);
  322.    unsigned a,b,c;
  323.    sscanf(line, "%u.%u.%u", &a, &b, &c);
  324.    if ((((a << 8U) | b) != VER_HL) || (c != (VER_A & 0x7F)))
  325.        errexit("wrong ini-file version");
  326.  
  327.    GetPrivateProfileString(misc, "Help",  "help_eng.html", helpname, sizeof helpname, ininame);
  328.    addpath(helpname);
  329.  
  330.    if (GetPrivateProfileInt(misc, "HideConsole", 0, ininame))
  331.    {
  332.        FreeConsole();
  333.        nowait = 1;
  334.    }
  335.  
  336.    conf.ConfirmExit = u8(GetPrivateProfileInt(misc, "ConfirmExit", 0, ininame));
  337.  
  338.    conf.sleepidle = u8(GetPrivateProfileInt(misc, "ShareCPU", 0, ininame));
  339.    conf.highpriority = u8(GetPrivateProfileInt(misc, "HighPriority", 0, ininame));
  340.    GetPrivateProfileString(misc, "SyncMode", "SOUND", line, sizeof line, ininame);
  341.    conf.SyncMode = SM_SOUND;
  342.    if(!strnicmp(line, "SOUND", 5))
  343.        conf.SyncMode = SM_SOUND;
  344.    else if(!strnicmp(line, "VIDEO", 5))
  345.        conf.SyncMode = SM_VIDEO;
  346.    else if(!strnicmp(line, "TSC", 3))
  347.        conf.SyncMode = SM_TSC;
  348.    conf.HighResolutionTimer = GetPrivateProfileInt(misc, "HighResolutionTimer", 0, ininame);
  349.    conf.tape_traps = u8(GetPrivateProfileInt(misc, "TapeTraps", 1, ininame));
  350.    conf.tape_autostart = u8(GetPrivateProfileInt(misc, "TapeAutoStart", 1, ininame));
  351.    conf.EFF7_mask = u8(GetPrivateProfileInt(misc, "EFF7mask", 0, ininame));
  352.  
  353.    GetPrivateProfileString(rom, "ATM1", nil, conf.atm1_rom_path, sizeof conf.atm1_rom_path, ininame);
  354.    GetPrivateProfileString(rom, "ATM2", nil, conf.atm2_rom_path, sizeof conf.atm2_rom_path, ininame);
  355.    GetPrivateProfileString(rom, "ATM3", nil, conf.atm3_rom_path, sizeof conf.atm3_rom_path, ininame);
  356.    GetPrivateProfileString(rom, "SCORP", nil, conf.scorp_rom_path, sizeof conf.scorp_rom_path, ininame);
  357.    GetPrivateProfileString(rom, "PROFROM", nil, conf.prof_rom_path, sizeof conf.prof_rom_path, ininame);
  358.    GetPrivateProfileString(rom, "PROFI", nil, conf.profi_rom_path, sizeof conf.profi_rom_path, ininame);
  359. //[vv]   GetPrivateProfileString(rom, "KAY", nil, conf.kay_rom_path, sizeof conf.kay_rom_path, ininame);
  360.    GetPrivateProfileString(rom, "PLUS3", nil, conf.plus3_rom_path, sizeof conf.plus3_rom_path, ininame);
  361.    GetPrivateProfileString(rom, "QUORUM", nil, conf.quorum_rom_path, sizeof conf.quorum_rom_path, ininame);
  362.    #ifdef MOD_GSZ80
  363.    GetPrivateProfileString(rom, "GS", nil, conf.gs_rom_path, sizeof conf.gs_rom_path, ininame);
  364.    addpath(conf.gs_rom_path);
  365.    #endif
  366.    GetPrivateProfileString(rom, "MOONSOUND", nil, conf.moonsound_rom_path, sizeof conf.moonsound_rom_path, ininame);
  367.    addpath(conf.moonsound_rom_path);
  368.    addpath(conf.atm1_rom_path);
  369.    addpath(conf.atm2_rom_path);
  370.    addpath(conf.atm3_rom_path);
  371.    addpath(conf.scorp_rom_path);
  372.    addpath(conf.prof_rom_path);
  373.    addpath(conf.profi_rom_path);
  374.    addpath(conf.plus3_rom_path);
  375.    addpath(conf.quorum_rom_path);
  376. //[vv]   addpath(conf.kay_rom_path);
  377.  
  378.    GetPrivateProfileString(rom, "ROMSET", "default", line, sizeof line, ininame);
  379.    if(*line)
  380.    {
  381.        load_romset(&conf, line); conf.use_romset = 1;
  382.    }
  383.    else
  384.    {
  385.        conf.use_romset = 0;
  386.    }
  387.  
  388.    conf.smuc = u8(GetPrivateProfileInt(misc, "SMUC", 0, ininame));
  389.    GetPrivateProfileString(misc, "CMOS", nil, line, sizeof line, ininame);
  390.    conf.cmos = 0;
  391.    if (!strnicmp(line, "DALLAS", 6)) conf.cmos=1;
  392.    if (!strnicmp(line, "512Bu1", 6)) conf.cmos=2;
  393.    conf.cache = u8(GetPrivateProfileInt(misc, "Cache", 0, ininame));
  394.    if (conf.cache && conf.cache!=16 && conf.cache!=32) conf.cache = 0;
  395.    GetPrivateProfileString(misc, "HIMEM", nil, line, sizeof line, ininame);
  396.    conf.mem_model = MM_PENTAGON;
  397.    unsigned i; //Alone Coder 0.36.7
  398.    for (/*unsigned*/ i = 0; i < N_MM_MODELS; i++)
  399.       if (!strnicmp(line, mem_model[i].shortname, strlen(mem_model[i].shortname)))
  400.          conf.mem_model = mem_model[i].Model;
  401.    conf.ramsize = GetPrivateProfileInt(misc, "RamSize", 128, ininame);
  402.    conf.Sna128Lock = GetPrivateProfileInt(misc, "Sna128Lock", 1, ininame);
  403.  
  404.    GetPrivateProfileString(misc, "DIR", nil, conf.workdir, sizeof conf.workdir, ininame);
  405.  
  406.    GetCurrentDirectory(_countof(line), line);
  407.    SetCurrentDirectory(conf.workdir);
  408.    GetCurrentDirectory(_countof(temp.SnapDir), temp.SnapDir);
  409.    SetCurrentDirectory(line);
  410.    strcpy(temp.RomDir, temp.SnapDir);
  411.    strcpy(temp.HddDir, temp.SnapDir);
  412.    strcpy(temp.SdDir, temp.SnapDir);
  413.  
  414.    conf.reset_rom = RM_SOS;
  415.    GetPrivateProfileString(misc, "RESET", nil, line, sizeof line, ininame);
  416.    if (!strnicmp(line, "DOS", 3)) conf.reset_rom = RM_DOS;
  417.    if (!strnicmp(line, "MENU", 4)) conf.reset_rom = RM_128;
  418.    if (!strnicmp(line, "SYS", 3)) conf.reset_rom = RM_SYS;
  419.  
  420.    GetPrivateProfileString(misc, "Modem", nil, line, sizeof line, ininame);
  421.    conf.modem_port = 0;
  422.  
  423.    sscanf(line, "COM%d", &conf.modem_port);
  424.  
  425.    conf.paper = GetPrivateProfileInt(ula, "Paper", 17989, ininame);
  426.    conf.t_line = GetPrivateProfileInt(ula, "Line", 224, ininame);
  427.    conf.intfq = GetPrivateProfileInt(ula, "int", 50, ininame);
  428.    conf.intlen = GetPrivateProfileInt(ula, "intlen", 32, ininame);
  429.    /*conf.frame*/frametime/*Alone Coder*/ = GetPrivateProfileInt(ula, "Frame", 71680, ininame);
  430.    conf.border_4T = u8(GetPrivateProfileInt(ula, "4TBorder", 0, ininame));
  431.    conf.even_M1 = u8(GetPrivateProfileInt(ula, "EvenM1", 0, ininame));
  432.    conf.floatbus = u8(GetPrivateProfileInt(ula, "FloatBus", 0, ininame));
  433.    conf.floatdos = u8(GetPrivateProfileInt(ula, "FloatDOS", 0, ininame));
  434.    conf.portff = GetPrivateProfileInt(ula, "PortFF", 0, ininame) != 0;
  435.    conf.mcx_small = GetPrivateProfileInt(ula, "mcx_small", 320, ininame);
  436.    conf.mcy_small = GetPrivateProfileInt(ula, "mcy_small", 240, ininame);
  437.    conf.b_top_small = GetPrivateProfileInt(ula, "b_top_small", 24, ininame);
  438.    conf.b_left_small = GetPrivateProfileInt(ula, "b_left_small", 32, ininame);
  439.    conf.mcx_full = GetPrivateProfileInt(ula, "mcx_full", 384, ininame);
  440.    conf.mcy_full = GetPrivateProfileInt(ula, "mcy_full", 288, ininame);
  441.    conf.b_top_full = GetPrivateProfileInt(ula, "b_top_full", 48, ininame);
  442.    conf.b_left_full = GetPrivateProfileInt(ula, "b_left_full", 64, ininame);
  443.  
  444.    conf.ula_preset=u8(-1U);
  445.    add_presets(ula, "preset", &num_ula, ulapreset, &conf.ula_preset);
  446.  
  447.    load_ula_preset();
  448.  
  449.    conf.atm.mem_swap = u8(GetPrivateProfileInt(ula, "AtmMemSwap", 0, ininame));
  450.    conf.use_comp_pal = u8(GetPrivateProfileInt(ula, "UsePalette", 1, ininame));
  451.    conf.profi_monochrome = u8(GetPrivateProfileInt(ula, "ProfiMonochrome", 0, ininame));
  452.    conf.ula_plus = GetPrivateProfileInt(ula, "ULAPlus", 0, ininame) != 0;
  453.  
  454.    conf.flashcolor = u8(GetPrivateProfileInt(video, "FlashColor", 0, ininame));
  455.    conf.frameskip = u8(GetPrivateProfileInt(video, "SkipFrame", 0, ininame));
  456.    conf.flip = (conf.SyncMode == SM_VIDEO) ? 1 : u8(GetPrivateProfileInt(video, "VSync", 0, ininame));
  457.    conf.fullscr = u8(GetPrivateProfileInt(video, "FullScr", 1, ininame));
  458.    conf.refresh = GetPrivateProfileInt(video, "Refresh", 0, ininame);
  459.    conf.frameskipmax = u8(GetPrivateProfileInt(video, "SkipFrameMaxSpeed", 33, ininame));
  460.    conf.updateb = u8(GetPrivateProfileInt(video, "Update", 1, ininame));
  461.    conf.ch_size = u8(GetPrivateProfileInt(video, "ChunkSize", 0, ininame));
  462.    conf.noflic = u8(GetPrivateProfileInt(video, "NoFlic", 0, ininame));
  463.    conf.alt_nf = u8(GetPrivateProfileInt(video, "AltNoFlic", 0, ininame));
  464.    conf.scanbright = GetPrivateProfileInt(video, "ScanIntens", 66, ininame);
  465.    conf.pixelscroll = u8(GetPrivateProfileInt(video, "PixelScroll", 0, ininame));
  466.    conf.detect_video = u8(GetPrivateProfileInt(video, "DetectModel", 1, ininame));
  467.    conf.fontsize = 8;
  468.  
  469.    conf.videoscale = u8(GetPrivateProfileInt(video, "scale", 2, ininame));
  470.  
  471.    conf.rsm.mix_frames = u8(GetPrivateProfileInt(video, "rsm.frames", 8, ininame));
  472.    GetPrivateProfileString(video, "rsm.mode", nil, line, sizeof line, ininame);
  473.    conf.rsm.mode = RSM_FIR0;
  474.    if (!strnicmp(line, "FULL", 4)) conf.rsm.mode = RSM_FIR0;
  475.    if (!strnicmp(line, "2C", 2)) conf.rsm.mode = RSM_FIR1;
  476.    if (!strnicmp(line, "3C", 2)) conf.rsm.mode = RSM_FIR2;
  477.    if (!strnicmp(line, "SIMPLE", 6)) conf.rsm.mode = RSM_SIMPLE;
  478.  
  479.    GetPrivateProfileString(video, "AtariPreset", nil, conf.atariset, sizeof conf.atariset, ininame);
  480.  
  481.    GetPrivateProfileString(video, video, nil, line, sizeof line, ininame);
  482.    conf.render = 0;
  483.    for (i = 0; renders[i].func; i++)
  484.       if (!strnicmp(line, renders[i].nick, strlen(renders[i].nick)))
  485.          conf.render = i;
  486.  
  487.    GetPrivateProfileString(video, "driver", nil, line, sizeof line, ininame);
  488.    conf.driver = DRIVER_DDRAW;
  489.    for (i = 0; drivers[i].nick; i++)
  490.       if (!strnicmp(line, drivers[i].nick, strlen(drivers[i].nick)))
  491.          conf.driver = i;
  492.  
  493.    conf.fast_sl = u8(GetPrivateProfileInt(video, "fastlines", 0, ininame));
  494.  
  495.    GetPrivateProfileString(video, "Border", nil, line, sizeof line, ininame);
  496.    conf.bordersize = 1;
  497.    if (!strnicmp(line, "none", 4))
  498.        conf.bordersize = 0;
  499.    else if (!strnicmp(line, "small", 5))
  500.        conf.bordersize = 1;
  501.    else if (!strnicmp(line, "wide", 4))
  502.        conf.bordersize = 2;
  503.    conf.minres = GetPrivateProfileInt(video, "MinRes", 0, ininame);
  504.  
  505.    GetPrivateProfileString(video, "Hide", nil, line, sizeof line, ininame);
  506.    char *ptr = strchr(line, ';'); if (ptr) *ptr = 0;
  507.    for (ptr = line;;)
  508.    {
  509.       size_t max = renders_count - 1;
  510.       for (i = 0; renders[i].func; i++)
  511.       {
  512.          size_t sl = strlen(renders[i].nick);
  513.          if (!strnicmp(ptr, renders[i].nick, sl) && !isalnum(ptr[sl]))
  514.          {
  515.             ptr += sl;
  516.             memcpy(&renders[i], &renders[i+1], (sizeof *renders) * (max-i));
  517.             break;
  518.          }
  519.       }
  520.       if (!*ptr++)
  521.           break;
  522.    }
  523.  
  524.    GetPrivateProfileString(video, "ScrShotDir", ".", conf.scrshot_dir, sizeof(conf.scrshot_dir), ininame);
  525.  
  526.    GetPrivateProfileString(video, "ScrShot", nil, line, sizeof line, ininame);
  527.    conf.scrshot = 0;
  528.    if(!strnicmp(line, "scr", 3))
  529.        conf.scrshot = 0;
  530.    else if(!strnicmp(line, "bmp", 3))
  531.        conf.scrshot = 1;
  532.    else if(!strnicmp(line, "png", 3))
  533.        conf.scrshot = 2;
  534.  
  535.    GetPrivateProfileString(video, "ffmpeg.exec", "ffmpeg.exe", conf.ffmpeg.exec, sizeof conf.ffmpeg.exec, ininame);
  536.    GetPrivateProfileString(video, "ffmpeg.parm", nil, conf.ffmpeg.parm, sizeof conf.ffmpeg.parm, ininame);
  537.    GetPrivateProfileString(video, "ffmpeg.vout", "video#.avi", conf.ffmpeg.vout, sizeof conf.ffmpeg.vout, ininame);
  538.    conf.ffmpeg.newcons = i8(GetPrivateProfileInt(video, "ffmpeg.newconsole", 1, ininame));
  539.  
  540.    conf.trdos_present = u8(GetPrivateProfileInt(beta128, "beta128", 1, ininame));
  541.    conf.trdos_traps = u8(GetPrivateProfileInt(beta128, "Traps", 1, ininame));
  542.    conf.wd93_nodelay = u8(GetPrivateProfileInt(beta128, "Fast", 1, ininame));
  543.    conf.trdos_interleave = u8(GetPrivateProfileInt(beta128, "IL", 1, ininame)-1);
  544.    if (conf.trdos_interleave > 2) conf.trdos_interleave = 0;
  545.    conf.fdd_noise = u8(GetPrivateProfileInt(beta128, "Noise", 0, ininame));
  546.    GetPrivateProfileString(beta128, "BOOT", nil, conf.appendboot, sizeof conf.appendboot, ininame);
  547.    addpath(conf.appendboot);
  548.    conf.trdos_IORam=GetPrivateProfileInt(beta128, "RamPageFddIO", 0, ininame); //временный код, потом надо удалить
  549.    conf.wiznet=GetPrivateProfileInt(USBZXNET, "WizNet", 0, ininame);
  550.    GetPrivateProfileString(misc, "ColdRAM", "0000FFFF", conf.cold_ram_pat, sizeof conf.cold_ram_pat, ininame);
  551.    
  552.    conf.led.enabled = u8(GetPrivateProfileInt(leds, "leds", 1, ininame));
  553.    conf.led.flash_ay_kbd = u8(GetPrivateProfileInt(leds, "KBD_AY", 1, ininame));
  554.    conf.led.perf_t = u8(GetPrivateProfileInt(leds, "PerfShowT", 0, ininame));
  555.    conf.led.bandBpp = GetPrivateProfileInt(leds, "BandBpp", 512, ininame);
  556.    if (conf.led.bandBpp != 64 && conf.led.bandBpp != 128 && conf.led.bandBpp != 256 && conf.led.bandBpp != 512) conf.led.bandBpp = 512;
  557.  
  558.    static char nm[] = "AY\0Perf\0LOAD\0Input\0Time\0OSW\0MemBand";
  559.    char *n2 = nm;
  560.    for (i = 0; i < NUM_LEDS; i++) {
  561.       GetPrivateProfileString(leds, n2, nil, line, sizeof line, ininame);
  562.       int x, y;
  563.       unsigned z; unsigned r; n2 += strlen(n2) + 1;
  564.       if (sscanf(line, "%u:%d,%d", &z, &x, &y) != 3) r = 0;
  565.       else r = (x & 0xFFFF) + ((y << 16) & 0x7FFFFFFF) + z*0x80000000;
  566.       *(&conf.led.ay+i) = r;
  567.    }
  568.  
  569.    conf.sound.do_sound = do_sound_none;
  570.    GetPrivateProfileString(sound, "SoundDrv", nil, line, sizeof line, ininame);
  571.    if (!strnicmp(line, "wave", 4)) {
  572.       conf.sound.do_sound = do_sound_wave;
  573.       conf.soundbuffer = GetPrivateProfileInt(sound, "SoundBuffer", 0, ininame);
  574.       if (!conf.soundbuffer) conf.soundbuffer = 6;
  575.       if (conf.soundbuffer >= MAXWQSIZE) conf.soundbuffer = MAXWQSIZE-1;
  576.    }
  577.    if (!strnicmp(line, "ds", 2)) {
  578.       conf.sound.do_sound = do_sound_ds;
  579. //      conf.soundbuffer = GetPrivateProfileInt(sound, "DSoundBuffer", 1000, ininame);
  580. //      conf.soundbuffer *= 4; // 16-bit, stereo
  581.    }
  582.  
  583.    conf.sound.enabled = u8(GetPrivateProfileInt(sound, "Enabled", 1, ininame));
  584.    #ifdef MOD_GS
  585.    conf.sound.gsreset = u8(GetPrivateProfileInt(sound, "GSReset", 0, ininame));
  586.    #endif
  587.    conf.sound.fq = GetPrivateProfileInt(sound, "Fq", 44100, ininame);
  588.    conf.sound.dsprimary = u8(GetPrivateProfileInt(sound, "DSPrimary", 0, ininame));
  589.  
  590.    conf.gs_type = 0;
  591. #ifdef MOD_GS
  592.    GetPrivateProfileString(sound, "GSTYPE", nil, line, sizeof line, ininame);
  593.    #ifdef MOD_GSZ80
  594.    if (!strnicmp(line, "Z80", 3)) conf.gs_type = 1;
  595.    #endif
  596.    #ifdef MOD_GSBASS
  597.    if (!strnicmp(line, "BASS", 4)) conf.gs_type = 2;
  598.    #endif
  599.    conf.gs_ramsize = GetPrivateProfileInt(ngs, "RamSize", 2048, ininame);
  600. #endif
  601.  
  602.    conf.soundfilter = u8(GetPrivateProfileInt(sound, "SoundFilter", 0, ininame)); //Alone Coder 0.36.4
  603.    conf.RejectDC = u8(GetPrivateProfileInt(sound, "RejectDC", 1, ininame));
  604.  
  605.    conf.sound.beeper_vol = int(GetPrivateProfileInt(sound, "BeeperVol", 4000, ininame));
  606.    conf.sound.micout_vol = int(GetPrivateProfileInt(sound, "MicOutVol", 1000, ininame));
  607.    conf.sound.micin_vol = int(GetPrivateProfileInt(sound, "MicInVol", 1000, ininame));
  608.    conf.sound.ay_vol = int(GetPrivateProfileInt(sound, "AYVol", 4000, ininame));
  609.    conf.sound.covoxFB = int(GetPrivateProfileInt(sound, "CovoxFB", 0, ininame));
  610.    conf.sound.covoxFB_vol = int(GetPrivateProfileInt(sound, "CovoxFBVol", 8192, ininame));
  611.    conf.sound.covoxDD = int(GetPrivateProfileInt(sound, "CovoxDD", 0, ininame));
  612.    conf.sound.covoxDD_vol = int(GetPrivateProfileInt(sound, "CovoxDDVol", 4000, ininame));
  613.    conf.sound.sd = int(GetPrivateProfileInt(sound, "SD", 0, ininame));
  614.    conf.sound.sd_vol = int(GetPrivateProfileInt(sound, "SDVol", 4000, ininame));
  615.    //conf.sound.saa1099 = GetPrivateProfileInt(sound, "Saa1099", 0, ininame);
  616.    conf.sound.saa1099 = SAA_NONE;
  617.    GetPrivateProfileString(sound, "Saa1099", 0, line, sizeof line, ininame);
  618.    if(!strnicmp(line, "ZXM", 3))
  619.        conf.sound.saa1099 = SAA_ZXM;
  620.    else if(!strnicmp(line, "TFMpro", 6))
  621.        conf.sound.saa1099 = SAA_TFM_PRO;
  622.    conf.sound.moonsound = GetPrivateProfileInt(sound, "MoonSound", 0, ininame);
  623.    conf.sound.moonsound_vol = GetPrivateProfileInt(sound, "MoonSoundVol", 4000, ininame);
  624.  
  625.    #ifdef MOD_GS
  626.    conf.sound.gs_vol = int(GetPrivateProfileInt(sound, "GSVol", 8000, ininame));
  627.    #endif
  628.  
  629.    #ifdef MOD_GSBASS
  630.    conf.sound.bass_vol = int(GetPrivateProfileInt(sound, "BASSVol", 8000, ininame));
  631.    #endif
  632.  
  633.    conf.sound.saa1099fq = GetPrivateProfileInt(saa1099, "Fq", 8000000, ininame);
  634.  
  635.    add_presets(ay, "VOLTAB", &num_ayvols, ayvols, &conf.sound.ay_vols);
  636.    add_presets(ay, "STEREO", &num_aystereo, aystereo, &conf.sound.ay_stereo);
  637.    conf.sound.ayfq = GetPrivateProfileInt(ay, "Fq", 1774400, ininame);
  638.  
  639.    GetPrivateProfileString(ay, "Chip", nil, line, sizeof line, ininame);
  640.    conf.sound.ay_chip = SNDCHIP::CHIP_YM;
  641.    if (!strnicmp(line, "AY", 2)) conf.sound.ay_chip = SNDCHIP::CHIP_AY;
  642.    if (!strnicmp(line, "YM2203", 6)) conf.sound.ay_chip = SNDCHIP::CHIP_YM2203;else //Dexus
  643.    if (!strnicmp(line, "YM", 2)) conf.sound.ay_chip = SNDCHIP::CHIP_YM;
  644.  
  645.    conf.sound.ay_samples = u8(GetPrivateProfileInt(ay, "UseSamples", 0, ininame));
  646.  
  647.    GetPrivateProfileString(ay, "Scheme", nil, line, sizeof line, ininame);
  648.    conf.sound.ay_scheme = AY_SCHEME_NONE;
  649.    if (!strnicmp(line, "default", 7)) conf.sound.ay_scheme = AY_SCHEME_SINGLE;
  650.    if(!strnicmp(line, "FULLER", 6)) conf.sound.ay_scheme = AY_SCHEME_FULLER;
  651.    if (!strnicmp(line, "PSEUDO", 6)) conf.sound.ay_scheme = AY_SCHEME_PSEUDO;
  652.    if (!strnicmp(line, "QUADRO", 6)) conf.sound.ay_scheme = AY_SCHEME_QUADRO;
  653.    if (!strnicmp(line, "POS", 3)) conf.sound.ay_scheme = AY_SCHEME_POS;
  654.    if (!strnicmp(line, "CHRV", 4)) conf.sound.ay_scheme = AY_SCHEME_CHRV;
  655.  
  656.    GetPrivateProfileString(input, "ZXKeyMap", "default", conf.zxkeymap, sizeof conf.zxkeymap, ininame);
  657.    for (i = 0; i < zxk_maps_count; i++)
  658.    {
  659.       if (!strnicmp(conf.zxkeymap, zxk_maps[i].name, strlen(zxk_maps[i].name)))
  660.       {
  661.           conf.input.active_zxk = &zxk_maps[i];
  662.           break;
  663.       }
  664.    }
  665.  
  666.    if(!conf.input.active_zxk)
  667.    {
  668.        errmsg("Invalid keyboard layout '%s' specified, default used", conf.zxkeymap);
  669.        conf.input.active_zxk = &zxk_maps[0]; // default
  670.    }
  671.  
  672.    GetPrivateProfileString(input, "KeybLayout", "default", line, sizeof(line), ininame);
  673.    ptr = strtok(line, " ;");
  674.    strcpy(conf.keyset, ptr ? ptr : line);
  675.  
  676.    GetPrivateProfileString(input, "Mouse", nil, line, sizeof line, ininame);
  677.    conf.input.mouse = 0;
  678.    if (!strnicmp(line, "KEMPSTON", 8)) conf.input.mouse = 1;
  679.    if (!strnicmp(line, "AY", 2)) conf.input.mouse = 2;
  680. //0.36.6 from 0.35b2
  681.    GetPrivateProfileString(input, "Wheel", nil, line, sizeof line, ininame);
  682.    conf.input.mousewheel = MOUSE_WHEEL_NONE;
  683.    if (!strnicmp(line, "KEMPSTON", 8)) conf.input.mousewheel = MOUSE_WHEEL_KEMPSTON;
  684.    if (!strnicmp(line, "KEYBOARD", 8)) conf.input.mousewheel = MOUSE_WHEEL_KEYBOARD;
  685. //~
  686.    conf.input.joymouse = u8(GetPrivateProfileInt(input, "JoyMouse", 0, ininame));
  687.    conf.input.mousescale = i8(GetPrivateProfileInt(input, "MouseScale", 0, ininame));
  688.    conf.input.mouseswap = u8(GetPrivateProfileInt(input, "SwapMouse", 0, ininame));
  689.    conf.input.kjoy = u8(GetPrivateProfileInt(input, "KJoystick", 1, ininame));
  690.    conf.input.fjoy = GetPrivateProfileInt(input, "FJoystick", 1, ininame) != 0;
  691.    conf.input.keymatrix = u8(GetPrivateProfileInt(input, "Matrix", 1, ininame));
  692.    conf.input.firedelay = u8(GetPrivateProfileInt(input, "FireRate", 1, ininame));
  693.    conf.input.altlock = u8(GetPrivateProfileInt(input, "AltLock", 1, ininame));
  694.    conf.input.paste_hold = u8(GetPrivateProfileInt(input, "HoldDelay", 2, ininame));
  695.    conf.input.paste_release = u8(GetPrivateProfileInt(input, "ReleaseDelay", 5, ininame));
  696.    conf.input.paste_newline = u8(GetPrivateProfileInt(input, "NewlineDelay", 20, ininame));
  697.    conf.input.keybpcmode = u8(GetPrivateProfileInt(input, "KeybPCMode", 0, ininame));
  698.    conf.atm.xt_kbd = u8(GetPrivateProfileInt(input, "ATMKBD", 0, ininame));
  699.    conf.input.JoyId = GetPrivateProfileInt(input, "Joy", 0, ininame);
  700.  
  701.    GetPrivateProfileString(input, "Fire", "0", line, sizeof line, ininame);
  702.    conf.input.firenum = 0; conf.input.fire = 0;
  703.    zxkeymap *active_zxk = conf.input.active_zxk;
  704.    for (i = 0; i < active_zxk->zxk_size; i++)
  705.       if (!stricmp(line, active_zxk->zxk[i].name))
  706.       {  conf.input.firenum = i; break; }
  707.  
  708.    char buff[0x7000];
  709.    GetPrivateProfileSection(colors, buff, sizeof buff, ininame);
  710.    GetPrivateProfileString(colors, "color", "default", line, sizeof line, ininame);
  711.    conf.pal = 0;
  712.  
  713.    for (i = 1, ptr = buff; i < _countof(pals); ptr += strlen(ptr)+1)
  714.    {
  715.       if (!*ptr)
  716.           break;
  717.       if (!isalnum(*ptr) || !strnicmp(ptr, "color=", 6))
  718.           continue;
  719.       char *ptr1 = strchr(ptr, '=');
  720.       if (!ptr1)
  721.           continue;
  722.       *ptr1 = 0; strcpy(pals[i].name, ptr); ptr = ptr1+1;
  723.       sscanf(ptr, "%02X,%02X,%02X,%02X,%02X,%02X:%X,%X,%X;%X,%X,%X;%X,%X,%X",
  724.          &pals[i].ZZ,  &pals[i].ZN,  &pals[i].NN,
  725.          &pals[i].NB,  &pals[i].BB,  &pals[i].ZB,
  726.          &pals[i].r11, &pals[i].r12, &pals[i].r13,
  727.          &pals[i].r21, &pals[i].r22, &pals[i].r23,
  728.          &pals[i].r31, &pals[i].r32, &pals[i].r33);
  729.  
  730.       pals[i].r11 = min(pals[i].r11, 256U);
  731.       pals[i].r12 = min(pals[i].r12, 256U);
  732.       pals[i].r13 = min(pals[i].r13, 256U);
  733.  
  734.       pals[i].r21 = min(pals[i].r21, 256U);
  735.       pals[i].r22 = min(pals[i].r22, 256U);
  736.       pals[i].r23 = min(pals[i].r23, 256U);
  737.  
  738.       pals[i].r31 = min(pals[i].r31, 256U);
  739.       pals[i].r32 = min(pals[i].r32, 256U);
  740.       pals[i].r33 = min(pals[i].r33, 256U);
  741.  
  742.       if (!strnicmp(line, pals[i].name, strlen(pals[i].name)))
  743.           conf.pal = i;
  744.       i++;
  745.    }
  746.    conf.num_pals = i;
  747.  
  748.    GetPrivateProfileString(hdd, "SCHEME", nil, line, sizeof line, ininame);
  749.    conf.ide_scheme = IDE_NONE;
  750.    if(!strnicmp(line, "ATM", 3))
  751.        conf.ide_scheme = IDE_ATM;
  752.    else if(!strnicmp(line, "NEMO-DIVIDE", 11))
  753.        conf.ide_scheme = IDE_NEMO_DIVIDE;
  754.    else if(!strnicmp(line, "NEMO-A8", 7))
  755.        conf.ide_scheme = IDE_NEMO_A8;
  756.    else if(!strnicmp(line, "NEMO", 4))
  757.        conf.ide_scheme = IDE_NEMO;
  758.    else if(!strnicmp(line, "SMUC", 4))
  759.        conf.ide_scheme = IDE_SMUC;
  760.    else if(!strnicmp(line, "PROFI", 5))
  761.        conf.ide_scheme = IDE_PROFI;
  762.    else if(!strnicmp(line, "DIVIDE", 6))
  763.        conf.ide_scheme = IDE_DIVIDE;
  764.  
  765.    conf.ide_skip_real = u8(GetPrivateProfileInt(hdd, "SkipReal", 0, ininame));
  766.    GetPrivateProfileString(hdd, "CDROM", "SPTI", line, sizeof line, ininame);
  767.    conf.cd_aspi = !strnicmp(line, "ASPI", 4) ? 1 : 0;
  768.  
  769.    for (int ide_device = 0; ide_device < 2; ide_device++)
  770.    {
  771.       char param[32];
  772.       sprintf(param, "LBA%d", ide_device);
  773.  
  774.       GetPrivateProfileString(hdd, param, "0", line, sizeof(line), ininame);
  775.       conf.ide[ide_device].lba = strtoull(line, nullptr, 10);
  776.       sprintf(param, "CHS%d", ide_device);
  777.       GetPrivateProfileString(hdd, param, "0/0/0", line, sizeof line, ininame);
  778.       unsigned c, h, s;
  779.  
  780.       sscanf(line, "%u/%u/%u", &c, &h, &s);
  781.       if(h > 16)
  782.       {
  783.           sprintf(line, "HDD%d heads count > 16 : %u\n", ide_device, h);
  784.           errexit(line);
  785.       }
  786.       if(s > 63)
  787.       {
  788.           sprintf(line, "error HDD%d sectors count > 63 : %u\n", ide_device, s);
  789.           errexit(line);
  790.       }
  791.       if(c > 16383)
  792.       {
  793.           sprintf(line, "error HDD%d cylinders count > 16383 : %u\n", ide_device, c);
  794.           errexit(line);
  795.       }
  796.  
  797.       conf.ide[ide_device].c = c;
  798.       conf.ide[ide_device].h = h;
  799.       conf.ide[ide_device].s = s;
  800.  
  801.       sprintf(param, "Image%d", ide_device);
  802.       GetPrivateProfileString(hdd, param, nil, conf.ide[ide_device].image, sizeof conf.ide[ide_device].image, ininame);
  803.  
  804.       if(conf.ide[ide_device].image[0] &&
  805.          conf.ide[ide_device].image[0] != '<')
  806.           addpath(conf.ide[ide_device].image);
  807.  
  808.       sprintf(param, "HD%dRO", ide_device);
  809.       conf.ide[ide_device].readonly = (BYTE)GetPrivateProfileInt(hdd, param, 0, ininame);
  810.       sprintf(param, "CD%d", ide_device);
  811.       conf.ide[ide_device].cd = (BYTE)GetPrivateProfileInt(hdd, param, 0, ininame);
  812.  
  813.       if(!conf.ide[ide_device].cd &&
  814.          conf.ide[ide_device].lba == 0 &&
  815.          conf.ide[ide_device].image[0] &&
  816.          conf.ide[ide_device].image[0] != '<')
  817.       {
  818.           int file = open(conf.ide[ide_device].image, O_RDONLY | O_BINARY, S_IREAD);
  819.           if(file >= 0)
  820.           {
  821.               __int64 sz = _filelengthi64(file);
  822.               close(file);
  823.               conf.ide[ide_device].lba = unsigned(sz / 512);
  824.           }
  825.       }
  826.    }
  827.  
  828.    addpath(line, "CMOS");
  829.    FILE *f0 = fopen(line, "rb");
  830.    if (f0)
  831.    {
  832.      fread(cmos, 1, sizeof cmos, f0);
  833.      fclose(f0);
  834.    }
  835.    else
  836.        cmos[0x11] = 0xAA;
  837.  
  838.    addpath(line, "NVRAM");
  839.    if ((f0 = fopen(line, "rb")))
  840.    {
  841.         fread(nvram, 1, sizeof nvram, f0);
  842.         fclose(f0);
  843.    }
  844.  
  845.    if(conf.gs_type == 1) // z80gs mode
  846.    {
  847.        GetPrivateProfileString(ngs, "SDCARD", nullptr, conf.ngs_sd_card_path, _countof(conf.ngs_sd_card_path), ininame);
  848.        if ( strcmp(conf.ngs_sd_card_path, "<CH341>") != 0 ) addpath(conf.ngs_sd_card_path);
  849.        if(conf.ngs_sd_card_path[0])
  850.            printf("NGS SDCARD='%s'\n", conf.ngs_sd_card_path);
  851.    }
  852.  
  853.    conf.zc = u8(GetPrivateProfileInt(misc, "ZC", 0, ininame));
  854.    if(conf.zc)
  855.    {
  856.        GetPrivateProfileString(zc, "SDCARD", nullptr, conf.zc_sd_card_path, _countof(conf.zc_sd_card_path), ininame);
  857.        if ( strcmp(conf.zc_sd_card_path, "<CH341>") != 0 ) addpath(conf.zc_sd_card_path);
  858.        if(conf.zc_sd_card_path[0])
  859.            printf("ZC SDCARD='%s'\n", conf.zc_sd_card_path);
  860.        conf.sd_delay = GetPrivateProfileInt(zc, "SDDelay", 1000, ininame);
  861.    }
  862.  
  863.    GetPrivateProfileString("AUTOLOAD", "DefaultDrive", nil, line, sizeof(line), ininame);
  864.    if(!strnicmp(line, "Auto", 4))
  865.        DefaultDrive = -1U;
  866.    else if(!strnicmp(line, "A", 1))
  867.        DefaultDrive = 0;
  868.    else if(!strnicmp(line, "B", 1))
  869.        DefaultDrive = 1;
  870.    else if(!strnicmp(line, "C", 1))
  871.        DefaultDrive = 2;
  872.    else if(!strnicmp(line, "D", 1))
  873.        DefaultDrive = 3;
  874.  
  875.    load_arch(ininame);
  876.    loadkeys(ac_main);
  877. #ifdef MOD_MONITOR
  878.    loadkeys(ac_main_xt);
  879.    loadkeys(ac_regs);
  880.    loadkeys(ac_trace);
  881.    loadkeys(ac_mem);
  882. #endif
  883.    temp.scale = GetPrivateProfileInt(video, "winscale", 1, ininame);
  884. }
  885.  
  886. void autoload()
  887. {
  888.    static char autoload[] = "AUTOLOAD";
  889.    char line[512];
  890.  
  891.    for (unsigned disk = 0; disk < 4; disk++) {
  892.       char key[8]; sprintf(key, "disk%c", int('A'+disk));
  893.       GetPrivateProfileString(autoload, key, nil, line, sizeof line, ininame);
  894.       if (!*line) continue;
  895.       addpath(line);
  896.       trd_toload = disk;
  897.       if (!loadsnap(line)) errmsg("failed to autoload <%s>", line);
  898.    }
  899.  
  900.    GetPrivateProfileString(autoload, "snapshot", nil, line, sizeof line, ininame);
  901.    if (!*line) return;
  902.    addpath(line);
  903.    if (!loadsnap(line)) { color(CONSCLR_ERROR); printf("failed to start snapshot <%s>\n", line); }
  904. }
  905.  
  906. static void apply_memory()
  907. {
  908.    #ifdef MOD_GSZ80
  909.    if (conf.gs_type == 1)
  910.    {
  911.        if(load_rom(conf.gs_rom_path, ROM_GS_M, 32) != 512) // 512k rom
  912.        {
  913.            errmsg("invalid ROM size for NGS (need 512kb), NGS disabled\n");
  914.            conf.gs_type = 0;
  915.        }
  916.    }
  917.    #endif
  918.  
  919.    if (zxmmoonsound.load_rom(conf.moonsound_rom_path))
  920.    {
  921.        errmsg("failed to load MoonSound ROM, MoonSound disabled\n");
  922.        conf.sound.moonsound = 0;
  923.    }
  924.  
  925.    if (conf.ramsize != 128 && conf.ramsize != 256 && conf.ramsize != 512 && conf.ramsize != 1024 && conf.ramsize != 4096)
  926.       conf.ramsize = 0;
  927.    if (!(mem_model[conf.mem_model].availRAMs & conf.ramsize)) {
  928.       conf.ramsize = mem_model[conf.mem_model].defaultRAM;
  929.       color(CONSCLR_ERROR);
  930.       printf("invalid RAM size for %s, using default (%uK)\n",
  931.          mem_model[conf.mem_model].fullname, conf.ramsize);
  932.    }
  933.  
  934.    switch(conf.mem_model)
  935.    {
  936.    case MM_ATM710:
  937.    case MM_ATM3:
  938.       base_sos_rom = ROM_BASE_M + 0*PAGE;
  939.       base_dos_rom = ROM_BASE_M + 1*PAGE;
  940.       base_128_rom = ROM_BASE_M + 2*PAGE;
  941.       base_sys_rom = ROM_BASE_M + 3*PAGE;
  942.    break;
  943.  
  944.    case MM_ATM450:
  945.    case MM_PROFI:
  946.       base_sys_rom = ROM_BASE_M + 0*PAGE;
  947.       base_dos_rom = ROM_BASE_M + 1*PAGE;
  948.       base_128_rom = ROM_BASE_M + 2*PAGE;
  949.       base_sos_rom = ROM_BASE_M + 3*PAGE;
  950.    break;
  951.  
  952.    case MM_PLUS3:
  953.       base_128_rom = ROM_BASE_M + 0*PAGE;
  954.       base_sys_rom = ROM_BASE_M + 1*PAGE;
  955.       base_dos_rom = ROM_BASE_M + 2*PAGE;
  956.       base_sos_rom = ROM_BASE_M + 3*PAGE;
  957.    break;
  958.  
  959.    case MM_QUORUM:
  960.       base_sys_rom = ROM_BASE_M + 0*PAGE;
  961.       base_dos_rom = ROM_BASE_M + 1*PAGE;
  962.       base_128_rom = ROM_BASE_M + 2*PAGE;
  963.       base_sos_rom = ROM_BASE_M + 3*PAGE;
  964.    break;
  965.  
  966. /*
  967.    case MM_KAY:
  968.       base_128_rom = ROM_BASE_M + 0*PAGE;
  969.       base_sos_rom = ROM_BASE_M + 1*PAGE;
  970.       base_dos_rom = ROM_BASE_M + 2*PAGE;
  971.       base_sys_rom = ROM_BASE_M + 3*PAGE;
  972.    break;
  973. */
  974.  
  975.    default:
  976.       base_128_rom = ROM_BASE_M + 0*PAGE;
  977.       base_sos_rom = ROM_BASE_M + 1*PAGE;
  978.       base_sys_rom = ROM_BASE_M + 2*PAGE;
  979.       base_dos_rom = ROM_BASE_M + 3*PAGE;
  980.    }
  981.  
  982.    unsigned romsize;
  983.    if (conf.use_romset)
  984.    {
  985.       if (!load_rom(conf.sos_rom_path, base_sos_rom))
  986.           errexit("failed to load BASIC48 ROM");
  987.       if (!load_rom(conf.zx128_rom_path, base_128_rom) && conf.reset_rom == RM_128)
  988.           conf.reset_rom = RM_SOS;
  989.       if (!load_rom(conf.dos_rom_path, base_dos_rom))
  990.           conf.trdos_present = 0;
  991.       if (!load_rom(conf.sys_rom_path, base_sys_rom) && conf.reset_rom == RM_SYS)
  992.           conf.reset_rom = RM_SOS;
  993.       romsize = 64;
  994.    }
  995.    else
  996.    {
  997.       if (conf.mem_model == MM_ATM710 || conf.mem_model == MM_ATM3)
  998.       {
  999.          romsize = load_rom(conf.mem_model == MM_ATM710 ? conf.atm2_rom_path : conf.atm3_rom_path, ROM_BASE_M, 64);
  1000.          if (romsize != 64 && romsize != 128 && romsize != 512 && romsize != 1024)
  1001.             errexit("invalid ROM size for ATM bios");
  1002.          unsigned char *lastpage = ROM_BASE_M + (romsize - 64) * 1024;
  1003.          base_sos_rom = lastpage + 0*PAGE;
  1004.          base_dos_rom = lastpage + 1*PAGE;
  1005.          base_128_rom = lastpage + 2*PAGE;
  1006.          base_sys_rom = lastpage + 3*PAGE;
  1007.       }
  1008.       else if (conf.mem_model == MM_PROFSCORP)
  1009.       {
  1010.          romsize = load_rom(conf.prof_rom_path, ROM_BASE_M, 16);
  1011.          if (romsize != 64 && romsize != 128 && romsize != 256)
  1012.             errexit("invalid PROF-ROM size");
  1013.       }
  1014.       else
  1015.       {
  1016.          const char *romname = nullptr;
  1017.          switch(conf.mem_model)
  1018.          {
  1019.          case MM_PROFI: romname = conf.profi_rom_path; break;
  1020.          case MM_SCORP: romname = conf.scorp_rom_path; break;
  1021. //[vv]         case MM_KAY: romname = conf.kay_rom_path; break;
  1022.          case MM_ATM450: romname = conf.atm1_rom_path; break;
  1023.          case MM_PLUS3: romname = conf.plus3_rom_path; break;
  1024.          case MM_QUORUM: romname = conf.quorum_rom_path; break;
  1025.  
  1026.          default:
  1027.              errexit("ROMSET should be defined for this memory model");
  1028.          }
  1029.  
  1030.          romsize = load_rom(romname, ROM_BASE_M, 64);
  1031.          if (romsize != 64)
  1032.              errexit("invalid ROM filesize");
  1033.       }
  1034.    }
  1035.  
  1036.    if (conf.mem_model == MM_PROFSCORP)
  1037.    {
  1038.       temp.profrom_mask = 0;
  1039.       if (romsize == 128)
  1040.           temp.profrom_mask = 1;
  1041.       if (romsize == 256)
  1042.           temp.profrom_mask = 3;
  1043.  
  1044.       comp.profrom_bank &= temp.profrom_mask;
  1045.       set_scorp_profrom(0);
  1046.    }
  1047.  
  1048. #ifdef MOD_MONITOR
  1049.    load_labels(conf.sos_labels_path, base_sos_rom, 0x4000);
  1050. #endif
  1051.  
  1052.    temp.gs_ram_mask = u8((conf.gs_ramsize-1) >> 4);
  1053.    temp.ram_mask = u8((conf.ramsize-1) >> 4);
  1054.    temp.rom_mask = u8((romsize-1) >> 4);
  1055.    set_banks();
  1056.  
  1057.    for(unsigned i = 0; i < CpuMgr.GetCount(); i++)
  1058.    {
  1059.        Z80 &cpu = CpuMgr.Cpu(i);
  1060.        cpu.dbgchk = isbrk(cpu);
  1061.    }
  1062. }
  1063.  
  1064.  
  1065. void applyconfig(bool Init)
  1066. {
  1067. #ifdef MOD_GS
  1068.     init_gs(Init);
  1069. #endif
  1070. //   printf("%s\n", __FUNCTION__);
  1071.  
  1072.    //[vv] disable turbo
  1073.    comp.pEFF7 |= EFF7_GIGASCREEN;
  1074.  
  1075. //Alone Coder 0.36.4
  1076.    conf.frame = frametime;
  1077.    cpu.SetTpi(conf.frame);
  1078. /*
  1079.    if ((conf.mem_model == MM_PENTAGON)&&(comp.pEFF7 & EFF7_GIGASCREEN))
  1080.        conf.frame = 71680;
  1081. */
  1082. //~Alone Coder
  1083.    temp.ticks_frame = (unsigned)(temp.cpufq / double(conf.intfq) + 1.0);
  1084.    loadzxkeys(&conf);
  1085.    apply_memory();
  1086.  
  1087.    temp.snd_frame_ticks = (conf.sound.fq << TICK_FF) / conf.intfq;
  1088.    temp.snd_frame_samples = temp.snd_frame_ticks >> TICK_FF;
  1089.    temp.frameskip = conf.sound.enabled? conf.frameskip : conf.frameskipmax;
  1090.  
  1091.    input.firedelay = 1; // if conf.input.fire changed
  1092.    input.clear_zx();
  1093.  
  1094.    modem.open(conf.modem_port);
  1095.  
  1096.    load_atariset();
  1097.    apply_video();
  1098.    apply_sound();
  1099.  
  1100.    hdd.dev[0].configure(conf.ide+0);
  1101.    hdd.dev[1].configure(conf.ide+1);
  1102.    if (conf.atm.xt_kbd) input.atm51.clear();
  1103.  
  1104.    if(conf.gs_type == 1)
  1105.    {
  1106.        SdCard.Close();
  1107.        SdCard.Open(conf.ngs_sd_card_path);
  1108.    }
  1109.  
  1110.    if(conf.zc)
  1111.    {
  1112.        Zc.Close();
  1113.        Zc.Open(conf.zc_sd_card_path);
  1114.    }
  1115.         if(conf.wiznet){
  1116.                 comp.wiznet.p83=0;
  1117.                 comp.wiznet.p82=0;
  1118.                 comp.wiznet.p81=0;
  1119.                 Wiz5300_Close();
  1120.                 Wiz5300_Init();
  1121.         }
  1122.  
  1123.    setpal(0);
  1124. }
  1125.  
  1126. void load_arch(const char *fname)
  1127. {
  1128.    GetPrivateProfileString("ARC", "SkipFiles", nil, skiparc, sizeof skiparc, fname);
  1129.    char *p; //Alone Coder 0.36.7
  1130.    for (/*char * */p = skiparc;;) {
  1131.       char *nxt = strchr(p, ';');
  1132.       if (!nxt) break;
  1133.       *nxt = 0; p = nxt+1;
  1134.    }
  1135.    p[strlen(p)+1] = 0;
  1136.  
  1137.    GetPrivateProfileSection("ARC", arcbuffer, sizeof arcbuffer, fname);
  1138.    for (char *x = arcbuffer; *x; ) {
  1139.       char *newx = x + strlen(x)+1;
  1140.       char *y = strchr(x, '=');
  1141.       if (!y) {
  1142. ignore_line:
  1143.          memcpy(x, newx, sizeof arcbuffer - size_t(newx-arcbuffer));
  1144.       } else {
  1145.          *y = 0; if (!stricmp(x, "SkipFiles")) goto ignore_line;
  1146.          x = newx;
  1147.       }
  1148.    }
  1149. }
  1150.  
  1151. void loadkeys(action *table)
  1152. {
  1153.    unsigned num[0x300], i = 0;
  1154.    unsigned j; //Alone Coder 0.36.7
  1155.    if (!table->name)
  1156.        return; // empty table (can't sort)
  1157.    for (action *p = table; p->name; p++, i++)
  1158.    {
  1159.       char line[0x400];
  1160.       GetPrivateProfileString("SYSTEM.KEYS", p->name, "`", line, sizeof line, ininame);
  1161.       if (*line == '`')
  1162.       {
  1163.          errmsg("keydef for %s not found", p->name);
  1164.          load_errors = 1;
  1165. bad_key:
  1166.          p->k1 = 0xFE;
  1167.          p->k2 = 0xFF;
  1168.          p->k3 = 0xFD;
  1169.          continue;
  1170.       }
  1171.       char *s = strchr(line, ';');
  1172.       if(s)
  1173.           *s = 0;
  1174.       p->k1 = p->k2 = p->k3 = p->k4 = 0; num[i] = 0;
  1175.       for (s = line;;)
  1176.       {
  1177.          while (*s == ' ') s++;
  1178.          if (!*s)
  1179.              break;
  1180.          char *s1 = s;
  1181.          while (isalnum(*s))
  1182.              s++;
  1183.          for (j = 0; j < pckeys_count; j++)
  1184.          {
  1185.             if ((int)strlen(pckeys[j].name)==s-s1 && !strnicmp(s1, pckeys[j].name, size_t(s-s1)))
  1186.             {
  1187.                switch (num[i])
  1188.                {
  1189.                   case 0: p->k1 = pckeys[j].virtkey; break;
  1190.                   case 1: p->k2 = pckeys[j].virtkey; break;
  1191.                   case 2: p->k3 = pckeys[j].virtkey; break;
  1192.                   case 3: p->k4 = pckeys[j].virtkey; break;
  1193.                   default:
  1194.                      color(CONSCLR_ERROR);
  1195.                      printf("warning: too many keys in %s=%s\n", p->name, line);
  1196.                      load_errors = 1;
  1197.                }
  1198.                num[i]++;
  1199.                break;
  1200.             }
  1201.          }
  1202.          if (j == pckeys_count)
  1203.          {
  1204.             color(CONSCLR_ERROR);
  1205.             char x = *s; *s = 0;
  1206.             printf("bad key: %s\n", s1); *s = x;
  1207.             load_errors = 1;
  1208.          }
  1209.       }
  1210.       if (!num[i])
  1211.           goto bad_key;
  1212.    }
  1213.  
  1214.    // sort keys
  1215.    for (unsigned k = 0; k < i-1; k++)
  1216.    {
  1217.       unsigned max = k;
  1218.       for (unsigned l = k+1; l < i; l++)
  1219.          if (num[l] > num[max])
  1220.              max = l;
  1221.  
  1222.       action tmp = table[k];
  1223.       table[k] = table[max];
  1224.       table[max] = tmp;
  1225.  
  1226.       unsigned tm = num[k];
  1227.       num[k] = num[max];
  1228.       num[max] = tm;
  1229.    }
  1230. }
  1231.  
  1232. void loadzxkeys(CONFIG *conf)
  1233. {
  1234.    char section[0x200];
  1235.    sprintf(section, "ZX.KEYS.%s", conf->keyset);
  1236.    char line[0x300];
  1237.    char *s; //Alone Coder 0.36.7
  1238.    unsigned k; //Alone Coder 0.36.7
  1239.    zxkeymap *active_zxk = conf->input.active_zxk;
  1240.  
  1241.    for (unsigned i = 0; i < VK_MAX; i++)
  1242.    {
  1243.       inports[i].port1 = inports[i].port2 = &input.kjoy;
  1244.       inports[i].mask1 = inports[i].mask2 = 0xFF;
  1245.       for (unsigned j = 0; j < pckeys_count; j++)
  1246.       {
  1247.          if (pckeys[j].virtkey == i)
  1248.          {
  1249.             GetPrivateProfileString(section, pckeys[j].name, "", line, sizeof line, ininame);
  1250.             s = strtok(line, " ;");
  1251.             if(s)
  1252.             {
  1253.                for(k = 0; k < active_zxk->zxk_size; k++)
  1254.                {
  1255.                   if (!stricmp(s, active_zxk->zxk[k].name))
  1256.                   {
  1257.                      inports[i].port1 = active_zxk->zxk[k].port;
  1258.                      inports[i].mask1 = active_zxk->zxk[k].mask;
  1259.                      switch(i)
  1260.                      {
  1261.                      case DIK_CONTROL:
  1262.                          inports[DIK_LCONTROL].port1 = active_zxk->zxk[k].port;
  1263.                          inports[DIK_LCONTROL].mask1 = active_zxk->zxk[k].mask;
  1264.                          inports[DIK_RCONTROL].port1 = active_zxk->zxk[k].port;
  1265.                          inports[DIK_RCONTROL].mask1 = active_zxk->zxk[k].mask;
  1266.                      break;
  1267.  
  1268.                      case DIK_SHIFT:
  1269.                          inports[DIK_LSHIFT].port1 = active_zxk->zxk[k].port;
  1270.                          inports[DIK_LSHIFT].mask1 = active_zxk->zxk[k].mask;
  1271.                          inports[DIK_RSHIFT].port1 = active_zxk->zxk[k].port;
  1272.                          inports[DIK_RSHIFT].mask1 = active_zxk->zxk[k].mask;
  1273.                      break;
  1274.  
  1275.                      case DIK_MENU:
  1276.                          inports[DIK_LMENU].port1 = active_zxk->zxk[k].port;
  1277.                          inports[DIK_LMENU].mask1 = active_zxk->zxk[k].mask;
  1278.                          inports[DIK_RMENU].port1 = active_zxk->zxk[k].port;
  1279.                          inports[DIK_RMENU].mask1 = active_zxk->zxk[k].mask;
  1280.                      break;
  1281.                      }
  1282.                      break;
  1283.                   }
  1284.                }
  1285.             }
  1286.             s = strtok(nullptr, " ;");
  1287.             if(s)
  1288.             {
  1289.                for (k = 0; k < active_zxk->zxk_size; k++)
  1290.                {
  1291.                   if (!stricmp(s, active_zxk->zxk[k].name))
  1292.                   {
  1293.                      inports[i].port2 = active_zxk->zxk[k].port;
  1294.                      inports[i].mask2 = active_zxk->zxk[k].mask;
  1295.  
  1296.                      switch(i)
  1297.                      {
  1298.                      case DIK_CONTROL:
  1299.                          inports[DIK_LCONTROL].port2 = active_zxk->zxk[k].port;
  1300.                          inports[DIK_LCONTROL].mask2 = active_zxk->zxk[k].mask;
  1301.                          inports[DIK_RCONTROL].port2 = active_zxk->zxk[k].port;
  1302.                          inports[DIK_RCONTROL].mask2 = active_zxk->zxk[k].mask;
  1303.                      break;
  1304.  
  1305.                      case DIK_SHIFT:
  1306.                          inports[DIK_LSHIFT].port2 = active_zxk->zxk[k].port;
  1307.                          inports[DIK_LSHIFT].mask2 = active_zxk->zxk[k].mask;
  1308.                          inports[DIK_RSHIFT].port2 = active_zxk->zxk[k].port;
  1309.                          inports[DIK_RSHIFT].mask2 = active_zxk->zxk[k].mask;
  1310.                      break;
  1311.  
  1312.                      case DIK_MENU:
  1313.                          inports[DIK_LMENU].port2 = active_zxk->zxk[k].port;
  1314.                          inports[DIK_LMENU].mask2 = active_zxk->zxk[k].mask;
  1315.                          inports[DIK_RMENU].port2 = active_zxk->zxk[k].port;
  1316.                          inports[DIK_RMENU].mask2 = active_zxk->zxk[k].mask;
  1317.                      break;
  1318.                      }
  1319.                      break;
  1320.                   }
  1321.                }
  1322.             }
  1323.             break;
  1324.          }
  1325.       }
  1326.    }
  1327. }
  1328.