1 // license:BSD-3-Clause
2 // copyright-holders:R. Belmont, Olivier Galibert, hap
3 #ifndef MAME_SOUND_YMF278B_H
4 #define MAME_SOUND_YMF278B_H
5 
6 #pragma once
7 
8 #include "dirom.h"
9 
10 class ymf278b_device : public device_t, public device_sound_interface, public device_rom_interface<22>
11 {
12 public:
13 	ymf278b_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock);
14 
15 	// configuration helpers
irq_handler()16 	auto irq_handler() { return m_irq_handler.bind(); }
17 
18 	u8 read(offs_t offset);
19 	void write(offs_t offset, u8 data);
20 
21 protected:
22 	// device-level overrides
23 	virtual void device_post_load() override;
24 	virtual void device_start() override;
25 	virtual void device_reset() override;
26 	virtual void device_stop() override;
27 	virtual void device_clock_changed() override;
28 
29 	virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) override;
30 
31 	// sound stream update overrides
32 	virtual void sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs) override;
33 
34 	// device_rom_interface overrides
35 	virtual void rom_bank_updated() override;
36 
37 private:
38 	struct YMF278BSlot
39 	{
40 		int16_t wave;     /* wavetable number */
41 		int16_t F_NUMBER; /* frequency */
42 		int8_t octave;    /* octave */
43 		int8_t preverb;   /* pseudo-reverb */
44 		int8_t DAMP;      /* damping */
45 		int8_t CH;        /* output channel */
46 		int8_t LD;        /* level direct */
47 		int8_t TL;        /* total level */
48 		int8_t pan;       /* panpot */
49 		int8_t LFO;       /* LFO */
50 		int8_t VIB;       /* vibrato */
51 		int8_t AM;        /* tremolo */
52 
53 		int8_t AR;        /* attack rate */
54 		int8_t D1R;       /* decay 1 rate */
55 		int8_t DL;        /* decay level */
56 		int8_t D2R;       /* decay 2 rate */
57 		int8_t RC;        /* rate correction */
58 		int8_t RR;        /* release rate */
59 
60 		uint32_t step;    /* fixed-point frequency step */
61 		uint64_t stepptr; /* fixed-point pointer into the sample */
62 
63 		int8_t active;    /* channel is playing */
64 		int8_t KEY_ON;    /* slot keyed on */
65 		int8_t bits;      /* width of the samples */
66 		uint32_t startaddr;
67 		uint32_t loopaddr;
68 		uint32_t endaddr;
69 
70 		int env_step;
71 		uint32_t env_vol;
72 		uint32_t env_vol_step;
73 		uint32_t env_vol_lim;
74 		int8_t env_preverb;
75 
76 		int num;        /* slot number (for debug only) */
77 	};
78 
79 	int compute_rate(YMF278BSlot *slot, int val);
80 	uint32_t compute_decay_env_vol_step(YMF278BSlot *slot, int val);
81 	void compute_freq_step(YMF278BSlot *slot);
82 	void compute_envelope(YMF278BSlot *slot);
83 	void irq_check();
84 	void A_w(uint8_t reg, uint8_t data);
85 	void B_w(uint8_t reg, uint8_t data);
86 	void retrigger_note(YMF278BSlot *slot);
87 	void C_w(uint8_t reg, uint8_t data);
88 	void timer_busy_start(int is_pcm);
89 	void precompute_rate_tables();
90 	void register_save_state();
91 
update_request()92 	void update_request() { m_stream->update(); }
93 
static_irq_handler(device_t * param,int irq)94 	static void static_irq_handler(device_t *param, int irq) { }
static_timer_handler(device_t * param,int c,const attotime & period)95 	static void static_timer_handler(device_t *param, int c, const attotime &period) { }
static_update_request(device_t * param,int interval)96 	static void static_update_request(device_t *param, int interval) { downcast<ymf278b_device *>(param)->update_request(); }
97 
98 	// internal state
99 	uint8_t m_pcmregs[256];
100 	YMF278BSlot m_slots[24];
101 	int8_t m_wavetblhdr;
102 	int8_t m_memmode;
103 	int32_t m_memadr;
104 
105 	uint8_t m_status_busy, m_status_ld;
106 	emu_timer *m_timer_busy;
107 	emu_timer *m_timer_ld;
108 	uint8_t m_exp;
109 
110 	int32_t m_fm_l, m_fm_r;
111 	int32_t m_pcm_l, m_pcm_r;
112 
113 	attotime m_timer_base;
114 	uint8_t m_timer_a_count, m_timer_b_count;
115 	uint8_t m_enable, m_current_irq;
116 	int m_irq_line;
117 
118 	uint8_t m_port_C, m_port_AB, m_lastport;
119 
120 	// precomputed tables
121 	uint32_t m_lut_ar[64];              // attack rate
122 	uint32_t m_lut_dr[64];              // decay rate
123 	int32_t m_volume[256*4];            // precalculated attenuation values with some margin for envelope and pan levels
124 	int m_pan_left[16],m_pan_right[16]; // pan volume offsets
125 	int32_t m_mix_level[8];
126 
127 	emu_timer *m_timer_a, *m_timer_b;
128 	int m_clock;
129 	int m_rate;
130 
131 	sound_stream * m_stream;
132 	std::vector<int32_t> m_mix_buffer;
133 	devcb_write_line m_irq_handler;
134 	uint8_t m_last_fm_data;
135 
136 	// ymf262
137 	void *m_ymf262;
138 };
139 
140 DECLARE_DEVICE_TYPE(YMF278B, ymf278b_device)
141 
142 #endif // MAME_SOUND_YMF278B_H
143