2019-09-17 09:22:39 +00:00
|
|
|
#include "cdrom.h"
|
2019-09-20 10:14:00 +00:00
|
|
|
#include "common/cd_image.h"
|
2020-01-24 04:53:40 +00:00
|
|
|
#include "common/log.h"
|
2019-09-17 09:22:39 +00:00
|
|
|
#include "common/state_wrapper.h"
|
2019-10-13 04:16:49 +00:00
|
|
|
#include "dma.h"
|
2019-09-17 12:18:58 +00:00
|
|
|
#include "interrupt_controller.h"
|
2020-02-21 15:19:10 +00:00
|
|
|
#include "settings.h"
|
2019-10-15 07:27:35 +00:00
|
|
|
#include "spu.h"
|
2019-09-21 15:12:16 +00:00
|
|
|
#include "system.h"
|
2020-09-01 02:13:56 +00:00
|
|
|
#ifdef WITH_IMGUI
|
|
|
|
#include "imgui.h"
|
|
|
|
#endif
|
2019-09-17 09:22:39 +00:00
|
|
|
Log_SetChannel(CDROM);
|
|
|
|
|
2020-05-30 11:59:03 +00:00
|
|
|
struct CommandInfo
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
u8 expected_parameters;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::array<CommandInfo, 255> s_command_info = {{
|
2020-09-13 01:54:51 +00:00
|
|
|
{"Sync", 0}, {"Getstat", 0}, {"Setloc", 3}, {"Play", 0}, {"Forward", 0}, {"Backward", 0},
|
|
|
|
{"ReadN", 0}, {"MotorOn", 0}, {"Stop", 0}, {"Pause", 0}, {"Reset", 0}, {"Mute", 0},
|
|
|
|
{"Demute", 0}, {"Setfilter", 2}, {"Setmode", 1}, {"Getparam", 0}, {"GetlocL", 0}, {"GetlocP", 0},
|
|
|
|
{"SetSession", 1}, {"GetTN", 0}, {"GetTD", 1}, {"SeekL", 0}, {"SeekP", 0}, {"SetClock", 0},
|
|
|
|
{"GetClock", 0}, {"Test", 1}, {"GetID", 0}, {"ReadS", 0}, {"Init", 0}, {"GetQ", 2},
|
|
|
|
{"ReadTOC", 0}, {"VideoCD", 6}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0}, {"Unknown", 0},
|
|
|
|
{"Unknown", 0}, {"Unknown", 0}, {nullptr, 0} // Unknown
|
2020-05-30 11:59:03 +00:00
|
|
|
}};
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
CDROM g_cdrom;
|
|
|
|
|
2020-03-12 05:32:41 +00:00
|
|
|
CDROM::CDROM() = default;
|
2019-09-17 09:22:39 +00:00
|
|
|
|
|
|
|
CDROM::~CDROM() = default;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
void CDROM::Initialize()
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
2020-01-24 04:53:40 +00:00
|
|
|
m_command_event =
|
2020-07-31 07:09:18 +00:00
|
|
|
TimingEvents::CreateTimingEvent("CDROM Command Event", 1, 1, std::bind(&CDROM::ExecuteCommand, this), false);
|
|
|
|
m_drive_event = TimingEvents::CreateTimingEvent("CDROM Drive Event", 1, 1,
|
|
|
|
std::bind(&CDROM::ExecuteDrive, this, std::placeholders::_2), false);
|
2020-02-21 15:19:10 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
if (g_settings.cdrom_read_thread)
|
2020-02-21 15:19:10 +00:00
|
|
|
m_reader.StartThread();
|
2020-07-31 07:09:18 +00:00
|
|
|
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::Shutdown()
|
|
|
|
{
|
|
|
|
m_command_event.reset();
|
|
|
|
m_drive_event.reset();
|
|
|
|
m_reader.StopThread();
|
|
|
|
m_reader.RemoveMedia();
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::Reset()
|
2019-10-05 06:07:15 +00:00
|
|
|
{
|
|
|
|
SoftReset();
|
2020-04-05 14:25:56 +00:00
|
|
|
|
|
|
|
// this should be reading sector 0
|
|
|
|
m_reader.WaitForReadToComplete();
|
|
|
|
if (m_reader.GetSectorSubQ().IsCRCValid())
|
|
|
|
m_last_subq = m_reader.GetSectorSubQ();
|
2019-10-05 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::SoftReset()
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
2019-10-27 09:42:37 +00:00
|
|
|
m_command = Command::None;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_command_event->Deactivate();
|
2019-10-27 09:42:37 +00:00
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2019-09-17 12:18:58 +00:00
|
|
|
m_status.bits = 0;
|
|
|
|
m_secondary_status.bits = 0;
|
2020-05-19 16:26:06 +00:00
|
|
|
m_secondary_status.motor_on = CanReadMedia();
|
2019-10-08 09:26:58 +00:00
|
|
|
m_mode.bits = 0;
|
2020-04-10 14:40:39 +00:00
|
|
|
m_current_double_speed = false;
|
2019-10-15 07:27:35 +00:00
|
|
|
m_interrupt_enable_register = INTERRUPT_REGISTER_MASK;
|
|
|
|
m_interrupt_flag_register = 0;
|
2019-10-26 07:41:37 +00:00
|
|
|
m_pending_async_interrupt = 0;
|
2019-10-26 11:07:28 +00:00
|
|
|
m_setloc_position = {};
|
2020-05-19 15:52:53 +00:00
|
|
|
m_current_lba = 0;
|
2020-02-21 15:19:10 +00:00
|
|
|
if (m_reader.HasMedia())
|
2020-05-19 15:52:53 +00:00
|
|
|
m_reader.QueueReadSector(m_current_lba);
|
|
|
|
m_seek_start_lba = 0;
|
|
|
|
m_seek_end_lba = 0;
|
2019-10-26 11:07:28 +00:00
|
|
|
m_setloc_pending = false;
|
|
|
|
m_read_after_seek = false;
|
|
|
|
m_play_after_seek = false;
|
|
|
|
m_muted = false;
|
|
|
|
m_adpcm_muted = false;
|
2020-04-04 15:39:43 +00:00
|
|
|
m_xa_filter_file_number = 0;
|
|
|
|
m_xa_filter_channel_number = 0;
|
|
|
|
m_xa_current_file_number = 0;
|
|
|
|
m_xa_current_channel_number = 0;
|
|
|
|
m_xa_current_set = false;
|
2019-10-22 13:07:51 +00:00
|
|
|
std::memset(&m_last_sector_header, 0, sizeof(m_last_sector_header));
|
|
|
|
std::memset(&m_last_sector_subheader, 0, sizeof(m_last_sector_subheader));
|
2020-03-29 07:41:31 +00:00
|
|
|
m_last_sector_header_valid = false;
|
2019-11-10 12:45:48 +00:00
|
|
|
std::memset(&m_last_subq, 0, sizeof(m_last_subq));
|
2019-11-10 13:03:52 +00:00
|
|
|
m_last_cdda_report_frame_nibble = 0xFF;
|
2019-10-15 07:27:35 +00:00
|
|
|
|
|
|
|
m_next_cd_audio_volume_matrix[0][0] = 0x80;
|
|
|
|
m_next_cd_audio_volume_matrix[0][1] = 0x00;
|
|
|
|
m_next_cd_audio_volume_matrix[1][0] = 0x00;
|
|
|
|
m_next_cd_audio_volume_matrix[1][1] = 0x80;
|
|
|
|
m_cd_audio_volume_matrix = m_next_cd_audio_volume_matrix;
|
2020-07-21 14:03:44 +00:00
|
|
|
ResetAudioDecoder();
|
2019-10-15 07:27:35 +00:00
|
|
|
|
2019-09-17 09:22:39 +00:00
|
|
|
m_param_fifo.Clear();
|
|
|
|
m_response_fifo.Clear();
|
2019-10-26 07:41:37 +00:00
|
|
|
m_async_response_fifo.Clear();
|
2019-09-17 09:22:39 +00:00
|
|
|
m_data_fifo.Clear();
|
2020-03-12 05:32:41 +00:00
|
|
|
|
2020-03-28 15:13:25 +00:00
|
|
|
m_current_read_sector_buffer = 0;
|
|
|
|
m_current_write_sector_buffer = 0;
|
2020-03-12 05:32:41 +00:00
|
|
|
for (u32 i = 0; i < NUM_SECTOR_BUFFERS; i++)
|
|
|
|
{
|
|
|
|
m_sector_buffers[i].data.fill(0);
|
|
|
|
m_sector_buffers[i].size = 0;
|
|
|
|
}
|
2019-10-15 07:27:35 +00:00
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
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);
|
2019-10-27 09:42:37 +00:00
|
|
|
sw.Do(&m_drive_state);
|
2019-09-17 09:22:39 +00:00
|
|
|
sw.Do(&m_status.bits);
|
2019-09-17 13:04:00 +00:00
|
|
|
sw.Do(&m_secondary_status.bits);
|
2019-10-08 09:26:58 +00:00
|
|
|
sw.Do(&m_mode.bits);
|
2020-04-10 14:40:39 +00:00
|
|
|
sw.Do(&m_current_double_speed);
|
2019-10-15 07:27:35 +00:00
|
|
|
sw.Do(&m_interrupt_enable_register);
|
|
|
|
sw.Do(&m_interrupt_flag_register);
|
2019-10-26 07:41:37 +00:00
|
|
|
sw.Do(&m_pending_async_interrupt);
|
2019-10-26 11:07:28 +00:00
|
|
|
sw.DoPOD(&m_setloc_position);
|
2020-05-19 15:52:53 +00:00
|
|
|
sw.Do(&m_current_lba);
|
|
|
|
sw.Do(&m_seek_start_lba);
|
|
|
|
sw.Do(&m_seek_end_lba);
|
2019-10-26 11:07:28 +00:00
|
|
|
sw.Do(&m_setloc_pending);
|
|
|
|
sw.Do(&m_read_after_seek);
|
|
|
|
sw.Do(&m_play_after_seek);
|
|
|
|
sw.Do(&m_muted);
|
|
|
|
sw.Do(&m_adpcm_muted);
|
2020-04-04 15:39:43 +00:00
|
|
|
sw.Do(&m_xa_filter_file_number);
|
|
|
|
sw.Do(&m_xa_filter_channel_number);
|
|
|
|
sw.Do(&m_xa_current_file_number);
|
|
|
|
sw.Do(&m_xa_current_channel_number);
|
|
|
|
sw.Do(&m_xa_current_set);
|
2019-10-22 13:07:51 +00:00
|
|
|
sw.DoBytes(&m_last_sector_header, sizeof(m_last_sector_header));
|
|
|
|
sw.DoBytes(&m_last_sector_subheader, sizeof(m_last_sector_subheader));
|
2020-03-29 07:41:31 +00:00
|
|
|
sw.Do(&m_last_sector_header_valid);
|
2019-11-10 12:45:48 +00:00
|
|
|
sw.DoBytes(&m_last_subq, sizeof(m_last_subq));
|
2019-11-10 13:03:52 +00:00
|
|
|
sw.Do(&m_last_cdda_report_frame_nibble);
|
2019-11-13 06:33:49 +00:00
|
|
|
sw.Do(&m_play_track_number_bcd);
|
2020-03-07 05:10:19 +00:00
|
|
|
sw.Do(&m_async_command_parameter);
|
2019-10-15 07:27:35 +00:00
|
|
|
sw.Do(&m_cd_audio_volume_matrix);
|
|
|
|
sw.Do(&m_next_cd_audio_volume_matrix);
|
|
|
|
sw.Do(&m_xa_last_samples);
|
|
|
|
sw.Do(&m_xa_resample_ring_buffer);
|
|
|
|
sw.Do(&m_xa_resample_p);
|
|
|
|
sw.Do(&m_xa_resample_sixstep);
|
2019-09-17 09:22:39 +00:00
|
|
|
sw.Do(&m_param_fifo);
|
|
|
|
sw.Do(&m_response_fifo);
|
2019-10-26 07:41:37 +00:00
|
|
|
sw.Do(&m_async_response_fifo);
|
2019-09-17 09:22:39 +00:00
|
|
|
sw.Do(&m_data_fifo);
|
2020-03-12 05:32:41 +00:00
|
|
|
|
2020-03-28 15:13:25 +00:00
|
|
|
sw.Do(&m_current_read_sector_buffer);
|
|
|
|
sw.Do(&m_current_write_sector_buffer);
|
2020-03-12 05:32:41 +00:00
|
|
|
for (u32 i = 0; i < NUM_SECTOR_BUFFERS; i++)
|
|
|
|
{
|
|
|
|
sw.Do(&m_sector_buffers[i].data);
|
|
|
|
sw.Do(&m_sector_buffers[i].size);
|
|
|
|
}
|
2019-09-23 15:33:18 +00:00
|
|
|
|
2020-07-21 14:03:44 +00:00
|
|
|
sw.Do(&m_audio_fifo);
|
|
|
|
|
2020-05-12 14:13:57 +00:00
|
|
|
u32 requested_sector = (sw.IsWriting() ? (m_reader.WaitForReadToComplete(), m_reader.GetLastReadSector()) : 0);
|
|
|
|
sw.Do(&requested_sector);
|
|
|
|
|
2019-09-23 15:33:18 +00:00
|
|
|
if (sw.IsReading())
|
|
|
|
{
|
2020-02-21 15:19:10 +00:00
|
|
|
if (m_reader.HasMedia())
|
2020-05-12 14:13:57 +00:00
|
|
|
m_reader.QueueReadSector(requested_sector);
|
2020-01-24 04:53:40 +00:00
|
|
|
UpdateCommandEvent();
|
|
|
|
m_drive_event->SetState(!IsDriveIdle());
|
2019-09-23 15:33:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 09:22:39 +00:00
|
|
|
return !sw.HasError();
|
|
|
|
}
|
|
|
|
|
2019-11-16 05:27:57 +00:00
|
|
|
void CDROM::InsertMedia(std::unique_ptr<CDImage> media)
|
2019-09-20 10:14:00 +00:00
|
|
|
{
|
2020-05-19 16:26:06 +00:00
|
|
|
if (CanReadMedia())
|
2019-09-20 10:14:00 +00:00
|
|
|
RemoveMedia();
|
|
|
|
|
2020-03-12 03:51:53 +00:00
|
|
|
// set the region from the system area of the disc
|
2020-09-01 02:29:22 +00:00
|
|
|
m_disc_region = System::GetRegionForImage(media.get());
|
2020-03-12 03:51:53 +00:00
|
|
|
Log_InfoPrintf("Inserting new media, disc region: %s, console region: %s", Settings::GetDiscRegionName(m_disc_region),
|
2020-07-31 07:09:18 +00:00
|
|
|
Settings::GetConsoleRegionName(System::GetRegion()));
|
2020-03-12 03:51:53 +00:00
|
|
|
|
2020-03-28 15:15:04 +00:00
|
|
|
// motor automatically spins up
|
2020-05-19 16:26:06 +00:00
|
|
|
if (m_drive_state != DriveState::ShellOpening)
|
|
|
|
m_secondary_status.motor_on = true;
|
2020-03-28 15:15:04 +00:00
|
|
|
|
2020-03-29 07:41:31 +00:00
|
|
|
// reading TOC? interestingly this doesn't work for GetlocL though...
|
2020-04-05 14:25:56 +00:00
|
|
|
CDImage::SubChannelQ subq;
|
|
|
|
if (media->Seek(0) && media->ReadSubChannelQ(&subq) && subq.IsCRCValid())
|
|
|
|
m_last_subq = subq;
|
2020-03-29 07:41:31 +00:00
|
|
|
|
2020-02-21 15:19:10 +00:00
|
|
|
m_reader.SetMedia(std::move(media));
|
2019-09-20 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 14:02:37 +00:00
|
|
|
std::unique_ptr<CDImage> CDROM::RemoveMedia(bool force /* = false */)
|
2019-09-20 10:14:00 +00:00
|
|
|
{
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!HasMedia() && !force)
|
2020-07-21 14:02:37 +00:00
|
|
|
return nullptr;
|
2019-09-20 10:14:00 +00:00
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
const TickCount stop_ticks = GetTicksForStop(true);
|
|
|
|
|
2019-09-20 10:14:00 +00:00
|
|
|
Log_InfoPrintf("Removing CD...");
|
2020-07-21 14:02:37 +00:00
|
|
|
std::unique_ptr<CDImage> image = m_reader.RemoveMedia();
|
2019-10-28 07:34:46 +00:00
|
|
|
|
2020-03-29 07:41:31 +00:00
|
|
|
m_last_sector_header_valid = false;
|
|
|
|
|
2020-03-28 15:15:04 +00:00
|
|
|
m_secondary_status.motor_on = false;
|
2019-10-28 07:34:46 +00:00
|
|
|
m_secondary_status.shell_open = true;
|
2020-03-28 15:15:04 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
2020-03-12 03:51:53 +00:00
|
|
|
m_disc_region = DiscRegion::Other;
|
2019-10-28 07:34:46 +00:00
|
|
|
|
|
|
|
// If the drive was doing anything, we need to abort the command.
|
2020-03-28 15:15:04 +00:00
|
|
|
m_drive_state = DriveState::Idle;
|
|
|
|
m_command = Command::None;
|
|
|
|
m_command_event->Deactivate();
|
|
|
|
m_drive_event->Deactivate();
|
|
|
|
|
|
|
|
// The console sends an interrupt when the shell is opened regardless of whether a command was executing.
|
2020-07-13 15:26:37 +00:00
|
|
|
if (HasPendingAsyncInterrupt())
|
|
|
|
ClearAsyncInterrupt();
|
2020-03-28 15:15:04 +00:00
|
|
|
SendAsyncErrorResponse(STAT_ERROR, 0x08);
|
2020-05-19 16:26:06 +00:00
|
|
|
|
|
|
|
// Begin spin-down timer, we can't swap the new disc in immediately for some games (e.g. Metal Gear Solid).
|
|
|
|
if (!force)
|
|
|
|
{
|
|
|
|
m_drive_state = DriveState::ShellOpening;
|
|
|
|
m_drive_event->SetIntervalAndSchedule(stop_ticks);
|
|
|
|
}
|
2020-07-21 14:02:37 +00:00
|
|
|
|
|
|
|
return image;
|
2019-09-20 10:14:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 15:19:10 +00:00
|
|
|
void CDROM::SetUseReadThread(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled == m_reader.IsUsingThread())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
m_reader.StartThread();
|
|
|
|
else
|
|
|
|
m_reader.StopThread();
|
|
|
|
}
|
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
void CDROM::CPUClockChanged()
|
|
|
|
{
|
|
|
|
// reschedule the disc read event
|
|
|
|
if (IsReadingOrPlaying())
|
|
|
|
m_drive_event->SetInterval(GetTicksForRead());
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:22:39 +00:00
|
|
|
u8 CDROM::ReadRegister(u32 offset)
|
|
|
|
{
|
|
|
|
switch (offset)
|
|
|
|
{
|
|
|
|
case 0: // status register
|
2020-03-31 12:28:48 +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
|
2019-09-17 12:18:58 +00:00
|
|
|
{
|
2019-09-29 15:08:17 +00:00
|
|
|
if (m_response_fifo.IsEmpty())
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Response FIFO empty on read");
|
|
|
|
return 0xFF;
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
const u8 value = m_response_fifo.Pop();
|
|
|
|
UpdateStatusRegister();
|
2020-03-31 12:28:48 +00:00
|
|
|
Log_DebugPrintf("CDROM read response FIFO -> 0x%08X", ZeroExtend32(value));
|
2019-09-17 12:18:58 +00:00
|
|
|
return value;
|
|
|
|
}
|
2019-09-17 09:22:39 +00:00
|
|
|
|
|
|
|
case 2: // always data FIFO
|
2019-09-17 12:18:58 +00:00
|
|
|
{
|
|
|
|
const u8 value = m_data_fifo.Pop();
|
|
|
|
UpdateStatusRegister();
|
2020-03-31 12:28:48 +00:00
|
|
|
Log_DebugPrintf("CDROM read data FIFO -> 0x%08X", ZeroExtend32(value));
|
2019-09-17 12:18:58 +00:00
|
|
|
return value;
|
|
|
|
}
|
2019-09-17 09:22:39 +00:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
if (m_status.index & 1)
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
const u8 value = m_interrupt_flag_register | ~INTERRUPT_REGISTER_MASK;
|
2020-03-31 12:28:48 +00:00
|
|
|
Log_DebugPrintf("CDROM read interrupt flag register -> 0x%02X", ZeroExtend32(value));
|
2020-03-18 14:28:41 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const u8 value = m_interrupt_enable_register | ~INTERRUPT_REGISTER_MASK;
|
2020-03-31 12:28:48 +00:00
|
|
|
Log_DebugPrintf("CDROM read interrupt enable register -> 0x%02X", ZeroExtend32(value));
|
2020-03-18 14:28:41 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
Log_TracePrintf("CDROM status register <- 0x%02X", value);
|
|
|
|
m_status.bits = (m_status.bits & static_cast<u8>(~3)) | (value & u8(3));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 reg = (m_status.index * 3u) + (offset - 1);
|
|
|
|
switch (reg)
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2020-05-30 16:41:42 +00:00
|
|
|
Log_DebugPrintf("CDROM command register <- 0x%02X (%s)", value, s_command_info[value].name);
|
2020-03-18 14:28:41 +00:00
|
|
|
BeginCommand(static_cast<Command>(value));
|
2019-09-17 09:22:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
if (m_param_fifo.IsFull())
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
Log_WarningPrintf("Parameter FIFO overflow");
|
|
|
|
m_param_fifo.RemoveOne();
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
2020-03-18 14:28:41 +00:00
|
|
|
|
|
|
|
m_param_fifo.Push(value);
|
|
|
|
UpdateStatusRegister();
|
|
|
|
return;
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
Log_DebugPrintf("Request register <- 0x%02X", value);
|
|
|
|
const RequestRegister rr{value};
|
2020-05-27 17:07:32 +00:00
|
|
|
|
|
|
|
// Sound map is not currently implemented, haven't found anything which uses it.
|
|
|
|
if (rr.SMEN)
|
|
|
|
Log_ErrorPrintf("Sound map enable set");
|
|
|
|
if (rr.BFWR)
|
|
|
|
Log_ErrorPrintf("Buffer write enable set");
|
|
|
|
|
2020-03-18 14:28:41 +00:00
|
|
|
if (rr.BFRD)
|
|
|
|
{
|
|
|
|
LoadDataFIFO();
|
|
|
|
}
|
|
|
|
else
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
Log_DebugPrintf("Clearing data FIFO");
|
|
|
|
m_data_fifo.Clear();
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
2020-03-18 14:28:41 +00:00
|
|
|
|
|
|
|
UpdateStatusRegister();
|
|
|
|
return;
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
Log_ErrorPrintf("Sound map data out <- 0x%02X", value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Interrupt enable register <- 0x%02X", value);
|
|
|
|
m_interrupt_enable_register = value & INTERRUPT_REGISTER_MASK;
|
|
|
|
UpdateInterruptRequest();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Interrupt flag register <- 0x%02X", value);
|
|
|
|
m_interrupt_flag_register &= ~(value & INTERRUPT_REGISTER_MASK);
|
|
|
|
if (m_interrupt_flag_register == 0)
|
|
|
|
{
|
|
|
|
if (HasPendingAsyncInterrupt())
|
|
|
|
DeliverAsyncInterrupt();
|
|
|
|
else
|
|
|
|
UpdateCommandEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bit 6 clears the parameter FIFO.
|
|
|
|
if (value & 0x40)
|
2019-09-17 09:22:39 +00:00
|
|
|
{
|
2020-03-18 14:28:41 +00:00
|
|
|
m_param_fifo.Clear();
|
|
|
|
UpdateStatusRegister();
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
2020-03-18 14:28:41 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Sound map coding info <- 0x%02X", value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Audio volume for left-to-left output <- 0x%02X", value);
|
|
|
|
m_next_cd_audio_volume_matrix[0][0] = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Audio volume for left-to-right output <- 0x%02X", value);
|
|
|
|
m_next_cd_audio_volume_matrix[0][1] = value;
|
|
|
|
return;
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 14:28:41 +00:00
|
|
|
case 9:
|
|
|
|
{
|
2020-03-18 14:28:57 +00:00
|
|
|
Log_DebugPrintf("Audio volume for right-to-right output <- 0x%02X", value);
|
|
|
|
m_next_cd_audio_volume_matrix[1][1] = value;
|
2020-03-18 14:28:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 10:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Audio volume for right-to-left output <- 0x%02X", value);
|
|
|
|
m_next_cd_audio_volume_matrix[1][0] = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Audio volume apply changes <- 0x%02X", value);
|
|
|
|
m_adpcm_muted = ConvertToBoolUnchecked(value & u8(0x01));
|
|
|
|
if (value & 0x20)
|
|
|
|
m_cd_audio_volume_matrix = m_next_cd_audio_volume_matrix;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Unknown CDROM register write: offset=0x%02X, index=%d, reg=%u, value=0x%02X", offset,
|
|
|
|
m_status.index.GetValue(), reg, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 06:48:11 +00:00
|
|
|
void CDROM::DMARead(u32* words, u32 word_count)
|
2019-09-21 15:12:16 +00:00
|
|
|
{
|
2019-10-13 06:48:11 +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
|
|
|
{
|
2019-10-13 06:48:11 +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
|
|
|
}
|
|
|
|
|
2019-10-13 06:48:11 +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
|
|
|
}
|
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
void CDROM::SetInterrupt(Interrupt interrupt)
|
|
|
|
{
|
|
|
|
m_interrupt_flag_register = static_cast<u8>(interrupt);
|
2019-11-08 13:48:09 +00:00
|
|
|
UpdateInterruptRequest();
|
2019-09-17 12:18:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 07:41:37 +00:00
|
|
|
void CDROM::SetAsyncInterrupt(Interrupt interrupt)
|
|
|
|
{
|
2020-03-23 14:21:22 +00:00
|
|
|
if (m_interrupt_flag_register == static_cast<u8>(interrupt))
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Not setting async interrupt %u because there is already one unacknowledged",
|
|
|
|
static_cast<u8>(interrupt));
|
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-26 07:41:37 +00:00
|
|
|
Assert(m_pending_async_interrupt == 0);
|
|
|
|
m_pending_async_interrupt = static_cast<u8>(interrupt);
|
|
|
|
if (!HasPendingInterrupt())
|
|
|
|
DeliverAsyncInterrupt();
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:31:59 +00:00
|
|
|
void CDROM::ClearAsyncInterrupt()
|
2019-10-26 12:34:05 +00:00
|
|
|
{
|
|
|
|
m_pending_async_interrupt = 0;
|
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
}
|
|
|
|
|
2019-10-26 07:41:37 +00:00
|
|
|
void CDROM::DeliverAsyncInterrupt()
|
|
|
|
{
|
|
|
|
Assert(m_pending_async_interrupt != 0 && !HasPendingInterrupt());
|
2020-05-11 12:55:13 +00:00
|
|
|
Log_DebugPrintf("Delivering async interrupt %u", m_pending_async_interrupt);
|
2019-10-26 07:41:37 +00:00
|
|
|
|
2020-05-15 14:33:01 +00:00
|
|
|
if (m_pending_async_interrupt == static_cast<u8>(Interrupt::DataReady))
|
|
|
|
m_current_read_sector_buffer = m_current_write_sector_buffer;
|
|
|
|
|
2019-10-26 07:41:37 +00:00
|
|
|
m_response_fifo.Clear();
|
|
|
|
m_response_fifo.PushFromQueue(&m_async_response_fifo);
|
|
|
|
m_interrupt_flag_register = m_pending_async_interrupt;
|
|
|
|
m_pending_async_interrupt = 0;
|
2019-11-08 13:48:09 +00:00
|
|
|
UpdateInterruptRequest();
|
2019-11-13 06:33:49 +00:00
|
|
|
UpdateStatusRegister();
|
2020-01-24 04:53:40 +00:00
|
|
|
UpdateCommandEvent();
|
2019-10-26 07:41:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 06:12:37 +00:00
|
|
|
void CDROM::SendACKAndStat()
|
2019-09-24 14:41:09 +00:00
|
|
|
{
|
|
|
|
m_response_fifo.Push(m_secondary_status.bits);
|
2019-10-26 06:12:37 +00:00
|
|
|
SetInterrupt(Interrupt::ACK);
|
2019-09-24 14:41:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 05:53:53 +00:00
|
|
|
void CDROM::SendErrorResponse(u8 stat_bits /* = STAT_ERROR */, u8 reason /* = 0x80 */)
|
2019-10-18 12:44:28 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
m_response_fifo.Push(m_secondary_status.bits | stat_bits);
|
2019-10-18 12:44:28 +00:00
|
|
|
m_response_fifo.Push(reason);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetInterrupt(Interrupt::Error);
|
2019-10-18 12:44:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 05:53:53 +00:00
|
|
|
void CDROM::SendAsyncErrorResponse(u8 stat_bits /* = STAT_ERROR */, u8 reason /* = 0x80 */)
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits | stat_bits);
|
2019-10-26 11:07:28 +00:00
|
|
|
m_async_response_fifo.Push(reason);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::Error);
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
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();
|
2019-09-17 12:18:58 +00:00
|
|
|
m_status.RSLRRDY = !m_response_fifo.IsEmpty();
|
|
|
|
m_status.DRQSTS = !m_data_fifo.IsEmpty();
|
2019-10-27 09:42:37 +00:00
|
|
|
m_status.BUSYSTS = HasPendingCommand();
|
2019-10-13 04:16:49 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_dma.SetRequest(DMA::Channel::CDROM, m_status.DRQSTS);
|
2019-09-21 15:12:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 13:48:09 +00:00
|
|
|
void CDROM::UpdateInterruptRequest()
|
|
|
|
{
|
|
|
|
if ((m_interrupt_flag_register & m_interrupt_enable_register) == 0)
|
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_interrupt_controller.InterruptRequest(InterruptController::IRQ::CDROM);
|
2019-11-08 13:48:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 14:40:39 +00:00
|
|
|
TickCount CDROM::GetAckDelayForCommand(Command command)
|
2019-09-21 15:12:16 +00:00
|
|
|
{
|
2020-03-31 12:28:48 +00:00
|
|
|
if (command == Command::Init)
|
|
|
|
{
|
|
|
|
// Init takes longer.
|
|
|
|
return 120000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests show that the average time to acknowledge a command is significantly higher when a disc is in the drive,
|
|
|
|
// presumably because the controller is busy doing discy-things.
|
2020-04-29 15:36:05 +00:00
|
|
|
constexpr u32 default_ack_delay_no_disc = 15000;
|
|
|
|
constexpr u32 default_ack_delay_with_disc = 25000;
|
2020-05-19 16:26:06 +00:00
|
|
|
return CanReadMedia() ? default_ack_delay_with_disc : default_ack_delay_no_disc;
|
2019-09-21 15:12:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 14:40:39 +00:00
|
|
|
TickCount CDROM::GetTicksForRead()
|
2019-09-23 13:31:51 +00:00
|
|
|
{
|
2020-09-29 13:29:28 +00:00
|
|
|
const TickCount tps = System::GetTicksPerSecond();
|
2020-10-04 14:05:14 +00:00
|
|
|
|
|
|
|
if (g_settings.cdrom_read_speedup > 1 && !m_mode.cdda && !m_mode.xa_enable && m_mode.double_speed)
|
|
|
|
return tps / (150 * g_settings.cdrom_read_speedup);
|
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
return m_mode.double_speed ? (tps / 150) : (tps / 75);
|
2019-09-23 13:31:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 14:40:39 +00:00
|
|
|
TickCount CDROM::GetTicksForSeek(CDImage::LBA new_lba)
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
2020-09-29 13:29:28 +00:00
|
|
|
const TickCount tps = System::GetTicksPerSecond();
|
2020-05-19 15:52:53 +00:00
|
|
|
const CDImage::LBA current_lba = m_secondary_status.motor_on ? m_current_lba : 0;
|
2019-10-26 11:07:28 +00:00
|
|
|
const u32 lba_diff = static_cast<u32>((new_lba > current_lba) ? (new_lba - current_lba) : (current_lba - new_lba));
|
|
|
|
|
2019-11-10 05:01:18 +00:00
|
|
|
// Formula from Mednafen.
|
2020-06-01 12:20:26 +00:00
|
|
|
TickCount ticks = std::max<TickCount>(
|
|
|
|
20000, static_cast<u32>(
|
2020-09-29 13:29:28 +00:00
|
|
|
((static_cast<u64>(lba_diff) * static_cast<u64>(tps) * static_cast<u64>(1000)) / (72 * 60 * 75)) / 1000));
|
2019-11-10 05:01:18 +00:00
|
|
|
if (!m_secondary_status.motor_on)
|
2020-09-29 13:29:28 +00:00
|
|
|
ticks += tps;
|
2019-11-10 05:01:18 +00:00
|
|
|
if (lba_diff >= 2550)
|
2020-09-29 13:29:28 +00:00
|
|
|
ticks += static_cast<TickCount>((u64(tps) * 300) / 1000);
|
2020-09-19 12:09:30 +00:00
|
|
|
else
|
2020-06-01 12:20:26 +00:00
|
|
|
{
|
|
|
|
// When paused, the CDC seems to keep reading the disc until it hits the position it's set to, then skip 10-15
|
|
|
|
// sectors back (depending on how far into the disc it is). We'll be generous and use 4 sectors, since on average
|
|
|
|
// it's probably closer.
|
|
|
|
ticks += GetTicksForRead() * 4u;
|
|
|
|
}
|
2020-04-10 14:40:39 +00:00
|
|
|
|
|
|
|
if (m_mode.double_speed != m_current_double_speed)
|
|
|
|
{
|
|
|
|
Log_DevPrintf("Switched from %s to %s speed", m_current_double_speed ? "double" : "single",
|
|
|
|
m_mode.double_speed ? "double" : "single");
|
|
|
|
m_current_double_speed = m_mode.double_speed;
|
|
|
|
|
|
|
|
// Approximate time for the motor to change speed?
|
2020-09-29 13:29:28 +00:00
|
|
|
ticks += static_cast<u32>(static_cast<double>(tps) * 0.1);
|
2020-04-10 14:40:39 +00:00
|
|
|
}
|
2019-11-10 05:01:18 +00:00
|
|
|
|
|
|
|
Log_DevPrintf("Seek time for %u LBAs: %d", lba_diff, ticks);
|
2019-10-26 11:07:28 +00:00
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
TickCount CDROM::GetTicksForStop(bool motor_was_on)
|
|
|
|
{
|
|
|
|
return motor_was_on ? (m_mode.double_speed ? 25000000 : 13000000) : 7000;
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:59:11 +00:00
|
|
|
CDImage::LBA CDROM::GetNextSectorToBeRead()
|
|
|
|
{
|
|
|
|
if (!IsReadingOrPlaying())
|
|
|
|
return m_current_lba;
|
|
|
|
|
|
|
|
m_reader.WaitForReadToComplete();
|
|
|
|
return m_reader.GetLastReadSector();
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
void CDROM::BeginCommand(Command command)
|
|
|
|
{
|
2020-05-30 12:04:12 +00:00
|
|
|
TickCount ack_delay = GetAckDelayForCommand(command);
|
|
|
|
|
|
|
|
if (HasPendingCommand())
|
|
|
|
{
|
|
|
|
// The behavior here is kinda.. interesting. Some commands seem to take precedence over others, for example
|
|
|
|
// sending a Nop command followed by a GetlocP will return the GetlocP response, and the same for the inverse.
|
|
|
|
// However, other combinations result in strange behavior, for example sending a Setloc followed by a ReadN will
|
|
|
|
// fail with ERROR_REASON_INCORRECT_NUMBER_OF_PARAMETERS. This particular example happens in Voice Idol
|
|
|
|
// Collection - Pool Bar Story, and the loading time is lengthened as well as audio slowing down if this
|
|
|
|
// behavior is not correct. So, let's use a heuristic; if the number of parameters of the "old" command is
|
|
|
|
// greater than the "new" command, empty the FIFO, which will return the error when the command executes.
|
|
|
|
// Otherwise, override the command with the new one.
|
|
|
|
if (s_command_info[static_cast<u8>(m_command)].expected_parameters >
|
|
|
|
s_command_info[static_cast<u8>(command)].expected_parameters)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Ignoring command 0x%02X (%s) and emptying FIFO as 0x%02x (%s) is still pending",
|
|
|
|
static_cast<u8>(command), s_command_info[static_cast<u8>(command)].name,
|
|
|
|
static_cast<u8>(m_command), s_command_info[static_cast<u8>(m_command)].name);
|
|
|
|
m_param_fifo.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_WarningPrintf("Cancelling pending command 0x%02X (%s) for new command 0x%02X (%s)", static_cast<u8>(m_command),
|
|
|
|
s_command_info[static_cast<u8>(m_command)].name, static_cast<u8>(command),
|
|
|
|
s_command_info[static_cast<u8>(command)].name);
|
|
|
|
|
|
|
|
// subtract the currently-elapsed ack ticks from the new command
|
|
|
|
const TickCount elapsed_ticks = m_command_event->GetInterval() - m_command_event->GetTicksUntilNextExecution();
|
|
|
|
ack_delay = std::max(ack_delay - elapsed_ticks, 1);
|
|
|
|
m_command_event->Deactivate();
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
m_command = command;
|
2020-05-30 12:04:12 +00:00
|
|
|
m_command_event->SetIntervalAndSchedule(ack_delay);
|
2020-01-24 04:53:40 +00:00
|
|
|
UpdateCommandEvent();
|
2019-09-21 15:12:16 +00:00
|
|
|
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-17 12:18:58 +00:00
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
m_command = Command::None;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_command_event->Deactivate();
|
2019-09-21 15:12:16 +00:00
|
|
|
UpdateStatusRegister();
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:28:48 +00:00
|
|
|
void CDROM::AbortCommand()
|
|
|
|
{
|
|
|
|
m_command = Command::None;
|
|
|
|
m_command_event->Deactivate();
|
|
|
|
UpdateStatusRegister();
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
void CDROM::ExecuteCommand()
|
|
|
|
{
|
2020-05-30 11:59:03 +00:00
|
|
|
const CommandInfo& ci = s_command_info[static_cast<u8>(m_command)];
|
|
|
|
Log_DevPrintf("CDROM executing command 0x%02X (%s)", static_cast<u8>(m_command), ci.name);
|
|
|
|
if (m_param_fifo.GetSize() < ci.expected_parameters)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Too few parameters for command 0x%02X (%s), expecting %u got %u", static_cast<u8>(m_command),
|
|
|
|
ci.name, ci.expected_parameters, m_param_fifo.GetSize());
|
|
|
|
SendErrorResponse(STAT_ERROR, ERROR_REASON_INCORRECT_NUMBER_OF_PARAMETERS);
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
2019-09-21 15:12:16 +00:00
|
|
|
|
2019-11-08 12:42:31 +00:00
|
|
|
if (!m_response_fifo.IsEmpty())
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Response FIFO not empty on command begin");
|
|
|
|
m_response_fifo.Clear();
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
switch (m_command)
|
2019-09-17 12:18:58 +00:00
|
|
|
{
|
|
|
|
case Command::Getstat:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM Getstat command");
|
|
|
|
|
|
|
|
// if bit 0 or 2 is set, send an additional byte
|
2019-10-26 06:12:37 +00:00
|
|
|
SendACKAndStat();
|
2019-10-28 07:34:46 +00:00
|
|
|
|
|
|
|
// shell open bit is cleared after sending the status
|
2020-05-19 16:26:06 +00:00
|
|
|
if (CanReadMedia())
|
2019-10-28 07:34:46 +00:00
|
|
|
m_secondary_status.shell_open = false;
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
EndCommand();
|
|
|
|
return;
|
2019-09-17 12:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case Command::Test:
|
|
|
|
{
|
|
|
|
const u8 subcommand = m_param_fifo.Pop();
|
|
|
|
ExecuteTestCommand(subcommand);
|
2019-09-21 15:12:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Command::GetID:
|
|
|
|
{
|
2019-10-27 09:42:37 +00:00
|
|
|
Log_DebugPrintf("CDROM GetID command");
|
2020-03-12 03:51:53 +00:00
|
|
|
SendACKAndStat();
|
2019-10-27 09:42:37 +00:00
|
|
|
|
2020-03-12 03:51:53 +00:00
|
|
|
m_drive_state = DriveState::ReadingID;
|
2020-04-10 14:40:39 +00:00
|
|
|
m_drive_event->Schedule(GetTicksForSeek(0) + GetTicksForRead());
|
2019-09-21 15:12:16 +00:00
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
EndCommand();
|
2019-09-21 15:12:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-08 14:21:11 +00:00
|
|
|
case Command::ReadTOC:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM ReadTOC command");
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2019-11-08 14:21:11 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2019-11-08 14:21:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendACKAndStat();
|
|
|
|
|
|
|
|
m_drive_state = DriveState::ReadingTOC;
|
2020-09-29 13:29:28 +00:00
|
|
|
m_drive_event->Schedule(System::GetTicksPerSecond() / 2); // half a second
|
2019-11-08 14:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-26 11:07:28 +00:00
|
|
|
case Command::Setfilter:
|
|
|
|
{
|
|
|
|
const u8 file = m_param_fifo.Peek(0);
|
|
|
|
const u8 channel = m_param_fifo.Peek(1);
|
|
|
|
Log_DebugPrintf("CDROM setfilter command 0x%02X 0x%02X", ZeroExtend32(file), ZeroExtend32(channel));
|
2020-04-04 15:39:43 +00:00
|
|
|
m_xa_filter_file_number = file;
|
|
|
|
m_xa_filter_channel_number = channel;
|
2020-05-12 14:14:17 +00:00
|
|
|
m_xa_current_set = false;
|
2019-10-26 11:07:28 +00:00
|
|
|
SendACKAndStat();
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Command::Setmode:
|
|
|
|
{
|
|
|
|
const u8 mode = m_param_fifo.Peek(0);
|
|
|
|
Log_DebugPrintf("CDROM setmode command 0x%02X", ZeroExtend32(mode));
|
|
|
|
|
|
|
|
m_mode.bits = mode;
|
|
|
|
SendACKAndStat();
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
case Command::Setloc:
|
|
|
|
{
|
2019-12-06 06:23:08 +00:00
|
|
|
m_setloc_position.minute = PackedBCDToBinary(m_param_fifo.Peek(0));
|
|
|
|
m_setloc_position.second = PackedBCDToBinary(m_param_fifo.Peek(1));
|
|
|
|
m_setloc_position.frame = PackedBCDToBinary(m_param_fifo.Peek(2));
|
2019-10-26 11:07:28 +00:00
|
|
|
m_setloc_pending = true;
|
2019-10-15 07:25:01 +00:00
|
|
|
Log_DebugPrintf("CDROM setloc command (%02X, %02X, %02X)", ZeroExtend32(m_param_fifo.Peek(0)),
|
|
|
|
ZeroExtend32(m_param_fifo.Peek(1)), ZeroExtend32(m_param_fifo.Peek(2)));
|
2019-10-26 06:12:37 +00:00
|
|
|
SendACKAndStat();
|
2019-09-21 15:12:16 +00:00
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Command::SeekL:
|
|
|
|
case Command::SeekP:
|
|
|
|
{
|
2019-11-16 02:54:41 +00:00
|
|
|
const bool logical = (m_command == Command::SeekL);
|
|
|
|
Log_DebugPrintf("CDROM %s command", logical ? "SeekL" : "SeekP");
|
2020-07-10 10:55:00 +00:00
|
|
|
|
|
|
|
if (IsSeeking())
|
|
|
|
UpdatePositionWhileSeeking();
|
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2019-09-21 15:12:16 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2019-09-21 15:12:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-10-27 04:38:48 +00:00
|
|
|
SendACKAndStat();
|
2019-11-16 02:54:41 +00:00
|
|
|
BeginSeeking(logical, false, false);
|
2019-09-21 15:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-07 05:10:19 +00:00
|
|
|
case Command::SetSession:
|
|
|
|
{
|
2020-05-30 11:59:03 +00:00
|
|
|
const u8 session = m_param_fifo.Peek(0);
|
2020-03-07 05:10:19 +00:00
|
|
|
Log_DebugPrintf("CDROM SetSession command, session=%u", session);
|
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia() || m_drive_state == DriveState::Reading || m_drive_state == DriveState::Playing)
|
2020-03-07 05:10:19 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2020-03-07 05:10:19 +00:00
|
|
|
}
|
|
|
|
else if (session == 0)
|
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x10);
|
2020-03-07 05:10:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendACKAndStat();
|
|
|
|
|
|
|
|
m_async_command_parameter = session;
|
|
|
|
m_drive_state = DriveState::ChangingSession;
|
2020-09-29 13:29:28 +00:00
|
|
|
m_drive_event->Schedule(System::GetTicksPerSecond() / 2); // half a second
|
2020-03-07 05:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
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");
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
2019-10-20 05:55:23 +00:00
|
|
|
else
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
2019-10-27 04:38:48 +00:00
|
|
|
SendACKAndStat();
|
2020-05-11 12:55:13 +00:00
|
|
|
|
2020-06-01 14:59:11 +00:00
|
|
|
if ((!m_setloc_pending || m_setloc_position.ToLBA() == GetNextSectorToBeRead()) &&
|
2020-05-24 03:27:02 +00:00
|
|
|
(m_drive_state == DriveState::Reading || (IsSeeking() && m_read_after_seek)))
|
2020-05-11 12:55:13 +00:00
|
|
|
{
|
2020-05-19 16:05:17 +00:00
|
|
|
Log_DevPrintf("Ignoring read command with no/same setloc, already reading/reading after seek");
|
2020-05-11 12:55:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-27 16:41:38 +00:00
|
|
|
if (IsSeeking())
|
|
|
|
UpdatePositionWhileSeeking();
|
|
|
|
|
2020-05-11 12:55:13 +00:00
|
|
|
BeginReading();
|
|
|
|
}
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
2019-10-20 05:55:23 +00:00
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
2019-09-23 13:31:51 +00:00
|
|
|
|
2019-10-20 05:55:23 +00:00
|
|
|
case Command::Play:
|
|
|
|
{
|
|
|
|
u8 track = m_param_fifo.IsEmpty() ? 0 : m_param_fifo.Peek(0);
|
|
|
|
Log_DebugPrintf("CDROM play command, track=%u", track);
|
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2019-10-20 05:55:23 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2019-10-20 05:55:23 +00:00
|
|
|
}
|
|
|
|
else
|
2019-09-23 13:31:51 +00:00
|
|
|
{
|
2019-10-26 11:07:28 +00:00
|
|
|
SendACKAndStat();
|
2020-05-11 12:55:13 +00:00
|
|
|
|
2020-06-01 14:59:11 +00:00
|
|
|
if (track == 0 && (!m_setloc_pending || m_setloc_position.ToLBA() == GetNextSectorToBeRead()) &&
|
2020-05-24 03:27:02 +00:00
|
|
|
(m_drive_state == DriveState::Playing || (IsSeeking() && m_play_after_seek)))
|
2020-05-11 12:55:13 +00:00
|
|
|
{
|
2020-05-19 16:05:17 +00:00
|
|
|
Log_DevPrintf("Ignoring play command with no/same setloc, already playing/playing after seek");
|
2020-05-11 12:55:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-27 16:41:38 +00:00
|
|
|
if (IsSeeking())
|
|
|
|
UpdatePositionWhileSeeking();
|
|
|
|
|
2020-05-11 12:55:13 +00:00
|
|
|
BeginPlaying(track);
|
|
|
|
}
|
2019-09-23 13:31:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Command::Pause:
|
|
|
|
{
|
2020-03-31 12:28:55 +00:00
|
|
|
if (m_secondary_status.seeking)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("CDROM Pause command while seeking - sending error response");
|
2020-05-30 11:59:03 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, ERROR_REASON_NOT_READY);
|
2020-03-31 12:28:55 +00:00
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:28:48 +00:00
|
|
|
// TODO: Should return an error if seeking.
|
2019-10-27 09:42:37 +00:00
|
|
|
const bool was_reading = (m_drive_state == DriveState::Reading || m_drive_state == DriveState::Playing);
|
2020-01-24 04:53:40 +00:00
|
|
|
const TickCount pause_time = was_reading ? (m_mode.double_speed ? 2000000 : 1000000) : 7000;
|
2019-10-27 09:42:37 +00:00
|
|
|
Log_DebugPrintf("CDROM pause command");
|
|
|
|
SendACKAndStat();
|
|
|
|
|
2020-07-29 15:55:35 +00:00
|
|
|
// Reset audio buffer here - control room cutscene audio repeats in Dino Crisis otherwise.
|
|
|
|
ResetAudioDecoder();
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
m_drive_state = DriveState::Pausing;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Schedule(pause_time);
|
2019-09-21 15:12:16 +00:00
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
EndCommand();
|
2019-09-21 15:12:16 +00:00
|
|
|
return;
|
2019-09-17 12:18:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 07:19:29 +00:00
|
|
|
case Command::Stop:
|
|
|
|
{
|
2020-05-19 16:26:06 +00:00
|
|
|
const TickCount stop_time = GetTicksForStop(m_secondary_status.motor_on);
|
2019-10-28 07:19:29 +00:00
|
|
|
Log_DebugPrintf("CDROM stop command");
|
|
|
|
SendACKAndStat();
|
|
|
|
|
|
|
|
m_drive_state = DriveState::Stopping;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Schedule(stop_time);
|
2019-10-28 07:19:29 +00:00
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:15:04 +00:00
|
|
|
case Command::Reset:
|
2019-09-21 16:32:45 +00:00
|
|
|
{
|
2020-03-28 15:15:04 +00:00
|
|
|
Log_DebugPrintf("CDROM reset command");
|
2019-10-27 09:42:37 +00:00
|
|
|
SendACKAndStat();
|
|
|
|
|
2020-07-10 10:55:14 +00:00
|
|
|
if (IsSeeking())
|
|
|
|
UpdatePositionWhileSeeking();
|
|
|
|
|
2020-03-28 15:15:04 +00:00
|
|
|
m_drive_state = DriveState::Resetting;
|
2020-07-13 15:26:37 +00:00
|
|
|
m_drive_event->SetIntervalAndSchedule(BASE_RESET_TICKS + ((m_current_lba != 0) ? GetTicksForSeek(0) : 0));
|
2020-07-11 10:34:16 +00:00
|
|
|
m_seek_start_lba = m_current_lba;
|
|
|
|
m_seek_end_lba = 0;
|
2019-09-21 16:32:45 +00:00
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
EndCommand();
|
2019-09-21 16:32:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-28 07:25:04 +00:00
|
|
|
case Command::MotorOn:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM motor on command");
|
|
|
|
if (m_secondary_status.motor_on)
|
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x20);
|
2019-10-28 07:25:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SendACKAndStat();
|
|
|
|
|
2020-03-28 15:15:04 +00:00
|
|
|
m_drive_state = DriveState::Resetting;
|
2020-09-29 13:29:28 +00:00
|
|
|
m_drive_event->Schedule(System::GetTicksPerSecond());
|
2019-10-28 07:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-15 07:27:35 +00:00
|
|
|
case Command::Mute:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM mute command");
|
|
|
|
m_muted = true;
|
2019-10-26 06:12:37 +00:00
|
|
|
SendACKAndStat();
|
2019-10-15 07:27:35 +00:00
|
|
|
EndCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-09-23 13:31:51 +00:00
|
|
|
case Command::Demute:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM demute command");
|
|
|
|
m_muted = false;
|
2019-10-26 06:12:37 +00:00
|
|
|
SendACKAndStat();
|
2019-09-23 13:31:51 +00:00
|
|
|
EndCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-04 09:05:19 +00:00
|
|
|
case Command::GetlocL:
|
|
|
|
{
|
2020-03-29 07:41:31 +00:00
|
|
|
if (!m_last_sector_header_valid)
|
2019-10-27 09:42:37 +00:00
|
|
|
{
|
2020-06-01 11:40:26 +00:00
|
|
|
Log_DevPrintf("CDROM GetlocL command - header invalid, status 0x%02X", m_secondary_status.bits);
|
|
|
|
SendErrorResponse(STAT_ERROR, ERROR_REASON_NOT_READY);
|
2019-10-27 09:42:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-01 11:40:26 +00:00
|
|
|
Log_DebugPrintf("CDROM GetlocL command - [%02X:%02X:%02X]", m_last_sector_header.minute,
|
|
|
|
m_last_sector_header.second, m_last_sector_header.frame);
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-04 09:05:19 +00:00
|
|
|
EndCommand();
|
2019-10-27 09:42:37 +00:00
|
|
|
return;
|
2019-10-04 09:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case Command::GetlocP:
|
|
|
|
{
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2020-03-29 07:41:31 +00:00
|
|
|
{
|
2020-04-05 14:25:56 +00:00
|
|
|
Log_DebugPrintf("CDROM GetlocP command - not ready");
|
2020-05-30 11:59:03 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, ERROR_REASON_NOT_READY);
|
2020-03-29 07:41:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-19 15:52:53 +00:00
|
|
|
if (IsSeeking())
|
|
|
|
UpdatePositionWhileSeeking();
|
|
|
|
|
2020-04-05 14:25:56 +00:00
|
|
|
Log_DebugPrintf("CDROM GetlocP command - T%02x I%02x R[%02x:%02x:%02x] A[%02x:%02x:%02x]",
|
|
|
|
m_last_subq.track_number_bcd, m_last_subq.index_number_bcd, m_last_subq.relative_minute_bcd,
|
|
|
|
m_last_subq.relative_second_bcd, m_last_subq.relative_frame_bcd,
|
|
|
|
m_last_subq.absolute_minute_bcd, m_last_subq.absolute_second_bcd,
|
|
|
|
m_last_subq.absolute_frame_bcd);
|
|
|
|
|
2020-03-29 07:41:31 +00:00
|
|
|
m_response_fifo.Push(m_last_subq.track_number_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.index_number_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.relative_minute_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.relative_second_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.relative_frame_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.absolute_minute_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.absolute_second_bcd);
|
|
|
|
m_response_fifo.Push(m_last_subq.absolute_frame_bcd);
|
|
|
|
SetInterrupt(Interrupt::ACK);
|
|
|
|
}
|
|
|
|
|
2019-10-04 09:05:19 +00:00
|
|
|
EndCommand();
|
2020-03-29 07:41:31 +00:00
|
|
|
return;
|
2019-10-04 09:05:19 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 12:44:28 +00:00
|
|
|
case Command::GetTN:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM GetTN command");
|
2020-05-19 16:26:06 +00:00
|
|
|
if (CanReadMedia())
|
2019-10-18 12:44:28 +00:00
|
|
|
{
|
2020-02-21 15:19:10 +00:00
|
|
|
m_reader.WaitForReadToComplete();
|
|
|
|
|
2019-10-18 12:44:28 +00:00
|
|
|
m_response_fifo.Push(m_secondary_status.bits);
|
2020-03-29 14:06:06 +00:00
|
|
|
m_response_fifo.Push(BinaryToBCD(Truncate8(m_reader.GetMedia()->GetFirstTrackNumber())));
|
|
|
|
m_response_fifo.Push(BinaryToBCD(Truncate8(m_reader.GetMedia()->GetLastTrackNumber())));
|
2019-10-18 12:44:28 +00:00
|
|
|
SetInterrupt(Interrupt::ACK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2019-10-18 12:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-10-18 13:06:13 +00:00
|
|
|
case Command::GetTD:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM GetTD command");
|
|
|
|
Assert(m_param_fifo.GetSize() >= 1);
|
2019-12-06 06:23:08 +00:00
|
|
|
const u8 track = PackedBCDToBinary(m_param_fifo.Peek());
|
2019-10-18 13:06:13 +00:00
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2019-10-18 13:06:13 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x80);
|
2019-10-18 13:06:13 +00:00
|
|
|
}
|
2020-02-21 15:19:10 +00:00
|
|
|
else if (track > m_reader.GetMedia()->GetTrackCount())
|
2019-10-18 13:06:13 +00:00
|
|
|
{
|
2020-03-08 05:53:53 +00:00
|
|
|
SendErrorResponse(STAT_ERROR, 0x10);
|
2019-10-18 13:06:13 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CDImage::Position pos;
|
|
|
|
if (track == 0)
|
2020-02-21 15:19:10 +00:00
|
|
|
pos = CDImage::Position::FromLBA(m_reader.GetMedia()->GetLBACount());
|
2019-10-18 13:06:13 +00:00
|
|
|
else
|
2020-02-21 15:19:10 +00:00
|
|
|
pos = m_reader.GetMedia()->GetTrackStartMSFPosition(track);
|
2019-10-18 13:06:13 +00:00
|
|
|
|
|
|
|
m_response_fifo.Push(m_secondary_status.bits);
|
2019-12-06 06:23:08 +00:00
|
|
|
m_response_fifo.Push(BinaryToBCD(Truncate8(pos.minute)));
|
|
|
|
m_response_fifo.Push(BinaryToBCD(Truncate8(pos.second)));
|
2019-10-18 13:06:13 +00:00
|
|
|
SetInterrupt(Interrupt::ACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
EndCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-11-08 12:45:06 +00:00
|
|
|
case Command::Getparam:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM Getparam command");
|
|
|
|
|
|
|
|
m_response_fifo.Push(m_secondary_status.bits);
|
|
|
|
m_response_fifo.Push(m_mode.bits);
|
|
|
|
m_response_fifo.Push(0);
|
2020-04-04 15:39:43 +00:00
|
|
|
m_response_fifo.Push(m_xa_filter_file_number);
|
|
|
|
m_response_fifo.Push(m_xa_filter_channel_number);
|
2019-11-08 12:45:06 +00:00
|
|
|
SetInterrupt(Interrupt::ACK);
|
|
|
|
EndCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-03-27 16:24:36 +00:00
|
|
|
case Command::Sync:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("CDROM sync command");
|
|
|
|
|
|
|
|
SendErrorResponse(STAT_ERROR, 0x40);
|
|
|
|
EndCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
default:
|
2020-03-07 04:00:15 +00:00
|
|
|
{
|
|
|
|
Log_ErrorPrintf("Unknown CDROM command 0x%04X with %u parameters, please report", static_cast<u16>(m_command),
|
|
|
|
m_param_fifo.GetSize());
|
|
|
|
Panic("Unknown CDROM command");
|
|
|
|
}
|
|
|
|
break;
|
2019-09-17 12:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::ExecuteTestCommand(u8 subcommand)
|
|
|
|
{
|
|
|
|
switch (subcommand)
|
|
|
|
{
|
2019-11-08 14:21:04 +00:00
|
|
|
case 0x04: // Reset SCEx counters
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Reset SCEx counters");
|
|
|
|
m_secondary_status.motor_on = true;
|
|
|
|
m_response_fifo.Push(m_secondary_status.bits);
|
|
|
|
SetInterrupt(Interrupt::ACK);
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x05: // Read SCEx counters
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Read SCEx counters");
|
|
|
|
m_response_fifo.Push(m_secondary_status.bits);
|
|
|
|
m_response_fifo.Push(0); // # of TOC reads?
|
|
|
|
m_response_fifo.Push(0); // # of SCEx strings received
|
|
|
|
SetInterrupt(Interrupt::ACK);
|
|
|
|
EndCommand();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
case 0x20: // Get CDROM BIOS Date/Version
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Get CDROM BIOS Date/Version");
|
2020-03-12 03:51:38 +00:00
|
|
|
|
|
|
|
static constexpr u8 response[] = {0x95, 0x05, 0x16, 0xC1};
|
2019-09-17 12:18:58 +00:00
|
|
|
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-17 12:18:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-20 13:40:19 +00:00
|
|
|
case 0x22:
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Get CDROM region ID string");
|
2020-03-12 03:51:38 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
switch (System::GetRegion())
|
2020-03-12 03:51:38 +00:00
|
|
|
{
|
|
|
|
case ConsoleRegion::NTSC_J:
|
|
|
|
{
|
|
|
|
static constexpr u8 response[] = {'f', 'o', 'r', ' ', 'J', 'a', 'p', 'a', 'n'};
|
|
|
|
m_response_fifo.PushRange(response, countof(response));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ConsoleRegion::PAL:
|
|
|
|
{
|
|
|
|
static constexpr u8 response[] = {'f', 'o', 'r', ' ', 'E', 'u', 'r', 'o', 'p', 'e'};
|
|
|
|
m_response_fifo.PushRange(response, countof(response));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ConsoleRegion::NTSC_U:
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
static constexpr u8 response[] = {'f', 'o', 'r', ' ', 'U', '/', 'C'};
|
|
|
|
m_response_fifo.PushRange(response, countof(response));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:18:58 +00:00
|
|
|
default:
|
|
|
|
{
|
2020-03-07 04:00:15 +00:00
|
|
|
Log_ErrorPrintf("Unknown test command 0x%02X, %u parameters", subcommand, m_param_fifo.GetSize());
|
2019-11-13 06:33:49 +00:00
|
|
|
Panic("Unknown test command");
|
2019-09-17 12:18:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 09:22:39 +00:00
|
|
|
}
|
2019-09-21 15:12:16 +00:00
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
void CDROM::UpdateCommandEvent()
|
|
|
|
{
|
|
|
|
// if there's a pending interrupt, we can't execute the command yet
|
|
|
|
// so deactivate it until the interrupt is acknowledged
|
|
|
|
if (!HasPendingCommand() || HasPendingInterrupt())
|
|
|
|
{
|
|
|
|
m_command_event->Deactivate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (HasPendingCommand())
|
|
|
|
{
|
|
|
|
m_command_event->Activate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::ExecuteDrive(TickCount ticks_late)
|
|
|
|
{
|
|
|
|
switch (m_drive_state)
|
|
|
|
{
|
2020-03-28 15:15:04 +00:00
|
|
|
case DriveState::Resetting:
|
|
|
|
DoResetComplete(ticks_late);
|
2020-01-24 04:53:40 +00:00
|
|
|
break;
|
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
case DriveState::ShellOpening:
|
|
|
|
DoShellOpenComplete(ticks_late);
|
|
|
|
break;
|
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
case DriveState::SeekingPhysical:
|
|
|
|
case DriveState::SeekingLogical:
|
|
|
|
DoSeekComplete(ticks_late);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DriveState::Pausing:
|
|
|
|
DoPauseComplete();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DriveState::Stopping:
|
|
|
|
DoStopComplete();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DriveState::ReadingID:
|
|
|
|
DoIDRead();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DriveState::ReadingTOC:
|
|
|
|
DoTOCRead();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DriveState::Reading:
|
|
|
|
case DriveState::Playing:
|
|
|
|
DoSectorRead();
|
|
|
|
break;
|
|
|
|
|
2020-03-07 05:10:19 +00:00
|
|
|
case DriveState::ChangingSession:
|
|
|
|
DoChangeSessionComplete();
|
|
|
|
break;
|
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
case DriveState::Idle:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 04:16:20 +00:00
|
|
|
void CDROM::BeginReading(TickCount ticks_late /* = 0 */, bool after_seek /* = false */)
|
2019-09-21 15:12:16 +00:00
|
|
|
{
|
2020-03-12 05:32:41 +00:00
|
|
|
ClearSectorBuffers();
|
|
|
|
|
2020-05-31 07:03:13 +00:00
|
|
|
if (!after_seek && m_setloc_pending)
|
2019-10-20 05:55:23 +00:00
|
|
|
{
|
2019-11-16 02:54:41 +00:00
|
|
|
BeginSeeking(true, true, false);
|
2019-10-27 07:58:20 +00:00
|
|
|
return;
|
2019-10-20 05:55:23 +00:00
|
|
|
}
|
2019-09-21 15:12:16 +00:00
|
|
|
|
2020-06-26 07:15:39 +00:00
|
|
|
// If we were seeking, we want to start reading from the seek target, not the current sector
|
|
|
|
// Fixes crash in Disney's The Lion King - Simba's Mighty Adventure.
|
|
|
|
if (IsSeeking())
|
|
|
|
{
|
|
|
|
Log_DevPrintf("Read command while seeking, scheduling read after seek %u -> %u finishes in %d ticks",
|
|
|
|
m_seek_start_lba, m_seek_end_lba, m_drive_event->GetTicksUntilNextExecution());
|
|
|
|
m_read_after_seek = true;
|
|
|
|
m_play_after_seek = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_DebugPrintf("Starting reading @ LBA %u", m_current_lba);
|
2019-10-27 09:42:37 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
2019-09-21 15:12:16 +00:00
|
|
|
m_secondary_status.motor_on = true;
|
2020-07-29 15:55:35 +00:00
|
|
|
ResetAudioDecoder();
|
2019-11-08 11:16:21 +00:00
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
const TickCount ticks = GetTicksForRead();
|
2020-05-19 15:52:53 +00:00
|
|
|
const TickCount first_sector_ticks = ticks + (after_seek ? 0 : GetTicksForSeek(m_current_lba)) - ticks_late;
|
2019-11-13 06:33:49 +00:00
|
|
|
m_drive_state = DriveState::Reading;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->SetInterval(ticks);
|
2020-04-25 04:16:20 +00:00
|
|
|
m_drive_event->Schedule(first_sector_ticks);
|
2020-03-28 15:13:25 +00:00
|
|
|
m_current_read_sector_buffer = 0;
|
|
|
|
m_current_write_sector_buffer = 0;
|
2020-02-21 15:19:10 +00:00
|
|
|
|
2020-05-19 15:52:53 +00:00
|
|
|
m_reader.QueueReadSector(m_current_lba);
|
2019-11-13 06:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 04:16:20 +00:00
|
|
|
void CDROM::BeginPlaying(u8 track_bcd, TickCount ticks_late /* = 0 */, bool after_seek /* = false */)
|
2019-11-13 06:33:49 +00:00
|
|
|
{
|
|
|
|
Log_DebugPrintf("Starting playing CDDA track %x", track_bcd);
|
2019-11-10 13:03:52 +00:00
|
|
|
m_last_cdda_report_frame_nibble = 0xFF;
|
2019-11-13 06:33:49 +00:00
|
|
|
m_play_track_number_bcd = track_bcd;
|
|
|
|
|
|
|
|
// if track zero, start from current position
|
|
|
|
if (track_bcd != 0)
|
|
|
|
{
|
|
|
|
// play specific track?
|
2020-02-21 15:19:10 +00:00
|
|
|
if (track_bcd > m_reader.GetMedia()->GetTrackCount())
|
2019-11-13 06:33:49 +00:00
|
|
|
{
|
|
|
|
// restart current track
|
2020-02-21 15:19:10 +00:00
|
|
|
track_bcd = BinaryToBCD(Truncate8(m_reader.GetMedia()->GetTrackNumber()));
|
2019-11-13 06:33:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 15:19:10 +00:00
|
|
|
m_setloc_position = m_reader.GetMedia()->GetTrackStartMSFPosition(PackedBCDToBinary(track_bcd));
|
2019-11-13 06:33:49 +00:00
|
|
|
m_setloc_pending = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_setloc_pending)
|
|
|
|
{
|
2019-11-16 02:54:41 +00:00
|
|
|
BeginSeeking(false, false, true);
|
2019-11-13 06:33:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_secondary_status.ClearActiveBits();
|
|
|
|
m_secondary_status.motor_on = true;
|
2020-08-23 10:01:50 +00:00
|
|
|
m_secondary_status.playing_cdda = true;
|
2020-03-12 05:32:41 +00:00
|
|
|
ClearSectorBuffers();
|
2020-07-29 15:55:35 +00:00
|
|
|
ResetAudioDecoder();
|
2019-11-13 06:33:49 +00:00
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
const TickCount ticks = GetTicksForRead();
|
2020-05-19 15:52:53 +00:00
|
|
|
const TickCount first_sector_ticks = ticks + (after_seek ? 0 : GetTicksForSeek(m_current_lba)) - ticks_late;
|
2019-11-13 06:33:49 +00:00
|
|
|
m_drive_state = DriveState::Playing;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->SetInterval(ticks);
|
2020-04-25 04:16:20 +00:00
|
|
|
m_drive_event->Schedule(first_sector_ticks);
|
2020-03-28 15:13:25 +00:00
|
|
|
m_current_read_sector_buffer = 0;
|
|
|
|
m_current_write_sector_buffer = 0;
|
2020-02-21 15:19:10 +00:00
|
|
|
|
2020-05-19 15:52:53 +00:00
|
|
|
m_reader.QueueReadSector(m_current_lba);
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 02:54:41 +00:00
|
|
|
void CDROM::BeginSeeking(bool logical, bool read_after_seek, bool play_after_seek)
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
|
|
|
if (!m_setloc_pending)
|
|
|
|
Log_WarningPrintf("Seeking without setloc set");
|
|
|
|
|
2019-11-10 05:01:18 +00:00
|
|
|
m_read_after_seek = read_after_seek;
|
|
|
|
m_play_after_seek = play_after_seek;
|
2019-10-26 11:07:28 +00:00
|
|
|
m_setloc_pending = false;
|
|
|
|
|
2020-02-21 15:19:10 +00:00
|
|
|
Log_DebugPrintf("Seeking to [%02u:%02u:%02u] (LBA %u) (%s)", m_setloc_position.minute, m_setloc_position.second,
|
|
|
|
m_setloc_position.frame, m_setloc_position.ToLBA(), logical ? "logical" : "physical");
|
2019-10-26 11:07:28 +00:00
|
|
|
|
2020-04-10 14:40:39 +00:00
|
|
|
const CDImage::LBA seek_lba = m_setloc_position.ToLBA();
|
|
|
|
const TickCount seek_time = GetTicksForSeek(seek_lba);
|
2019-11-10 05:01:18 +00:00
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
|
|
|
m_secondary_status.motor_on = true;
|
2020-03-07 05:10:49 +00:00
|
|
|
m_secondary_status.seeking = true;
|
2020-06-01 11:40:26 +00:00
|
|
|
m_last_sector_header_valid = false;
|
2020-07-29 15:55:35 +00:00
|
|
|
ResetAudioDecoder();
|
2019-10-27 09:42:37 +00:00
|
|
|
|
2019-11-16 02:54:41 +00:00
|
|
|
m_drive_state = logical ? DriveState::SeekingLogical : DriveState::SeekingPhysical;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->SetIntervalAndSchedule(seek_time);
|
2019-12-02 07:02:57 +00:00
|
|
|
|
2020-05-19 15:52:53 +00:00
|
|
|
m_seek_start_lba = m_current_lba;
|
|
|
|
m_seek_end_lba = seek_lba;
|
2020-05-12 14:13:57 +00:00
|
|
|
m_reader.QueueReadSector(seek_lba);
|
2019-10-27 09:42:37 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 15:52:53 +00:00
|
|
|
void CDROM::UpdatePositionWhileSeeking()
|
|
|
|
{
|
|
|
|
DebugAssert(IsSeeking());
|
|
|
|
|
|
|
|
const float completed_frac = 1.0f - (static_cast<float>(m_drive_event->GetTicksUntilNextExecution()) /
|
|
|
|
static_cast<float>(m_drive_event->GetInterval()));
|
|
|
|
|
|
|
|
CDImage::LBA current_lba;
|
|
|
|
if (m_seek_end_lba > m_seek_start_lba)
|
|
|
|
{
|
2020-07-13 15:26:37 +00:00
|
|
|
current_lba =
|
|
|
|
m_seek_start_lba +
|
|
|
|
std::max<CDImage::LBA>(
|
|
|
|
static_cast<CDImage::LBA>(static_cast<float>(m_seek_end_lba - m_seek_start_lba) * completed_frac), 1);
|
|
|
|
}
|
|
|
|
else if (m_seek_end_lba < m_seek_start_lba)
|
|
|
|
{
|
|
|
|
current_lba =
|
|
|
|
m_seek_start_lba -
|
|
|
|
std::max<CDImage::LBA>(
|
|
|
|
static_cast<CDImage::LBA>(static_cast<float>(m_seek_start_lba - m_seek_end_lba) * completed_frac), 1);
|
2020-05-19 15:52:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-13 15:26:37 +00:00
|
|
|
// strange seek...
|
|
|
|
return;
|
2020-05-19 15:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Log_DevPrintf("Update position while seeking from %u to %u - %u (%.2f)", m_seek_start_lba, m_seek_end_lba,
|
|
|
|
current_lba, completed_frac);
|
|
|
|
|
|
|
|
// access the image directly since we want to preserve the cached data for the seek complete
|
|
|
|
CDImage::SubChannelQ subq;
|
|
|
|
if (m_reader.ReadSectorUncached(current_lba, &subq, nullptr) && subq.IsCRCValid())
|
|
|
|
{
|
|
|
|
m_last_subq = subq;
|
|
|
|
m_current_lba = current_lba;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
void CDROM::DoShellOpenComplete(TickCount ticks_late)
|
|
|
|
{
|
|
|
|
// media is now readable (if any)
|
|
|
|
m_drive_state = DriveState::Idle;
|
|
|
|
m_drive_event->Deactivate();
|
|
|
|
|
|
|
|
if (m_reader.HasMedia())
|
|
|
|
m_secondary_status.motor_on = true;
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:15:04 +00:00
|
|
|
void CDROM::DoResetComplete(TickCount ticks_late)
|
2019-10-27 09:42:37 +00:00
|
|
|
{
|
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2019-10-27 09:42:37 +00:00
|
|
|
|
2020-03-28 15:16:01 +00:00
|
|
|
m_secondary_status.bits = 0;
|
2020-05-19 16:26:06 +00:00
|
|
|
m_secondary_status.motor_on = CanReadMedia();
|
2020-03-28 15:14:58 +00:00
|
|
|
m_mode.bits = 0;
|
|
|
|
m_mode.read_raw_sector = true;
|
2019-10-26 11:07:28 +00:00
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2020-03-28 15:15:04 +00:00
|
|
|
{
|
|
|
|
Log_DevPrintf("CDROM reset - no disc");
|
|
|
|
m_secondary_status.shell_open = true;
|
|
|
|
SendAsyncErrorResponse(STAT_ERROR, 0x08);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-10 10:55:14 +00:00
|
|
|
m_current_lba = 0;
|
|
|
|
m_reader.QueueReadSector(0);
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::Complete);
|
2020-03-28 15:15:04 +00:00
|
|
|
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2020-03-28 15:15:04 +00:00
|
|
|
{
|
|
|
|
m_secondary_status.motor_on = false;
|
|
|
|
m_secondary_status.shell_open = true;
|
|
|
|
}
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 04:53:40 +00:00
|
|
|
void CDROM::DoSeekComplete(TickCount ticks_late)
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
2019-11-16 02:54:41 +00:00
|
|
|
const bool logical = (m_drive_state == DriveState::SeekingLogical);
|
2019-10-27 09:42:37 +00:00
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2019-10-27 09:42:37 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
2019-10-26 11:07:28 +00:00
|
|
|
|
2020-02-21 15:19:10 +00:00
|
|
|
bool seek_okay = m_reader.WaitForReadToComplete();
|
2019-11-16 02:54:41 +00:00
|
|
|
if (seek_okay)
|
|
|
|
{
|
2020-04-05 14:25:56 +00:00
|
|
|
const CDImage::SubChannelQ& subq = m_reader.GetSectorSubQ();
|
|
|
|
if (subq.IsCRCValid())
|
2019-11-16 02:54:41 +00:00
|
|
|
{
|
2020-04-05 14:25:56 +00:00
|
|
|
// seek and update sub-q for ReadP command
|
|
|
|
m_last_subq = subq;
|
2020-05-12 14:13:57 +00:00
|
|
|
const auto [seek_mm, seek_ss, seek_ff] = CDImage::Position::FromLBA(m_reader.GetLastReadSector()).ToBCD();
|
2020-04-05 14:25:56 +00:00
|
|
|
seek_okay = (subq.IsCRCValid() && subq.absolute_minute_bcd == seek_mm && subq.absolute_second_bcd == seek_ss &&
|
|
|
|
subq.absolute_frame_bcd == seek_ff);
|
|
|
|
if (seek_okay)
|
2019-11-16 02:54:41 +00:00
|
|
|
{
|
2020-08-04 17:14:04 +00:00
|
|
|
if (subq.control.data)
|
2020-04-05 14:25:56 +00:00
|
|
|
{
|
2020-08-04 17:14:04 +00:00
|
|
|
if (logical)
|
|
|
|
ProcessDataSectorHeader(m_reader.GetSectorBuffer().data());
|
2020-04-05 14:25:56 +00:00
|
|
|
}
|
|
|
|
else
|
2020-03-28 15:16:01 +00:00
|
|
|
{
|
2020-04-05 14:25:56 +00:00
|
|
|
if (logical)
|
|
|
|
Log_WarningPrintf("Logical seek to non-data sector [%02x:%02x:%02x]", seek_mm, seek_ss, seek_ff);
|
2020-03-28 15:16:01 +00:00
|
|
|
}
|
2020-05-08 05:41:41 +00:00
|
|
|
|
|
|
|
if (subq.track_number_bcd == CDImage::LEAD_OUT_TRACK_NUMBER)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Invalid seek to lead-out area (LBA %u)", m_reader.GetLastReadSector());
|
|
|
|
seek_okay = false;
|
|
|
|
}
|
2019-11-16 02:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seek_okay)
|
2019-10-26 11:07:28 +00:00
|
|
|
{
|
2020-05-19 15:52:53 +00:00
|
|
|
m_current_lba = m_reader.GetLastReadSector();
|
2020-05-12 14:13:57 +00:00
|
|
|
|
2019-10-26 11:07:28 +00:00
|
|
|
// seek complete, transition to play/read if requested
|
2019-11-13 06:33:49 +00:00
|
|
|
// INT2 is not sent on play/read
|
|
|
|
if (m_read_after_seek)
|
2019-11-08 11:16:21 +00:00
|
|
|
{
|
2020-04-25 04:16:20 +00:00
|
|
|
BeginReading(ticks_late, true);
|
2019-11-13 06:33:49 +00:00
|
|
|
}
|
|
|
|
else if (m_play_after_seek)
|
|
|
|
{
|
2020-04-25 04:16:20 +00:00
|
|
|
BeginPlaying(0, ticks_late, true);
|
2019-11-08 11:16:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::Complete);
|
2019-11-08 11:16:21 +00:00
|
|
|
}
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-12 14:13:57 +00:00
|
|
|
CDImage::Position pos(CDImage::Position::FromLBA(m_reader.GetLastReadSector()));
|
2020-02-21 15:19:10 +00:00
|
|
|
Log_WarningPrintf("%s seek to [%02u:%02u:%02u] failed", logical ? "Logical" : "Physical", pos.minute, pos.second,
|
|
|
|
pos.frame);
|
2020-03-08 05:53:53 +00:00
|
|
|
SendAsyncErrorResponse(STAT_SEEK_ERROR, 0x04);
|
2020-03-29 07:41:31 +00:00
|
|
|
m_last_sector_header_valid = false;
|
2019-10-26 11:07:28 +00:00
|
|
|
}
|
2019-09-21 15:12:16 +00:00
|
|
|
|
2019-10-26 11:07:28 +00:00
|
|
|
m_setloc_pending = false;
|
|
|
|
m_read_after_seek = false;
|
|
|
|
m_play_after_seek = false;
|
2019-11-08 11:16:21 +00:00
|
|
|
UpdateStatusRegister();
|
2019-09-21 15:12:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
void CDROM::DoPauseComplete()
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Pause complete");
|
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2019-10-27 09:42:37 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
|
|
|
|
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::Complete);
|
2019-10-27 09:42:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 07:19:29 +00:00
|
|
|
void CDROM::DoStopComplete()
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Stop complete");
|
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2019-10-28 07:19:29 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
|
|
|
m_secondary_status.motor_on = false;
|
|
|
|
|
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::Complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::DoChangeSessionComplete()
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Changing session complete");
|
|
|
|
m_drive_state = DriveState::Idle;
|
|
|
|
m_drive_event->Deactivate();
|
|
|
|
m_secondary_status.ClearActiveBits();
|
|
|
|
m_secondary_status.motor_on = true;
|
|
|
|
|
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
if (m_async_command_parameter == 0x01)
|
|
|
|
{
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
|
|
|
SetAsyncInterrupt(Interrupt::Complete);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// we don't emulate multisession discs.. for now
|
2020-03-08 05:53:53 +00:00
|
|
|
SendAsyncErrorResponse(STAT_SEEK_ERROR, 0x40);
|
2020-03-07 05:10:19 +00:00
|
|
|
}
|
2019-10-28 07:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
void CDROM::DoIDRead()
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("ID read complete");
|
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2019-10-27 09:42:37 +00:00
|
|
|
m_secondary_status.ClearActiveBits();
|
2020-05-19 16:26:06 +00:00
|
|
|
m_secondary_status.motor_on = CanReadMedia();
|
2019-10-27 09:42:37 +00:00
|
|
|
|
2020-03-12 03:51:53 +00:00
|
|
|
// TODO: Audio CD.
|
|
|
|
u8 stat_byte = m_secondary_status.bits;
|
|
|
|
u8 flags_byte = 0;
|
2020-05-19 16:26:06 +00:00
|
|
|
if (!CanReadMedia())
|
2020-03-12 03:51:53 +00:00
|
|
|
{
|
|
|
|
flags_byte |= (1 << 6); // Disc Missing
|
|
|
|
}
|
2020-04-10 14:40:39 +00:00
|
|
|
else
|
2020-03-12 03:51:53 +00:00
|
|
|
{
|
2020-04-10 14:40:39 +00:00
|
|
|
// this is where it would get read from the start of the disc?
|
2020-05-19 15:52:53 +00:00
|
|
|
m_current_lba = 0;
|
2020-05-12 14:13:57 +00:00
|
|
|
m_reader.QueueReadSector(0);
|
2020-04-10 14:40:39 +00:00
|
|
|
|
2020-08-04 17:14:04 +00:00
|
|
|
if (g_settings.cdrom_region_check && (m_disc_region == DiscRegion::Other ||
|
|
|
|
System::GetRegion() != System::GetConsoleRegionForDiscRegion(m_disc_region)))
|
2020-04-10 14:40:39 +00:00
|
|
|
{
|
|
|
|
stat_byte |= STAT_ID_ERROR;
|
|
|
|
flags_byte |= (1 << 7); // Unlicensed
|
|
|
|
}
|
2020-03-12 03:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
m_async_response_fifo.Clear();
|
2020-03-12 03:51:53 +00:00
|
|
|
m_async_response_fifo.Push(stat_byte);
|
|
|
|
m_async_response_fifo.Push(flags_byte);
|
|
|
|
m_async_response_fifo.Push(0x20); // TODO: Disc type from TOC
|
|
|
|
m_async_response_fifo.Push(0x00); // TODO: Session info?
|
|
|
|
|
|
|
|
static constexpr u32 REGION_STRING_LENGTH = 4;
|
|
|
|
static constexpr std::array<std::array<u8, REGION_STRING_LENGTH>, static_cast<size_t>(DiscRegion::Count)>
|
|
|
|
region_strings = {{{'S', 'C', 'E', 'I'}, {'S', 'C', 'E', 'A'}, {'S', 'C', 'E', 'E'}, {0, 0, 0, 0}}};
|
|
|
|
m_async_response_fifo.PushRange(region_strings[static_cast<u8>(m_disc_region)].data(), REGION_STRING_LENGTH);
|
|
|
|
|
|
|
|
SetAsyncInterrupt((flags_byte != 0) ? Interrupt::Error : Interrupt::Complete);
|
2019-10-27 09:42:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 14:21:11 +00:00
|
|
|
void CDROM::DoTOCRead()
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("TOC read complete");
|
|
|
|
m_drive_state = DriveState::Idle;
|
2020-01-24 04:53:40 +00:00
|
|
|
m_drive_event->Deactivate();
|
2020-03-28 15:15:04 +00:00
|
|
|
|
2019-11-08 14:21:11 +00:00
|
|
|
m_async_response_fifo.Clear();
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::Complete);
|
2019-11-08 14:21:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:50:22 +00:00
|
|
|
void CDROM::StopReadingWithDataEnd()
|
|
|
|
{
|
|
|
|
ClearAsyncInterrupt();
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
|
|
|
SetAsyncInterrupt(Interrupt::DataEnd);
|
|
|
|
|
|
|
|
m_secondary_status.ClearActiveBits();
|
|
|
|
m_drive_state = DriveState::Idle;
|
|
|
|
m_drive_event->Deactivate();
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
void CDROM::DoSectorRead()
|
|
|
|
{
|
2020-02-21 15:19:10 +00:00
|
|
|
if (!m_reader.WaitForReadToComplete())
|
|
|
|
Panic("Sector read failed");
|
|
|
|
|
2020-05-12 14:13:57 +00:00
|
|
|
// TODO: Queue the next read here and swap the buffer.
|
2020-05-19 15:52:53 +00:00
|
|
|
m_current_lba = m_reader.GetLastReadSector();
|
2020-05-12 14:13:57 +00:00
|
|
|
|
2019-09-21 15:12:16 +00:00
|
|
|
// TODO: Error handling
|
2020-02-21 15:19:10 +00:00
|
|
|
const CDImage::SubChannelQ& subq = m_reader.GetSectorSubQ();
|
2020-08-17 14:13:18 +00:00
|
|
|
if (subq.IsCRCValid())
|
|
|
|
{
|
|
|
|
m_last_subq = subq;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const CDImage::Position pos(CDImage::Position::FromLBA(m_current_lba));
|
|
|
|
Log_DevPrintf("Sector %u [%02u:%02u:%02u] has invalid subchannel Q", m_current_lba, pos.minute, pos.second,
|
|
|
|
pos.frame);
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:50:22 +00:00
|
|
|
if (subq.track_number_bcd == CDImage::LEAD_OUT_TRACK_NUMBER)
|
|
|
|
{
|
|
|
|
Log_DevPrintf("Read reached lead-out area of disc at LBA %u, pausing", m_reader.GetLastReadSector());
|
|
|
|
StopReadingWithDataEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-13 06:33:49 +00:00
|
|
|
const bool is_data_sector = subq.control.data;
|
|
|
|
if (!is_data_sector)
|
2019-11-12 10:31:59 +00:00
|
|
|
{
|
2019-11-13 06:33:49 +00:00
|
|
|
if (m_play_track_number_bcd == 0)
|
|
|
|
{
|
|
|
|
// track number was not specified, but we've found the track now
|
|
|
|
m_play_track_number_bcd = subq.track_number_bcd;
|
2020-03-29 14:06:06 +00:00
|
|
|
Log_DebugPrintf("Setting playing track number to %u", m_play_track_number_bcd);
|
2019-11-13 06:33:49 +00:00
|
|
|
}
|
|
|
|
else if (m_mode.auto_pause && subq.track_number_bcd != m_play_track_number_bcd)
|
|
|
|
{
|
|
|
|
// we don't want to update the position if the track changes, so we check it before reading the actual sector.
|
|
|
|
Log_DevPrintf("Auto pause at the end of track %u (LBA %u)", m_play_track_number_bcd,
|
2020-02-21 15:19:10 +00:00
|
|
|
m_reader.GetLastReadSector());
|
2019-11-12 10:31:59 +00:00
|
|
|
|
2020-05-08 00:50:22 +00:00
|
|
|
StopReadingWithDataEnd();
|
2019-11-13 06:33:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-11-12 10:31:59 +00:00
|
|
|
}
|
2020-03-29 07:41:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ProcessDataSectorHeader(m_reader.GetSectorBuffer().data());
|
|
|
|
}
|
2019-11-12 10:31:59 +00:00
|
|
|
|
2020-04-10 16:44:04 +00:00
|
|
|
if (is_data_sector && m_drive_state == DriveState::Reading)
|
|
|
|
{
|
|
|
|
ProcessDataSector(m_reader.GetSectorBuffer().data(), subq);
|
|
|
|
}
|
2020-07-22 05:23:13 +00:00
|
|
|
else if (!is_data_sector &&
|
|
|
|
(m_drive_state == DriveState::Playing || (m_drive_state == DriveState::Reading && m_mode.cdda)))
|
2020-04-10 16:44:04 +00:00
|
|
|
{
|
|
|
|
ProcessCDDASector(m_reader.GetSectorBuffer().data(), subq);
|
|
|
|
}
|
|
|
|
else if (m_drive_state != DriveState::Reading && m_drive_state != DriveState::Playing)
|
|
|
|
{
|
|
|
|
Panic("Not reading or playing");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-19 15:52:53 +00:00
|
|
|
Log_WarningPrintf("Skipping sector %u as it is a %s sector and we're not %s", m_current_lba,
|
2020-04-10 16:44:04 +00:00
|
|
|
is_data_sector ? "data" : "audio", is_data_sector ? "reading" : "playing");
|
2019-11-13 06:33:49 +00:00
|
|
|
}
|
2020-02-21 15:19:10 +00:00
|
|
|
|
2020-06-01 14:59:11 +00:00
|
|
|
m_reader.QueueReadSector(m_current_lba + 1u);
|
2019-10-20 05:55:23 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 07:41:31 +00:00
|
|
|
void CDROM::ProcessDataSectorHeader(const u8* raw_sector)
|
2019-10-20 05:55:23 +00:00
|
|
|
{
|
2019-10-27 03:44:23 +00:00
|
|
|
std::memcpy(&m_last_sector_header, &raw_sector[SECTOR_SYNC_SIZE], sizeof(m_last_sector_header));
|
|
|
|
std::memcpy(&m_last_sector_subheader, &raw_sector[SECTOR_SYNC_SIZE + sizeof(m_last_sector_header)],
|
2019-10-04 09:05:19 +00:00
|
|
|
sizeof(m_last_sector_subheader));
|
2020-03-29 07:41:31 +00:00
|
|
|
m_last_sector_header_valid = true;
|
2019-11-16 02:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CDROM::ProcessDataSector(const u8* raw_sector, const CDImage::SubChannelQ& subq)
|
|
|
|
{
|
2020-05-19 15:52:53 +00:00
|
|
|
Log_DevPrintf("Read sector %u: mode %u submode 0x%02X into buffer %u", m_current_lba,
|
2020-03-28 15:13:25 +00:00
|
|
|
ZeroExtend32(m_last_sector_header.sector_mode), ZeroExtend32(m_last_sector_subheader.submode.bits),
|
|
|
|
m_current_write_sector_buffer);
|
2019-09-30 10:01:41 +00:00
|
|
|
|
2020-04-05 14:25:49 +00:00
|
|
|
// The reading bit shouldn't be set until the first sector is processed.
|
|
|
|
m_secondary_status.reading = true;
|
|
|
|
|
2019-10-04 09:05:19 +00:00
|
|
|
if (m_mode.xa_enable && m_last_sector_header.sector_mode == 2)
|
2019-09-30 10:01:41 +00:00
|
|
|
{
|
2019-10-04 09:05:19 +00:00
|
|
|
if (m_last_sector_subheader.submode.realtime && m_last_sector_subheader.submode.audio)
|
2019-09-30 10:01:41 +00:00
|
|
|
{
|
2020-04-04 15:39:43 +00:00
|
|
|
ProcessXAADPCMSector(raw_sector, subq);
|
2019-10-15 07:25:48 +00:00
|
|
|
|
|
|
|
// Audio+realtime sectors aren't delivered to the CPU.
|
2019-10-27 03:44:23 +00:00
|
|
|
return;
|
2019-10-04 09:05:19 +00:00
|
|
|
}
|
2019-10-27 03:44:23 +00:00
|
|
|
}
|
2019-09-30 10:01:41 +00:00
|
|
|
|
2020-03-12 05:32:41 +00:00
|
|
|
// TODO: How does XA relate to this buffering?
|
2020-09-24 14:25:32 +00:00
|
|
|
const u32 sb_num = (m_current_write_sector_buffer + 1) % NUM_SECTOR_BUFFERS;
|
|
|
|
SectorBuffer* sb = &m_sector_buffers[sb_num];
|
2020-03-12 05:32:41 +00:00
|
|
|
if (sb->size > 0)
|
2019-10-27 03:44:23 +00:00
|
|
|
{
|
2020-03-31 12:29:03 +00:00
|
|
|
Log_DevPrintf("Sector buffer %u was not read, previous sector dropped",
|
|
|
|
(m_current_write_sector_buffer - 1) % NUM_SECTOR_BUFFERS);
|
2019-09-30 10:01:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 12:31:22 +00:00
|
|
|
if (m_mode.ignore_bit)
|
2020-05-19 15:52:53 +00:00
|
|
|
Log_WarningPrintf("SetMode.4 bit set on read of sector %u", m_current_lba);
|
2020-04-19 12:31:22 +00:00
|
|
|
|
2019-10-27 03:44:23 +00:00
|
|
|
if (m_mode.read_raw_sector)
|
2019-09-30 10:01:41 +00:00
|
|
|
{
|
2020-03-12 05:32:41 +00:00
|
|
|
std::memcpy(sb->data.data(), raw_sector + SECTOR_SYNC_SIZE, RAW_SECTOR_OUTPUT_SIZE);
|
|
|
|
sb->size = RAW_SECTOR_OUTPUT_SIZE;
|
2019-09-30 10:01:41 +00:00
|
|
|
}
|
2019-10-27 03:44:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: This should actually depend on the mode...
|
2020-09-24 14:25:32 +00:00
|
|
|
if (m_last_sector_header.sector_mode != 2)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Ignoring non-mode2 sector at %u", m_current_lba);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:32:41 +00:00
|
|
|
std::memcpy(sb->data.data(), raw_sector + CDImage::SECTOR_SYNC_SIZE + 12, DATA_SECTOR_OUTPUT_SIZE);
|
|
|
|
sb->size = DATA_SECTOR_OUTPUT_SIZE;
|
2019-10-27 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 14:25:32 +00:00
|
|
|
m_current_write_sector_buffer = sb_num;
|
|
|
|
|
2020-03-23 14:21:22 +00:00
|
|
|
// Deliver to CPU
|
|
|
|
if (HasPendingAsyncInterrupt())
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Data interrupt was not delivered");
|
|
|
|
ClearAsyncInterrupt();
|
|
|
|
}
|
|
|
|
|
2019-10-27 03:44:23 +00:00
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
2020-03-07 05:10:19 +00:00
|
|
|
SetAsyncInterrupt(Interrupt::DataReady);
|
2019-09-21 15:12:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:27:35 +00:00
|
|
|
static std::array<std::array<s16, 29>, 7> s_zigzag_table = {
|
|
|
|
{{0, 0x0, 0x0, 0x0, 0x0, -0x0002, 0x000A, -0x0022, 0x0041, -0x0054,
|
|
|
|
0x0034, 0x0009, -0x010A, 0x0400, -0x0A78, 0x234C, 0x6794, -0x1780, 0x0BCD, -0x0623,
|
|
|
|
0x0350, -0x016D, 0x006B, 0x000A, -0x0010, 0x0011, -0x0008, 0x0003, -0x0001},
|
|
|
|
{0, 0x0, 0x0, -0x0002, 0x0, 0x0003, -0x0013, 0x003C, -0x004B, 0x00A2,
|
|
|
|
-0x00E3, 0x0132, -0x0043, -0x0267, 0x0C9D, 0x74BB, -0x11B4, 0x09B8, -0x05BF, 0x0372,
|
|
|
|
-0x01A8, 0x00A6, -0x001B, 0x0005, 0x0006, -0x0008, 0x0003, -0x0001, 0x0},
|
|
|
|
{0, 0x0, -0x0001, 0x0003, -0x0002, -0x0005, 0x001F, -0x004A, 0x00B3, -0x0192,
|
|
|
|
0x02B1, -0x039E, 0x04F8, -0x05A6, 0x7939, -0x05A6, 0x04F8, -0x039E, 0x02B1, -0x0192,
|
|
|
|
0x00B3, -0x004A, 0x001F, -0x0005, -0x0002, 0x0003, -0x0001, 0x0, 0x0},
|
|
|
|
{0, -0x0001, 0x0003, -0x0008, 0x0006, 0x0005, -0x001B, 0x00A6, -0x01A8, 0x0372,
|
|
|
|
-0x05BF, 0x09B8, -0x11B4, 0x74BB, 0x0C9D, -0x0267, -0x0043, 0x0132, -0x00E3, 0x00A2,
|
|
|
|
-0x004B, 0x003C, -0x0013, 0x0003, 0x0, -0x0002, 0x0, 0x0, 0x0},
|
|
|
|
{-0x0001, 0x0003, -0x0008, 0x0011, -0x0010, 0x000A, 0x006B, -0x016D, 0x0350, -0x0623,
|
|
|
|
0x0BCD, -0x1780, 0x6794, 0x234C, -0x0A78, 0x0400, -0x010A, 0x0009, 0x0034, -0x0054,
|
|
|
|
0x0041, -0x0022, 0x000A, -0x0001, 0x0, 0x0001, 0x0, 0x0, 0x0},
|
|
|
|
{0x0002, -0x0008, 0x0010, -0x0023, 0x002B, 0x001A, -0x00EB, 0x027B, -0x0548, 0x0AFA,
|
|
|
|
-0x16FA, 0x53E0, 0x3C07, -0x1249, 0x080E, -0x0347, 0x015B, -0x0044, -0x0017, 0x0046,
|
|
|
|
-0x0023, 0x0011, -0x0005, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
|
|
|
|
{-0x0005, 0x0011, -0x0023, 0x0046, -0x0017, -0x0044, 0x015B, -0x0347, 0x080E, -0x1249,
|
|
|
|
0x3C07, 0x53E0, -0x16FA, 0x0AFA, -0x0548, 0x027B, -0x00EB, 0x001A, 0x002B, -0x0023,
|
|
|
|
0x0010, -0x0008, 0x0002, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}};
|
|
|
|
|
|
|
|
static s16 ZigZagInterpolate(const s16* ringbuf, const s16* table, u8 p)
|
|
|
|
{
|
|
|
|
s32 sum = 0;
|
|
|
|
for (u8 i = 0; i < 29; i++)
|
|
|
|
sum += (s32(ringbuf[(p - i) & 0x1F]) * s32(table[i])) / 0x8000;
|
|
|
|
|
|
|
|
return static_cast<s16>(std::clamp<s32>(sum, -0x8000, 0x7FFF));
|
|
|
|
}
|
|
|
|
|
2019-12-05 14:33:46 +00:00
|
|
|
static constexpr s32 ApplyVolume(s16 sample, u8 volume)
|
2019-10-15 07:27:35 +00:00
|
|
|
{
|
2019-12-05 14:33:46 +00:00
|
|
|
return s32(sample) * static_cast<s32>(ZeroExtend32(volume)) >> 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr s16 SaturateVolume(s32 volume)
|
|
|
|
{
|
|
|
|
return static_cast<s16>(std::clamp<s32>(volume, -0x8000, 0x7FFF));
|
2019-10-15 07:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<bool STEREO, bool SAMPLE_RATE>
|
2020-07-21 14:03:44 +00:00
|
|
|
void CDROM::ResampleXAADPCM(const s16* frames_in, u32 num_frames_in)
|
2019-10-15 07:27:35 +00:00
|
|
|
{
|
2020-07-21 14:03:44 +00:00
|
|
|
// Since the disc reads and SPU are running at different speeds, we might be _slightly_ behind, which is fine, since
|
|
|
|
// the SPU will over-read in the next batch to catch up.
|
|
|
|
if (m_audio_fifo.GetSize() > AUDIO_FIFO_LOW_WATERMARK)
|
|
|
|
{
|
|
|
|
Log_DevPrintf("Dropping %u XA frames because audio FIFO still has %u frames", num_frames_in,
|
|
|
|
m_audio_fifo.GetSize());
|
|
|
|
return;
|
|
|
|
}
|
2020-03-07 07:29:38 +00:00
|
|
|
|
2020-07-21 14:03:44 +00:00
|
|
|
s16* left_ringbuf = m_xa_resample_ring_buffer[0].data();
|
|
|
|
s16* right_ringbuf = m_xa_resample_ring_buffer[1].data();
|
|
|
|
u8 p = m_xa_resample_p;
|
|
|
|
u8 sixstep = m_xa_resample_sixstep;
|
2020-03-07 07:29:38 +00:00
|
|
|
for (u32 in_sample_index = 0; in_sample_index < num_frames_in; in_sample_index++)
|
2019-10-15 07:27:35 +00:00
|
|
|
{
|
2020-03-07 07:29:38 +00:00
|
|
|
const s16 left = *(frames_in++);
|
|
|
|
const s16 right = STEREO ? *(frames_in++) : left;
|
2019-10-15 07:27:35 +00:00
|
|
|
|
2020-01-08 22:46:52 +00:00
|
|
|
if constexpr (!STEREO)
|
|
|
|
{
|
2020-01-10 03:31:12 +00:00
|
|
|
UNREFERENCED_VARIABLE(right);
|
2020-01-08 22:46:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:27:35 +00:00
|
|
|
for (u32 sample_dup = 0; sample_dup < (SAMPLE_RATE ? 2 : 1); sample_dup++)
|
|
|
|
{
|
|
|
|
left_ringbuf[p] = left;
|
|
|
|
if constexpr (STEREO)
|
|
|
|
right_ringbuf[p] = right;
|
|
|
|
p = (p + 1) % 32;
|
|
|
|
sixstep--;
|
|
|
|
|
|
|
|
if (sixstep == 0)
|
|
|
|
{
|
|
|
|
sixstep = 6;
|
|
|
|
for (u32 j = 0; j < 7; j++)
|
|
|
|
{
|
|
|
|
const s16 left_interp = ZigZagInterpolate(left_ringbuf, s_zigzag_table[j].data(), p);
|
|
|
|
const s16 right_interp = STEREO ? ZigZagInterpolate(right_ringbuf, s_zigzag_table[j].data(), p) : left_interp;
|
|
|
|
|
2020-07-21 14:03:44 +00:00
|
|
|
const s16 left_out = SaturateVolume(ApplyVolume(left_interp, m_cd_audio_volume_matrix[0][0]) +
|
|
|
|
ApplyVolume(right_interp, m_cd_audio_volume_matrix[1][0]));
|
|
|
|
const s16 right_out = SaturateVolume(ApplyVolume(left_interp, m_cd_audio_volume_matrix[0][1]) +
|
|
|
|
ApplyVolume(right_interp, m_cd_audio_volume_matrix[1][1]));
|
|
|
|
AddCDAudioFrame(left_out, right_out);
|
2019-10-15 07:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 14:03:44 +00:00
|
|
|
m_xa_resample_p = p;
|
|
|
|
m_xa_resample_sixstep = sixstep;
|
2019-10-15 07:27:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 15:39:43 +00:00
|
|
|
void CDROM::ResetCurrentXAFile()
|
|
|
|
{
|
|
|
|
m_xa_current_channel_number = 0;
|
|
|
|
m_xa_current_file_number = 0;
|
|
|
|
m_xa_current_set = false;
|
|
|
|
}
|
|
|
|
|
2020-07-21 14:03:44 +00:00
|
|
|
void CDROM::ResetAudioDecoder()
|
2020-05-12 02:36:08 +00:00
|
|
|
{
|
2020-07-29 15:55:35 +00:00
|
|
|
ResetCurrentXAFile();
|
|
|
|
|
2020-05-12 02:36:08 +00:00
|
|
|
m_xa_last_samples.fill(0);
|
|
|
|
for (u32 i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
m_xa_resample_ring_buffer[i].fill(0);
|
|
|
|
m_xa_resample_p = 0;
|
|
|
|
m_xa_resample_sixstep = 6;
|
|
|
|
}
|
2020-07-21 14:03:44 +00:00
|
|
|
m_audio_fifo.Clear();
|
2020-05-12 02:36:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 10:31:59 +00:00
|
|
|
void CDROM::ProcessXAADPCMSector(const u8* raw_sector, const CDImage::SubChannelQ& subq)
|
2019-10-15 07:27:35 +00:00
|
|
|
{
|
2020-04-04 15:39:43 +00:00
|
|
|
// Check for automatic ADPCM filter.
|
|
|
|
if (m_mode.xa_filter && (m_last_sector_subheader.file_number != m_xa_filter_file_number ||
|
|
|
|
m_last_sector_subheader.channel_number != m_xa_filter_channel_number))
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Skipping sector due to filter mismatch (expected %u/%u got %u/%u)", m_xa_filter_file_number,
|
|
|
|
m_xa_filter_channel_number, m_last_sector_subheader.file_number,
|
|
|
|
m_last_sector_subheader.channel_number);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the current file being played. If this is not set by the filter, it'll be set by the first file/sector which
|
|
|
|
// is read. Fixes audio in Tomb Raider III menu.
|
|
|
|
if (!m_xa_current_set)
|
|
|
|
{
|
2020-07-01 11:43:24 +00:00
|
|
|
// Some games (Taxi 2 and Blues Blues) have junk audio sectors with a channel number of 255.
|
|
|
|
// We need to skip them otherwise it ends up playing the incorrect file.
|
|
|
|
// TODO: Verify with a hardware test.
|
|
|
|
if (m_last_sector_subheader.channel_number == 255 && (!m_mode.xa_filter || m_xa_filter_channel_number != 255))
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Skipping XA file with file number %u and channel number %u (submode 0x%02X coding 0x%02X)",
|
|
|
|
m_last_sector_subheader.file_number, m_last_sector_subheader.channel_number,
|
|
|
|
m_last_sector_subheader.submode.bits, m_last_sector_subheader.codinginfo.bits);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:55:35 +00:00
|
|
|
ResetAudioDecoder();
|
2020-04-04 15:39:43 +00:00
|
|
|
m_xa_current_file_number = m_last_sector_subheader.file_number;
|
|
|
|
m_xa_current_channel_number = m_last_sector_subheader.channel_number;
|
|
|
|
m_xa_current_set = true;
|
|
|
|
}
|
|
|
|
else if (m_last_sector_subheader.file_number != m_xa_current_file_number ||
|
|
|
|
m_last_sector_subheader.channel_number != m_xa_current_channel_number)
|
|
|
|
{
|
|
|
|
Log_DebugPrintf("Skipping sector due to current file mismatch (expected %u/%u got %u/%u)", m_xa_current_file_number,
|
|
|
|
m_xa_current_channel_number, m_last_sector_subheader.file_number,
|
|
|
|
m_last_sector_subheader.channel_number);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset current file on EOF, and play the file in the next sector.
|
|
|
|
if (m_last_sector_subheader.submode.eof)
|
|
|
|
ResetCurrentXAFile();
|
|
|
|
|
2019-10-15 07:27:35 +00:00
|
|
|
std::array<s16, CDXA::XA_ADPCM_SAMPLES_PER_SECTOR_4BIT> sample_buffer;
|
2019-10-27 03:44:23 +00:00
|
|
|
CDXA::DecodeADPCMSector(raw_sector, sample_buffer.data(), m_xa_last_samples.data());
|
2019-10-15 07:27:35 +00:00
|
|
|
|
|
|
|
// Only send to SPU if we're not muted.
|
2020-10-03 02:24:03 +00:00
|
|
|
if (m_muted || m_adpcm_muted || g_settings.cdrom_mute_cd_audio)
|
2019-10-15 07:27:35 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_spu.GeneratePendingSamples();
|
2020-03-07 07:29:38 +00:00
|
|
|
|
2019-10-15 07:27:35 +00:00
|
|
|
if (m_last_sector_subheader.codinginfo.IsStereo())
|
|
|
|
{
|
|
|
|
const u32 num_samples = m_last_sector_subheader.codinginfo.GetSamplesPerSector() / 2;
|
|
|
|
if (m_last_sector_subheader.codinginfo.IsHalfSampleRate())
|
2020-07-21 14:03:44 +00:00
|
|
|
ResampleXAADPCM<true, true>(sample_buffer.data(), num_samples);
|
2019-10-15 07:27:35 +00:00
|
|
|
else
|
2020-07-21 14:03:44 +00:00
|
|
|
ResampleXAADPCM<true, false>(sample_buffer.data(), num_samples);
|
2019-10-15 07:27:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const u32 num_samples = m_last_sector_subheader.codinginfo.GetSamplesPerSector();
|
|
|
|
if (m_last_sector_subheader.codinginfo.IsHalfSampleRate())
|
2020-07-21 14:03:44 +00:00
|
|
|
ResampleXAADPCM<false, true>(sample_buffer.data(), num_samples);
|
2019-10-15 07:27:35 +00:00
|
|
|
else
|
2020-07-21 14:03:44 +00:00
|
|
|
ResampleXAADPCM<false, false>(sample_buffer.data(), num_samples);
|
2019-10-15 07:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 10:31:59 +00:00
|
|
|
void CDROM::ProcessCDDASector(const u8* raw_sector, const CDImage::SubChannelQ& subq)
|
2019-10-20 05:55:23 +00:00
|
|
|
{
|
|
|
|
// For CDDA sectors, the whole sector contains the audio data.
|
2020-05-19 15:52:53 +00:00
|
|
|
Log_DevPrintf("Read sector %u as CDDA", m_current_lba);
|
2019-10-20 05:55:23 +00:00
|
|
|
|
2020-08-23 10:01:50 +00:00
|
|
|
// The reporting doesn't happen if we're reading with the CDDA mode bit set.
|
|
|
|
if (m_drive_state == DriveState::Playing && m_mode.report_audio)
|
2019-11-10 13:03:52 +00:00
|
|
|
{
|
2020-08-23 10:01:50 +00:00
|
|
|
const u8 frame_nibble = subq.absolute_frame_bcd >> 4;
|
|
|
|
if (m_last_cdda_report_frame_nibble != frame_nibble)
|
2019-11-10 13:03:52 +00:00
|
|
|
{
|
2020-08-23 10:01:50 +00:00
|
|
|
m_last_cdda_report_frame_nibble = frame_nibble;
|
2019-11-10 13:03:52 +00:00
|
|
|
|
2020-08-23 10:01:50 +00:00
|
|
|
Log_DebugPrintf("CDDA report at track[%02x] index[%02x] rel[%02x:%02x:%02x]", subq.track_number_bcd,
|
|
|
|
subq.index_number_bcd, subq.relative_minute_bcd, subq.relative_second_bcd,
|
|
|
|
subq.relative_frame_bcd);
|
2019-11-10 13:03:52 +00:00
|
|
|
|
2020-08-23 10:01:50 +00:00
|
|
|
ClearAsyncInterrupt();
|
|
|
|
m_async_response_fifo.Push(m_secondary_status.bits);
|
|
|
|
m_async_response_fifo.Push(subq.track_number_bcd);
|
|
|
|
m_async_response_fifo.Push(subq.index_number_bcd);
|
|
|
|
if (subq.absolute_frame_bcd & 0x10)
|
|
|
|
{
|
|
|
|
m_async_response_fifo.Push(subq.relative_minute_bcd);
|
|
|
|
m_async_response_fifo.Push(0x80 | subq.relative_second_bcd);
|
|
|
|
m_async_response_fifo.Push(subq.relative_frame_bcd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_async_response_fifo.Push(subq.absolute_minute_bcd);
|
|
|
|
m_async_response_fifo.Push(subq.absolute_second_bcd);
|
|
|
|
m_async_response_fifo.Push(subq.absolute_frame_bcd);
|
2020-07-22 05:23:13 +00:00
|
|
|
}
|
2020-08-23 10:01:50 +00:00
|
|
|
|
|
|
|
m_async_response_fifo.Push(0); // peak low
|
|
|
|
m_async_response_fifo.Push(0); // peak high
|
|
|
|
SetAsyncInterrupt(Interrupt::DataReady);
|
2019-11-10 13:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 05:55:23 +00:00
|
|
|
|
2019-10-27 03:44:23 +00:00
|
|
|
// Apply volume when pushing sectors to SPU.
|
2020-10-03 02:24:03 +00:00
|
|
|
if (m_muted || g_settings.cdrom_mute_cd_audio)
|
2019-10-27 03:44:23 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
g_spu.GeneratePendingSamples();
|
2020-03-07 07:29:38 +00:00
|
|
|
|
2019-10-27 03:44:23 +00:00
|
|
|
constexpr bool is_stereo = true;
|
2020-03-21 02:30:57 +00:00
|
|
|
constexpr u32 num_samples = CDImage::RAW_SECTOR_SIZE / sizeof(s16) / (is_stereo ? 2 : 1);
|
2020-07-21 14:03:44 +00:00
|
|
|
const u32 remaining_space = m_audio_fifo.GetSpace();
|
|
|
|
if (remaining_space < num_samples)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Dropping %u frames from audio FIFO", num_samples - remaining_space);
|
|
|
|
m_audio_fifo.Remove(num_samples - remaining_space);
|
|
|
|
}
|
2019-10-27 03:44:23 +00:00
|
|
|
|
|
|
|
const u8* sector_ptr = raw_sector;
|
|
|
|
for (u32 i = 0; i < num_samples; i++)
|
|
|
|
{
|
|
|
|
s16 samp_left, samp_right;
|
|
|
|
std::memcpy(&samp_left, sector_ptr, sizeof(samp_left));
|
|
|
|
std::memcpy(&samp_right, sector_ptr + sizeof(s16), sizeof(samp_right));
|
|
|
|
sector_ptr += sizeof(s16) * 2;
|
|
|
|
|
2019-12-05 14:33:46 +00:00
|
|
|
const s16 left = SaturateVolume(ApplyVolume(samp_left, m_cd_audio_volume_matrix[0][0]) +
|
2020-03-18 14:28:57 +00:00
|
|
|
ApplyVolume(samp_right, m_cd_audio_volume_matrix[1][0]));
|
|
|
|
const s16 right = SaturateVolume(ApplyVolume(samp_left, m_cd_audio_volume_matrix[0][1]) +
|
2019-12-05 14:33:46 +00:00
|
|
|
ApplyVolume(samp_right, m_cd_audio_volume_matrix[1][1]));
|
2020-07-21 14:03:44 +00:00
|
|
|
AddCDAudioFrame(left, right);
|
2019-10-27 03:44:23 +00:00
|
|
|
}
|
2019-10-20 05:55:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 10:01:41 +00:00
|
|
|
void CDROM::LoadDataFIFO()
|
|
|
|
{
|
2020-03-12 05:32:41 +00:00
|
|
|
if (!m_data_fifo.IsEmpty())
|
2019-09-30 10:01:41 +00:00
|
|
|
{
|
2020-03-12 05:32:41 +00:00
|
|
|
Log_DevPrintf("Load data fifo when not empty");
|
2019-09-30 10:01:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:32:41 +00:00
|
|
|
// any data to load?
|
2020-03-28 15:13:25 +00:00
|
|
|
SectorBuffer& sb = m_sector_buffers[m_current_read_sector_buffer];
|
2020-03-12 05:32:41 +00:00
|
|
|
if (sb.size == 0)
|
|
|
|
{
|
|
|
|
Log_WarningPrintf("Attempting to load empty sector buffer");
|
|
|
|
m_data_fifo.PushRange(sb.data.data(), RAW_SECTOR_OUTPUT_SIZE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-28 15:13:25 +00:00
|
|
|
m_data_fifo.PushRange(sb.data.data(), sb.size);
|
2020-03-12 05:32:41 +00:00
|
|
|
sb.size = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:55:13 +00:00
|
|
|
Log_DebugPrintf("Loaded %u bytes to data FIFO from buffer %u", m_data_fifo.GetSize(), m_current_read_sector_buffer);
|
2020-03-28 15:13:25 +00:00
|
|
|
m_current_read_sector_buffer = m_current_write_sector_buffer;
|
2019-09-30 10:01:41 +00:00
|
|
|
}
|
2019-10-17 13:54:51 +00:00
|
|
|
|
2020-03-12 05:32:41 +00:00
|
|
|
void CDROM::ClearSectorBuffers()
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < NUM_SECTOR_BUFFERS; i++)
|
|
|
|
m_sector_buffers[i].size = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-17 13:54:51 +00:00
|
|
|
void CDROM::DrawDebugWindow()
|
|
|
|
{
|
2020-09-01 02:13:56 +00:00
|
|
|
#ifdef WITH_IMGUI
|
2019-10-18 08:18:04 +00:00
|
|
|
static const ImVec4 active_color{1.0f, 1.0f, 1.0f, 1.0f};
|
|
|
|
static const ImVec4 inactive_color{0.4f, 0.4f, 0.4f, 1.0f};
|
2020-02-28 06:59:51 +00:00
|
|
|
const float framebuffer_scale = ImGui::GetIO().DisplayFramebufferScale.x;
|
2019-10-18 08:18:04 +00:00
|
|
|
|
2020-07-29 14:51:12 +00:00
|
|
|
ImGui::SetNextWindowSize(ImVec2(800.0f * framebuffer_scale, 550.0f * framebuffer_scale), ImGuiCond_FirstUseEver);
|
2020-07-31 07:09:18 +00:00
|
|
|
if (!ImGui::Begin("CDROM State", &g_settings.debugging.show_cdrom_state))
|
2019-10-17 13:54:51 +00:00
|
|
|
{
|
|
|
|
ImGui::End();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw voice states
|
|
|
|
if (ImGui::CollapsingHeader("Media", ImGuiTreeNodeFlags_DefaultOpen))
|
|
|
|
{
|
2020-05-19 16:26:06 +00:00
|
|
|
if (m_reader.HasMedia())
|
2019-10-17 13:54:51 +00:00
|
|
|
{
|
2020-02-21 15:19:10 +00:00
|
|
|
const CDImage* media = m_reader.GetMedia();
|
2020-07-12 16:23:56 +00:00
|
|
|
const CDImage::Position disc_position = CDImage::Position::FromLBA(m_current_lba);
|
|
|
|
const CDImage::Position track_position = CDImage::Position::FromLBA(
|
|
|
|
m_current_lba - media->GetTrackStartPosition(static_cast<u8>(media->GetTrackNumber())));
|
2019-10-17 13:54:51 +00:00
|
|
|
|
2020-02-21 15:19:10 +00:00
|
|
|
ImGui::Text("Filename: %s", media->GetFileName().c_str());
|
2020-07-12 16:23:56 +00:00
|
|
|
ImGui::Text("Disc Position: MSF[%02u:%02u:%02u] LBA[%u]", disc_position.minute, disc_position.second,
|
|
|
|
disc_position.frame, disc_position.ToLBA());
|
|
|
|
ImGui::Text("Track Position: Number[%u] MSF[%02u:%02u:%02u] LBA[%u]", media->GetTrackNumber(),
|
|
|
|
track_position.minute, track_position.second, track_position.frame, track_position.ToLBA());
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::Text("Last Sector: %02X:%02X:%02X (Mode %u)", m_last_sector_header.minute, m_last_sector_header.second,
|
|
|
|
m_last_sector_header.frame, m_last_sector_header.sector_mode);
|
2019-10-17 13:54:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImGui::Text("No media inserted.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::CollapsingHeader("Status/Mode", ImGuiTreeNodeFlags_DefaultOpen))
|
|
|
|
{
|
2020-05-19 16:26:06 +00:00
|
|
|
static constexpr std::array<const char*, 12> drive_state_names = {
|
|
|
|
{"Idle", "Opening Shell", "Resetting", "Seeking (Physical)", "Seeking (Logical)", "Reading ID", "Reading TOC",
|
|
|
|
"Reading", "Playing", "Pausing", "Stopping", "Changing Session"}};
|
2019-10-27 09:42:37 +00:00
|
|
|
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::Columns(3);
|
|
|
|
|
|
|
|
ImGui::Text("Status");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::Text("Secondary Status");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
ImGui::Text("Mode Status");
|
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_status.ADPBUSY ? active_color : inactive_color, "ADPBUSY: %s",
|
|
|
|
m_status.ADPBUSY ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.error ? active_color : inactive_color, "Error: %s",
|
|
|
|
m_secondary_status.error ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.cdda ? active_color : inactive_color, "CDDA: %s", m_mode.cdda ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_status.PRMEMPTY ? active_color : inactive_color, "PRMEMPTY: %s",
|
|
|
|
m_status.PRMEMPTY ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.motor_on ? active_color : inactive_color, "Motor On: %s",
|
|
|
|
m_secondary_status.motor_on ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.auto_pause ? active_color : inactive_color, "Auto Pause: %s",
|
|
|
|
m_mode.auto_pause ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_status.PRMWRDY ? active_color : inactive_color, "PRMWRDY: %s",
|
|
|
|
m_status.PRMWRDY ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.seek_error ? active_color : inactive_color, "Seek Error: %s",
|
|
|
|
m_secondary_status.seek_error ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.report_audio ? active_color : inactive_color, "Report Audio: %s",
|
|
|
|
m_mode.report_audio ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_status.RSLRRDY ? active_color : inactive_color, "RSLRRDY: %s",
|
|
|
|
m_status.RSLRRDY ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.id_error ? active_color : inactive_color, "ID Error: %s",
|
|
|
|
m_secondary_status.id_error ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.xa_filter ? active_color : inactive_color, "XA Filter: %s (File %u Channel %u)",
|
2020-04-04 15:39:43 +00:00
|
|
|
m_mode.xa_filter ? "Yes" : "No", m_xa_filter_file_number, m_xa_filter_channel_number);
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_status.DRQSTS ? active_color : inactive_color, "DRQSTS: %s", m_status.DRQSTS ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.shell_open ? active_color : inactive_color, "Shell Open: %s",
|
|
|
|
m_secondary_status.shell_open ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.ignore_bit ? active_color : inactive_color, "Ignore Bit: %s",
|
|
|
|
m_mode.ignore_bit ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_status.BUSYSTS ? active_color : inactive_color, "BUSYSTS: %s",
|
|
|
|
m_status.BUSYSTS ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2020-03-29 07:41:31 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.reading ? active_color : inactive_color, "Reading: %s",
|
|
|
|
m_secondary_status.reading ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.read_raw_sector ? active_color : inactive_color, "Read Raw Sectors: %s",
|
|
|
|
m_mode.read_raw_sector ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.seeking ? active_color : inactive_color, "Seeking: %s",
|
|
|
|
m_secondary_status.seeking ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.xa_enable ? active_color : inactive_color, "XA Enable: %s",
|
|
|
|
m_mode.xa_enable ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_secondary_status.playing_cdda ? active_color : inactive_color, "Playing CDDA: %s",
|
|
|
|
m_secondary_status.playing_cdda ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_mode.double_speed ? active_color : inactive_color, "Double Speed: %s",
|
|
|
|
m_mode.double_speed ? "Yes" : "No");
|
2019-10-17 13:54:51 +00:00
|
|
|
ImGui::NextColumn();
|
|
|
|
|
|
|
|
ImGui::Columns(1);
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::NewLine();
|
|
|
|
|
2019-10-27 09:42:37 +00:00
|
|
|
if (HasPendingCommand())
|
|
|
|
{
|
|
|
|
ImGui::TextColored(active_color, "Command: 0x%02X (%d ticks remaining)", static_cast<u8>(m_command),
|
2020-01-24 04:53:40 +00:00
|
|
|
m_command_event->IsActive() ? m_command_event->GetTicksUntilNextExecution() : 0);
|
2019-10-27 09:42:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImGui::TextColored(inactive_color, "Command: None");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsDriveIdle())
|
|
|
|
{
|
|
|
|
ImGui::TextColored(inactive_color, "Drive: Idle");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImGui::TextColored(active_color, "Drive: %s (%d ticks remaining)",
|
2020-01-24 04:53:40 +00:00
|
|
|
drive_state_names[static_cast<u8>(m_drive_state)],
|
|
|
|
m_drive_event->IsActive() ? m_drive_event->GetTicksUntilNextExecution() : 0);
|
2019-10-27 09:42:37 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::Text("Interrupt Enable Register: 0x%02X", m_interrupt_enable_register);
|
|
|
|
ImGui::Text("Interrupt Flag Register: 0x%02X", m_interrupt_flag_register);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::CollapsingHeader("CD Audio", ImGuiTreeNodeFlags_DefaultOpen))
|
|
|
|
{
|
2020-04-05 14:25:49 +00:00
|
|
|
if (m_drive_state == DriveState::Reading && m_mode.xa_enable)
|
2020-04-04 15:39:43 +00:00
|
|
|
{
|
2020-04-05 14:25:49 +00:00
|
|
|
ImGui::TextColored(active_color, "Playing: XA-ADPCM (File %u / Channel %u)", m_xa_current_file_number,
|
|
|
|
m_xa_current_channel_number);
|
2020-04-04 15:39:43 +00:00
|
|
|
}
|
2020-04-05 14:25:49 +00:00
|
|
|
else if (m_drive_state == DriveState::Playing)
|
2020-04-04 15:39:43 +00:00
|
|
|
{
|
|
|
|
ImGui::TextColored(active_color, "Playing: CDDA (Track %x)", m_last_subq.track_number_bcd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ImGui::TextColored(inactive_color, "Playing: Inactive");
|
|
|
|
}
|
|
|
|
|
2019-10-18 08:18:04 +00:00
|
|
|
ImGui::TextColored(m_muted ? inactive_color : active_color, "Muted: %s", m_muted ? "Yes" : "No");
|
|
|
|
ImGui::Text("Left Output: Left Channel=%02X (%u%%), Right Channel=%02X (%u%%)", m_cd_audio_volume_matrix[0][0],
|
2020-03-18 14:28:57 +00:00
|
|
|
ZeroExtend32(m_cd_audio_volume_matrix[0][0]) * 100 / 0x80, m_cd_audio_volume_matrix[1][0],
|
|
|
|
ZeroExtend32(m_cd_audio_volume_matrix[1][0]) * 100 / 0x80);
|
|
|
|
ImGui::Text("Right Output: Left Channel=%02X (%u%%), Right Channel=%02X (%u%%)", m_cd_audio_volume_matrix[0][1],
|
|
|
|
ZeroExtend32(m_cd_audio_volume_matrix[0][1]) * 100 / 0x80, m_cd_audio_volume_matrix[1][1],
|
2019-10-18 08:18:04 +00:00
|
|
|
ZeroExtend32(m_cd_audio_volume_matrix[1][1]) * 100 / 0x80);
|
2020-07-29 14:51:12 +00:00
|
|
|
|
|
|
|
ImGui::Text("Audio FIFO Size: %u frames", m_audio_fifo.GetSize());
|
2019-10-17 13:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::End();
|
2020-09-01 02:13:56 +00:00
|
|
|
#endif
|
2019-10-17 13:54:51 +00:00
|
|
|
}
|