From 2ec9bec58b89831cdead4cdec8f593c5b99d2715 Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 23 Aug 2009 22:01:55 +0000 Subject: [PATCH] sms wip: Alex kidd shinobi plays sound git-svn-id: file:///home/notaz/opt/svn/PicoDrive@759 be3aeb3a-fb24-0410-a615-afba39da0efa --- pico/pico.c | 18 ++-- pico/pico_int.h | 10 ++- pico/sms.c | 204 ++++++++++++++++++++++++++++++++++++++++++--- pico/sound/sound.c | 27 ++++++ 4 files changed, 241 insertions(+), 18 deletions(-) diff --git a/pico/pico.c b/pico/pico.c index f900dfe..00184e6 100644 --- a/pico/pico.c +++ b/pico/pico.c @@ -141,14 +141,23 @@ int PicoReset(void) { unsigned char sram_reg=Pico.m.sram_reg; // must be preserved - if (Pico.romsize<=0) return 1; + if (Pico.romsize <= 0) + return 1; /* must call now, so that banking is reset, and correct vectors get fetched */ - if (PicoResetHook) PicoResetHook(); + if (PicoResetHook) + PicoResetHook(); PicoMemReset(); - SekReset(); memset(&PicoPadInt,0,sizeof(PicoPadInt)); + emustatus = 0; + + if (PicoAHW & PAHW_SMS) { + PicoResetMS(); + return 0; + } + + SekReset(); // s68k doesn't have the TAS quirk, so we just globally set normal TAS handler in MCD mode (used by Batman games). SekSetRealTAS(PicoAHW & PAHW_MCD); SekCycleCntT=0; @@ -156,7 +165,6 @@ int PicoReset(void) if (PicoAHW & PAHW_MCD) // needed for MCD to reset properly, probably some bug hides behind this.. memset(Pico.ioports,0,sizeof(Pico.ioports)); - emustatus = 0; Pico.m.dirtyPal = 1; @@ -169,7 +177,7 @@ int PicoReset(void) PsndReset(); // pal must be known here // create an empty "dma" to cause 68k exec start at random frame location - if (Pico.m.dma_xfers == 0 && !(PicoOpt&POPT_DIS_VDP_FIFO)) + if (Pico.m.dma_xfers == 0 && !(PicoOpt & POPT_DIS_VDP_FIFO)) Pico.m.dma_xfers = rand() & 0x1fff; SekFinishIdleDet(); diff --git a/pico/pico_int.h b/pico/pico_int.h index 45955ad..9841b18 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -272,10 +272,14 @@ struct PicoMisc struct Pico { unsigned char ram[0x10000]; // 0x00000 scratch ram - unsigned short vram[0x8000]; // 0x10000 + union { + unsigned short vram[0x8000]; // 0x10000 + unsigned char vramb[0x4000]; // VRAM in SMS mode + }; unsigned char zram[0x2000]; // 0x20000 Z80 ram unsigned char ioports[0x10]; - unsigned int pad[0x3c]; // unused + unsigned char sms_io_ctl; + unsigned char pad[0xef]; // unused unsigned short cram[0x40]; // 0x22100 unsigned short vsram[0x40]; // 0x22180 @@ -549,10 +553,12 @@ PICO_INTERNAL void PsndReset(void); PICO_INTERNAL void PsndDoDAC(int line_to); PICO_INTERNAL void PsndClear(void); PICO_INTERNAL void PsndGetSamples(int y); +PICO_INTERNAL void PsndGetSamplesMS(void); extern int PsndDacLine; // sms.c void PicoPowerMS(void); +void PicoResetMS(void); void PicoMemSetupMS(void); void PicoFrameMS(void); diff --git a/pico/sms.c b/pico/sms.c index a7a0f8f..5416845 100644 --- a/pico/sms.c +++ b/pico/sms.c @@ -1,29 +1,185 @@ #include "pico_int.h" +#include "sound/sn76496.h" -static unsigned char z80_sms_in(unsigned short p) +static unsigned char vdp_data_read(void) { - elprintf(EL_ANOMALY, "Z80 port %04x read", p); - return 0xff; + struct PicoVideo *pv = &Pico.video; + unsigned char d; + + d = Pico.vramb[pv->addr]; + pv->addr = (pv->addr + 1) & 0x3fff; + pv->pending = 0; + return d; +} + +static unsigned char vdp_ctl_read(void) +{ + unsigned char d = Pico.video.pending_ints << 7; + Pico.video.pending = 0; + Pico.video.pending_ints = 0; + + elprintf(EL_SR, "VDP sr: %02x", d); + return d; +} + +static void vdp_data_write(unsigned char d) +{ + struct PicoVideo *pv = &Pico.video; + + if (pv->type == 3) { + Pico.cram[pv->addr & 0x1f] = d; + } else { + Pico.vramb[pv->addr] = d; + } + pv->addr = (pv->addr + 1) & 0x3fff; + elprintf(EL_ANOMALY, " addr=%04x", pv->addr); + + pv->pending = 0; +} + +static void vdp_ctl_write(unsigned char d) +{ + struct PicoVideo *pv = &Pico.video; + + if (pv->pending) { + if ((d >> 6) == 2) { + pv->reg[d & 0x0f] = pv->addr; + elprintf(EL_ANOMALY, " VDP r%02x=%02x", d & 0x0f, pv->addr & 0xff); + } + pv->type = d >> 6; + pv->addr &= 0x00ff; + pv->addr |= (d & 0x3f) << 8; + } else { + pv->addr &= 0x3f00; + pv->addr |= d; + } + pv->pending ^= 1; } -static void z80_sms_out(unsigned short p, unsigned char d) +static unsigned char z80_sms_in(unsigned short a) { - elprintf(EL_ANOMALY, "Z80 port %04x write %02x", p, d); + unsigned char d = 0; + + elprintf(EL_ANOMALY, "z80 port %04x read", a); + a &= 0xc1; + switch (a) + { + case 0x00: + case 0x01: + d = 0xff; + break; + + case 0x40: /* V counter */ + d = Pico.video.v_counter; + break; + + case 0x41: /* H counter */ + d = Pico.m.rotate++; + break; + + case 0x80: + d = vdp_data_read(); + break; + + case 0x81: + d = vdp_ctl_read(); + break; + + case 0xc0: /* I/O port A and B */ + d = ~((PicoPad[0] & 0x3f) | (PicoPad[1] << 6)); + break; + + case 0xc1: /* I/O port B and miscellaneous */ + d = (Pico.sms_io_ctl & 0x80) | ((Pico.sms_io_ctl << 1) & 0x40) | 0x30; + d |= ~(PicoPad[1] >> 2) & 0x0f; + break; + } + + elprintf(EL_ANOMALY, "ret = %02x", d); + return d; +} + +static void z80_sms_out(unsigned short a, unsigned char d) +{ + elprintf(EL_ANOMALY, "z80 port %04x write %02x", a, d); + a &= 0xc1; + switch (a) + { + case 0x01: + Pico.sms_io_ctl = d; + break; + + case 0x40: + case 0x41: + if (PicoOpt & POPT_EN_PSG) + SN76496Write(d); + break; + + case 0x80: + vdp_data_write(d); + break; + + case 0x81: + vdp_ctl_write(d); + break; + } +} + +static void write_bank(unsigned short a, unsigned char d) +{ + d &= 0x3f; // XXX + switch (a & 0x0f) + { + case 0x0c: + break; + case 0x0d: + if (d != 0) + elprintf(EL_STATUS|EL_ANOMALY, "bank0 changed to %d!", d); + break; + case 0x0e: + z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0); +#ifdef _USE_CZ80 + Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (UINT32)Pico.rom + (d << 14)); +#endif + break; + case 0x0f: + z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0); +#ifdef _USE_CZ80 + Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (UINT32)Pico.rom + (d << 14)); +#endif + break; + } } static unsigned char MEMH_FUNC xread(unsigned short a) { - elprintf(EL_ANOMALY, "Z80 read [%04x]", a); - return 0; + elprintf(EL_ANOMALY, "z80 read [%04x]", a); + return Pico.zram[a & 0x1fff]; +} + +static void MEMH_FUNC xwrite(unsigned int a, unsigned char d) +{ + elprintf(EL_ANOMALY, "z80 write [%04x] %02x", a, d); + if (a >= 0xc000) + Pico.zram[a & 0x1fff] = d; + if (a >= 0xfff0) + write_bank(a, d); } -static void MEMH_FUNC xwrite(unsigned int a, unsigned char data) +void PicoResetMS(void) { - elprintf(EL_ANOMALY, "Z80 write [%04x] %02x", a, data); + z80_reset(); + PsndReset(); // pal must be known here } void PicoPowerMS(void) { + memset(&Pico.ram,0,(unsigned int)&Pico.rom-(unsigned int)&Pico.ram); + memset(&Pico.video,0,sizeof(Pico.video)); + memset(&Pico.m,0,sizeof(Pico.m)); + Pico.m.pal = 0; + + PicoReset(); } void PicoMemSetupMS(void) @@ -32,7 +188,7 @@ void PicoMemSetupMS(void) z80_map_set(z80_read_map, 0xc000, 0xdfff, Pico.zram, 0); z80_map_set(z80_read_map, 0xe000, 0xffff, xread, 1); - z80_map_set(z80_write_map, 0x0000, 0xbfff, Pico.rom, 0); + z80_map_set(z80_write_map, 0x0000, 0xbfff, xwrite, 1); z80_map_set(z80_write_map, 0xc000, 0xdfff, Pico.zram, 0); z80_map_set(z80_write_map, 0xe000, 0xffff, xwrite, 1); @@ -43,6 +199,7 @@ void PicoMemSetupMS(void) #ifdef _USE_CZ80 Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (UINT32)Pico.rom); Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (UINT32)Pico.zram); + Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (UINT32)Pico.zram); Cz80_Set_INPort(&CZ80, z80_sms_in); Cz80_Set_OUTPort(&CZ80, z80_sms_out); #endif @@ -50,6 +207,31 @@ void PicoMemSetupMS(void) void PicoFrameMS(void) { - z80_run(OSC_NTSC / 13 / 60); + struct PicoVideo *pv = &Pico.video; + int is_pal = Pico.m.pal; + int lines = is_pal ? 313 : 262; + int cycles_line = is_pal ? 58020 : 58293; /* (226.6 : 227.7) * 256 */ + int cycles_done = 0, cycles_aim = 0; + int lines_vis = 192; + int y; + + for (y = 0; y < lines; y++) + { + pv->v_counter = Pico.m.scanline = y; + + if (y == lines_vis + 1) { + Pico.video.pending_ints |= 1; + if (Pico.video.reg[1] & 0x20) { + elprintf(EL_INTS, "vint"); + z80_int(); + } + } + + cycles_aim += cycles_line; + cycles_done += z80_run((cycles_aim - cycles_done) >> 8) << 8; + } + + PsndGetSamplesMS(); + elprintf(EL_ANOMALY, "frame end"); } diff --git a/pico/sound/sound.c b/pico/sound/sound.c index e1e0408..7f3fd8d 100644 --- a/pico/sound/sound.c +++ b/pico/sound/sound.c @@ -396,3 +396,30 @@ PICO_INTERNAL void PsndGetSamples(int y) #endif } +PICO_INTERNAL void PsndGetSamplesMS(void) +{ + int *buf32 = PsndBuffer; + int stereo = (PicoOpt & 8) >> 3; + int length = PsndLen; + +#if !SIMPLE_WRITE_SOUND + // compensate for float part of PsndLen + PsndLen_exc_cnt += PsndLen_exc_add; + if (PsndLen_exc_cnt >= 0x10000) { + PsndLen_exc_cnt -= 0x10000; + length++; + } +#endif + + // PSG + if (PicoOpt & POPT_EN_PSG) + SN76496Update(PsndOut, length, stereo); + + // convert + limit to normal 16bit output + PsndMix_32_to_16l(PsndOut, buf32, length); + + if (PicoWriteSound != NULL) + PicoWriteSound(length); + PsndClear(); +} + -- 2.39.2