X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2Fcd%2FMemory.c;h=44f84d82db49e335269a6ff1448bb9db726d912e;hb=55ca4154a2e8aa3687c4f73f2c4c4d5fcdc5a836;hp=08a4486fbbc593493c2a4813db6aa9ae213b2f15;hpb=c008977e8ec74b68cbccc017620a156cc39b79b2;p=picodrive.git diff --git a/Pico/cd/Memory.c b/Pico/cd/Memory.c index 08a4486..44f84d8 100644 --- a/Pico/cd/Memory.c +++ b/Pico/cd/Memory.c @@ -1,10 +1,6 @@ -// This is part of Pico Library - -// (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. +// Memory I/O handlers for Sega/Mega CD. +// Loosely based on Gens code. +// (c) Copyright 2007, Grazvydas "notaz" Ignotas // A68K no longer supported here @@ -26,14 +22,17 @@ typedef unsigned int u32; //#define __debug_io //#define __debug_io2 -//#define rdprintf dprintf -#define rdprintf(...) +#define rdprintf dprintf +//#define rdprintf(...) //#define wrdprintf dprintf #define wrdprintf(...) +#define plprintf dprintf +//#define plprintf(...) // ----------------------------------------------------------------- // poller detection +//#undef USE_POLL_DETECT #define POLL_LIMIT 16 #define POLL_CYCLES 124 // int m68k_poll_addr, m68k_poll_cnt; @@ -115,6 +114,7 @@ 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: { @@ -143,7 +143,7 @@ void m68k_reg_write8(u32 a, u32 d) #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; @@ -163,7 +163,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; @@ -174,7 +174,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; @@ -183,6 +183,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) \ { \ @@ -208,9 +233,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); + d = (Pico_mcd->s68k_regs[2]<<8) | (Pico_mcd->s68k_regs[3]&0x1f); //printf("s68k_regs r3: %02x @%06x\n", (u8)d, SekPcS68k); - goto poll_detect; + return s68k_poll_detect(a, d); case 6: return CDC_Read_Reg(); case 8: @@ -240,30 +265,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; } @@ -398,7 +402,7 @@ 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); + dprintf("m68k FIXME: strange w%i: [%06x], %08x @%06x", realsize, a&0xffffff, d, SekPc); } @@ -407,6 +411,7 @@ static void OtherWrite8End(u32 a, u32 d, int realsize) #include "cell_map.c" #endif // !def _ASM_CD_MEMORY_C + // ----------------------------------------------------------------- // Read Rom and read Ram @@ -425,7 +430,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; @@ -482,7 +487,7 @@ 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)); @@ -539,7 +544,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]; @@ -587,7 +592,6 @@ static u32 PicoReadM68k32(u32 a) // ----------------------------------------------------------------- -// Write Ram #ifdef _ASM_CD_MEMORY_C void PicoWriteM68k8(u32 a,u8 d); @@ -609,7 +613,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; @@ -631,8 +635,11 @@ 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); } @@ -657,7 +664,7 @@ 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; @@ -687,8 +694,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; @@ -723,7 +730,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; @@ -755,8 +762,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); @@ -764,6 +773,8 @@ static void PicoWriteM68k32(u32 a,u32 d) #endif +// ----------------------------------------------------------------- +// S68k // ----------------------------------------------------------------- #ifdef _ASM_CD_MEMORY_C @@ -785,7 +796,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; @@ -1008,7 +1025,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; @@ -1120,7 +1137,7 @@ static void PicoWriteS68k8(u32 a,u8 d) // 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; } @@ -1196,7 +1213,8 @@ static void PicoWriteS68k16(u32 a,u16 d) // prg RAM if (a < 0x80000) { wrdprintf("s68k_prgram w16: [%06x] %04x @%06x", a, d, SekPcS68k); - *(u16 *)(Pico_mcd->prg_ram+a)=d; + if (a >= (Pico_mcd->s68k_regs[2]<<8)) // needed for Dungeon Explorer + *(u16 *)(Pico_mcd->prg_ram+a)=d; return; } @@ -1280,8 +1298,10 @@ static void PicoWriteS68k32(u32 a,u32 d) // 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; } @@ -1293,6 +1313,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);