Debian Codegraph @

BambooTracker/chip/ymfm/ymfm_2608.cpp

raw
1 /*
2 * Copyright (C) 2022 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 "ymfm_2608.hpp"
27
28 extern const unsigned char YM2608_ADPCM_ROM[0x2000];
29
30 namespace chip
31 {
32 Ymfm2608::YmfmInterface::YmfmInterface(uint32_t dramSize) : dram_(dramSize) {}
33
34 uint8_t Ymfm2608::YmfmInterface::ymfm_external_read(ymfm::access_class type, uint32_t address)
35 {
36 switch (type) {
37 case ymfm::access_class::ACCESS_ADPCM_A:
38 return address < 0x2000 ? YM2608_ADPCM_ROM[address] : 0;
39 case ymfm::access_class::ACCESS_ADPCM_B:
40 return address < dram_.size() ? dram_[address] : 0;
41 default:
42 return 0;
43 }
44 }
45
46 void Ymfm2608::YmfmInterface::ymfm_external_write(ymfm::access_class type, uint32_t address, uint8_t data)
47 {
48 if (type == ymfm::access_class::ACCESS_ADPCM_B && address < dram_.size()) {
49 dram_[address] = data;
50 }
51 }
52
53 //**************************************************
54 Ymfm2608::~Ymfm2608()
55 {
56 stopDevice();
57 }
58
59 int Ymfm2608::startDevice(int clock, int& rateSsg, uint32_t dramSize)
60 {
61 ymfmIntf_ = std::make_unique<YmfmInterface>(dramSize);
62 ymfm_ = std::make_unique<ymfm::ym2608>(*ymfmIntf_);
63 // Prescale = 6
64 // Set FM output rate
65 ymfm_->set_fidelity(ymfm::opn_fidelity::OPN_FIDELITY_MIN);
66 rateSsg = clock / 32;
67
68 ymfm_->reset();
69
70 return clock / 144; // FM synthesis rate is clock / 2 / 72
71 }
72
73 void Ymfm2608::stopDevice()
74 {
75 ymfm_.reset();
76 }
77
78 void Ymfm2608::resetDevice()
79 {
80 ymfm_->reset();
81 }
82
83 void Ymfm2608::writeAddressToPortA(uint8_t address)
84 {
85 ymfm_->write_address(address);
86 }
87
88 void Ymfm2608::writeAddressToPortB(uint8_t address)
89 {
90 ymfm_->write_address_hi(address);
91 }
92
93 void Ymfm2608::writeDataToPortA(uint8_t data)
94 {
95 ymfm_->write_data(data);
96 }
97
98 void Ymfm2608::writeDataToPortB(uint8_t data)
99 {
100 ymfm_->write_data_hi(data);
101 }
102
103 uint8_t Ymfm2608::readData()
104 {
105 return ymfm_->read_data();
106 }
107
108 void Ymfm2608::updateStream(sample** outputs, int nSamples)
109 {
110 sample* bufl = outputs[STEREO_LEFT];
111 sample* bufr = outputs[STEREO_RIGHT];
112
113 ymfm::ym2608::output_data data;
114 for (int i = 0; i < nSamples; ++i) {
115 ymfm_->generate_fm_adpcm(&data);
116 // Raise volume
117 *bufl++ = data.data[0] << 1;
118 *bufr++ = data.data[1] << 1;
119 }
120 }
121
122 void Ymfm2608::updateSsgStream(sample** outputs, int nSamples)
123 {
124 sample* bufl = outputs[STEREO_LEFT];
125 sample* bufr = outputs[STEREO_RIGHT];
126
127 ymfm::ym2608::output_data data;
128 for (int i = 0; i < nSamples; ++i) {
129 ymfm_->generate_ssg(&data);
130 // Modify volume
131 int32_t s = data.data[2] * 3 / 4;
132 *bufl++ = s;
133 *bufr++ = s;
134 }
135 }
136 }