From 6c606b5506a36127271f1da6b1e85413a126c77f Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 25 Dec 2021 17:06:12 -0500 Subject: [PATCH 01/29] Fix through route to `TargetPlatform::TypeDistinguisher`. --- Storage/Disk/DiskImage/DiskImage.hpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Storage/Disk/DiskImage/DiskImage.hpp b/Storage/Disk/DiskImage/DiskImage.hpp index ed051756f..d12d4709a 100644 --- a/Storage/Disk/DiskImage/DiskImage.hpp +++ b/Storage/Disk/DiskImage/DiskImage.hpp @@ -14,6 +14,7 @@ #include "../Disk.hpp" #include "../Track/Track.hpp" +#include "../../TargetPlatforms.hpp" namespace Storage { namespace Disk { @@ -86,8 +87,11 @@ class DiskImageHolderBase: public Disk { Provides a wrapper that wraps a DiskImage to make it into a Disk, providing caching and, thereby, an intermediate store for modified tracks so that mutable disk images can either update on the fly or perform a block update on closure, as appropriate. + + Implements TargetPlatform::TypeDistinguisher to return either no information whatsoever, if + the underlying image doesn't implement TypeDistinguisher, or else to pass the call along. */ -template class DiskImageHolder: public DiskImageHolderBase { +template class DiskImageHolder: public DiskImageHolderBase, public TargetPlatform::TypeDistinguisher { public: template DiskImageHolder(Ts&&... args) : disk_image_(args...) {} @@ -103,6 +107,14 @@ template class DiskImageHolder: public DiskImageHolderBase { private: T disk_image_; + + TargetPlatform::Type target_platform_type() final { + if constexpr (std::is_base_of::value) { + return static_cast(&disk_image_)->target_platform_type(); + } else { + return TargetPlatform::Type(~0); + } + } }; #include "DiskImageImplementation.hpp" From dba3a3d9426cf45cc92ee2939fda43b28ec1a0f8 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 25 Dec 2021 17:06:47 -0500 Subject: [PATCH 02/29] Add through route to an IPF container. --- Analyser/Static/StaticAnalyser.cpp | 13 +++-- .../Clock Signal.xcodeproj/project.pbxproj | 8 +++ OSBindings/Mac/Clock Signal/Info.plist | 20 +++++++ Storage/Disk/DiskImage/Formats/IPF.cpp | 27 +++++++++ Storage/Disk/DiskImage/Formats/IPF.hpp | 56 +++++++++++++++++++ 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 Storage/Disk/DiskImage/Formats/IPF.cpp create mode 100644 Storage/Disk/DiskImage/Formats/IPF.hpp diff --git a/Analyser/Static/StaticAnalyser.cpp b/Analyser/Static/StaticAnalyser.cpp index fb0c1b7a5..99668d6e1 100644 --- a/Analyser/Static/StaticAnalyser.cpp +++ b/Analyser/Static/StaticAnalyser.cpp @@ -43,11 +43,12 @@ #include "../../Storage/Disk/DiskImage/Formats/AppleDSK.hpp" #include "../../Storage/Disk/DiskImage/Formats/CPCDSK.hpp" #include "../../Storage/Disk/DiskImage/Formats/D64.hpp" -#include "../../Storage/Disk/DiskImage/Formats/MacintoshIMG.hpp" #include "../../Storage/Disk/DiskImage/Formats/G64.hpp" #include "../../Storage/Disk/DiskImage/Formats/DMK.hpp" #include "../../Storage/Disk/DiskImage/Formats/FAT12.hpp" #include "../../Storage/Disk/DiskImage/Formats/HFE.hpp" +#include "../../Storage/Disk/DiskImage/Formats/IPF.hpp" +#include "../../Storage/Disk/DiskImage/Formats/MacintoshIMG.hpp" #include "../../Storage/Disk/DiskImage/Formats/MSA.hpp" #include "../../Storage/Disk/DiskImage/Formats/NIB.hpp" #include "../../Storage/Disk/DiskImage/Formats/OricMFMDSK.hpp" @@ -103,8 +104,8 @@ static Media GetMediaAndPlatforms(const std::string &file_name, TargetPlatform:: #define InsertInstance(list, instance, platforms) \ list.emplace_back(instance);\ potential_platforms |= platforms;\ - TargetPlatform::TypeDistinguisher *distinguisher = dynamic_cast(list.back().get());\ - if(distinguisher) potential_platforms &= distinguisher->target_platform_type(); \ + TargetPlatform::TypeDistinguisher *const distinguisher = dynamic_cast(list.back().get());\ + if(distinguisher) potential_platforms &= distinguisher->target_platform_type(); #define Insert(list, class, platforms, ...) \ InsertInstance(list, new Storage::class(__VA_ARGS__), platforms); @@ -161,7 +162,11 @@ static Media GetMediaAndPlatforms(const std::string &file_name, TargetPlatform:: // HFE (TODO: switch to AllDisk once the MSX stops being so greedy) Format("img", result.disks, Disk::DiskImageHolder, TargetPlatform::Macintosh) // IMG (DiskCopy 4.2) Format("image", result.disks, Disk::DiskImageHolder, TargetPlatform::Macintosh) // IMG (DiskCopy 4.2) - Format("img", result.disks, Disk::DiskImageHolder, TargetPlatform::Enterprise) // IMG (Enterprise/MS-DOS style) + Format("img", result.disks, Disk::DiskImageHolder, TargetPlatform::Enterprise) // IMG (Enterprise/MS-DOS style) + Format( "ipf", + result.disks, + Disk::DiskImageHolder, + TargetPlatform::Amiga | TargetPlatform::AtariST | TargetPlatform::AmstradCPC | TargetPlatform::ZXSpectrum) // IPF Format("msa", result.disks, Disk::DiskImageHolder, TargetPlatform::AtariST) // MSA Format("nib", result.disks, Disk::DiskImageHolder, TargetPlatform::DiskII) // NIB Format("o", result.tapes, Tape::ZX80O81P, TargetPlatform::ZX8081) // O diff --git a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj index 7962a7be7..734e8c3cb 100644 --- a/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj +++ b/OSBindings/Mac/Clock Signal.xcodeproj/project.pbxproj @@ -273,6 +273,8 @@ 4B59199C1DAC6C46005BB85C /* OricTAP.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B59199A1DAC6C46005BB85C /* OricTAP.cpp */; }; 4B595FAD2086DFBA0083CAA8 /* AudioToggle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B595FAC2086DFBA0083CAA8 /* AudioToggle.cpp */; }; 4B595FAE2086DFBA0083CAA8 /* AudioToggle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B595FAC2086DFBA0083CAA8 /* AudioToggle.cpp */; }; + 4B5B37312777C7FC0047F238 /* IPF.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5B372F2777C7FC0047F238 /* IPF.cpp */; }; + 4B5B37322777C7FC0047F238 /* IPF.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5B372F2777C7FC0047F238 /* IPF.cpp */; }; 4B5D5C9725F56FC7001B4623 /* Spectrum.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5D5C9525F56FC7001B4623 /* Spectrum.cpp */; }; 4B5D5C9825F56FC7001B4623 /* Spectrum.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5D5C9525F56FC7001B4623 /* Spectrum.cpp */; }; 4B5FADBA1DE3151600AEC565 /* FileHolder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4B5FADB81DE3151600AEC565 /* FileHolder.cpp */; }; @@ -1350,6 +1352,8 @@ 4B59199B1DAC6C46005BB85C /* OricTAP.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = OricTAP.hpp; sourceTree = ""; }; 4B595FAB2086DFBA0083CAA8 /* AudioToggle.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = AudioToggle.hpp; sourceTree = ""; }; 4B595FAC2086DFBA0083CAA8 /* AudioToggle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = AudioToggle.cpp; sourceTree = ""; }; + 4B5B372F2777C7FC0047F238 /* IPF.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = IPF.cpp; sourceTree = ""; }; + 4B5B37302777C7FC0047F238 /* IPF.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = IPF.hpp; sourceTree = ""; }; 4B5D5C9525F56FC7001B4623 /* Spectrum.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; name = Spectrum.cpp; path = Parsers/Spectrum.cpp; sourceTree = ""; }; 4B5D5C9625F56FC7001B4623 /* Spectrum.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; name = Spectrum.hpp; path = Parsers/Spectrum.hpp; sourceTree = ""; }; 4B5FADB81DE3151600AEC565 /* FileHolder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FileHolder.cpp; sourceTree = ""; }; @@ -2788,6 +2792,7 @@ 4BEBFB4B2002C4BF000708CC /* FAT12.cpp */, 4B4518931F75FD1B00926311 /* G64.cpp */, 4B4518951F75FD1B00926311 /* HFE.cpp */, + 4B5B372F2777C7FC0047F238 /* IPF.cpp */, 4BB4BFAE22A42F290069048D /* MacintoshIMG.cpp */, 4B58601C1F806AB200AEE2E3 /* MFMSectorDump.cpp */, 4BC131782346DF2B00E4FF3D /* MSA.cpp */, @@ -2807,6 +2812,7 @@ 4BEBFB4C2002C4BF000708CC /* FAT12.hpp */, 4B4518941F75FD1B00926311 /* G64.hpp */, 4B4518961F75FD1B00926311 /* HFE.hpp */, + 4B5B37302777C7FC0047F238 /* IPF.hpp */, 4BB4BFAF22A42F290069048D /* MacintoshIMG.hpp */, 4B58601D1F806AB200AEE2E3 /* MFMSectorDump.hpp */, 4BC131792346DF2B00E4FF3D /* MSA.hpp */, @@ -5421,6 +5427,7 @@ 4BC131772346DE9100E4FF3D /* StaticAnalyser.cpp in Sources */, 4B055ACF1FAE9B030060FFFF /* SoundGenerator.cpp in Sources */, 4B4DEC08252BFA56004583AC /* 65816Base.cpp in Sources */, + 4B5B37322777C7FC0047F238 /* IPF.cpp in Sources */, 4B894519201967B4007DE474 /* ConfidenceCounter.cpp in Sources */, 4B055AEE1FAE9BBF0060FFFF /* Keyboard.cpp in Sources */, 4B055AED1FAE9BA20060FFFF /* Z80Storage.cpp in Sources */, @@ -5574,6 +5581,7 @@ 4B89451E201967B4007DE474 /* Tape.cpp in Sources */, 4BAF2B4E2004580C00480230 /* DMK.cpp in Sources */, 4BB697CE1D4BA44400248BDF /* CommodoreGCR.cpp in Sources */, + 4B5B37312777C7FC0047F238 /* IPF.cpp in Sources */, 4B0ACC3023775819008902D0 /* TIASound.cpp in Sources */, 4B7136861F78724F008B8ED9 /* Encoder.cpp in Sources */, 4B0E04EA1FC9E5DA00F43484 /* CAS.cpp in Sources */, diff --git a/OSBindings/Mac/Clock Signal/Info.plist b/OSBindings/Mac/Clock Signal/Info.plist index c0ae89362..635d364c9 100644 --- a/OSBindings/Mac/Clock Signal/Info.plist +++ b/OSBindings/Mac/Clock Signal/Info.plist @@ -652,6 +652,26 @@ NSDocumentClass $(PRODUCT_MODULE_NAME).MachineDocument + + CFBundleTypeExtensions + + ipf + + CFBundleTypeName + Software Preservation Society Disk Image + CFBundleTypeOSTypes + + ???? + + CFBundleTypeRole + Viewer + LSHandlerRank + Owner + LSTypeIsPackage + + NSDocumentClass + $(PRODUCT_MODULE_NAME).MachineDocument + CFBundleExecutable $(EXECUTABLE_NAME) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp new file mode 100644 index 000000000..44999490e --- /dev/null +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -0,0 +1,27 @@ +// +// IPF.cpp +// Clock Signal +// +// Created by Thomas Harte on 25/12/2021. +// Copyright © 2021 Thomas Harte. All rights reserved. +// + +#include "IPF.hpp" + +using namespace Storage::Disk; + + +IPF::IPF(const std::string &file_name) : file_(file_name) { +} + +HeadPosition IPF::get_maximum_head_position() { + return HeadPosition(80); // TODO; +} + +int IPF::get_head_count() { + return 2; // TODO; +} + +std::shared_ptr IPF::get_track_at_position([[maybe_unused]] Track::Address address) { + return nullptr; +} diff --git a/Storage/Disk/DiskImage/Formats/IPF.hpp b/Storage/Disk/DiskImage/Formats/IPF.hpp new file mode 100644 index 000000000..b7f08a52b --- /dev/null +++ b/Storage/Disk/DiskImage/Formats/IPF.hpp @@ -0,0 +1,56 @@ +// +// IPF.hpp +// Clock Signal +// +// Created by Thomas Harte on 25/12/2021. +// Copyright © 2021 Thomas Harte. All rights reserved. +// + +#ifndef IPF_hpp +#define IPF_hpp + +#include "../DiskImage.hpp" +#include "../../../FileHolder.hpp" +#include "../../../TargetPlatforms.hpp" + +#include + +namespace Storage { +namespace Disk { + +/*! + Provides a @c DiskImage containing an IPF. +*/ +class IPF: public DiskImage, public TargetPlatform::TypeDistinguisher { + public: + /*! + Construct an @c IPF containing content from the file with name @c file_name. + + @throws Storage::FileHolder::Error::CantOpen if this file can't be opened. + @throws Error::InvalidFormat if the file doesn't appear to contain an .HFE format image. + @throws Error::UnknownVersion if the file looks correct but is an unsupported version. + */ + IPF(const std::string &file_name); + + // implemented to satisfy @c Disk + HeadPosition get_maximum_head_position() final; + int get_head_count() final; + std::shared_ptr get_track_at_position(Track::Address address) final; + + private: + Storage::FileHolder file_; + uint16_t seek_track(Track::Address address); + + int head_count_; + int track_count_; + + TargetPlatform::Type target_platform_type() final { + return platform_type_; + } + TargetPlatform::Type platform_type_ = TargetPlatform::Amiga; +}; + +} +} + +#endif /* IPF_hpp */ From c118dd8afe15bb9dd6005186b0064c82c139a829 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 25 Dec 2021 17:27:50 -0500 Subject: [PATCH 03/29] Adds just enough to list all the blocks in an IPF. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index 44999490e..f5056073f 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -12,6 +12,28 @@ using namespace Storage::Disk; IPF::IPF(const std::string &file_name) : file_(file_name) { + while(true) { + const auto start_of_block = file_.tell(); + const uint32_t type = file_.get32be(); + uint32_t length = file_.get32be(); // Can't be const because of the dumb encoding of DATA blocks. + [[maybe_unused]] const uint32_t crc = file_.get32be(); + if(file_.eof()) break; + +#define BLOCK(a, b, c, d) (a << 24) | (b << 16) | (c << 8) | d + switch(type) { + default: + printf("Ignoring %c%c%c%c, starting at %ld of length %d\n", (type >> 24), (type >> 16) & 0xff, (type >> 8) & 0xff, type & 0xff, start_of_block, length); + break; + + case BLOCK('D', 'A', 'T', 'A'): { + length += file_.get32be(); + printf("Handling DATA block at %ld of length %d\n", start_of_block, length); + } break; + } +#undef BLOCK + + file_.seek(start_of_block + length, SEEK_SET); + } } HeadPosition IPF::get_maximum_head_position() { From e457ce66ea01d5968806f5370729cabca8ad9d35 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 25 Dec 2021 17:32:29 -0500 Subject: [PATCH 04/29] Adds sanity checks around CAPS block. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index f5056073f..45220eb8e 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -19,12 +19,26 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { [[maybe_unused]] const uint32_t crc = file_.get32be(); if(file_.eof()) break; -#define BLOCK(a, b, c, d) (a << 24) | (b << 16) | (c << 8) | d +#define BLOCK(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d) + + // Sanity check: the first thing in a file should be the CAPS record. + if(!start_of_block && type != BLOCK('C', 'A', 'P', 'S')) { + throw Error::InvalidFormat; + } + switch(type) { default: printf("Ignoring %c%c%c%c, starting at %ld of length %d\n", (type >> 24), (type >> 16) & 0xff, (type >> 8) & 0xff, type & 0xff, start_of_block, length); break; + case BLOCK('C', 'A', 'P', 'S'): + // Analogously to the sanity check above, if a CAPS block is anywhere other + // than first then something is amiss. + if(start_of_block) { + throw Error::InvalidFormat; + } + break; + case BLOCK('D', 'A', 'T', 'A'): { length += file_.get32be(); printf("Handling DATA block at %ld of length %d\n", start_of_block, length); From a6b326da4831ef6fac5e279e0cee497a3022ebfc Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 25 Dec 2021 18:17:13 -0500 Subject: [PATCH 05/29] Parse the INFO record. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 50 ++++++++++++++++++++++++-- Storage/Disk/DiskImage/Formats/IPF.hpp | 4 +-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index 45220eb8e..5720b44ab 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -39,6 +39,52 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { } break; + case BLOCK('I', 'N', 'F', 'O'): { + // There are a lot of useful archival fields in the info chunk, which for emulation + // aren't that interesting. + + // Make sure this is a floppy disk. + const uint32_t media_type = file_.get32be(); + if(media_type != 1) { + throw Error::InvalidFormat; + } + + // Skip: encoder type, revision, file key and revision, CRC of the original .ctr, and minimum track. + file_.seek(24, SEEK_CUR); + track_count_ = int(1 + file_.get32be()); + + // Skip: min side. + file_.seek(4, SEEK_CUR); + head_count_ = int(1 + file_.get32be()); + + // Skip: creation date, time. + file_.seek(8, SEEK_CUR); + + platform_type_ = 0; + for(int c = 0; c < 4; c++) { + const uint8_t platform = file_.get8(); + switch(platform) { + default: break; + case 1: platform_type_ |= TargetPlatform::Amiga; break; + case 2: platform_type_ |= TargetPlatform::AtariST; break; + /* Omitted: 3 -> IBM PC */ + case 4: platform_type_ |= TargetPlatform::AmstradCPC; break; + case 5: platform_type_ |= TargetPlatform::ZXSpectrum; break; + /* Omitted: 6 -> Sam Coupé */ + /* Omitted: 7 -> Archimedes */ + /* Omitted: 8 -> C64 */ + /* Omitted: 9 -> Atari 8-bit */ + } + } + + // If the file didn't declare anything, default to supporting everything. + if(!platform_type_) { + platform_type_ = ~0; + } + + // Ignore: disk number, creator ID, reserved area. + } break; + case BLOCK('D', 'A', 'T', 'A'): { length += file_.get32be(); printf("Handling DATA block at %ld of length %d\n", start_of_block, length); @@ -51,11 +97,11 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { } HeadPosition IPF::get_maximum_head_position() { - return HeadPosition(80); // TODO; + return HeadPosition(track_count_); } int IPF::get_head_count() { - return 2; // TODO; + return head_count_; } std::shared_ptr IPF::get_track_at_position([[maybe_unused]] Track::Address address) { diff --git a/Storage/Disk/DiskImage/Formats/IPF.hpp b/Storage/Disk/DiskImage/Formats/IPF.hpp index b7f08a52b..ad016f29a 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.hpp +++ b/Storage/Disk/DiskImage/Formats/IPF.hpp @@ -45,9 +45,9 @@ class IPF: public DiskImage, public TargetPlatform::TypeDistinguisher { int track_count_; TargetPlatform::Type target_platform_type() final { - return platform_type_; + return TargetPlatform::Type(platform_type_); } - TargetPlatform::Type platform_type_ = TargetPlatform::Amiga; + TargetPlatform::IntType platform_type_ = TargetPlatform::Amiga; }; } From 0433db037049e24f0e2add5f8fc07c04353ad278 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sat, 25 Dec 2021 19:36:54 -0500 Subject: [PATCH 06/29] Eliminate macro. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index 5720b44ab..c97656be2 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -10,6 +10,15 @@ using namespace Storage::Disk; +namespace { + +constexpr uint32_t block(const char *src) { + static_assert(sizeof(int) >= sizeof(uint32_t)); + return uint32_t((src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]); +} + +} + IPF::IPF(const std::string &file_name) : file_(file_name) { while(true) { @@ -19,10 +28,8 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { [[maybe_unused]] const uint32_t crc = file_.get32be(); if(file_.eof()) break; -#define BLOCK(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d) - // Sanity check: the first thing in a file should be the CAPS record. - if(!start_of_block && type != BLOCK('C', 'A', 'P', 'S')) { + if(!start_of_block && type != block("CAPS")) { throw Error::InvalidFormat; } @@ -31,7 +38,7 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { printf("Ignoring %c%c%c%c, starting at %ld of length %d\n", (type >> 24), (type >> 16) & 0xff, (type >> 8) & 0xff, type & 0xff, start_of_block, length); break; - case BLOCK('C', 'A', 'P', 'S'): + case block("CAPS"): // Analogously to the sanity check above, if a CAPS block is anywhere other // than first then something is amiss. if(start_of_block) { @@ -39,7 +46,7 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { } break; - case BLOCK('I', 'N', 'F', 'O'): { + case block("INFO"): { // There are a lot of useful archival fields in the info chunk, which for emulation // aren't that interesting. @@ -85,12 +92,11 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { // Ignore: disk number, creator ID, reserved area. } break; - case BLOCK('D', 'A', 'T', 'A'): { + case block("DATA"): { length += file_.get32be(); printf("Handling DATA block at %ld of length %d\n", start_of_block, length); } break; } -#undef BLOCK file_.seek(start_of_block + length, SEEK_SET); } From 28572d4392abe36082c243c6afcb8cefa76ddb17 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 26 Dec 2021 09:12:44 -0500 Subject: [PATCH 07/29] Enforce string-length requirement. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index c97656be2..ca55cd1ae 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -12,9 +12,13 @@ using namespace Storage::Disk; namespace { -constexpr uint32_t block(const char *src) { - static_assert(sizeof(int) >= sizeof(uint32_t)); - return uint32_t((src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]); +constexpr uint32_t block(const char (& src)[5]) { + return uint32_t( + (uint32_t(src[0]) << 24) | + (uint32_t(src[1]) << 16) | + (uint32_t(src[2]) << 8) | + uint32_t(src[3]) + ); } } @@ -92,6 +96,9 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { // Ignore: disk number, creator ID, reserved area. } break; + case block("IMGE"): + break; + case block("DATA"): { length += file_.get32be(); printf("Handling DATA block at %ld of length %d\n", start_of_block, length); From 9d3cf9c73cae11ec161b9f9bf76d9aaba9cf1804 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Sun, 26 Dec 2021 14:49:51 -0500 Subject: [PATCH 08/29] Collate descriptions of all tracks. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 61 ++++++++++++++++++++++++-- Storage/Disk/DiskImage/Formats/IPF.hpp | 25 +++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index ca55cd1ae..978f24b12 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -25,6 +25,10 @@ constexpr uint32_t block(const char (& src)[5]) { IPF::IPF(const std::string &file_name) : file_(file_name) { + std::map tracks_by_data_key; + + // For now, just build up a list of tracks that exist, noting the file position at which their data begins + // plus the other fields that'll be necessary to convert them into flux on demand later. while(true) { const auto start_of_block = file_.tell(); const uint32_t type = file_.get32be(); @@ -96,12 +100,63 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { // Ignore: disk number, creator ID, reserved area. } break; - case block("IMGE"): - break; + case block("IMGE"): { + // Get track location. + const uint32_t track = file_.get32be(); + const uint32_t side = file_.get32be(); + const Track::Address address{int(side), HeadPosition(int(track))}; + + // Hence generate a TrackDescription. + auto pair = tracks_.emplace(address, TrackDescription()); + TrackDescription &description = pair.first->second; + + // Read those fields of interest... + + // Bit density. I've no idea why the density can't just be given as a measurement. + description.density = TrackDescription::Density(file_.get32be()); + if(description.density > TrackDescription::Density::Max) { + description.density = TrackDescription::Density::Unknown; + } + + + file_.seek(12, SEEK_CUR); // Skipped: signal type, track bytes, start byte position. + description.start_bit_pos = file_.get32be(); + description.data_bits = file_.get32be(); + description.gap_bits = file_.get32be(); + + file_.seek(4, SEEK_CUR); // Skipped: track bits, which is entirely redundant. + description.block_count = file_.get32be(); + + file_.seek(4, SEEK_CUR); // Skipped: encoder process. + description.has_fuzzy_bits = file_.get32be() & 1; + + // For some reason the authors decided to introduce another primary key, + // in addition to that which naturally exists of (track, side). So set up + // a mapping from the one to the other. + const uint32_t data_key = file_.get32be(); + tracks_by_data_key.emplace(data_key, address); + } break; case block("DATA"): { length += file_.get32be(); - printf("Handling DATA block at %ld of length %d\n", start_of_block, length); + + file_.seek(8, SEEK_CUR); // Skipped: bit size, CRC. + + // Grab the data key and use that to establish the file starting + // position for this track. + // + // Assumed here: DATA records will come after corresponding IMGE records. + const uint32_t data_key = file_.get32be(); + const auto pair = tracks_by_data_key.find(data_key); + if(pair == tracks_by_data_key.end()) { + break; + } + + auto description = tracks_.find(pair->second); + if(description == tracks_.end()) { + break; + } + description->second.file_offset = file_.tell(); } break; } diff --git a/Storage/Disk/DiskImage/Formats/IPF.hpp b/Storage/Disk/DiskImage/Formats/IPF.hpp index ad016f29a..993704546 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.hpp +++ b/Storage/Disk/DiskImage/Formats/IPF.hpp @@ -14,6 +14,7 @@ #include "../../../TargetPlatforms.hpp" #include +#include namespace Storage { namespace Disk { @@ -41,8 +42,32 @@ class IPF: public DiskImage, public TargetPlatform::TypeDistinguisher { Storage::FileHolder file_; uint16_t seek_track(Track::Address address); + struct TrackDescription { + long file_offset = 0; + enum class Density { + Unknown, + Noise, + Auto, + CopylockAmiga, + CopylockAmigaNew, + CopylockST, + SpeedlockAmiga, + OldSpeedlockAmiga, + AdamBrierleyAmiga, + AdamBrierleyDensityKeyAmiga, + + Max = AdamBrierleyDensityKeyAmiga + } density = Density::Unknown; + uint32_t start_bit_pos = 0; + uint32_t data_bits = 0; + uint32_t gap_bits = 0; + uint32_t block_count; + bool has_fuzzy_bits = false; + }; + int head_count_; int track_count_; + std::map tracks_; TargetPlatform::Type target_platform_type() final { return TargetPlatform::Type(platform_type_); From 9b6ccbcc957b6721899f4520cd14ac92fde59ff2 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 27 Dec 2021 18:12:44 -0500 Subject: [PATCH 09/29] Parses data and gap stream elements. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 122 ++++++++++++++++++++++++- Storage/Disk/DiskImage/Formats/IPF.hpp | 1 + 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index 978f24b12..e7a039700 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -21,6 +21,15 @@ constexpr uint32_t block(const char (& src)[5]) { ); } +constexpr size_t block_size(Storage::FileHolder &file, uint8_t header) { + uint8_t size_width = header >> 5; + size_t length = 0; + while(size_width--) { + length = (length << 8) | file.get8(); + } + return length; +} + } @@ -64,8 +73,11 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { throw Error::InvalidFormat; } - // Skip: encoder type, revision, file key and revision, CRC of the original .ctr, and minimum track. - file_.seek(24, SEEK_CUR); + // Determine whether this is a newer SPS-style file. + is_sps_format_ = file_.get32be() > 1; + + // Skip: revision, file key and revision, CRC of the original .ctr, and minimum track. + file_.seek(20, SEEK_CUR); track_count_ = int(1 + file_.get32be()); // Skip: min side. @@ -173,5 +185,111 @@ int IPF::get_head_count() { } std::shared_ptr IPF::get_track_at_position([[maybe_unused]] Track::Address address) { + // Get the track description, if it exists, and check either that the file has contents for the track. + auto pair = tracks_.find(address); + if(pair == tracks_.end()) { + return nullptr; + } + const TrackDescription &description = pair->second; + if(!description.file_offset) { + return nullptr; + } + + // Seek to track content. + file_.seek(description.file_offset, SEEK_SET); + + // Read the block descriptions up front. + // + // This is less efficient than just seeking for each block in turn, + // but is a useful crutch to comprehension of the file format on a + // first run through. + struct BlockDescriptor { + uint32_t data_bits = 0; + uint32_t gap_bits = 0; + uint32_t gap_offset = 0; + bool is_mfm = false; + bool has_forward_gap = false; + bool has_backwards_gap = false; + bool data_unit_is_bits = false; + uint32_t default_gap_value = 0; + uint32_t data_offset = 0; + }; + std::vector blocks; + blocks.reserve(description.block_count); + for(uint32_t c = 0; c < description.block_count; c++) { + auto &block = blocks.emplace_back(); + block.data_bits = file_.get32be(); + block.gap_bits = file_.get32be(); + if(is_sps_format_) { + block.gap_offset = file_.get32be(); + file_.seek(4, SEEK_CUR); // Skip 'cell type' which appears to provide no content. + } else { + // Skip potlower-resolution copies of data_bits and gap_bits. + file_.seek(8, SEEK_CUR); + } + block.is_mfm = file_.get32be() == 1; + + const uint32_t flags = file_.get32be(); + block.has_forward_gap = flags & 1; + block.has_backwards_gap = flags & 2; + block.data_unit_is_bits = flags & 4; + + block.default_gap_value = file_.get32be(); + block.data_offset = file_.get32be(); + } + + // TODO: Append as necessary for each gap and data stream as per above. + for(auto &block: blocks) { + if(block.gap_offset) { + file_.seek(description.file_offset + block.gap_offset, SEEK_SET); + while(true) { + const uint8_t gap_header = file_.get8(); + if(!gap_header) break; + + // Decompose the header and read the length. + enum class Type { + None, GapLength, SampleLength + } type = Type(gap_header & 0x1f); + const size_t length = block_size(file_, gap_header); + + // TODO: write the gap. + switch(type) { + case Type::GapLength: + printf("Unhandled gap length %zu\n", length); + break; + + default: + case Type::SampleLength: + printf("Unhandled sampled gap length %zu\n", length); + file_.seek(long(length >> 3), SEEK_CUR); + break; + } + } + } + + if(block.data_offset) { + file_.seek(description.file_offset + block.data_offset, SEEK_SET); + while(true) { + const uint8_t data_header = file_.get8(); + if(!data_header) break; + + // Decompose the header and read the length. + enum class Type { + None, Sync, Data, Gap, Raw, Fuzzy + } type = Type(data_header & 0x1f); + const size_t length = block_size(file_, data_header) * (block.data_unit_is_bits ? 1 : 8); + + // TODO: write the data. + switch(type) { + default: + printf("Unhandled data type %d, length %zu\n", int(type), length); + file_.seek(long(length >> 3), SEEK_CUR); + break; + } + } + } + printf("\n"); + } + return nullptr; } diff --git a/Storage/Disk/DiskImage/Formats/IPF.hpp b/Storage/Disk/DiskImage/Formats/IPF.hpp index 993704546..306aa693d 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.hpp +++ b/Storage/Disk/DiskImage/Formats/IPF.hpp @@ -68,6 +68,7 @@ class IPF: public DiskImage, public TargetPlatform::TypeDistinguisher { int head_count_; int track_count_; std::map tracks_; + bool is_sps_format_ = false; TargetPlatform::Type target_platform_type() final { return TargetPlatform::Type(platform_type_); From dc994f001dcaa9a7a3a687c1f8c05201253690ac Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 27 Dec 2021 18:55:11 -0500 Subject: [PATCH 10/29] Mention units. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index e7a039700..47cb56246 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -255,12 +255,12 @@ std::shared_ptr IPF::get_track_at_position([[maybe_unused]] Track::Addres // TODO: write the gap. switch(type) { case Type::GapLength: - printf("Unhandled gap length %zu\n", length); + printf("Unhandled gap length %zu bytes\n", length); break; default: case Type::SampleLength: - printf("Unhandled sampled gap length %zu\n", length); + printf("Unhandled sampled gap length %zu bytes\n", length); file_.seek(long(length >> 3), SEEK_CUR); break; } @@ -282,7 +282,7 @@ std::shared_ptr IPF::get_track_at_position([[maybe_unused]] Track::Addres // TODO: write the data. switch(type) { default: - printf("Unhandled data type %d, length %zu\n", int(type), length); + printf("Unhandled data type %d, length %zu bits\n", int(type), length); file_.seek(long(length >> 3), SEEK_CUR); break; } From 4f3c75477118f095fc691b9b2fea331cf7f0891d Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Mon, 27 Dec 2021 19:15:46 -0500 Subject: [PATCH 11/29] Adds exposition. --- Storage/Disk/DiskImage/Formats/IPF.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.hpp b/Storage/Disk/DiskImage/Formats/IPF.hpp index 306aa693d..20352c99c 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.hpp +++ b/Storage/Disk/DiskImage/Formats/IPF.hpp @@ -20,7 +20,10 @@ namespace Storage { namespace Disk { /*! - Provides a @c DiskImage containing an IPF. + Provides a @c DiskImage containing an IPF, which is a mixed stream of raw flux windows and + unencoded MFM sections along with gap records that can be used to record write splices, all + of which is variably clocked (albeit not at flux transition resolution; as a result IPF files tend to be + close in size to more primitive formats). */ class IPF: public DiskImage, public TargetPlatform::TypeDistinguisher { public: From 350c98ab4d6fb6d6fec2065cdc45b25e20d71bb8 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Wed, 29 Dec 2021 18:15:37 -0500 Subject: [PATCH 12/29] Add those densities I've yet discovered the rules for. --- Storage/Disk/DiskImage/Formats/IPF.cpp | 61 +++++++++++++++++++++++++- Storage/Disk/DiskImage/Formats/IPF.hpp | 2 + 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Storage/Disk/DiskImage/Formats/IPF.cpp b/Storage/Disk/DiskImage/Formats/IPF.cpp index 47cb56246..6f21e53c6 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.cpp +++ b/Storage/Disk/DiskImage/Formats/IPF.cpp @@ -32,7 +32,6 @@ constexpr size_t block_size(Storage::FileHolder &file, uint8_t header) { } - IPF::IPF(const std::string &file_name) : file_(file_name) { std::map tracks_by_data_key; @@ -130,7 +129,6 @@ IPF::IPF(const std::string &file_name) : file_(file_name) { description.density = TrackDescription::Density::Unknown; } - file_.seek(12, SEEK_CUR); // Skipped: signal type, track bytes, start byte position. description.start_bit_pos = file_.get32be(); description.data_bits = file_.get32be(); @@ -293,3 +291,62 @@ std::shared_ptr IPF::get_track_at_position([[maybe_unused]] Track::Addres return nullptr; } + +/// @returns A vector of the length of a bit in each block for a count of @c blocks in an area of data density @c density. +/// +/// @discussion At least to me, this is the least well-designed part] of the IPF specification; rather than just dictating cell +/// densities (or, equivalently, lengths) in the file, densities are named according to their protection scheme and the decoder +/// is required to know all named protection schemes. Which makes IPF unable to handle arbitrary disks (or, indeed, disks +/// with multiple protection schemes on a single track). +std::vector IPF::bit_lengths(TrackDescription::Density density, size_t blocks) { + std::vector result; + result.reserve(size_t(blocks)); + + // Establish the default density of 2 µs. + for(size_t c = 0; c < blocks; c++) { + result.push_back(Storage::Time(1, 500'000)); // i.e. default to 2µs. + } + + switch(density) { + default: + break; + + case TrackDescription::Density::CopylockAmiga: + if(blocks > 4) result[4] = Storage::Time(189, 100'000'000); // 1.89µs + if(blocks > 5) result[5] = Storage::Time(199, 100'000'000); // 1.99µs + if(blocks > 6) result[6] = Storage::Time(209, 100'000'000); // 2.09µs + break; + + case TrackDescription::Density::CopylockAmigaNew: + if(blocks > 0) result[0] = Storage::Time(189, 100'000'000); // 1.89µs + if(blocks > 1) result[1] = Storage::Time(199, 100'000'000); // 1.99µs + if(blocks > 2) result[2] = Storage::Time(209, 100'000'000); // 2.09µs + break; + + case TrackDescription::Density::CopylockST: + if(blocks > 5) result[5] = Storage::Time(21, 10'000'000); // 2.1µs + break; + + case TrackDescription::Density::SpeedlockAmiga: + if(blocks > 1) result[1] = Storage::Time(11, 5'000'000); // 2.2µs + if(blocks > 2) result[2] = Storage::Time(9, 5'000'000); // 1.8µs + break; + + case TrackDescription::Density::OldSpeedlockAmiga: + if(blocks > 1) result[1] = Storage::Time(21, 10'000'000); // 2.1µs + break; + + case TrackDescription::Density::AdamBrierleyAmiga: + if(blocks > 1) result[1] = Storage::Time(11, 5'000'000); // 2.2µs + if(blocks > 2) result[2] = Storage::Time(21, 10'000'000); // 2.1µs + + if(blocks > 4) result[3] = Storage::Time(19, 10'000'000); // 1.9µs + if(blocks > 5) result[5] = Storage::Time(9, 5'000'000); // 1.8µs + if(blocks > 6) result[6] = Storage::Time(17, 10'000'000); // 1.7µs + break; + + // TODO: AdamBrierleyDensityKeyAmiga. + } + + return result; +} diff --git a/Storage/Disk/DiskImage/Formats/IPF.hpp b/Storage/Disk/DiskImage/Formats/IPF.hpp index 20352c99c..d8bd5d4d6 100644 --- a/Storage/Disk/DiskImage/Formats/IPF.hpp +++ b/Storage/Disk/DiskImage/Formats/IPF.hpp @@ -68,6 +68,8 @@ class IPF: public DiskImage, public TargetPlatform::TypeDistinguisher { bool has_fuzzy_bits = false; }; + std::vector