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;
35 d = pv->status | (pv->pending_ints << 7);
36 pv->pending = pv->pending_ints = 0;
39 elprintf(EL_SR, "VDP sr: %02x", d);
43 static void vdp_data_write(unsigned char d)
45 struct PicoVideo *pv = &Pico.video;
48 PicoMem.cram[pv->addr & 0x1f] = d;
51 PicoMem.vramb[pv->addr] = d;
53 pv->addr = (pv->addr + 1) & 0x3fff;
58 static void vdp_ctl_write(unsigned char d)
60 struct PicoVideo *pv = &Pico.video;
64 pv->reg[d & 0x0f] = pv->addr;
65 elprintf(EL_IO, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff);
69 pv->addr |= (d & 0x3f) << 8;
77 static unsigned char z80_sms_in(unsigned short a)
81 elprintf(EL_IO, "z80 port %04x read", a);
90 case 0x40: /* V counter */
91 d = Pico.video.v_counter;
92 elprintf(EL_HVCNT, "V counter read: %02x", d);
95 case 0x41: /* H counter */
97 elprintf(EL_HVCNT, "H counter read: %02x", d);
108 case 0xc0: /* I/O port A and B */
109 d = ~((PicoIn.pad[0] & 0x3f) | (PicoIn.pad[1] << 6));
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;
118 elprintf(EL_IO, "ret = %02x", d);
122 static void z80_sms_out(unsigned short a, unsigned char d)
124 elprintf(EL_IO, "z80 port %04x write %02x", a, d);
134 if ((d & 0x90) == 0x90 && PsndPsgLine < Pico.m.scanline)
135 PsndDoPSG(Pico.m.scanline);
149 static int bank_mask;
151 static void write_bank(unsigned short a, unsigned char d)
153 elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
157 elprintf(EL_STATUS|EL_ANOMALY, "%02x written to control reg!", d);
161 elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d);
165 z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
167 Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
172 z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
174 Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
178 Pico.ms.carthw[a & 0x0f] = d;
181 static void xwrite(unsigned int a, unsigned char d)
183 elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
185 PicoMem.zram[a & 0x1fff] = d;
190 void PicoResetMS(void)
193 PsndReset(); // pal must be known here
196 void PicoPowerMS(void)
200 memset(&PicoMem,0,sizeof(PicoMem));
201 memset(&Pico.video,0,sizeof(Pico.video));
202 memset(&Pico.m,0,sizeof(Pico.m));
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)
210 if (Pico.romsize > (1 << s))
213 bank_mask = (tmp - 1) >> 14;
215 Pico.ms.carthw[0x0e] = 1;
216 Pico.ms.carthw[0x0f] = 2;
221 void PicoMemSetupMS(void)
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);
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);
232 drZ80.z80_in = z80_sms_in;
233 drZ80.z80_out = z80_sms_out;
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);
244 void PicoStateLoadedMS(void)
246 write_bank(0xfffe, Pico.ms.carthw[0x0e]);
247 write_bank(0xffff, Pico.ms.carthw[0x0f]);
250 void PicoFrameMS(void)
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;
259 int hint; // Hint counter
265 nmi = (PicoIn.pad[0] >> 7) & 1;
266 if (!Pico.ms.nmi_state && nmi)
268 Pico.ms.nmi_state = nmi;
270 PicoFrameStartMode4();
271 hint = pv->reg[0x0a];
273 for (y = 0; y < lines; y++)
275 pv->v_counter = Pico.m.scanline = y;
277 pv->v_counter = y - 6;
279 if (y < lines_vis && !skip)
286 hint = pv->reg[0x0a];
287 pv->pending_ints |= 2;
288 if (pv->reg[0] & 0x10) {
289 elprintf(EL_INTS, "hint");
294 else if (y == lines_vis + 1) {
295 pv->pending_ints |= 1;
296 if (pv->reg[1] & 0x20) {
297 elprintf(EL_INTS, "vint");
302 // 224 because of how it's done for MD...
303 if (y == 224 && PsndOut)
306 cycles_aim += cycles_line;
307 cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8;
310 if (PsndOut && PsndPsgLine < lines)
311 PsndDoPSG(lines - 1);
314 void PicoFrameDrawOnlyMS(void)
319 PicoFrameStartMode4();
321 for (y = 0; y < lines_vis; y++)
325 // vim:ts=2:sw=2:expandtab