forked from iPodLinux-Community/iGameGear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.c
280 lines (218 loc) · 6.49 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
/*
Copyright (C) 1998, 1999, 2000 Charles Mac Donald
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_cart cart;
t_input input;
#ifdef ENABLESOUND
t_snd snd;
OPLL *opll;
struct
{
char reg[64];
}ym2413;
#endif
void system_init(int rate)
{
/* Initialize the VDP emulation */
vdp_init();
/* Initialize the SMS emulation */
sms_init();
/* Initialize the look-up tables and related data */
render_init();
/* Enable sound emulation if the sample rate was specified */
#ifdef ENABLESOUND
audio_init(rate);
#endif
/* Don't save SRAM by default */
sms.save = 0;
/* Clear emulated button state */
memset(&input, 0, sizeof(t_input));
}
#ifdef ENABLESOUND
void audio_init(int rate)
{
/* Clear sound context */
memset(&snd, 0, sizeof(t_snd));
/* Reset logging data */
snd.log = 0;
snd.callback = NULL;
/* Oops.. sound is disabled */
if(!rate) return;
/* Calculate buffer size in samples */
snd.bufsize = (rate / 60);
/* Sound output */
snd.buffer[0] = (signed short int *)malloc(snd.bufsize * 2);
snd.buffer[1] = (signed short int *)malloc(snd.bufsize * 2);
if(!snd.buffer[0] || !snd.buffer[1]) return;
memset(snd.buffer[0], 0, snd.bufsize * 2);
memset(snd.buffer[1], 0, snd.bufsize * 2);
/* YM2413 sound stream */
snd.fm_buffer = (signed short int *)malloc(snd.bufsize * 2);
if(!snd.fm_buffer) return;
memset(snd.fm_buffer, 0, snd.bufsize * 2);
/* SN76489 sound stream */
snd.psg_buffer[0] = (signed short int *)malloc(snd.bufsize * 2);
snd.psg_buffer[1] = (signed short int *)malloc(snd.bufsize * 2);
if(!snd.psg_buffer[0] || !snd.psg_buffer[1]) return;
memset(snd.psg_buffer[0], 0, snd.bufsize * 2);
memset(snd.psg_buffer[1], 0, snd.bufsize * 2);
/* Set up SN76489 emulation */
SN76496_init(0, MASTER_CLOCK, 255, rate);
/* Set up YM2413 emulation */
OPLL_init(3579545, rate) ;
opll = OPLL_new() ;
OPLL_reset(opll) ;
OPLL_reset_patch(opll,0) ; /* if use default voice data. */
/* Inform other functions that we can use sound */
snd.enabled = 1;
}
#endif
void system_shutdown(void)
{
#ifdef ENABLESOUND
if(snd.enabled)
{
OPLL_delete(opll);
OPLL_close();
}
#endif
}
void system_reset(void)
{
cpu_reset();
vdp_reset();
sms_reset();
render_reset();
system_load_sram();
#ifdef ENABLESOUND
if(snd.enabled)
{
OPLL_reset(opll) ;
OPLL_reset_patch(opll,0) ; /* if use default voice data. */
}
#endif
}
void system_save_state(void *fd)
{
/* Save VDP context */
fwrite(&vdp, sizeof(t_vdp), 1, fd);
/* Save SMS context */
fwrite(&sms, sizeof(t_sms), 1, fd);
/* Save Z80 context */
fwrite(Z80_Context, sizeof(Z80_Regs), 1, fd);
fwrite(&after_EI, sizeof(int), 1, fd);
#ifdef ENABLESOUND
/* Save YM2413 registers */
fwrite(&ym2413.reg[0], 0x40, 1, fd);
/* Save SN76489 context */
fwrite(&sn[0], sizeof(t_SN76496), 1, fd);
#endif
}
void system_load_state(void *fd)
{
int i;
uint8 reg[0x40];
/* Initialize everything */
cpu_reset();
system_reset();
/* Load VDP context */
fread(&vdp, sizeof(t_vdp), 1, fd);
/* Load SMS context */
fread(&sms, sizeof(t_sms), 1, fd);
/* Load Z80 context */
fread(Z80_Context, sizeof(Z80_Regs), 1, fd);
fread(&after_EI, sizeof(int), 1, fd);
#ifdef ENABLESOUND
/* Load YM2413 registers */
fread(reg, 0x40, 1, fd);
/* Load SN76489 context */
fread(&sn[0], sizeof(t_SN76496), 1, fd);
#endif
/* Restore callbacks */
z80_set_irq_callback(sms_irq_callback);
cpu_readmap[0] = cart.rom + 0x0000; /* 0000-3FFF */
cpu_readmap[1] = cart.rom + 0x2000;
cpu_readmap[2] = cart.rom + 0x4000; /* 4000-7FFF */
cpu_readmap[3] = cart.rom + 0x6000;
cpu_readmap[4] = cart.rom + 0x0000; /* 0000-3FFF */
cpu_readmap[5] = cart.rom + 0x2000;
cpu_readmap[6] = sms.ram;
cpu_readmap[7] = sms.ram;
cpu_writemap[0] = sms.dummy;
cpu_writemap[1] = sms.dummy;
cpu_writemap[2] = sms.dummy;
cpu_writemap[3] = sms.dummy;
cpu_writemap[4] = sms.dummy;
cpu_writemap[5] = sms.dummy;
cpu_writemap[6] = sms.ram;
cpu_writemap[7] = sms.ram;
sms_mapper_w(3, sms.fcr[3]);
sms_mapper_w(2, sms.fcr[2]);
sms_mapper_w(1, sms.fcr[1]);
sms_mapper_w(0, sms.fcr[0]);
/* Force full pattern cache update */
is_vram_dirty = 1;
memset(vram_dirty, 1, 0x200);
/* Restore palette */
for(i = 0; i < PALETTE_SIZE; i += 1)
palette_sync(i);
/* Restore sound state */
#ifdef ENABLESOUND
if(snd.enabled)
{
/* Clear YM2413 context */
OPLL_reset(opll) ;
OPLL_reset_patch(opll,0) ; /* if use default voice data. */
/* Restore rhythm enable first */
ym2413_write(0, 0, 0x0E);
ym2413_write(0, 1, reg[0x0E]);
/* User instrument settings */
for(i = 0x00; i <= 0x07; i += 1)
{
ym2413_write(0, 0, i);
ym2413_write(0, 1, reg[i]);
}
/* Channel frequency */
for(i = 0x10; i <= 0x18; i += 1)
{
ym2413_write(0, 0, i);
ym2413_write(0, 1, reg[i]);
}
/* Channel frequency + ctrl. */
for(i = 0x20; i <= 0x28; i += 1)
{
ym2413_write(0, 0, i);
ym2413_write(0, 1, reg[i]);
}
/* Instrument and volume settings */
for(i = 0x30; i <= 0x38; i += 1)
{
ym2413_write(0, 0, i);
ym2413_write(0, 1, reg[i]);
}
}
#endif
}
#ifdef ENABLESOUND
void ym2413_write(int chip, int offset, int data)
{
static uint8 latch = 0;
if(offset & 1)
OPLL_writeReg(opll, latch, data);
else
latch = data;
}
#endif