Debian Codegraph @

BambooTracker/chip/mame/mame_2608.cpp

raw
1 /*
2 * Copyright (C) 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 "mame_2608.hpp"
27 #include <algorithm>
28
29 extern "C"
30 {
31 #include "fmopn.h"
32 }
33
34 namespace chip
35 {
36 namespace // SSG callbacks
37 {
38 void setSsgClock(void* param, uint32_t clock)
39 {
40 auto state = reinterpret_cast<Mame2608State*>(param);
41 if (state->ssg) PSG_set_clock(state->ssg, clock);
42 }
43
44 void writeSsg(void* param, uint8_t address, uint8_t data)
45 {
46 auto state = reinterpret_cast<Mame2608State*>(param);
47 if (state->ssg) PSG_writeIO(state->ssg, address, data);
48 }
49
50 uint8_t readSsg(void* param)
51 {
52 auto state = reinterpret_cast<Mame2608State*>(param);
53 return (state->ssg ? PSG_readIO(state->ssg) : 0);
54 }
55
56 void resetSsg(void* param)
57 {
58 auto state = reinterpret_cast<Mame2608State*>(param);
59 if (state->ssg) PSG_reset(state->ssg);
60 }
61
62 const ssg_callbacks SSG_INTF =
63 {
64 setSsgClock,
65 writeSsg,
66 readSsg,
67 resetSsg
68 };
69 }
70
71 Mame2608::~Mame2608()
72 {
73 stopDevice();
74 }
75
76 int Mame2608::startDevice(int clock, int& rateSsg, uint32_t dramSize)
77 {
78 int clockSsg = clock / 4;
79 rateSsg = clockSsg / 8;
80 state_.ssg = PSG_new(clockSsg, rateSsg);
81 if (!state_.ssg) return 0;
82 PSG_setVolumeMode(state_.ssg, 1); // YM2149 volume mode
83
84 int rate = clock / 144; // FM synthesis rate is clock / 2 / 72
85 state_.chip = ym2608_init(&state_, clock, rate, nullptr, nullptr);
86 if (!state_.chip) return 0;
87 ym2608_link_ssg(state_.chip, &SSG_INTF, &state_);
88 ym2608_alloc_pcmromb(state_.chip, dramSize);
89
90 return rate;
91 }
92
93 void Mame2608::stopDevice()
94 {
95 if (state_.chip) {
96 ym2608_shutdown(state_.chip);
97 state_.chip = nullptr;
98 }
99 if (state_.ssg)
100 {
101 PSG_delete(state_.ssg);
102 state_.ssg = nullptr;
103 }
104 }
105
106 void Mame2608::resetDevice()
107 {
108 ym2608_reset_chip(state_.chip); // Also reset SSG
109 }
110
111 void Mame2608::writeAddressToPortA(uint8_t address)
112 {
113 ym2608_write(state_.chip, 0, address);
114 }
115
116 void Mame2608::writeAddressToPortB(uint8_t address)
117 {
118 ym2608_write(state_.chip, 2, address);
119 }
120
121 void Mame2608::writeDataToPortA(uint8_t data)
122 {
123 ym2608_write(state_.chip, 1, data);
124 }
125
126 void Mame2608::writeDataToPortB(uint8_t data)
127 {
128 ym2608_write(state_.chip, 3, data);
129 }
130
131 uint8_t Mame2608::readData()
132 {
133 return ym2608_read(state_.chip, 1);
134 }
135
136 void Mame2608::updateStream(sample** outputs, int nSamples)
137 {
138 ym2608_update_one(state_.chip, nSamples, outputs);
139 }
140
141 void Mame2608::updateSsgStream(sample** outputs, int nSamples)
142 {
143 if (state_.ssg) {
144 sample* bufl = outputs[STEREO_LEFT];
145 sample* bufr = outputs[STEREO_RIGHT];
146 for (int i = 0; i < nSamples; ++i) {
147 int16_t s = PSG_calc(state_.ssg) << 1;
148 *bufl++ = s;
149 *bufr++ = s;
150 }
151 }
152 else {
153 std::fill_n(outputs[STEREO_LEFT], nSamples, 0);
154 std::fill_n(outputs[STEREO_RIGHT], nSamples, 0);
155 }
156 }
157 }
158
159 namespace
160 {
161 sample* DUMMY_BUF[] = { nullptr, nullptr };
162 }
163
164 void ym2608_update_request(void* param)
165 {
166 auto state = reinterpret_cast<chip::Mame2608State*>(param);
167 ym2608_update_one(state->chip, 0, DUMMY_BUF);
168 }