2019-10-27 01:33:57 +00:00
|
|
|
//
|
|
|
|
// DMAController.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 26/10/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "DMAController.hpp"
|
|
|
|
|
2019-12-24 03:00:40 +00:00
|
|
|
#define LOG_PREFIX "[DMA] "
|
|
|
|
#include "../../../Outputs/Log.hpp"
|
|
|
|
|
2019-10-27 01:33:57 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
using namespace Atari::ST;
|
|
|
|
|
2019-11-03 22:24:36 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum Control: uint16_t {
|
|
|
|
Direction = 0x100,
|
|
|
|
DRQSource = 0x80,
|
|
|
|
SectorCountSelect = 0x10,
|
|
|
|
CPUTarget = 0x08
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-27 02:39:11 +00:00
|
|
|
DMAController::DMAController() {
|
2019-10-29 01:21:53 +00:00
|
|
|
fdc_.set_delegate(this);
|
2019-10-31 02:59:32 +00:00
|
|
|
fdc_.set_clocking_hint_observer(this);
|
2019-10-27 01:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t DMAController::read(int address) {
|
|
|
|
switch(address & 7) {
|
|
|
|
// Reserved.
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
// Disk controller or sector count.
|
|
|
|
case 2:
|
2019-11-03 22:24:36 +00:00
|
|
|
if(control_ & Control::SectorCountSelect) {
|
|
|
|
return uint16_t((byte_count_ + 511) >> 9); // Assumed here: the count is of sectors remaining, i.e. it decrements
|
|
|
|
// only when a sector is complete.
|
2019-10-27 01:33:57 +00:00
|
|
|
} else {
|
2019-11-03 22:24:36 +00:00
|
|
|
if(control_ & Control::CPUTarget) {
|
|
|
|
return 0xffff;
|
|
|
|
} else {
|
|
|
|
return 0xff00 | fdc_.get_register(control_ >> 1);
|
|
|
|
}
|
2019-10-27 01:33:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// DMA status.
|
|
|
|
case 3:
|
2019-11-03 22:24:36 +00:00
|
|
|
// TODO: should DRQ come from the HDC if that mode is selected?
|
|
|
|
return 0xfff8 | (error_ ? 0 : 1) | (byte_count_ ? 2 : 0) | (fdc_.get_data_request_line() ? 4 : 0);
|
2019-10-27 01:33:57 +00:00
|
|
|
|
|
|
|
// DMA addressing.
|
2019-11-03 22:24:36 +00:00
|
|
|
case 4: return uint16_t(0xff00 | ((address_ >> 16) & 0xff));
|
|
|
|
case 5: return uint16_t(0xff00 | ((address_ >> 8) & 0xff));
|
|
|
|
case 6: return uint16_t(0xff00 | ((address_ >> 0) & 0xff));
|
2019-10-27 01:33:57 +00:00
|
|
|
}
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DMAController::write(int address, uint16_t value) {
|
|
|
|
switch(address & 7) {
|
|
|
|
// Reserved.
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
// Disk controller or sector count.
|
|
|
|
case 2:
|
2019-11-03 22:24:36 +00:00
|
|
|
if(control_ & Control::SectorCountSelect) {
|
|
|
|
byte_count_ = (value & 0xff) << 9; // The computer provides a sector count; that times 512 is a byte count.
|
|
|
|
|
|
|
|
// TODO: if this is a write-mode DMA operation, try to fill both buffers, ASAP.
|
2019-10-27 01:33:57 +00:00
|
|
|
} else {
|
2019-11-03 22:24:36 +00:00
|
|
|
if(control_ & Control::CPUTarget) {
|
|
|
|
// TODO: HDC.
|
|
|
|
} else {
|
|
|
|
fdc_.set_register(control_ >> 1, uint8_t(value));
|
|
|
|
}
|
2019-10-27 01:33:57 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// DMA control; meaning is:
|
|
|
|
//
|
2019-11-03 22:24:36 +00:00
|
|
|
// b0: unused
|
2019-10-27 01:33:57 +00:00
|
|
|
// b1, b2 = address lines for FDC access.
|
2019-11-03 22:24:36 +00:00
|
|
|
// b3 = 1 => CPU HDC access; 0 => CPU FDC access.
|
|
|
|
// b4 = 1 => sector count access; 0 => [F/H]DC access.
|
|
|
|
// b5: unused.
|
|
|
|
// b6 = officially, 1 => DMA off; 0 => DMA on. Ignored in real hardware.
|
|
|
|
// b7 = 1 => FDC DRQs being observed; 0 => HDC access DRQs being observed.
|
|
|
|
// b8 = 1 => DMA is writing to [F/H]DC; 0 => DMA is reading. Changing value resets DMA state.
|
2019-10-27 01:33:57 +00:00
|
|
|
//
|
|
|
|
// All other bits: undefined.
|
|
|
|
case 3:
|
2019-11-03 22:24:36 +00:00
|
|
|
// Check for a DMA state reset.
|
|
|
|
if((control_^value) & Control::Direction) {
|
|
|
|
bytes_received_ = active_buffer_ = 0;
|
|
|
|
error_ = false;
|
|
|
|
byte_count_ = 0;
|
|
|
|
}
|
2019-10-27 01:33:57 +00:00
|
|
|
control_ = value;
|
|
|
|
break;
|
|
|
|
|
2019-12-20 03:58:07 +00:00
|
|
|
// DMA addressing; cf. http://www.atari-forum.com/viewtopic.php?t=30289 on a hardware
|
|
|
|
// feature emulated here: 'carry' will ripple upwards if a write resets the top bit
|
|
|
|
// of the byte it is adjusting.
|
2019-11-03 22:24:36 +00:00
|
|
|
case 4: address_ = int((address_ & 0x00ffff) | ((value & 0xff) << 16)); break;
|
2019-12-20 03:58:07 +00:00
|
|
|
case 5:
|
2019-12-21 01:49:14 +00:00
|
|
|
if(((value << 8) ^ address_) & ~(value << 8) & 0x8000) address_ += 0x10000;
|
2019-12-20 03:58:07 +00:00
|
|
|
address_ = int((address_ & 0xff00ff) | ((value & 0xff) << 8));
|
|
|
|
break;
|
|
|
|
case 6:
|
2019-12-21 01:49:14 +00:00
|
|
|
if((value ^ address_) & ~value & 0x80) address_ += 0x100;
|
2019-12-20 03:58:07 +00:00
|
|
|
address_ = int((address_ & 0xffff00) | ((value & 0xfe) << 0));
|
|
|
|
break; // Lowest bit: discarded.
|
2019-10-27 01:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 03:04:08 +00:00
|
|
|
void DMAController::set_floppy_drive_selection(bool drive1, bool drive2, bool side2) {
|
2019-12-25 02:43:39 +00:00
|
|
|
// LOG("Selected: " << (drive1 ? "1" : "-") << (drive2 ? "2" : "-") << (side2 ? "s" : "-"));
|
2019-11-03 03:04:08 +00:00
|
|
|
fdc_.set_floppy_drive_selection(drive1, drive2, side2);
|
|
|
|
}
|
|
|
|
|
2019-11-03 03:26:42 +00:00
|
|
|
void DMAController::set_floppy_disk(std::shared_ptr<Storage::Disk::Disk> disk, size_t drive) {
|
|
|
|
fdc_.drives_[drive]->set_disk(disk);
|
|
|
|
}
|
|
|
|
|
2019-10-27 01:33:57 +00:00
|
|
|
void DMAController::run_for(HalfCycles duration) {
|
|
|
|
running_time_ += duration;
|
|
|
|
fdc_.run_for(duration.flush<Cycles>());
|
|
|
|
}
|
2019-10-29 01:13:21 +00:00
|
|
|
|
|
|
|
void DMAController::wd1770_did_change_output(WD::WD1770 *) {
|
2019-11-03 03:26:42 +00:00
|
|
|
// Check for a change in interrupt state.
|
2019-10-29 01:13:21 +00:00
|
|
|
const bool old_interrupt_line = interrupt_line_;
|
|
|
|
interrupt_line_ = fdc_.get_interrupt_request_line();
|
2019-11-04 02:11:25 +00:00
|
|
|
if(delegate_ && interrupt_line_ != old_interrupt_line) {
|
|
|
|
delegate_->dma_controller_did_change_output(this);
|
2019-10-29 01:13:21 +00:00
|
|
|
}
|
2019-11-03 03:26:42 +00:00
|
|
|
|
2019-11-03 22:24:36 +00:00
|
|
|
// Check for a change in DRQ state, if it's the FDC that is currently being watched.
|
2019-11-04 02:11:25 +00:00
|
|
|
if(byte_count_ && fdc_.get_data_request_line() && (control_ & Control::DRQSource)) {
|
|
|
|
--byte_count_;
|
|
|
|
|
2019-11-03 22:24:36 +00:00
|
|
|
if(control_ & Control::Direction) {
|
|
|
|
// TODO: DMA is supposed to be helping with a write.
|
|
|
|
} else {
|
|
|
|
// DMA is enabling a read.
|
|
|
|
|
|
|
|
// Read from the data register into the active buffer.
|
2019-11-04 02:11:25 +00:00
|
|
|
if(bytes_received_ < 16) {
|
|
|
|
buffer_[active_buffer_].contents[bytes_received_] = fdc_.get_register(3);
|
|
|
|
++bytes_received_;
|
|
|
|
}
|
2019-11-03 22:24:36 +00:00
|
|
|
if(bytes_received_ == 16) {
|
2019-11-04 02:11:25 +00:00
|
|
|
// Mark buffer as full.
|
|
|
|
buffer_[active_buffer_].is_full = true;
|
|
|
|
|
|
|
|
// Move to the next if it is empty; if it isn't, note a DMA error.
|
|
|
|
const auto next_buffer = active_buffer_ ^ 1;
|
|
|
|
error_ |= buffer_[next_buffer].is_full;
|
|
|
|
if(!buffer_[next_buffer].is_full) {
|
|
|
|
bytes_received_ = 0;
|
|
|
|
active_buffer_ = next_buffer;
|
|
|
|
}
|
2019-11-03 22:24:36 +00:00
|
|
|
|
2019-11-04 02:11:25 +00:00
|
|
|
// Set bus request.
|
|
|
|
if(!bus_request_line_) {
|
|
|
|
bus_request_line_ = true;
|
|
|
|
if(delegate_) delegate_->dma_controller_did_change_output(this);
|
|
|
|
}
|
2019-11-03 22:24:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 03:26:42 +00:00
|
|
|
}
|
2019-10-29 01:13:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 02:11:25 +00:00
|
|
|
int DMAController::bus_grant(uint16_t *ram, size_t size) {
|
|
|
|
// Being granted the bus negates the request.
|
|
|
|
bus_request_line_ = false;
|
|
|
|
if(delegate_) delegate_->dma_controller_did_change_output(this);
|
|
|
|
|
2019-12-20 03:58:07 +00:00
|
|
|
size <<= 1; // Convert to bytes.
|
2019-11-04 02:11:25 +00:00
|
|
|
if(control_ & Control::Direction) {
|
|
|
|
// TODO: writes.
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// Check that the older buffer is full; stop if not.
|
|
|
|
if(!buffer_[active_buffer_ ^ 1].is_full) return 0;
|
|
|
|
|
2019-12-25 00:15:58 +00:00
|
|
|
#define b(i, n) " " << PADHEX(2) << int(buffer_[i].contents[n])
|
2019-12-24 03:00:40 +00:00
|
|
|
#define b2(i, n) b(i, n) << b(i, n+1)
|
|
|
|
#define b4(i, n) b2(i, n) << b2(i, n+2)
|
|
|
|
#define b16(i) b4(i, 0) << b4(i, 4) << b4(i, 8) << b4(i, 12)
|
2019-12-25 02:43:39 +00:00
|
|
|
// LOG("[1] to " << PADHEX(6) << address_ << b16(active_buffer_ ^ 1));
|
2019-12-24 02:31:46 +00:00
|
|
|
|
2019-11-04 02:11:25 +00:00
|
|
|
for(int c = 0; c < 8; ++c) {
|
2019-12-20 03:58:07 +00:00
|
|
|
if(size_t(address_) < size) {
|
|
|
|
ram[address_ >> 1] = uint16_t(
|
|
|
|
(buffer_[active_buffer_ ^ 1].contents[(c << 1) + 0] << 8) |
|
|
|
|
(buffer_[active_buffer_ ^ 1].contents[(c << 1) + 1] << 0)
|
|
|
|
);
|
|
|
|
}
|
2019-11-04 02:11:25 +00:00
|
|
|
address_ += 2;
|
|
|
|
}
|
|
|
|
buffer_[active_buffer_ ^ 1].is_full = false;
|
|
|
|
|
|
|
|
// Check that the newer buffer is full; stop if not.
|
|
|
|
if(!buffer_[active_buffer_ ].is_full) return 8;
|
|
|
|
|
2019-12-25 02:43:39 +00:00
|
|
|
// LOG("[2] to " << PADHEX(6) << address_ << b16(active_buffer_));
|
2019-12-24 03:00:40 +00:00
|
|
|
#undef b16
|
|
|
|
#undef b4
|
|
|
|
#undef b2
|
|
|
|
#undef b
|
2019-12-24 02:31:46 +00:00
|
|
|
|
2019-11-04 02:11:25 +00:00
|
|
|
for(int c = 0; c < 8; ++c) {
|
2019-12-20 03:58:07 +00:00
|
|
|
if(size_t(address_) < size) {
|
|
|
|
ram[address_ >> 1] = uint16_t(
|
|
|
|
(buffer_[active_buffer_].contents[(c << 1) + 0] << 8) |
|
|
|
|
(buffer_[active_buffer_].contents[(c << 1) + 1] << 0)
|
|
|
|
);
|
|
|
|
}
|
2019-11-04 02:11:25 +00:00
|
|
|
address_ += 2;
|
|
|
|
}
|
|
|
|
buffer_[active_buffer_].is_full = false;
|
|
|
|
|
|
|
|
// Both buffers were full, so unblock reading.
|
|
|
|
bytes_received_ = 0;
|
|
|
|
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DMAController::set_delegate(Delegate *delegate) {
|
|
|
|
delegate_ = delegate;
|
2019-10-29 01:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DMAController::get_interrupt_line() {
|
|
|
|
return interrupt_line_;
|
|
|
|
}
|
2019-10-31 02:59:32 +00:00
|
|
|
|
2019-11-04 02:11:25 +00:00
|
|
|
bool DMAController::get_bus_request_line() {
|
|
|
|
return bus_request_line_;
|
|
|
|
}
|
|
|
|
|
2019-10-31 02:59:32 +00:00
|
|
|
void DMAController::set_component_prefers_clocking(ClockingHint::Source *, ClockingHint::Preference) {
|
|
|
|
update_clocking_observer();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClockingHint::Preference DMAController::preferred_clocking() {
|
|
|
|
return (fdc_.preferred_clocking() == ClockingHint::Preference::None) ? ClockingHint::Preference::None : ClockingHint::Preference::RealTime;
|
|
|
|
}
|
2019-11-04 02:57:54 +00:00
|
|
|
|
|
|
|
void DMAController::set_activity_observer(Activity::Observer *observer) {
|
|
|
|
fdc_.drives_[0]->set_activity_observer(observer, "Internal", true);
|
|
|
|
fdc_.drives_[1]->set_activity_observer(observer, "External", true);
|
|
|
|
}
|