2016-12-01 12:41:52 +00:00
|
|
|
//
|
2016-09-17 22:01:00 +00:00
|
|
|
// 1770.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/09/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "1770.hpp"
|
2016-09-25 00:12:45 +00:00
|
|
|
#include "../../Storage/Disk/Encodings/MFM.hpp"
|
2016-09-20 02:06:56 +00:00
|
|
|
|
|
|
|
using namespace WD;
|
|
|
|
|
2016-12-01 03:39:55 +00:00
|
|
|
WD1770::Status::Status() :
|
|
|
|
type(Status::One),
|
|
|
|
write_protect(false),
|
|
|
|
record_type(false),
|
|
|
|
spin_up(false),
|
|
|
|
record_not_found(false),
|
|
|
|
crc_error(false),
|
|
|
|
seek_error(false),
|
|
|
|
lost_data(false),
|
2016-12-01 12:41:52 +00:00
|
|
|
data_request(false),
|
2016-12-07 02:16:29 +00:00
|
|
|
interrupt_request(false),
|
2016-12-01 03:39:55 +00:00
|
|
|
busy(false)
|
|
|
|
{}
|
|
|
|
|
2016-11-26 15:29:30 +00:00
|
|
|
WD1770::WD1770(Personality p) :
|
2016-09-29 01:28:34 +00:00
|
|
|
Storage::Disk::Controller(8000000, 16, 300),
|
2016-12-26 21:46:26 +00:00
|
|
|
crc_generator_(0x1021, 0xffff),
|
2016-09-25 00:12:45 +00:00
|
|
|
interesting_event_mask_(Event::Command),
|
|
|
|
resume_point_(0),
|
2016-09-25 18:11:22 +00:00
|
|
|
delay_time_(0),
|
2016-09-26 01:38:52 +00:00
|
|
|
index_hole_count_target_(-1),
|
2016-09-27 11:36:37 +00:00
|
|
|
is_awaiting_marker_value_(false),
|
2016-12-29 00:26:21 +00:00
|
|
|
data_mode_(DataMode::Scanning),
|
2016-11-26 15:29:30 +00:00
|
|
|
delegate_(nullptr),
|
2016-12-02 01:12:22 +00:00
|
|
|
personality_(p),
|
|
|
|
head_is_loaded_(false)
|
2016-09-25 00:12:45 +00:00
|
|
|
{
|
2016-09-23 01:28:23 +00:00
|
|
|
set_is_double_density(false);
|
2016-09-25 00:12:45 +00:00
|
|
|
posit_event(Event::Command);
|
2016-09-23 01:25:31 +00:00
|
|
|
}
|
2016-09-20 19:56:31 +00:00
|
|
|
|
2016-09-20 02:06:56 +00:00
|
|
|
void WD1770::set_is_double_density(bool is_double_density)
|
|
|
|
{
|
2016-09-25 00:12:45 +00:00
|
|
|
is_double_density_ = is_double_density;
|
2016-09-23 01:28:23 +00:00
|
|
|
Storage::Time bit_length;
|
|
|
|
bit_length.length = 1;
|
|
|
|
bit_length.clock_rate = is_double_density ? 500000 : 250000;
|
|
|
|
set_expected_bit_length(bit_length);
|
2016-09-26 01:38:52 +00:00
|
|
|
|
|
|
|
if(!is_double_density) is_awaiting_marker_value_ = false;
|
2016-09-20 02:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WD1770::set_register(int address, uint8_t value)
|
|
|
|
{
|
2016-09-20 19:56:31 +00:00
|
|
|
switch(address&3)
|
|
|
|
{
|
|
|
|
case 0:
|
2016-11-25 12:51:39 +00:00
|
|
|
{
|
|
|
|
if((value&0xf0) == 0xd0)
|
|
|
|
{
|
|
|
|
printf("!!!TODO: force interrupt!!!\n");
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.type = Status::One;
|
|
|
|
});
|
2016-11-25 12:51:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
command_ = value;
|
|
|
|
posit_event(Event::Command);
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 19:56:31 +00:00
|
|
|
break;
|
|
|
|
case 1: track_ = value; break;
|
|
|
|
case 2: sector_ = value; break;
|
2016-12-25 17:31:38 +00:00
|
|
|
case 3:
|
|
|
|
data_ = value;
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = false;
|
|
|
|
});
|
|
|
|
break;
|
2016-09-20 19:56:31 +00:00
|
|
|
}
|
2016-09-20 02:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t WD1770::get_register(int address)
|
|
|
|
{
|
2016-09-20 19:56:31 +00:00
|
|
|
switch(address&3)
|
|
|
|
{
|
2016-12-01 03:26:02 +00:00
|
|
|
default:
|
|
|
|
{
|
2016-12-07 02:16:29 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.interrupt_request = false;
|
|
|
|
});
|
2016-12-01 03:26:02 +00:00
|
|
|
uint8_t status =
|
|
|
|
(status_.write_protect ? Flag::WriteProtect : 0) |
|
|
|
|
(status_.crc_error ? Flag::CRCError : 0) |
|
|
|
|
(status_.busy ? Flag::Busy : 0);
|
|
|
|
switch(status_.type)
|
|
|
|
{
|
|
|
|
case Status::One:
|
|
|
|
status |=
|
|
|
|
(get_is_track_zero() ? Flag::TrackZero : 0) |
|
|
|
|
(status_.seek_error ? Flag::SeekError : 0);
|
|
|
|
// TODO: index hole
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Status::Two:
|
|
|
|
case Status::Three:
|
|
|
|
status |=
|
|
|
|
(status_.record_type ? Flag::RecordType : 0) |
|
|
|
|
(status_.lost_data ? Flag::LostData : 0) |
|
2016-12-01 12:41:52 +00:00
|
|
|
(status_.data_request ? Flag::DataRequest : 0) |
|
2016-12-01 03:26:02 +00:00
|
|
|
(status_.record_not_found ? Flag::RecordNotFound : 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-01 12:41:52 +00:00
|
|
|
if(!has_motor_on_line())
|
2016-12-01 03:26:02 +00:00
|
|
|
{
|
2016-12-02 02:13:16 +00:00
|
|
|
status |= get_drive_is_ready() ? 0 : Flag::NotReady;
|
|
|
|
if(status_.type == Status::One)
|
|
|
|
status |= (head_is_loaded_ ? Flag::HeadLoaded : 0);
|
2016-12-01 03:26:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status |= (get_motor_on() ? Flag::MotorOn : 0);
|
|
|
|
if(status_.type == Status::One)
|
|
|
|
status |= (status_.spin_up ? Flag::SpinUp : 0);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
2016-09-20 19:56:31 +00:00
|
|
|
case 1: return track_;
|
|
|
|
case 2: return sector_;
|
2016-12-01 12:41:52 +00:00
|
|
|
case 3:
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = false;
|
|
|
|
});
|
|
|
|
return data_;
|
2016-09-20 19:56:31 +00:00
|
|
|
}
|
2016-09-20 02:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WD1770::run_for_cycles(unsigned int number_of_cycles)
|
|
|
|
{
|
2016-09-26 01:24:16 +00:00
|
|
|
Storage::Disk::Controller::run_for_cycles((int)number_of_cycles);
|
2016-09-25 00:12:45 +00:00
|
|
|
|
|
|
|
if(delay_time_)
|
|
|
|
{
|
|
|
|
if(delay_time_ <= number_of_cycles)
|
|
|
|
{
|
|
|
|
delay_time_ = 0;
|
|
|
|
posit_event(Event::Timer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delay_time_ -= number_of_cycles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WD1770::process_input_bit(int value, unsigned int cycles_since_index_hole)
|
|
|
|
{
|
2016-12-29 00:26:21 +00:00
|
|
|
if(data_mode_ == DataMode::Writing) return;
|
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
shift_register_ = (shift_register_ << 1) | value;
|
|
|
|
bits_since_token_++;
|
|
|
|
|
2016-12-29 00:26:21 +00:00
|
|
|
if(data_mode_ == DataMode::Scanning)
|
2016-09-25 00:12:45 +00:00
|
|
|
{
|
2017-01-02 01:39:19 +00:00
|
|
|
Token::Type token_type = Token::Byte;
|
2016-09-27 11:36:37 +00:00
|
|
|
if(!is_double_density_)
|
2016-09-25 00:12:45 +00:00
|
|
|
{
|
2016-09-27 11:36:37 +00:00
|
|
|
switch(shift_register_ & 0xffff)
|
|
|
|
{
|
|
|
|
case Storage::Encodings::MFM::FMIndexAddressMark:
|
|
|
|
token_type = Token::Index;
|
2016-12-29 00:51:27 +00:00
|
|
|
crc_generator_.reset();
|
2017-01-02 01:39:19 +00:00
|
|
|
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::IndexAddressByte);
|
2016-09-27 11:36:37 +00:00
|
|
|
break;
|
|
|
|
case Storage::Encodings::MFM::FMIDAddressMark:
|
|
|
|
token_type = Token::ID;
|
2016-12-29 00:51:27 +00:00
|
|
|
crc_generator_.reset();
|
2017-01-02 01:39:19 +00:00
|
|
|
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::IDAddressByte);
|
2016-09-27 11:36:37 +00:00
|
|
|
break;
|
|
|
|
case Storage::Encodings::MFM::FMDataAddressMark:
|
|
|
|
token_type = Token::Data;
|
2016-12-29 00:51:27 +00:00
|
|
|
crc_generator_.reset();
|
2017-01-02 01:39:19 +00:00
|
|
|
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::DataAddressByte);
|
2016-09-27 11:36:37 +00:00
|
|
|
break;
|
|
|
|
case Storage::Encodings::MFM::FMDeletedDataAddressMark:
|
|
|
|
token_type = Token::DeletedData;
|
2016-12-29 00:51:27 +00:00
|
|
|
crc_generator_.reset();
|
2017-01-02 01:39:19 +00:00
|
|
|
crc_generator_.add(latest_token_.byte_value = Storage::Encodings::MFM::DeletedDataAddressByte);
|
2016-09-27 11:36:37 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
}
|
2016-09-27 11:36:37 +00:00
|
|
|
else
|
2016-09-26 01:38:52 +00:00
|
|
|
{
|
2016-09-27 11:36:37 +00:00
|
|
|
switch(shift_register_ & 0xffff)
|
|
|
|
{
|
2016-12-28 23:29:37 +00:00
|
|
|
case Storage::Encodings::MFM::MFMIndexSync:
|
2016-09-27 11:36:37 +00:00
|
|
|
bits_since_token_ = 0;
|
|
|
|
is_awaiting_marker_value_ = true;
|
2017-01-02 01:39:19 +00:00
|
|
|
|
|
|
|
token_type = Token::Sync;
|
|
|
|
latest_token_.byte_value = Storage::Encodings::MFM::MFMIndexSyncByteValue;
|
|
|
|
break;
|
2016-12-28 23:29:37 +00:00
|
|
|
case Storage::Encodings::MFM::MFMSync:
|
2016-09-27 11:36:37 +00:00
|
|
|
bits_since_token_ = 0;
|
|
|
|
is_awaiting_marker_value_ = true;
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
2017-01-02 01:39:19 +00:00
|
|
|
|
|
|
|
token_type = Token::Sync;
|
|
|
|
latest_token_.byte_value = Storage::Encodings::MFM::MFMSyncByteValue;
|
|
|
|
break;
|
2016-09-27 11:36:37 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-09-26 01:38:52 +00:00
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-09-27 11:36:37 +00:00
|
|
|
if(token_type != Token::Byte)
|
|
|
|
{
|
|
|
|
latest_token_.type = token_type;
|
|
|
|
bits_since_token_ = 0;
|
|
|
|
posit_event(Event::Token);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(bits_since_token_ == 16)
|
|
|
|
{
|
|
|
|
latest_token_.type = Token::Byte;
|
|
|
|
latest_token_.byte_value = (uint8_t)(
|
|
|
|
((shift_register_ & 0x0001) >> 0) |
|
|
|
|
((shift_register_ & 0x0004) >> 1) |
|
|
|
|
((shift_register_ & 0x0010) >> 2) |
|
|
|
|
((shift_register_ & 0x0040) >> 3) |
|
|
|
|
((shift_register_ & 0x0100) >> 4) |
|
|
|
|
((shift_register_ & 0x0400) >> 5) |
|
|
|
|
((shift_register_ & 0x1000) >> 6) |
|
|
|
|
((shift_register_ & 0x4000) >> 7));
|
|
|
|
bits_since_token_ = 0;
|
2016-09-26 01:38:52 +00:00
|
|
|
|
|
|
|
if(is_awaiting_marker_value_ && is_double_density_)
|
|
|
|
{
|
|
|
|
is_awaiting_marker_value_ = false;
|
|
|
|
switch(latest_token_.byte_value)
|
|
|
|
{
|
2016-12-31 20:25:11 +00:00
|
|
|
case Storage::Encodings::MFM::IndexAddressByte:
|
2016-09-26 01:38:52 +00:00
|
|
|
latest_token_.type = Token::Index;
|
|
|
|
break;
|
2016-12-31 20:25:11 +00:00
|
|
|
case Storage::Encodings::MFM::IDAddressByte:
|
2016-09-26 01:38:52 +00:00
|
|
|
latest_token_.type = Token::ID;
|
|
|
|
break;
|
2016-12-31 20:25:11 +00:00
|
|
|
case Storage::Encodings::MFM::DataAddressByte:
|
2016-09-26 01:38:52 +00:00
|
|
|
latest_token_.type = Token::Data;
|
|
|
|
break;
|
2016-12-31 20:25:11 +00:00
|
|
|
case Storage::Encodings::MFM::DeletedDataAddressByte:
|
2016-09-26 01:38:52 +00:00
|
|
|
latest_token_.type = Token::DeletedData;
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.add(latest_token_.byte_value);
|
2016-09-25 00:12:45 +00:00
|
|
|
posit_event(Event::Token);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WD1770::process_index_hole()
|
|
|
|
{
|
|
|
|
index_hole_count_++;
|
|
|
|
posit_event(Event::IndexHole);
|
2016-09-25 18:11:22 +00:00
|
|
|
if(index_hole_count_target_ == index_hole_count_)
|
|
|
|
{
|
|
|
|
posit_event(Event::IndexHoleTarget);
|
|
|
|
index_hole_count_target_ = -1;
|
|
|
|
}
|
2016-09-25 02:43:12 +00:00
|
|
|
|
|
|
|
// motor power-down
|
2016-12-01 12:41:52 +00:00
|
|
|
if(index_hole_count_ == 9 && !status_.busy && has_motor_on_line())
|
2016-12-01 03:26:02 +00:00
|
|
|
{
|
|
|
|
set_motor_on(false);
|
|
|
|
}
|
2016-12-02 01:12:22 +00:00
|
|
|
|
|
|
|
// head unload
|
|
|
|
if(index_hole_count_ == 15 && !status_.busy && has_head_load_line())
|
|
|
|
{
|
|
|
|
set_head_load_request(false);
|
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
}
|
2016-09-20 20:58:23 +00:00
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
void WD1770::process_write_completed()
|
|
|
|
{
|
|
|
|
posit_event(Event::DataWritten);
|
|
|
|
}
|
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
#define WAIT_FOR_EVENT(mask) resume_point_ = __LINE__; interesting_event_mask_ = mask; return; case __LINE__:
|
|
|
|
#define WAIT_FOR_TIME(ms) resume_point_ = __LINE__; interesting_event_mask_ = Event::Timer; delay_time_ = ms * 8000; if(delay_time_) return; case __LINE__:
|
2016-12-25 17:31:38 +00:00
|
|
|
#define WAIT_FOR_BYTES(count) resume_point_ = __LINE__; interesting_event_mask_ = Event::Token; distance_into_section_ = 0; return; case __LINE__: if(latest_token_.type == Token::Byte) distance_into_section_++; if(distance_into_section_ < count) { interesting_event_mask_ = Event::Token; return; }
|
2016-09-25 00:12:45 +00:00
|
|
|
#define BEGIN_SECTION() switch(resume_point_) { default:
|
|
|
|
#define END_SECTION() 0; }
|
|
|
|
|
2016-09-25 13:54:49 +00:00
|
|
|
#define READ_ID() \
|
|
|
|
if(new_event_type == Event::Token) \
|
|
|
|
{ \
|
2016-12-29 00:26:21 +00:00
|
|
|
if(!distance_into_section_ && latest_token_.type == Token::ID) {data_mode_ = DataMode::Reading; distance_into_section_++; } \
|
2016-09-25 13:54:49 +00:00
|
|
|
else if(distance_into_section_ && distance_into_section_ < 7 && latest_token_.type == Token::Byte) \
|
|
|
|
{ \
|
2016-11-21 05:21:49 +00:00
|
|
|
header_[distance_into_section_ - 1] = latest_token_.byte_value; \
|
2016-09-25 13:54:49 +00:00
|
|
|
distance_into_section_++; \
|
|
|
|
} \
|
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-09-25 18:11:22 +00:00
|
|
|
#define CONCATENATE(x, y) x ## y
|
|
|
|
#define INDIRECT_CONCATENATE(x, y) TOKENPASTE(x, y)
|
|
|
|
#define LINE_LABEL INDIRECT_CONCATENATE(label, __LINE__)
|
|
|
|
|
|
|
|
#define SPIN_UP() \
|
2016-09-26 01:24:16 +00:00
|
|
|
set_motor_on(true); \
|
2016-09-25 18:11:22 +00:00
|
|
|
index_hole_count_ = 0; \
|
|
|
|
index_hole_count_target_ = 6; \
|
2016-12-01 03:26:02 +00:00
|
|
|
WAIT_FOR_EVENT(Event::IndexHoleTarget); \
|
|
|
|
status_.spin_up = true;
|
2016-09-25 18:11:22 +00:00
|
|
|
|
2016-12-31 21:01:44 +00:00
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! ! ! BITS !
|
|
|
|
// ! TYPE ! COMMAND ! 7 6 5 4 3 2 1 0 !
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! 1 ! Restore ! 0 0 0 0 h v r1 r0 !
|
|
|
|
// ! 1 ! Seek ! 0 0 0 1 h v r1 r0 !
|
|
|
|
// ! 1 ! Step ! 0 0 1 u h v r1 r0 !
|
|
|
|
// ! 1 ! Step-in ! 0 1 0 u h v r1 r0 !
|
|
|
|
// ! 1 ! Step-out ! 0 1 1 u h v r1 r0 !
|
|
|
|
// ! 2 ! Rd sectr ! 1 0 0 m h E 0 0 !
|
|
|
|
// ! 2 ! Wt sectr ! 1 0 1 m h E P a0 !
|
|
|
|
// ! 3 ! Rd addr ! 1 1 0 0 h E 0 0 !
|
|
|
|
// ! 3 ! Rd track ! 1 1 1 0 h E 0 0 !
|
|
|
|
// ! 3 ! Wt track ! 1 1 1 1 h E P 0 !
|
|
|
|
// ! 4 ! Forc int ! 1 1 0 1 i3 i2 i1 i0 !
|
|
|
|
// +--------+----------+-------------------------+
|
2016-12-02 01:12:22 +00:00
|
|
|
|
2016-09-25 02:04:54 +00:00
|
|
|
void WD1770::posit_event(Event new_event_type)
|
2016-09-25 00:12:45 +00:00
|
|
|
{
|
2016-09-25 02:04:54 +00:00
|
|
|
if(!(interesting_event_mask_ & (int)new_event_type)) return;
|
|
|
|
interesting_event_mask_ &= ~new_event_type;
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-12-01 12:41:52 +00:00
|
|
|
Status new_status;
|
2016-09-25 00:12:45 +00:00
|
|
|
BEGIN_SECTION()
|
|
|
|
|
|
|
|
// Wait for a new command, branch to the appropriate handler.
|
|
|
|
wait_for_command:
|
2016-09-25 02:04:54 +00:00
|
|
|
printf("Idle...\n");
|
2016-12-29 00:26:21 +00:00
|
|
|
data_mode_ = DataMode::Scanning;
|
2016-09-25 02:43:12 +00:00
|
|
|
index_hole_count_ = 0;
|
2016-12-01 03:39:55 +00:00
|
|
|
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.busy = false;
|
2016-12-07 02:16:29 +00:00
|
|
|
status.interrupt_request = true;
|
2016-12-01 12:41:52 +00:00
|
|
|
});
|
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
WAIT_FOR_EVENT(Event::Command);
|
2016-12-01 12:41:52 +00:00
|
|
|
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.busy = true;
|
2016-12-07 02:16:29 +00:00
|
|
|
status.interrupt_request = false;
|
2016-12-01 12:41:52 +00:00
|
|
|
});
|
|
|
|
|
2016-09-25 02:04:54 +00:00
|
|
|
printf("Starting %02x\n", command_);
|
2016-12-01 03:26:02 +00:00
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
if(!(command_ & 0x80)) goto begin_type_1;
|
|
|
|
if(!(command_ & 0x40)) goto begin_type_2;
|
|
|
|
goto begin_type_3;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Type 1 entry point.
|
|
|
|
*/
|
2016-12-31 21:01:44 +00:00
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! ! ! BITS !
|
|
|
|
// ! TYPE ! COMMAND ! 7 6 5 4 3 2 1 0 !
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! 1 ! Restore ! 0 0 0 0 h v r1 r0 !
|
|
|
|
// ! 1 ! Seek ! 0 0 0 1 h v r1 r0 !
|
|
|
|
// ! 1 ! Step ! 0 0 1 u h v r1 r0 !
|
|
|
|
// ! 1 ! Step-in ! 0 1 0 u h v r1 r0 !
|
|
|
|
// ! 1 ! Step-out ! 0 1 1 u h v r1 r0 !
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
begin_type_1:
|
|
|
|
// Set initial flags, skip spin-up if possible.
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.type = Status::One;
|
|
|
|
status.seek_error = false;
|
|
|
|
status.crc_error = false;
|
|
|
|
status.data_request = false;
|
|
|
|
});
|
2016-12-01 03:26:02 +00:00
|
|
|
|
2016-12-02 01:12:22 +00:00
|
|
|
if(!has_motor_on_line() && !has_head_load_line()) goto test_type1_type;
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-12-02 01:12:22 +00:00
|
|
|
if(has_motor_on_line()) goto begin_type1_spin_up;
|
|
|
|
goto begin_type1_load_head;
|
|
|
|
|
|
|
|
begin_type1_load_head:
|
|
|
|
if(!(command_&0x08))
|
|
|
|
{
|
|
|
|
set_head_load_request(false);
|
|
|
|
goto test_type1_type;
|
|
|
|
}
|
|
|
|
set_head_load_request(true);
|
|
|
|
if(head_is_loaded_) goto test_type1_type;
|
2016-12-02 02:13:16 +00:00
|
|
|
WAIT_FOR_EVENT(Event::HeadLoad);
|
2016-12-02 01:12:22 +00:00
|
|
|
goto test_type1_type;
|
|
|
|
|
|
|
|
begin_type1_spin_up:
|
|
|
|
if((command_&0x08) || get_motor_on()) goto test_type1_type;
|
2016-09-25 18:11:22 +00:00
|
|
|
SPIN_UP();
|
2016-09-25 00:12:45 +00:00
|
|
|
|
|
|
|
test_type1_type:
|
|
|
|
// Set step direction if this is a step in or out.
|
|
|
|
if((command_ >> 5) == 2) step_direction_ = 1;
|
|
|
|
if((command_ >> 5) == 3) step_direction_ = 0;
|
|
|
|
if((command_ >> 5) != 0) goto perform_step_command;
|
|
|
|
|
2016-09-25 02:43:12 +00:00
|
|
|
// This is now definitely either a seek or a restore; if it's a restore then set track to 0xff and data to 0x00.
|
|
|
|
if(!(command_ & 0x10))
|
|
|
|
{
|
|
|
|
track_ = 0xff;
|
|
|
|
data_ = 0;
|
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
|
|
|
|
perform_seek_or_restore_command:
|
|
|
|
if(track_ == data_) goto verify;
|
2016-09-25 02:04:54 +00:00
|
|
|
step_direction_ = (data_ > track_);
|
2016-09-25 00:12:45 +00:00
|
|
|
|
|
|
|
adjust_track:
|
2016-09-25 02:04:54 +00:00
|
|
|
if(step_direction_) track_++; else track_--;
|
2016-09-25 00:12:45 +00:00
|
|
|
|
|
|
|
perform_step:
|
|
|
|
if(!step_direction_ && get_is_track_zero())
|
|
|
|
{
|
|
|
|
track_ = 0;
|
|
|
|
goto verify;
|
|
|
|
}
|
2016-09-25 02:48:45 +00:00
|
|
|
step(step_direction_ ? 1 : -1);
|
2016-09-25 00:12:45 +00:00
|
|
|
int time_to_wait;
|
|
|
|
switch(command_ & 3)
|
|
|
|
{
|
2016-09-20 19:56:31 +00:00
|
|
|
default:
|
2016-12-01 03:26:02 +00:00
|
|
|
case 0: time_to_wait = 6; break;
|
|
|
|
case 1: time_to_wait = 12; break;
|
|
|
|
case 2: time_to_wait = (personality_ == P1772) ? 2 : 20; break;
|
|
|
|
case 3: time_to_wait = (personality_ == P1772) ? 3 : 30; break;
|
2016-09-20 19:56:31 +00:00
|
|
|
}
|
2016-09-25 00:16:23 +00:00
|
|
|
WAIT_FOR_TIME(time_to_wait);
|
2016-09-25 00:12:45 +00:00
|
|
|
if(command_ >> 5) goto verify;
|
|
|
|
goto perform_seek_or_restore_command;
|
2016-09-21 02:14:33 +00:00
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
perform_step_command:
|
|
|
|
if(command_ & 0x10) goto adjust_track;
|
|
|
|
goto perform_step;
|
2016-09-21 02:14:33 +00:00
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
verify:
|
|
|
|
if(!(command_ & 0x04))
|
|
|
|
{
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
2016-09-23 01:25:31 +00:00
|
|
|
|
2016-09-25 13:54:49 +00:00
|
|
|
index_hole_count_ = 0;
|
2016-09-26 01:44:46 +00:00
|
|
|
distance_into_section_ = 0;
|
2016-09-25 13:54:49 +00:00
|
|
|
|
|
|
|
verify_read_data:
|
|
|
|
WAIT_FOR_EVENT(Event::IndexHole | Event::Token);
|
|
|
|
READ_ID();
|
|
|
|
|
|
|
|
if(index_hole_count_ == 6)
|
|
|
|
{
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.seek_error = true;
|
|
|
|
});
|
2016-09-25 13:54:49 +00:00
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
if(distance_into_section_ == 7)
|
|
|
|
{
|
2016-12-29 00:26:21 +00:00
|
|
|
data_mode_ = DataMode::Scanning;
|
2016-12-31 21:01:44 +00:00
|
|
|
if(crc_generator_.get_value())
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.crc_error = true;
|
|
|
|
});
|
|
|
|
goto verify_read_data;
|
|
|
|
}
|
|
|
|
|
2016-11-21 05:21:49 +00:00
|
|
|
if(header_[0] == track_)
|
2016-09-25 13:54:49 +00:00
|
|
|
{
|
2016-09-27 11:36:37 +00:00
|
|
|
printf("Reached track %d\n", track_);
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.crc_error = false;
|
|
|
|
});
|
2016-09-25 13:54:49 +00:00
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
distance_into_section_ = 0;
|
|
|
|
}
|
|
|
|
goto verify_read_data;
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-09-25 02:04:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Type 2 entry point.
|
|
|
|
*/
|
2016-12-31 21:01:44 +00:00
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! ! ! BITS !
|
|
|
|
// ! TYPE ! COMMAND ! 7 6 5 4 3 2 1 0 !
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! 2 ! Rd sectr ! 1 0 0 m h E 0 0 !
|
|
|
|
// ! 2 ! Wt sectr ! 1 0 1 m h E P a0 !
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
begin_type_2:
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.type = Status::Two;
|
|
|
|
status.lost_data = false;
|
|
|
|
status.record_not_found = false;
|
|
|
|
status.write_protect = false;
|
|
|
|
status.record_type = false;
|
|
|
|
status.data_request = false;
|
|
|
|
});
|
2016-09-25 02:29:02 +00:00
|
|
|
distance_into_section_ = 0;
|
2016-12-01 03:26:02 +00:00
|
|
|
|
2016-12-02 01:12:22 +00:00
|
|
|
if((command_&0x08) && has_motor_on_line()) goto test_type2_delay;
|
|
|
|
if(!has_motor_on_line() && !has_head_load_line()) goto test_type2_delay;
|
|
|
|
|
|
|
|
if(has_motor_on_line()) goto begin_type2_spin_up;
|
|
|
|
goto begin_type2_load_head;
|
2016-09-25 02:04:54 +00:00
|
|
|
|
2016-12-02 01:12:22 +00:00
|
|
|
begin_type2_load_head:
|
|
|
|
set_head_load_request(true);
|
|
|
|
if(head_is_loaded_) goto test_type2_delay;
|
2016-12-02 02:13:16 +00:00
|
|
|
WAIT_FOR_EVENT(Event::HeadLoad);
|
2016-12-02 01:12:22 +00:00
|
|
|
goto test_type2_delay;
|
|
|
|
|
|
|
|
begin_type2_spin_up:
|
|
|
|
if(get_motor_on()) goto test_type2_delay;
|
2016-09-25 02:04:54 +00:00
|
|
|
// Perform spin up.
|
2016-09-25 18:11:22 +00:00
|
|
|
SPIN_UP();
|
2016-09-25 02:04:54 +00:00
|
|
|
|
|
|
|
test_type2_delay:
|
|
|
|
index_hole_count_ = 0;
|
|
|
|
if(!(command_ & 0x04)) goto test_type2_write_protection;
|
|
|
|
WAIT_FOR_TIME(30);
|
|
|
|
|
|
|
|
test_type2_write_protection:
|
2016-12-25 14:46:12 +00:00
|
|
|
if(command_&0x20 && get_drive_is_read_only())
|
2016-09-25 02:04:54 +00:00
|
|
|
{
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.write_protect = true;
|
|
|
|
});
|
2016-09-25 02:04:54 +00:00
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
type2_get_header:
|
|
|
|
WAIT_FOR_EVENT(Event::IndexHole | Event::Token);
|
2016-09-25 13:54:49 +00:00
|
|
|
READ_ID();
|
|
|
|
|
|
|
|
if(index_hole_count_ == 5)
|
2016-09-25 02:04:54 +00:00
|
|
|
{
|
2016-12-02 23:36:47 +00:00
|
|
|
printf("Failed to find sector %d\n", sector_);
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.record_not_found = true;
|
|
|
|
});
|
2016-09-25 02:04:54 +00:00
|
|
|
goto wait_for_command;
|
|
|
|
}
|
2016-09-25 13:54:49 +00:00
|
|
|
if(distance_into_section_ == 7)
|
|
|
|
{
|
2016-12-26 19:24:33 +00:00
|
|
|
printf("Considering %d/%d\n", header_[0], header_[2]);
|
2016-12-29 00:26:21 +00:00
|
|
|
data_mode_ = DataMode::Scanning;
|
2016-12-01 03:26:02 +00:00
|
|
|
if(header_[0] == track_ && header_[2] == sector_ &&
|
2016-12-01 12:41:52 +00:00
|
|
|
(has_motor_on_line() || !(command_&0x02) || ((command_&0x08) >> 3) == header_[1]))
|
2016-09-25 13:54:49 +00:00
|
|
|
{
|
2016-12-26 01:38:25 +00:00
|
|
|
printf("Found %d/%d\n", header_[0], header_[2]);
|
2016-12-28 23:29:37 +00:00
|
|
|
if(crc_generator_.get_value())
|
|
|
|
{
|
2016-12-29 02:24:19 +00:00
|
|
|
printf("CRC error; back to searching\n");
|
2016-12-28 23:29:37 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.crc_error = true;
|
|
|
|
});
|
|
|
|
goto type2_get_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.crc_error = false;
|
|
|
|
});
|
2016-09-25 13:54:49 +00:00
|
|
|
goto type2_read_or_write_data;
|
|
|
|
}
|
|
|
|
distance_into_section_ = 0;
|
|
|
|
}
|
2016-09-25 02:04:54 +00:00
|
|
|
goto type2_get_header;
|
|
|
|
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-09-25 02:04:54 +00:00
|
|
|
type2_read_or_write_data:
|
2016-09-25 02:29:02 +00:00
|
|
|
if(command_&0x20) goto type2_write_data;
|
|
|
|
goto type2_read_data;
|
|
|
|
|
|
|
|
type2_read_data:
|
|
|
|
WAIT_FOR_EVENT(Event::Token);
|
|
|
|
// TODO: timeout
|
|
|
|
if(latest_token_.type == Token::Data || latest_token_.type == Token::DeletedData)
|
|
|
|
{
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([this] (Status &status) {
|
|
|
|
status.record_type = (latest_token_.type == Token::DeletedData);
|
|
|
|
});
|
2016-09-25 02:29:02 +00:00
|
|
|
distance_into_section_ = 0;
|
2016-12-29 00:26:21 +00:00
|
|
|
data_mode_ = DataMode::Reading;
|
2016-09-25 02:29:02 +00:00
|
|
|
goto type2_read_byte;
|
|
|
|
}
|
|
|
|
goto type2_read_data;
|
|
|
|
|
|
|
|
type2_read_byte:
|
|
|
|
WAIT_FOR_EVENT(Event::Token);
|
|
|
|
if(latest_token_.type != Token::Byte) goto type2_read_byte;
|
2016-12-26 17:48:49 +00:00
|
|
|
data_ = latest_token_.byte_value;
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data |= status.data_request;
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
2016-09-25 02:29:02 +00:00
|
|
|
distance_into_section_++;
|
2016-11-21 05:21:49 +00:00
|
|
|
if(distance_into_section_ == 128 << header_[3])
|
2016-09-25 02:35:34 +00:00
|
|
|
{
|
|
|
|
distance_into_section_ = 0;
|
|
|
|
goto type2_check_crc;
|
|
|
|
}
|
|
|
|
goto type2_read_byte;
|
|
|
|
|
|
|
|
type2_check_crc:
|
|
|
|
WAIT_FOR_EVENT(Event::Token);
|
|
|
|
if(latest_token_.type != Token::Byte) goto type2_read_byte;
|
2016-11-21 05:21:49 +00:00
|
|
|
header_[distance_into_section_] = latest_token_.byte_value;
|
2016-09-25 02:35:34 +00:00
|
|
|
distance_into_section_++;
|
|
|
|
if(distance_into_section_ == 2)
|
2016-09-25 02:29:02 +00:00
|
|
|
{
|
2016-12-28 23:29:37 +00:00
|
|
|
if(crc_generator_.get_value())
|
2016-12-26 21:46:26 +00:00
|
|
|
{
|
2016-12-29 02:24:19 +00:00
|
|
|
printf("CRC error; terminating\n");
|
2016-12-28 23:29:37 +00:00
|
|
|
update_status([this] (Status &status) {
|
|
|
|
status.crc_error = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
2016-12-26 21:46:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 02:29:02 +00:00
|
|
|
if(command_ & 0x10)
|
|
|
|
{
|
|
|
|
sector_++;
|
|
|
|
goto test_type2_write_protection;
|
|
|
|
}
|
2016-09-27 11:36:37 +00:00
|
|
|
printf("Read sector %d\n", sector_);
|
2016-09-25 02:29:02 +00:00
|
|
|
goto wait_for_command;
|
|
|
|
}
|
2016-09-25 02:35:34 +00:00
|
|
|
goto type2_check_crc;
|
|
|
|
|
2016-09-25 02:29:02 +00:00
|
|
|
|
|
|
|
type2_write_data:
|
2016-12-25 17:31:38 +00:00
|
|
|
WAIT_FOR_BYTES(2);
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
|
|
|
WAIT_FOR_BYTES(9);
|
|
|
|
if(status_.data_request)
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
WAIT_FOR_BYTES(1);
|
|
|
|
if(is_double_density_)
|
|
|
|
{
|
|
|
|
WAIT_FOR_BYTES(11);
|
|
|
|
}
|
|
|
|
|
2016-12-29 00:26:21 +00:00
|
|
|
data_mode_ = DataMode::Writing;
|
2016-12-25 17:31:38 +00:00
|
|
|
begin_writing();
|
|
|
|
for(int c = 0; c < (is_double_density_ ? 12 : 6); c++)
|
|
|
|
{
|
2016-12-26 00:18:45 +00:00
|
|
|
write_byte(0);
|
2016-12-25 17:31:38 +00:00
|
|
|
}
|
2016-12-26 01:15:07 +00:00
|
|
|
WAIT_FOR_EVENT(Event::DataWritten);
|
|
|
|
|
|
|
|
if(is_double_density_)
|
|
|
|
{
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
2016-12-28 23:48:50 +00:00
|
|
|
for(int c = 0; c < 3; c++) write_raw_short(Storage::Encodings::MFM::MFMSync);
|
2016-12-31 20:25:11 +00:00
|
|
|
write_byte((command_&0x01) ? Storage::Encodings::MFM::DeletedDataAddressByte : Storage::Encodings::MFM::DataAddressByte);
|
2016-12-26 01:15:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.reset();
|
2016-12-31 20:25:11 +00:00
|
|
|
crc_generator_.add((command_&0x01) ? Storage::Encodings::MFM::DeletedDataAddressByte : Storage::Encodings::MFM::DataAddressByte);
|
2016-12-26 01:19:47 +00:00
|
|
|
write_raw_short((command_&0x01) ? Storage::Encodings::MFM::FMDeletedDataAddressMark : Storage::Encodings::MFM::FMDataAddressMark);
|
2016-12-26 01:15:07 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
WAIT_FOR_EVENT(Event::DataWritten);
|
|
|
|
distance_into_section_ = 0;
|
|
|
|
|
|
|
|
type2_write_loop:
|
2016-12-26 17:48:49 +00:00
|
|
|
/*
|
|
|
|
This deviates from the data sheet slightly since that would prima facie request one more byte
|
|
|
|
of data than is actually written — the last time around the loop it has transferred from the
|
|
|
|
data register to the data shift register, set data request, written the byte, checked that data
|
|
|
|
request has been satified, then finally considers whether all bytes are done. Based on both
|
|
|
|
natural expectations and the way that emulated machines responded, I believe that to be a
|
|
|
|
documentation error.
|
|
|
|
*/
|
|
|
|
write_byte(data_);
|
2016-12-25 20:46:49 +00:00
|
|
|
distance_into_section_++;
|
2016-12-29 02:24:19 +00:00
|
|
|
if(distance_into_section_ == 128 << header_[3])
|
2016-12-25 20:46:49 +00:00
|
|
|
{
|
2016-12-29 02:24:19 +00:00
|
|
|
goto type2_write_crc;
|
2016-12-25 20:46:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:24:19 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
|
|
|
WAIT_FOR_EVENT(Event::DataWritten);
|
2016-12-25 20:46:49 +00:00
|
|
|
if(status_.data_request)
|
|
|
|
{
|
2016-12-31 21:01:44 +00:00
|
|
|
end_writing();
|
2016-12-25 20:46:49 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:13:05 +00:00
|
|
|
goto type2_write_loop;
|
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
type2_write_crc:
|
2016-12-26 21:46:26 +00:00
|
|
|
{
|
|
|
|
uint16_t crc = crc_generator_.get_value();
|
|
|
|
write_byte(crc >> 8);
|
2016-12-29 02:24:19 +00:00
|
|
|
write_byte(crc & 0xff);
|
2016-12-26 21:46:26 +00:00
|
|
|
}
|
2016-12-26 00:18:45 +00:00
|
|
|
write_byte(0xff);
|
2016-12-25 20:46:49 +00:00
|
|
|
WAIT_FOR_EVENT(Event::DataWritten);
|
2016-12-25 17:31:38 +00:00
|
|
|
end_writing();
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-12-25 20:46:49 +00:00
|
|
|
if(command_ & 0x10)
|
|
|
|
{
|
|
|
|
sector_++;
|
|
|
|
goto test_type2_write_protection;
|
|
|
|
}
|
|
|
|
printf("Wrote sector %d\n", sector_);
|
|
|
|
goto wait_for_command;
|
|
|
|
|
2016-12-31 21:01:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Type 3 entry point.
|
|
|
|
*/
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! ! ! BITS !
|
|
|
|
// ! TYPE ! COMMAND ! 7 6 5 4 3 2 1 0 !
|
|
|
|
// +--------+----------+-------------------------+
|
|
|
|
// ! 3 ! Rd addr ! 1 1 0 0 h E 0 0 !
|
|
|
|
// ! 3 ! Rd track ! 1 1 1 0 h E 0 0 !
|
|
|
|
// ! 3 ! Wt track ! 1 1 1 1 h E P 0 !
|
|
|
|
// +--------+----------+-------------------------+
|
2016-09-25 00:12:45 +00:00
|
|
|
begin_type_3:
|
2016-12-01 12:41:52 +00:00
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.type = Status::Three;
|
2017-01-02 02:00:25 +00:00
|
|
|
status.crc_error = false;
|
|
|
|
status.lost_data = false;
|
|
|
|
status.record_not_found = false;
|
2016-12-01 12:41:52 +00:00
|
|
|
});
|
2017-01-02 01:39:19 +00:00
|
|
|
if(!has_motor_on_line() && !has_head_load_line()) goto type3_test_delay;
|
|
|
|
|
|
|
|
if(has_motor_on_line()) goto begin_type3_spin_up;
|
|
|
|
goto begin_type3_load_head;
|
|
|
|
|
|
|
|
begin_type3_load_head:
|
|
|
|
set_head_load_request(true);
|
|
|
|
if(head_is_loaded_) goto type3_test_delay;
|
|
|
|
WAIT_FOR_EVENT(Event::HeadLoad);
|
|
|
|
goto type3_test_delay;
|
|
|
|
|
|
|
|
begin_type3_spin_up:
|
|
|
|
if((command_&0x08) || get_motor_on()) goto type3_test_delay;
|
|
|
|
SPIN_UP();
|
|
|
|
|
|
|
|
type3_test_delay:
|
|
|
|
if(!(command_&0x04)) goto test_type3_type;
|
|
|
|
WAIT_FOR_TIME(30);
|
|
|
|
|
|
|
|
test_type3_type:
|
2017-01-02 01:56:52 +00:00
|
|
|
if(!(command_&0x20)) goto begin_read_address;
|
|
|
|
if(!(command_&0x10)) goto begin_read_track;
|
|
|
|
goto begin_write_track;
|
2016-12-31 21:01:44 +00:00
|
|
|
|
|
|
|
begin_read_address:
|
2017-01-02 01:55:09 +00:00
|
|
|
index_hole_count_ = 0;
|
|
|
|
distance_into_section_ = 0;
|
|
|
|
|
|
|
|
read_address_get_header:
|
|
|
|
WAIT_FOR_EVENT(Event::IndexHole | Event::Token);
|
|
|
|
if(new_event_type == Event::Token)
|
|
|
|
{
|
|
|
|
if(!distance_into_section_ && latest_token_.type == Token::ID) {data_mode_ = DataMode::Reading; distance_into_section_++; }
|
|
|
|
else if(distance_into_section_ && distance_into_section_ < 7 && latest_token_.type == Token::Byte)
|
|
|
|
{
|
|
|
|
if(status_.data_request)
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
header_[distance_into_section_ - 1] = data_ = latest_token_.byte_value;
|
|
|
|
track_ = header_[0];
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
|
|
|
distance_into_section_++;
|
|
|
|
|
|
|
|
if(distance_into_section_ == 7)
|
|
|
|
{
|
2017-01-02 02:00:25 +00:00
|
|
|
if(crc_generator_.get_value())
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.crc_error = true;
|
|
|
|
});
|
|
|
|
}
|
2017-01-02 01:55:09 +00:00
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(index_hole_count_ == 6)
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.record_not_found = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
goto read_address_get_header;
|
2016-12-31 21:01:44 +00:00
|
|
|
|
|
|
|
begin_read_track:
|
2017-01-02 01:39:19 +00:00
|
|
|
WAIT_FOR_EVENT(Event::IndexHole);
|
|
|
|
index_hole_count_ = 0;
|
|
|
|
|
|
|
|
read_track_read_byte:
|
|
|
|
WAIT_FOR_EVENT(Event::Token | Event::IndexHole);
|
|
|
|
if(index_hole_count_)
|
|
|
|
{
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
if(status_.data_request)
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
data_ = latest_token_.byte_value;
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
|
|
|
goto read_track_read_byte;
|
2016-12-31 21:01:44 +00:00
|
|
|
|
|
|
|
begin_write_track:
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = false;
|
|
|
|
status.lost_data = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
write_track_test_write_protect:
|
|
|
|
if(get_drive_is_read_only())
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.write_protect = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
|
|
|
WAIT_FOR_BYTES(3);
|
|
|
|
if(status_.data_request)
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data = true;
|
|
|
|
});
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
|
|
|
|
WAIT_FOR_EVENT(Event::IndexHoleTarget);
|
|
|
|
begin_writing();
|
|
|
|
index_hole_count_ = 0;
|
|
|
|
|
|
|
|
write_track_write_loop:
|
|
|
|
if(is_double_density_)
|
|
|
|
{
|
|
|
|
switch(data_)
|
|
|
|
{
|
|
|
|
case 0xf5:
|
|
|
|
write_raw_short(Storage::Encodings::MFM::MFMSync);
|
|
|
|
crc_generator_.set_value(Storage::Encodings::MFM::MFMPostSyncCRCValue);
|
|
|
|
break;
|
|
|
|
case 0xf6:
|
|
|
|
write_raw_short(Storage::Encodings::MFM::MFMIndexSync);
|
|
|
|
break;
|
|
|
|
case 0xff: {
|
|
|
|
uint16_t crc = crc_generator_.get_value();
|
|
|
|
write_byte(crc >> 8);
|
|
|
|
write_byte(crc & 0xff);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
write_byte(data_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch(data_)
|
|
|
|
{
|
|
|
|
case 0xf8: case 0xf9: case 0xfa: case 0xfb:
|
|
|
|
case 0xfd: case 0xfe:
|
|
|
|
// clock is 0xc7 = 1010 0000 0010 1010 = 0xa022
|
|
|
|
write_raw_short(
|
|
|
|
(uint16_t)(
|
|
|
|
0xa022 |
|
|
|
|
((data_ & 0x80) << 7) |
|
|
|
|
((data_ & 0x40) << 6) |
|
|
|
|
((data_ & 0x20) << 5) |
|
|
|
|
((data_ & 0x10) << 4) |
|
|
|
|
((data_ & 0x08) << 3) |
|
|
|
|
((data_ & 0x04) << 2) |
|
|
|
|
((data_ & 0x02) << 1) |
|
|
|
|
(data_ & 0x01)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
crc_generator_.reset();
|
|
|
|
crc_generator_.add(data_);
|
|
|
|
break;
|
|
|
|
case 0xfc:
|
|
|
|
write_raw_short(Storage::Encodings::MFM::FMIndexAddressMark);
|
|
|
|
break;
|
|
|
|
case 0xf7: {
|
|
|
|
uint16_t crc = crc_generator_.get_value();
|
|
|
|
write_byte(crc >> 8);
|
|
|
|
write_byte(crc & 0xff);
|
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
write_byte(data_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.data_request = true;
|
|
|
|
});
|
|
|
|
WAIT_FOR_EVENT(Event::DataWritten);
|
|
|
|
if(status_.data_request)
|
|
|
|
{
|
|
|
|
update_status([] (Status &status) {
|
|
|
|
status.lost_data = true;
|
|
|
|
});
|
|
|
|
end_writing();
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
|
|
|
if(index_hole_count_)
|
|
|
|
{
|
|
|
|
end_writing();
|
|
|
|
goto wait_for_command;
|
|
|
|
}
|
2016-09-25 00:12:45 +00:00
|
|
|
|
2016-12-31 21:01:44 +00:00
|
|
|
goto write_track_write_loop;
|
2016-09-25 00:12:45 +00:00
|
|
|
|
|
|
|
END_SECTION()
|
2016-09-21 02:14:33 +00:00
|
|
|
}
|
2016-11-21 05:21:49 +00:00
|
|
|
|
2016-12-01 12:41:52 +00:00
|
|
|
void WD1770::update_status(std::function<void(Status &)> updater)
|
2016-11-21 05:21:49 +00:00
|
|
|
{
|
2016-12-01 12:41:52 +00:00
|
|
|
if(delegate_)
|
2016-11-21 05:21:49 +00:00
|
|
|
{
|
2016-12-01 12:41:52 +00:00
|
|
|
Status old_status = status_;
|
|
|
|
updater(status_);
|
|
|
|
bool did_change =
|
|
|
|
(status_.busy != old_status.busy) ||
|
|
|
|
(status_.data_request != old_status.data_request);
|
|
|
|
if(did_change) delegate_->wd1770_did_change_output(this);
|
2016-11-21 05:21:49 +00:00
|
|
|
}
|
2016-12-01 12:41:52 +00:00
|
|
|
else updater(status_);
|
2016-11-21 05:21:49 +00:00
|
|
|
}
|
2016-12-02 01:12:22 +00:00
|
|
|
|
|
|
|
void WD1770::set_head_load_request(bool head_load) {}
|
|
|
|
|
|
|
|
void WD1770::set_head_loaded(bool head_loaded)
|
|
|
|
{
|
|
|
|
head_is_loaded_ = head_loaded;
|
2016-12-02 02:13:16 +00:00
|
|
|
if(head_loaded) posit_event(Event::HeadLoad);
|
2016-12-02 01:12:22 +00:00
|
|
|
}
|
2016-12-26 00:18:45 +00:00
|
|
|
|
|
|
|
void WD1770::write_bit(int bit)
|
|
|
|
{
|
|
|
|
if(is_double_density_)
|
|
|
|
{
|
|
|
|
Controller::write_bit(!bit && !last_bit_);
|
|
|
|
Controller::write_bit(!!bit);
|
|
|
|
last_bit_ = bit;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Controller::write_bit(true);
|
|
|
|
Controller::write_bit(!!bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WD1770::write_byte(uint8_t byte)
|
|
|
|
{
|
|
|
|
for(int c = 0; c < 8; c++) write_bit((byte << c)&0x80);
|
2016-12-28 23:29:37 +00:00
|
|
|
crc_generator_.add(byte);
|
2016-12-26 00:18:45 +00:00
|
|
|
}
|
2016-12-26 01:15:07 +00:00
|
|
|
|
|
|
|
void WD1770::write_raw_short(uint16_t value)
|
|
|
|
{
|
|
|
|
for(int c = 0; c < 16; c++)
|
|
|
|
{
|
|
|
|
Controller::write_bit(!!((value << c)&0x8000));
|
|
|
|
}
|
|
|
|
}
|