| 1 |
/* |
| 2 |
* Copyright (C) 2020-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "c86ctl_wrapper.hpp" |
| 27 |
#include <cmath> |
| 28 |
|
| 29 |
#include "c86ctl.h" |
| 30 |
|
| 31 |
C86ctl::C86ctl() : base_(nullptr), rc_(nullptr), gm_(nullptr) {} |
| 32 |
|
| 33 |
C86ctl::~C86ctl() |
| 34 |
{ |
| 35 |
if (gm_) gm_->Release(); |
| 36 |
if (rc_) rc_->Release(); |
| 37 |
if (base_) { |
| 38 |
base_->deinitialize(); |
| 39 |
base_->Release(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
bool C86ctl::createInstance(RealChipInterfaceGeneratorFunc* f) |
| 44 |
{ |
| 45 |
// Create instance |
| 46 |
auto tmpFunc = f ? reinterpret_cast<void*>((*f)()) : nullptr; // Avoid MSVC C4191 |
| 47 |
if (f) delete f; |
| 48 |
|
| 49 |
if (auto createInstance = reinterpret_cast<HRESULT(WINAPI*)(REFIID, void**)>(tmpFunc)) { |
| 50 |
c86ctl::IRealChipBase* base = nullptr; |
| 51 |
createInstance(c86ctl::IID_IRealChipBase, reinterpret_cast<void**>(&base)); |
| 52 |
if (base_) { |
| 53 |
base_->Release(); |
| 54 |
base_ = nullptr; |
| 55 |
if (!base) return false; |
| 56 |
} |
| 57 |
base_ = base; |
| 58 |
base_->initialize(); |
| 59 |
int nChip = base_->getNumberOfChip(); |
| 60 |
for (int i = 0; i < nChip; ++i) { |
| 61 |
c86ctl::IRealChip2* rc = nullptr; |
| 62 |
base_->getChipInterface(i, c86ctl::IID_IRealChip, reinterpret_cast<void**>(&rc)); |
| 63 |
if (rc) { |
| 64 |
if (rc_) rc_->Release(); |
| 65 |
rc_ = rc; |
| 66 |
rc_->reset(); |
| 67 |
|
| 68 |
c86ctl::IGimic2* gm = nullptr; |
| 69 |
if (rc_->QueryInterface(c86ctl::IID_IGimic2, reinterpret_cast<void**>(&gm)) == S_OK) { |
| 70 |
c86ctl::ChipType type; |
| 71 |
gm->getModuleType(&type); |
| 72 |
switch (type) { |
| 73 |
case c86ctl::CHIP_YM2608: |
| 74 |
case c86ctl::CHIP_YM2608NOADPCM: |
| 75 |
if (gm_) gm_->Release(); |
| 76 |
gm_ = gm; |
| 77 |
return true; |
| 78 |
|
| 79 |
default: |
| 80 |
break; |
| 81 |
} |
| 82 |
gm->Release(); |
| 83 |
} |
| 84 |
rc_->Release(); |
| 85 |
rc_ = nullptr; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
else { // Clear interfaces |
| 90 |
if (!base_) return false; |
| 91 |
rc_->reset(); |
| 92 |
gm_->Release(); |
| 93 |
gm_ = nullptr; |
| 94 |
rc_->Release(); |
| 95 |
rc_ = nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
base_->deinitialize(); |
| 99 |
base_->Release(); |
| 100 |
base_ = nullptr; |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
bool C86ctl::hasConnected() const |
| 105 |
{ |
| 106 |
return (base_ != nullptr); |
| 107 |
} |
| 108 |
|
| 109 |
void C86ctl::reset() |
| 110 |
{ |
| 111 |
if (rc_) rc_->reset(); |
| 112 |
} |
| 113 |
|
| 114 |
void C86ctl::setRegister(uint32_t addr, uint8_t data) |
| 115 |
{ |
| 116 |
if (rc_) rc_->out(addr, data); |
| 117 |
} |
| 118 |
|
| 119 |
void C86ctl::setSSGVolume(double dB) |
| 120 |
{ |
| 121 |
if (gm_) { |
| 122 |
// NOTE: estimate SSG volume roughly |
| 123 |
uint8_t vol = static_cast<uint8_t>(std::round((dB < -3.0) ? (2.5 * dB + 45.5) |
| 124 |
: (7. * dB + 59.))); |
| 125 |
gm_->setSSGVolume(vol); |
| 126 |
} |
| 127 |
} |