-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.c
350 lines (292 loc) · 7.95 KB
/
system.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
Copyright (C) 1999, 2000, 2001, 2002, 2003 Charles MacDonald
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "shared.h"
t_bitmap bitmap;
t_input input;
t_snd snd;
static int sound_tbl[262];
#ifdef _arch_dreamcast
#include "menu.h"
#include "dreamcast-x/snd_stream.h"
void **audio_callback(int samples_requested, int *samples_returned);
#endif
#define SND_SIZE (snd.buffer_size * sizeof(int16))
int audio_init(int rate)
{
int i;
/* 68000 and YM2612 clock */
float vclk = 53693175.0 / 7;
/* Z80 and SN76489 clock */
float zclk = 3579545.0;
/* Clear the sound data context */
memset(&snd, 0, sizeof(snd));
/* Make sure the requested sample rate is valid */
if(!rate || ((rate < 8000) | (rate > 44100)))
{
return (0);
}
/* Calculate the sound buffer size */
#ifdef MODE_PAL
snd.buffer_size = (rate / 50);
#else
snd.buffer_size = (rate / 60);
#endif
snd.sample_rate = rate;
/* Allocate sound buffers */
#ifdef _arch_dreamcast
snd.buffer[0] = memalign(32, SND_SIZE);
snd.buffer[1] = memalign(32, SND_SIZE);
snd.fm.buffer[0] = memalign(32, SND_SIZE);
snd.fm.buffer[1] = memalign(32, SND_SIZE);
snd.psg.buffer = memalign(32, SND_SIZE);
#else // !_arch_dreamcast
snd.buffer[0] = malloc(snd.buffer_size * sizeof(int16));
snd.buffer[1] = malloc(snd.buffer_size * sizeof(int16));
#ifdef GENS_FM
snd.fm.buffer[0] = malloc(snd.buffer_size * sizeof(int));
snd.fm.buffer[1] = malloc(snd.buffer_size * sizeof(int));
#else // !GENS_FM
snd.fm.buffer[0] = malloc(snd.buffer_size * sizeof(int16));
snd.fm.buffer[1] = malloc(snd.buffer_size * sizeof(int16));
#endif // GENS_FM
snd.psg.buffer = malloc(snd.buffer_size * sizeof(int16));
#endif // _arch_dreamcast
/* Make sure we could allocate everything */
if(!snd.buffer[0] || !snd.buffer[1] || !snd.fm.buffer[0] || !snd.fm.buffer[1] || !snd.psg.buffer)
{
return (0);
}
/* Initialize sound chip emulation */
SN76496_sh_start(zclk, 100, rate);
#ifdef GENS_FM
YM2612_Init(vclk, rate, 0);
#else
YM2612Init(1, vclk, rate, NULL, NULL);
#endif
/* Set audio enable flag */
snd.enabled = 1;
/* Make sound table */
for (i = 0; i < 262; i++)
{
float p = snd.buffer_size * i;
p = p / 262;
sound_tbl[i] = p;
}
#ifdef _arch_dreamcast
gpdc_snd_stream_init(&audio_callback);
gpdc_snd_stream_clear();
gpdc_snd_stream_start(rate, 1);
#endif
return (1);
}
void audio_shutdown(void)
{
#ifdef _arch_dreamcast
gpdc_snd_stream_stop();
gpdc_snd_stream_shutdown();
#endif
}
void system_init(void)
{
gen_init();
vdp_init();
render_init();
}
void system_reset(void)
{
gen_reset();
vdp_reset();
render_reset();
if(snd.enabled)
{
#ifdef GENS_FM
YM2612_Reset();
#else
YM2612ResetChip(0);
#endif
memset(snd.buffer[0], 0, SND_SIZE);
memset(snd.buffer[1], 0, SND_SIZE);
#ifdef GENS_FM
memset(snd.fm.buffer[0], 0, SND_SIZE*2);
memset(snd.fm.buffer[1], 0, SND_SIZE*2);
#else
memset(snd.fm.buffer[0], 0, SND_SIZE);
memset(snd.fm.buffer[1], 0, SND_SIZE);
#endif
memset(snd.psg.buffer, 0, SND_SIZE);
}
}
void system_shutdown(void)
{
gen_shutdown();
vdp_shutdown();
render_shutdown();
}
int system_frame(int do_skip)
{
int line;
if(gen_running == 0)
return 0;
/* Clear V-Blank flag */
status &= ~0x0008;
/* Toggle even/odd flag (IM2 only) */
if(im2_flag)
status ^= 0x0010;
/* Point to start of sound buffer */
snd.fm.lastStage = snd.fm.curStage = 0;
snd.psg.lastStage = snd.psg.curStage = 0;
#ifdef GENS_FM
/* Blank sound buffer */
memset(snd.fm.buffer[0], 0, SND_SIZE*2);
memset(snd.fm.buffer[1], 0, SND_SIZE*2);
#endif
#ifndef XVDP
/* Parse sprites for line 0 (done on line 261) */
parse_satb(0x80);
#endif
for(line = 0; line < 262; line += 1)
{
/* Used by HV counter */
v_counter = line;
/* Run Z80 emulation (if enabled) */
if(zreset == 1 && zbusreq == 0)
{
#ifdef _arch_dreamcast
z80_execute(dc_options.z80_clock);
#elif defined MODE_PAL
z80_execute(274);
#else
z80_execute(228);
#endif
if(!gen_running) break;
}
/* Run 68000 emulation */
#ifdef _arch_dreamcast
M68K_Exec(dc_options.m68_clock);
#elif defined MODE_PAL
M68K_Exec(585);
#else
M68K_Exec(487);
#endif
if(!gen_running) break;
/* If a Z80 interrupt is still pending after a scanline, cancel it */
if(zirq == 1)
{
zirq = 0;
z80_set_irq_line(0, CLEAR_LINE);
}
#ifndef XVDP
/* Render a line of the display */
if(do_skip == 0)
{
if(line < frame_end ) render_line(line);
if(line < frame_end-1 ) parse_satb(0x81 + line);
}
#endif
/* Do H interrupt processing */
if(line <= frame_end)
{
counter -= 1;
if(counter == -1)
{
counter = reg[10];
hint_pending = 1;
if(reg[0] & 0x10)
{
M68K_SetIRQ(4);
}
}
}
else
{
counter = reg[10];
}
/* Do end of frame processing */
if(line == frame_end)
{
status |= 0x0088;
vint_pending = 1;
/* Give enough time to read the interrupt pending flag before
the interrupt actually occurs. */
M68K_Exec(16);
if(!gen_running) break;
if(reg[1] & 0x20)
{
M68K_SetIRQ(6);
}
if(zreset == 1 && zbusreq == 0)
{
z80_set_irq_line(0, ASSERT_LINE);
zirq = 1;
}
}
fm_update_timers();
snd.fm.curStage = sound_tbl[line];
snd.psg.curStage = sound_tbl[line];
}
if(snd.enabled)
{
audio_update();
}
return gen_running;
}
#ifdef GENS_FM
# define PSG_VOLUME 3
# define FM_VOLUME 2
#else
# define PSG_VOLUME 2
# define FM_VOLUME 1
#endif
void audio_update(void)
{
int i;
int16 acc;
#ifdef GENS_FM
int32 *tempBuffer[2];
#else
int16 *tempBuffer[2];
#endif
tempBuffer[0] = snd.fm.buffer[0] + snd.fm.lastStage;
tempBuffer[1] = snd.fm.buffer[1] + snd.fm.lastStage;
#ifdef GENS_FM
YM2612_Update((int **)tempBuffer, snd.buffer_size - snd.fm.lastStage);
YM2612_DacAndTimers_Update((int **)tempBuffer, snd.buffer_size - snd.fm.lastStage);
#else
YM2612UpdateOne(0, (int16 **)tempBuffer, snd.buffer_size - snd.fm.lastStage);
#endif
SN76496Update(0, snd.psg.buffer + snd.psg.lastStage, snd.buffer_size - snd.psg.lastStage);
for(i = 0; i < snd.buffer_size; i += 1)
{
int16 psg = snd.psg.buffer[i] / PSG_VOLUME;
acc = 0;
acc += snd.fm.buffer[0][i] / FM_VOLUME;
acc += psg;
snd.buffer[0][i] = acc;
acc = 0;
acc += snd.fm.buffer[1][i] / FM_VOLUME;
acc += psg;
snd.buffer[1][i] = acc;
}
#ifdef _arch_dreamcast
gpdc_snd_stream_poll();
#endif
}
#ifdef _arch_dreamcast
void **audio_callback(int samples_requested, int *samples_returned)
{
*samples_returned = snd.buffer_size;
return (void**)snd.buffer;
}
#endif