2019-08-25 19:09:27 +00:00
|
|
|
//
|
|
|
|
// HFV.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 25/08/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "HFV.hpp"
|
|
|
|
|
|
|
|
using namespace Storage::MassStorage;
|
|
|
|
|
|
|
|
HFV::HFV(const std::string &file_name) : file_(file_name) {
|
|
|
|
// Is the file a multiple of 512 bytes in size and larger than a floppy disk?
|
|
|
|
const auto file_size = file_.stats().st_size;
|
|
|
|
if(file_size & 511 || file_size <= 800*1024) throw std::exception();
|
|
|
|
|
2021-05-13 23:06:00 +00:00
|
|
|
// Is this an HFS volume?
|
|
|
|
// TODO: check filing system for MFS or HFS+.
|
|
|
|
const auto prefix = file_.read(2);
|
|
|
|
if(prefix[0] != 'L' || prefix[1] != 'K') throw std::exception();
|
2019-08-25 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t HFV::get_block_size() {
|
|
|
|
return 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HFV::get_number_of_blocks() {
|
2019-08-26 03:03:54 +00:00
|
|
|
return mapper_.get_number_of_blocks();
|
2019-08-25 19:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> HFV::get_block(size_t address) {
|
2019-09-16 02:06:45 +00:00
|
|
|
const auto written = writes_.find(address);
|
|
|
|
if(written != writes_.end()) return written->second;
|
|
|
|
|
2019-08-26 03:03:54 +00:00
|
|
|
const auto source_address = mapper_.to_source_address(address);
|
|
|
|
if(source_address >= 0 && size_t(source_address)*get_block_size() < size_t(file_.stats().st_size)) {
|
|
|
|
const long file_offset = long(get_block_size()) * long(source_address);
|
|
|
|
file_.seek(file_offset, SEEK_SET);
|
|
|
|
return mapper_.convert_source_block(source_address, file_.read(get_block_size()));
|
|
|
|
} else {
|
|
|
|
return mapper_.convert_source_block(source_address);
|
|
|
|
}
|
2019-08-25 19:09:27 +00:00
|
|
|
}
|
2019-08-25 19:16:35 +00:00
|
|
|
|
|
|
|
void HFV::set_block(size_t address, const std::vector<uint8_t> &contents) {
|
2019-09-19 00:18:02 +00:00
|
|
|
const auto source_address = mapper_.to_source_address(address);
|
|
|
|
if(source_address >= 0 && size_t(source_address)*get_block_size() < size_t(file_.stats().st_size)) {
|
|
|
|
const long file_offset = long(get_block_size()) * long(source_address);
|
|
|
|
file_.seek(file_offset, SEEK_SET);
|
|
|
|
file_.write(contents);
|
|
|
|
} else {
|
|
|
|
writes_[address] = contents;
|
2023-05-12 18:14:45 +00:00
|
|
|
}
|
2019-08-26 03:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HFV::set_drive_type(Encodings::Macintosh::DriveType drive_type) {
|
|
|
|
mapper_.set_drive_type(drive_type, size_t(file_.stats().st_size) / get_block_size());
|
2019-08-25 19:16:35 +00:00
|
|
|
}
|