Duckstation/src/core/cdrom.cpp

851 lines
22 KiB
C++
Raw Normal View History

2019-09-17 09:22:39 +00:00
#include "cdrom.h"
#include "YBaseLib/Log.h"
2019-09-20 10:14:00 +00:00
#include "common/cd_image.h"
2019-09-17 09:22:39 +00:00
#include "common/state_wrapper.h"
#include "dma.h"
#include "interrupt_controller.h"
2019-09-21 15:12:16 +00:00
#include "system.h"
2019-09-17 09:22:39 +00:00
Log_SetChannel(CDROM);
CDROM::CDROM() : m_sector_buffer(SECTOR_BUFFER_SIZE) {}
2019-09-17 09:22:39 +00:00
CDROM::~CDROM() = default;
2019-09-21 15:12:16 +00:00
bool CDROM::Initialize(System* system, DMA* dma, InterruptController* interrupt_controller)
2019-09-17 09:22:39 +00:00
{
2019-09-21 15:12:16 +00:00
m_system = system;
2019-09-17 09:22:39 +00:00
m_dma = dma;
m_interrupt_controller = interrupt_controller;
return true;
}
void CDROM::Reset()
2019-10-05 06:07:15 +00:00
{
if (m_media)
m_media->Seek(0);
SoftReset();
}
void CDROM::SoftReset()
2019-09-17 09:22:39 +00:00
{
2019-09-21 15:12:16 +00:00
m_command_state = CommandState::Idle;
2019-09-23 15:33:18 +00:00
m_command = Command::Sync;
m_command_stage = 0;
m_command_remaining_ticks = 0;
m_sector_read_remaining_ticks = 0;
m_reading = false;
m_muted = false;
m_status.bits = 0;
m_secondary_status.bits = 0;
2019-10-08 09:26:58 +00:00
m_mode.bits = 0;
m_setloc = {};
m_setloc_dirty = false;
m_last_sector_header = {};
m_last_sector_subheader = {};
m_interrupt_enable_register = INTERRUPT_REGISTER_MASK;
m_interrupt_flag_register = 0;
2019-09-17 09:22:39 +00:00
m_param_fifo.Clear();
m_response_fifo.Clear();
m_data_fifo.Clear();
2019-10-05 06:07:15 +00:00
m_sector_buffer.clear();
UpdateStatusRegister();
2019-09-17 09:22:39 +00:00
}
bool CDROM::DoState(StateWrapper& sw)
{
2019-09-23 15:33:18 +00:00
sw.Do(&m_command);
sw.Do(&m_command_stage);
sw.Do(&m_command_remaining_ticks);
sw.Do(&m_sector_read_remaining_ticks);
sw.Do(&m_reading);
sw.Do(&m_muted);
sw.Do(&m_setloc.minute);
sw.Do(&m_setloc.second);
sw.Do(&m_setloc.frame);
sw.Do(&m_setloc_dirty);
2019-09-21 15:12:16 +00:00
sw.Do(&m_command_state);
2019-09-17 09:22:39 +00:00
sw.Do(&m_status.bits);
sw.Do(&m_secondary_status.bits);
2019-10-08 09:26:58 +00:00
sw.Do(&m_mode.bits);
sw.DoPOD(&m_setloc);
sw.Do(&m_setloc_dirty);
sw.DoPOD(&m_last_sector_header);
sw.DoPOD(&m_last_sector_subheader);
sw.Do(&m_interrupt_enable_register);
sw.Do(&m_interrupt_flag_register);
2019-09-17 09:22:39 +00:00
sw.Do(&m_param_fifo);
sw.Do(&m_response_fifo);
sw.Do(&m_data_fifo);
2019-09-29 15:07:57 +00:00
sw.Do(&m_sector_buffer);
2019-09-23 15:33:18 +00:00
u64 media_lba = m_media ? m_media->GetCurrentLBA() : 0;
sw.Do(&m_media_filename);
sw.Do(&media_lba);
2019-09-23 15:33:18 +00:00
if (sw.IsReading())
{
if (m_command_state == CommandState::WaitForExecute)
m_system->SetDowncount(m_command_remaining_ticks);
if (m_reading)
m_system->SetDowncount(m_sector_read_remaining_ticks);
// load up media if we had something in there before
m_media.reset();
if (!m_media_filename.empty())
{
m_media = std::make_unique<CDImage>();
if (!m_media->Open(m_media_filename.c_str()) || !m_media->Seek(media_lba))
{
Log_ErrorPrintf("Failed to re-insert CD media from save state: '%s'. Ejecting.", m_media_filename.c_str());
RemoveMedia();
}
}
2019-09-23 15:33:18 +00:00
}
2019-09-17 09:22:39 +00:00
return !sw.HasError();
}
2019-09-20 10:14:00 +00:00
bool CDROM::InsertMedia(const char* filename)
{
auto media = std::make_unique<CDImage>();
if (!media->Open(filename))
{
Log_ErrorPrintf("Failed to open media at '%s'", filename);
return false;
}
if (HasMedia())
RemoveMedia();
m_media = std::move(media);
m_media_filename = filename;
2019-09-21 15:12:16 +00:00
// m_secondary_status.shell_open = false;
2019-09-20 10:14:00 +00:00
return true;
}
void CDROM::RemoveMedia()
{
if (!m_media)
return;
// TODO: Error while reading?
Log_InfoPrintf("Removing CD...");
m_media.reset();
m_media_filename.clear();
2019-09-21 15:12:16 +00:00
// m_secondary_status.shell_open = true;
2019-09-20 10:14:00 +00:00
}
2019-09-17 09:22:39 +00:00
u8 CDROM::ReadRegister(u32 offset)
{
switch (offset)
{
case 0: // status register
2019-09-30 10:01:41 +00:00
Log_TracePrintf("CDROM read status register <- 0x%08X", m_status.bits);
2019-09-17 09:22:39 +00:00
return m_status.bits;
case 1: // always response FIFO
{
if (m_response_fifo.IsEmpty())
{
Log_DebugPrintf("Response FIFO empty on read");
return 0xFF;
}
const u8 value = m_response_fifo.Pop();
UpdateStatusRegister();
2019-09-21 15:12:16 +00:00
Log_DebugPrintf("CDROM read response FIFO <- 0x%08X", ZeroExtend32(value));
return value;
}
2019-09-17 09:22:39 +00:00
case 2: // always data FIFO
{
const u8 value = m_data_fifo.Pop();
UpdateStatusRegister();
2019-09-21 15:12:16 +00:00
Log_DebugPrintf("CDROM read data FIFO <- 0x%08X", ZeroExtend32(value));
return value;
}
2019-09-17 09:22:39 +00:00
case 3:
{
switch (m_status.index)
{
case 0:
case 2:
{
const u8 value = m_interrupt_enable_register | ~INTERRUPT_REGISTER_MASK;
Log_DebugPrintf("CDROM read interrupt enable register <- 0x%02X", ZeroExtend32(value));
return value;
}
2019-09-17 09:22:39 +00:00
case 1:
case 3:
{
const u8 value = m_interrupt_flag_register | ~INTERRUPT_REGISTER_MASK;
Log_DebugPrintf("CDROM read interrupt flag register <- 0x%02X", ZeroExtend32(value));
return value;
}
2019-09-17 09:22:39 +00:00
}
}
break;
}
Log_ErrorPrintf("Unknown CDROM register read: offset=0x%02X, index=%d", offset,
ZeroExtend32(m_status.index.GetValue()));
Panic("Unknown CDROM register");
return 0;
}
void CDROM::WriteRegister(u32 offset, u8 value)
{
switch (offset)
{
case 0:
{
2019-09-30 10:01:41 +00:00
Log_TracePrintf("CDROM status register <- 0x%02X", ZeroExtend32(value));
2019-09-17 09:22:39 +00:00
m_status.bits = (m_status.bits & static_cast<u8>(~3)) | (value & u8(3));
return;
}
break;
case 1:
{
switch (m_status.index)
{
case 0:
{
Log_DebugPrintf("CDROM command register <- 0x%02X", ZeroExtend32(value));
2019-09-21 15:12:16 +00:00
if (m_command_state == CommandState::Idle)
BeginCommand(static_cast<Command>(value));
2019-09-17 09:22:39 +00:00
else
2019-09-21 15:12:16 +00:00
Log_ErrorPrintf("Ignoring write (0x%02X) to command register in non-idle state", ZeroExtend32(value));
2019-09-17 09:22:39 +00:00
return;
}
case 1:
{
Log_ErrorPrintf("Sound map data out <- 0x%02X", ZeroExtend32(value));
return;
}
case 2:
{
Log_ErrorPrintf("Sound map coding info <- 0x%02X", ZeroExtend32(value));
return;
}
case 3:
{
Log_ErrorPrintf("Audio volume for right-to-left output <- 0x%02X", ZeroExtend32(value));
return;
}
}
}
break;
case 2:
{
switch (m_status.index)
{
case 0:
{
if (m_param_fifo.IsFull())
{
Log_WarningPrintf("Parameter FIFO overflow");
m_param_fifo.RemoveOne();
}
m_param_fifo.Push(value);
UpdateStatusRegister();
2019-09-17 09:22:39 +00:00
return;
}
case 1:
{
Log_DebugPrintf("Interrupt enable register <- 0x%02X", ZeroExtend32(value));
m_interrupt_enable_register = value & INTERRUPT_REGISTER_MASK;
return;
}
case 2:
{
Log_ErrorPrintf("Audio volume for left-to-left output <- 0x%02X", ZeroExtend32(value));
return;
}
case 3:
{
Log_ErrorPrintf("Audio volume for right-to-left output <- 0x%02X", ZeroExtend32(value));
return;
}
}
}
break;
case 3:
{
switch (m_status.index)
{
case 0:
{
2019-09-23 15:33:18 +00:00
// TODO: sector buffer is not the data fifo
2019-09-21 15:12:16 +00:00
Log_DebugPrintf("Request register <- 0x%02X", value);
const RequestRegister rr{value};
Assert(!rr.SMEN);
if (rr.BFRD)
{
2019-09-30 10:01:41 +00:00
LoadDataFIFO();
}
else
{
Log_DebugPrintf("Clearing data FIFO");
m_data_fifo.Clear();
}
2019-09-21 15:12:16 +00:00
UpdateStatusRegister();
2019-09-17 09:22:39 +00:00
return;
}
case 1:
{
Log_DebugPrintf("Interrupt flag register <- 0x%02X", value);
m_interrupt_flag_register &= ~(value & INTERRUPT_REGISTER_MASK);
2019-09-21 15:12:16 +00:00
if (m_interrupt_flag_register == 0 && m_command_state == CommandState::WaitForIRQClear)
{
m_system->Synchronize();
m_command_state = CommandState::WaitForExecute;
m_system->SetDowncount(m_command_remaining_ticks);
}
// Bit 6 clears the parameter FIFO.
if (value & 0x40)
{
m_param_fifo.Clear();
UpdateStatusRegister();
}
2019-09-17 09:22:39 +00:00
return;
}
case 2:
{
Log_ErrorPrintf("Audio volume for left-to-right output <- 0x%02X", ZeroExtend32(value));
return;
}
case 3:
{
Log_ErrorPrintf("Audio volume apply changes <- 0x%02X", ZeroExtend32(value));
return;
}
}
}
break;
}
Log_ErrorPrintf("Unknown CDROM register write: offset=0x%02X, index=%d, value=0x%02X", offset,
ZeroExtend32(m_status.index.GetValue()), ZeroExtend32(value));
}
void CDROM::DMARead(u32* words, u32 word_count)
2019-09-21 15:12:16 +00:00
{
const u32 words_in_fifo = m_data_fifo.GetSize() / 4;
if (words_in_fifo < word_count)
2019-09-21 15:12:16 +00:00
{
Log_ErrorPrintf("DMA read on empty/near-empty data FIFO");
std::memset(words + words_in_fifo, 0, sizeof(u32) * (word_count - words_in_fifo));
2019-09-21 15:12:16 +00:00
}
const u32 bytes_to_read = std::min<u32>(word_count * sizeof(u32), m_data_fifo.GetSize());
m_data_fifo.PopRange(reinterpret_cast<u8*>(words), bytes_to_read);
2019-09-21 15:12:16 +00:00
}
void CDROM::SetInterrupt(Interrupt interrupt)
{
m_interrupt_flag_register = static_cast<u8>(interrupt);
if (HasPendingInterrupt())
m_interrupt_controller->InterruptRequest(InterruptController::IRQ::CDROM);
}
2019-09-24 14:41:09 +00:00
void CDROM::PushStatResponse(Interrupt interrupt /*= Interrupt::ACK*/)
{
m_response_fifo.Push(m_secondary_status.bits);
SetInterrupt(interrupt);
}
void CDROM::UpdateStatusRegister()
{
m_status.ADPBUSY = false;
m_status.PRMEMPTY = m_param_fifo.IsEmpty();
2019-09-21 15:12:16 +00:00
m_status.PRMWRDY = !m_param_fifo.IsFull();
m_status.RSLRRDY = !m_response_fifo.IsEmpty();
m_status.DRQSTS = !m_data_fifo.IsEmpty();
2019-09-23 15:33:18 +00:00
m_status.BUSYSTS = m_command_state == CommandState::WaitForExecute;
m_dma->SetRequest(DMA::Channel::CDROM, m_status.DRQSTS);
2019-09-21 15:12:16 +00:00
}
2019-10-05 06:07:15 +00:00
u32 CDROM::GetAckDelayForCommand() const
2019-09-21 15:12:16 +00:00
{
2019-10-05 06:07:15 +00:00
const u32 default_ack_delay = 20000;
if (m_command == Command::Init)
return 60000;
else
return default_ack_delay;
2019-09-21 15:12:16 +00:00
}
u32 CDROM::GetTicksForRead() const
{
return m_mode.double_speed ? (MASTER_CLOCK / 150) : (MASTER_CLOCK / 75);
}
2019-09-21 15:12:16 +00:00
void CDROM::Execute(TickCount ticks)
{
switch (m_command_state)
{
case CommandState::Idle:
case CommandState::WaitForIRQClear:
break;
case CommandState::WaitForExecute:
{
m_command_remaining_ticks -= ticks;
if (m_command_remaining_ticks <= 0)
ExecuteCommand();
else
m_system->SetDowncount(m_command_remaining_ticks);
2019-09-21 15:12:16 +00:00
}
break;
default:
UnreachableCode();
break;
}
if (m_reading)
{
m_sector_read_remaining_ticks -= ticks;
if (m_sector_read_remaining_ticks <= 0)
DoSectorRead();
else
m_system->SetDowncount(m_sector_read_remaining_ticks);
2019-09-21 15:12:16 +00:00
}
}
2019-09-21 15:12:16 +00:00
void CDROM::BeginCommand(Command command)
{
m_response_fifo.Clear();
m_system->Synchronize();
2019-09-21 15:12:16 +00:00
m_command = command;
m_command_stage = 0;
2019-10-05 06:07:15 +00:00
m_command_remaining_ticks = GetAckDelayForCommand();
if (m_command_remaining_ticks == 0)
{
ExecuteCommand();
}
else
{
m_command_state = CommandState::WaitForExecute;
m_system->SetDowncount(m_command_remaining_ticks);
UpdateStatusRegister();
}
2019-09-21 15:12:16 +00:00
}
void CDROM::NextCommandStage(bool wait_for_irq, u32 time)
{
// prevent re-execution when synchronizing below
m_command_state = CommandState::WaitForIRQClear;
m_command_remaining_ticks = time;
m_command_stage++;
2019-09-23 15:33:18 +00:00
UpdateStatusRegister();
2019-09-21 15:12:16 +00:00
if (wait_for_irq)
return;
m_system->Synchronize();
m_command_state = CommandState::WaitForExecute;
m_system->SetDowncount(m_command_remaining_ticks);
UpdateStatusRegister();
}
2019-09-17 09:22:39 +00:00
2019-09-21 15:12:16 +00:00
void CDROM::EndCommand()
2019-09-17 09:22:39 +00:00
{
2019-09-21 15:12:16 +00:00
m_param_fifo.Clear();
2019-09-21 15:12:16 +00:00
m_command_state = CommandState::Idle;
m_command = Command::Sync;
m_command_stage = 0;
m_command_remaining_ticks = 0;
UpdateStatusRegister();
}
void CDROM::ExecuteCommand()
{
Log_DevPrintf("CDROM executing command 0x%02X stage %u", ZeroExtend32(static_cast<u8>(m_command)), m_command_stage);
switch (m_command)
{
case Command::Getstat:
{
Log_DebugPrintf("CDROM Getstat command");
// if bit 0 or 2 is set, send an additional byte
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
EndCommand();
return;
}
case Command::Test:
{
const u8 subcommand = m_param_fifo.Pop();
ExecuteTestCommand(subcommand);
2019-09-21 15:12:16 +00:00
return;
}
case Command::GetID:
{
Log_DebugPrintf("CDROM GetID command - stage %u", m_command_stage);
if (m_command_stage == 0)
{
if (!HasMedia())
{
static constexpr u8 response[] = {0x11, 0x80};
m_response_fifo.PushRange(response, countof(response));
SetInterrupt(Interrupt::INT5);
EndCommand();
}
else
{
// INT3(stat), ...
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-10-05 06:07:15 +00:00
NextCommandStage(true, 18000);
2019-09-21 15:12:16 +00:00
}
}
else
{
static constexpr u8 response2[] = {0x02, 0x00, 0x20, 0x00, 0x53, 0x43, 0x45, 0x41}; // last byte is 0x49 for EU
m_response_fifo.PushRange(response2, countof(response2));
SetInterrupt(Interrupt::INT2);
EndCommand();
}
return;
}
case Command::Setloc:
{
// TODO: Verify parameter count
m_setloc.minute = BCDToDecimal(m_param_fifo.Peek(0));
m_setloc.second = BCDToDecimal(m_param_fifo.Peek(1));
m_setloc.frame = BCDToDecimal(m_param_fifo.Peek(2));
2019-09-23 15:33:18 +00:00
m_setloc_dirty = true;
2019-09-21 15:12:16 +00:00
Log_DebugPrintf("CDROM setloc command (%u, %u, %u)", ZeroExtend32(m_setloc.minute), ZeroExtend32(m_setloc.second),
ZeroExtend32(m_setloc.frame));
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
EndCommand();
return;
}
case Command::SeekL:
case Command::SeekP:
{
// TODO: Data vs audio mode
Log_DebugPrintf("CDROM seek command");
if (m_command_stage == 0)
{
2019-09-23 15:33:18 +00:00
Assert(m_setloc_dirty);
2019-09-21 15:12:16 +00:00
StopReading();
if (!m_media || !m_media->Seek(m_setloc.minute, m_setloc.second, m_setloc.frame))
2019-09-21 15:12:16 +00:00
{
Panic("Error in Setloc command");
return;
}
2019-09-23 15:33:18 +00:00
m_setloc_dirty = false;
2019-09-21 15:12:16 +00:00
m_secondary_status.motor_on = true;
m_secondary_status.seeking = true;
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-10-05 06:07:15 +00:00
NextCommandStage(false, 20000);
2019-09-21 15:12:16 +00:00
}
else
{
m_secondary_status.seeking = false;
m_response_fifo.Push(m_secondary_status.bits);
SetInterrupt(Interrupt::INT2);
EndCommand();
}
return;
}
2019-09-24 14:41:09 +00:00
case Command::Setfilter:
{
const u8 file = m_param_fifo.Peek(0);
const u8 channel = m_param_fifo.Peek(1);
Log_WarningPrintf("CDROM setfilter command 0x%02X 0x%02X", ZeroExtend32(file), ZeroExtend32(channel));
PushStatResponse(Interrupt::ACK);
EndCommand();
return;
}
2019-09-21 15:12:16 +00:00
case Command::Setmode:
{
const u8 mode = m_param_fifo.Peek(0);
Log_DebugPrintf("CDROM setmode command 0x%02X", ZeroExtend32(mode));
m_mode.bits = mode;
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
EndCommand();
return;
}
case Command::ReadN:
2019-09-24 14:41:09 +00:00
case Command::ReadS:
2019-09-21 15:12:16 +00:00
{
Log_DebugPrintf("CDROM read command");
// TODO: Seek timing and clean up...
2019-09-23 15:33:18 +00:00
if (m_setloc_dirty)
{
if (!m_media || !m_media->Seek(m_setloc.minute, m_setloc.second, m_setloc.frame))
{
Panic("Seek error");
}
2019-09-23 15:33:18 +00:00
m_setloc_dirty = false;
}
2019-09-21 15:12:16 +00:00
EndCommand();
BeginReading();
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
return;
}
case Command::Pause:
{
if (m_command_stage == 0)
{
2019-09-23 15:33:18 +00:00
const bool was_reading = m_reading;
2019-09-21 15:12:16 +00:00
Log_DebugPrintf("CDROM pause command");
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
StopReading();
2019-10-05 06:07:15 +00:00
NextCommandStage(true, was_reading ? (m_mode.double_speed ? 2000000 : 1000000) : 7000);
2019-09-21 15:12:16 +00:00
}
else
{
m_response_fifo.Push(m_secondary_status.bits);
SetInterrupt(Interrupt::INT2);
EndCommand();
}
return;
}
case Command::Init:
{
if (m_command_stage == 0)
{
Log_DebugPrintf("CDROM init command");
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
StopReading();
2019-10-05 06:07:15 +00:00
NextCommandStage(true, 8000);
}
else
{
2019-10-05 06:07:15 +00:00
m_mode.bits = 0;
m_secondary_status.bits = 0;
m_secondary_status.motor_on = true;
m_response_fifo.Push(m_secondary_status.bits);
SetInterrupt(Interrupt::INT2);
EndCommand();
}
return;
}
break;
case Command::Demute:
{
Log_DebugPrintf("CDROM demute command");
m_muted = false;
m_response_fifo.Push(m_secondary_status.bits);
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
EndCommand();
}
break;
case Command::GetlocL:
{
Log_DebugPrintf("CDROM GetlocL command");
m_response_fifo.PushRange(reinterpret_cast<const u8*>(&m_last_sector_header), sizeof(m_last_sector_header));
m_response_fifo.PushRange(reinterpret_cast<const u8*>(&m_last_sector_subheader), sizeof(m_last_sector_subheader));
SetInterrupt(Interrupt::ACK);
EndCommand();
}
break;
case Command::GetlocP:
{
// TODO: Subchannel Q support..
Log_DebugPrintf("CDROM GetlocP command");
m_response_fifo.Push(1); // track number
m_response_fifo.Push(1); // index
m_response_fifo.Push(DecimalToBCD(m_last_sector_header.minute)); // minute within track
m_response_fifo.Push(DecimalToBCD(m_last_sector_header.second)); // second within track
m_response_fifo.Push(DecimalToBCD(m_last_sector_header.frame)); // frame within track
m_response_fifo.Push(DecimalToBCD(m_last_sector_header.minute)); // minute on entire disc
m_response_fifo.Push(DecimalToBCD(m_last_sector_header.second)); // second on entire disc
m_response_fifo.Push(DecimalToBCD(m_last_sector_header.frame)); // frame on entire disc
SetInterrupt(Interrupt::ACK);
EndCommand();
}
break;
default:
Panic("Unknown command");
break;
}
}
void CDROM::ExecuteTestCommand(u8 subcommand)
{
switch (subcommand)
{
case 0x20: // Get CDROM BIOS Date/Version
{
Log_DebugPrintf("Get CDROM BIOS Date/Version");
static constexpr u8 response[] = {0x94, 0x09, 0x19, 0xC0};
m_response_fifo.PushRange(response, countof(response));
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
EndCommand();
return;
}
2019-09-20 13:40:19 +00:00
case 0x22:
{
Log_DebugPrintf("Get CDROM region ID string");
static constexpr u8 response[] = {'f', 'o', 'r', ' ', 'U', '/', 'C'};
m_response_fifo.PushRange(response, countof(response));
2019-09-24 14:41:09 +00:00
SetInterrupt(Interrupt::ACK);
2019-09-21 15:12:16 +00:00
EndCommand();
2019-09-20 13:40:19 +00:00
return;
}
default:
{
Log_ErrorPrintf("Unknown test command 0x%02X", subcommand);
return;
}
}
2019-09-17 09:22:39 +00:00
}
2019-09-21 15:12:16 +00:00
void CDROM::BeginReading()
{
Log_DebugPrintf("Starting reading");
m_secondary_status.motor_on = true;
m_secondary_status.seeking = false;
m_secondary_status.reading = true;
m_reading = true;
m_sector_read_remaining_ticks = GetTicksForRead();
2019-09-21 15:12:16 +00:00
m_system->SetDowncount(m_sector_read_remaining_ticks);
UpdateStatusRegister();
}
void CDROM::DoSectorRead()
{
if (HasPendingInterrupt())
{
// can't read with a pending interrupt?
Log_WarningPrintf("Missed sector read...");
// m_sector_read_remaining_ticks += 10;
// m_system->SetDowncount(m_sector_read_remaining_ticks);
// return;
2019-09-21 15:12:16 +00:00
}
2019-10-08 09:26:58 +00:00
if (!m_sector_buffer.empty())
{
Log_WarningPrintf("Sector buffer was not empty");
}
2019-09-21 15:12:16 +00:00
// TODO: Error handling
// TODO: Sector buffer should be two sectors?
Assert(!m_mode.ignore_bit);
m_sector_buffer.resize(RAW_SECTOR_SIZE);
2019-09-30 10:01:41 +00:00
m_media->Read(CDImage::ReadMode::RawSector, 1, m_sector_buffer.data());
std::memcpy(&m_last_sector_header, &m_sector_buffer[SECTOR_SYNC_SIZE], sizeof(m_last_sector_header));
std::memcpy(&m_last_sector_subheader, &m_sector_buffer[SECTOR_SYNC_SIZE + sizeof(m_last_sector_header)],
sizeof(m_last_sector_subheader));
Log_DevPrintf("Read sector %llu: mode %u submode 0x%02X", m_media->GetCurrentLBA(),
ZeroExtend32(m_last_sector_header.sector_mode), ZeroExtend32(m_last_sector_subheader.submode.bits));
2019-09-30 10:01:41 +00:00
bool pass_to_cpu = true;
if (m_mode.xa_enable && m_last_sector_header.sector_mode == 2)
2019-09-30 10:01:41 +00:00
{
if (m_last_sector_subheader.submode.realtime && m_last_sector_subheader.submode.audio)
2019-09-30 10:01:41 +00:00
{
// TODO: Decode audio sector. Do we still transfer this to the CPU?
Log_WarningPrintf("Decode CD-XA audio sector");
m_sector_buffer.clear();
pass_to_cpu = false;
}
2019-09-30 10:01:41 +00:00
if (m_last_sector_subheader.submode.eof)
{
Log_WarningPrintf("End of CD-XA file");
2019-09-30 10:01:41 +00:00
}
}
if (pass_to_cpu)
{
m_response_fifo.Push(m_secondary_status.bits);
SetInterrupt(Interrupt::INT1);
UpdateStatusRegister();
}
2019-09-21 15:12:16 +00:00
m_sector_read_remaining_ticks += GetTicksForRead();
2019-09-21 15:12:16 +00:00
m_system->SetDowncount(m_sector_read_remaining_ticks);
}
void CDROM::StopReading()
{
if (!m_reading)
return;
Log_DebugPrintf("Stopping reading");
m_secondary_status.reading = false;
m_reading = false;
}
2019-09-30 10:01:41 +00:00
void CDROM::LoadDataFIFO()
{
// any data to load?
if (m_sector_buffer.empty())
{
Log_DevPrintf("Attempting to load empty sector buffer");
return;
}
if (m_mode.read_raw_sector)
{
m_data_fifo.PushRange(m_sector_buffer.data() + CDImage::SECTOR_SYNC_SIZE,
CDImage::RAW_SECTOR_SIZE - CDImage::SECTOR_SYNC_SIZE);
}
else
{
m_data_fifo.PushRange(m_sector_buffer.data() + CDImage::SECTOR_SYNC_SIZE + 12, CDImage::DATA_SECTOR_SIZE);
}
Log_DebugPrintf("Loaded %u bytes to data FIFO", m_data_fifo.GetSize());
m_sector_buffer.clear();
}