Debian Codegraph @

BambooTracker/chip/ymfm/ymfm_ssg.cpp

raw
1 // BSD 3-Clause License
2 //
3 // Copyright (c) 2021, Aaron Giles
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice,
13 // this list of conditions and the following disclaimer in the documentation
14 // and/or other materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include "ymfm_ssg.h"
32
33 namespace ymfm
34 {
35
36 //*********************************************************
37 // SSG REGISTERS
38 //*********************************************************
39
40 //-------------------------------------------------
41 // reset - reset the register state
42 //-------------------------------------------------
43
44 void ssg_registers::reset()
45 {
46 std::fill_n(&m_regdata[0], REGISTERS, 0);
47 }
48
49
50 //-------------------------------------------------
51 // save_restore - save or restore the data
52 //-------------------------------------------------
53
54 void ssg_registers::save_restore(ymfm_saved_state &state)
55 {
56 state.save_restore(m_regdata);
57 }
58
59
60
61 //*********************************************************
62 // SSG ENGINE
63 //*********************************************************
64
65 //-------------------------------------------------
66 // ssg_engine - constructor
67 //-------------------------------------------------
68
69 ssg_engine::ssg_engine(ymfm_interface &intf) :
70 m_intf(intf),
71 m_tone_count{ 0,0,0 },
72 m_tone_state{ 0,0,0 },
73 m_envelope_count(0),
74 m_envelope_state(0),
75 m_noise_count(0),
76 m_noise_state(1),
77 m_override(nullptr)
78 {
79 }
80
81
82 //-------------------------------------------------
83 // reset - reset the engine state
84 //-------------------------------------------------
85
86 void ssg_engine::reset()
87 {
88 // defer to the override if present
89 if (m_override != nullptr)
90 return m_override->ssg_reset();
91
92 // reset register state
93 m_regs.reset();
94
95 // reset engine state
96 for (int chan = 0; chan < 3; chan++)
97 {
98 m_tone_count[chan] = 0;
99 m_tone_state[chan] = 0;
100 }
101 m_envelope_count = 0;
102 m_envelope_state = 0;
103 m_noise_count = 0;
104 m_noise_state = 1;
105 }
106
107
108 //-------------------------------------------------
109 // save_restore - save or restore the data
110 //-------------------------------------------------
111
112 void ssg_engine::save_restore(ymfm_saved_state &state)
113 {
114 // save register state
115 m_regs.save_restore(state);
116
117 // save engine state
118 state.save_restore(m_tone_count);
119 state.save_restore(m_tone_state);
120 state.save_restore(m_envelope_count);
121 state.save_restore(m_envelope_state);
122 state.save_restore(m_noise_count);
123 state.save_restore(m_noise_state);
124 }
125
126
127 //-------------------------------------------------
128 // clock - master clocking function
129 //-------------------------------------------------
130
131 void ssg_engine::clock()
132 {
133 // clock tones; tone period units are clock/16 but since we run at clock/8
134 // that works out for us to toggle the state (50% duty cycle) at twice the
135 // programmed period
136 for (int chan = 0; chan < 3; chan++)
137 {
138 m_tone_count[chan]++;
139 if (m_tone_count[chan] >= m_regs.ch_tone_period(chan))
140 {
141 m_tone_state[chan] ^= 1;
142 m_tone_count[chan] = 0;
143 }
144 }
145
146 // clock noise; noise period units are clock/16 but since we run at clock/8,
147 // our counter needs a right shift prior to compare; note that a period of 0
148 // should produce an indentical result to a period of 1, so add a special
149 // check against that case
150 m_noise_count++;
151 if ((m_noise_count >> 1) >= m_regs.noise_period() && m_noise_count != 1)
152 {
153 m_noise_state ^= (bitfield(m_noise_state, 0) ^ bitfield(m_noise_state, 3)) << 17;
154 m_noise_state >>= 1;
155 m_noise_count = 0;
156 }
157
158 // clock envelope; envelope period units are clock/8 (manual says clock/256
159 // but that's for all 32 steps)
160 m_envelope_count++;
161 if (m_envelope_count >= m_regs.envelope_period())
162 {
163 m_envelope_state++;
164 m_envelope_count = 0;
165 }
166 }
167
168
169 //-------------------------------------------------
170 // output - output the current state
171 //-------------------------------------------------
172
173 void ssg_engine::output(output_data &output)
174 {
175 // volume to amplitude table, taken from MAME's implementation but biased
176 // so that 0 == 0
177 static int16_t const s_amplitudes[32] =
178 {
179 0, 32, 78, 141, 178, 222, 262, 306,
180 369, 441, 509, 585, 701, 836, 965, 1112,
181 1334, 1595, 1853, 2146, 2576, 3081, 3576, 4135,
182 5000, 6006, 7023, 8155, 9963,11976,14132,16382
183 };
184
185 // compute the envelope volume
186 uint32_t envelope_volume;
187 if ((m_regs.envelope_hold() | (m_regs.envelope_continue() ^ 1)) && m_envelope_state >= 32)
188 {
189 m_envelope_state = 32;
190 envelope_volume = ((m_regs.envelope_attack() ^ m_regs.envelope_alternate()) & m_regs.envelope_continue()) ? 31 : 0;
191 }
192 else
193 {
194 uint32_t attack = m_regs.envelope_attack();
195 if (m_regs.envelope_alternate())
196 attack ^= bitfield(m_envelope_state, 5);
197 envelope_volume = (m_envelope_state & 31) ^ (attack ? 0 : 31);
198 }
199
200 // iterate over channels
201 for (int chan = 0; chan < 3; chan++)
202 {
203 // noise depends on the noise state, which is the LSB of m_noise_state
204 uint32_t noise_on = m_regs.ch_noise_enable_n(chan) | m_noise_state;
205
206 // tone depends on the current tone state
207 uint32_t tone_on = m_regs.ch_tone_enable_n(chan) | m_tone_state[chan];
208
209 // if neither tone nor noise enabled, return 0
210 uint32_t volume;
211 if ((noise_on & tone_on) == 0)
212 volume = 0;
213
214 // if the envelope is enabled, use its amplitude
215 else if (m_regs.ch_envelope_enable(chan))
216 volume = envelope_volume;
217
218 // otherwise, scale the tone amplitude up to match envelope values
219 // according to the datasheet, amplitude 15 maps to envelope 31
220 else
221 {
222 volume = m_regs.ch_amplitude(chan) * 2;
223 if (volume != 0)
224 volume |= 1;
225 }
226
227 // convert to amplitude
228 output.data[chan] = s_amplitudes[volume];
229 }
230 }
231
232
233 //-------------------------------------------------
234 // read - handle reads from the SSG registers
235 //-------------------------------------------------
236
237 uint8_t ssg_engine::read(uint32_t regnum)
238 {
239 // defer to the override if present
240 if (m_override != nullptr)
241 return m_override->ssg_read(regnum);
242
243 // read from the I/O ports call the handlers if they are configured for input
244 if (regnum == 0x0e && !m_regs.io_a_out())
245 return m_intf.ymfm_external_read(ACCESS_IO, 0);
246 else if (regnum == 0x0f && !m_regs.io_b_out())
247 return m_intf.ymfm_external_read(ACCESS_IO, 1);
248
249 // otherwise just return the register value
250 return m_regs.read(regnum);
251 }
252
253
254 //-------------------------------------------------
255 // write - handle writes to the SSG registers
256 //-------------------------------------------------
257
258 void ssg_engine::write(uint32_t regnum, uint8_t data)
259 {
260 // defer to the override if present
261 if (m_override != nullptr)
262 return m_override->ssg_write(regnum, data);
263
264 // store the raw value to the register array;
265 // most writes are passive, consumed only when needed
266 m_regs.write(regnum, data);
267
268 // writes to the envelope shape register reset the state
269 if (regnum == 0x0d)
270 m_envelope_state = 0;
271
272 // writes to the I/O ports call the handlers if they are configured for output
273 else if (regnum == 0x0e && m_regs.io_a_out())
274 m_intf.ymfm_external_write(ACCESS_IO, 0, data);
275 else if (regnum == 0x0f && m_regs.io_b_out())
276 m_intf.ymfm_external_write(ACCESS_IO, 1, data);
277 }
278
279 }