sms: do psg like md does
[picodrive.git] / pico / sms.c
... / ...
CommitLineData
1/*
2 * SMS emulation
3 * (C) notaz, 2009-2010
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
8/*
9 * TODO:
10 * - start in a state as if BIOS ran
11 * - RAM support in mapper
12 * - region support
13 * - H counter
14 */
15#include "pico_int.h"
16#include "memory.h"
17#include "sound/sn76496.h"
18
19static unsigned char vdp_data_read(void)
20{
21 struct PicoVideo *pv = &Pico.video;
22 unsigned char d;
23
24 d = PicoMem.vramb[pv->addr];
25 pv->addr = (pv->addr + 1) & 0x3fff;
26 pv->pending = 0;
27 return d;
28}
29
30static unsigned char vdp_ctl_read(void)
31{
32 struct PicoVideo *pv = &Pico.video;
33 unsigned char d;
34
35 d = pv->status | (pv->pending_ints << 7);
36 pv->pending = pv->pending_ints = 0;
37 pv->status = 0;
38
39 elprintf(EL_SR, "VDP sr: %02x", d);
40 return d;
41}
42
43static void vdp_data_write(unsigned char d)
44{
45 struct PicoVideo *pv = &Pico.video;
46
47 if (pv->type == 3) {
48 PicoMem.cram[pv->addr & 0x1f] = d;
49 Pico.m.dirtyPal = 1;
50 } else {
51 PicoMem.vramb[pv->addr] = d;
52 }
53 pv->addr = (pv->addr + 1) & 0x3fff;
54
55 pv->pending = 0;
56}
57
58static void vdp_ctl_write(unsigned char d)
59{
60 struct PicoVideo *pv = &Pico.video;
61
62 if (pv->pending) {
63 if ((d >> 6) == 2) {
64 pv->reg[d & 0x0f] = pv->addr;
65 elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
66 }
67 pv->type = d >> 6;
68 pv->addr &= 0x00ff;
69 pv->addr |= (d & 0x3f) << 8;
70 } else {
71 pv->addr &= 0x3f00;
72 pv->addr |= d;
73 }
74 pv->pending ^= 1;
75}
76
77static unsigned char z80_sms_in(unsigned short a)
78{
79 unsigned char d = 0;
80
81 elprintf(EL_IO, "z80 port %04x read", a);
82 a &= 0xc1;
83 switch (a)
84 {
85 case 0x00:
86 case 0x01:
87 d = 0xff;
88 break;
89
90 case 0x40: /* V counter */
91 d = Pico.video.v_counter;
92 elprintf(EL_HVCNT, "V counter read: %02x", d);
93 break;
94
95 case 0x41: /* H counter */
96 d = Pico.m.rotate++;
97 elprintf(EL_HVCNT, "H counter read: %02x", d);
98 break;
99
100 case 0x80:
101 d = vdp_data_read();
102 break;
103
104 case 0x81:
105 d = vdp_ctl_read();
106 break;
107
108 case 0xc0: /* I/O port A and B */
109 d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
110 break;
111
112 case 0xc1: /* I/O port B and miscellaneous */
113 d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
114 d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
115 break;
116 }
117
118 elprintf(EL_IO, "ret = %02x", d);
119 return d;
120}
121
122static void z80_sms_out(unsigned short a, unsigned char d)
123{
124 elprintf(EL_IO, "z80 port %04x write %02x", a, d);
125 a &= 0xc1;
126 switch (a)
127 {
128 case 0x01:
129 Pico.ms.io_ctl = d;
130 break;
131
132 case 0x40:
133 case 0x41:
134 if ((d & 0x90) == 0x90 && PsndPsgLine < Pico.m.scanline)
135 PsndDoPSG(Pico.m.scanline);
136 SN76496Write(d);
137 break;
138
139 case 0x80:
140 vdp_data_write(d);
141 break;
142
143 case 0x81:
144 vdp_ctl_write(d);
145 break;
146 }
147}
148
149static int bank_mask;
150
151static void write_bank(unsigned short a, unsigned char d)
152{
153 elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
154 switch (a & 0x0f)
155 {
156 case 0x0c:
157 elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
158 break;
159 case 0x0d:
160 if (d != 0)
161 elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d);
162 break;
163 case 0x0e:
164 d &= bank_mask;
165 z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
166#ifdef _USE_CZ80
167 Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
168#endif
169 break;
170 case 0x0f:
171 d &= bank_mask;
172 z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
173#ifdef _USE_CZ80
174 Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
175#endif
176 break;
177 }
178 Pico.ms.carthw[a & 0x0f] = d;
179}
180
181static void xwrite(unsigned int a, unsigned char d)
182{
183 elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
184 if (a >= 0xc000)
185 PicoMem.zram[a & 0x1fff] = d;
186 if (a >= 0xfff8)
187 write_bank(a, d);
188}
189
190void PicoResetMS(void)
191{
192 z80_reset();
193 PsndReset(); // pal must be known here
194}
195
196void PicoPowerMS(void)
197{
198 int s, tmp;
199
200 memset(&PicoMem,0,sizeof(PicoMem));
201 memset(&Pico.video,0,sizeof(Pico.video));
202 memset(&Pico.m,0,sizeof(Pico.m));
203 Pico.m.pal = 0;
204
205 // calculate a mask for bank writes.
206 // ROM loader has aligned the size for us, so this is safe.
207 s = 0; tmp = Pico.romsize;
208 while ((tmp >>= 1) != 0)
209 s++;
210 if (Pico.romsize > (1 << s))
211 s++;
212 tmp = 1 << s;
213 bank_mask = (tmp - 1) >> 14;
214
215 Pico.ms.carthw[0x0e] = 1;
216 Pico.ms.carthw[0x0f] = 2;
217
218 PicoReset();
219}
220
221void PicoMemSetupMS(void)
222{
223 z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
224 z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
225 z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
226
227 z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
228 z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
229 z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
230
231#ifdef _USE_DRZ80
232 drZ80.z80_in = z80_sms_in;
233 drZ80.z80_out = z80_sms_out;
234#endif
235#ifdef _USE_CZ80
236 Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
237 Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
238 Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
239 Cz80_Set_INPort(&CZ80, z80_sms_in);
240 Cz80_Set_OUTPort(&CZ80, z80_sms_out);
241#endif
242}
243
244void PicoStateLoadedMS(void)
245{
246 write_bank(0xfffe, Pico.ms.carthw[0x0e]);
247 write_bank(0xffff, Pico.ms.carthw[0x0f]);
248}
249
250void PicoFrameMS(void)
251{
252 struct PicoVideo *pv = &Pico.video;
253 int is_pal = Pico.m.pal;
254 int lines = is_pal ? 313 : 262;
255 int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
256 int cycles_done = 0, cycles_aim = 0;
257 int skip = PicoIn.skipFrame;
258 int lines_vis = 192;
259 int hint; // Hint counter
260 int nmi;
261 int y;
262
263 PsndStartFrame();
264
265 nmi = (PicoIn.pad[0] >> 7) & 1;
266 if (!Pico.ms.nmi_state && nmi)
267 z80_nmi();
268 Pico.ms.nmi_state = nmi;
269
270 PicoFrameStartMode4();
271 hint = pv->reg[0x0a];
272
273 for (y = 0; y < lines; y++)
274 {
275 pv->v_counter = Pico.m.scanline = y;
276 if (y > 218)
277 pv->v_counter = y - 6;
278
279 if (y < lines_vis && !skip)
280 PicoLineMode4(y);
281
282 if (y <= lines_vis)
283 {
284 if (--hint < 0)
285 {
286 hint = pv->reg[0x0a];
287 pv->pending_ints |= 2;
288 if (pv->reg[0] & 0x10) {
289 elprintf(EL_INTS, "hint");
290 z80_int();
291 }
292 }
293 }
294 else if (y == lines_vis + 1) {
295 pv->pending_ints |= 1;
296 if (pv->reg[1] & 0x20) {
297 elprintf(EL_INTS, "vint");
298 z80_int();
299 }
300 }
301
302 // 224 because of how it's done for MD...
303 if (y == 224 && PsndOut)
304 PsndGetSamplesMS();
305
306 cycles_aim += cycles_line;
307 cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
308 }
309
310 if (PsndOut && PsndPsgLine < lines)
311 PsndDoPSG(lines - 1);
312}
313
314void PicoFrameDrawOnlyMS(void)
315{
316 int lines_vis = 192;
317 int y;
318
319 PicoFrameStartMode4();
320
321 for (y = 0; y < lines_vis; y++)
322 PicoLineMode4(y);
323}
324
325// vim:ts=2:sw=2:expandtab