sms, memory mapping improvements

This commit is contained in:
kub 2021-10-08 19:34:15 +02:00
parent 47ea630f6d
commit 032c76a3a2
4 changed files with 69 additions and 41 deletions

View file

@ -35,8 +35,8 @@ static int detect_media(const char *fname)
{ {
static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 }; static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };
static const char *sms_exts[] = { "sms", "gg", "sg" }; static const char *sms_exts[] = { "sms", "gg", "sg" };
static const char *md_exts[] = { "gen", "bin", "smd" }; static const char *md_exts[] = { "gen", "smd" };
char buff0[32], buff[32]; char buff0[512], buff[32];
unsigned short *d16; unsigned short *d16;
pm_file *pmf; pm_file *pmf;
char ext[5]; char ext[5];
@ -56,7 +56,7 @@ static int detect_media(const char *fname)
if (pmf == NULL) if (pmf == NULL)
return PM_BAD_DETECT; return PM_BAD_DETECT;
if (pm_read(buff0, 32, pmf) != 32) { if (pm_read(buff0, 512, pmf) != 512) {
pm_close(pmf); pm_close(pmf);
return PM_BAD_DETECT; return PM_BAD_DETECT;
} }
@ -111,6 +111,11 @@ extension_check:
lprintf("bad MD reset vector, assuming SMS\n"); lprintf("bad MD reset vector, assuming SMS\n");
goto looks_like_sms; goto looks_like_sms;
} }
d16 = (unsigned short *)(buff0 + 0x1a0);
if ((((d16[0] << 16) | d16[1]) & 0xffffff) != 0) {
lprintf("bad MD rom start, assuming SMS\n");
goto looks_like_sms;
}
looks_like_md: looks_like_md:
pm_close(pmf); pm_close(pmf);

View file

@ -57,6 +57,10 @@ void z80_map_set(uptr *map, int start_addr, int end_addr,
const void *func_or_mh, int is_func) const void *func_or_mh, int is_func)
{ {
xmap_set(map, Z80_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func); xmap_set(map, Z80_MEM_SHIFT, start_addr, end_addr, func_or_mh, is_func);
#ifdef _USE_CZ80
if (!is_func)
Cz80_Set_Fetch(&CZ80, start_addr, end_addr, (FPTR)func_or_mh);
#endif
} }
void cpu68k_map_set(uptr *map, int start_addr, int end_addr, void cpu68k_map_set(uptr *map, int start_addr, int end_addr,
@ -1290,8 +1294,6 @@ static void z80_mem_setup(void)
drZ80.z80_out = z80_md_out; drZ80.z80_out = z80_md_out;
#endif #endif
#ifdef _USE_CZ80 #ifdef _USE_CZ80
Cz80_Set_Fetch(&CZ80, 0x0000, 0x1fff, (FPTR)PicoMem.zram); // main RAM
Cz80_Set_Fetch(&CZ80, 0x2000, 0x3fff, (FPTR)PicoMem.zram); // mirror
Cz80_Set_INPort(&CZ80, z80_md_in); Cz80_Set_INPort(&CZ80, z80_md_in);
Cz80_Set_OUTPort(&CZ80, z80_md_out); Cz80_Set_OUTPort(&CZ80, z80_md_out);
#endif #endif

View file

@ -347,7 +347,8 @@ struct PicoMS
unsigned char carthw[0x10]; unsigned char carthw[0x10];
unsigned char io_ctl; unsigned char io_ctl;
unsigned char nmi_state; unsigned char nmi_state;
unsigned char pad[0x4e]; unsigned char mapper;
unsigned char pad[0x4d];
}; };
// emu state and data for the asm code // emu state and data for the asm code

View file

@ -8,7 +8,6 @@
/* /*
* TODO: * TODO:
* - start in a state as if BIOS ran * - start in a state as if BIOS ran
* - RAM support in mapper
* - region support * - region support
* - H counter * - H counter
*/ */
@ -231,7 +230,8 @@ static void write_sram(unsigned short a, unsigned char d)
Pico.sv.data[a] = d; Pico.sv.data[a] = d;
} }
static void write_bank(unsigned short a, unsigned char d) // 16KB bank mapping for Sega mapper
static void write_bank_sega(unsigned short a, unsigned char d)
{ {
elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc()); elprintf(EL_Z80BNK, "bank %04x %02x @ %04x", a, d, z80_pc());
Pico.ms.carthw[a & 0x0f] = d; Pico.ms.carthw[a & 0x0f] = d;
@ -240,16 +240,10 @@ static void write_bank(unsigned short a, unsigned char d)
case 0x0d: case 0x0d:
d &= bank_mask; d &= bank_mask;
z80_map_set(z80_read_map, 0x0400, 0x3fff, Pico.rom+0x400 + (d << 14), 0); z80_map_set(z80_read_map, 0x0400, 0x3fff, Pico.rom+0x400 + (d << 14), 0);
#ifdef _USE_CZ80
Cz80_Set_Fetch(&CZ80, 0x0400, 0x3fff, (FPTR)Pico.rom+0x400 + (d << 14));
#endif
break; break;
case 0x0e: case 0x0e:
d &= bank_mask; d &= bank_mask;
z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0); z80_map_set(z80_read_map, 0x4000, 0x7fff, Pico.rom + (d << 14), 0);
#ifdef _USE_CZ80
Cz80_Set_Fetch(&CZ80, 0x4000, 0x7fff, (FPTR)Pico.rom + (d << 14));
#endif
break; break;
case 0x0c: case 0x0c:
@ -260,39 +254,50 @@ static void write_bank(unsigned short a, unsigned char d)
if (Pico.ms.carthw[0xc] & 0x08) { if (Pico.ms.carthw[0xc] & 0x08) {
d = (Pico.ms.carthw[0xc] & 0x04) >> 2; d = (Pico.ms.carthw[0xc] & 0x04) >> 2;
z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.sv.data + d*0x4000, 0); z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.sv.data + d*0x4000, 0);
#ifdef _USE_CZ80
Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.sv.data + d*0x4000);
#endif
z80_map_set(z80_write_map, 0x8000, 0xbfff, write_sram, 1); z80_map_set(z80_write_map, 0x8000, 0xbfff, write_sram, 1);
} else { } else {
d = Pico.ms.carthw[0xf] & bank_mask; d = Pico.ms.carthw[0xf] & bank_mask;
z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0); z80_map_set(z80_read_map, 0x8000, 0xbfff, Pico.rom + (d << 14), 0);
#ifdef _USE_CZ80
Cz80_Set_Fetch(&CZ80, 0x8000, 0xbfff, (FPTR)Pico.rom + (d << 14));
#endif
z80_map_set(z80_write_map, 0x8000, 0xbfff, xwrite, 1); z80_map_set(z80_write_map, 0x8000, 0xbfff, xwrite, 1);
} }
break; break;
} }
} }
// 8KB ROM mapping for MSX mapper
static void write_bank_msx(unsigned short a, unsigned char d)
{
Pico.ms.mapper = 1; // TODO define (more) mapper types
Pico.ms.carthw[a] = d;
a = (a^2)*0x2000 + 0x4000;
d &= 2*bank_mask + 1;
z80_map_set(z80_read_map, a, a+0x1fff, Pico.rom + (d << 13), 0);
}
// TODO mapping is currently auto-selecting, but that's not very reliable.
static void xwrite(unsigned int a, unsigned char d) static void xwrite(unsigned int a, unsigned char d)
{ {
elprintf(EL_IO, "z80 write [%04x] %02x", a, d); elprintf(EL_IO, "z80 write [%04x] %02x", a, d);
if (a >= 0xc000) if (a >= 0xc000)
PicoMem.zram[a & 0x1fff] = d; PicoMem.zram[a & 0x1fff] = d;
if (a >= 0xfff8)
write_bank(a, d); // Sega. Maps 4 bank 16KB each
// codemasters if (a >= 0xfff8 /*&& !Pico.ms.mapper*/)
if (a == 0x0000) write_bank_sega(a, d);
write_bank(0xfffd, d); // Codemasters. Similar to Sega, but different addresses
if (a == 0x0000 && !Pico.ms.mapper)
write_bank_sega(0xfffd, d);
if (a == 0x4000) if (a == 0x4000)
write_bank(0xfffe, d); write_bank_sega(0xfffe, d);
if (a == 0x8000) if (a == 0x8000)
write_bank(0xffff, d); write_bank_sega(0xffff, d);
// korean // Korean. 1 selectable 16KB bank at the top
if (a == 0xa000) if (a == 0xa000)
write_bank(0xffff, d); write_bank_sega(0xffff, d);
// MSX. 4 selectable 8KB banks at the top
if (a <= 0x0003 && (a || Pico.ms.mapper))
write_bank_msx(a, d);
} }
void PicoResetMS(void) void PicoResetMS(void)
@ -300,6 +305,10 @@ void PicoResetMS(void)
z80_reset(); z80_reset();
PsndReset(); // pal must be known here PsndReset(); // pal must be known here
ymflag = 0xffff; ymflag = 0xffff;
Pico.m.dirtyPal = 1;
// reset memory mapping
PicoMemSetupMS();
} }
void PicoPowerMS(void) void PicoPowerMS(void)
@ -321,11 +330,6 @@ void PicoPowerMS(void)
tmp = 1 << s; tmp = 1 << s;
bank_mask = (tmp - 1) >> 14; bank_mask = (tmp - 1) >> 14;
Pico.ms.carthw[0x0c] = 0;
Pico.ms.carthw[0x0d] = 0;
Pico.ms.carthw[0x0e] = 1;
Pico.ms.carthw[0x0f] = 2;
PicoReset(); PicoReset();
} }
@ -344,20 +348,36 @@ void PicoMemSetupMS(void)
drZ80.z80_out = z80_sms_out; drZ80.z80_out = z80_sms_out;
#endif #endif
#ifdef _USE_CZ80 #ifdef _USE_CZ80
Cz80_Set_Fetch(&CZ80, 0x0000, 0xbfff, (FPTR)Pico.rom);
Cz80_Set_Fetch(&CZ80, 0xc000, 0xdfff, (FPTR)PicoMem.zram);
Cz80_Set_Fetch(&CZ80, 0xe000, 0xffff, (FPTR)PicoMem.zram);
Cz80_Set_INPort(&CZ80, z80_sms_in); Cz80_Set_INPort(&CZ80, z80_sms_in);
Cz80_Set_OUTPort(&CZ80, z80_sms_out); Cz80_Set_OUTPort(&CZ80, z80_sms_out);
#endif #endif
// memory mapper setup, linearly mapped, default is Sega mapper
Pico.ms.carthw[0x00] = 4;
Pico.ms.carthw[0x01] = 5;
Pico.ms.carthw[0x02] = 2;
Pico.ms.carthw[0x03] = 3;
Pico.ms.carthw[0x0c] = 0;
Pico.ms.carthw[0x0d] = 0;
Pico.ms.carthw[0x0e] = 1;
Pico.ms.carthw[0x0f] = 2;
Pico.ms.mapper = 0;
} }
void PicoStateLoadedMS(void) void PicoStateLoadedMS(void)
{ {
write_bank(0xfffc, Pico.ms.carthw[0x0c]); if (Pico.ms.mapper) {
write_bank(0xfffd, Pico.ms.carthw[0x0d]); xwrite(0x0000, Pico.ms.carthw[0]);
write_bank(0xfffe, Pico.ms.carthw[0x0e]); xwrite(0x0001, Pico.ms.carthw[1]);
write_bank(0xffff, Pico.ms.carthw[0x0f]); xwrite(0x0002, Pico.ms.carthw[2]);
xwrite(0x0003, Pico.ms.carthw[3]);
} else {
xwrite(0xfffc, Pico.ms.carthw[0x0c]);
xwrite(0xfffd, Pico.ms.carthw[0x0d]);
xwrite(0xfffe, Pico.ms.carthw[0x0e]);
xwrite(0xffff, Pico.ms.carthw[0x0f]);
}
} }
void PicoFrameMS(void) void PicoFrameMS(void)