Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay Y2R completion events #524

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/kernel/kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "services/service_manager.hpp"

class CPU;
struct Scheduler;

class Kernel {
std::span<u32, 16> regs;
Expand Down Expand Up @@ -243,6 +244,7 @@ class Kernel {
}

ServiceManager& getServiceManager() { return serviceManager; }
Scheduler& getScheduler();

void sendGPUInterrupt(GPUInterrupt type) { serviceManager.sendGPUInterrupt(type); }
void clearInstructionCache();
Expand Down
3 changes: 2 additions & 1 deletion include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ struct Scheduler {
VBlank = 0, // End of frame event
UpdateTimers = 1, // Update kernel timer objects
RunDSP = 2, // Make the emulated DSP run for one audio frame
Panic = 3, // Dummy event that is always pending and should never be triggered (Timestamp = UINT64_MAX)
SignalY2R = 3, // Signal that a Y2R conversion has finished
Panic = 4, // Dummy event that is always pending and should never be triggered (Timestamp = UINT64_MAX)
TotalNumberOfEvents // How many event types do we have in total?
};
static constexpr usize totalNumberOfEvents = static_cast<usize>(EventType::TotalNumberOfEvents);
Expand Down
1 change: 1 addition & 0 deletions include/services/service_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,5 @@ class ServiceManager {
HIDService& getHID() { return hid; }
NFCService& getNFC() { return nfc; }
DSPService& getDSP() { return dsp; }
Y2RService& getY2R() { return y2r; }
};
6 changes: 5 additions & 1 deletion include/services/y2r.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,12 @@ class Y2RService {
void startConversion(u32 messagePointer);
void stopConversion(u32 messagePointer);

public:
bool isBusy;

public:
Y2RService(Memory& mem, Kernel& kernel) : mem(mem), kernel(kernel) {}
void reset();
void handleSyncRequest(u32 messagePointer);

void signalConversionDone();
};
2 changes: 2 additions & 0 deletions src/core/kernel/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,5 @@ std::string Kernel::getProcessName(u32 pid) {
Helpers::panic("Attempted to name non-current process");
}
}

Scheduler& Kernel::getScheduler() { return cpu.getScheduler(); }
33 changes: 27 additions & 6 deletions src/core/services/y2r.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void Y2RService::reset() {
inputLineWidth = 420;

conversionCoefficients.fill(0);
isBusy = false;
}

void Y2RService::handleSyncRequest(u32 messagePointer) {
Expand Down Expand Up @@ -156,6 +157,11 @@ void Y2RService::setTransferEndInterrupt(u32 messagePointer) {
void Y2RService::stopConversion(u32 messagePointer) {
log("Y2R::StopConversion\n");

if (isBusy) {
isBusy = false;
kernel.getScheduler().removeEvent(Scheduler::EventType::SignalY2R);
}

mem.write32(messagePointer, IPC::responseHeader(0x27, 1, 0));
mem.write32(messagePointer + 4, Result::Success);
}
Expand All @@ -167,7 +173,7 @@ void Y2RService::isBusyConversion(u32 messagePointer) {

mem.write32(messagePointer, IPC::responseHeader(0x28, 2, 0));
mem.write32(messagePointer + 4, Result::Success);
mem.write32(messagePointer + 8, static_cast<u32>(BusyStatus::NotBusy));
mem.write32(messagePointer + 8, static_cast<u32>(isBusy ? BusyStatus::Busy : BusyStatus::NotBusy));
}

void Y2RService::setBlockAlignment(u32 messagePointer) {
Expand Down Expand Up @@ -434,11 +440,15 @@ void Y2RService::startConversion(u32 messagePointer) {
mem.write32(messagePointer, IPC::responseHeader(0x26, 1, 0));
mem.write32(messagePointer + 4, Result::Success);

// Make Y2R conversion end instantly.
// Signal the transfer end event if it's been created. TODO: Is this affected by SetTransferEndInterrupt?
if (transferEndEvent.has_value()) {
kernel.signalEvent(transferEndEvent.value());
}
// Schedule Y2R conversion end event.
// The tick value is tweaked based on the minimum delay needed to get FIFA 15 to not hang due to a race condition on its title screen
static constexpr u64 delayTicks = 1'350'000;
isBusy = true;

// Remove any potential pending Y2R event and schedule a new one
Scheduler& scheduler = kernel.getScheduler();
scheduler.removeEvent(Scheduler::EventType::SignalY2R);
scheduler.addEvent(Scheduler::EventType::SignalY2R, scheduler.currentTimestamp + delayTicks);
}

void Y2RService::isFinishedSendingYUV(u32 messagePointer) {
Expand Down Expand Up @@ -484,4 +494,15 @@ void Y2RService::isFinishedReceiving(u32 messagePointer) {
mem.write32(messagePointer, IPC::responseHeader(0x17, 2, 0));
mem.write32(messagePointer + 4, Result::Success);
mem.write32(messagePointer + 8, finished ? 1 : 0);
}

void Y2RService::signalConversionDone() {
if (isBusy) {
isBusy = false;

// Signal the transfer end event if it's been created. TODO: Is this affected by SetTransferEndInterrupt?
if (transferEndEvent.has_value()) {
kernel.signalEvent(transferEndEvent.value());
}
}
}
2 changes: 2 additions & 0 deletions src/emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ void Emulator::pollScheduler() {
break;
}

case Scheduler::EventType::SignalY2R: kernel.getServiceManager().getY2R().signalConversionDone(); break;

default: {
Helpers::panic("Scheduler: Unimplemented event type received: %d\n", static_cast<int>(eventType));
break;
Expand Down
Loading