1 // license:BSD-3-Clause
2 // copyright-holders:Ernesto Corvi
3 /******************************************************************************
4 * FILE
5 * Yamaha 3812 emulator interface - MAME VERSION
6 *
7 * CREATED BY
8 * Ernesto Corvi
9 *
10 * UPDATE LOG
11 * JB 28-04-2002 Fixed simultaneous usage of all three different chip types.
12 * Used real sample rate when resample filter is active.
13 * AAT 12-28-2001 Protected Y8950 from accessing unmapped port and keyboard handlers.
14 * CHS 1999-01-09 Fixes new ym3812 emulation interface.
15 * CHS 1998-10-23 Mame streaming sound chip update
16 * EC 1998 Created Interface
17 *
18 * NOTES
19 *
20 ******************************************************************************/
21 #include "emu.h"
22 #include "3812intf.h"
23 #include "sound/fmopl.h"
24
25
irq_handler(int irq)26 void ym3812_device::irq_handler(int irq)
27 {
28 m_timer[2]->adjust(attotime::zero, irq);
29 }
30
31 /* Timer overflow callback from timer.c */
device_timer(emu_timer & timer,device_timer_id id,int param,void * ptr)32 void ym3812_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
33 {
34 switch(id)
35 {
36 case TIMER_A:
37 ym3812_timer_over(m_chip,0);
38 break;
39
40 case TIMER_B:
41 ym3812_timer_over(m_chip,1);
42 break;
43
44 case TIMER_IRQ_SYNC:
45 if (!m_irq_handler.isnull())
46 m_irq_handler(param);
47 break;
48 }
49 }
50
timer_handler(int c,const attotime & period)51 void ym3812_device::timer_handler(int c, const attotime &period)
52 {
53 if( period == attotime::zero )
54 { /* Reset FM Timer */
55 m_timer[c]->enable(false);
56 }
57 else
58 { /* Start FM Timer */
59 m_timer[c]->adjust(period);
60 }
61 }
62
63
64 //-------------------------------------------------
65 // sound_stream_update - handle a stream update
66 //-------------------------------------------------
67
sound_stream_update(sound_stream & stream,std::vector<read_stream_view> const & inputs,std::vector<write_stream_view> & outputs)68 void ym3812_device::sound_stream_update(sound_stream &stream, std::vector<read_stream_view> const &inputs, std::vector<write_stream_view> &outputs)
69 {
70 ym3812_update_one(m_chip, outputs[0]);
71 }
72
73 //-------------------------------------------------
74 // device_start - device-specific startup
75 //-------------------------------------------------
76
device_start()77 void ym3812_device::device_start()
78 {
79 int rate = clock() / 72;
80
81 m_irq_handler.resolve();
82
83 /* stream system initialize */
84 m_chip = ym3812_init(this, clock(), rate);
85 if (!m_chip)
86 throw emu_fatalerror("ym3812_device(%s): Error creating YM3812 chip", tag());
87
88 calculate_rates();
89
90 /* YM3812 setup */
91 ym3812_set_timer_handler (m_chip, ym3812_device::static_timer_handler, this);
92 ym3812_set_irq_handler (m_chip, ym3812_device::static_irq_handler, this);
93 ym3812_set_update_handler(m_chip, ym3812_device::static_update_request, this);
94
95 m_timer[0] = timer_alloc(TIMER_A);
96 m_timer[1] = timer_alloc(TIMER_B);
97 m_timer[2] = timer_alloc(TIMER_IRQ_SYNC);
98 }
99
device_clock_changed()100 void ym3812_device::device_clock_changed()
101 {
102 calculate_rates();
103 ym3812_clock_changed(m_chip, clock(), clock() / 72);
104 }
105
calculate_rates()106 void ym3812_device::calculate_rates()
107 {
108 int rate = clock() / 72;
109
110 if (m_stream != nullptr)
111 m_stream->set_sample_rate(rate);
112 else
113 m_stream = stream_alloc(0, 1, rate);
114 }
115
116 //-------------------------------------------------
117 // device_stop - device-specific stop
118 //-------------------------------------------------
119
device_stop()120 void ym3812_device::device_stop()
121 {
122 ym3812_shutdown(m_chip);
123 }
124
125 //-------------------------------------------------
126 // device_reset - device-specific reset
127 //-------------------------------------------------
128
device_reset()129 void ym3812_device::device_reset()
130 {
131 ym3812_reset_chip(m_chip);
132 }
133
134
read(offs_t offset)135 u8 ym3812_device::read(offs_t offset)
136 {
137 return ym3812_read(m_chip, offset & 1);
138 }
139
write(offs_t offset,u8 data)140 void ym3812_device::write(offs_t offset, u8 data)
141 {
142 ym3812_write(m_chip, offset & 1, data);
143 }
144
status_port_r()145 u8 ym3812_device::status_port_r() { return read(0); }
read_port_r()146 u8 ym3812_device::read_port_r() { return read(1); }
control_port_w(u8 data)147 void ym3812_device::control_port_w(u8 data) { write(0, data); }
write_port_w(u8 data)148 void ym3812_device::write_port_w(u8 data) { write(1, data); }
149
150
151 DEFINE_DEVICE_TYPE(YM3812, ym3812_device, "ym3812", "YM3812 OPL2")
152
ym3812_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)153 ym3812_device::ym3812_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
154 : device_t(mconfig, YM3812, tag, owner, clock)
155 , device_sound_interface(mconfig, *this)
156 , m_stream(nullptr)
157 , m_timer{ nullptr, nullptr }
158 , m_chip(nullptr)
159 , m_irq_handler(*this)
160 {
161 }
162