Skip to content

Commit

Permalink
Splitting cdrom-device.cpp up.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasnoble committed Sep 30, 2024
1 parent 7cbf597 commit ec82317
Show file tree
Hide file tree
Showing 6 changed files with 690 additions and 522 deletions.
176 changes: 176 additions & 0 deletions src/mips/psyqo/src/cdrom-device-cdda.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
MIT License
Copyright (c) 2022 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "psyqo/cdrom-device.hh"

#include <EASTL/atomic.h>

#include "psyqo/hardware/cdrom.hh"
#include "psyqo/kernel.hh"
#include "psyqo/msf.hh"

namespace {

enum class PlayCDDAActionState : uint8_t {
IDLE,
GETTD,
SETMODE,
SETLOC,
SEEK,
SEEK_ACK,
PLAY,
// needs to stay unique across all actions, and
// will be hardcoded in the pause command
PLAYING = 100,
STOPPING = 101,
STOPPING_ACK,
};

class PlayCDDAAction : public psyqo::CDRomDevice::Action<PlayCDDAActionState> {
public:
PlayCDDAAction() : Action("PlayCDDAAction") {}
void start(psyqo::CDRomDevice *device, unsigned track, bool stopAtEndOfTrack,
eastl::function<void(bool)> &&callback) {
psyqo::Kernel::assert(getState() == PlayCDDAActionState::IDLE,
"CDRomDevice::playCDDA() called while another action is in progress");
registerMe(device);
setCallback(eastl::move(callback));
setState(PlayCDDAActionState::GETTD);
m_stopAtEndOfTrack = stopAtEndOfTrack;
eastl::atomic_signal_fence(eastl::memory_order_release);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::GETTD, psyqo::itob(track));
}

Check warning on line 65 in src/mips/psyqo/src/cdrom-device-cdda.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Code Duplication

The module contains 3 functions with similar structure: start,start,start. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
void start(psyqo::CDRomDevice *device, psyqo::MSF msf, bool stopAtEndOfTrack,
eastl::function<void(bool)> &&callback) {
psyqo::Kernel::assert(getState() == PlayCDDAActionState::IDLE,
"CDRomDevice::playCDDA() called while another action is in progress");
registerMe(device);
setCallback(eastl::move(callback));
setState(PlayCDDAActionState::SEEK);
m_start = msf;
eastl::atomic_signal_fence(eastl::memory_order_release);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::SETMODE, stopAtEndOfTrack ? 0x02 : 0);
}
void start(psyqo::CDRomDevice *device, eastl::function<void(bool)> &&callback) {
psyqo::Kernel::assert(getState() == PlayCDDAActionState::IDLE,
"CDRomDevice::playCDDA() called while another action is in progress");
registerMe(device);
setCallback(eastl::move(callback));
setState(PlayCDDAActionState::PLAY);
eastl::atomic_signal_fence(eastl::memory_order_release);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::PLAY);
}
bool complete(const psyqo::CDRomDevice::Response &) override {
switch (getState()) {
case PlayCDDAActionState::SEEK_ACK:
setState(PlayCDDAActionState::PLAY);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::PLAY);
break;
case PlayCDDAActionState::STOPPING_ACK:
setSuccess(true);
return true;
default:
psyqo::Kernel::abort("PlayCDDAAction got CDROM complete in wrong state");
break;
}
return false;
}
bool acknowledge(const psyqo::CDRomDevice::Response &response) override {
switch (getState()) {
case PlayCDDAActionState::GETTD:
m_start.m = psyqo::btoi(response[1]);
m_start.s = psyqo::btoi(response[2]);
m_start.f = 0;
setState(PlayCDDAActionState::SETMODE);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::SETMODE,
m_stopAtEndOfTrack ? 0x02 : 0);
break;
case PlayCDDAActionState::SETMODE:
setState(PlayCDDAActionState::SETLOC);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::SETLOC, m_start.m, m_start.s,
m_start.f);
break;
case PlayCDDAActionState::SETLOC:
setState(PlayCDDAActionState::SEEK);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::SEEKP);
break;
case PlayCDDAActionState::SEEK:
setState(PlayCDDAActionState::SEEK_ACK);
break;
case PlayCDDAActionState::PLAY:
setState(PlayCDDAActionState::PLAYING);
queueCallbackFromISR(true);
break;
case PlayCDDAActionState::STOPPING:
setState(PlayCDDAActionState::STOPPING_ACK);
break;
default:
psyqo::Kernel::abort("PlayCDDAAction got CDROM acknowledge in wrong state");
break;
}
return false;
}
bool end(const psyqo::CDRomDevice::Response &) override {
// We got raced to the end of the track and/or disc by the
// pause command, so we should just ignore this.
if (getState() == PlayCDDAActionState::STOPPING) return false;
psyqo::Kernel::assert(getState() == PlayCDDAActionState::PLAYING,
"PlayCDDAAction got CDROM end in wrong state");
setSuccess(true);
return true;
}
psyqo::MSF m_start;
bool m_stopAtEndOfTrack = false;
};

PlayCDDAAction s_playCDDAAction;

} // namespace

void psyqo::CDRomDevice::playCDDATrack(unsigned track, eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::playCDDATrack called with pending action");
s_playCDDAAction.start(this, track, true, eastl::move(callback));
}

void psyqo::CDRomDevice::playCDDATrack(MSF start, eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::playCDDATrack called with pending action");
s_playCDDAAction.start(this, start, true, eastl::move(callback));
}

void psyqo::CDRomDevice::playCDDADisc(unsigned track, eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::playCDDADisc called with pending action");
s_playCDDAAction.start(this, track, false, eastl::move(callback));
}

void psyqo::CDRomDevice::playCDDADisc(MSF start, eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::playCDDADisc called with pending action");
s_playCDDAAction.start(this, start, false, eastl::move(callback));
}

void psyqo::CDRomDevice::resumeCDDA(eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::resumeCDDA called with pending action");
s_playCDDAAction.start(this, eastl::move(callback));
}
108 changes: 108 additions & 0 deletions src/mips/psyqo/src/cdrom-device-muteunmute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
MIT License
Copyright (c) 2022 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "psyqo/cdrom-device.hh"

#include <EASTL/atomic.h>

#include "psyqo/hardware/cdrom.hh"
#include "psyqo/kernel.hh"

namespace {

enum class MuteActionState : uint8_t {
IDLE,
MUTE,
};

class MuteAction : public psyqo::CDRomDevice::Action<MuteActionState> {
public:
MuteAction() : Action("MuteAction") {}
void start(psyqo::CDRomDevice *device, eastl::function<void(bool)> &&callback) {
psyqo::Kernel::assert(getState() == MuteActionState::IDLE,
"CDRomDevice::mute() called while another action is in progress");
registerMe(device);
setCallback(eastl::move(callback));
setState(MuteActionState::MUTE);
eastl::atomic_signal_fence(eastl::memory_order_release);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::MUTE);
}

Check warning on line 52 in src/mips/psyqo/src/cdrom-device-muteunmute.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Code Duplication

The module contains 2 functions with similar structure: start,start. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.
bool complete(const psyqo::CDRomDevice::Response &) override {
setSuccess(true);
return true;
}
};

MuteAction s_muteAction;

} // namespace

void psyqo::CDRomDevice::mute(eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::mute called with pending action");
s_muteAction.start(this, eastl::move(callback));
}

psyqo::TaskQueue::Task psyqo::CDRomDevice::scheduleMute() {
return TaskQueue::Task([this](auto task) { mute([task](bool success) { task->complete(success); }); });
}

namespace {

enum class UnmuteActionState : uint8_t {
IDLE,
UNMUTE,
};

class UnmuteAction : public psyqo::CDRomDevice::Action<UnmuteActionState> {
public:
UnmuteAction() : Action("UnmuteAction") {}
void start(psyqo::CDRomDevice *device, eastl::function<void(bool)> &&callback) {
psyqo::Kernel::assert(getState() == UnmuteActionState::IDLE,
"CDRomDevice::unmute() called while another action is in progress");
registerMe(device);
setCallback(eastl::move(callback));
setState(UnmuteActionState::UNMUTE);
eastl::atomic_signal_fence(eastl::memory_order_release);
psyqo::Hardware::CDRom::Command.send(psyqo::Hardware::CDRom::CDL::UNMUTE);
}
bool complete(const psyqo::CDRomDevice::Response &) override {
setSuccess(true);
return true;
}
};

UnmuteAction s_unmuteAction;

} // namespace

void psyqo::CDRomDevice::unmute(eastl::function<void(bool)> &&callback) {
Kernel::assert(m_callback == nullptr, "CDRomDevice::unmute called with pending action");
s_unmuteAction.start(this, eastl::move(callback));
}

psyqo::TaskQueue::Task psyqo::CDRomDevice::scheduleUnmute() {
return TaskQueue::Task([this](auto task) { unmute([task](bool success) { task->complete(success); }); });
}
Loading

0 comments on commit ec82317

Please sign in to comment.