From 7c97eaae7d26349a7d70729712560fd37bc41ddf Mon Sep 17 00:00:00 2001 From: Maximilien Noal Date: Thu, 3 Dec 2026 09:38:16 +0100 Subject: [PATCH 1/2] fix(dos): preserve MSCDEX entries when remounting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...DosBatchExecutionEngine.CommandHandlers.cs | 4 +-- .../OperatingSystem/DosDriveManager.cs | 4 +-- .../AsmFixtures/DosBatchScenarioCatalog.cs | 23 ++++++++++++++++++ .../mscdex_drive_count.asm | 20 +++++++++++++++ .../mscdex_drive_count.com | Bin 0 -> 29 bytes .../GeneratedCodeReadability/bounds.tsv | 2 ++ 6 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm create mode 100644 tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.com diff --git a/src/Spice86.Core/Emulator/OperatingSystem/DosBatchExecutionEngine.CommandHandlers.cs b/src/Spice86.Core/Emulator/OperatingSystem/DosBatchExecutionEngine.CommandHandlers.cs index 2ca17c94d9..5acb585a4b 100644 --- a/src/Spice86.Core/Emulator/OperatingSystem/DosBatchExecutionEngine.CommandHandlers.cs +++ b/src/Spice86.Core/Emulator/OperatingSystem/DosBatchExecutionEngine.CommandHandlers.cs @@ -1354,9 +1354,9 @@ private void MountCdRomImages(char driveLetter, IReadOnlyList imagePaths CdRomDrive drive = new CdRomDrive(images, _channelCreator, _activityNotifier, driveLetter); byte driveIndex = DosDriveManager.TryGetLetterIndex(driveLetter, out int idx) ? (byte)idx : (byte)3; MscdexDriveEntry entry = new MscdexDriveEntry(driveLetter, driveIndex, drive); - _mscdex.AddDrive(entry); string volumeLabel = drive.Image.PrimaryVolume.VolumeIdentifier ?? string.Empty; _driveManager.RegisterCdRomDriveLetter(driveLetter, string.Empty, volumeLabel, drive); + _mscdex.AddDrive(entry); string paths = string.Join(", ", imagePaths); WriteToStandardOutput($"Drive {driveLetter}: mounted {imagePaths.Count} CD-ROM image(s): {paths}\r\n"); } catch (IOException ex) { @@ -1374,8 +1374,8 @@ private bool MountCdRomFolder(char driveLetter, string hostPath) { CdRomDrive drive = new CdRomDrive(image, _channelCreator, _activityNotifier, driveLetter); byte driveIndex = DosDriveManager.TryGetLetterIndex(driveLetter, out int idx) ? (byte)idx : (byte)3; MscdexDriveEntry entry = new MscdexDriveEntry(driveLetter, driveIndex, drive); - _mscdex.AddDrive(entry); _driveManager.RegisterCdRomDriveLetter(driveLetter, hostPath, volumeLabel, drive); + _mscdex.AddDrive(entry); return true; } catch (IOException ex) { WriteToStandardOutput($"MOUNT: failed to create CD-ROM drive from folder: {ex.Message}\r\n"); diff --git a/src/Spice86.Core/Emulator/OperatingSystem/DosDriveManager.cs b/src/Spice86.Core/Emulator/OperatingSystem/DosDriveManager.cs index 1383874489..3cac26c215 100644 --- a/src/Spice86.Core/Emulator/OperatingSystem/DosDriveManager.cs +++ b/src/Spice86.Core/Emulator/OperatingSystem/DosDriveManager.cs @@ -707,8 +707,8 @@ public void MountFolderAsCdRom(char driveLetter, string hostPath) { CdRomDrive drive = new CdRomDrive(image, _channelCreator, _activityNotifier, upper); byte driveIndex = GetDriveIndexOrDefault(upper); MscdexDriveEntry entry = new MscdexDriveEntry(upper, driveIndex, drive); - _mscdex.AddDrive(entry); RegisterCdRomDriveLetter(upper, hostPath, volumeLabel, drive); + _mscdex.AddDrive(entry); if (_loggerService.IsEnabled(LogLevel.Information)) { _loggerService.LogInformation("MOUNT: Drive {Drive}: is now backed by folder {Path}", upper, hostPath); } @@ -756,9 +756,9 @@ public void MountImageAsCdRom(char driveLetter, string imagePath) { CdRomDrive drive = new CdRomDrive(image, _channelCreator, _activityNotifier, upper); byte driveIndex = GetDriveIndexOrDefault(upper); MscdexDriveEntry entry = new MscdexDriveEntry(upper, driveIndex, drive); - _mscdex.AddDrive(entry); string volumeLabel = image.PrimaryVolume.VolumeIdentifier ?? string.Empty; RegisterCdRomDriveLetter(upper, string.Empty, volumeLabel, drive); + _mscdex.AddDrive(entry); if (_loggerService.IsEnabled(LogLevel.Information)) { _loggerService.LogInformation("IMGMOUNT: Mounted image {Image} on drive {Drive}:", imagePath, upper); } diff --git a/tests/Spice86.Tests/AsmFixtures/DosBatchScenarioCatalog.cs b/tests/Spice86.Tests/AsmFixtures/DosBatchScenarioCatalog.cs index 4cfe8a7adf..a33aa08d09 100644 --- a/tests/Spice86.Tests/AsmFixtures/DosBatchScenarioCatalog.cs +++ b/tests/Spice86.Tests/AsmFixtures/DosBatchScenarioCatalog.cs @@ -275,6 +275,7 @@ internal static class DosBatchScenarioCatalog { VerPrintsVersion(), VolPrintsVolumeLabel(), VolOnMountedCdDrivePrintsMediaLabel(), + MountRemountPreservesMscdexRegistration(), DateHelpSwitchPrintsUsage(), TimeHelpSwitchPrintsUsage(), DirAdListsDirectoriesOnly(), @@ -1914,6 +1915,28 @@ private static AsmScenario VolOnMountedCdDrivePrintsMediaLabel() { }); } + /// MOUNT of the same drive letter remounts the drive: the MSCDEX registration of the + /// first mount survives, so a DOS program querying the MSCDEX drive count still sees the CD drive + /// after the remount. + /// The scenario. + private static AsmScenario MountRemountPreservesMscdexRegistration() { + return Scenario("batch_mount_remount_preserves_mscdex_drive", run => run + .ResourceCopy(ProgramDirectory, "mscdex_drive_count.com", "PROBE.COM") + .Folder("DISC1") + .Folder("DISC2") + .StartBatch( + $"MOUNT D \"{run.PathOf("DISC1")}\" -t cdrom\r\n" + + "PROBE.COM > FIRST.BIN\r\n" + + $"MOUNT D \"{run.PathOf("DISC2")}\" -t cdrom\r\n" + + "PROBE.COM > SECOND.BIN\r\n", + _ => { + File.ReadAllBytes(run.PathOf("FIRST.BIN"))[0].Should().Be(1, + "after the first MOUNT, MSCDEX reports one CD drive"); + File.ReadAllBytes(run.PathOf("SECOND.BIN"))[0].Should().Be(1, + "remounting the same drive letter must keep the MSCDEX registration"); + })); + } + /// DATE /? prints its usage text. /// The scenario. private static AsmScenario DateHelpSwitchPrintsUsage() { diff --git a/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm b/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm new file mode 100644 index 0000000000..92a3e0406a --- /dev/null +++ b/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm @@ -0,0 +1,20 @@ +; Build: fasm mscdex_drive_count.asm mscdex_drive_count.com +; Queries the MSCDEX CD drive count with INT 2Fh AH=15h AL=00h, writes the count +; (BX) as one byte to standard output with INT 21h AH=40h, then exits. The test +; reads the output byte back and expects the CD drive to still be registered. +use16 +org 100h + + mov ah, 15h + mov al, 0 + int 2Fh + mov [count], bl + mov ah, 40h + mov bx, 1 + mov cx, 1 + mov dx, offset count + int 21h + mov ax, 4C00h + int 21h + +count: rb 1 diff --git a/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.com b/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.com new file mode 100644 index 0000000000000000000000000000000000000000..e4b5b512ffae2ebab5ffc1b74b17095a6c5b3cf0 GIT binary patch literal 29 lcmdlYx`E-Weutb4!xo3#j0`&&8FtAqoK@Vx;B!`y0RWTo30MFC literal 0 HcmV?d00001 diff --git a/tests/Spice86.Tests/Resources/GeneratedCodeReadability/bounds.tsv b/tests/Spice86.Tests/Resources/GeneratedCodeReadability/bounds.tsv index 6660932c6c..c6b1de55a4 100644 --- a/tests/Spice86.Tests/Resources/GeneratedCodeReadability/bounds.tsv +++ b/tests/Spice86.Tests/Resources/GeneratedCodeReadability/bounds.tsv @@ -283,6 +283,8 @@ batch_md_existing_error.ivt.spec program 0 0 0 0 0 0 0 0 0 0 0 0 2 0 - batch_md_existing_error.ivt.spec support 0 0 0 0 3 0 0 4 8 4 0 0 105 0 - batch_mkdir_alias.ivt.spec program 0 0 0 0 0 0 0 0 0 0 0 0 2 0 - batch_mkdir_alias.ivt.spec support 0 0 0 0 3 0 0 4 8 4 0 0 105 0 - +batch_mount_remount_preserves_mscdex_drive.ivt.spec program 1 0 0 0 3 6 5 0 0 0 0 0 16 2 serviceCalls.Dos>=2 +batch_mount_remount_preserves_mscdex_drive.ivt.spec support 0 0 0 0 13 0 0 5 10 6 0 0 150 1 - batch_move_missing_source.ivt.spec program 0 0 0 0 0 0 0 0 0 0 0 0 2 0 - batch_move_missing_source.ivt.spec support 0 0 0 0 3 0 0 4 8 4 0 0 105 0 - batch_move_rename.ivt.spec program 0 0 0 0 0 0 0 0 0 0 0 0 2 0 - From a1ffdc37c1d21e6ebfdbe96ec2c4608afef6279a Mon Sep 17 00:00:00 2001 From: Maximilien Noal Date: Thu, 3 Dec 2026 15:23:44 +0100 Subject: [PATCH 2/2] fix(cdrom): stop playback before disposing image on drive removal CodeRabbit review: remounting a CD drive while CD audio is playing disposed the old image while its mixer callback could still call Read on it. StopAudio() before Image.Dispose() in Mscdex.RemoveDrive disables the mixer channel and flips the playback status, so the callback exits before touching the image. Also use FASM label-as-immediate syntax (mov dx, count) so the probe source regenerates with FASM. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Spice86.Core/Emulator/InterruptHandlers/Mscdex/Mscdex.cs | 3 +++ .../Resources/DosConsoleProgramTests/mscdex_drive_count.asm | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Spice86.Core/Emulator/InterruptHandlers/Mscdex/Mscdex.cs b/src/Spice86.Core/Emulator/InterruptHandlers/Mscdex/Mscdex.cs index f5cabf6c3e..d2d4a4ac9e 100644 --- a/src/Spice86.Core/Emulator/InterruptHandlers/Mscdex/Mscdex.cs +++ b/src/Spice86.Core/Emulator/InterruptHandlers/Mscdex/Mscdex.cs @@ -162,6 +162,9 @@ public bool RemoveDrive(char driveLetter) { MscdexDriveEntry drive = _drives[i]; _drives.RemoveAt(i); _audioStates.Remove(upper); + // Stop audio before disposing the image so the mixer callback + // can no longer call Read on a disposed image. + drive.Drive.StopAudio(); drive.Drive.Image.Dispose(); if (_loggerService.IsEnabled(LogLevel.Information)) { _loggerService.LogInformation("MSCDEX: Removed drive {Drive}:", upper); diff --git a/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm b/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm index 92a3e0406a..ebfaaf0315 100644 --- a/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm +++ b/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.asm @@ -12,7 +12,7 @@ org 100h mov ah, 40h mov bx, 1 mov cx, 1 - mov dx, offset count + mov dx, count int 21h mov ax, 4C00h int 21h