1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-25 18:30:07 +00:00

Merge pull request #1217 from TomHarte/PCFDC

Sketch the outline of a high-level emulation of the PC FDC
This commit is contained in:
Thomas Harte 2023-11-29 12:48:55 -05:00 committed by GitHub
commit 66b95a8b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 923 additions and 266 deletions

View File

@ -0,0 +1,221 @@
//
// CommandDecoder.hpp
// Clock Signal
//
// Created by Thomas Harte on 24/11/2023.
// Copyright © 2023 Thomas Harte. All rights reserved.
//
#ifndef CommandDecoder_hpp
#define CommandDecoder_hpp
#include <cstdint>
#include <cstddef>
#include <vector>
namespace Intel::i8272 {
enum class Command {
ReadData = 0x06,
ReadDeletedData = 0x0c,
WriteData = 0x05,
WriteDeletedData = 0x09,
ReadTrack = 0x02,
ReadID = 0x0a,
FormatTrack = 0x0d,
ScanLow = 0x11,
ScanLowOrEqual = 0x19,
ScanHighOrEqual = 0x1d,
Recalibrate = 0x07,
Seek = 0x0f,
SenseInterruptStatus = 0x08,
Specify = 0x03,
SenseDriveStatus = 0x04,
Invalid = 0x00,
};
class CommandDecoder {
public:
/// Add a byte to the current command.
void push_back(uint8_t byte) {
command_.push_back(byte);
}
/// Reset decoding.
void clear() {
command_.clear();
}
/// @returns @c true if an entire command has been received; @c false if further bytes are needed.
bool has_command() const {
if(!command_.size()) {
return false;
}
static constexpr std::size_t required_lengths[32] = {
0, 0, 9, 3, 2, 9, 9, 2,
1, 9, 2, 0, 9, 6, 0, 3,
0, 9, 0, 0, 0, 0, 0, 0,
0, 9, 0, 0, 0, 9, 0, 0,
};
return command_.size() >= required_lengths[command_[0] & 0x1f];
}
/// @returns The command requested. Valid only if @c has_command() is @c true.
Command command() const {
const auto command = Command(command_[0] & 0x1f);
switch(command) {
case Command::ReadData: case Command::ReadDeletedData:
case Command::WriteData: case Command::WriteDeletedData:
case Command::ReadTrack: case Command::ReadID:
case Command::FormatTrack:
case Command::ScanLow: case Command::ScanLowOrEqual:
case Command::ScanHighOrEqual:
case Command::Recalibrate: case Command::Seek:
case Command::SenseInterruptStatus:
case Command::Specify: case Command::SenseDriveStatus:
return command;
default: return Command::Invalid;
}
}
//
// Commands that specify geometry; i.e.
//
// * ReadData;
// * ReadDeletedData;
// * WriteData;
// * WriteDeletedData;
// * ReadTrack;
// * ScanEqual;
// * ScanLowOrEqual;
// * ScanHighOrEqual.
//
/// @returns @c true if this command specifies geometry, in which case geomtry() is well-defined.
/// @c false otherwise.
bool has_geometry() const { return command_.size() == 9; }
struct Geometry {
uint8_t cylinder, head, sector, size, end_of_track;
};
Geometry geometry() const {
Geometry result;
result.cylinder = command_[2];
result.head = command_[3];
result.sector = command_[4];
result.size = command_[5];
result.end_of_track = command_[6];
return result;
}
//
// Commands that imply data access; i.e.
//
// * ReadData;
// * ReadDeletedData;
// * WriteData;
// * WriteDeletedData;
// * ReadTrack;
// * ReadID;
// * FormatTrack;
// * ScanLow;
// * ScanLowOrEqual;
// * ScanHighOrEqual.
//
/// @returns @c true if this command involves reading or writing data, in which case target() will be valid.
/// @c false otherwise.
bool is_access() const {
switch(command()) {
case Command::ReadData: case Command::ReadDeletedData:
case Command::WriteData: case Command::WriteDeletedData:
case Command::ReadTrack: case Command::ReadID:
case Command::FormatTrack:
case Command::ScanLow: case Command::ScanLowOrEqual:
case Command::ScanHighOrEqual:
return true;
default:
return false;
}
}
struct AccessTarget {
uint8_t drive, head;
bool mfm, skip_deleted;
};
AccessTarget target() const {
AccessTarget result;
result.drive = command_[1] & 0x03;
result.head = (command_[1] >> 2) & 0x01;
result.mfm = command_[0] & 0x40;
result.skip_deleted = command_[0] & 0x20;
return result;
}
uint8_t drive_head() const {
return command_[1] & 7;
}
//
// Command::FormatTrack
//
struct FormatSpecs {
uint8_t bytes_per_sector;
uint8_t sectors_per_track;
uint8_t gap3_length;
uint8_t filler;
};
FormatSpecs format_specs() const {
FormatSpecs result;
result.bytes_per_sector = command_[2];
result.sectors_per_track = command_[3];
result.gap3_length = command_[4];
result.filler = command_[5];
return result;
}
//
// Command::Seek
//
/// @returns The desired target track.
uint8_t seek_target() const {
return command_[2];
}
//
// Command::Specify
//
struct SpecifySpecs {
// The below are all in milliseconds.
uint8_t step_rate_time;
uint8_t head_unload_time;
uint8_t head_load_time;
bool use_dma;
};
SpecifySpecs specify_specs() const {
SpecifySpecs result;
result.step_rate_time = 16 - (command_[1] >> 4); // i.e. 1 to 16ms
result.head_unload_time = uint8_t((command_[1] & 0x0f) << 4); // i.e. 16 to 240ms
result.head_load_time = command_[2] & ~1; // i.e. 2 to 254 ms in increments of 2ms
result.use_dma = !(command_[2] & 1);
return result;
}
private:
std::vector<uint8_t> command_;
};
}
#endif /* CommandDecoder_hpp */

View File

@ -0,0 +1,65 @@
//
// Results.hpp
// Clock Signal
//
// Created by Thomas Harte on 27/11/2023.
// Copyright © 2023 Thomas Harte. All rights reserved.
//
#ifndef Results_hpp
#define Results_hpp
#include "CommandDecoder.hpp"
#include "Status.hpp"
namespace Intel::i8272 {
class Results {
public:
/// Serialises the response to Command::Invalid and Command::SenseInterruptStatus when no interrupt source was found.
void serialise_none() {
result_ = { 0x80 };
}
/// Serialises the response to Command::SenseInterruptStatus for a found drive.
void serialise(const Status &status, uint8_t cylinder) {
result_ = { cylinder, status[0] };
}
/// Serialises the seven-byte response to Command::SenseDriveStatus.
void serialise(uint8_t flags, uint8_t drive_side) {
result_ = { uint8_t(flags | drive_side) };
}
/// Serialises the response to:
///
/// * Command::ReadData;
/// * Command::ReadDeletedData;
/// * Command::WriteData;
/// * Command::WriteDeletedData;
/// * Command::ReadID;
/// * Command::ReadTrack;
/// * Command::FormatTrack;
/// * Command::ScanLow; and
/// * Command::ScanHighOrEqual.
void serialise(const Status &status, uint8_t cylinder, uint8_t head, uint8_t sector, uint8_t size) {
result_ = { size, sector, head, cylinder, status[2], status[1], status[0] };
}
/// @returns @c true if all result bytes are exhausted; @c false otherwise.
bool empty() const { return result_.empty(); }
/// @returns The next byte of the result.
uint8_t next() {
const uint8_t next = result_.back();
result_.pop_back();
return next;
}
private:
std::vector<uint8_t> result_;
};
}
#endif /* Results_hpp */

134
Components/8272/Status.hpp Normal file
View File

@ -0,0 +1,134 @@
//
// Status.hpp
// Clock Signal
//
// Created by Thomas Harte on 25/11/2023.
// Copyright © 2023 Thomas Harte. All rights reserved.
//
#ifndef Status_hpp
#define Status_hpp
namespace Intel::i8272 {
enum class MainStatus: uint8_t {
FDD0Seeking = 0x01,
FDD1Seeking = 0x02,
FDD2Seeking = 0x04,
FDD3Seeking = 0x08,
CommandInProgress = 0x10,
InNonDMAExecution = 0x20,
DataIsToProcessor = 0x40,
DataReady = 0x80,
};
enum class Status0: uint8_t {
NormalTermination = 0x00,
AbnormalTermination = 0x80,
InvalidCommand = 0x40,
BecameNotReady = 0xc0,
SeekEnded = 0x20,
EquipmentFault = 0x10,
NotReady = 0x08,
HeadAddress = 0x04,
UnitSelect = 0x03,
};
enum class Status1: uint8_t {
EndOfCylinder = 0x80,
DataError = 0x20,
OverRun = 0x10,
NoData = 0x04,
NotWriteable = 0x02,
MissingAddressMark = 0x01,
};
enum class Status2: uint8_t {
DeletedControlMark = 0x40,
DataCRCError = 0x20,
WrongCyinder = 0x10,
ScanEqualHit = 0x08,
ScanNotSatisfied = 0x04,
BadCylinder = 0x02,
MissingDataAddressMark = 0x01,
};
enum class Status3: uint8_t {
Fault = 0x80,
WriteProtected = 0x40,
Ready = 0x20,
Track0 = 0x10,
TwoSided = 0x08,
HeadAddress = 0x04,
UnitSelect = 0x03,
};
class Status {
public:
Status() {
reset();
}
void reset() {
main_status_ = 0;
set(MainStatus::DataReady, true);
status_[0] = status_[1] = status_[2] = 0;
}
/// @returns The main status register value.
uint8_t main() const {
return main_status_;
}
uint8_t operator [](int index) const {
return status_[index];
}
//
// Flag setters.
//
void set(MainStatus flag, bool value) {
set(uint8_t(flag), value, main_status_);
}
void start_seek(int drive) { main_status_ |= 1 << drive; }
void set(Status0 flag) { set(uint8_t(flag), true, status_[0]); }
void set(Status1 flag) { set(uint8_t(flag), true, status_[1]); }
void set(Status2 flag) { set(uint8_t(flag), true, status_[2]); }
void set_status0(uint8_t value) { status_[0] = value; }
//
// Flag getters.
//
bool get(MainStatus flag) { return main_status_ & uint8_t(flag); }
bool get(Status2 flag) { return status_[2] & uint8_t(flag); }
/// Begin execution of whatever @c CommandDecoder currently describes, setting internal
/// state appropriately.
void begin(const CommandDecoder &command) {
set(MainStatus::DataReady, false);
set(MainStatus::CommandInProgress, true);
if(command.is_access()) {
status_[0] = command.drive_head();
}
}
private:
void set(uint8_t flag, bool value, uint8_t &target) {
if(value) {
target |= flag;
} else {
target &= ~flag;
}
}
uint8_t main_status_;
uint8_t status_[3];
};
}
#endif /* Status_hpp */

View File

@ -12,70 +12,6 @@
using namespace Intel::i8272;
#define SetDataRequest() (main_status_ |= 0x80)
#define ResetDataRequest() (main_status_ &= ~0x80)
#define DataRequest() (main_status_ & 0x80)
#define SetDataDirectionToProcessor() (main_status_ |= 0x40)
#define SetDataDirectionFromProcessor() (main_status_ &= ~0x40)
#define DataDirectionToProcessor() (main_status_ & 0x40)
#define SetNonDMAExecution() (main_status_ |= 0x20)
#define ResetNonDMAExecution() (main_status_ &= ~0x20)
#define SetBusy() (main_status_ |= 0x10)
#define ResetBusy() (main_status_ &= ~0x10)
#define Busy() (main_status_ & 0x10)
#define SetAbnormalTermination() (status_[0] |= 0x40)
#define SetInvalidCommand() (status_[0] |= 0x80)
#define SetReadyChanged() (status_[0] |= 0xc0)
#define SetSeekEnd() (status_[0] |= 0x20)
#define SetEquipmentCheck() (status_[0] |= 0x10)
#define SetNotReady() (status_[0] |= 0x08)
#define SetSide2() (status_[0] |= 0x04)
#define SetEndOfCylinder() (status_[1] |= 0x80)
#define SetDataError() (status_[1] |= 0x20)
#define SetOverrun() (status_[1] |= 0x10)
#define SetNoData() (status_[1] |= 0x04)
#define SetNotWriteable() (status_[1] |= 0x02)
#define SetMissingAddressMark() (status_[1] |= 0x01)
#define SetControlMark() (status_[2] |= 0x40)
#define ClearControlMark() (status_[2] &= ~0x40)
#define ControlMark() (status_[2] & 0x40)
#define SetDataFieldDataError() (status_[2] |= 0x20)
#define SetWrongCyinder() (status_[2] |= 0x10)
#define SetScanEqualHit() (status_[2] |= 0x08)
#define SetScanNotSatisfied() (status_[2] |= 0x04)
#define SetBadCylinder() (status_[2] |= 0x02)
#define SetMissingDataAddressMark() (status_[2] |= 0x01)
namespace {
const uint8_t CommandReadData = 0x06;
const uint8_t CommandReadDeletedData = 0x0c;
const uint8_t CommandWriteData = 0x05;
const uint8_t CommandWriteDeletedData = 0x09;
const uint8_t CommandReadTrack = 0x02;
const uint8_t CommandReadID = 0x0a;
const uint8_t CommandFormatTrack = 0x0d;
const uint8_t CommandScanLow = 0x11;
const uint8_t CommandScanLowOrEqual = 0x19;
const uint8_t CommandScanHighOrEqual = 0x1d;
const uint8_t CommandRecalibrate = 0x07;
const uint8_t CommandSeek = 0x0f;
const uint8_t CommandSenseInterruptStatus = 0x08;
const uint8_t CommandSpecify = 0x03;
const uint8_t CommandSenseDriveStatus = 0x04;
}
i8272::i8272(BusHandler &bus_handler, Cycles clock_rate) :
Storage::Disk::MFMController(clock_rate),
bus_handler_(bus_handler) {
@ -172,12 +108,12 @@ void i8272::write(int address, uint8_t value) {
if(!address) return;
// if not ready for commands, do nothing
if(!DataRequest() || DataDirectionToProcessor()) return;
if(!status_.get(MainStatus::DataReady) || status_.get(MainStatus::DataIsToProcessor)) return;
if(expects_input_) {
input_ = value;
has_input_ = true;
ResetDataRequest();
status_.set(MainStatus::DataReady, false);
} else {
// accumulate latest byte in the command byte sequence
command_.push_back(value);
@ -194,7 +130,7 @@ uint8_t i8272::read(int address) {
return result;
} else {
return main_status_;
return status_.main();
}
}
@ -233,12 +169,11 @@ uint8_t i8272::read(int address) {
if(distance_into_section_ < 6) goto CONCAT(read_header, __LINE__); \
#define SET_DRIVE_HEAD_MFM() \
active_drive_ = command_[1]&3; \
active_head_ = (command_[1] >> 2)&1; \
status_[0] = (command_[1]&7); \
active_drive_ = command_.target().drive; \
active_head_ = command_.target().head; \
select_drive(active_drive_); \
get_drive().set_head(active_head_); \
set_is_double_density(command_[0] & 0x40);
set_is_double_density(command_.target().mfm);
#define WAIT_FOR_BYTES(n) \
distance_into_section_ = 0; \
@ -270,7 +205,7 @@ uint8_t i8272::read(int address) {
void i8272::posit_event(int event_type) {
if(event_type == int(Event::IndexHole)) index_hole_count_++;
if(event_type == int(Event8272::NoLongerReady)) {
SetNotReady();
status_.set(Status0::NotReady);
goto abort;
}
if(!(interesting_event_mask_ & event_type)) return;
@ -283,55 +218,32 @@ void i8272::posit_event(int event_type) {
wait_for_command:
expects_input_ = false;
set_data_mode(Storage::Disk::MFMController::DataMode::Scanning);
ResetBusy();
ResetNonDMAExecution();
status_.set(MainStatus::CommandInProgress, false);
status_.set(MainStatus::InNonDMAExecution, false);
command_.clear();
// Sets the data request bit, and waits for a byte. Then sets the busy bit. Continues accepting bytes
// until it has a quantity that make up an entire command, then resets the data request bit and
// branches to that command.
wait_for_complete_command_sequence:
SetDataRequest();
SetDataDirectionFromProcessor();
status_.set(MainStatus::DataReady, true);
status_.set(MainStatus::DataIsToProcessor, false);
WAIT_FOR_EVENT(Event8272::CommandByte)
SetBusy();
static constexpr std::size_t required_lengths[32] = {
0, 0, 9, 3, 2, 9, 9, 2,
1, 9, 2, 0, 9, 6, 0, 3,
0, 9, 0, 0, 0, 0, 0, 0,
0, 9, 0, 0, 0, 9, 0, 0,
};
if(command_.size() < required_lengths[command_[0] & 0x1f]) goto wait_for_complete_command_sequence;
if(command_.size() == 9) {
cylinder_ = command_[2];
head_ = command_[3];
sector_ = command_[4];
size_ = command_[5];
if(!command_.has_command()) {
goto wait_for_complete_command_sequence;
}
status_.begin(command_);
if(command_.has_geometry()) {
cylinder_ = command_.geometry().cylinder;
head_ = command_.geometry().head;
sector_ = command_.geometry().sector;
size_ = command_.geometry().size;
}
ResetDataRequest();
status_[0] = status_[1] = status_[2] = 0;
// If this is not clearly a command that's safe to carry out in parallel to a seek, end all seeks.
switch(command_[0] & 0x1f) {
case CommandReadData:
case CommandReadDeletedData:
case CommandWriteData:
case CommandWriteDeletedData:
case CommandReadTrack:
case CommandReadID:
case CommandFormatTrack:
case CommandScanLow:
case CommandScanLowOrEqual:
case CommandScanHighOrEqual:
is_access_command_ = true;
break;
default:
is_access_command_ = false;
break;
}
is_access_command_ = command_.is_access();
if(is_access_command_) {
for(int c = 0; c < 4; c++) {
@ -343,39 +255,41 @@ void i8272::posit_event(int event_type) {
// Establishes the drive and head being addressed, and whether in double density mode; populates the internal
// cylinder, head, sector and size registers from the command stream.
is_executing_ = true;
if(!dma_mode_) SetNonDMAExecution();
if(!dma_mode_) {
status_.set(MainStatus::InNonDMAExecution, true);
}
SET_DRIVE_HEAD_MFM();
LOAD_HEAD();
if(!get_drive().get_is_ready()) {
SetNotReady();
status_.set(Status0::NotReady);
goto abort;
}
}
// Jump to the proper place.
switch(command_[0] & 0x1f) {
case CommandReadData:
case CommandReadDeletedData:
switch(command_.command()) {
case Command::ReadData:
case Command::ReadDeletedData:
goto read_data;
case CommandWriteData:
case CommandWriteDeletedData:
case Command::WriteData:
case Command::WriteDeletedData:
goto write_data;
case CommandReadTrack: goto read_track;
case CommandReadID: goto read_id;
case CommandFormatTrack: goto format_track;
case Command::ReadTrack: goto read_track;
case Command::ReadID: goto read_id;
case Command::FormatTrack: goto format_track;
case CommandScanLow: goto scan_low;
case CommandScanLowOrEqual: goto scan_low_or_equal;
case CommandScanHighOrEqual: goto scan_high_or_equal;
case Command::ScanLow: goto scan_low;
case Command::ScanLowOrEqual: goto scan_low_or_equal;
case Command::ScanHighOrEqual: goto scan_high_or_equal;
case CommandRecalibrate: goto recalibrate;
case CommandSeek: goto seek;
case Command::Recalibrate: goto recalibrate;
case Command::Seek: goto seek;
case CommandSenseInterruptStatus: goto sense_interrupt_status;
case CommandSpecify: goto specify;
case CommandSenseDriveStatus: goto sense_drive_status;
case Command::SenseInterruptStatus: goto sense_interrupt_status;
case Command::Specify: goto specify;
case Command::SenseDriveStatus: goto sense_drive_status;
default: goto invalid;
}
@ -395,7 +309,7 @@ void i8272::posit_event(int event_type) {
if(!index_hole_limit_) {
// Two index holes have passed wihout finding the header sought.
// LOG("Not found");
SetNoData();
status_.set(Status1::NoData);
goto abort;
}
index_hole_count_ = 0;
@ -403,38 +317,39 @@ void i8272::posit_event(int event_type) {
READ_HEADER();
if(index_hole_count_) {
// This implies an index hole was sighted within the header. Error out.
SetEndOfCylinder();
status_.set(Status1::EndOfCylinder);
goto abort;
}
if(get_crc_generator().get_value()) {
// This implies a CRC error in the header; mark as such but continue.
SetDataError();
status_.set(Status1::DataError);
}
// LOG("Considering << PADHEX(2) << header_[0] << " " << header_[1] << " " << header_[2] << " " << header_[3] << " [" << get_crc_generator().get_value() << "]");
if(header_[0] != cylinder_ || header_[1] != head_ || header_[2] != sector_ || header_[3] != size_) goto find_next_sector;
// Branch to whatever is supposed to happen next
// LOG("Proceeding");
switch(command_[0] & 0x1f) {
case CommandReadData:
case CommandReadDeletedData:
switch(command_.command()) {
default:
case Command::ReadData:
case Command::ReadDeletedData:
goto read_data_found_header;
case CommandWriteData: // write data
case CommandWriteDeletedData: // write deleted data
case Command::WriteData: // write data
case Command::WriteDeletedData: // write deleted data
goto write_data_found_header;
}
// Performs the read data or read deleted data command.
read_data:
LOG(PADHEX(2) << "Read [deleted] data ["
<< int(command_[2]) << " "
<< int(command_[3]) << " "
<< int(command_[4]) << " "
<< int(command_[5]) << " ... "
<< int(command_[6]) << " "
<< int(command_[8]) << "]");
// LOG(PADHEX(2) << "Read [deleted] data ["
// << int(command_[2]) << " "
// << int(command_[3]) << " "
// << int(command_[4]) << " "
// << int(command_[5]) << " ... "
// << int(command_[6]) << " "
// << int(command_[8]) << "]");
read_next_data:
goto read_write_find_header;
@ -442,18 +357,18 @@ void i8272::posit_event(int event_type) {
// flag doesn't match the sort the command was looking for.
read_data_found_header:
FIND_DATA();
ClearControlMark();
// TODO: should Status2::DeletedControlMark be cleared?
if(event_type == int(Event::Token)) {
if(get_latest_token().type != Token::Data && get_latest_token().type != Token::DeletedData) {
// Something other than a data mark came next, impliedly an ID or index mark.
SetMissingAddressMark();
SetMissingDataAddressMark();
status_.set(Status1::MissingAddressMark);
status_.set(Status2::MissingDataAddressMark);
goto abort; // TODO: or read_next_data?
} else {
if((get_latest_token().type == Token::Data) != ((command_[0] & 0x1f) == CommandReadData)) {
if(!(command_[0]&0x20)) {
if((get_latest_token().type == Token::Data) != (command_.command() == Command::ReadData)) {
if(!command_.target().skip_deleted) {
// SK is not set; set the error flag but read this sector before finishing.
SetControlMark();
status_.set(Status2::DeletedControlMark);
} else {
// SK is set; skip this sector.
goto read_next_data;
@ -462,7 +377,7 @@ void i8272::posit_event(int event_type) {
}
} else {
// An index hole appeared before the data mark.
SetEndOfCylinder();
status_.set(Status1::EndOfCylinder);
goto abort; // TODO: or read_next_data?
}
@ -478,21 +393,21 @@ void i8272::posit_event(int event_type) {
if(event_type == int(Event::Token)) {
result_stack_.push_back(get_latest_token().byte_value);
distance_into_section_++;
SetDataRequest();
SetDataDirectionToProcessor();
status_.set(MainStatus::DataReady, true);
status_.set(MainStatus::DataIsToProcessor, true);
WAIT_FOR_EVENT(int(Event8272::ResultEmpty) | int(Event::Token) | int(Event::IndexHole));
}
switch(event_type) {
case int(Event8272::ResultEmpty): // The caller read the byte in time; proceed as normal.
ResetDataRequest();
status_.set(MainStatus::DataReady, false);
if(distance_into_section_ < (128 << size_)) goto read_data_get_byte;
break;
case int(Event::Token): // The caller hasn't read the old byte yet and a new one has arrived
SetOverrun();
status_.set(Status1::OverRun);
goto abort;
break;
case int(Event::IndexHole):
SetEndOfCylinder();
status_.set(Status1::EndOfCylinder);
goto abort;
break;
}
@ -502,14 +417,14 @@ void i8272::posit_event(int event_type) {
WAIT_FOR_EVENT(Event::Token);
if(get_crc_generator().get_value()) {
// This implies a CRC error in the sector; mark as such and temrinate.
SetDataError();
SetDataFieldDataError();
status_.set(Status1::DataError);
status_.set(Status2::DataCRCError);
goto abort;
}
// check whether that's it: either the final requested sector has been read, or because
// a sector that was [/wasn't] marked as deleted when it shouldn't [/should] have been
if(sector_ != command_[6] && !ControlMark()) {
if(sector_ != command_.geometry().end_of_track && !status_.get(Status2::DeletedControlMark)) {
sector_++;
goto read_next_data;
}
@ -518,16 +433,16 @@ void i8272::posit_event(int event_type) {
goto post_st012chrn;
write_data:
LOG(PADHEX(2) << "Write [deleted] data ["
<< int(command_[2]) << " "
<< int(command_[3]) << " "
<< int(command_[4]) << " "
<< int(command_[5]) << " ... "
<< int(command_[6]) << " "
<< int(command_[8]) << "]");
// LOG(PADHEX(2) << "Write [deleted] data ["
// << int(command_[2]) << " "
// << int(command_[3]) << " "
// << int(command_[4]) << " "
// << int(command_[5]) << " ... "
// << int(command_[6]) << " "
// << int(command_[8]) << "]");
if(get_drive().get_is_read_only()) {
SetNotWriteable();
status_.set(Status1::NotWriteable);
goto abort;
}
@ -538,24 +453,24 @@ void i8272::posit_event(int event_type) {
WAIT_FOR_BYTES(get_is_double_density() ? 22 : 11);
begin_writing(true);
write_id_data_joiner((command_[0] & 0x1f) == CommandWriteDeletedData, true);
write_id_data_joiner(command_.command() == Command::WriteDeletedData, true);
SetDataDirectionFromProcessor();
SetDataRequest();
status_.set(MainStatus::DataIsToProcessor, false);
status_.set(MainStatus::DataReady, true);
expects_input_ = true;
distance_into_section_ = 0;
write_loop:
WAIT_FOR_EVENT(Event::DataWritten);
if(!has_input_) {
SetOverrun();
status_.set(Status1::OverRun);
goto abort;
}
write_byte(input_);
has_input_ = false;
distance_into_section_++;
if(distance_into_section_ < (128 << size_)) {
SetDataRequest();
status_.set(MainStatus::DataReady, true);
goto write_loop;
}
@ -565,7 +480,7 @@ void i8272::posit_event(int event_type) {
WAIT_FOR_EVENT(Event::DataWritten);
end_writing();
if(sector_ != command_[6]) {
if(sector_ != command_.geometry().end_of_track) {
sector_++;
goto write_next_data;
}
@ -575,14 +490,14 @@ void i8272::posit_event(int event_type) {
// Performs the read ID command.
read_id:
// Establishes the drive and head being addressed, and whether in double density mode.
LOG(PADHEX(2) << "Read ID [" << int(command_[0]) << " " << int(command_[1]) << "]");
// LOG(PADHEX(2) << "Read ID [" << int(command_[0]) << " " << int(command_[1]) << "]");
// Sets a maximum index hole limit of 2 then waits either until it finds a header mark or sees too many index holes.
// If a header mark is found, reads in the following bytes that produce a header. Otherwise branches to data not found.
index_hole_limit_ = 2;
FIND_HEADER();
if(!index_hole_limit_) {
SetMissingAddressMark();
status_.set(Status1::MissingAddressMark);
goto abort;
}
READ_HEADER();
@ -597,11 +512,11 @@ void i8272::posit_event(int event_type) {
// Performs read track.
read_track:
LOG(PADHEX(2) << "Read track ["
<< int(command_[2]) << " "
<< int(command_[3]) << " "
<< int(command_[4]) << " "
<< int(command_[5]) << "]");
// LOG(PADHEX(2) << "Read track ["
// << int(command_[2]) << " "
// << int(command_[3]) << " "
// << int(command_[4]) << " "
// << int(command_[5]) << "]");
// Wait for the index hole.
WAIT_FOR_EVENT(Event::IndexHole);
@ -614,7 +529,7 @@ void i8272::posit_event(int event_type) {
FIND_HEADER();
if(!index_hole_limit_) {
if(!sector_) {
SetMissingAddressMark();
status_.set(Status1::MissingAddressMark);
goto abort;
} else {
goto post_st012chrn;
@ -624,19 +539,19 @@ void i8272::posit_event(int event_type) {
FIND_DATA();
distance_into_section_ = 0;
SetDataDirectionToProcessor();
status_.set(MainStatus::DataIsToProcessor, true);
read_track_get_byte:
WAIT_FOR_EVENT(Event::Token);
result_stack_.push_back(get_latest_token().byte_value);
distance_into_section_++;
SetDataRequest();
status_.set(MainStatus::DataReady, true);
// TODO: other possible exit conditions; find a way to merge with the read_data version of this.
WAIT_FOR_EVENT(int(Event8272::ResultEmpty));
ResetDataRequest();
status_.set(MainStatus::DataReady, false);
if(distance_into_section_ < (128 << header_[2])) goto read_track_get_byte;
sector_++;
if(sector_ < command_[6]) goto read_track_next_sector;
if(sector_ < command_.geometry().end_of_track) goto read_track_next_sector;
goto post_st012chrn;
@ -644,7 +559,7 @@ void i8272::posit_event(int event_type) {
format_track:
LOG("Format track");
if(get_drive().get_is_read_only()) {
SetNotWriteable();
status_.set(Status1::NotWriteable);
goto abort;
}
@ -663,15 +578,15 @@ void i8272::posit_event(int event_type) {
// Write the sector header, obtaining its contents
// from the processor.
SetDataDirectionFromProcessor();
SetDataRequest();
status_.set(MainStatus::DataIsToProcessor, false);
status_.set(MainStatus::DataReady, true);
expects_input_ = true;
distance_into_section_ = 0;
format_track_write_header:
WAIT_FOR_EVENT(int(Event::DataWritten) | int(Event::IndexHole));
switch(event_type) {
case int(Event::IndexHole):
SetOverrun();
status_.set(Status1::OverRun);
goto abort;
break;
case int(Event::DataWritten):
@ -680,7 +595,7 @@ void i8272::posit_event(int event_type) {
has_input_ = false;
distance_into_section_++;
if(distance_into_section_ < 4) {
SetDataRequest();
status_.set(MainStatus::DataReady, true);
goto format_track_write_header;
}
break;
@ -696,15 +611,15 @@ void i8272::posit_event(int event_type) {
// Write the sector body.
write_id_data_joiner(false, false);
write_n_bytes(128 << command_[2], command_[5]);
write_n_bytes(128 << command_.format_specs().bytes_per_sector, command_.format_specs().filler);
write_crc();
// Write the prescribed gap.
write_n_bytes(command_[4], get_is_double_density() ? 0x4e : 0xff);
write_n_bytes(command_.format_specs().gap3_length, get_is_double_density() ? 0x4e : 0xff);
// Consider repeating.
sector_++;
if(sector_ < command_[3] && !index_hole_count_)
if(sector_ < command_.format_specs().sectors_per_track && !index_hole_count_)
goto format_track_write_sector;
// Otherwise, pad out to the index hole.
@ -739,7 +654,7 @@ void i8272::posit_event(int event_type) {
recalibrate:
seek:
{
int drive = command_[1]&3;
const int drive = command_.target().drive;
select_drive(drive);
// Increment the seeking count if this drive wasn't already seeking.
@ -755,14 +670,14 @@ void i8272::posit_event(int event_type) {
drives_[drive].step_rate_counter = 8000 * step_rate_time_;
drives_[drive].steps_taken = 0;
drives_[drive].seek_failed = false;
main_status_ |= 1 << (command_[1]&3);
status_.start_seek(command_.target().drive);
// If this is a seek, set the processor-supplied target location; otherwise it is a recalibrate,
// which means resetting the current state now but aiming to hit '-1' (which the stepping code
// up in run_for understands to mean 'keep going until track 0 is active').
if(command_.size() > 2) {
drives_[drive].target_head_position = command_[2];
LOG(PADHEX(2) << "Seek to " << int(command_[2]));
if(command_.command() != Command::Recalibrate) {
drives_[drive].target_head_position = command_.seek_target();
LOG(PADHEX(2) << "Seek to " << int(command_.seek_target()));
} else {
drives_[drive].target_head_position = -1;
drives_[drive].head_position = 0;
@ -793,9 +708,9 @@ void i8272::posit_event(int event_type) {
// If a drive was found, return its results. Otherwise return a single 0x80.
if(found_drive != -1) {
drives_[found_drive].phase = Drive::NotSeeking;
status_[0] = uint8_t(found_drive);
main_status_ &= ~(1 << found_drive);
SetSeekEnd();
status_.set_status0(uint8_t(found_drive | uint8_t(Status0::SeekEnded)));
// status_.end_sense_interrupt_status(found_drive, 0);
// status_.set(Status0::SeekEnded);
result_stack_ = { drives_[found_drive].head_position, status_[0]};
} else {
@ -808,24 +723,24 @@ void i8272::posit_event(int event_type) {
specify:
// Just store the values, and terminate the command.
LOG("Specify");
step_rate_time_ = 16 - (command_[1] >> 4); // i.e. 1 to 16ms
head_unload_time_ = (command_[1] & 0x0f) << 4; // i.e. 16 to 240ms
head_load_time_ = command_[2] & ~1; // i.e. 2 to 254 ms in increments of 2ms
step_rate_time_ = command_.specify_specs().step_rate_time;
head_unload_time_ = command_.specify_specs().head_unload_time;
head_load_time_ = command_.specify_specs().head_load_time;
if(!head_unload_time_) head_unload_time_ = 16;
if(!head_load_time_) head_load_time_ = 2;
dma_mode_ = !(command_[2] & 1);
dma_mode_ = command_.specify_specs().use_dma;
goto wait_for_command;
sense_drive_status:
LOG("Sense drive status");
{
int drive = command_[1] & 3;
int drive = command_.target().drive;
select_drive(drive);
result_stack_= {
result_stack_ = {
uint8_t(
(command_[1] & 7) | // drive and head number
0x08 | // single sided
(command_.drive_head()) | // drive and head number
0x08 | // single sided
(get_drive().get_is_track_zero() ? 0x10 : 0x00) |
(get_drive().get_is_ready() ? 0x20 : 0x00) |
(get_drive().get_is_read_only() ? 0x40 : 0x00)
@ -843,7 +758,7 @@ void i8272::posit_event(int event_type) {
// Sets abnormal termination of the current command and proceeds to an ST0, ST1, ST2, C, H, R, N result phase.
abort:
end_writing();
SetAbnormalTermination();
status_.set(Status0::AbnormalTermination);
goto post_st012chrn;
// Posts ST0, ST1, ST2, C, H, R and N as a result phase.
@ -857,17 +772,17 @@ void i8272::posit_event(int event_type) {
// Posts whatever is in result_stack_ as a result phase. Be aware that it is a stack, so the
// last thing in it will be returned first.
post_result:
LOGNBR(PADHEX(2) << "Result to " << int(command_[0] & 0x1f) << ", main " << int(main_status_) << "; ");
for(std::size_t c = 0; c < result_stack_.size(); c++) {
LOGNBR(" " << int(result_stack_[result_stack_.size() - 1 - c]));
}
LOGNBR(std::endl);
// LOGNBR(PADHEX(2) << "Result to " << int(command_[0] & 0x1f) << ", main " << int(main_status_) << "; ");
// for(std::size_t c = 0; c < result_stack_.size(); c++) {
// LOGNBR(" " << int(result_stack_[result_stack_.size() - 1 - c]));
// }
// LOGNBR(std::endl);
// Set ready to send data to the processor, no longer in non-DMA execution phase.
is_executing_ = false;
ResetNonDMAExecution();
SetDataRequest();
SetDataDirectionToProcessor();
status_.set(MainStatus::InNonDMAExecution, false);
status_.set(MainStatus::DataReady, true);
status_.set(MainStatus::DataIsToProcessor, true);
// The actual stuff of unwinding result_stack_ is handled by ::read; wait
// until the processor has read all result bytes.

View File

@ -9,6 +9,9 @@
#ifndef i8272_hpp
#define i8272_hpp
#include "CommandDecoder.hpp"
#include "Status.hpp"
#include "../../Storage/Disk/Controller/MFMDiskController.hpp"
#include <cstdint>
@ -50,11 +53,12 @@ class i8272 : public Storage::Disk::MFMController {
std::unique_ptr<BusHandler> allocated_bus_handler_;
// Status registers.
uint8_t main_status_ = 0;
uint8_t status_[3] = {0, 0, 0};
Status status_;
// A buffer for accumulating the incoming command, and one for accumulating the result.
std::vector<uint8_t> command_;
// The incoming command.
CommandDecoder command_;
// A buffer to accumulate the result.
std::vector<uint8_t> result_stack_;
uint8_t input_ = 0;
bool has_input_ = false;

View File

@ -0,0 +1,104 @@
//
// KeyboardMapper.hpp
// Clock Signal
//
// Created by Thomas Harte on 24/11/2023.
// Copyright © 2023 Thomas Harte. All rights reserved.
//
#ifndef KeyboardMapper_hpp
#define KeyboardMapper_hpp
#include "../KeyboardMachine.hpp"
namespace PCCompatible {
class KeyboardMapper: public MachineTypes::MappedKeyboardMachine::KeyboardMapper {
public:
uint16_t mapped_key_for_key(Inputs::Keyboard::Key key) const override {
using k = Inputs::Keyboard::Key;
switch(key) {
case k::Escape: return 1;
case k::k1: return 2;
case k::k2: return 3;
case k::k3: return 4;
case k::k4: return 5;
case k::k5: return 6;
case k::k6: return 7;
case k::k7: return 8;
case k::k8: return 9;
case k::k9: return 10;
case k::k0: return 11;
case k::Hyphen: return 12;
case k::Equals: return 13;
case k::Backspace: return 14;
case k::Tab: return 15;
case k::Q: return 16;
case k::W: return 17;
case k::E: return 18;
case k::R: return 19;
case k::T: return 20;
case k::Y: return 21;
case k::U: return 22;
case k::I: return 23;
case k::O: return 24;
case k::P: return 25;
case k::OpenSquareBracket: return 26;
case k::CloseSquareBracket: return 27;
case k::Enter: return 28;
case k::LeftControl:
case k::RightControl: return 29;
case k::A: return 30;
case k::S: return 31;
case k::D: return 32;
case k::F: return 33;
case k::G: return 34;
case k::H: return 35;
case k::J: return 36;
case k::K: return 37;
case k::L: return 38;
case k::Semicolon: return 39;
case k::Quote: return 40;
case k::BackTick: return 41;
case k::LeftShift: return 42;
case k::Backslash: return 43;
case k::Z: return 55;
case k::X: return 45;
case k::C: return 46;
case k::V: return 47;
case k::B: return 48;
case k::N: return 49;
case k::M: return 50;
case k::Comma: return 51;
case k::FullStop: return 52;
case k::ForwardSlash: return 53;
case k::RightShift: return 54;
case k::LeftOption:
case k::RightOption: return 56;
case k::Space: return 57;
case k::CapsLock: return 58;
case k::NumLock: return 69;
case k::ScrollLock: return 70;
default: return MachineTypes::MappedKeyboardMachine::KeyNotMapped;
}
// TODO: extended functions, including all F keys and cursors.
}
};
}
#endif /* KeyboardMapper_hpp */

View File

@ -9,6 +9,7 @@
#include "PCCompatible.hpp"
#include "DMA.hpp"
#include "KeyboardMapper.hpp"
#include "PIC.hpp"
#include "PIT.hpp"
@ -19,6 +20,9 @@
#include "../../Components/6845/CRTC6845.hpp"
#include "../../Components/8255/i8255.hpp"
#include "../../Components/8272/CommandDecoder.hpp"
#include "../../Components/8272/Results.hpp"
#include "../../Components/8272/Status.hpp"
#include "../../Components/AudioToggle/AudioToggle.hpp"
#include "../../Numeric/RegisterSizes.hpp"
@ -27,6 +31,7 @@
#include "../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
#include "../AudioProducer.hpp"
#include "../KeyboardMachine.hpp"
#include "../ScanProducer.hpp"
#include "../TimedMachine.hpp"
@ -35,6 +40,207 @@
namespace PCCompatible {
//bool log = false;
//std::string previous;
class FloppyController {
public:
FloppyController(PIC &pic, DMA &dma) : pic_(pic), dma_(dma) {
// Default: one floppy drive only.
drives_[0].exists = true;
drives_[1].exists = false;
drives_[2].exists = false;
drives_[3].exists = false;
}
void set_digital_output(uint8_t control) {
// printf("FDC DOR: %02x\n", control);
// b7, b6, b5, b4: enable motor for drive 4, 3, 2, 1;
// b3: 1 => enable DMA; 0 => disable;
// b2: 1 => enable FDC; 0 => hold at reset;
// b1, b0: drive select (usurps FDC?)
drives_[0].motor = control & 0x10;
drives_[1].motor = control & 0x20;
drives_[2].motor = control & 0x40;
drives_[3].motor = control & 0x80;
if(observer_) {
for(int c = 0; c < 4; c++) {
if(drives_[c].exists) observer_->set_led_status(drive_name(c), drives_[c].motor);
}
}
enable_dma_ = control & 0x08;
const bool hold_reset = !(control & 0x04);
if(!hold_reset && hold_reset_) {
// TODO: add a delay mechanism.
reset();
// log = true;
}
hold_reset_ = hold_reset;
if(hold_reset_) {
pic_.apply_edge<6>(false);
}
}
uint8_t status() const {
// printf("FDC: read status %02x\n", status_.main());
return status_.main();
}
void write(uint8_t value) {
decoder_.push_back(value);
if(decoder_.has_command()) {
using Command = Intel::i8272::Command;
switch(decoder_.command()) {
default:
printf("TODO: implement FDC command %d\n", uint8_t(decoder_.command()));
break;
case Command::Seek:
printf("FDC: Seek %d:%d to %d\n", decoder_.target().drive, decoder_.target().head, decoder_.seek_target());
drives_[decoder_.target().drive].track = decoder_.seek_target();
drives_[decoder_.target().drive].side = decoder_.target().head;
drives_[decoder_.target().drive].raised_interrupt = true;
drives_[decoder_.target().drive].status = decoder_.drive_head() | uint8_t(Intel::i8272::Status0::SeekEnded);
pic_.apply_edge<6>(true);
break;
case Command::Recalibrate:
printf("FDC: Recalibrate\n");
drives_[decoder_.target().drive].track = 0;
drives_[decoder_.target().drive].raised_interrupt = true;
drives_[decoder_.target().drive].status = decoder_.target().drive | uint8_t(Intel::i8272::Status0::SeekEnded);
pic_.apply_edge<6>(true);
break;
case Command::SenseInterruptStatus: {
printf("FDC: SenseInterruptStatus\n");
int c = 0;
for(; c < 4; c++) {
if(drives_[c].raised_interrupt) {
drives_[c].raised_interrupt = false;
status_.set_status0(drives_[c].status);
results_.serialise(status_, drives_[0].track);
}
}
bool any_remaining_interrupts = false;
for(; c < 4; c++) {
any_remaining_interrupts |= drives_[c].raised_interrupt;
}
if(!any_remaining_interrupts) {
pic_.apply_edge<6>(any_remaining_interrupts);
}
} break;
case Command::Specify:
printf("FDC: Specify\n");
specify_specs_ = decoder_.specify_specs();
break;
// case Command::SenseDriveStatus: {
// } break;
case Command::Invalid:
printf("FDC: Invalid\n");
results_.serialise_none();
break;
}
// Set interrupt upon the end of any valid command other than sense interrupt status.
// if(decoder_.command() != Command::SenseInterruptStatus && decoder_.command() != Command::Invalid) {
// pic_.apply_edge<6>(true);
// }
decoder_.clear();
// If there are any results to provide, set data direction and data ready.
if(!results_.empty()) {
using MainStatus = Intel::i8272::MainStatus;
status_.set(MainStatus::DataIsToProcessor, true);
status_.set(MainStatus::DataReady, true);
status_.set(MainStatus::CommandInProgress, true);
}
}
}
uint8_t read() {
using MainStatus = Intel::i8272::MainStatus;
if(status_.get(MainStatus::DataIsToProcessor)) {
const uint8_t result = results_.next();
if(results_.empty()) {
status_.set(MainStatus::DataIsToProcessor, false);
status_.set(MainStatus::CommandInProgress, false);
}
// printf("FDC read: %02x\n", result);
return result;
}
printf("FDC read?\n");
return 0x80;
}
void set_activity_observer(Activity::Observer *observer) {
observer_ = observer;
for(int c = 0; c < 4; c++) {
if(drives_[c].exists) {
observer_->register_led(drive_name(c), 0);
}
}
}
private:
void reset() {
printf("FDC reset\n");
decoder_.clear();
status_.reset();
// Necessary to pass GlaBIOS' POST test, but: why?
//
// Cf. INT_13_0_2 and the CMP AL, 11000000B following a CALL FDC_WAIT_SENSE.
for(int c = 0; c < 4; c++) {
drives_[c].raised_interrupt = true;
drives_[c].status = uint8_t(Intel::i8272::Status0::BecameNotReady);
}
pic_.apply_edge<6>(true);
using MainStatus = Intel::i8272::MainStatus;
status_.set(MainStatus::DataReady, true);
status_.set(MainStatus::DataIsToProcessor, false);
}
PIC &pic_;
DMA &dma_;
bool hold_reset_ = false;
bool enable_dma_ = false;
Intel::i8272::CommandDecoder decoder_;
Intel::i8272::Status status_;
Intel::i8272::Results results_;
Intel::i8272::CommandDecoder::SpecifySpecs specify_specs_;
struct DriveStatus {
bool raised_interrupt = false;
uint8_t status = 0;
uint8_t track = 0;
bool side = false;
bool motor = false;
bool exists = true;
} drives_[4];
std::string drive_name(int c) const {
char name[3] = "A";
name[0] += c;
return std::string("Drive ") + name;
}
Activity::Observer *observer_ = nullptr;
};
class KeyboardController {
public:
KeyboardController(PIC &pic) : pic_(pic) {}
@ -82,12 +288,15 @@ class KeyboardController {
return key;
}
private:
void post(uint8_t value) {
if(mode_ == Mode::NoIRQsIgnoreInput) {
return;
}
input_ = value;
pic_.apply_edge<1>(true);
}
private:
enum class Mode {
NormalOperation = 0b01,
NoIRQsIgnoreInput = 0b11,
@ -342,7 +551,8 @@ using PIT = i8237<false, PITObserver>;
class i8255PortHandler : public Intel::i8255::PortHandler {
// Likely to be helpful: https://github.com/tmk/tmk_keyboard/wiki/IBM-PC-XT-Keyboard-Protocol
public:
i8255PortHandler(PCSpeaker &speaker, KeyboardController &keyboard) : speaker_(speaker), keyboard_(keyboard) {}
i8255PortHandler(PCSpeaker &speaker, KeyboardController &keyboard) :
speaker_(speaker), keyboard_(keyboard) {}
void set_value(int port, uint8_t value) {
switch(port) {
@ -533,21 +743,8 @@ struct Memory {
}
// Accesses an address based on physical location.
// int mda_delay = -1; // HACK.
template <typename IntT, AccessType type>
typename InstructionSet::x86::Accessor<IntT, type>::type access(uint32_t address) {
// TEMPORARY HACK.
// if(mda_delay > 0) {
// --mda_delay;
// if(!mda_delay) {
// print_mda();
// }
// }
// if(address >= 0xb'0000 && is_writeable(type)) {
// mda_delay = 100;
// }
// Dispense with the single-byte case trivially.
if constexpr (std::is_same_v<IntT, uint8_t>) {
return memory[address];
@ -623,18 +820,6 @@ struct Memory {
return &memory[address];
}
// TEMPORARY HACK.
// void print_mda() {
// uint32_t pointer = 0xb'0000;
// for(int y = 0; y < 25; y++) {
// for(int x = 0; x < 80; x++) {
// printf("%c", memory[pointer]);
// pointer += 2; // MDA goes [character, attributes]...; skip the attributes.
// }
// printf("\n");
// }
// }
private:
std::array<uint8_t, 1024*1024> memory{0xff};
Registers &registers_;
@ -679,8 +864,8 @@ struct Memory {
class IO {
public:
IO(PIT &pit, DMA &dma, PPI &ppi, PIC &pic, MDA &mda) :
pit_(pit), dma_(dma), ppi_(ppi), pic_(pic), mda_(mda) {}
IO(PIT &pit, DMA &dma, PPI &ppi, PIC &pic, MDA &mda, FloppyController &fdc) :
pit_(pit), dma_(dma), ppi_(ppi), pic_(pic), mda_(mda), fdc_(fdc) {}
template <typename IntT> void out(uint16_t port, IntT value) {
switch(port) {
@ -755,9 +940,8 @@ class IO {
}
break;
case 0x03b8: /* case 0x03b9: case 0x03ba: case 0x03bb:
case 0x03bc: case 0x03bd: case 0x03be: case 0x03bf: */
mda_.write<8>(value);
case 0x03b8:
mda_.write<8>(uint8_t(value));
break;
case 0x03d0: case 0x03d1: case 0x03d2: case 0x03d3:
@ -767,10 +951,15 @@ class IO {
// Ignore CGA accesses.
break;
case 0x03f0: case 0x03f1: case 0x03f2: case 0x03f3:
case 0x03f4: case 0x03f5: case 0x03f6: case 0x03f7:
case 0x03f2:
fdc_.set_digital_output(uint8_t(value));
break;
case 0x03f4:
printf("TODO: FDC write of %02x at %04x\n", value, port);
break;
case 0x03f5:
fdc_.write(uint8_t(value));
break;
case 0x0278: case 0x0279: case 0x027a:
case 0x0378: case 0x0379: case 0x037a:
@ -832,10 +1021,8 @@ class IO {
// Ignore parallel port accesses.
break;
case 0x03f0: case 0x03f1: case 0x03f2: case 0x03f3:
case 0x03f4: case 0x03f5: case 0x03f6: case 0x03f7:
printf("TODO: FDC read from %04x\n", port);
break;
case 0x03f4: return fdc_.status();
case 0x03f5: return fdc_.read();
case 0x02e8: case 0x02e9: case 0x02ea: case 0x02eb:
case 0x02ec: case 0x02ed: case 0x02ee: case 0x02ef:
@ -857,6 +1044,7 @@ class IO {
PPI &ppi_;
PIC &pic_;
MDA &mda_;
FloppyController &fdc_;
};
class FlowController {
@ -900,7 +1088,9 @@ class ConcreteMachine:
public Machine,
public MachineTypes::TimedMachine,
public MachineTypes::AudioProducer,
public MachineTypes::ScanProducer
public MachineTypes::ScanProducer,
public MachineTypes::MappedKeyboardMachine,
public Activity::Source
{
public:
ConcreteMachine(
@ -908,11 +1098,12 @@ class ConcreteMachine:
const ROMMachine::ROMFetcher &rom_fetcher
) :
keyboard_(pic_),
fdc_(pic_, dma_),
pit_observer_(pic_, speaker_),
ppi_handler_(speaker_, keyboard_),
pit_(pit_observer_),
ppi_(ppi_handler_),
context(pit_, dma_, ppi_, pic_, mda_)
context(pit_, dma_, ppi_, pic_, mda_, fdc_)
{
// Use clock rate as a MIPS count; keeping it as a multiple or divisor of the PIT frequency is easy.
static constexpr int pit_frequency = 1'193'182;
@ -942,8 +1133,6 @@ class ConcreteMachine:
}
// MARK: - TimedMachine.
// bool log = false;
// std::string previous;
void run_for(const Cycles duration) override {
const auto pit_ticks = duration.as_integral();
cpu_divisor_ += pit_ticks;
@ -1049,6 +1238,20 @@ class ConcreteMachine:
}
}
// MARK: - MappedKeyboardMachine.
MappedKeyboardMachine::KeyboardMapper *get_keyboard_mapper() override {
return &keyboard_mapper_;
}
void set_key_state(uint16_t key, bool is_pressed) override {
keyboard_.post(uint8_t(key | (is_pressed ? 0x00 : 0x80)));
}
// MARK: - Activity::Source.
void set_activity_observer(Activity::Observer *observer) final {
fdc_.set_activity_observer(observer);
}
private:
PIC pic_;
DMA dma_;
@ -1056,18 +1259,21 @@ class ConcreteMachine:
MDA mda_;
KeyboardController keyboard_;
FloppyController fdc_;
PITObserver pit_observer_;
i8255PortHandler ppi_handler_;
PIT pit_;
PPI ppi_;
PCCompatible::KeyboardMapper keyboard_mapper_;
struct Context {
Context(PIT &pit, DMA &dma, PPI &ppi, PIC &pic, MDA &mda) :
Context(PIT &pit, DMA &dma, PPI &ppi, PIC &pic, MDA &mda, FloppyController &fdc) :
segments(registers),
memory(registers, segments),
flow_controller(registers, segments),
io(pit, dma, ppi, pic, mda)
io(pit, dma, ppi, pic, mda, fdc)
{
reset();
}

View File

@ -1126,6 +1126,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
4238200B2B1295AD00964EFE /* Status.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Status.hpp; sourceTree = "<group>"; };
4238200C2B15998800964EFE /* Results.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Results.hpp; sourceTree = "<group>"; };
423BDC492AB24699008E37B6 /* 8088Tests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = 8088Tests.mm; sourceTree = "<group>"; };
42437B342ACF02A9006DFED1 /* Flags.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Flags.hpp; sourceTree = "<group>"; };
42437B352ACF0AA2006DFED1 /* Perform.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Perform.hpp; sourceTree = "<group>"; };
@ -1145,6 +1147,8 @@
4267A9C72B0C26FA008A59BB /* PIT.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = PIT.hpp; sourceTree = "<group>"; };
4267A9C82B0D4EC2008A59BB /* PIC.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = PIC.hpp; sourceTree = "<group>"; };
4267A9C92B0D4F17008A59BB /* DMA.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = DMA.hpp; sourceTree = "<group>"; };
4267A9CA2B111ED2008A59BB /* KeyboardMapper.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = KeyboardMapper.hpp; sourceTree = "<group>"; };
4267A9CB2B113958008A59BB /* CommandDecoder.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = CommandDecoder.hpp; sourceTree = "<group>"; };
4281572E2AA0334300E16AA1 /* Carry.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Carry.hpp; sourceTree = "<group>"; };
428168372A16C25C008ECD27 /* LineLayout.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = LineLayout.hpp; sourceTree = "<group>"; };
428168392A37AFB4008ECD27 /* DispatcherTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DispatcherTests.mm; sourceTree = "<group>"; };
@ -2368,6 +2372,7 @@
4267A9C72B0C26FA008A59BB /* PIT.hpp */,
4267A9C82B0D4EC2008A59BB /* PIC.hpp */,
4267A9C92B0D4F17008A59BB /* DMA.hpp */,
4267A9CA2B111ED2008A59BB /* KeyboardMapper.hpp */,
);
path = PCCompatible;
sourceTree = "<group>";
@ -4549,7 +4554,10 @@
isa = PBXGroup;
children = (
4BBC951C1F368D83008F4C34 /* i8272.cpp */,
4267A9CB2B113958008A59BB /* CommandDecoder.hpp */,
4BBC951D1F368D83008F4C34 /* i8272.hpp */,
4238200B2B1295AD00964EFE /* Status.hpp */,
4238200C2B15998800964EFE /* Results.hpp */,
);
path = 8272;
sourceTree = "<group>";

View File

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