2022-02-05 17:12:10 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-09-30 19:34:47 +00:00
|
|
|
Copyright (C) 2018-24 divingkatae and maximum
|
2022-02-05 17:12:10 +00:00
|
|
|
(theweirdo) spatium
|
|
|
|
|
|
|
|
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file SCSI bus emulation. */
|
|
|
|
|
|
|
|
#include <devices/common/hwcomponent.h>
|
|
|
|
#include <devices/common/scsi/scsi.h>
|
2023-05-30 17:46:27 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2022-02-05 17:12:10 +00:00
|
|
|
#include <loguru.hpp>
|
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
|
2023-05-30 17:46:27 +00:00
|
|
|
ScsiBus::ScsiBus(const std::string name)
|
2022-02-05 17:12:10 +00:00
|
|
|
{
|
2023-05-30 17:46:27 +00:00
|
|
|
this->set_name(name);
|
2022-02-05 17:12:10 +00:00
|
|
|
supports_types(HWCompType::SCSI_BUS);
|
|
|
|
|
|
|
|
for(int i = 0; i < SCSI_MAX_DEVS; i++) {
|
|
|
|
this->devices[i] = nullptr;
|
|
|
|
this->dev_ctrl_lines[i] = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-25 00:45:44 +00:00
|
|
|
this->ctrl_lines = 0; // all control lines released
|
|
|
|
this->data_lines = 0; // data lines released
|
2022-02-05 17:12:10 +00:00
|
|
|
this->arb_winner_id = -1;
|
2022-10-25 00:45:44 +00:00
|
|
|
this->initiator_id = -1;
|
|
|
|
this->target_id = -1;
|
2022-02-05 17:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiBus::register_device(int id, ScsiDevice* dev_obj)
|
|
|
|
{
|
|
|
|
if (this->devices[id] != nullptr) {
|
|
|
|
ABORT_F("ScsiBus: device with ID %d already registered", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->devices[id] = dev_obj;
|
2023-11-01 15:07:29 +00:00
|
|
|
|
|
|
|
dev_obj->set_bus_object_ptr(this);
|
2022-02-05 17:12:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiBus::change_bus_phase(int initiator_id)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < SCSI_MAX_DEVS; i++) {
|
|
|
|
if (i == initiator_id)
|
|
|
|
continue; // don't notify the initiator
|
|
|
|
if (this->devices[i] != nullptr) {
|
2023-11-01 15:07:29 +00:00
|
|
|
this->devices[i]->notify(ScsiMsg::BUS_PHASE_CHANGE, this->cur_phase);
|
2022-02-05 17:12:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiBus::assert_ctrl_line(int initiator_id, uint16_t mask)
|
|
|
|
{
|
2023-10-19 05:43:46 +00:00
|
|
|
DCHECK_F(initiator_id >= 0 && initiator_id < SCSI_MAX_DEVS, "ScsiBus: invalid initiator ID %d", initiator_id);
|
|
|
|
|
2022-02-05 17:12:10 +00:00
|
|
|
uint16_t new_state = 0xFFFFU & mask;
|
|
|
|
|
2022-11-01 01:52:30 +00:00
|
|
|
this->dev_ctrl_lines[initiator_id] |= new_state;
|
2022-02-05 17:12:10 +00:00
|
|
|
|
|
|
|
if (new_state == this->ctrl_lines) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_state & SCSI_CTRL_RST) {
|
2022-11-01 01:52:30 +00:00
|
|
|
this->ctrl_lines |= SCSI_CTRL_RST;
|
2022-02-05 17:12:10 +00:00
|
|
|
this->cur_phase = ScsiPhase::RESET;
|
|
|
|
change_bus_phase(initiator_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiBus::release_ctrl_line(int id, uint16_t mask)
|
|
|
|
{
|
2023-10-19 05:43:46 +00:00
|
|
|
DCHECK_F(id >= 0 && id < SCSI_MAX_DEVS, "ScsiBus: invalid initiator ID %d", id);
|
|
|
|
|
2022-02-05 17:12:10 +00:00
|
|
|
uint16_t new_state = 0;
|
|
|
|
|
|
|
|
this->dev_ctrl_lines[id] &= ~mask;
|
|
|
|
|
|
|
|
// OR control lines of all devices together
|
|
|
|
for (int i = 0; i < SCSI_MAX_DEVS; i++) {
|
|
|
|
new_state |= this->dev_ctrl_lines[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->ctrl_lines & SCSI_CTRL_RST) {
|
|
|
|
if (!(new_state & SCSI_CTRL_RST)) {
|
|
|
|
this->ctrl_lines = new_state;
|
|
|
|
this->cur_phase = ScsiPhase::BUS_FREE;
|
|
|
|
change_bus_phase(id);
|
|
|
|
}
|
2022-10-25 00:45:44 +00:00
|
|
|
} else {
|
|
|
|
this->ctrl_lines = new_state;
|
2022-02-05 17:12:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiBus::release_ctrl_lines(int id)
|
|
|
|
{
|
|
|
|
this->release_ctrl_line(id, 0xFFFFUL);
|
|
|
|
}
|
|
|
|
|
2022-10-25 00:45:44 +00:00
|
|
|
uint16_t ScsiBus::test_ctrl_lines(uint16_t mask)
|
|
|
|
{
|
|
|
|
uint16_t new_state = 0;
|
|
|
|
|
|
|
|
// OR control lines of all devices together
|
|
|
|
for (int i = 0; i < SCSI_MAX_DEVS; i++) {
|
|
|
|
new_state |= this->dev_ctrl_lines[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_state & mask;
|
|
|
|
}
|
|
|
|
|
2022-10-27 11:47:43 +00:00
|
|
|
int ScsiBus::switch_phase(int id, int new_phase)
|
|
|
|
{
|
|
|
|
int old_phase = this->cur_phase;
|
|
|
|
|
|
|
|
// leave the current phase (low-level)
|
|
|
|
switch (old_phase) {
|
|
|
|
case ScsiPhase::COMMAND:
|
|
|
|
this->release_ctrl_line(id, SCSI_CTRL_CD);
|
|
|
|
break;
|
|
|
|
case ScsiPhase::DATA_IN:
|
|
|
|
this->release_ctrl_line(id, SCSI_CTRL_IO);
|
|
|
|
break;
|
2022-11-02 20:27:18 +00:00
|
|
|
case ScsiPhase::STATUS:
|
|
|
|
this->release_ctrl_line(id, SCSI_CTRL_CD | SCSI_CTRL_IO);
|
|
|
|
break;
|
2022-11-07 11:24:02 +00:00
|
|
|
case ScsiPhase::MESSAGE_OUT:
|
|
|
|
this->release_ctrl_line(id, SCSI_CTRL_CD | SCSI_CTRL_MSG);
|
|
|
|
break;
|
2022-10-27 11:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// enter new phase (low-level)
|
|
|
|
switch (new_phase) {
|
|
|
|
case ScsiPhase::COMMAND:
|
|
|
|
this->assert_ctrl_line(id, SCSI_CTRL_CD);
|
|
|
|
break;
|
|
|
|
case ScsiPhase::DATA_IN:
|
|
|
|
this->assert_ctrl_line(id, SCSI_CTRL_IO);
|
|
|
|
break;
|
2022-11-02 20:27:18 +00:00
|
|
|
case ScsiPhase::STATUS:
|
|
|
|
this->assert_ctrl_line(id, SCSI_CTRL_CD | SCSI_CTRL_IO);
|
|
|
|
break;
|
2022-11-07 11:24:02 +00:00
|
|
|
case ScsiPhase::MESSAGE_OUT:
|
|
|
|
this->assert_ctrl_line(id, SCSI_CTRL_CD | SCSI_CTRL_MSG);
|
|
|
|
break;
|
2022-10-27 11:47:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// switch the bus to the new phase (high-level)
|
|
|
|
this->cur_phase = new_phase;
|
|
|
|
change_bus_phase(id);
|
|
|
|
|
|
|
|
return old_phase;
|
|
|
|
}
|
|
|
|
|
2022-02-05 17:12:10 +00:00
|
|
|
bool ScsiBus::begin_arbitration(int initiator_id)
|
|
|
|
{
|
|
|
|
if (this->cur_phase == ScsiPhase::BUS_FREE) {
|
|
|
|
this->data_lines |= 1 << initiator_id;
|
|
|
|
this->cur_phase = ScsiPhase::ARBITRATION;
|
|
|
|
change_bus_phase(initiator_id);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScsiBus::end_arbitration(int initiator_id)
|
|
|
|
{
|
|
|
|
int highest_id = -1;
|
|
|
|
|
|
|
|
// find the highest ID bit on the data lines
|
|
|
|
for (int id = 7; id >= 0; id--) {
|
|
|
|
if (this->data_lines & (1 << id)) {
|
|
|
|
highest_id = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (highest_id >= 0) {
|
|
|
|
this->arb_winner_id = highest_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return highest_id == initiator_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScsiBus::begin_selection(int initiator_id, int target_id, bool atn)
|
|
|
|
{
|
|
|
|
// perform bus integrity checks
|
|
|
|
if (this->cur_phase != ScsiPhase::ARBITRATION || this->arb_winner_id != initiator_id)
|
|
|
|
return false;
|
|
|
|
|
2022-10-25 00:45:44 +00:00
|
|
|
this->assert_ctrl_line(initiator_id, SCSI_CTRL_SEL);
|
|
|
|
|
2022-11-02 22:21:46 +00:00
|
|
|
this->data_lines = (1 << initiator_id) | (1 << target_id);
|
2022-02-05 17:12:10 +00:00
|
|
|
|
|
|
|
if (atn) {
|
|
|
|
assert_ctrl_line(initiator_id, SCSI_CTRL_ATN);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->initiator_id = initiator_id;
|
|
|
|
this->cur_phase = ScsiPhase::SELECTION;
|
|
|
|
change_bus_phase(initiator_id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScsiBus::confirm_selection(int target_id)
|
|
|
|
{
|
|
|
|
this->target_id = target_id;
|
|
|
|
|
|
|
|
// notify initiator about selection confirmation from target
|
|
|
|
if (this->initiator_id >= 0) {
|
2023-11-01 15:07:29 +00:00
|
|
|
this->devices[this->initiator_id]->notify(ScsiMsg::CONFIRM_SEL, target_id);
|
2022-02-05 17:12:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScsiBus::end_selection(int initiator_id, int target_id)
|
|
|
|
{
|
|
|
|
// check for selection confirmation from target
|
|
|
|
return this->target_id == target_id;
|
|
|
|
}
|
2022-02-06 00:50:19 +00:00
|
|
|
|
2022-11-07 11:24:02 +00:00
|
|
|
bool ScsiBus::pull_data(const int id, uint8_t* dst_ptr, const int size)
|
2022-10-25 00:45:44 +00:00
|
|
|
{
|
2022-11-07 11:24:02 +00:00
|
|
|
if (dst_ptr == nullptr || !size) {
|
2022-10-25 00:45:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:24:02 +00:00
|
|
|
if (!this->devices[id]->send_data(dst_ptr, size)) {
|
|
|
|
LOG_F(ERROR, "ScsiBus: error while transferring T->I data!");
|
|
|
|
return false;
|
2022-10-25 00:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:24:02 +00:00
|
|
|
bool ScsiBus::push_data(const int id, const uint8_t* src_ptr, const int size)
|
2022-10-31 22:17:08 +00:00
|
|
|
{
|
2022-11-07 11:24:02 +00:00
|
|
|
if (!this->devices[id]->rcv_data(src_ptr, size)) {
|
|
|
|
LOG_F(ERROR, "ScsiBus: error while transferring I->T data!");
|
2022-10-31 22:17:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-03 09:50:50 +00:00
|
|
|
int ScsiBus::target_xfer_data() {
|
|
|
|
return this->devices[this->target_id]->xfer_data();
|
|
|
|
}
|
|
|
|
|
2022-11-02 20:27:18 +00:00
|
|
|
void ScsiBus::target_next_step()
|
|
|
|
{
|
2023-11-01 15:07:29 +00:00
|
|
|
this->devices[this->target_id]->next_step();
|
2022-11-02 20:27:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-07 11:24:02 +00:00
|
|
|
bool ScsiBus::negotiate_xfer(int& bytes_in, int& bytes_out)
|
|
|
|
{
|
|
|
|
this->devices[this->target_id]->prepare_xfer(this, bytes_in, bytes_out);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-06 00:50:19 +00:00
|
|
|
void ScsiBus::disconnect(int dev_id)
|
|
|
|
{
|
|
|
|
this->release_ctrl_lines(dev_id);
|
|
|
|
if (!(this->ctrl_lines & SCSI_CTRL_BSY) && !(this->ctrl_lines & SCSI_CTRL_SEL)) {
|
|
|
|
this->cur_phase = ScsiPhase::BUS_FREE;
|
|
|
|
change_bus_phase(dev_id);
|
|
|
|
}
|
|
|
|
}
|
2023-05-30 17:46:27 +00:00
|
|
|
|
2023-09-30 19:34:47 +00:00
|
|
|
static const DeviceDescription ScsiCurio_Descriptor = {
|
|
|
|
ScsiBus::create_ScsiCurio, {}, {}
|
2023-05-30 17:46:27 +00:00
|
|
|
};
|
|
|
|
|
2023-09-30 19:34:47 +00:00
|
|
|
static const DeviceDescription ScsiMesh_Descriptor = {
|
|
|
|
ScsiBus::create_ScsiMesh, {}, {}
|
2023-05-30 17:46:27 +00:00
|
|
|
};
|
|
|
|
|
2023-09-30 19:34:47 +00:00
|
|
|
REGISTER_DEVICE(ScsiCurio, ScsiCurio_Descriptor);
|
|
|
|
REGISTER_DEVICE(ScsiMesh, ScsiMesh_Descriptor);
|