forked from schellingb/dosbox-pure
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdosbox_pure_run.h
855 lines (794 loc) · 35.9 KB
/
dosbox_pure_run.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
* Copyright (C) 2020-2024 Bernhard Schelling
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
static const char* DBP_MachineNames[] = { "SVGA (Super Video Graphics Array)", "VGA (Video Graphics Array)", "EGA (Enhanced Graphics Adapter", "CGA (Color Graphics Adapter)", "Tandy (Tandy Graphics Adapter", "Hercules (Hercules Graphics Card)", "PCjr" };
struct DBP_Run
{
static void RunBatchFile(BatchFile* bf)
{
DBP_ASSERT(!dbp_game_running);
const bool inAutoexec = (first_shell->bf && first_shell->bf->filename[0] == 'Z');
while (first_shell->bf) delete first_shell->bf;
bf->prev = NULL; // was just deleted
bf->echo = true; // always want this back after returning
first_shell->bf = bf;
first_shell->echo = false;
if (!inAutoexec)
{
// Sending this key sequence makes sure DOS_Shell::Run will run our batch file immediately
// It also clears anything typed already on the command line or finishes DOS_Shell::CMD_PAUSE or DOS_Shell::CMD_CHOICE
KEYBOARD_AddKey(KBD_esc, true);
KEYBOARD_AddKey(KBD_esc, false);
KEYBOARD_AddKey(KBD_enter, true);
KEYBOARD_AddKey(KBD_enter, false);
}
dbp_lastmenuticks = DBP_GetTicks();
}
static void ConsoleClearScreen()
{
DBP_ASSERT(!dbp_game_running);
reg_ax = 0x0003;
CALLBACK_RunRealInt(0x10);
}
struct BatchFileExec : BatchFile
{
BatchFileExec(const std::string& _exe) : BatchFile(first_shell,"Z:\\AUTOEXEC.BAT","","") { filename = _exe; if (!_exe.length()) location = 1; }
virtual bool ReadLine(char * line)
{
*(line++) = '@';
switch (location++)
{
case 0:
{
ConsoleClearScreen();
char *fn = (char*)filename.c_str(), *r = fn + ((fn[0] && fn[1] == ':') ? 2 : 0), *p = r + (*r == '\\' ? 1 : 0), *param = strchr(p, ' '), *sl;
if (param) { *param = '\0'; sl = strrchr(p, '\\'); *param = ' '; } else { sl = strrchr(p, '\\'); };
const Bit8u drive = ((((fn[0] >= 'A' && fn[0] <= 'Z') || (fn[0] >= 'a' && fn[0] <= 'z')) && fn[1] == ':') ? (fn[0] & 0x5F) : 'C') - 'A';
if (Drives[drive])
{
DOS_SetDefaultDrive(drive);
if (sl)
{
memcpy(Drives[drive]->curdir,p, sl - p);
Drives[drive]->curdir[sl - p] = '\0';
}
else Drives[drive]->curdir[0] = '\0';
}
else { sl = NULL; p = fn; } // try call full string which will likely show an error to tell the user auto start is wrong
const char* f = (sl ? sl + 1 : p), *fext = strchr(f, '.');
bool isbat = (fext && !strcasecmp(fext, ".bat"));
int call_cmd_len = (isbat ? 5 : 0), flen = (int)strlen(f);
memcpy(line, "call ", call_cmd_len);
memcpy(line+call_cmd_len, f, flen);
memcpy(line+call_cmd_len+flen, "\n", 2);
break;
}
case 1:
memcpy(line, "Z:PUREMENU", 10);
memcpy(line+10, " -FINISH\n", 10);
delete this;
break;
}
return true;
}
};
struct BatchFileBoot : BatchFile
{
BatchFileBoot(char drive) : BatchFile(first_shell,"Z:\\AUTOEXEC.BAT","","") { file_handle = drive; }
virtual bool ReadLine(char * line)
{
if (location++)
{
// This function does not do `delete this;` instead it calls DBP_OnBIOSReboot to eventually do that
memcpy(line, "@PAUSE\n", 8);
if (location > 2) { startup.mode = RUN_NONE; DBP_OnBIOSReboot(); }
return true;
}
ConsoleClearScreen();
memcpy(line, "@Z:BOOT -l \n", 14);
line[11] = (char)file_handle; // drive letter
if (machine == MCH_PCJR && file_handle == 'A' && !dbp_images.empty())
{
// The path to the image needs to be passed to boot for pcjr carts
const std::string& imgpath = dbp_images[dbp_image_index].path;
line[12] = ' ';
memcpy(line+13, imgpath.c_str(), imgpath.size());
memcpy(line+13+imgpath.size(), "\n", 2);
}
return true;
}
static bool HaveCDImage()
{
for (DBP_Image& i : dbp_images) if (DBP_Image_IsCD(i)) return true;
return false;
}
static bool MountOSIMG(char drive, const char* path, const char* type, bool needwritable, bool complainnotfound)
{
FILE* raw_file_h = NULL;
if (needwritable && (raw_file_h = fopen_wrap(path, "rb+")) != NULL) goto openok;
if ((raw_file_h = fopen_wrap(path, "rb")) == NULL)
{
if (complainnotfound)
retro_notify(0, RETRO_LOG_ERROR, "Unable to open %s file: %s%s", type, path, "");
return false;
}
if (needwritable)
{
retro_notify(0, RETRO_LOG_ERROR, "Unable to open %s file: %s%s", type, path, " (file is read-only!)");
fclose(raw_file_h);
return false;
}
openok:
DOS_File* df = new rawFile(raw_file_h, needwritable);
df->AddRef();
imageDiskList[drive-'A'] = new imageDisk(df, "", 0, true);
imageDiskList[drive-'A']->Set_GeometryForHardDisk();
return true;
}
};
static void Exec(const std::string& _exe)
{
RunBatchFile(new BatchFileExec(_exe));
}
static void BootImage()
{
DBP_ASSERT(!dbp_images.empty()); // IT_BOOTIMG should only be available if this is true
if (!dbp_images.empty())
{
DBP_Mount(); // make sure something is mounted
// If hard disk image was mounted to D:, swap it to be the bootable C: drive
std::swap(imageDiskList['D'-'A'], imageDiskList['C'-'A']);
// If there is no mounted hard disk image but a D: drive, setup the CDROM IDE controller
if (!imageDiskList['C'-'A'] && Drives['D'-'A'])
IDE_SetupControllers(BatchFileBoot::HaveCDImage() ? 'D' : 0);
// Install the NE2000 network card
NET_SetupEthernet();
}
RunBatchFile(new BatchFileBoot(imageDiskList['A'-'A'] ? 'A' : 'C'));
}
static void BootOS(bool is_install, int osidx_or_size)
{
// Make sure we have at least 32 MB of RAM, if not set it to 64
if ((MEM_TotalPages() / 256) < 32)
{
dbp_reboot_set64mem = true;
DBP_OnBIOSReboot();
return;
}
std::string path;
if (!is_install)
{
path = DBP_GetSaveFile(SFT_SYSTEMDIR).append(dbp_osimages[osidx_or_size]);
}
else if (osidx_or_size)
{
const char* filename;
path = DBP_GetSaveFile(SFT_NEWOSIMAGE, &filename);
// Create a new empty hard disk image of the requested size
memoryDrive* memDrv = new memoryDrive();
DBP_SetDriveLabelFromContentPath(memDrv, path.c_str(), 'C', filename, path.c_str() + path.size() - 3);
imageDisk* memDsk = new imageDisk(memDrv, (Bit32u)(osidx_or_size*8));
Bit32u heads, cyl, sect, sectSize;
memDsk->Get_Geometry(&heads, &cyl, §, §Size);
FILE* f = fopen_wrap(path.c_str(), "wb");
if (!f) { retro_notify(0, RETRO_LOG_ERROR, "Unable to open %s file: %s%s", "OS image", path.c_str(), " (create file failed)"); return; }
for (Bit32u i = 0, total = heads * cyl * sect; i != total; i++) { Bit8u data[512]; memDsk->Read_AbsoluteSector(i, data); fwrite(data, 512, 1, f); }
fclose(f);
delete memDsk;
delete memDrv;
// If using system directory index cache, append the new OS image to that now
if (dbp_system_cached)
if (FILE* fc = fopen_wrap(DBP_GetSaveFile(SFT_SYSTEMDIR).append("DOSBoxPureMidiCache.txt").c_str(), "a"))
{ fprintf(fc, "%s\n", filename); fclose(fc); }
// Set last_info to this new image to support BIOS rebooting with it
startup.mode = RUN_BOOTOS;
startup.info = (int)dbp_osimages.size();
dbp_osimages.emplace_back(filename);
}
const bool have_cd_image = BatchFileBoot::HaveCDImage();
if (!path.empty())
{
// When booting an external disk image as C:, use whatever is C: in DOSBox DOS as the second hard disk in the booted OS (it being E: in Drives[] doesn't matter)
char newC = ((have_cd_image || DBP_IsMounted('D')) ? 'E' : 'D'); // alternative would be to do DBP_Remount('D', 'E'); and always use 'D'
if (imageDiskList['C'-'A'])
imageDiskList[newC-'A'] = imageDiskList['C'-'A'];
else if (!BatchFileBoot::MountOSIMG(newC, (dbp_content_path + ".img").c_str(), "D: drive image", true, false) && Drives['C'-'A'])
{
Bit32u save_hash = 0;
DBP_SetDriveLabelFromContentPath(Drives['C'-'A'], dbp_content_path.c_str(), 'C', NULL, NULL, true);
const char* dfreespace = retro_get_variable("dosbox_pure_bootos_dfreespace", "1024"); // can also be "discard" or "hide"
if (dfreespace && dfreespace[0] != 'h')
{
std::string save_path = DBP_GetSaveFile(SFT_VIRTUALDISK, NULL, &save_hash); // always call to fill out save_hash and dbp_vdisk_filter
Bit32u freeSpace = (Bit32u)atoi(dfreespace);
imageDiskList[newC-'A'] = new imageDisk(Drives['C'-'A'], (freeSpace ? freeSpace : 1024), (freeSpace ? save_path.c_str() : NULL) , save_hash, &dbp_vdisk_filter);
}
}
// Ramdisk setting must be false while installing os
char ramdisk = (is_install ? 'f' : retro_get_variable("dosbox_pure_bootos_ramdisk", "false")[0]);
// Now mount OS hard disk image as C: drive
if (BatchFileBoot::MountOSIMG('C', path.c_str(), "OS image", (ramdisk == 'f'), true) && ramdisk == 'd')
imageDiskList['C'-'A']->SetDifferencingDisk(DBP_GetSaveFile(SFT_DIFFDISK).c_str());
}
else if (!imageDiskList['C'-'A'] && Drives['C'-'A'])
{
// Running without hard disk image uses the DOS C: drive as a read-only C: hard disk
imageDiskList['C'-'A'] = new imageDisk(Drives['C'-'A'], 0);
}
// Try reading a boot disk image off from an ISO file
Bit8u* bootdisk_image; Bit32u bootdisk_size;
if (!Drives['A'-'A'] && Drives['D'-'A'] && dynamic_cast<isoDrive*>(Drives['D'-'A']) && ((isoDrive*)(Drives['D'-'A']))->CheckBootDiskImage(&bootdisk_image, &bootdisk_size))
{
Drives['Y'-'A'] = new memoryDrive();
DriveCreateFile(Drives['Y'-'A'], "CDBOOT.IMG", bootdisk_image, bootdisk_size);
free(bootdisk_image);
DBP_Mount(DBP_AppendImage("$Y:\\CDBOOT.IMG", false), true); // this mounts the image as the A drive
//// Generate autoexec bat that starts the OS setup program?
//DriveCreateFile(Drives['A'-'A'], "CONFIG.SYS", (const Bit8u*)"", 0);
//DriveCreateFile(Drives['A'-'A'], "AUTOEXEC.BAT", (const Bit8u*)"DIR\r\n", 5);
}
// Setup IDE controllers for the hard drives and one CDROM drive (if any CDROM image is mounted)
IDE_SetupControllers(have_cd_image ? 'D' : 0);
// Install the NE2000 network card
NET_SetupEthernet();
// Switch cputype to highest feature set (needed for Windows 9x) and increase real mode CPU cycles
Section* section = control->GetSection("cpu");
section->ExecuteDestroy(false);
section->HandleInputline("cputype=pentium_slow");
if (retro_get_variable("dosbox_pure_bootos_forcenormal", "false")[0] == 't') section->HandleInputline("core=normal");
section->ExecuteInit(false);
section->GetProp("cputype")->MarkFixed();
if (dbp_content_year < 1993 && (CPU_CycleAutoAdjust || (CPU_AutoDetermineMode & (CPU_AUTODETERMINE_CYCLES|(CPU_AUTODETERMINE_CYCLES<<CPU_AUTODETERMINE_SHIFT))))) DBP_SetCyclesByYear(1993, 1993);
RunBatchFile(new BatchFileBoot(!is_install ? 'C' : 'A'));
}
static void RunShell(int shellidx)
{
if (!Drives['C'-'A']) return;
if (dbp_had_game_running) { DBP_OnBIOSReboot(); return; }
dbp_had_game_running = true;
unionDrive* base_drive = dynamic_cast<unionDrive*>(Drives['C'-'A']);
if (!base_drive) return;
std::string path = DBP_GetSaveFile(SFT_SYSTEMDIR).append(dbp_shellzips[shellidx]);
FILE* zip_file_h = fopen_wrap(path.c_str(), "rb");
if (!zip_file_h) { retro_notify(0, RETRO_LOG_ERROR, "Unable to open %s file: %s", "System Shell", path.c_str()); return; }
base_drive->AddUnder(*new zipDrive(new rawFile(zip_file_h, false), false), true);
const char* exes[] = { "C:\\WINDOWS.BAT", "C:\\AUTOEXEC.BAT", "C:\\WINDOWS\\WIN.COM" };
for (const char* exe : exes)
if (Drives['C'-'A']->FileExists(exe + 3))
{ RunBatchFile(new BatchFileExec(exe)); return; }
ConsoleClearScreen();
Bit16u sz;
DOS_WriteFile(STDOUT, (Bit8u*)"To auto run the shell, make sure one of these files exist:\r\n", &(sz = sizeof("To auto run the shell, make sure one of these files exist:\r\n")-1));
for (const char* exe : exes) { DOS_WriteFile(STDOUT, (Bit8u*)"\r\n- ", &(sz = 4)); DOS_WriteFile(STDOUT, (Bit8u*)exe, &(sz = (Bit16u)strlen(exe))); }
DOS_WriteFile(STDOUT, (Bit8u*)"\r\n\r\n", &(sz = 4));
KEYBOARD_AddKey(KBD_enter, true);
KEYBOARD_AddKey(KBD_enter, false);
}
enum EMode : Bit8u { RUN_NONE, RUN_EXEC, RUN_BOOTIMG, RUN_BOOTOS, RUN_INSTALLOS, RUN_SHELL, RUN_VARIANT, RUN_COMMANDLINE };
static struct Startup { EMode mode; bool reboot; int info; std::string exec; } startup;
static struct Autoboot { Startup startup; bool have, use; int skip; Bit32u hash; } autoboot;
static struct Autoinput { std::string str; const char* ptr; Bit32s oldcycles; Bit8u oldchange; Bit16s oldyear; } autoinput;
static struct Patch { int enabled_variant; bool show_default; } patch;
static bool Run(EMode mode, int info, std::string& str, bool from_osd = false)
{
DBP_ASSERT(from_osd || mode != RUN_VARIANT); // only the OSD can switch the variant
if (from_osd) autoinput.str.clear();
if (mode == RUN_VARIANT)
{
patch.enabled_variant = info;
patchDrive::ActivateVariant(patch.enabled_variant);
DOSYMLLoader ymlload(true);
startup.reboot |= ymlload.reboot; // read startup and autoinput from YML
mode = startup.mode;
info = startup.info;
if (mode == RUN_NONE) return false; // YML had no startup
autoboot.use = !ymlload.is_utility; // disable autoboot for utility config
autoboot.skip = 0; // otherwise force enable auto start when switching variant
WriteAutoBoot(RUN_VARIANT, patch.enabled_variant, str);
}
else
{
if (from_osd) WriteAutoBoot(mode, info, str);
startup.mode = mode;
startup.info = info;
if (mode == RUN_EXEC) startup.exec.swap(str); // remember to set cursor again and for rebooting a different IT_RUN
}
const Property* bootImgMachine = ((mode == RUN_BOOTIMG) ? control->GetProp("dosbox", "machine") : NULL);
if (startup.reboot || dbp_game_running || (bootImgMachine && info && info != *(const char*)bootImgMachine->GetValue() && !bootImgMachine->IsFixed()))
{
startup.reboot = false;
if (bootImgMachine) dbp_reboot_machine = (info ? (char)info : *(const char*)bootImgMachine->GetValue());
DBP_OnBIOSReboot();
return true;
}
if (autoboot.use && autoboot.skip)
{
autoinput.str.assign(31, ' ');
autoinput.str.resize(sprintf(&autoinput.str[0], (autoboot.skip == -1 ? "(WAITMODECHANGE)" : "(WAIT:%d)"), autoboot.skip * 15));
}
autoinput.ptr = ((mode != RUN_COMMANDLINE && autoinput.str.size()) ? autoinput.str.c_str() : NULL);
autoinput.oldcycles = 0;
if (autoinput.ptr && dbp_content_year > 1970 && (CPU_CycleAutoAdjust || (CPU_AutoDetermineMode & (CPU_AUTODETERMINE_CYCLES|(CPU_AUTODETERMINE_CYCLES<<CPU_AUTODETERMINE_SHIFT)))))
{
// enforce cycle rate during auto input (but limited to 1994 CPU speed, above will likely just waste time waiting for rendering out the skipped frames)
autoinput.oldcycles = CPU_CycleMax;
autoinput.oldchange = (Bit8u)control->GetProp("cpu", "cycles")->getChange();
autoinput.oldyear = dbp_content_year;
if (dbp_content_year > 1994) dbp_content_year = 1994;
DBP_SetCyclesByYear(dbp_content_year, 1994);
}
// if a booted OS does a bios reboot, auto reboot that OS from now on
if (mode == RUN_EXEC || mode == RUN_COMMANDLINE)
startup.mode = RUN_NONE;
if (mode == RUN_EXEC)
Exec(startup.exec);
else if (mode == RUN_BOOTIMG)
BootImage();
else if (mode == RUN_BOOTOS || mode == RUN_INSTALLOS)
BootOS(mode == RUN_INSTALLOS, startup.info);
else if (mode == RUN_SHELL)
RunShell(startup.info);
return true;
}
struct DOSYMLLoader
{
const char *Key, *End, *Next, *KeyX, *Val, *ValX, *first_startup_mode_key = NULL;
int cpu_cycles = 0, cpu_hz = 0, cpu_year = 0, cpu_set_max = 0;
bool reboot, is_utility = false;
bool Parse(const char *yml_key, const char* db_section, const char* db_key, ...)
{
if (yml_key && (strncmp(yml_key, Key, (size_t)(KeyX - Key)) || yml_key[KeyX - Key])) return false;
va_list ap; va_start(ap, db_key); std::string val;
for (;;)
{
const char* mapFrom = va_arg(ap, const char*);
if (!*mapFrom) { va_end(ap); return false; }
if (*mapFrom == '~')
{
val.append(Val, (size_t)(ValX - Val));
}
else if (*mapFrom == '/')
{
char buf[32];
sprintf(buf, "%d", (atoi(Val) / 1024));
val = buf;
}
else if (*mapFrom == '^')
{
if (db_key[1] == 'p') // called by ProcessKey() with 'mpu401'
{
Parse(yml_key, "midi", "midiconfig", mapFrom);
val.assign("intelligent");
}
else // recursively called from above with 'midiconfig'
{
const char* p = dbp_content_path.c_str(), *fs = strrchr(p, '/'), *bs = strrchr(p, '\\');
(((val += '^') += (yml_key[7] == 't' ? 'M' : 'S')).append(p, (fs > bs ? (fs - p) : bs ? (bs - p) : 0)) += CROSS_FILESPLIT).append(Val, (size_t)(ValX - Val));
}
}
else
{
const char* mapTo = va_arg(ap, const char*);
if (strncmp(mapFrom, Val, (size_t)(ValX - Val))) continue;
val.append(mapTo);
}
va_end(ap);
Section* section = control->GetSection(db_section);
Property* prop = section->GetProp(db_key);
const Value& propVal = prop->GetValue();
const char* set_val = val.c_str();
bool set = !strcmp(set_val, (propVal.type == Value::V_STRING ? (const char*)propVal : propVal.ToString().c_str()));
if (!set)
{
bool will_reboot = (reboot || (prop->getChange() > Property::Changeable::WhenIdle));
if (!will_reboot) section->ExecuteDestroy(false);
set = (!set && prop->SetValue(val) && !strcmp(set_val, (propVal.type == Value::V_STRING ? (const char*)propVal : propVal.ToString().c_str())));
if (!will_reboot) section->ExecuteInit(false);
if (set) reboot = will_reboot;
}
if (set) prop->MarkFixed();
return set;
}
}
bool ParseCPU(const char *yml_key)
{
if (strncmp(yml_key, Key, (size_t)(KeyX - Key)) || yml_key[KeyX - Key]) return false;
again: switch (yml_key[4])
{
case 'm': cpu_set_max = 1; yml_key += 4; goto again; // cpu_max_*
case 'c': return ((cpu_cycles = atoi(Val)) >= 100); // cpu_cycles
case 'h': return ((cpu_hz = atoi(Val)) >= 500); // cpu_hz
case 'y': return ((cpu_year = atoi(Val)) >= 1970); // cpu_year
}
return false;
}
bool ParseRun(const char *yml_key)
{
if (strncmp(yml_key, Key, (size_t)(KeyX - Key)) || yml_key[KeyX - Key]) return false;
switch (yml_key[4])
{
case 'i': // run_input
autoinput.ptr = NULL;
autoinput.str.clear();
autoinput.str.append(Val, (size_t)(ValX - Val));
break;
case 'p': // run_path
startup.exec = std::string(Val, (size_t)(ValX - Val));
if (startup.mode == RUN_BOOTIMG) goto exec2bootimg;
if (!first_startup_mode_key) first_startup_mode_key = Key;
startup.mode = RUN_EXEC;
break;
case 'b': // run_boot
case 'm': // run_mount
{
int imgidx = -1;
for (DBP_Image& i : dbp_images)
if ((i.path.size() == 4+(ValX - Val) && i.path[0] == '$' && !strncasecmp(&i.path[4], Val, (ValX - Val)))
|| (i.longpath.size() == (ValX - Val) && !strncasecmp(&i.longpath[0], Val, (ValX - Val))))
{ imgidx = (int)(&i - &dbp_images[0]); break; }
if (imgidx == -1) return false;
dbp_images[imgidx].remount = true;
}
if (yml_key[4] == 'm') break; // run_mount
if (startup.mode == RUN_EXEC)
{
exec2bootimg:
((static_cast<Section_line*>(control->GetSection("autoexec"))->data += '@') += startup.exec) += '\n';
}
if (!first_startup_mode_key) first_startup_mode_key = Key;
startup.mode = RUN_BOOTIMG;
startup.info = 0;
break;
case 'u': // run_utility
is_utility = ((Val[0]|0x20) == 't');
break;
}
return true;
}
bool ProcessKey()
{
switch (*Key)
{
case 'c':
return (0
||Parse("cpu_type", "cpu", "cputype" , "auto","auto" , "generic_386","386" , "generic_486","486_slow" , "generic_pentium","pentium_slow" , "")
||ParseCPU("cpu_cycles")||ParseCPU("cpu_hz")||ParseCPU("cpu_year")||ParseCPU("cpu_max_cycles")||ParseCPU("cpu_max_hz")||ParseCPU("cpu_max_year")
);
case 'm':
return (0
||Parse("mem_size", "dosbox", "memsize", "/")
||Parse("mem_xms", "dos", "xms" , "true","true" , "false","false" , "")
||Parse("mem_ems", "dos", "ems" , "true","true" , "false","false" , "")
||Parse("mem_umb", "dos", "umb" , "true","true" , "false","false" , "")
||Parse("mem_doslimit", "dos", "memlimit", "~")
);
case 'v':
return (0
||Parse("video_card", "dosbox", "machine" , "generic_svga","svga_s3" , "generic_hercules","hercules" , "generic_cga","cga" , "generic_tandy","tandy" , "generic_pcjr","pcjr" , "generic_ega","ega" , "generic_vga","vgaonly" , "svga_s3_trio","svga_s3", "svga_tseng_et3000","svga_et3000" , "svga_tseng_et4000","svga_et4000" , "svga_paradise_pvga1a","svga_paradise" , "")
||Parse("video_memory", "dosbox", "vmemsize", "/")
||Parse("video_voodoo", "pci", "voodoo" , "true","8mb" , "false","false" , "")
);
case 's':
return (0
||Parse("sound_card", "sblaster", "sbtype" , "sb16","sb16" , "sb1","sb1" , "sb2","sb2" , "sbpro1","sbpro1" , "sbpro2","sbpro2" , "gameblaster","gb" , "none","none" , "")
||Parse("sound_port", "sblaster", "sbbase" , "~")
||Parse("sound_irq", "sblaster", "irq", "~")
||Parse("sound_dma", "sblaster", "dma", "~")
||Parse("sound_hdma", "sblaster", "hdma", "~")
||Parse("sound_midi", "midi", "mpu401" , "true","intelligent" , "false","none" , "^")
||Parse("sound_mt32", "midi", "mpu401" , "true","intelligent" , "false","none" , "^")
||Parse("sound_gus", "gus", "gus" , "true","true" , "false","false" , "")
||Parse("sound_tandy", "speaker", "tandy" , "true","on" , "false","auto" , "")
);
case 'r':
return (0
||ParseRun("run_path")
||ParseRun("run_boot")
||ParseRun("run_mount")
||ParseRun("run_input")
||ParseRun("run_utility")
);
}
return false;
}
DOSYMLLoader(bool parseRun, bool isPreInit = false) : reboot(isPreInit)
{
if (parseRun) startup.mode = RUN_NONE;
for (Key = patchDrive::dos_yml.c_str(), End = Key+patchDrive::dos_yml.size(); Key < End; Key = Next + 1)
{
for (Next = Key; *Next != '\n' && *Next != '\r' && *Next; Next++) {}
if (Next == Key || *Key == '#') continue;
for (KeyX = Key; *KeyX && *KeyX != ':' && *KeyX > ' '; KeyX++) {}
if (*KeyX != ':' || KeyX == Key || KeyX[1] != ' ' ) goto syntaxerror;
for (Val = KeyX + 2; *Val == ' '; Val++) {}
for (ValX = Val; *ValX && *ValX != '\r' && *ValX != '\n' && (*ValX != '#' || ValX[-1] != ' '); ValX++) {}
while (ValX[-1] == ' ') ValX--;
if (ValX <= Val) goto syntaxerror;
if ((*Key == 'r' && !parseRun) || ProcessKey()) continue;
syntaxerror:
retro_notify(0, RETRO_LOG_ERROR, "Error in DOS.YML: %.*s", (int)(Next-Key), Key);
continue;
}
if (cpu_cycles || cpu_year || cpu_hz)
{
if (cpu_cycles) {}
else if (cpu_year) cpu_cycles = (int)DBP_CyclesForYear(cpu_year);
else
{
float cycle_per_hz = .3f; // default with auto (just a bad guess)
switch (*(const char*)control->GetProp("cpu", "cputype")->GetValue())
{
case 'p': cycle_per_hz = .55700f; break; // Pentium (586): Mhz * 557.00
case '4': cycle_per_hz = .38000f; break; // 486: Mhz * 380.00
case '3': cycle_per_hz = .18800f; break; // 386: Mhz * 188.00
case '2': cycle_per_hz = .09400f; break; // AT (286): Mhz * 94.00
case '8': cycle_per_hz = .05828f; break; // XT (8088/8086): Mhz * 58.28
}
cpu_cycles = (int)(cpu_hz * cycle_per_hz + .4999f);
}
char buf[32];
ValX = (Val = buf) + sprintf(buf, "%s%d", (cpu_set_max ? "max limit " : ""), (int)cpu_cycles);
if (Parse(NULL, "cpu", "cycles", "~") && cpu_cycles >= 8192) // Switch to dynamic core for newer real mode games
{ ValX = (Val = "dynamic") + 7; Parse(NULL, "cpu", "core", "~"); }
}
}
};
static bool PostInitFirstTime()
{
ReadAutoBoot();
size_t root_yml_len = patchDrive::dos_yml.size();
patchDrive::ActivateVariant(patch.enabled_variant);
DOSYMLLoader ymlload(true);
// Show default variant as an option only if there is a auto run setting in the base root yml
patch.show_default = (ymlload.first_startup_mode_key && ymlload.first_startup_mode_key < patchDrive::dos_yml.c_str() + root_yml_len);
// reset and re-run PreInit to set all settings of yml
if (ymlload.reboot) return true;
if (autoboot.use && autoboot.startup.mode != RUN_VARIANT) startup = autoboot.startup;
else if (startup.mode != RUN_NONE && !autoboot.use && patchDrive::variants.Len()) startup.mode = RUN_NONE;
return false;
}
static void PreInit()
{
if (!dbp_biosreboot) startup.mode = RUN_NONE;
if (patchDrive::dos_yml.size()) DOSYMLLoader(!dbp_biosreboot && (patchDrive::variants.Len() == 0 || autoboot.startup.mode == RUN_VARIANT), true); // ignore run keys on bios reboot
if (!dbp_biosreboot && autoboot.use && autoboot.startup.mode != RUN_VARIANT) startup = autoboot.startup;
}
static void ReadAutoBoot()
{
char buf[DOS_PATHLENGTH + 32 + 256 + 256 + 2];
Bit16u autostrlen = DriveReadFileBytes(Drives['C'-'A'], "AUTOBOOT.DBP", (Bit8u*)buf, (Bit16u)(sizeof(buf)-1));
autoboot.have = !!autostrlen;
const char* cpath = (autostrlen ? NULL : strrchr(dbp_content_path.c_str(), '#'));
if (cpath && (dbp_content_path.c_str() + dbp_content_path.length() - cpath) <= DOS_PATHLENGTH)
autostrlen = (Bit16u)sprintf(buf, "%s%s", (cpath[1] && cpath[2] == ':' ? "" : "C:\\"), cpath + 1);
for (char *p = buf, *pEnd = p + autostrlen, *line, line_no = 1; p != pEnd; line_no++)
{
if ((p += (!*p ? (!p[1] ? 2 : 1) : 0)) >= pEnd) break;
for (line = p; p != pEnd && *p >= ' ';) p++;
if (*p == '\r' && p[1] == '\n') p[1] = '\0';
*p = '\0'; // for strcmp/atoi/DOS_FileExists/assign
if (line_no == 1)
{
char linetype = (line[1] == '*' ? line[0] : 0), *str = line + (linetype ? 2 : 0);
if (linetype == 0)
{
char *param = strchr(str, ' ');
if (param) *param = '\0';
bool exists = DOS_FileExists(str);
if (param) *param = ' ';
if (exists) { autoboot.startup.mode = RUN_EXEC; autoboot.startup.exec.assign(str); }
}
else if (linetype == 'O' || linetype == 'S')
{
const size_t suffix_len = (linetype == 'O' ? 4: 5);
const std::vector<std::string>& strs = (linetype == 'O' ? dbp_osimages : dbp_shellzips);
for (const std::string& it : strs)
if (it.size() == (p - str) + suffix_len && !memcmp(str, it.c_str(), it.size() - suffix_len))
{ autoboot.startup.mode = (linetype == 'O' ? RUN_BOOTOS : RUN_SHELL); autoboot.startup.info = (int)(&it - &strs[0]); break; }
}
else if (linetype == 'V' && patchDrive::variants.Len()) { autoboot.startup.mode = RUN_VARIANT; autoboot.skip = 0; line += 2; goto parseVariant; }
else if (linetype == 'I')
{
for (const char* it : DBP_MachineNames)
if (!strcmp(it, str))
{ autoboot.startup.mode = RUN_BOOTIMG; autoboot.startup.info = (Bit16s)(it[0]|0x20); break; }
}
}
else if (line_no == 2)
{
autoboot.skip = atoi(line);
}
else if (line_no == 3 && *line)
{
for (const DBP_Image& i : dbp_images)
if (!strcmp(DBP_Image_Label(i), line))
{ if (!i.mounted) DBP_Mount((unsigned)(&i - &dbp_images[0]), true); break; }
}
else if (line_no == 4)
{
parseVariant:
if (const std::string* v = patchDrive::variants.Get(line))
patch.enabled_variant = patchDrive::variants.GetStorageIndex(v) + 1;
}
}
autoboot.use = (autoboot.startup.mode != RUN_NONE);
autoboot.hash = HashAutoBoot();
}
static void WriteAutoBoot(EMode mode, int info, const std::string& str)
{
if (!autoboot.use || mode == RUN_NONE || mode == RUN_INSTALLOS || mode == RUN_COMMANDLINE)
{
if (autoboot.have) Drives['C'-'A']->FileUnlink((char*)"AUTOBOOT.DBP");
autoboot.startup.mode = RUN_NONE; autoboot.have = autoboot.use = false;
return;
}
DBP_ASSERT(mode == RUN_EXEC || mode == RUN_BOOTOS || mode == RUN_SHELL || mode == RUN_VARIANT || mode == RUN_BOOTIMG);
autoboot.startup.mode = mode;
autoboot.startup.info = info;
autoboot.startup.exec.assign(mode == RUN_EXEC ? str.c_str() : "");
if (HashAutoBoot() == autoboot.hash) return;
autoboot.have = true;
autoboot.hash = HashAutoBoot();
const char* varname = (patch.enabled_variant ? patchDrive::variants.GetStorage()[patch.enabled_variant - 1].c_str() : NULL), *img = NULL;
const char *var = ((varname && mode != RUN_VARIANT) ? varname : NULL), *line1 = (mode != RUN_VARIANT ? str.c_str() : (varname ? varname : ""));
for (const DBP_Image& i : dbp_images) { if (i.mounted) { if (&i != &dbp_images[0]) img = DBP_Image_Label(i); break; } }
char buf[DOS_PATHLENGTH + 32 + 256 + 256], *p = buf;
if (mode != RUN_EXEC) { *(p++) = (mode == RUN_BOOTOS ? 'O' : mode == RUN_SHELL ? 'S' : mode == RUN_VARIANT ? 'V' : 'I'); *(p++) = '*'; }
if (1) p += snprintf(p, (&buf[sizeof(buf)] - p), "%s", line1); // line 1
if (var || img || autoboot.skip) p += snprintf(p, (&buf[sizeof(buf)] - p), "\r\n%d", autoboot.skip); // line 2
if (var || img) p += snprintf(p, (&buf[sizeof(buf)] - p), "\r\n%s", (img ? img : "")); // line 3
if (var) p += snprintf(p, (&buf[sizeof(buf)] - p), "\r\n%s", var); // line 4
if (!DriveCreateFile(Drives['C'-'A'], "AUTOBOOT.DBP", (Bit8u*)buf, (Bit32u)(p - buf))) { DBP_ASSERT(false); }
}
static Bit32u HashAutoBoot() { return DriveCalculateCRC32((Bit8u*)&autoboot, sizeof(autoboot), BaseStringToPointerHashMap::Hash(autoboot.startup.exec.c_str())); }
static Bit32u ModeHash() { return (Bit32u)((render.src.width * 2100781) ^ (render.src.height * 65173) ^ ((Bitu)(render.src.fps * 521)) ^ (render.src.bpp * 31) ^ ((Bitu)vga.mode + 1)); }
static void ProcessAutoInput()
{
extern Bitu PIC_Ticks;
static Bitu InpTickStart, InpNextTick; static Bit32u InpDelay, InpReleaseKey, InpSkipMode;
if (autoinput.ptr == autoinput.str.c_str())
InpTickStart = PIC_Ticks, InpNextTick = 0, InpDelay = 70, InpReleaseKey = InpSkipMode = 0;
const Bitu InpDoneTicks = PIC_Ticks - InpTickStart;
if (InpSkipMode && !vga.draw.resizing)
{
Bit32u mode = ModeHash();
if (InpSkipMode == mode) { } // video mode unchanged
else if (InpSkipMode < 31) { InpSkipMode = mode; } // initial resolution was set (before it was size 0)
else { InpSkipMode = 0; InpNextTick = InpDoneTicks; } // new video mode
}
while (InpDoneTicks >= InpNextTick)
{
if (InpReleaseKey)
{
if (InpReleaseKey & 0x100) { KEYBOARD_AddKey(KBD_rightalt, false); InpReleaseKey &= 0xFF; }
if (InpReleaseKey & 0x80) { KEYBOARD_AddKey(KBD_leftshift, false); InpReleaseKey &= 0x7F; }
KEYBOARD_AddKey((KBD_KEYS)InpReleaseKey, false);
InpReleaseKey = 0;
if (*autoinput.ptr) { InpNextTick += InpDelay; continue; }
}
if (!*autoinput.ptr) { autoinput.ptr = NULL; break; }
const char *cmd = autoinput.ptr, *cmdNext = cmd + 1, *cmdColon = NULL;
bool bShift = false, bAltGr = false;
char tmp;
Bit32u i = 0, cmdlen = 1;
if (cmd[0] != '(' || cmd[1] == '(')
{
if (!(cmd[0] != '(')) cmdNext++; // Treat (( as textinput (
KBD_KEYS mappedkey = KBD_NONE;
char DBP_DOS_KeyboardLayout_MapChar(char c, bool& bShift, bool& bAltGr);
switch ((tmp = DBP_DOS_KeyboardLayout_MapChar(cmd[0], bShift, bAltGr)))
{
case '\x1B': i = KBD_esc; break;
case '-': i = KBD_minus; break;
case '=': i = KBD_equals; break;
case '\b': i = KBD_backspace; break;
case '\t': i = KBD_tab; break;
case '[': i = KBD_leftbracket; break;
case ']': i = KBD_rightbracket; break;
case ';': i = KBD_semicolon; break;
case '\'': i = KBD_quote; break;
case '`': i = KBD_grave; break;
case '\\': i = KBD_backslash; break;
case ',': i = KBD_comma; break;
case '.': i = KBD_period; break;
case '/': i = KBD_slash; break;
default: cmd = &tmp;
}
}
else if ((cmdNext = strchr(cmdNext, ')')) != NULL)
{
if ((cmdColon = strchr(++cmd, ':')) != NULL && cmdColon >= cmdNext-1) cmdColon = NULL;
cmdlen = (Bit32u)((cmdColon ? cmdColon : cmdNext) - cmd);
cmdNext++;
}
static const char* DBP_Commands[KBD_LAST+3] =
{
"","1","2","3","4","5","6","7","8","9","0","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
"f1","f2","f3","f4","f5","f6","f7","f8","f9","f10","f11","f12","esc","tab","backspace","enter","space","leftalt","rightalt","leftctrl","rightctrl","leftshift","rightshift",
"capslock","scrolllock","numlock","grave","minus","equals","backslash","leftbracket","rightbracket","semicolon","quote","period","comma","slash","extra_lt_gt",
"printscreen","pause","insert","home","pageup","delete","end","pagedown","left","up","down","right","kp1","kp2","kp3","kp4","kp5","kp6","kp7","kp8","kp9","kp0",
"kpdivide","kpmultiply","kpminus","kpplus","kpenter","kpperiod",
"wait","waitmodechange","delay"
};
if (i == 0)
for (; i != KBD_LAST+3; i++)
if (!strncasecmp(DBP_Commands[i], cmd, cmdlen) && DBP_Commands[i][cmdlen] == '\0')
break;
if (i == KBD_LAST+0 && cmdColon) // wait command
{
InpNextTick += atoi(cmdColon+1);
}
else if (i == KBD_LAST+1) // waitmodechange command
{
if (vga.draw.resizing && InpDoneTicks && (InpDoneTicks - InpNextTick < 5000)) break; // don't start while vga is resizing
InpNextTick += 30000; // wait max 30 seconds (if the game crashes, auto input is aborted below)
InpSkipMode = ModeHash();
}
else if (i == KBD_LAST+2 && cmdColon) // delay command
{
InpDelay = (Bit32u)atoi(cmdColon+1);
}
else if (i < KBD_LAST && cmdColon && (!strncasecmp(cmdColon+1, "down", 4) || strncasecmp(cmdColon+1, "up", 2))) // key command
{
KEYBOARD_AddKey((KBD_KEYS)i, (cmdColon[1]|0x20) == 'd');
}
else if (i < KBD_LAST) // key press
{
if (bShift) KEYBOARD_AddKey(KBD_leftshift, true);
if (bAltGr) KEYBOARD_AddKey(KBD_rightalt, true);
KEYBOARD_AddKey((KBD_KEYS)i, true);
InpReleaseKey = (i | (bShift ? 0x80 : 0) | (bAltGr ? 0x100 : 0));
InpNextTick += 70; // fixed press duration
}
else
{
log_cb(RETRO_LOG_INFO, "[DOSBOX ERROR] Unknown command in run_input string: '%s'\n", cmd);
autoinput.ptr = NULL;
break;
}
autoinput.ptr = cmdNext;
}
// Check if done, dbp_game_running should switch to true at tick 1 unless the game crashes or exits but give it 5 seconds to be sure
if (autoinput.ptr && (dbp_game_running || InpDoneTicks < 5000))
{
// Disable line rendering (without using VGA frameskipping which affects the emulation)
struct Local { static void EmptyLineHandler(const void*) { } };
RENDER_DrawLine = Local::EmptyLineHandler;
// Scrap mixed audio instead of accumulating it while skipping frames
DBP_MIXER_ScrapAudio();
}
else
{
// done
autoinput.ptr = NULL; // reset on game crash/exit (dbp_game_running is false)
DBP_KEYBOARD_ReleaseKeys();
if (autoinput.oldcycles)
{
if (!CPU_CycleAutoAdjust && CPU_CycleMax == DBP_CyclesForYear(dbp_content_year, 1994) && control->GetProp("cpu", "cycles")->getChange() == autoinput.oldchange)
CPU_CycleMax = autoinput.oldcycles; // revert from Run()
else if (CPU_CycleAutoAdjust && cpu.pmode && (CPU_AutoDetermineMode & (CPU_AUTODETERMINE_CORE<<CPU_AUTODETERMINE_SHIFT)))
CPU_OldCycleMax = autoinput.oldcycles; // we switched to protected mode since auto input, fix up old cycles
dbp_content_year = autoinput.oldyear;
DBP_SetRealModeCycles(); // if still in real mode reset the defaults
}
}
}
};
DBP_Run::Startup DBP_Run::startup;
DBP_Run::Autoinput DBP_Run::autoinput;
DBP_Run::Autoboot DBP_Run::autoboot;
DBP_Run::Patch DBP_Run::patch;