summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/ymfm_mame.h
blob: 27a7d002b185a8a2e07aced2452cf6a57c5e03f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// license:BSD-3-Clause
// copyright-holders:Aaron Giles

#ifndef MAME_SOUND_YMFM_MAME_H
#define MAME_SOUND_YMFM_MAME_H

#pragma once

#include "ymfm/src/ymfm.h"
#include "ymfm/src/ymfm_ssg.h"


// set this to control the output sample rate for SSG-based chips
#define SSG_FIDELITY (ymfm::OPN_FIDELITY_MED)



//*********************************************************
//  MAME INTERFACES
//*********************************************************

// ======================> ym_generic_device

// generic base class for a standalone FM device; this class contains the shared
// configuration helpers, timers, and ymfm interface implementation; it also
// specifies pure virtual functions for read/write access, which means it
// can be used as a generic proxy for systems that have multiple FM types that are
// swappable
class ym_generic_device : public device_t, public device_sound_interface, public ymfm::ymfm_interface
{
public:
	// constructor
	ym_generic_device(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock, device_type type) :
		device_t(mconfig, type, tag, owner, clock),
		device_sound_interface(mconfig, *this),
		m_timer{ nullptr, nullptr },
		m_update_irq(*this),
		m_io_read{ *this, *this },
		m_io_write{ *this, *this }
	{
	}

	// configuration helpers
	auto irq_handler() { return m_update_irq.bind(); }
	auto io_read_handler(int index = 0) { return m_io_read[index & 1].bind(); }
	auto io_write_handler(int index = 0) { return m_io_write[index & 1].bind(); }

	// read access interface, implemented by the derived chip-specific class
	virtual u8 read(offs_t offset) = 0;
	virtual u8 status_r() = 0;

	// write access interface, implemented by the derived chip-specific class
	virtual void write(offs_t offset, u8 data) = 0;
	virtual void address_w(u8 data) = 0;
	virtual void data_w(u8 data) = 0;

protected:
	// the chip implementation calls this when a write happens to the mode
	// register, which could affect timers and interrupts; our responsibility
	// is to ensure the system is up to date before calling the engine's
	// engine_mode_write() method
	virtual void ymfm_sync_mode_write(uint8_t data) override
	{
		machine().scheduler().synchronize(timer_expired_delegate(FUNC(ym_generic_device::fm_mode_write), this), data);
	}

	// the chip implementation calls this when the chip's status has changed,
	// which may affect the interrupt state; our responsibility is to ensure
	// the system is up to date before calling the engine's
	// engine_check_interrupts() method
	virtual void ymfm_sync_check_interrupts() override
	{
		// if we're currently executing a CPU, schedule the interrupt check;
		// otherwise, do it directly
		auto &scheduler = machine().scheduler();
		if (scheduler.currently_executing())
			scheduler.synchronize(timer_expired_delegate(FUNC(ym_generic_device::fm_check_interrupts), this));
		else
			m_engine->engine_check_interrupts();
	}

	// the chip implementation calls this when one of the two internal timers
	// has changed state; our responsibility is to arrange to call the engine's
	// engine_timer_expired() method after the provided number of clocks; if
	// duration_in_clocks is negative, we should cancel any outstanding timers
	virtual void ymfm_set_timer(uint32_t tnum, int32_t duration_in_clocks) override
	{
		if (duration_in_clocks >= 0)
			m_timer[tnum]->adjust(attotime::from_ticks(duration_in_clocks, device_t::clock()), tnum);
		else
			m_timer[tnum]->enable(false);
	}

	// the chip implementation calls this when the state of the IRQ signal has
	// changed due to a status change; our responsibility is to respons as
	// needed to the change in IRQ state, signaling any consumers
	virtual void ymfm_update_irq(bool asserted) override
	{
		if (!m_update_irq.isnull())
			m_update_irq(asserted ? ASSERT_LINE : CLEAR_LINE);
	}

	// the chip implementation calls this to indicate that the chip should be
	// considered in a busy state until the given number of clocks has passed;
	// our responsibility is to compute and remember the ending time based on
	// the chip's clock for later checking
	virtual void ymfm_set_busy_end(uint32_t clocks) override
	{
		m_busy_end = machine().time() + attotime::from_ticks(clocks, device_t::clock());
	}

	// the chip implementation calls this to see if the chip is still currently
	// is a busy state, as specified by a previous call to ymfm_set_busy_end();
	// our responsibility is to compare the current time against the previously
	// noted busy end time and return true if we haven't yet passed it
	virtual bool ymfm_is_busy() override
	{
		return (machine().time() < m_busy_end);
	}

	// the chip implementation calls this whenever data is read from outside
	// of the chip; our responsibility is to provide the data requested
	virtual uint8_t ymfm_external_read(ymfm::access_class type, uint32_t address) override
	{
		return (type != ymfm::ACCESS_IO || m_io_read[address & 1].isnull()) ? 0 : m_io_read[address & 1]();
	}

	// the chip implementation calls this whenever data is written outside
	// of the chip; our responsibility is to pass the written data on to any consumers
	virtual void ymfm_external_write(ymfm::access_class type, uint32_t address, uint8_t data) override
	{
		if (type == ymfm::ACCESS_IO && !m_io_write[address & 1].isnull())
			m_io_write[address & 1](data);
	}

	// handle device start
	virtual void device_start() override
	{
		// allocate our timers
		for (int tnum = 0; tnum < 2; tnum++)
			m_timer[tnum] = timer_alloc(FUNC(ym_generic_device::fm_timer_handler), this);

		// resolve the handlers
		m_update_irq.resolve();
		m_io_read[0].resolve();
		m_io_read[1].resolve();
		m_io_write[0].resolve();
		m_io_write[1].resolve();

		// remember the busy end time
		save_item(NAME(m_busy_end));
	}

	// timer callbacks
	void fm_mode_write(int param) { m_engine->engine_mode_write(param); }
	void fm_check_interrupts(int param) { m_engine->engine_check_interrupts(); }
	void fm_timer_handler(int param) { m_engine->engine_timer_expired(param); }

	// internal state
	attotime m_busy_end;             // busy end time
	emu_timer *m_timer[2];           // two timers
	devcb_write_line m_update_irq;   // IRQ update callback
	devcb_read8 m_io_read[2];        // up to 2 input port handlers
	devcb_write8 m_io_write[2];      // up to 2 output port handlers
};


// ======================> ymfm_device_base

// this template provides most of the basics used by device objects in MAME
// that wrap ymfm chips; it provides basic read/write functions
template<typename ChipClass>
class ymfm_device_base : public ym_generic_device
{
protected:
	static constexpr int OUTPUTS = ChipClass::OUTPUTS;

public:
	// constructor
	ymfm_device_base(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock, device_type type) :
		ym_generic_device(mconfig, tag, owner, clock, type),
		m_stream(nullptr),
		m_chip(*this)
	{
	}

	// read access: update the streams before performing the read
	virtual u8 read(offs_t offset) override { return update_streams().read(offset); }
	virtual u8 status_r() override { return update_streams().read_status(); }

	// write access: update the strams before performing the write
	virtual void write(offs_t offset, u8 data) override { update_streams().write(offset, data); }
	virtual void address_w(u8 data) override { update_streams().write_address(data); }
	virtual void data_w(u8 data) override { update_streams().write_data(data); }

protected:
	// handle device start
	virtual void device_start() override
	{
		// let our parent do its startup
		ym_generic_device::device_start();

		// allocate our stream
		m_stream = device_sound_interface::stream_alloc(0, OUTPUTS, XTAL::u(m_chip.sample_rate(device_t::clock().value())));

		// compute the size of the save buffer by doing an initial save
		ymfm::ymfm_saved_state state(m_save_blob, true);
		m_chip.save_restore(state);

		// now register the blob for save, on the assumption the size won't change
		save_item(NAME(m_save_blob));
	}

	// device reset
	virtual void device_reset() override
	{
		m_chip.reset();
	}

	// handle clock changed
	virtual void device_clock_changed() override
	{
		if (m_stream != nullptr)
			m_stream->set_sample_rate(XTAL::u(m_chip.sample_rate(device_t::clock().value())));
	}

	// handle pre-saving by filling the blob
	virtual void device_pre_save() override
	{
		// remember the original blob size
		auto orig_size = m_save_blob.size();

		// save the state
		ymfm::ymfm_saved_state state(m_save_blob, true);
		m_chip.save_restore(state);

		// ensure that the size didn't change since we first allocated
		if (m_save_blob.size() != orig_size)
			throw emu_fatalerror("State size changed for ymfm chip");
	}

	// handle post-loading by restoring from the blob
	virtual void device_post_load() override
	{
		// populate the state from the blob
		ymfm::ymfm_saved_state state(m_save_blob, false);
		m_chip.save_restore(state);
	}

	// sound overrides
	virtual void sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs) override
	{
		update_internal(outputs);
	}

	// update streams
	virtual ChipClass &update_streams()
	{
		m_stream->update();
		return m_chip;
	}

	// internal update helper
	void update_internal(std::vector<write_stream_view> &outputs, int output_shift = 0)
	{
		// local buffer to hold samples
		constexpr int MAX_SAMPLES = 256;
		typename ChipClass::output_data output[MAX_SAMPLES];

		// parameters
		int const outcount = std::min(outputs.size(), std::size(output[0].data));
		int const numsamples = outputs[0].samples();

		// generate the FM/ADPCM stream
		for (int sampindex = 0; sampindex < numsamples; sampindex += MAX_SAMPLES)
		{
			int cursamples = std::min(numsamples - sampindex, MAX_SAMPLES);
			m_chip.generate(output, cursamples);
			for (int outnum = 0; outnum < outcount; outnum++)
			{
				int eff_outnum = (outnum + output_shift) % OUTPUTS;
				for (int index = 0; index < cursamples; index++)
					outputs[eff_outnum].put_int(sampindex + index, output[index].data[outnum], 32768);
			}
		}
	}

	// internal state
	sound_stream *m_stream;           // sound stream
	ChipClass m_chip;                 // core chip implementation
	std::vector<uint8_t> m_save_blob; // state saving buffer
};


// ======================> ymfm_ssg_device_base

// this template adds SSG support to the base template, using ymfm's internal
// SSG implementation
template<typename ChipClass>
class ymfm_ssg_device_base : public ymfm_device_base<ChipClass>
{
	using parent = ymfm_device_base<ChipClass>;

public:
	// constructor
	ymfm_ssg_device_base(const machine_config &mconfig, const char *tag, device_t *owner, const XTAL &clock, device_type type) :
		ymfm_device_base<ChipClass>(mconfig, tag, owner, clock, type)
	{
	}

protected:
	// sound overrides
	virtual void sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs) override
	{
		// ymfm outputs FM first, then SSG, while MAME traditionally
		// wants SSG streams first; to do this, we rotate the outputs
		// by the number of SSG output channels
		parent::update_internal(outputs, ChipClass::SSG_OUTPUTS);

		// for the single-output case, also apply boost the gain to better match
		// previous version, which summed instead of averaged the outputs
		if (ChipClass::SSG_OUTPUTS == 1)
			outputs[0].apply_gain(3.0);
	}
};

#endif // MAME_SOUND_YMFM_H