Subversion Repositories pentevo

Rev

Rev 798 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

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