Debian Codegraph @

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