1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-28 07:29:45 +00:00

Compacts gaps when necessary to fit more sectors.

This commit is contained in:
Thomas Harte 2019-12-19 19:22:48 -05:00
parent d802e8aee3
commit 797abae4b3
2 changed files with 63 additions and 10 deletions

View File

@ -67,7 +67,7 @@
</Testables> </Testables>
</TestAction> </TestAction>
<LaunchAction <LaunchAction
buildConfiguration = "Debug" buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableASanStackUseAfterReturn = "YES" enableASanStackUseAfterReturn = "YES"

View File

@ -12,10 +12,16 @@
#include "../../Track/PCMTrack.hpp" #include "../../Track/PCMTrack.hpp"
#include "../../../../NumberTheory/CRC.hpp" #include "../../../../NumberTheory/CRC.hpp"
#include <cassert>
#include <set> #include <set>
using namespace Storage::Encodings::MFM; using namespace Storage::Encodings::MFM;
enum class SurfaceItem {
Mark,
Data
};
class MFMEncoder: public Encoder { class MFMEncoder: public Encoder {
public: public:
MFMEncoder(std::vector<bool> &target) : Encoder(target) {} MFMEncoder(std::vector<bool> &target) : Encoder(target) {}
@ -59,6 +65,14 @@ class MFMEncoder: public Encoder {
add_byte(DeletedDataAddressByte); add_byte(DeletedDataAddressByte);
} }
size_t item_size(SurfaceItem item) {
switch(item) {
case SurfaceItem::Mark: return 8; // Three syncs plus the mark type.
case SurfaceItem::Data: return 2; // Just a single encoded byte.
default: assert(false);
}
}
private: private:
uint16_t last_output_; uint16_t last_output_;
void output_short(uint16_t value) { void output_short(uint16_t value) {
@ -116,6 +130,11 @@ class FMEncoder: public Encoder {
crc_generator_.add(DeletedDataAddressByte); crc_generator_.add(DeletedDataAddressByte);
output_short(FMDeletedDataAddressMark); output_short(FMDeletedDataAddressMark);
} }
size_t item_size(SurfaceItem item) {
// Marks are just slightly-invalid bytes, so everything is the same length.
return 2;
}
}; };
template<class T> std::shared_ptr<Storage::Disk::Track> template<class T> std::shared_ptr<Storage::Disk::Track>
@ -131,18 +150,53 @@ template<class T> std::shared_ptr<Storage::Disk::Track>
segment.data.reserve(expected_track_bytes * 8); segment.data.reserve(expected_track_bytes * 8);
T shifter(segment.data); T shifter(segment.data);
// output the index mark // Make a pre-estimate of output size, in case any of the idealised gaps
// provided need to be shortened.
const size_t data_size = shifter.item_size(SurfaceItem::Data);
const size_t mark_size = shifter.item_size(SurfaceItem::Mark);
const size_t max_size = (expected_track_bytes + (expected_track_bytes / 10)) * 8;
size_t total_sector_bytes = 0;
for(const auto sector : sectors) {
total_sector_bytes += size_t(128 << sector->size) + 2;
}
while(true) {
const size_t size =
mark_size +
post_index_address_mark_bytes * data_size +
total_sector_bytes * data_size +
sectors.size() * (
(pre_address_mark_bytes + 6 + post_address_mark_bytes + pre_data_mark_bytes + post_data_bytes) * data_size + 2 * mark_size
);
// If this track already fits, do nothing.
if(size*8 < max_size) break;
// If all gaps are already zero, do nothing.
if(!post_index_address_mark_bytes && !pre_address_mark_bytes && !post_address_mark_bytes && !pre_data_mark_bytes && !post_data_bytes)
break;
// Very simple solution: try halving all gaps.
post_index_address_mark_bytes >>= 1;
pre_address_mark_bytes >>= 1;
post_address_mark_bytes >>= 1;
pre_data_mark_bytes >>= 1;
post_data_bytes >>= 1;
}
// Output the index mark.
shifter.add_index_address_mark(); shifter.add_index_address_mark();
// add the post-index mark // Add the post-index mark.
for(std::size_t c = 0; c < post_index_address_mark_bytes; c++) shifter.add_byte(post_index_address_mark_value); for(std::size_t c = 0; c < post_index_address_mark_bytes; c++) shifter.add_byte(post_index_address_mark_value);
// add sectors // Add sectors.
for(const Sector *sector : sectors) { for(const Sector *sector : sectors) {
// gap // Gap.
for(std::size_t c = 0; c < pre_address_mark_bytes; c++) shifter.add_byte(0x00); for(std::size_t c = 0; c < pre_address_mark_bytes; c++) shifter.add_byte(0x00);
// sector header // Sector header.
shifter.add_ID_address_mark(); shifter.add_ID_address_mark();
shifter.add_byte(sector->address.track); shifter.add_byte(sector->address.track);
shifter.add_byte(sector->address.side); shifter.add_byte(sector->address.side);
@ -150,11 +204,11 @@ template<class T> std::shared_ptr<Storage::Disk::Track>
shifter.add_byte(sector->size); shifter.add_byte(sector->size);
shifter.add_crc(sector->has_header_crc_error); shifter.add_crc(sector->has_header_crc_error);
// gap // Gap.
for(std::size_t c = 0; c < post_address_mark_bytes; c++) shifter.add_byte(post_address_mark_value); for(std::size_t c = 0; c < post_address_mark_bytes; c++) shifter.add_byte(post_address_mark_value);
for(std::size_t c = 0; c < pre_data_mark_bytes; c++) shifter.add_byte(0x00); for(std::size_t c = 0; c < pre_data_mark_bytes; c++) shifter.add_byte(0x00);
// data, if attached // Data, if attached.
// TODO: allow for weak/fuzzy data. // TODO: allow for weak/fuzzy data.
if(!sector->samples.empty()) { if(!sector->samples.empty()) {
if(sector->is_deleted) if(sector->is_deleted)
@ -173,14 +227,13 @@ template<class T> std::shared_ptr<Storage::Disk::Track>
shifter.add_crc(sector->has_data_crc_error); shifter.add_crc(sector->has_data_crc_error);
} }
// gap // Gap.
for(std::size_t c = 0; c < post_data_bytes; c++) shifter.add_byte(post_data_value); for(std::size_t c = 0; c < post_data_bytes; c++) shifter.add_byte(post_data_value);
} }
while(segment.data.size() < expected_track_bytes*8) shifter.add_byte(0x00); while(segment.data.size() < expected_track_bytes*8) shifter.add_byte(0x00);
// Allow the amount of data written to be up to 10% more than the expected size. Which is generous. // Allow the amount of data written to be up to 10% more than the expected size. Which is generous.
const std::size_t max_size = (expected_track_bytes + (expected_track_bytes / 10)) * 8;
if(segment.data.size() > max_size) segment.data.resize(max_size); if(segment.data.size() > max_size) segment.data.resize(max_size);
return std::shared_ptr<Storage::Disk::Track>(new Storage::Disk::PCMTrack(std::move(segment))); return std::shared_ptr<Storage::Disk::Track>(new Storage::Disk::PCMTrack(std::move(segment)));