sms: do psg like md does
[picodrive.git] / pico / sms.c
CommitLineData
cff531af 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 */
87b0845f 8/*
9 * TODO:
10 * - start in a state as if BIOS ran
87b0845f 11 * - RAM support in mapper
12 * - region support
87b0845f 13 * - H counter
14 */
3e49ffd0 15#include "pico_int.h"
af37bca8 16#include "memory.h"
2ec9bec5 17#include "sound/sn76496.h"
3e49ffd0 18
2ec9bec5 19static unsigned char vdp_data_read(void)
3e49ffd0 20{
2ec9bec5 21 struct PicoVideo *pv = &Pico.video;
22 unsigned char d;
23
88fd63ad 24 d = PicoMem.vramb[pv->addr];
2ec9bec5 25 pv->addr = (pv->addr + 1) & 0x3fff;
26 pv->pending = 0;
27 return d;
28}
29
30static unsigned char vdp_ctl_read(void)
31{
1c25c32c 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;
2ec9bec5 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) {
88fd63ad 48 PicoMem.cram[pv->addr & 0x1f] = d;
200772b7 49 Pico.m.dirtyPal = 1;
2ec9bec5 50 } else {
88fd63ad 51 PicoMem.vramb[pv->addr] = d;
2ec9bec5 52 }
53 pv->addr = (pv->addr + 1) & 0x3fff;
2ec9bec5 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;
200772b7 65 elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
2ec9bec5 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;
3e49ffd0 75}
76
2ec9bec5 77static unsigned char z80_sms_in(unsigned short a)
3e49ffd0 78{
2ec9bec5 79 unsigned char d = 0;
80
200772b7 81 elprintf(EL_IO, "z80 port %04x read", a);
2ec9bec5 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;
87b0845f 92 elprintf(EL_HVCNT, "V counter read: %02x", d);
2ec9bec5 93 break;
94
95 case 0x41: /* H counter */
96 d = Pico.m.rotate++;
87b0845f 97 elprintf(EL_HVCNT, "H counter read: %02x", d);
2ec9bec5 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 */
93f9619e 109 d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
2ec9bec5 110 break;
111
112 case 0xc1: /* I/O port B and miscellaneous */
b4db550e 113 d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
93f9619e 114 d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
2ec9bec5 115 break;
116 }
117
200772b7 118 elprintf(EL_IO, "ret = %02x", d);
2ec9bec5 119 return d;
120}
121
122static void z80_sms_out(unsigned short a, unsigned char d)
123{
200772b7 124 elprintf(EL_IO, "z80 port %04x write %02x", a, d);
2ec9bec5 125 a &= 0xc1;
126 switch (a)
127 {
128 case 0x01:
b4db550e 129 Pico.ms.io_ctl = d;
2ec9bec5 130 break;
131
132 case 0x40:
133 case 0x41:
075672bf 134 if ((d & 0x90) == 0x90 && PsndPsgLine < Pico.m.scanline)
135 PsndDoPSG(Pico.m.scanline);
136 SN76496Write(d);
2ec9bec5 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
87b0845f 149static int bank_mask;
150
2ec9bec5 151static void write_bank(unsigned short a, unsigned char d)
152{
460603fa 153 elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
2ec9bec5 154 switch (a & 0x0f)
155 {
156 case 0x0c:
87b0845f 157 elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
2ec9bec5 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:
87b0845f 164 d &= bank_mask;
2ec9bec5 165 z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
166#ifdef _USE_CZ80
b8a1c09a 167 Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
2ec9bec5 168#endif
169 break;
170 case 0x0f:
87b0845f 171 d &= bank_mask;
2ec9bec5 172 z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
173#ifdef _USE_CZ80
b8a1c09a 174 Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
2ec9bec5 175#endif
176 break;
177 }
b4db550e 178 Pico.ms.carthw[a & 0x0f] = d;
3e49ffd0 179}
180
553c3eaa 181static void xwrite(unsigned int a, unsigned char d)
2ec9bec5 182{
200772b7 183 elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
2ec9bec5 184 if (a >= 0xc000)
88fd63ad 185 PicoMem.zram[a & 0x1fff] = d;
b4db550e 186 if (a >= 0xfff8)
2ec9bec5 187 write_bank(a, d);
3e49ffd0 188}
189
2ec9bec5 190void PicoResetMS(void)
3e49ffd0 191{
2ec9bec5 192 z80_reset();
193 PsndReset(); // pal must be known here
3e49ffd0 194}
195
196void PicoPowerMS(void)
197{
87b0845f 198 int s, tmp;
199
88fd63ad 200 memset(&PicoMem,0,sizeof(PicoMem));
2ec9bec5 201 memset(&Pico.video,0,sizeof(Pico.video));
202 memset(&Pico.m,0,sizeof(Pico.m));
203 Pico.m.pal = 0;
204
87b0845f 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
b4db550e 215 Pico.ms.carthw[0x0e] = 1;
216 Pico.ms.carthw[0x0f] = 2;
217
2ec9bec5 218 PicoReset();
3e49ffd0 219}
220
221void PicoMemSetupMS(void)
222{
223 z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
88fd63ad 224 z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
225 z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
3e49ffd0 226
2ec9bec5 227 z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
88fd63ad 228 z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
3e49ffd0 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
b8a1c09a 236 Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
88fd63ad 237 Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
238 Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
3e49ffd0 239 Cz80_Set_INPort(&CZ80, z80_sms_in);
240 Cz80_Set_OUTPort(&CZ80, z80_sms_out);
241#endif
242}
243
b4db550e 244void PicoStateLoadedMS(void)
245{
246 write_bank(0xfffe, Pico.ms.carthw[0x0e]);
247 write_bank(0xffff, Pico.ms.carthw[0x0f]);
248}
249
3e49ffd0 250void PicoFrameMS(void)
251{
2ec9bec5 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;
93f9619e 257 int skip = PicoIn.skipFrame;
2ec9bec5 258 int lines_vis = 192;
87b0845f 259 int hint; // Hint counter
835122bc 260 int nmi;
2ec9bec5 261 int y;
262
4f2cdbf5 263 PsndStartFrame();
264
93f9619e 265 nmi = (PicoIn.pad[0] >> 7) & 1;
835122bc 266 if (!Pico.ms.nmi_state && nmi)
267 z80_nmi();
268 Pico.ms.nmi_state = nmi;
269
200772b7 270 PicoFrameStartMode4();
87b0845f 271 hint = pv->reg[0x0a];
200772b7 272
2ec9bec5 273 for (y = 0; y < lines; y++)
274 {
275 pv->v_counter = Pico.m.scanline = y;
03065bb6 276 if (y > 218)
277 pv->v_counter = y - 6;
2ec9bec5 278
19954be1 279 if (y < lines_vis && !skip)
200772b7 280 PicoLineMode4(y);
87b0845f 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 }
200772b7 294 else if (y == lines_vis + 1) {
87b0845f 295 pv->pending_ints |= 1;
296 if (pv->reg[1] & 0x20) {
2ec9bec5 297 elprintf(EL_INTS, "vint");
298 z80_int();
299 }
300 }
301
075672bf 302 // 224 because of how it's done for MD...
303 if (y == 224 && PsndOut)
304 PsndGetSamplesMS();
305
2ec9bec5 306 cycles_aim += cycles_line;
307 cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
308 }
309
075672bf 310 if (PsndOut && PsndPsgLine < lines)
311 PsndDoPSG(lines - 1);
3e49ffd0 312}
313
87b0845f 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
075672bf 325// vim:ts=2:sw=2:expandtab