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/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..ebfaaf0315 --- /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, 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 0000000000..e4b5b512ff Binary files /dev/null and b/tests/Spice86.Tests/Resources/DosConsoleProgramTests/mscdex_drive_count.com differ 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 -