Example #1
0
        /// <summary>
        /// Creates a Spectrum compatible floppy file with the specified name
        /// </summary>
        /// <param name="filename">File name with full pass</param>
        /// <returns>The opened floppy file</returns>
        public static VirtualFloppyFile OpenFloppyFile(string filename)
        {
            using (var reader = new BinaryReader(File.OpenRead(filename)))
            {
                var header = reader.ReadBytes(HEADER.Length);
                if (!header.SequenceEqual(HEADER))
                {
                    throw new InvalidOperationException("Invalid floppy file header");
                }

                var isWriteProtected = reader.ReadByte() != 0x00;
                var isDoubleSided    = reader.ReadByte() != 0x00;
                var tracks           = reader.ReadByte();
                var sectorsPerTrack  = reader.ReadByte();
                var firstSector      = reader.ReadByte();
                var lengthRead       = 0;
                for (var i = 0; i < (isDoubleSided ? 2 : 1) * tracks * sectorsPerTrack; i++)
                {
                    var sectorData = reader.ReadBytes(512);
                    lengthRead += sectorData.Length;
                    if (sectorData.Length < 512)
                    {
                        throw new InvalidOperationException(
                                  $"Floppy file is shorter then expected, its size is {lengthRead} bytes.");
                    }
                }
                reader.BaseStream.Seek(HEADER_SIZE, SeekOrigin.Begin);
                var formatSpec = reader.ReadBytes(10);
                var floppy     = new VirtualFloppyFile(filename, isWriteProtected, formatSpec, firstSector);
                if (!floppy.IsSpectrumVmCompatible())
                {
                    throw new InvalidOperationException("Floppy file is not Spectrum compatible");
                }
                return(floppy);
            }
        }
Example #2
0
        /// <summary>
        /// Creates a Spectrum compatible floppy file with the specified name
        /// </summary>
        /// <param name="filename">File name with full pass</param>
        /// <param name="format">Format to create</param>
        /// <returns>The newly created floppy file</returns>
        public static VirtualFloppyFile CreateSpectrumFloppyFile(string filename, FloppyFormat format = FloppyFormat.SpectrumP3)
        {
            var dir = Path.GetDirectoryName(filename);

            if (dir != null && !Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (var writer = new BinaryWriter(File.Create(filename)))
            {
                if (!s_FormatHeaders.TryGetValue(format, out var formatDesc))
                {
                    formatDesc = s_DefaultFormatDescriptor;
                }
                var formatBytes = formatDesc.Format;
                var floppy      = new VirtualFloppyFile(filename, false, formatBytes, formatDesc.SectorIndex);
                writer.Write(HEADER);
                writer.Write(floppy.IsWriteProtected ? (byte)0x01 : (byte)0x00);
                writer.Write(floppy.IsDoubleSided ? (byte)0x01 : (byte)0x00);
                writer.Write(floppy.Tracks);
                writer.Write(floppy.SectorsPerTrack);
                writer.Write(floppy.FirstSectorIndex);
                var sectorData = new byte[512];
                for (var i = 0; i < sectorData.Length; i++)
                {
                    sectorData[i] = 0xE5;
                }
                for (var i = 0; i < (floppy.IsDoubleSided ? 2 : 1) * floppy.Tracks * floppy.SectorsPerTrack; i++)
                {
                    writer.Write(sectorData);
                }
                writer.Seek(HEADER_SIZE, SeekOrigin.Begin);
                writer.Write(formatBytes);
                return(floppy);
            }
        }