-
Notifications
You must be signed in to change notification settings - Fork 3
/
system.c
249 lines (192 loc) · 6.04 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
/*
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"
void psms_manage_sram(u8 *sram, int mode);
t_bitmap bitmap;
t_cart cart;
t_snd snd;
t_input input;
FM_OPL *ym3812;
extern int snd_sample;
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 */
audio_init(rate);
/* Don't save SRAM by default */
sms.save = 0;
/* Clear emulated button state */
memset(&input, 0, sizeof(t_input));
}
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);
snd.bufsize = snd_sample;
/* Sound output */
//snd.buffer[0] = (signed short int *)malloc(snd.bufsize * 2);
//snd.buffer[1] = (signed short int *)malloc(snd.bufsize * 2);
snd.buffer[0] = (signed short int *)memalign(16, snd.bufsize * 2);
snd.buffer[1] = (signed short int *)memalign(16, snd.bufsize * 2);
if(!snd.buffer[0] || !snd.buffer[1]) return;
/* YM3812/YM2413 sound stream */
snd.fm_buffer = (signed short int *)memalign(16, snd.bufsize * 2);
if(!snd.fm_buffer) return;
/* SN76489 sound stream */
snd.psg_buffer[0] = (signed short int *)memalign(16, snd.bufsize * 2);
snd.psg_buffer[1] = (signed short int *)memalign(16, snd.bufsize * 2);
if(!snd.psg_buffer[0] || !snd.psg_buffer[1]) return;
/* Set up SN76489 emulation */
SN76496_init(0, MASTER_CLOCK, 255, rate);
/* Set up YM3812 emulation */
ym3812 = OPLCreate(OPL_TYPE_YM3812, MASTER_CLOCK, rate);
if(!ym3812) return;
/* Set up YM2413 emulation */
ym2413_init(1);
/* Inform other functions that we can use sound */
snd.enabled = 1;
sms.use_fm = 0; // FM aint working for some reason, will investigate.
}
void system_shutdown(void)
{
if(snd.enabled) OPLDestroy(ym3812);
}
void system_reset(void)
{
cpu_reset();
vdp_reset();
sms_reset();
render_reset();
//system_load_sram();
psms_manage_sram(sms.sram, SRAM_LOAD);
if(snd.enabled)
{
SN76496_init(0, MASTER_CLOCK, 255, 48000); // Added for PSMS
OPLResetChip(ym3812);
ym2413_reset(0);
}
}
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);
// Save YM2413 registers
fwrite(&ym2413[0].reg[0], 0x40, 1, fd);
// Save SN76489 context
fwrite(&sn[0], sizeof(t_SN76496), 1, fd);
*/
}
void system_load_state(void *fd)
{
/*
int i;
byte 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);
// Load YM2413 registers
fread(reg, 0x40, 1, fd);
// Load SN76489 context
fread(&sn[0], sizeof(t_SN76496), 1, fd);
// 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
if(snd.enabled)
{
// Restore YM2413 emulation
OPLResetChip(ym3812);
// Clear YM2413 context
ym2413_reset(0);
// 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]);
}
}
*/
}