2016-07-10 12:54:39 +00:00
|
|
|
//
|
|
|
|
// Disk.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Disk_hpp
|
|
|
|
#define Disk_hpp
|
|
|
|
|
2016-11-26 06:27:06 +00:00
|
|
|
#include <map>
|
2016-12-30 03:15:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2016-12-25 03:11:31 +00:00
|
|
|
#include <set>
|
2016-12-30 03:15:58 +00:00
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
#include "../Storage.hpp"
|
2016-12-30 03:15:58 +00:00
|
|
|
#include "../../Concurrency/AsyncTaskQueue.hpp"
|
2016-07-10 12:54:39 +00:00
|
|
|
|
|
|
|
namespace Storage {
|
2016-08-27 21:15:09 +00:00
|
|
|
namespace Disk {
|
2016-07-10 12:54:39 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Models a single track on a disk as a series of events, each event being of arbitrary length
|
|
|
|
and resulting in either a flux transition or the sensing of an index hole.
|
|
|
|
|
|
|
|
Subclasses should implement @c get_next_event.
|
|
|
|
*/
|
|
|
|
class Track {
|
|
|
|
public:
|
2016-07-10 22:36:52 +00:00
|
|
|
/*!
|
|
|
|
Describes a detectable track event — either a flux transition or the passing of the index hole,
|
|
|
|
along with the length of time between the previous event and its occurance.
|
|
|
|
|
|
|
|
The sum of all lengths of time across an entire track should be 1 — if an event is said to be
|
|
|
|
1/3 away then that means 1/3 of a rotation.
|
|
|
|
*/
|
2016-07-10 12:54:39 +00:00
|
|
|
struct Event {
|
|
|
|
enum {
|
|
|
|
IndexHole, FluxTransition
|
|
|
|
} type;
|
|
|
|
Time length;
|
|
|
|
};
|
|
|
|
|
2016-07-10 22:36:52 +00:00
|
|
|
/*!
|
2016-08-03 10:59:45 +00:00
|
|
|
@returns the next event that will be detected during rotation of this disk.
|
2016-07-10 22:36:52 +00:00
|
|
|
*/
|
2016-07-10 12:54:39 +00:00
|
|
|
virtual Event get_next_event() = 0;
|
2016-08-03 10:59:45 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Jumps to the event latest offset that is less than or equal to the input time.
|
|
|
|
|
|
|
|
@returns the time jumped to.
|
|
|
|
*/
|
2016-12-17 21:26:45 +00:00
|
|
|
virtual Time seek_to(const Time &time_since_index_hole) = 0;
|
2016-12-30 22:25:39 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
The virtual copy constructor pattern; returns a copy of the Track.
|
|
|
|
*/
|
|
|
|
virtual Track *clone() = 0;
|
2016-07-10 12:54:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Models a disk as a collection of tracks, providing a range of possible track positions and allowing
|
|
|
|
a point sampling of the track beneath any of those positions (if any).
|
|
|
|
|
|
|
|
The intention is not that tracks necessarily be evenly spaced; a head_position_count of 3 wih track
|
|
|
|
A appearing in positions 0 and 1, and track B appearing in position 2 is an appropriate use of this API
|
|
|
|
if it matches the media.
|
|
|
|
|
|
|
|
The track returned is point sampled only; if a particular disk drive has a sufficiently large head to
|
|
|
|
pick up multiple tracks at once then the drive responsible for asking for multiple tracks and for
|
|
|
|
merging the results.
|
|
|
|
*/
|
|
|
|
class Disk {
|
|
|
|
public:
|
2016-12-26 19:24:33 +00:00
|
|
|
virtual ~Disk() {}
|
2016-07-10 12:54:39 +00:00
|
|
|
|
|
|
|
/*!
|
2016-11-26 15:35:11 +00:00
|
|
|
@returns the number of discrete positions that this disk uses to model its complete surface area.
|
2016-07-10 12:54:39 +00:00
|
|
|
|
|
|
|
This is not necessarily a track count. There is no implicit guarantee that every position will
|
2016-11-26 15:35:11 +00:00
|
|
|
return a distinct track, or — e.g. if the media is holeless — will return any track at all.
|
2016-07-10 12:54:39 +00:00
|
|
|
*/
|
|
|
|
virtual unsigned int get_head_position_count() = 0;
|
|
|
|
|
2016-09-18 23:21:02 +00:00
|
|
|
/*!
|
2016-11-26 15:35:11 +00:00
|
|
|
@returns the number of heads (and, therefore, impliedly surfaces) available on this disk.
|
2016-09-18 23:21:02 +00:00
|
|
|
*/
|
|
|
|
virtual unsigned int get_head_count() { return 1; }
|
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
/*!
|
2016-11-26 15:35:11 +00:00
|
|
|
@returns the @c Track at @c position underneath @c head if there are any detectable events there;
|
|
|
|
returns @c nullptr otherwise.
|
2016-07-10 12:54:39 +00:00
|
|
|
*/
|
2016-11-26 06:27:06 +00:00
|
|
|
std::shared_ptr<Track> get_track_at_position(unsigned int head, unsigned int position);
|
|
|
|
|
2016-12-25 03:11:31 +00:00
|
|
|
/*!
|
|
|
|
Replaces the Track at position @c position underneath @c head with @c track. Ignored if this disk is read-only.
|
|
|
|
Subclasses that are not read-only should use the protected methods @c get_is_modified and, optionally,
|
|
|
|
@c get_modified_track_at_position to query for changes when closing.
|
|
|
|
*/
|
|
|
|
void set_track_at_position(unsigned int head, unsigned int position, const std::shared_ptr<Track> &track);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns whether the disk image is read only. Defaults to @c true if not overridden.
|
|
|
|
*/
|
|
|
|
virtual bool get_is_read_only() { return true; }
|
|
|
|
|
2016-11-26 06:27:06 +00:00
|
|
|
protected:
|
2016-11-26 15:35:11 +00:00
|
|
|
/*!
|
|
|
|
Subclasses should implement this to return the @c Track at @c position underneath @c head. Returned tracks
|
|
|
|
are cached internally so subclasses shouldn't attempt to build their own caches or worry about preparing
|
|
|
|
for track accesses at file load time. Appropriate behaviour is to create them lazily, on demand.
|
|
|
|
*/
|
|
|
|
virtual std::shared_ptr<Track> get_uncached_track_at_position(unsigned int head, unsigned int position) = 0;
|
2016-11-26 06:27:06 +00:00
|
|
|
|
2016-12-31 20:30:48 +00:00
|
|
|
/*!
|
|
|
|
Subclasses that support writing should implement @c store_updated_track_at_position to determine which bytes
|
|
|
|
have to be written from @c track, then obtain @c file_access_mutex and write new data to their file to represent
|
|
|
|
the track underneath @c head at @c position.
|
|
|
|
|
|
|
|
The base class will ensure that calls are made to @c get_uncached_track_at_position only while it holds @c file_access_mutex;
|
|
|
|
that mutex therefore provides serialisation of file access.
|
2016-12-25 03:11:31 +00:00
|
|
|
|
2016-12-31 20:30:48 +00:00
|
|
|
This method will be called asynchronously. Subclasses are responsible for any synchronisation other than that
|
|
|
|
provided automatically via @c file_access_mutex.
|
|
|
|
*/
|
2016-12-30 03:15:58 +00:00
|
|
|
virtual void store_updated_track_at_position(unsigned int head, unsigned int position, const std::shared_ptr<Track> &track, std::mutex &file_access_mutex);
|
2016-12-31 20:30:48 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Subclasses that support writing should call @c flush_updates during their destructor if there is anything they
|
|
|
|
do in @c store_updated_track_at_position that would not be valid after their destructor has completed but prior
|
|
|
|
to Disk's constructor running.
|
|
|
|
*/
|
2016-12-30 03:15:58 +00:00
|
|
|
void flush_updates();
|
2016-12-25 03:11:31 +00:00
|
|
|
|
2016-11-26 06:27:06 +00:00
|
|
|
private:
|
2016-12-25 03:11:31 +00:00
|
|
|
int get_id_for_track_at_position(unsigned int head, unsigned int position);
|
2016-12-31 20:30:48 +00:00
|
|
|
std::map<int, std::shared_ptr<Track>> cached_tracks_;
|
|
|
|
|
2016-12-30 03:15:58 +00:00
|
|
|
std::mutex file_access_mutex_;
|
|
|
|
std::unique_ptr<Concurrency::AsyncTaskQueue> update_queue_;
|
2016-07-10 12:54:39 +00:00
|
|
|
};
|
|
|
|
|
2016-08-27 21:15:09 +00:00
|
|
|
}
|
2016-07-10 12:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Disk_hpp */
|