Basic CD image loading

This commit is contained in:
Connor McLaughlin
2019-09-20 20:14:00 +10:00
parent 53e755aa68
commit ad652c47ed
12 changed files with 260 additions and 8 deletions

View File

@ -1,5 +1,6 @@
#include "cdrom.h"
#include "YBaseLib/Log.h"
#include "common/cd_image.h"
#include "common/state_wrapper.h"
#include "interrupt_controller.h"
Log_SetChannel(CDROM);
@ -43,6 +44,34 @@ bool CDROM::DoState(StateWrapper& sw)
return !sw.HasError();
}
bool CDROM::InsertMedia(const char* filename)
{
auto media = std::make_unique<CDImage>();
if (!media->Open(filename))
{
Log_ErrorPrintf("Failed to open media at '%s'", filename);
return false;
}
if (HasMedia())
RemoveMedia();
m_media = std::move(media);
m_secondary_status.shell_open = false;
return true;
}
void CDROM::RemoveMedia()
{
if (!m_media)
return;
// TODO: Error while reading?
Log_InfoPrintf("Removing CD...");
m_media.reset();
m_secondary_status.shell_open = true;
}
u8 CDROM::ReadRegister(u32 offset)
{
switch (offset)

View File

@ -3,6 +3,7 @@
#include "common/bitfield.h"
#include "common/fifo_queue.h"
class CDImage;
class StateWrapper;
class DMA;
@ -18,6 +19,10 @@ public:
void Reset();
bool DoState(StateWrapper& sw);
bool HasMedia() const { return static_cast<bool>(m_media); }
bool InsertMedia(const char* filename);
void RemoveMedia();
// I/O
u8 ReadRegister(u32 offset);
void WriteRegister(u32 offset, u8 value);
@ -84,6 +89,7 @@ private:
DMA* m_dma;
InterruptController* m_interrupt_controller;
std::unique_ptr<CDImage> m_media;
enum class State : u32
{

View File

@ -184,6 +184,9 @@ void Core::Branch(u32 target)
u32 Core::GetExceptionVector(Exception excode) const
{
const u32 base = m_cop0_regs.sr.BEV ? UINT32_C(0xbfc00100) : UINT32_C(0x80000000);
#if 0
// apparently this isn't correct...
switch (excode)
{
case Exception::BP:
@ -192,6 +195,9 @@ u32 Core::GetExceptionVector(Exception excode) const
default:
return base | UINT32_C(0x00000080);
}
#else
return base | UINT32_C(0x00000080);
#endif
}
void Core::RaiseException(Exception excode, u8 coprocessor /* = 0 */)
@ -344,14 +350,15 @@ bool Core::FetchInstruction()
{
m_regs.pc = m_regs.npc;
if (!DoAlignmentCheck<MemoryAccessType::Read, MemoryAccessSize::Word>(static_cast<VirtualMemoryAddress>(m_regs.npc)))
if (!DoAlignmentCheck<MemoryAccessType::Read, MemoryAccessSize::Word>(
static_cast<VirtualMemoryAddress>(m_regs.npc)) ||
!DoMemoryAccess<MemoryAccessType::Read, MemoryAccessSize::Word, true, true>(
static_cast<VirtualMemoryAddress>(m_regs.npc), m_next_instruction.bits))
{
// this will call FetchInstruction() again when the pipeline is flushed.
return false;
}
DoMemoryAccess<MemoryAccessType::Read, MemoryAccessSize::Word, true, true>(
static_cast<VirtualMemoryAddress>(m_regs.npc), m_next_instruction.bits);
m_regs.npc += sizeof(m_next_instruction.bits);
return true;
}

View File

@ -39,7 +39,7 @@ bool Core::DoMemoryAccess(VirtualMemoryAddress address, u32& value)
case 0x03: // KUSEG 1536M-2048M
{
// Above 512mb raises an exception.
Panic("Bad user access");
RaiseException(is_instruction_fetch ? Exception::IBE : Exception::DBE);
return false;
}
@ -95,7 +95,7 @@ bool Core::DoMemoryAccess(VirtualMemoryAddress address, u32& value)
}
else
{
Panic("KSEG2 access");
RaiseException(is_instruction_fetch ? Exception::IBE : Exception::DBE);
return false;
}
}

View File

@ -24,10 +24,19 @@ bool HostInterface::InitializeSystem(const char* filename, const char* save_stat
const StaticString filename_str(filename);
if (filename_str.EndsWith(".psxexe", false) || filename_str.EndsWith(".exe", false))
{
Log_InfoPrintf("Sideloading EXE file %s", filename);
Log_InfoPrintf("Sideloading EXE file '%s'", filename);
if (!m_system->LoadEXE(filename))
{
Log_ErrorPrintf("Failed to load EXE file %s", filename);
Log_ErrorPrintf("Failed to load EXE file '%s'", filename);
return false;
}
}
else
{
Log_InfoPrintf("Inserting CDROM from image file '%s'", filename);
if (!m_system->InsertMedia(filename))
{
Log_ErrorPrintf("Failed to insert media '%s'", filename);
return false;
}
}

View File

@ -213,3 +213,18 @@ void System::SetPadDevice(u32 slot, std::shared_ptr<PadDevice> dev)
{
m_pad->SetDevice(slot, std::move(dev));
}
bool System::HasMedia() const
{
return m_cdrom->HasMedia();
}
bool System::InsertMedia(const char* path)
{
return m_cdrom->InsertMedia(path);
}
void System::RemoveMedia()
{
m_cdrom->RemoveMedia();
}

View File

@ -45,6 +45,10 @@ public:
void SetPadDevice(u32 slot, std::shared_ptr<PadDevice> dev);
bool HasMedia() const;
bool InsertMedia(const char* path);
void RemoveMedia();
private:
bool DoState(StateWrapper& sw);