1 /***************************************************************************
2
3 262intf.c
4
5 MAME interface for YMF262 (OPL3) emulator
6
7 ***************************************************************************/
8 #include "mamedef.h"
9 #include <stdlib.h>
10 //#include "attotime.h"
11 //#include "sndintrf.h"
12 //#include "streams.h"
13 #include "262intf.h"
14 #ifdef ENABLE_ALL_CORES
15 #include "ymf262.h"
16 #endif
17
18 #define OPLTYPE_IS_OPL3
19 #include "adlibemu.h"
20
21
22 #define EC_DBOPL 0x00 // DosBox OPL (AdLibEmu)
23 #ifdef ENABLE_ALL_CORES
24 #define EC_MAME 0x01 // YMF262 core from MAME
25 #endif
26
27 typedef struct _ymf262_state ymf262_state;
28 struct _ymf262_state
29 {
30 //sound_stream * stream;
31 //emu_timer * timer[2];
32 void * chip;
33 int EMU_CORE;
34 //const ymf262_interface *intf;
35 //const device_config *device;
36 };
37
38
39 /*INLINE ymf262_state *get_safe_token(const device_config *device)
40 {
41 assert(device != NULL);
42 assert(device->token != NULL);
43 assert(device->type == SOUND);
44 assert(sound_get_type(device) == SOUND_YMF262);
45 return (ymf262_state *)device->token;
46 }*/
47
48
49
50
IRQHandler_262(void * param,int irq)51 static void IRQHandler_262(void *param,int irq)
52 {
53 ymf262_state *info = (ymf262_state *)param;
54 //if (info->intf->handler) (info->intf->handler)(info->device, irq);
55 }
56
57 /*static TIMER_CALLBACK( timer_callback_262_0 )
58 {
59 ymf262_state *info = (ymf262_state *)ptr;
60 ymf262_timer_over(info->chip, 0);
61 }
62
63 static TIMER_CALLBACK( timer_callback_262_1 )
64 {
65 ymf262_state *info = (ymf262_state *)ptr;
66 ymf262_timer_over(info->chip, 1);
67 }*/
68
69 //static void timer_handler_262(void *param,int timer, attotime period)
timer_handler_262(void * param,int timer,int period)70 static void timer_handler_262(void *param,int timer, int period)
71 {
72 ymf262_state *info = (ymf262_state *)param;
73 if( period == 0 )
74 { /* Reset FM Timer */
75 //timer_enable(info->timer[timer], 0);
76 }
77 else
78 { /* Start FM Timer */
79 //timer_adjust_oneshot(info->timer[timer], period, 0);
80 }
81 }
82
83 //static STREAM_UPDATE( ymf262_stream_update )
ymf262_stream_update(void * param,stream_sample_t ** outputs,int samples)84 void ymf262_stream_update(void *param, stream_sample_t **outputs, int samples)
85 {
86 ymf262_state *info = (ymf262_state *)param;
87 switch(info->EMU_CORE)
88 {
89 #ifdef ENABLE_ALL_CORES
90 case EC_MAME:
91 ymf262_update_one(info->chip, outputs, samples);
92 break;
93 #endif
94 case EC_DBOPL:
95 adlib_OPL3_getsample(info->chip, outputs, samples);
96 break;
97 }
98 }
99
100 static stream_sample_t* DUMMYBUF[0x02] = {NULL, NULL};
101
_stream_update(void * param)102 static void _stream_update(void *param/*, int interval*/)
103 {
104 ymf262_state *info = (ymf262_state *)param;
105 //stream_update(info->stream);
106
107 switch(info->EMU_CORE)
108 {
109 #ifdef ENABLE_ALL_CORES
110 case EC_MAME:
111 ymf262_update_one(info->chip, DUMMYBUF, 0);
112 break;
113 #endif
114 case EC_DBOPL:
115 adlib_OPL3_getsample(info->chip, DUMMYBUF, 0);
116 break;
117 }
118 }
119
120
121 //static DEVICE_START( ymf262 )
device_start_ymf262(void ** _info,int EMU_CORE,int clock,int CHIP_SAMPLING_MODE,int CHIP_SAMPLE_RATE)122 int device_start_ymf262(void **_info, int EMU_CORE, int clock, int CHIP_SAMPLING_MODE, int CHIP_SAMPLE_RATE)
123 {
124 //static const ymf262_interface dummy = { 0 };
125 //ymf262_state *info = get_safe_token(device);
126 ymf262_state *info;
127 int rate;
128
129 #ifdef ENABLE_ALL_CORES
130 if (EMU_CORE >= 0x02)
131 EMU_CORE = EC_DBOPL;
132 #else
133 EMU_CORE = EC_DBOPL;
134 #endif
135
136 info = (ymf262_state *) calloc(1, sizeof(ymf262_state));
137 *_info = (void *) info;
138
139 info->EMU_CORE = EMU_CORE;
140 rate = clock/288;
141 if ((CHIP_SAMPLING_MODE == 0x01 && rate < CHIP_SAMPLE_RATE) ||
142 CHIP_SAMPLING_MODE == 0x02)
143 rate = CHIP_SAMPLE_RATE;
144
145 //info->intf = device->static_config ? (const ymf262_interface *)device->static_config : &dummy;
146 //info->intf = &dummy;
147 //info->device = device;
148
149 /* stream system initialize */
150 switch(EMU_CORE)
151 {
152 #ifdef ENABLE_ALL_CORES
153 case EC_MAME:
154 info->chip = ymf262_init(clock,rate);
155 //assert_always(info->chip != NULL, "Error creating YMF262 chip");
156
157 //info->stream = stream_create(device,0,4,rate,info,ymf262_stream_update);
158
159 /* YMF262 setup */
160 ymf262_set_timer_handler (info->chip, timer_handler_262, info);
161 ymf262_set_irq_handler (info->chip, IRQHandler_262, info);
162 ymf262_set_update_handler(info->chip, _stream_update, info);
163
164 //info->timer[0] = timer_alloc(device->machine, timer_callback_262_0, info);
165 //info->timer[1] = timer_alloc(device->machine, timer_callback_262_1, info);
166 break;
167 #endif
168 case EC_DBOPL:
169 info->chip = adlib_OPL3_init(clock, rate, _stream_update, info);
170 break;
171 }
172
173 return rate;
174 }
175
176 //static DEVICE_STOP( ymf262 )
device_stop_ymf262(void * _info)177 void device_stop_ymf262(void *_info)
178 {
179 //ymf262_state *info = get_safe_token(device);
180 ymf262_state *info = (ymf262_state *)_info;
181 switch(info->EMU_CORE)
182 {
183 #ifdef ENABLE_ALL_CORES
184 case EC_MAME:
185 ymf262_shutdown(info->chip);
186 break;
187 #endif
188 case EC_DBOPL:
189 adlib_OPL3_stop(info->chip);
190 break;
191 }
192 free(info);
193 }
194
195 /* reset */
196 //static DEVICE_RESET( ymf262 )
device_reset_ymf262(void * _info)197 void device_reset_ymf262(void *_info)
198 {
199 //ymf262_state *info = get_safe_token(device);
200 ymf262_state *info = (ymf262_state *)_info;
201 switch(info->EMU_CORE)
202 {
203 #ifdef ENABLE_ALL_CORES
204 case EC_MAME:
205 ymf262_reset_chip(info->chip);
206 break;
207 #endif
208 case EC_DBOPL:
209 adlib_OPL3_reset(info->chip);
210 break;
211 }
212 }
213
214
215 //READ8_DEVICE_HANDLER( ymf262_r )
ymf262_r(void * _info,offs_t offset)216 UINT8 ymf262_r(void *_info, offs_t offset)
217 {
218 //ymf262_state *info = get_safe_token(device);
219 ymf262_state *info = (ymf262_state *)_info;
220 switch(info->EMU_CORE)
221 {
222 #ifdef ENABLE_ALL_CORES
223 case EC_MAME:
224 return ymf262_read(info->chip, offset & 3);
225 #endif
226 case EC_DBOPL:
227 return adlib_OPL3_reg_read(info->chip, offset & 0x03);
228 default:
229 return 0x00;
230 }
231 }
232
233 //WRITE8_DEVICE_HANDLER( ymf262_w )
ymf262_w(void * _info,offs_t offset,UINT8 data)234 void ymf262_w(void *_info, offs_t offset, UINT8 data)
235 {
236 //ymf262_state *info = get_safe_token(device);
237 ymf262_state *info = (ymf262_state *)_info;
238 switch(info->EMU_CORE)
239 {
240 #ifdef ENABLE_ALL_CORES
241 case EC_MAME:
242 ymf262_write(info->chip, offset & 3, data);
243 break;
244 #endif
245 case EC_DBOPL:
246 adlib_OPL3_writeIO(info->chip, offset & 3, data);
247 break;
248 }
249 }
250
251 //READ8_DEVICE_HANDLER ( ymf262_status_r )
ymf262_status_r(void * info,offs_t offset)252 UINT8 ymf262_status_r(void *info, offs_t offset)
253 {
254 return ymf262_r(info, 0);
255 }
256 //WRITE8_DEVICE_HANDLER( ymf262_register_a_w )
ymf262_register_a_w(void * info,offs_t offset,UINT8 data)257 void ymf262_register_a_w(void *info, offs_t offset, UINT8 data)
258 {
259 ymf262_w(info, 0, data);
260 }
261 //WRITE8_DEVICE_HANDLER( ymf262_register_b_w )
ymf262_register_b_w(void * info,offs_t offset,UINT8 data)262 void ymf262_register_b_w(void *info, offs_t offset, UINT8 data)
263 {
264 ymf262_w(info, 2, data);
265 }
266 //WRITE8_DEVICE_HANDLER( ymf262_data_a_w )
ymf262_data_a_w(void * info,offs_t offset,UINT8 data)267 void ymf262_data_a_w(void *info, offs_t offset, UINT8 data)
268 {
269 ymf262_w(info, 1, data);
270 }
271 //WRITE8_DEVICE_HANDLER( ymf262_data_b_w )
ymf262_data_b_w(void * info,offs_t offset,UINT8 data)272 void ymf262_data_b_w(void *info, offs_t offset, UINT8 data)
273 {
274 ymf262_w(info, 3, data);
275 }
276
277
ymf262_set_mute_mask(void * _info,UINT32 MuteMask)278 void ymf262_set_mute_mask(void *_info, UINT32 MuteMask)
279 {
280 ymf262_state *info = (ymf262_state *)_info;
281 switch(info->EMU_CORE)
282 {
283 #ifdef ENABLE_ALL_CORES
284 case EC_MAME:
285 ymf262_set_mutemask(info->chip, MuteMask);
286 break;
287 #endif
288 case EC_DBOPL:
289 adlib_OPL3_set_mute_mask(info->chip, MuteMask);
290 break;
291 }
292
293 return;
294 }
295
296
297 /**************************************************************************
298 * Generic get_info
299 **************************************************************************/
300
301 /*DEVICE_GET_INFO( ymf262 )
302 {
303 switch (state)
304 {
305 // --- the following bits of info are returned as 64-bit signed integers ---
306 case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(ymf262_state); break;
307
308 // --- the following bits of info are returned as pointers to data or functions ---
309 case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( ymf262 ); break;
310 case DEVINFO_FCT_STOP: info->stop = DEVICE_STOP_NAME( ymf262 ); break;
311 case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME( ymf262 ); break;
312
313 // --- the following bits of info are returned as NULL-terminated strings ---
314 case DEVINFO_STR_NAME: strcpy(info->s, "YMF262"); break;
315 case DEVINFO_STR_FAMILY: strcpy(info->s, "Yamaha FM"); break;
316 case DEVINFO_STR_VERSION: strcpy(info->s, "1.0"); break;
317 case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break;
318 case DEVINFO_STR_CREDITS: strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
319 }
320 }*/
321
322