|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2025 PCSX-Redux authors |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include "psyqo/cdrom-device.hh" |
| 28 | + |
| 29 | +#include <EASTL/atomic.h> |
| 30 | + |
| 31 | +#include "psyqo/cdrom-commandbuffer.hh" |
| 32 | +#include "psyqo/hardware/cdrom.hh" |
| 33 | +#include "psyqo/kernel.hh" |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +enum class TestActionState : uint8_t { |
| 38 | + IDLE, |
| 39 | + TEST, |
| 40 | +}; |
| 41 | + |
| 42 | +class TestAction : public psyqo::CDRomDevice::Action<TestActionState> { |
| 43 | + public: |
| 44 | + TestAction() : Action("TestAction") {} |
| 45 | + |
| 46 | + void start(psyqo::CDRomDevice *device, const psyqo::Hardware::CDRom::CDRomCommandBuffer &commandBuffer, |
| 47 | + eastl::function<void(bool)> &&callback) { |
| 48 | + psyqo::Kernel::assert(device->isIdle(), "CDRomDevice::test() called while another action is in progress"); |
| 49 | + registerMe(device); |
| 50 | + setCallback(eastl::move(callback)); |
| 51 | + setState(TestActionState::TEST); |
| 52 | + eastl::atomic_signal_fence(eastl::memory_order_release); |
| 53 | + psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::TEST, commandBuffer); |
| 54 | + } |
| 55 | + void start(psyqo::CDRomDevice *device, eastl::function<void(bool)> &&callback) { |
| 56 | + start(device, m_commandBuffer, eastl::move(callback)); |
| 57 | + } |
| 58 | + bool complete(const psyqo::CDRomDevice::Response &) override { |
| 59 | + setSuccess(true); |
| 60 | + return true; |
| 61 | + } |
| 62 | + bool acknowledge(const psyqo::CDRomDevice::Response &response) override { |
| 63 | + setSuccess(true); |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + psyqo::Hardware::CDRom::CDRomCommandBuffer m_commandBuffer; |
| 68 | +}; |
| 69 | + |
| 70 | +TestAction s_testAction; |
| 71 | + |
| 72 | +} // namespace |
| 73 | + |
| 74 | +void psyqo::CDRomDevice::test(const psyqo::Hardware::CDRom::CDRomCommandBuffer &commandBuffer, |
| 75 | + eastl::function<void(bool)> &&callback) { |
| 76 | + Kernel::assert(m_callback == nullptr, "CDRomDevice::test called with pending action"); |
| 77 | + s_testAction.start(this, commandBuffer, eastl::move(callback)); |
| 78 | +} |
| 79 | + |
| 80 | +psyqo::TaskQueue::Task psyqo::CDRomDevice::scheduleTest( |
| 81 | + const psyqo::Hardware::CDRom::CDRomCommandBuffer &commandBuffer) { |
| 82 | + s_testAction.m_commandBuffer = commandBuffer; |
| 83 | + |
| 84 | + return TaskQueue::Task( |
| 85 | + [this](auto task) { s_testAction.start(this, [task](bool success) { task->complete(success); }); }); |
| 86 | +} |
| 87 | + |
| 88 | +void psyqo::CDRomDevice::testBlocking(GPU &gpu, const psyqo::Hardware::CDRom::CDRomCommandBuffer &commandBuffer) { |
| 89 | + Kernel::assert(m_callback == nullptr, "CDRomDevice::testBlocking called with pending action"); |
| 90 | + bool success = false; |
| 91 | + { |
| 92 | + BlockingAction blocking(this, gpu); |
| 93 | + s_testAction.start(this, commandBuffer, [&success](bool success_) { success = success_; }); |
| 94 | + } |
| 95 | +} |
0 commit comments