summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/sound/s_dsp.h
blob: 4ab841d0ebdd7522cbb5f2adbe0b3efd0d749ccf (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
// license:LGPL-2.1+
// copyright-holders:R. Belmont, Brad Martin
/*****************************************************************************
 *
 * Nintendo/Sony S-DSP emulation
 *
 ****************************************************************************/

#ifndef MAME_SOUND_S_DSP_H
#define MAME_SOUND_S_DSP_H

/***************************************************************************
 TYPE DEFINITIONS
 ***************************************************************************/

class s_dsp_device : public device_t, public device_sound_interface, public device_memory_interface
{
public:
	s_dsp_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock);

	void set_volume(int volume);

	u8 dsp_io_r(offs_t offset);
	void dsp_io_w(offs_t offset, u8 data);

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

	// sound stream update overrides
	virtual void sound_stream_update_legacy(sound_stream &stream, stream_sample_t const * const *inputs, stream_sample_t * const *outputs, int samples) override;

	// device_memory_interface configuration
	virtual space_config_vector memory_space_config() const override;

	address_space_config m_data_config;

private:
	memory_access<16, 0, 0, ENDIANNESS_LITTLE>::cache m_cache;
	memory_access<16, 0, 0, ENDIANNESS_LITTLE>::specific m_data;
	inline u8 read_byte(offs_t a) { return m_cache.read_byte(a); }
	inline u16 read_word(offs_t a) { return read_byte(a) | (read_byte(a + 1) << 8); }
	inline void write_byte(offs_t a, u8 d) { m_data.write_byte(a, d); }
	inline void write_word(offs_t a, u16 d) { write_byte(a, d & 0xff); write_byte(a + 1, (d >> 8) & 0xff); }

	enum class env_state_t32 : u8
	{
		ATTACK,
		DECAY,
		SUSTAIN,
		RELEASE
	};

	struct voice_state_type                      /* Voice state type             */
	{
		u16             mem_ptr;        /* Sample data memory pointer   */
		int             end;            /* End or loop after block      */
		int             envcnt;         /* Counts to envelope update    */
		env_state_t32   envstate;       /* Current envelope state       */
		int             envx;           /* Last env height (0-0x7FFF)   */
		int             filter;         /* Last header's filter         */
		int             half;           /* Active nybble of BRR         */
		int             header_cnt;     /* Bytes before new header (0-8)*/
		int             mixfrac;        /* Fractional part of smpl pstn */
		int             on_cnt;         /* Is it time to turn on yet?   */
		int             pitch;          /* Sample pitch (4096->32000Hz) */
		int             range;          /* Last header's range          */
		u32             samp_id;        /* Sample ID#                   */
		int             sampptr;        /* Where in sampbuf we are      */
		s32             smp1;           /* Last sample (for BRR filter) */
		s32             smp2;           /* Second-to-last sample decoded*/
		s16             sampbuf[4];     /* Buffer for Gaussian interp   */
	};

	inline u16 vptr(u8 sd, u8 v) { return read_word((sd << 8) + (m_dsp_regs[v + 4] << 2)); }           /* Ptr to start of sample data  */
	inline u16 lptr(u8 sd, u8 v) { return read_word((sd << 8) + (m_dsp_regs[v + 4] << 2) + 2); }       /* Loop pointer in sample data  */
	inline u16 pitch(u8 v) { return (m_dsp_regs[v + 2] | (m_dsp_regs[v + 3] << 8)) & 0x3fff; }

	/* Make reading the ADSR code easier */
	inline u8 SL(u8 v) { return m_dsp_regs[(v << 4) + 6] >> 5; }         /* Returns SUSTAIN level        */
	inline u8 SR(u8 v) { return m_dsp_regs[(v << 4) + 6] & 0x1f; }       /* Returns SUSTAIN rate         */

	void dsp_reset();
	void dsp_update(s16 *sound_ptr);
	int advance_envelope(int v);
	void state_register();

	// internal state
	sound_stream          *m_channel;
	u8                    m_dsp_addr;
	u8                    m_dsp_regs[256];      /* DSP registers */

	int                   m_keyed_on;
	int                   m_keys;               /* 8-bits for 8 voices */
	voice_state_type      m_voice_state[8];

	/* Noise stuff */
	int                   m_noise_cnt;
	int                   m_noise_lev;

	/* These are for the FIR echo filter */
#ifndef NO_ECHO
	s16                   m_fir_lbuf[8];
	s16                   m_fir_rbuf[8];
	int                   m_fir_ptr;
	int                   m_echo_ptr;
#endif

};

DECLARE_DEVICE_TYPE(S_DSP, s_dsp_device)


#endif // MAME_SOUND_S_DSP_H