2016-09-19 01:18:11 +00:00
|
|
|
//
|
|
|
|
// Disk.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/09/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-09-19 01:18:11 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Disk.hpp"
|
2018-01-25 02:48:44 +00:00
|
|
|
|
|
|
|
#include "../../../Storage/Disk/Controller/DiskController.hpp"
|
|
|
|
#include "../../../Storage/Disk/Encodings/MFM/Parser.hpp"
|
2020-01-20 04:14:35 +00:00
|
|
|
#include "../../../Numeric/CRC.hpp"
|
2018-01-25 02:48:44 +00:00
|
|
|
|
2016-09-19 11:35:02 +00:00
|
|
|
#include <algorithm>
|
2024-02-24 20:09:40 +00:00
|
|
|
#include <cstring>
|
2016-09-19 01:18:11 +00:00
|
|
|
|
2018-01-25 02:48:44 +00:00
|
|
|
using namespace Analyser::Static::Acorn;
|
2016-09-19 01:18:11 +00:00
|
|
|
|
2018-01-25 02:48:44 +00:00
|
|
|
std::unique_ptr<Catalogue> Analyser::Static::Acorn::GetDFSCatalogue(const std::shared_ptr<Storage::Disk::Disk> &disk) {
|
2016-09-19 02:18:01 +00:00
|
|
|
// c.f. http://beebwiki.mdfs.net/Acorn_DFS_disc_format
|
2019-12-22 04:52:04 +00:00
|
|
|
auto catalogue = std::make_unique<Catalogue>();
|
2023-12-11 03:17:23 +00:00
|
|
|
Storage::Encodings::MFM::Parser parser(Storage::Encodings::MFM::Density::Single, disk);
|
2016-09-19 02:03:06 +00:00
|
|
|
|
2023-12-11 14:58:24 +00:00
|
|
|
const Storage::Encodings::MFM::Sector *const names = parser.sector(0, 0, 0);
|
|
|
|
const Storage::Encodings::MFM::Sector *const details = parser.sector(0, 0, 1);
|
2016-09-19 02:18:01 +00:00
|
|
|
|
2016-09-25 21:46:11 +00:00
|
|
|
if(!names || !details) return nullptr;
|
2017-11-01 01:32:28 +00:00
|
|
|
if(names->samples.empty() || details->samples.empty()) return nullptr;
|
|
|
|
if(names->samples[0].size() != 256 || details->samples[0].size() != 256) return nullptr;
|
2016-09-19 02:18:01 +00:00
|
|
|
|
2017-11-01 01:32:28 +00:00
|
|
|
uint8_t final_file_offset = details->samples[0][5];
|
2016-09-19 11:35:02 +00:00
|
|
|
if(final_file_offset&7) return nullptr;
|
2016-12-30 22:29:51 +00:00
|
|
|
if(final_file_offset < 8) return nullptr;
|
2016-09-19 02:18:01 +00:00
|
|
|
|
2016-09-19 11:35:02 +00:00
|
|
|
char disk_name[13];
|
2017-11-01 01:32:28 +00:00
|
|
|
snprintf(disk_name, 13, "%.8s%.4s", &names->samples[0][0], &details->samples[0][0]);
|
2016-09-19 11:35:02 +00:00
|
|
|
catalogue->name = disk_name;
|
|
|
|
|
2017-11-01 01:32:28 +00:00
|
|
|
switch((details->samples[0][6] >> 4)&3) {
|
2016-09-19 11:35:02 +00:00
|
|
|
case 0: catalogue->bootOption = Catalogue::BootOption::None; break;
|
|
|
|
case 1: catalogue->bootOption = Catalogue::BootOption::LoadBOOT; break;
|
|
|
|
case 2: catalogue->bootOption = Catalogue::BootOption::RunBOOT; break;
|
|
|
|
case 3: catalogue->bootOption = Catalogue::BootOption::ExecBOOT; break;
|
|
|
|
}
|
|
|
|
|
2018-01-24 03:18:16 +00:00
|
|
|
for(std::size_t file_offset = 8; file_offset < final_file_offset; file_offset += 8) {
|
2016-09-19 11:35:02 +00:00
|
|
|
File new_file;
|
2016-09-19 02:18:01 +00:00
|
|
|
char name[10];
|
2017-11-01 01:32:28 +00:00
|
|
|
snprintf(name, 10, "%c.%.7s", names->samples[0][file_offset + 7] & 0x7f, &names->samples[0][file_offset]);
|
2016-09-19 11:35:02 +00:00
|
|
|
new_file.name = name;
|
2024-11-30 02:08:35 +00:00
|
|
|
new_file.load_address = uint32_t(
|
|
|
|
details->samples[0][file_offset] |
|
|
|
|
(details->samples[0][file_offset+1] << 8) |
|
|
|
|
((details->samples[0][file_offset+6]&0x0c) << 14)
|
|
|
|
);
|
|
|
|
new_file.execution_address = uint32_t(
|
|
|
|
details->samples[0][file_offset+2] |
|
|
|
|
(details->samples[0][file_offset+3] << 8) |
|
|
|
|
((details->samples[0][file_offset+6]&0xc0) << 10)
|
|
|
|
);
|
2021-01-31 04:10:59 +00:00
|
|
|
if(names->samples[0][file_offset + 7] & 0x80) {
|
|
|
|
// File is locked; it may not be altered or deleted.
|
|
|
|
new_file.flags |= File::Flags::Locked;
|
|
|
|
}
|
2016-09-19 11:35:02 +00:00
|
|
|
|
2024-11-30 02:08:35 +00:00
|
|
|
auto data_length = long(
|
|
|
|
details->samples[0][file_offset+4] |
|
|
|
|
(details->samples[0][file_offset+5] << 8) |
|
|
|
|
((details->samples[0][file_offset+6]&0x30) << 12)
|
|
|
|
);
|
2017-11-01 01:32:28 +00:00
|
|
|
int start_sector = details->samples[0][file_offset+7] | ((details->samples[0][file_offset+6]&0x03) << 8);
|
2020-05-10 03:00:39 +00:00
|
|
|
new_file.data.reserve(size_t(data_length));
|
2016-09-19 11:35:02 +00:00
|
|
|
|
|
|
|
if(start_sector < 2) continue;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(data_length > 0) {
|
2024-11-30 02:08:35 +00:00
|
|
|
const uint8_t sector = uint8_t(start_sector % 10);
|
|
|
|
const uint8_t track = uint8_t(start_sector / 10);
|
|
|
|
++start_sector;
|
2016-09-19 11:35:02 +00:00
|
|
|
|
2023-12-11 14:58:24 +00:00
|
|
|
const Storage::Encodings::MFM::Sector *next_sector = parser.sector(0, track, sector);
|
2016-09-19 11:35:02 +00:00
|
|
|
if(!next_sector) break;
|
|
|
|
|
2024-11-30 02:08:35 +00:00
|
|
|
const long length_from_sector = std::min(data_length, 256l);
|
2017-11-01 01:32:28 +00:00
|
|
|
new_file.data.insert(new_file.data.end(), next_sector->samples[0].begin(), next_sector->samples[0].begin() + length_from_sector);
|
2016-09-19 11:35:02 +00:00
|
|
|
data_length -= length_from_sector;
|
|
|
|
}
|
2021-01-31 04:10:59 +00:00
|
|
|
if(!data_length) catalogue->files.push_back(std::move(new_file));
|
2016-09-19 02:18:01 +00:00
|
|
|
}
|
2016-09-19 02:03:06 +00:00
|
|
|
|
2016-09-19 11:35:02 +00:00
|
|
|
return catalogue;
|
2016-09-19 01:18:11 +00:00
|
|
|
}
|
2021-01-31 04:10:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Primary resource used: "Acorn 8-Bit ADFS Filesystem Structure";
|
|
|
|
http://mdfs.net/Docs/Comp/Disk/Format/ADFS
|
|
|
|
*/
|
2018-01-25 02:48:44 +00:00
|
|
|
std::unique_ptr<Catalogue> Analyser::Static::Acorn::GetADFSCatalogue(const std::shared_ptr<Storage::Disk::Disk> &disk) {
|
2019-12-22 04:52:04 +00:00
|
|
|
auto catalogue = std::make_unique<Catalogue>();
|
2023-12-11 03:17:23 +00:00
|
|
|
Storage::Encodings::MFM::Parser parser(Storage::Encodings::MFM::Density::Double, disk);
|
2016-09-25 18:11:22 +00:00
|
|
|
|
2024-03-04 15:13:57 +00:00
|
|
|
// Grab the second half of the free-space map because it has the boot option in it.
|
2024-04-18 02:44:55 +00:00
|
|
|
const Storage::Encodings::MFM::Sector *free_space_map_second_half = parser.sector(0, 0, 1);
|
2016-09-25 22:32:26 +00:00
|
|
|
if(!free_space_map_second_half) return nullptr;
|
2024-04-18 02:44:55 +00:00
|
|
|
catalogue->has_large_sectors = free_space_map_second_half->samples[0].size() == 1024;
|
|
|
|
|
|
|
|
// Possibility: this is a large-sector disk with an old-style free space map. In which
|
|
|
|
// case the above just read the start of the root directory.
|
|
|
|
uint8_t first_directory_sector = 2;
|
|
|
|
if(catalogue->has_large_sectors && !memcmp(&free_space_map_second_half->samples[0][1], "Hugo", 4)) {
|
|
|
|
free_space_map_second_half = parser.sector(0, 0, 0);
|
|
|
|
if(!free_space_map_second_half) return nullptr;
|
|
|
|
first_directory_sector = 1;
|
|
|
|
}
|
2016-09-25 22:32:26 +00:00
|
|
|
|
|
|
|
std::vector<uint8_t> root_directory;
|
2024-04-18 02:44:55 +00:00
|
|
|
root_directory.reserve(catalogue->has_large_sectors ? 2*1024 : 5*256);
|
2024-03-04 15:13:57 +00:00
|
|
|
|
2024-04-18 02:44:55 +00:00
|
|
|
for(uint8_t c = first_directory_sector; c < first_directory_sector + (catalogue->has_large_sectors ? 2 : 5); c++) {
|
2023-12-11 14:58:24 +00:00
|
|
|
const Storage::Encodings::MFM::Sector *const sector = parser.sector(0, 0, c);
|
2016-09-25 22:32:26 +00:00
|
|
|
if(!sector) return nullptr;
|
2017-11-01 01:32:28 +00:00
|
|
|
root_directory.insert(root_directory.end(), sector->samples[0].begin(), sector->samples[0].end());
|
2016-09-25 22:32:26 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 15:13:57 +00:00
|
|
|
// Check for end of directory marker.
|
2024-04-18 02:44:55 +00:00
|
|
|
if(root_directory[catalogue->has_large_sectors ? 0x7d7 : 0x4cb]) return nullptr;
|
2024-03-04 15:13:57 +00:00
|
|
|
|
|
|
|
// Check for both directory identifiers.
|
2024-04-18 02:44:55 +00:00
|
|
|
const uint8_t *const start_id = &root_directory[1];
|
|
|
|
const uint8_t *const end_id = &root_directory[root_directory.size() - 5];
|
|
|
|
catalogue->is_hugo = !memcmp(start_id, "Hugo", 4) && !memcmp(end_id, "Hugo", 4);
|
|
|
|
const bool is_nick = !memcmp(start_id, "Nick", 4) && !memcmp(end_id, "Nick", 4);
|
2024-02-23 03:19:10 +00:00
|
|
|
if(!catalogue->is_hugo && !is_nick) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-09-25 22:32:26 +00:00
|
|
|
|
2024-04-18 02:44:55 +00:00
|
|
|
if(!catalogue->has_large_sectors) {
|
2024-03-04 15:13:57 +00:00
|
|
|
// TODO: I don't know where the boot option rests with large sectors.
|
|
|
|
switch(free_space_map_second_half->samples[0][0xfd]) {
|
|
|
|
default: catalogue->bootOption = Catalogue::BootOption::None; break;
|
|
|
|
case 1: catalogue->bootOption = Catalogue::BootOption::LoadBOOT; break;
|
|
|
|
case 2: catalogue->bootOption = Catalogue::BootOption::RunBOOT; break;
|
|
|
|
case 3: catalogue->bootOption = Catalogue::BootOption::ExecBOOT; break;
|
|
|
|
}
|
2016-09-25 22:32:26 +00:00
|
|
|
}
|
2016-09-25 18:11:22 +00:00
|
|
|
|
2021-01-31 04:10:59 +00:00
|
|
|
// Parse the root directory, at least.
|
2024-11-30 02:08:35 +00:00
|
|
|
const std::size_t directory_extent = catalogue->has_large_sectors ? 0x7d7 : 0x4cb;
|
|
|
|
for(std::size_t file_offset = 0x005; file_offset < directory_extent; file_offset += 0x1a) {
|
2021-01-31 04:10:59 +00:00
|
|
|
// Obtain the name, which will be at most ten characters long, and will
|
|
|
|
// be terminated by either a NULL character or a \r.
|
2024-03-04 15:13:57 +00:00
|
|
|
char name[11]{};
|
2021-01-31 04:10:59 +00:00
|
|
|
std::size_t c = 0;
|
|
|
|
for(; c < 10; c++) {
|
|
|
|
const char next = root_directory[file_offset + c] & 0x7f;
|
|
|
|
name[c] = next;
|
|
|
|
if(next == '\0' || next == '\r') break;
|
|
|
|
}
|
|
|
|
name[c] = '\0';
|
|
|
|
|
2024-05-14 02:25:02 +00:00
|
|
|
// An empty name implies the directory has ended; files are always listed in case-insensitive
|
|
|
|
// sorted order, with that list being terminated by a '\0'.
|
|
|
|
if(name[0] == '\0') break;
|
2021-01-31 04:10:59 +00:00
|
|
|
|
|
|
|
// Populate a file then.
|
|
|
|
File new_file;
|
|
|
|
new_file.name = name;
|
|
|
|
new_file.flags =
|
|
|
|
(root_directory[file_offset + 0] & 0x80 ? File::Flags::Readable : 0) |
|
|
|
|
(root_directory[file_offset + 1] & 0x80 ? File::Flags::Writable : 0) |
|
|
|
|
(root_directory[file_offset + 2] & 0x80 ? File::Flags::Locked : 0) |
|
|
|
|
(root_directory[file_offset + 3] & 0x80 ? File::Flags::IsDirectory : 0) |
|
|
|
|
(root_directory[file_offset + 4] & 0x80 ? File::Flags::ExecuteOnly : 0) |
|
|
|
|
(root_directory[file_offset + 5] & 0x80 ? File::Flags::PubliclyReadable : 0) |
|
|
|
|
(root_directory[file_offset + 6] & 0x80 ? File::Flags::PubliclyWritable : 0) |
|
|
|
|
(root_directory[file_offset + 7] & 0x80 ? File::Flags::PubliclyExecuteOnly : 0) |
|
|
|
|
(root_directory[file_offset + 8] & 0x80 ? File::Flags::IsPrivate : 0);
|
|
|
|
|
|
|
|
new_file.load_address =
|
|
|
|
(uint32_t(root_directory[file_offset + 0x0a]) << 0) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x0b]) << 8) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x0c]) << 16) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x0d]) << 24);
|
|
|
|
|
|
|
|
new_file.execution_address =
|
|
|
|
(uint32_t(root_directory[file_offset + 0x0e]) << 0) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x0f]) << 8) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x10]) << 16) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x11]) << 24);
|
|
|
|
|
|
|
|
new_file.sequence_number = root_directory[file_offset + 0x19];
|
|
|
|
|
|
|
|
const uint32_t size =
|
|
|
|
(uint32_t(root_directory[file_offset + 0x12]) << 0) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x13]) << 8) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x14]) << 16) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x15]) << 24);
|
|
|
|
|
|
|
|
uint32_t start_sector =
|
|
|
|
(uint32_t(root_directory[file_offset + 0x16]) << 0) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x17]) << 8) |
|
|
|
|
(uint32_t(root_directory[file_offset + 0x18]) << 16);
|
|
|
|
|
|
|
|
new_file.data.reserve(size);
|
|
|
|
while(new_file.data.size() < size) {
|
2024-11-30 02:08:35 +00:00
|
|
|
const Storage::Encodings::MFM::Sector *const sector =
|
|
|
|
parser.sector(start_sector / (80 * 16), (start_sector / 16) % 80, start_sector % 16);
|
2021-01-31 04:10:59 +00:00
|
|
|
if(!sector) break;
|
|
|
|
|
|
|
|
const auto length_from_sector = std::min(size - new_file.data.size(), sector->samples[0].size());
|
2024-11-30 02:08:35 +00:00
|
|
|
new_file.data.insert(
|
|
|
|
new_file.data.end(),
|
|
|
|
sector->samples[0].begin(),
|
|
|
|
sector->samples[0].begin() + ssize_t(length_from_sector)
|
|
|
|
);
|
2021-01-31 04:10:59 +00:00
|
|
|
++start_sector;
|
|
|
|
}
|
|
|
|
|
|
|
|
catalogue->files.push_back(std::move(new_file));
|
|
|
|
}
|
|
|
|
|
2024-05-22 01:49:30 +00:00
|
|
|
// Include the directory title.
|
2024-05-22 02:06:09 +00:00
|
|
|
const char *title, *name;
|
2024-05-22 01:49:30 +00:00
|
|
|
if(catalogue->has_large_sectors) {
|
2024-05-22 02:06:09 +00:00
|
|
|
title = reinterpret_cast<const char *>(&root_directory[0x7dd]);
|
|
|
|
name = reinterpret_cast<const char *>(&root_directory[0x7f0]);
|
2024-05-22 01:49:30 +00:00
|
|
|
} else {
|
2024-05-22 02:06:09 +00:00
|
|
|
title = reinterpret_cast<const char *>(&root_directory[0x4d9]);
|
|
|
|
name = reinterpret_cast<const char *>(&root_directory[0x4cc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
catalogue->name = std::string(title, strnlen(title, 19));
|
|
|
|
if(catalogue->name.empty() || catalogue->name == "$") {
|
|
|
|
catalogue->name = std::string(name, strnlen(name, 10));
|
2024-05-22 01:49:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 18:11:22 +00:00
|
|
|
return catalogue;
|
|
|
|
}
|