forked from iPodLinux-Community/iGameGear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.c
315 lines (263 loc) · 7.9 KB
/
sms.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
#include "shared.h"
/* SMS context */
t_sms sms;
/* Run the virtual console emulation for one frame */
void sms_frame(int skip_render)
{
/* Take care of hard resets */
if(input.system & INPUT_HARD_RESET)
{
system_reset();
}
/* Debounce pause key */
if(input.system & INPUT_PAUSE)
{
if(!sms.paused)
{
sms.paused = 1;
z80_set_nmi_line(ASSERT_LINE);
z80_set_nmi_line(CLEAR_LINE);
}
}
else
{
sms.paused = 0;
}
/*if(snd.log) snd.callback(0x00);*/
for(vdp.line = 0; vdp.line < 262; vdp.line += 1)
{
/* Handle VDP line events */
vdp_run();
/* Draw the current frame */
if(!skip_render) render_line(vdp.line);
/* Run the Z80 for a line */
z80_execute(227);
}
/* Update the emulated sound stream */
/*if(snd.enabled)
{
int count;
SN76496Update(0, snd.psg_buffer, snd.bufsize, sms.psg_mask);
if(sms.use_fm)
{
int i;
for(i = 0; i < snd.bufsize; i++)
{
snd.fm_buffer[i] = OPLL_calc(opll);
}
}
for(count = 0; count < snd.bufsize; count += 1)
{
signed short left = 0;
signed short right = 0;
left = right = snd.fm_buffer[count];
left += snd.psg_buffer[0][count];
right += snd.psg_buffer[1][count];
snd.buffer[0][count] = left;
snd.buffer[1][count] = right;
}
}*/
}
void sms_init(void)
{
#if PSX
z80_set_context((void *)0x1F800000);
#endif
cpu_reset();
sms_reset();
}
void sms_reset(void)
{
/* Clear SMS context */
memset(sms.dummy, 0, 0x2000);
memset(sms.ram, 0, 0x2000);
memset(sms.sram, 0, 0x8000);
sms.paused = sms.save = sms.port_3F = sms.port_F2 = sms.irq = 0x00;
sms.psg_mask = 0xFF;
/* Load memory maps with default values */
cpu_readmap[0] = cart.rom + 0x0000;
cpu_readmap[1] = cart.rom + 0x2000;
cpu_readmap[2] = cart.rom + 0x4000;
cpu_readmap[3] = cart.rom + 0x6000;
cpu_readmap[4] = cart.rom + 0x0000;
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.fcr[0] = 0x00;
sms.fcr[1] = 0x00;
sms.fcr[2] = 0x01;
sms.fcr[3] = 0x00;
}
/* Reset Z80 emulator */
void cpu_reset(void)
{
z80_reset(0);
z80_set_irq_callback(sms_irq_callback);
}
/* Write to memory */
void cpu_writemem16(int address, int data)
{
cpu_writemap[(address >> 13)][(address & 0x1FFF)] = data;
if(address >= 0xFFFC) sms_mapper_w(address & 3, data);
}
/* Write to an I/O port */
void cpu_writeport(int port, int data)
{
switch(port & 0xFF)
{
case 0x01: /* GG SIO */
case 0x02:
case 0x03:
case 0x04:
case 0x05:
break;
case 0x06: /* GG STEREO */
/*if(snd.log) {
snd.callback(0x04);
snd.callback(data);
}*/
sms.psg_mask = (data & 0xFF);
break;
//case 0x7E: /* SN76489 PSG */
//case 0x7F:
/*if(snd.log) {
snd.callback(0x03);
snd.callback(data);
}
if(snd.enabled) SN76496Write(0, data);
break;*/
case 0xBE: /* VDP DATA */
vdp_data_w(data);
break;
case 0xBD: /* VDP CTRL */
case 0xBF:
vdp_ctrl_w(data);
break;
case 0xF0: /* YM2413 */
/*case 0xF1:
if(snd.log) {
snd.callback((port & 1) ? 0x06 : 0x05);
snd.callback(data);
}
if(snd.enabled && sms.use_fm) ym2413_write(0, port & 1, data);
break;*/
case 0xF2: /* YM2413 DETECT */
if(sms.use_fm) sms.port_F2 = (data & 1);
break;
case 0x3F: /* TERRITORY CTRL. */
sms.port_3F = ((data & 0x80) | (data & 0x20) << 1) & 0xC0;
if(sms.country == TYPE_DOMESTIC) sms.port_3F ^= 0xC0;
break;
}
}
/* Read from an I/O port */
int cpu_readport(int port)
{
uint8 temp = 0xFF;
switch(port & 0xFF)
{
case 0x01: /* GG SIO */
case 0x02:
case 0x03:
case 0x04:
case 0x05:
return (0x00);
case 0x7E: /* V COUNTER */
return (vdp_vcounter_r());
break;
case 0x7F: /* H COUNTER */
return (vdp_hcounter_r());
break;
case 0x00: /* INPUT #2 */
temp = 0xFF;
if(input.system & INPUT_START) temp &= ~0x80;
if(sms.country == TYPE_DOMESTIC) temp &= ~0x40;
return (temp);
case 0xC0: /* INPUT #0 */
case 0xDC:
temp = 0xFF;
if(input.pad[0] & INPUT_UP) temp &= ~0x01;
if(input.pad[0] & INPUT_DOWN) temp &= ~0x02;
if(input.pad[0] & INPUT_LEFT) temp &= ~0x04;
if(input.pad[0] & INPUT_RIGHT) temp &= ~0x08;
if(input.pad[0] & INPUT_BUTTON2) temp &= ~0x10;
if(input.pad[0] & INPUT_BUTTON1) temp &= ~0x20;
if(input.pad[1] & INPUT_UP) temp &= ~0x40;
if(input.pad[1] & INPUT_DOWN) temp &= ~0x80;
return (temp);
case 0xC1: /* INPUT #1 */
case 0xDD:
temp = 0xFF;
if(input.pad[1] & INPUT_LEFT) temp &= ~0x01;
if(input.pad[1] & INPUT_RIGHT) temp &= ~0x02;
if(input.pad[1] & INPUT_BUTTON2) temp &= ~0x04;
if(input.pad[1] & INPUT_BUTTON1) temp &= ~0x08;
if(input.system & INPUT_SOFT_RESET) temp &= ~0x10;
return ((temp & 0x3F) | (sms.port_3F & 0xC0));
case 0xBE: /* VDP DATA */
return (vdp_data_r());
case 0xBD:
case 0xBF: /* VDP CTRL */
return (vdp_ctrl_r());
case 0xF2: /* YM2413 DETECT */
if(sms.use_fm) return (sms.port_F2);
break;
}
return (0xFF);
}
void sms_mapper_w(int address, int data)
{
/* Calculate ROM page index */
uint8 page = (data % cart.pages);
/* Save frame control register data */
sms.fcr[address] = data;
switch(address)
{
case 0:
if(data & 8)
{
sms.save = 1;
/* Page in ROM */
cpu_readmap[4] = &sms.sram[(data & 4) ? 0x4000 : 0x0000];
cpu_readmap[5] = &sms.sram[(data & 4) ? 0x6000 : 0x2000];
cpu_writemap[4] = &sms.sram[(data & 4) ? 0x4000 : 0x0000];
cpu_writemap[5] = &sms.sram[(data & 4) ? 0x6000 : 0x2000];
}
else
{
/* Page in RAM */
cpu_readmap[4] = &cart.rom[((sms.fcr[3] % cart.pages) << 14) + 0x0000];
cpu_readmap[5] = &cart.rom[((sms.fcr[3] % cart.pages) << 14) + 0x2000];
cpu_writemap[4] = sms.dummy;
cpu_writemap[5] = sms.dummy;
}
break;
case 1:
cpu_readmap[0] = &cart.rom[(page << 14) + 0x0000];
cpu_readmap[1] = &cart.rom[(page << 14) + 0x2000];
break;
case 2:
cpu_readmap[2] = &cart.rom[(page << 14) + 0x0000];
cpu_readmap[3] = &cart.rom[(page << 14) + 0x2000];
break;
case 3:
if(!(sms.fcr[0] & 0x08))
{
cpu_readmap[4] = &cart.rom[(page << 14) + 0x0000];
cpu_readmap[5] = &cart.rom[(page << 14) + 0x2000];
}
break;
}
}
int sms_irq_callback(int param)
{
return (0xFF);
}