summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/dmadac.h
blob: d221a7575e4a8880fe43c87a3cc8a5a6b8de73c3 (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/**********************************************************************************************
 *
 *   DMA-driven DAC driver
 *   by Aaron Giles
 *
 **********************************************************************************************/

#ifndef MAME_SOUND_DMADAC_H
#define MAME_SOUND_DMADAC_H

#pragma once


class dmadac_sound_device : public device_t, public device_sound_interface
{
public:
	dmadac_sound_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock = 0);

	void initialize_state();
	void flush();

	template <typename T> void transfer(int channel, offs_t channel_spacing, offs_t frame_spacing, offs_t total_frames, T* data) {
		int j;

		constexpr stream_buffer::sample_t sample_scale = 1.0 / double(std::numeric_limits<T>::max());

		if (m_enabled)
		{
			int maxin = (m_bufout + BUFFER_SIZE - 1) % BUFFER_SIZE;
			T* src = data + channel * channel_spacing;
			int curin = m_bufin;

			/* copy the data */
			for (j = 0; j < total_frames && curin != maxin; j++)
			{
				m_buffer[curin] = stream_buffer::sample_t(*src) * sample_scale;
				curin = (curin + 1) % BUFFER_SIZE;
				src += frame_spacing;
			}
			m_bufin = curin;

			/* log overruns */
			if (j != total_frames)
				logerror("dmadac_transfer: buffer overrun (short %d frames)\n", total_frames - j);
		}
	}

	void enable(uint8_t enable);
	void set_frequency(double frequency);
	void set_volume(uint16_t volume);

protected:
	// device-level overrides
	virtual void device_start() override ATTR_COLD;

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

private:
	// internal state
	/* sound stream and buffers */
	sound_stream *  m_channel;
	std::vector<stream_buffer::sample_t> m_buffer;
	uint32_t          m_bufin;
	uint32_t          m_bufout;

	/* per-channel parameters */
	stream_buffer::sample_t m_volume;
	uint8_t           m_enabled;

	static constexpr int BUFFER_SIZE = 32768;
};

DECLARE_DEVICE_TYPE(DMADAC, dmadac_sound_device)


void dmadac_transfer(dmadac_sound_device **devlist, uint8_t num_channels, offs_t channel_spacing, offs_t frame_spacing, offs_t total_frames, int16_t *data);
void dmadac_enable(dmadac_sound_device **devlist, uint8_t num_channels, uint8_t enable);
void dmadac_set_frequency(dmadac_sound_device **devlist, uint8_t num_channels, double frequency);
void dmadac_set_volume(dmadac_sound_device **devlist, uint8_t num_channels, uint16_t volume);

#endif // MAME_SOUND_DMADAC_H