X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2Fcd%2FMemory.c;h=8053185f1c2cd0569d7df6108023b9078bb14fd1;hb=8022f53da61b8e70420a3bac97250119bbe26457;hp=fdbf3166f62363d91f5a8ee9944de609f91afb1a;hpb=7a1f6e45c9f6a11152f62303b0203475a9fd431a;p=picodrive.git diff --git a/Pico/cd/Memory.c b/Pico/cd/Memory.c index fdbf316..8053185 100644 --- a/Pico/cd/Memory.c +++ b/Pico/cd/Memory.c @@ -1,27 +1,24 @@ -// This is part of Pico Library +// Memory I/O handlers for Sega/Mega CD. +// Loosely based on Gens code. +// (c) Copyright 2007, Grazvydas "notaz" Ignotas -// (c) Copyright 2004 Dave, All rights reserved. -// (c) Copyright 2007 notaz, All rights reserved. -// Free for non-commercial use. - -// For commercial use, separate licencing terms must be obtained. - -// A68K no longer supported here //#define __debug_io #include "../PicoInt.h" -#include "../sound/sound.h" #include "../sound/ym2612.h" #include "../sound/sn76496.h" #include "gfx_cd.h" #include "pcm.h" +#ifndef UTYPES_DEFINED typedef unsigned char u8; typedef unsigned short u16; typedef unsigned int u32; +#define UTYPES_DEFINED +#endif //#define __debug_io //#define __debug_io2 @@ -30,11 +27,18 @@ typedef unsigned int u32; #define rdprintf(...) //#define wrdprintf dprintf #define wrdprintf(...) +#define plprintf dprintf +//#define plprintf(...) + +#ifdef EMU_CORE_DEBUG +extern u32 lastread_a, lastread_d[16], lastwrite_cyc_d[16]; +extern int lrp_cyc, lwp_cyc; +#undef USE_POLL_DETECT +#endif // ----------------------------------------------------------------- // poller detection -#define USE_POLL_DETECT #define POLL_LIMIT 16 #define POLL_CYCLES 124 // int m68k_poll_addr, m68k_poll_cnt; @@ -53,7 +57,9 @@ static u32 m68k_reg_read16(u32 a) goto end; case 2: d = (Pico_mcd->s68k_regs[a]<<8) | (Pico_mcd->s68k_regs[a+1]&0xc7); - dprintf("m68k_regs r3: %02x @%06x", (u8)d, SekPcS68k); + // the DMNA delay must only be visible on s68k side (Lunar2, Silpheed) + if (Pico_mcd->m.state_flags&2) { d &= ~1; d |= 2; } + //printf("m68k_regs r3: %02x @%06x\n", (u8)d, SekPc); goto end; case 4: d = Pico_mcd->s68k_regs[4]<<8; @@ -114,11 +120,12 @@ void m68k_reg_write8(u32 a, u32 d) Pico_mcd->m.busreq = d; return; case 2: + dprintf("m68k: prg wp=%02x", d); Pico_mcd->s68k_regs[2] = d; // really use s68k side register return; case 3: { u32 dold = Pico_mcd->s68k_regs[3]&0x1f; - dprintf("m68k_regs w3: %02x @%06x", (u8)d, SekPc); + //printf("m68k_regs w3: %02x @%06x\n", (u8)d, SekPc); d &= 0xc2; if ((dold>>6) != ((d>>6)&3)) dprintf("m68k: prg bank: %i -> %i", (Pico_mcd->s68k_regs[a]>>6), ((d>>6)&3)); @@ -129,13 +136,20 @@ void m68k_reg_write8(u32 a, u32 d) d ^= 2; // writing 0 to DMNA actually sets it, 1 does nothing } else { //dold &= ~2; // ?? +#if 1 + if ((d & 2) && !(dold & 2)) { + Pico_mcd->m.state_flags |= 2; // we must delay setting DMNA bit (needed for Silpheed) + d &= ~2; + } +#else if (d & 2) dold &= ~1; // return word RAM to s68k in 2M mode +#endif } Pico_mcd->s68k_regs[3] = d | dold; // really use s68k side register #ifdef USE_POLL_DETECT if ((s68k_poll_adclk&0xfe) == 2 && s68k_poll_cnt > POLL_LIMIT) { SekSetStopS68k(0); s68k_poll_adclk = 0; - //printf("%05i:%03i: s68k poll release, a=%02x\n", Pico.m.frame_count, Pico.m.scanline, a); + plprintf("s68k poll release, a=%02x\n", a); } #endif return; @@ -155,7 +169,7 @@ void m68k_reg_write8(u32 a, u32 d) #ifdef USE_POLL_DETECT if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) { SekSetStopS68k(0); s68k_poll_adclk = 0; - //printf("%05i:%03i: s68k poll release, a=%02x\n", Pico.m.frame_count, Pico.m.scanline, a); + plprintf("s68k poll release, a=%02x\n", a); } #endif return; @@ -166,7 +180,7 @@ void m68k_reg_write8(u32 a, u32 d) #ifdef USE_POLL_DETECT if ((a&0xfe) == (s68k_poll_adclk&0xfe) && s68k_poll_cnt > POLL_LIMIT) { SekSetStopS68k(0); s68k_poll_adclk = 0; - //printf("%05i:%03i: s68k poll release, a=%02x\n", Pico.m.frame_count, Pico.m.scanline, a); + plprintf("s68k poll release, a=%02x\n", a); } #endif return; @@ -175,6 +189,31 @@ void m68k_reg_write8(u32 a, u32 d) dprintf("m68k FIXME: invalid write? [%02x] %02x", a, d); } +#ifndef _ASM_CD_MEMORY_C +static +#endif +u32 s68k_poll_detect(u32 a, u32 d) +{ +#ifdef USE_POLL_DETECT + // polling detection + if (a == (s68k_poll_adclk&0xff)) { + unsigned int clkdiff = SekCyclesDoneS68k() - (s68k_poll_adclk>>8); + if (clkdiff <= POLL_CYCLES) { + s68k_poll_cnt++; + //printf("-- diff: %u, cnt = %i\n", clkdiff, s68k_poll_cnt); + if (s68k_poll_cnt > POLL_LIMIT) { + SekSetStopS68k(1); + plprintf("s68k poll detected @ %06x, a=%02x\n", SekPcS68k, a); + } + s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a; + return d; + } + } + s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a; + s68k_poll_cnt = 0; +#endif + return d; +} #define READ_FONT_DATA(basemask) \ { \ @@ -200,9 +239,9 @@ u32 s68k_reg_read16(u32 a) case 0: return ((Pico_mcd->s68k_regs[0]&3)<<8) | 1; // ver = 0, not in reset state case 2: - d = (Pico_mcd->s68k_regs[a]<<8) | (Pico_mcd->s68k_regs[a+1]&0x1f); - dprintf("s68k_regs r3: %02x @%06x", (u8)d, SekPcS68k); - goto poll_detect; + d = (Pico_mcd->s68k_regs[2]<<8) | (Pico_mcd->s68k_regs[3]&0x1f); + //printf("s68k_regs r3: %02x @%06x\n", (u8)d, SekPcS68k); + return s68k_poll_detect(a, d); case 6: return CDC_Read_Reg(); case 8: @@ -232,30 +271,9 @@ u32 s68k_reg_read16(u32 a) d = (Pico_mcd->s68k_regs[a]<<8) | Pico_mcd->s68k_regs[a+1]; - if (a >= 0x0e && a < 0x30) goto poll_detect; - - return d; - -poll_detect: -#ifdef USE_POLL_DETECT - // polling detection - if (a == (s68k_poll_adclk&0xfe)) { - unsigned int clkdiff = SekCyclesDoneS68k() - (s68k_poll_adclk>>8); - if (clkdiff <= POLL_CYCLES) { - s68k_poll_cnt++; - //printf("-- diff: %u, cnt = %i\n", clkdiff, s68k_poll_cnt); - if (s68k_poll_cnt > POLL_LIMIT) { - SekSetStopS68k(1); - //printf("%05i:%03i: s68k poll detected @ %06x, a=%02x\n", Pico.m.frame_count, Pico.m.scanline, SekPcS68k, a); - } - s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a; - return d; - } - } - s68k_poll_adclk = (SekCyclesDoneS68k() << 8) | a; - s68k_poll_cnt = 0; + if (a >= 0x0e && a < 0x30) + return s68k_poll_detect(a, d); -#endif return d; } @@ -266,22 +284,19 @@ void s68k_reg_write8(u32 a, u32 d) { //dprintf("s68k_regs w%2i: [%02x] %02x @ %06x", realsize, a, d, SekPcS68k); - // TODO: review against Gens // Warning: d might have upper bits set switch (a) { case 2: return; // only m68k can change WP case 3: { int dold = Pico_mcd->s68k_regs[3]; - dprintf("s68k_regs w3: %02x @%06x", (u8)d, SekPc); + //printf("s68k_regs w3: %02x @%06x\n", (u8)d, SekPcS68k); d &= 0x1d; d |= dold&0xc2; if (d&4) { if ((d ^ dold) & 5) { d &= ~2; // in case of mode or bank change we clear DMNA (m68k req) bit -#ifdef _ASM_CD_MEMORY_C PicoMemResetCD(d); -#endif } #ifdef _ASM_CD_MEMORY_C if ((d ^ dold) & 0x1d) @@ -299,9 +314,7 @@ void s68k_reg_write8(u32 a, u32 d) d |= (dold&1) ? 2 : 1; // then give it to the one which had bank0 in 1M mode } wram_1M_to_2M(Pico_mcd->word_ram2M); -#ifdef _ASM_CD_MEMORY_C PicoMemResetCD(d); -#endif } else d |= dold&1; @@ -379,6 +392,24 @@ static u32 OtherRead16End(u32 a, int realsize) goto end; } + if (a==0x400000) { + if (SRam.data != NULL) d=3; // 64k cart + goto end; + } + + if ((a&0xfe0000)==0x600000) { + if (SRam.data != NULL) { + d=SRam.data[((a>>1)&0xffff)+0x2000]; + if (realsize == 8) d|=d<<8; + } + goto end; + } + + if (a==0x7ffffe) { + d=Pico_mcd->m.bcram_reg; + goto end; + } + dprintf("m68k FIXME: unusual r%i: %06x @%06x", realsize&~1, (a&0xfffffe)+(realsize&1), SekPc); end: @@ -390,23 +421,36 @@ static void OtherWrite8End(u32 a, u32 d, int realsize) { if ((a&0xffffc0)==0xa12000) { m68k_reg_write8(a, d); return; } - dprintf("m68k FIXME: strange w%i: %06x, %08x @%06x", realsize, a&0xffffff, d, SekPc); -} + if ((a&0xfe0000)==0x600000) { + if (SRam.data != NULL && (Pico_mcd->m.bcram_reg&1)) { + SRam.data[((a>>1)&0xffff)+0x2000]=d; + SRam.changed = 1; + } + return; + } + if (a==0x7fffff) { + Pico_mcd->m.bcram_reg=d; + return; + } + + dprintf("m68k FIXME: strange w%i: [%06x], %08x @%06x", realsize, a&0xffffff, d, SekPc); +} +#define _CD_MEMORY_C #undef _ASM_MEMORY_C #include "../MemoryCmn.c" #include "cell_map.c" #endif // !def _ASM_CD_MEMORY_C + // ----------------------------------------------------------------- // Read Rom and read Ram -//u8 PicoReadM68k8_(u32 a); #ifdef _ASM_CD_MEMORY_C -u8 PicoReadM68k8(u32 a); +u32 PicoReadM68k8(u32 a); #else -static u8 PicoReadM68k8(u32 a) +static u32 PicoReadM68k8(u32 a) { u32 d=0; @@ -417,7 +461,7 @@ static u8 PicoReadM68k8(u32 a) if (a < 0x20000) { d = *(u8 *)(Pico_mcd->bios+(a^1)); goto end; } // bios // prg RAM - if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&2)) { + if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) { u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6]; d = *(prg_bank+((a^1)&0x1ffff)); goto end; @@ -455,17 +499,23 @@ static u8 PicoReadM68k8(u32 a) #ifdef __debug_io dprintf("r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPc); #endif - return (u8)d; +#ifdef EMU_CORE_DEBUG + if (a>=Pico.romsize) { + lastread_a = a; + lastread_d[lrp_cyc++&15] = d; + } +#endif + return d; } #endif #ifdef _ASM_CD_MEMORY_C -u16 PicoReadM68k16(u32 a); +u32 PicoReadM68k16(u32 a); #else -static u16 PicoReadM68k16(u32 a) +static u32 PicoReadM68k16(u32 a) { - u16 d=0; + u32 d=0; if ((a&0xe00000)==0xe00000) { d=*(u16 *)(Pico.ram+(a&0xfffe)); goto end; } // Ram @@ -474,9 +524,11 @@ static u16 PicoReadM68k16(u32 a) if (a < 0x20000) { d = *(u16 *)(Pico_mcd->bios+a); goto end; } // bios // prg RAM - if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&2)) { + if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) { u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6]; + wrdprintf("m68k_prgram r16: [%i,%06x] @%06x", Pico_mcd->s68k_regs[3]>>6, a, SekPc); d = *(u16 *)(prg_bank+(a&0x1fffe)); + wrdprintf("ret = %04x", d); goto end; } @@ -500,7 +552,7 @@ static u16 PicoReadM68k16(u32 a) if ((a&0xffffc0)==0xa12000) rdprintf("m68k_regs r16: [%02x] @%06x", a&0x3f, SekPc); - d = (u16)OtherRead16(a, 16); + d = OtherRead16(a, 16); if ((a&0xffffc0)==0xa12000) rdprintf("ret = %04x", d); @@ -509,6 +561,12 @@ static u16 PicoReadM68k16(u32 a) #ifdef __debug_io dprintf("r16: %06x, %04x @%06x", a&0xffffff, d, SekPc); +#endif +#ifdef EMU_CORE_DEBUG + if (a>=Pico.romsize) { + lastread_a = a; + lastread_d[lrp_cyc++&15] = d; + } #endif return d; } @@ -529,7 +587,7 @@ static u32 PicoReadM68k32(u32 a) if (a < 0x20000) { u16 *pm=(u16 *)(Pico_mcd->bios+a); d = (pm[0]<<16)|pm[1]; goto end; } // bios // prg RAM - if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&2)) { + if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) { u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6]; u16 *pm=(u16 *)(prg_bank+(a&0x1fffe)); d = (pm[0]<<16)|pm[1]; @@ -570,6 +628,12 @@ static u32 PicoReadM68k32(u32 a) end: #ifdef __debug_io dprintf("r32: %06x, %08x @%06x", a&0xffffff, d, SekPc); +#endif +#ifdef EMU_CORE_DEBUG + if (a>=Pico.romsize) { + lastread_a = a; + lastread_d[lrp_cyc++&15] = d; + } #endif return d; } @@ -577,7 +641,6 @@ static u32 PicoReadM68k32(u32 a) // ----------------------------------------------------------------- -// Write Ram #ifdef _ASM_CD_MEMORY_C void PicoWriteM68k8(u32 a,u8 d); @@ -587,9 +650,9 @@ static void PicoWriteM68k8(u32 a,u8 d) #ifdef __debug_io dprintf("w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc); #endif - //if ((a&0xe0ffff)==0xe0a9ba+0x69c) - // dprintf("w8 : %06x, %02x @%06x", a&0xffffff, d, SekPc); - +#ifdef EMU_CORE_DEBUG + lastwrite_cyc_d[lwp_cyc++&15] = d; +#endif if ((a&0xe00000)==0xe00000) { // Ram *(u8 *)(Pico.ram+((a^1)&0xffff)) = d; @@ -599,7 +662,7 @@ static void PicoWriteM68k8(u32 a,u8 d) a&=0xffffff; // prg RAM - if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&2)) { + if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) { u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6]; *(u8 *)(prg_bank+((a^1)&0x1ffff))=d; return; @@ -621,10 +684,13 @@ static void PicoWriteM68k8(u32 a,u8 d) return; } - if ((a&0xffffc0)==0xa12000) + if ((a&0xffffc0)==0xa12000) { rdprintf("m68k_regs w8: [%02x] %02x @%06x", a&0x3f, d, SekPc); + m68k_reg_write8(a, d); + return; + } - OtherWrite8(a,d,8); + OtherWrite8(a,d); } #endif @@ -637,7 +703,9 @@ static void PicoWriteM68k16(u32 a,u16 d) #ifdef __debug_io dprintf("w16: %06x, %04x", a&0xffffff, d); #endif - // dprintf("w16: %06x, %04x @%06x", a&0xffffff, d, SekPc); +#ifdef EMU_CORE_DEBUG + lastwrite_cyc_d[lwp_cyc++&15] = d; +#endif if ((a&0xe00000)==0xe00000) { // Ram *(u16 *)(Pico.ram+(a&0xfffe))=d; @@ -647,8 +715,9 @@ static void PicoWriteM68k16(u32 a,u16 d) a&=0xfffffe; // prg RAM - if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&2)) { + if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) { u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6]; + wrdprintf("m68k_prgram w16: [%i,%06x] %04x @%06x", Pico_mcd->s68k_regs[3]>>6, a, d, SekPc); *(u16 *)(prg_bank+(a&0x1fffe))=d; return; } @@ -676,8 +745,8 @@ static void PicoWriteM68k16(u32 a,u16 d) Pico_mcd->s68k_regs[0xe] = d >> 8; #ifdef USE_POLL_DETECT if ((s68k_poll_adclk&0xfe) == 0xe && s68k_poll_cnt > POLL_LIMIT) { - SekSetStopS68k(0); s68k_poll_adclk = -1; - //printf("%05i:%03i: s68k poll release, a=%02x\n", Pico.m.frame_count, Pico.m.scanline, a); + SekSetStopS68k(0); s68k_poll_adclk = 0; + plprintf("s68k poll release, a=%02x\n", a); } #endif return; @@ -700,6 +769,9 @@ static void PicoWriteM68k32(u32 a,u32 d) #ifdef __debug_io dprintf("w32: %06x, %08x", a&0xffffff, d); #endif +#ifdef EMU_CORE_DEBUG + lastwrite_cyc_d[lwp_cyc++&15] = d; +#endif if ((a&0xe00000)==0xe00000) { @@ -712,7 +784,7 @@ static void PicoWriteM68k32(u32 a,u32 d) a&=0xfffffe; // prg RAM - if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&2)) { + if ((a&0xfe0000)==0x020000 && (Pico_mcd->m.busreq&3)!=1) { u8 *prg_bank = Pico_mcd->prg_ram_b[Pico_mcd->s68k_regs[3]>>6]; u16 *pm=(u16 *)(prg_bank+(a&0x1fffe)); pm[0]=(u16)(d>>16); pm[1]=(u16)d; @@ -744,8 +816,10 @@ static void PicoWriteM68k32(u32 a,u32 d) return; } - if ((a&0xffffc0)==0xa12000) + if ((a&0xffffc0)==0xa12000) { rdprintf("m68k_regs w32: [%02x] %08x @%06x", a&0x3f, d, SekPc); + if ((a&0x3e) == 0xe) dprintf("m68k FIXME: w32 [%02x]", a&0x3f); + } OtherWrite16(a, (u16)(d>>16)); OtherWrite16(a+2,(u16)d); @@ -753,15 +827,20 @@ static void PicoWriteM68k32(u32 a,u32 d) #endif +// ----------------------------------------------------------------- +// S68k // ----------------------------------------------------------------- #ifdef _ASM_CD_MEMORY_C -u8 PicoReadS68k8(u32 a); +u32 PicoReadS68k8(u32 a); #else -static u8 PicoReadS68k8(u32 a) +static u32 PicoReadS68k8(u32 a) { u32 d=0; +#ifdef EMU_CORE_DEBUG + u32 ab=a&0xfffffe; +#endif a&=0xffffff; // prg RAM @@ -774,7 +853,13 @@ static u8 PicoReadS68k8(u32 a) if ((a&0xfffe00) == 0xff8000) { a &= 0x1ff; rdprintf("s68k_regs r8: [%02x] @ %06x", a, SekPcS68k); - if (a >= 0x58 && a < 0x68) + if (a >= 0x0e && a < 0x30) { + d = Pico_mcd->s68k_regs[a]; + s68k_poll_detect(a, d); + rdprintf("ret = %02x", (u8)d); + goto end; + } + else if (a >= 0x58 && a < 0x68) d = gfx_cd_read(a&~1); else d = s68k_reg_read16(a&~1); if ((a&1)==0) d>>=8; @@ -787,7 +872,7 @@ static u8 PicoReadS68k8(u32 a) // test: batman returns wrdprintf("s68k_wram2M r8: [%06x] @%06x", a, SekPcS68k); if (Pico_mcd->s68k_regs[3]&4) { // 1M decode mode? - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; d = Pico_mcd->word_ram1M[bank][((a>>1)^1)&0x1ffff]; if (a&1) d &= 0x0f; else d >>= 4; @@ -806,7 +891,7 @@ static u8 PicoReadS68k8(u32 a) wrdprintf("s68k_wram1M r8: [%06x] @%06x", a, SekPcS68k); // if (!(Pico_mcd->s68k_regs[3]&4)) // dprintf("s68k_wram1M FIXME: wrong mode"); - bank = !(Pico_mcd->s68k_regs[3]&1); + bank = (Pico_mcd->s68k_regs[3]&1)^1; d = Pico_mcd->word_ram1M[bank][(a^1)&0x1ffff]; wrdprintf("ret = %02x", (u8)d); goto end; @@ -840,24 +925,32 @@ static u8 PicoReadS68k8(u32 a) #ifdef __debug_io2 dprintf("s68k r8 : %06x, %02x @%06x", a&0xffffff, (u8)d, SekPcS68k); #endif - return (u8)d; +#ifdef EMU_CORE_DEBUG + lastread_a = ab; + lastread_d[lrp_cyc++&15] = d; +#endif + return d; } #endif -//u16 PicoReadS68k16_(u32 a); #ifdef _ASM_CD_MEMORY_C -u16 PicoReadS68k16(u32 a); +u32 PicoReadS68k16(u32 a); #else -static u16 PicoReadS68k16(u32 a) +static u32 PicoReadS68k16(u32 a) { u32 d=0; +#ifdef EMU_CORE_DEBUG + u32 ab=a&0xfffffe; +#endif a&=0xfffffe; // prg RAM if (a < 0x80000) { + wrdprintf("s68k_prgram r16: [%06x] @%06x", a, SekPcS68k); d = *(u16 *)(Pico_mcd->prg_ram+a); + wrdprintf("ret = %04x", d); goto end; } @@ -876,7 +969,7 @@ static u16 PicoReadS68k16(u32 a) if ((a&0xfc0000)==0x080000) { // 080000-0bffff wrdprintf("s68k_wram2M r16: [%06x] @%06x", a, SekPcS68k); if (Pico_mcd->s68k_regs[3]&4) { // 1M decode mode? - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; d = Pico_mcd->word_ram1M[bank][((a>>1)^1)&0x1ffff]; d |= d << 4; d &= ~0xf0; dprintf("FIXME: decode"); @@ -894,7 +987,7 @@ static u16 PicoReadS68k16(u32 a) wrdprintf("s68k_wram1M r16: [%06x] @%06x", a, SekPcS68k); // if (!(Pico_mcd->s68k_regs[3]&4)) // dprintf("s68k_wram1M FIXME: wrong mode"); - bank = !(Pico_mcd->s68k_regs[3]&1); + bank = (Pico_mcd->s68k_regs[3]&1)^1; d = *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe)); wrdprintf("ret = %04x", d); goto end; @@ -931,6 +1024,10 @@ static u16 PicoReadS68k16(u32 a) #ifdef __debug_io2 dprintf("s68k r16: %06x, %04x @%06x", a&0xffffff, d, SekPcS68k); +#endif +#ifdef EMU_CORE_DEBUG + lastread_a = ab; + lastread_d[lrp_cyc++&15] = d; #endif return d; } @@ -944,6 +1041,9 @@ static u32 PicoReadS68k32(u32 a) { u32 d=0; +#ifdef EMU_CORE_DEBUG + u32 ab=a&0xfffffe; +#endif a&=0xfffffe; // prg RAM @@ -968,12 +1068,11 @@ static u32 PicoReadS68k32(u32 a) if ((a&0xfc0000)==0x080000) { // 080000-0bffff wrdprintf("s68k_wram2M r32: [%06x] @%06x", a, SekPcS68k); if (Pico_mcd->s68k_regs[3]&4) { // 1M decode mode? - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; a >>= 1; d = Pico_mcd->word_ram1M[bank][((a+0)^1)&0x1ffff] << 16; d |= Pico_mcd->word_ram1M[bank][((a+1)^1)&0x1ffff]; d |= d << 4; d &= 0x0f0f0f0f; - dprintf("FIXME: decode"); } else { // allow access in any mode, like Gens does u16 *pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe)); d = (pm[0]<<16)|pm[1]; @@ -988,7 +1087,7 @@ static u32 PicoReadS68k32(u32 a) wrdprintf("s68k_wram1M r32: [%06x] @%06x", a, SekPcS68k); // if (!(Pico_mcd->s68k_regs[3]&4)) // dprintf("s68k_wram1M FIXME: wrong mode"); - bank = !(Pico_mcd->s68k_regs[3]&1); + bank = (Pico_mcd->s68k_regs[3]&1)^1; u16 *pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe)); d = (pm[0]<<16)|pm[1]; wrdprintf("ret = %08x", d); goto end; @@ -996,7 +1095,7 @@ static u32 PicoReadS68k32(u32 a) // PCM if ((a&0xff8000)==0xff0000) { - dprintf("FIXME: s68k_pcm r32: [%06x] @%06x", a, SekPcS68k); + dprintf("s68k_pcm r32: [%06x] @%06x", a, SekPcS68k); a &= 0x7fff; if (a >= 0x2000) { a >>= 1; @@ -1035,6 +1134,12 @@ static u32 PicoReadS68k32(u32 a) #ifdef __debug_io2 dprintf("s68k r32: %06x, %08x @%06x", a&0xffffff, d, SekPcS68k); +#endif +#ifdef EMU_CORE_DEBUG + if (ab > 0x78) { // not vectors and stuff + lastread_a = ab; + lastread_d[lrp_cyc++&15] = d; + } #endif return d; } @@ -1045,15 +1150,13 @@ static u32 PicoReadS68k32(u32 a) /* check: jaguar xj 220 (draws entire world using decode) */ static void decode_write8(u32 a, u8 d, int r3) { - u8 *pd = Pico_mcd->word_ram1M[!(r3 & 1)] + (((a>>1)^1)&0x1ffff); + u8 *pd = Pico_mcd->word_ram1M[(r3 & 1)^1] + (((a>>1)^1)&0x1ffff); u8 oldmask = (a&1) ? 0xf0 : 0x0f; r3 &= 0x18; d &= 0x0f; if (!(a&1)) d <<= 4; - //dprintf("FIXME: decode, r3 = %02x", r3); - if (r3 == 8) { if ((!(*pd & (~oldmask))) && d) goto do_it; } else if (r3 > 8) { @@ -1070,7 +1173,7 @@ do_it: static void decode_write16(u32 a, u16 d, int r3) { - u8 *pd = Pico_mcd->word_ram1M[!(r3 & 1)] + (((a>>1)^1)&0x1ffff); + u8 *pd = Pico_mcd->word_ram1M[(r3 & 1)^1] + (((a>>1)^1)&0x1ffff); //if ((a & 0x3ffff) < 0x28000) return; @@ -1091,15 +1194,11 @@ static void decode_write16(u32 a, u16 d, int r3) } else { *pd = d; } - - //dprintf("FIXME: decode"); } #endif // ----------------------------------------------------------------- -//void PicoWriteS68k8_(u32 a,u8 d); -//void PicoWriteS68k8__(u32 a,u8 d); #ifdef _ASM_CD_MEMORY_C void PicoWriteS68k8(u32 a,u8 d); #else @@ -1110,21 +1209,15 @@ static void PicoWriteS68k8(u32 a,u8 d) #endif a&=0xffffff; -#if 0 - PicoWriteS68k8_(a, d); -/* if ((a&0xfc0000)!=0x080000) { - PicoWriteS68k8_(a, d); - return; - } - printf("r3: %02x\n", Pico_mcd->s68k_regs[3]); - PicoWriteS68k8__(a,d);*/ - return; + +#ifdef EMU_CORE_DEBUG + lastwrite_cyc_d[lwp_cyc++&15] = d; #endif // prg RAM if (a < 0x80000) { u8 *pm=(u8 *)(Pico_mcd->prg_ram+(a^1)); - *pm=d; + if (a >= (Pico_mcd->s68k_regs[2]<<8)) *pm=d; return; } @@ -1159,7 +1252,7 @@ static void PicoWriteS68k8(u32 a,u8 d) wrdprintf("s68k_wram1M w8: [%06x] %02x @%06x", a, d, SekPcS68k); // if (!(Pico_mcd->s68k_regs[3]&4)) // dprintf("s68k_wram1M FIXME: wrong mode"); - bank = !(Pico_mcd->s68k_regs[3]&1); + bank = (Pico_mcd->s68k_regs[3]&1)^1; *(u8 *)(Pico_mcd->word_ram1M[bank]+((a^1)&0x1ffff))=d; return; } @@ -1197,9 +1290,15 @@ static void PicoWriteS68k16(u32 a,u16 d) a&=0xfffffe; +#ifdef EMU_CORE_DEBUG + lastwrite_cyc_d[lwp_cyc++&15] = d; +#endif + // prg RAM if (a < 0x80000) { - *(u16 *)(Pico_mcd->prg_ram+a)=d; + wrdprintf("s68k_prgram w16: [%06x] %04x @%06x", a, d, SekPcS68k); + if (a >= (Pico_mcd->s68k_regs[2]<<8)) // needed for Dungeon Explorer + *(u16 *)(Pico_mcd->prg_ram+a)=d; return; } @@ -1240,7 +1339,7 @@ static void PicoWriteS68k16(u32 a,u16 d) wrdprintf("s68k_wram1M w16: [%06x] %04x @%06x", a, d, SekPcS68k); // if (!(Pico_mcd->s68k_regs[3]&4)) // dprintf("s68k_wram1M FIXME: wrong mode"); - bank = !(Pico_mcd->s68k_regs[3]&1); + bank = (Pico_mcd->s68k_regs[3]&1)^1; *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe))=d; return; } @@ -1281,10 +1380,16 @@ static void PicoWriteS68k32(u32 a,u32 d) a&=0xfffffe; +#ifdef EMU_CORE_DEBUG + lastwrite_cyc_d[lwp_cyc++&15] = d; +#endif + // prg RAM if (a < 0x80000) { - u16 *pm=(u16 *)(Pico_mcd->prg_ram+a); - pm[0]=(u16)(d>>16); pm[1]=(u16)d; + if (a >= (Pico_mcd->s68k_regs[2]<<8)) { + u16 *pm=(u16 *)(Pico_mcd->prg_ram+a); + pm[0]=(u16)(d>>16); pm[1]=(u16)d; + } return; } @@ -1296,6 +1401,7 @@ static void PicoWriteS68k32(u32 a,u32 d) gfx_cd_write16(a, d>>16); gfx_cd_write16(a+2, d&0xffff); } else { + if ((a&0x1fe) == 0xe) dprintf("s68k FIXME: w32 [%02x]", a&0x3f); s68k_reg_write8(a, d>>24); s68k_reg_write8(a+1,(d>>16)&0xff); s68k_reg_write8(a+2,(d>>8) &0xff); @@ -1327,7 +1433,7 @@ static void PicoWriteS68k32(u32 a,u32 d) wrdprintf("s68k_wram1M w32: [%06x] %08x @%06x", a, d, SekPcS68k); // if (!(Pico_mcd->s68k_regs[3]&4)) // dprintf("s68k_wram1M FIXME: wrong mode"); - bank = !(Pico_mcd->s68k_regs[3]&1); + bank = (Pico_mcd->s68k_regs[3]&1)^1; pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe)); pm[0]=(u16)(d>>16); pm[1]=(u16)d; return; @@ -1368,7 +1474,7 @@ static void PicoWriteS68k32(u32 a,u32 d) // ----------------------------------------------------------------- -#if defined(EMU_C68K) +#ifdef EMU_C68K static __inline int PicoMemBaseM68k(u32 pc) { if ((pc&0xe00000)==0xe00000) @@ -1382,7 +1488,7 @@ static __inline int PicoMemBaseM68k(u32 pc) if (!(Pico_mcd->s68k_regs[3]&4)) return (int)Pico_mcd->word_ram2M - 0x200000; // Program Counter in Word Ram if (pc < 0x220000) { - int bank = (Pico_mcd->s68k_regs[3]&1); + int bank = Pico_mcd->s68k_regs[3]&1; return (int)Pico_mcd->word_ram1M[bank] - 0x200000; } } @@ -1396,12 +1502,12 @@ static __inline int PicoMemBaseM68k(u32 pc) static u32 PicoCheckPcM68k(u32 pc) { - pc-=PicoCpu.membase; // Get real pc + pc-=PicoCpuCM68k.membase; // Get real pc pc&=0xfffffe; - PicoCpu.membase=PicoMemBaseM68k(pc); + PicoCpuCM68k.membase=PicoMemBaseM68k(pc); - return PicoCpu.membase+pc; + return PicoCpuCM68k.membase+pc; } @@ -1414,7 +1520,7 @@ static __inline int PicoMemBaseS68k(u32 pc) return (int)Pico_mcd->word_ram2M - 0x080000; if ((pc&0xfe0000)==0x0c0000) { // word RAM 1M area - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; return (int)Pico_mcd->word_ram1M[bank] - 0x0c0000; } @@ -1427,37 +1533,101 @@ static __inline int PicoMemBaseS68k(u32 pc) static u32 PicoCheckPcS68k(u32 pc) { - pc-=PicoCpuS68k.membase; // Get real pc + pc-=PicoCpuCS68k.membase; // Get real pc pc&=0xfffffe; - PicoCpuS68k.membase=PicoMemBaseS68k(pc); + PicoCpuCS68k.membase=PicoMemBaseS68k(pc); - return PicoCpuS68k.membase+pc; + return PicoCpuCS68k.membase+pc; } #endif +#ifndef _ASM_CD_MEMORY_C +void PicoMemResetCD(int r3) +{ +#ifdef EMU_F68K + // update fetchmap.. + int i; + if (!(r3 & 4)) + { + for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x240000; i++) + PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x200000; + } + else + { + for (i = M68K_FETCHBANK1*2/16; (i<<(24-FAMEC_FETCHBITS)) < 0x220000; i++) + PicoCpuFM68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[r3 & 1] - 0x200000; + for (i = M68K_FETCHBANK1*0x0c/0x100; (i<<(24-FAMEC_FETCHBITS)) < 0x0e0000; i++) + PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram1M[(r3&1)^1] - 0x0c0000; + } +#endif +} +#endif -void PicoMemSetupCD() +PICO_INTERNAL void PicoMemSetupCD(void) { dprintf("PicoMemSetupCD()"); #ifdef EMU_C68K // Setup m68k memory callbacks: - PicoCpu.checkpc=PicoCheckPcM68k; - PicoCpu.fetch8 =PicoCpu.read8 =PicoReadM68k8; - PicoCpu.fetch16=PicoCpu.read16=PicoReadM68k16; - PicoCpu.fetch32=PicoCpu.read32=PicoReadM68k32; - PicoCpu.write8 =PicoWriteM68k8; - PicoCpu.write16=PicoWriteM68k16; - PicoCpu.write32=PicoWriteM68k32; + PicoCpuCM68k.checkpc=PicoCheckPcM68k; + PicoCpuCM68k.fetch8 =PicoCpuCM68k.read8 =PicoReadM68k8; + PicoCpuCM68k.fetch16=PicoCpuCM68k.read16=PicoReadM68k16; + PicoCpuCM68k.fetch32=PicoCpuCM68k.read32=PicoReadM68k32; + PicoCpuCM68k.write8 =PicoWriteM68k8; + PicoCpuCM68k.write16=PicoWriteM68k16; + PicoCpuCM68k.write32=PicoWriteM68k32; // s68k - PicoCpuS68k.checkpc=PicoCheckPcS68k; - PicoCpuS68k.fetch8 =PicoCpuS68k.read8 =PicoReadS68k8; - PicoCpuS68k.fetch16=PicoCpuS68k.read16=PicoReadS68k16; - PicoCpuS68k.fetch32=PicoCpuS68k.read32=PicoReadS68k32; - PicoCpuS68k.write8 =PicoWriteS68k8; - PicoCpuS68k.write16=PicoWriteS68k16; - PicoCpuS68k.write32=PicoWriteS68k32; + PicoCpuCS68k.checkpc=PicoCheckPcS68k; + PicoCpuCS68k.fetch8 =PicoCpuCS68k.read8 =PicoReadS68k8; + PicoCpuCS68k.fetch16=PicoCpuCS68k.read16=PicoReadS68k16; + PicoCpuCS68k.fetch32=PicoCpuCS68k.read32=PicoReadS68k32; + PicoCpuCS68k.write8 =PicoWriteS68k8; + PicoCpuCS68k.write16=PicoWriteS68k16; + PicoCpuCS68k.write32=PicoWriteS68k32; #endif +#ifdef EMU_F68K + // m68k + PicoCpuFM68k.read_byte =PicoReadM68k8; + PicoCpuFM68k.read_word =PicoReadM68k16; + PicoCpuFM68k.read_long =PicoReadM68k32; + PicoCpuFM68k.write_byte=PicoWriteM68k8; + PicoCpuFM68k.write_word=PicoWriteM68k16; + PicoCpuFM68k.write_long=PicoWriteM68k32; + // s68k + PicoCpuFS68k.read_byte =PicoReadS68k8; + PicoCpuFS68k.read_word =PicoReadS68k16; + PicoCpuFS68k.read_long =PicoReadS68k32; + PicoCpuFS68k.write_byte=PicoWriteS68k8; + PicoCpuFS68k.write_word=PicoWriteS68k16; + PicoCpuFS68k.write_long=PicoWriteS68k32; + + // setup FAME fetchmap + { + int i; + // M68k + // by default, point everything to fitst 64k of ROM (BIOS) + for (i = 0; i < M68K_FETCHBANK1; i++) + PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom - (i<<(24-FAMEC_FETCHBITS)); + // now real ROM (BIOS) + for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < Pico.romsize; i++) + PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.rom; + // .. and RAM + for (i = M68K_FETCHBANK1*14/16; i < M68K_FETCHBANK1; i++) + PicoCpuFM68k.Fetch[i] = (unsigned int)Pico.ram - (i<<(24-FAMEC_FETCHBITS)); + // S68k + // PRG RAM is default + for (i = 0; i < M68K_FETCHBANK1; i++) + PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram - (i<<(24-FAMEC_FETCHBITS)); + // real PRG RAM + for (i = 0; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0x80000; i++) + PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->prg_ram; + // WORD RAM 2M area + for (i = M68K_FETCHBANK1*0x08/0x100; i < M68K_FETCHBANK1 && (i<<(24-FAMEC_FETCHBITS)) < 0xc0000; i++) + PicoCpuFS68k.Fetch[i] = (unsigned int)Pico_mcd->word_ram2M - 0x80000; + // PicoMemResetCD() will setup word ram for both + } +#endif + // m68k_poll_addr = m68k_poll_cnt = 0; s68k_poll_adclk = s68k_poll_cnt = 0; } @@ -1465,33 +1635,34 @@ void PicoMemSetupCD() #ifdef EMU_M68K unsigned char PicoReadCD8w (unsigned int a) { - return m68ki_cpu_p == &PicoS68kCPU ? PicoReadS68k8(a) : PicoReadM68k8(a); + return m68ki_cpu_p == &PicoCpuMS68k ? PicoReadS68k8(a) : PicoReadM68k8(a); } unsigned short PicoReadCD16w(unsigned int a) { - return m68ki_cpu_p == &PicoS68kCPU ? PicoReadS68k16(a) : PicoReadM68k16(a); + return m68ki_cpu_p == &PicoCpuMS68k ? PicoReadS68k16(a) : PicoReadM68k16(a); } unsigned int PicoReadCD32w(unsigned int a) { - return m68ki_cpu_p == &PicoS68kCPU ? PicoReadS68k32(a) : PicoReadM68k32(a); + return m68ki_cpu_p == &PicoCpuMS68k ? PicoReadS68k32(a) : PicoReadM68k32(a); } void PicoWriteCD8w (unsigned int a, unsigned char d) { - if (m68ki_cpu_p == &PicoS68kCPU) PicoWriteS68k8(a, d); else PicoWriteM68k8(a, d); + if (m68ki_cpu_p == &PicoCpuMS68k) PicoWriteS68k8(a, d); else PicoWriteM68k8(a, d); } void PicoWriteCD16w(unsigned int a, unsigned short d) { - if (m68ki_cpu_p == &PicoS68kCPU) PicoWriteS68k16(a, d); else PicoWriteM68k16(a, d); + if (m68ki_cpu_p == &PicoCpuMS68k) PicoWriteS68k16(a, d); else PicoWriteM68k16(a, d); } void PicoWriteCD32w(unsigned int a, unsigned int d) { - if (m68ki_cpu_p == &PicoS68kCPU) PicoWriteS68k32(a, d); else PicoWriteM68k32(a, d); + if (m68ki_cpu_p == &PicoCpuMS68k) PicoWriteS68k32(a, d); else PicoWriteM68k32(a, d); } // these are allowed to access RAM -unsigned int m68k_read_pcrelative_CD8 (unsigned int a) { +unsigned int m68k_read_pcrelative_CD8 (unsigned int a) +{ a&=0xffffff; - if(m68ki_cpu_p == &PicoS68kCPU) { + if(m68ki_cpu_p == &PicoCpuMS68k) { if (a < 0x80000) return *(u8 *)(Pico_mcd->prg_ram+(a^1)); // PRG Ram if ((a&0xfc0000)==0x080000 && !(Pico_mcd->s68k_regs[3]&4)) // word RAM (2M area: 080000-0bffff) return *(u8 *)(Pico_mcd->word_ram2M+((a^1)&0x3ffff)); if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // word RAM (1M area: 0c0000-0dffff) - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; return *(u8 *)(Pico_mcd->word_ram1M[bank]+((a^1)&0x1ffff)); } dprintf("s68k_read_pcrelative_CD8 FIXME: can't handle %06x", a); @@ -1510,14 +1681,15 @@ unsigned int m68k_read_pcrelative_CD8 (unsigned int a) { } return 0;//(u8) lastread_d; } -unsigned int m68k_read_pcrelative_CD16(unsigned int a) { +unsigned int m68k_read_pcrelative_CD16(unsigned int a) +{ a&=0xffffff; - if(m68ki_cpu_p == &PicoS68kCPU) { + if(m68ki_cpu_p == &PicoCpuMS68k) { if (a < 0x80000) return *(u16 *)(Pico_mcd->prg_ram+(a&~1)); // PRG Ram if ((a&0xfc0000)==0x080000 && !(Pico_mcd->s68k_regs[3]&4)) // word RAM (2M area: 080000-0bffff) return *(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe)); if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // word RAM (1M area: 0c0000-0dffff) - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; return *(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe)); } dprintf("s68k_read_pcrelative_CD16 FIXME: can't handle %06x", a); @@ -1536,15 +1708,16 @@ unsigned int m68k_read_pcrelative_CD16(unsigned int a) { } return 0; } -unsigned int m68k_read_pcrelative_CD32(unsigned int a) { +unsigned int m68k_read_pcrelative_CD32(unsigned int a) +{ u16 *pm; a&=0xffffff; - if(m68ki_cpu_p == &PicoS68kCPU) { + if(m68ki_cpu_p == &PicoCpuMS68k) { if (a < 0x80000) { u16 *pm=(u16 *)(Pico_mcd->prg_ram+(a&~1)); return (pm[0]<<16)|pm[1]; } // PRG Ram if ((a&0xfc0000)==0x080000 && !(Pico_mcd->s68k_regs[3]&4)) // word RAM (2M area: 080000-0bffff) { pm=(u16 *)(Pico_mcd->word_ram2M+(a&0x3fffe)); return (pm[0]<<16)|pm[1]; } if ((a&0xfe0000)==0x0c0000 && (Pico_mcd->s68k_regs[3]&4)) { // word RAM (1M area: 0c0000-0dffff) - int bank = !(Pico_mcd->s68k_regs[3]&1); + int bank = (Pico_mcd->s68k_regs[3]&1)^1; pm=(u16 *)(Pico_mcd->word_ram1M[bank]+(a&0x1fffe)); return (pm[0]<<16)|pm[1]; }