Debian Codegraph @

thirdparty/ymfm/src/ymfm_ssg.h

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 #ifndef YMFM_SSG_H
32 #define YMFM_SSG_H
33
34 #pragma once
35
36 #include "ymfm.h"
37
38 namespace ymfm
39 {
40
41 //*********************************************************
42 // OVERRIDE INTERFACE
43 //*********************************************************
44
45 // ======================> ssg_override
46
47 // this class describes a simple interface to allow the internal SSG to be
48 // overridden with another implementation
49 class ssg_override
50 {
51 public:
52 virtual ~ssg_override() = default;
53
54 // reset our status
55 virtual void ssg_reset() = 0;
56
57 // read/write to the SSG registers
58 virtual uint8_t ssg_read(uint32_t regnum) = 0;
59 virtual void ssg_write(uint32_t regnum, uint8_t data) = 0;
60
61 // notification when the prescale has changed
62 virtual void ssg_prescale_changed() = 0;
63 };
64
65
66 //*********************************************************
67 // REGISTER CLASS
68 //*********************************************************
69
70 // ======================> ssg_registers
71
72 //
73 // SSG register map:
74 //
75 // System-wide registers:
76 // 06 ---xxxxx Noise period
77 // 07 x------- I/O B in(0) or out(1)
78 // -x------ I/O A in(0) or out(1)
79 // --x----- Noise enable(0) or disable(1) for channel C
80 // ---x---- Noise enable(0) or disable(1) for channel B
81 // ----x--- Noise enable(0) or disable(1) for channel A
82 // -----x-- Tone enable(0) or disable(1) for channel C
83 // ------x- Tone enable(0) or disable(1) for channel B
84 // -------x Tone enable(0) or disable(1) for channel A
85 // 0B xxxxxxxx Envelope period fine
86 // 0C xxxxxxxx Envelope period coarse
87 // 0D ----x--- Envelope shape: continue
88 // -----x-- Envelope shape: attack/decay
89 // ------x- Envelope shape: alternate
90 // -------x Envelope shape: hold
91 // 0E xxxxxxxx 8-bit parallel I/O port A
92 // 0F xxxxxxxx 8-bit parallel I/O port B
93 //
94 // Per-channel registers:
95 // 00,02,04 xxxxxxxx Tone period (fine) for channel A,B,C
96 // 01,03,05 ----xxxx Tone period (coarse) for channel A,B,C
97 // 08,09,0A ---x---- Mode: fixed(0) or variable(1) for channel A,B,C
98 // ----xxxx Amplitude for channel A,B,C
99 //
100 class ssg_registers
101 {
102 public:
103 // constants
104 static constexpr uint32_t OUTPUTS = 3;
105 static constexpr uint32_t CHANNELS = 3;
106 static constexpr uint32_t REGISTERS = 0x10;
107 static constexpr uint32_t ALL_CHANNELS = (1 << CHANNELS) - 1;
108
109 // constructor
110 ssg_registers() { }
111
112 // reset to initial state
113 void reset();
114
115 // save/restore
116 void save_restore(ymfm_saved_state &state);
117
118 // direct read/write access
119 uint8_t read(uint32_t index) { return m_regdata[index]; }
120 void write(uint32_t index, uint8_t data) { m_regdata[index] = data; }
121
122 // system-wide registers
123 uint32_t noise_period() const { return bitfield(m_regdata[0x06], 0, 5); }
124 uint32_t io_b_out() const { return bitfield(m_regdata[0x07], 7); }
125 uint32_t io_a_out() const { return bitfield(m_regdata[0x07], 6); }
126 uint32_t envelope_period() const { return m_regdata[0x0b] | (m_regdata[0x0c] << 8); }
127 uint32_t envelope_continue() const { return bitfield(m_regdata[0x0d], 3); }
128 uint32_t envelope_attack() const { return bitfield(m_regdata[0x0d], 2); }
129 uint32_t envelope_alternate() const { return bitfield(m_regdata[0x0d], 1); }
130 uint32_t envelope_hold() const { return bitfield(m_regdata[0x0d], 0); }
131 uint32_t io_a_data() const { return m_regdata[0x0e]; }
132 uint32_t io_b_data() const { return m_regdata[0x0f]; }
133
134 // per-channel registers
135 uint32_t ch_noise_enable_n(uint32_t choffs) const { return bitfield(m_regdata[0x07], 3 + choffs); }
136 uint32_t ch_tone_enable_n(uint32_t choffs) const { return bitfield(m_regdata[0x07], 0 + choffs); }
137 uint32_t ch_tone_period(uint32_t choffs) const { return m_regdata[0x00 + 2 * choffs] | (bitfield(m_regdata[0x01 + 2 * choffs], 0, 4) << 8); }
138 uint32_t ch_envelope_enable(uint32_t choffs) const { return bitfield(m_regdata[0x08 + choffs], 4); }
139 uint32_t ch_amplitude(uint32_t choffs) const { return bitfield(m_regdata[0x08 + choffs], 0, 4); }
140
141 private:
142 // internal state
143 uint8_t m_regdata[REGISTERS]; // register data
144 };
145
146
147 // ======================> ssg_engine
148
149 class ssg_engine
150 {
151 public:
152 static constexpr int OUTPUTS = ssg_registers::OUTPUTS;
153 static constexpr int CHANNELS = ssg_registers::CHANNELS;
154 static constexpr int CLOCK_DIVIDER = 8;
155
156 using output_data = ymfm_output<OUTPUTS>;
157
158 // constructor
159 ssg_engine(ymfm_interface &intf);
160
161 // configure an override
162 void override(ssg_override &override) { m_override = &override; }
163
164 // reset our status
165 void reset();
166
167 // save/restore
168 void save_restore(ymfm_saved_state &state);
169
170 // master clocking function
171 void clock();
172
173 // compute sum of channel outputs
174 void output(output_data &output);
175
176 // read/write to the SSG registers
177 uint8_t read(uint32_t regnum);
178 void write(uint32_t regnum, uint8_t data);
179
180 // return a reference to our interface
181 ymfm_interface &intf() { return m_intf; }
182
183 // return a reference to our registers
184 ssg_registers &regs() { return m_regs; }
185
186 // true if we are overridden
187 bool overridden() const { return (m_override != nullptr); }
188
189 // indicate the prescale has changed
190 void prescale_changed() { if (m_override != nullptr) m_override->ssg_prescale_changed(); }
191
192 private:
193 // internal state
194 ymfm_interface &m_intf; // reference to the interface
195 uint32_t m_tone_count[3]; // current tone counter
196 uint32_t m_tone_state[3]; // current tone state
197 uint32_t m_envelope_count; // envelope counter
198 uint32_t m_envelope_state; // envelope state
199 uint32_t m_noise_count; // current noise counter
200 uint32_t m_noise_state; // current noise state
201 ssg_registers m_regs; // registers
202 ssg_override *m_override; // override interface
203 };
204
205 }
206
207 #endif // YMFM_SSG_H