5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
10 * - start in a state as if BIOS ran
11 * - RAM support in mapper
17 #include "sound/sn76496.h"
19 static unsigned char vdp_data_read(void)
21 struct PicoVideo *pv = &Pico.video;
24 d = PicoMem.vramb[pv->addr];
25 pv->addr = (pv->addr + 1) & 0x3fff;
30 static unsigned char vdp_ctl_read(void)
32 struct PicoVideo *pv = &Pico.video;
36 d = pv->status | (pv->pending_ints << 7);
37 pv->pending = pv->pending_ints = 0;
40 elprintf(EL_SR, "VDP sr: %02x", d);
44 static void vdp_data_write(unsigned char d)
46 struct PicoVideo *pv = &Pico.video;
49 PicoMem.cram[pv->addr & 0x1f] = d;
52 PicoMem.vramb[pv->addr] = d;
54 pv->addr = (pv->addr + 1) & 0x3fff;
59 static NOINLINE void vdp_reg_write(struct PicoVideo *pv, u8 a, u8 d)
66 l = pv->pending_ints & (d >> 3) & 2;
67 elprintf(EL_INTS, "hint %d", l);
71 l = pv->pending_ints & (d >> 5) & 1;
72 elprintf(EL_INTS, "vint %d", l);
78 static void vdp_ctl_write(u8 d)
80 struct PicoVideo *pv = &Pico.video;
84 elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
85 if (pv->reg[d & 0x0f] != (u8)pv->addr)
86 vdp_reg_write(pv, d & 0x0f, pv->addr);
90 pv->addr |= (d & 0x3f) << 8;
98 static unsigned char z80_sms_in(unsigned short a)
102 elprintf(EL_IO, "z80 port %04x read", a);
111 case 0x40: /* V counter */
112 d = Pico.video.v_counter;
113 elprintf(EL_HVCNT, "V counter read: %02x", d);
116 case 0x41: /* H counter */
118 elprintf(EL_HVCNT, "H counter read: %02x", d);
129 case 0xc0: /* I/O port A and B */
130 d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
133 case 0xc1: /* I/O port B and miscellaneous */
134 d = (Pico.ms.io_ctl & 0x80) | ((Pico.ms.io_ctl << 1) & 0x40) | 0x30;
135 d |= ~(PicoIn.pad[1] >> 2) & 0x0f;
139 elprintf(EL_IO, "ret = %02x", d);
143 static void z80_sms_out(unsigned short a, unsigned char d)
145 elprintf(EL_IO, "z80 port %04x write %02x", a, d);
155 if ((d & 0x90) == 0x90 && Pico.snd.psg_line < Pico.m.scanline)
156 PsndDoPSG(Pico.m.scanline);
170 static int bank_mask;
172 static void write_bank(unsigned short a, unsigned char d)
174 elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
178 elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
182 elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d);
186 z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
188 Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
193 z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
195 Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
199 Pico.ms.carthw[a & 0x0f] = d;
202 static void xwrite(unsigned int a, unsigned char d)
204 elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
206 PicoMem.zram[a & 0x1fff] = d;
211 void PicoResetMS(void)
214 PsndReset(); // pal must be known here
217 void PicoPowerMS(void)
221 memset(&PicoMem,0,sizeof(PicoMem));
222 memset(&Pico.video,0,sizeof(Pico.video));
223 memset(&Pico.m,0,sizeof(Pico.m));
226 // calculate a mask for bank writes.
227 // ROM loader has aligned the size for us, so this is safe.
228 s = 0; tmp = Pico.romsize;
229 while ((tmp >>= 1) != 0)
231 if (Pico.romsize > (1 << s))
234 bank_mask = (tmp - 1) >> 14;
236 Pico.ms.carthw[0x0e] = 1;
237 Pico.ms.carthw[0x0f] = 2;
242 void PicoMemSetupMS(void)
244 z80_map_set(z80_read_map, 0x0000, 0xbfff, Pico.rom, 0);
245 z80_map_set(z80_read_map, 0xc000, 0xdfff, PicoMem.zram, 0);
246 z80_map_set(z80_read_map, 0xe000, 0xffff, PicoMem.zram, 0);
248 z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1);
249 z80_map_set(z80_write_map, 0xc000, 0xdfff, PicoMem.zram, 0);
250 z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1);
253 drZ80.z80_in = z80_sms_in;
254 drZ80.z80_out = z80_sms_out;
257 Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
258 Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
259 Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
260 Cz80_Set_INPort(&CZ80, z80_sms_in);
261 Cz80_Set_OUTPort(&CZ80, z80_sms_out);
265 void PicoStateLoadedMS(void)
267 write_bank(0xfffe, Pico.ms.carthw[0x0e]);
268 write_bank(0xffff, Pico.ms.carthw[0x0f]);
271 void PicoFrameMS(void)
273 struct PicoVideo *pv = &Pico.video;
274 int is_pal = Pico.m.pal;
275 int lines = is_pal ? 313 : 262;
276 int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */
277 int cycles_done = 0, cycles_aim = 0;
278 int skip = PicoIn.skipFrame;
280 int hint; // Hint counter
286 nmi = (PicoIn.pad[0] >> 7) & 1;
287 if (!Pico.ms.nmi_state && nmi)
289 Pico.ms.nmi_state = nmi;
291 PicoFrameStartMode4();
292 hint = pv->reg[0x0a];
294 for (y = 0; y < lines; y++)
296 pv->v_counter = Pico.m.scanline = y;
298 pv->v_counter = y - 6;
300 if (y < lines_vis && !skip)
307 hint = pv->reg[0x0a];
308 pv->pending_ints |= 2;
309 if (pv->reg[0] & 0x10) {
310 elprintf(EL_INTS, "hint");
315 else if (y == lines_vis + 1) {
316 pv->pending_ints |= 1;
317 if (pv->reg[1] & 0x20) {
318 elprintf(EL_INTS, "vint");
323 // 224 because of how it's done for MD...
324 if (y == 224 && PicoIn.sndOut)
327 cycles_aim += cycles_line;
328 cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
331 if (PicoIn.sndOut && Pico.snd.psg_line < lines)
332 PsndDoPSG(lines - 1);
335 void PicoFrameDrawOnlyMS(void)
340 PicoFrameStartMode4();
342 for (y = 0; y < lines_vis; y++)
346 // vim:ts=2:sw=2:expandtab