Debian Codegraph @

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