2008-08-07 14:26:07 +02:00
|
|
|
/***************************************************************************************
|
|
|
|
* Genesis Plus 1.2a
|
|
|
|
* Genesis internals & Bus arbitration
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Charles Mac Donald (original code)
|
|
|
|
* modified by Eke-Eke (compatibility fixes & additional code), GC/Wii port
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
****************************************************************************************/
|
2007-08-10 22:34:06 +02:00
|
|
|
|
|
|
|
#include "shared.h"
|
|
|
|
|
|
|
|
uint8 *cart_rom; /* cart_rom NEED to be previously dynamically allocated */
|
2008-08-07 14:26:07 +02:00
|
|
|
uint8 bios_rom[0x800];
|
|
|
|
uint8 work_ram[0x10000]; /* 68K work RAM */
|
|
|
|
uint8 zram[0x2000]; /* Z80 work RAM */
|
|
|
|
uint8 zbusreq; /* /BUSREQ from Z80 */
|
|
|
|
uint8 zreset; /* /RESET to Z80 */
|
|
|
|
uint8 zbusack; /* /BUSACK to Z80 */
|
|
|
|
uint8 zirq; /* /IRQ to Z80 */
|
|
|
|
uint32 zbank; /* Address of Z80 bank window */
|
2007-08-10 22:34:06 +02:00
|
|
|
uint8 gen_running;
|
|
|
|
uint32 genromsize;
|
2008-08-07 14:26:07 +02:00
|
|
|
uint32 rom_size;
|
|
|
|
int32 resetline;
|
|
|
|
uint8 *rom_readmap[8];
|
2007-08-10 22:34:06 +02:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Init, reset, shutdown functions */
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2008-08-07 14:26:07 +02:00
|
|
|
void set_softreset(void)
|
|
|
|
{
|
|
|
|
resetline = (int) ((double) (lines_per_frame - 1) * rand() / (RAND_MAX + 1.0));
|
|
|
|
}
|
2007-08-10 22:34:06 +02:00
|
|
|
|
|
|
|
void gen_init (void)
|
|
|
|
{
|
|
|
|
int i;
|
2008-08-07 14:26:07 +02:00
|
|
|
|
|
|
|
/* initialize CPUs */
|
2007-08-10 22:34:06 +02:00
|
|
|
m68k_set_cpu_type (M68K_CPU_TYPE_68000);
|
2008-08-07 14:26:07 +02:00
|
|
|
m68k_init();
|
|
|
|
z80_init(0,0,0,z80_irq_callback);
|
|
|
|
|
|
|
|
/* default 68000 mapping */
|
|
|
|
for (i=16; i<24; i++)
|
|
|
|
{
|
|
|
|
m68k_readmap_8[i] = ILLEGAL;
|
|
|
|
m68k_readmap_16[i] = ILLEGAL;
|
|
|
|
m68k_writemap_8[i] = ILLEGAL;
|
|
|
|
m68k_writemap_16[i] = ILLEGAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Z80, I/O, CONTROL */
|
|
|
|
m68k_readmap_8[20] = SYSTEM_IO;
|
|
|
|
m68k_readmap_16[20] = SYSTEM_IO;
|
|
|
|
m68k_writemap_8[20] = SYSTEM_IO;
|
|
|
|
m68k_writemap_16[20] = SYSTEM_IO;
|
|
|
|
|
|
|
|
/* SEGA PICO */
|
|
|
|
if (system_hw == SYSTEM_PICO)
|
|
|
|
{
|
|
|
|
m68k_readmap_8[16] = PICO_HW;
|
|
|
|
m68k_readmap_16[16] = PICO_HW;
|
|
|
|
m68k_writemap_8[16] = PICO_HW;
|
|
|
|
m68k_writemap_16[16] = PICO_HW;
|
|
|
|
|
|
|
|
/* Notaz: there is no IO CONTROL area (Z80/YM2612/IO) */
|
|
|
|
m68k_readmap_8[20] = UNUSED;
|
|
|
|
m68k_readmap_16[20] = UNUSED;
|
|
|
|
m68k_writemap_8[20] = UNUSED;
|
|
|
|
m68k_writemap_16[20] = UNUSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VDP */
|
|
|
|
for (i=24; i<28; i++)
|
|
|
|
{
|
|
|
|
m68k_readmap_8[i] = VDP;
|
|
|
|
m68k_readmap_16[i] = VDP;
|
|
|
|
m68k_writemap_8[i] = VDP;
|
|
|
|
m68k_writemap_16[i] = VDP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WRAM */
|
|
|
|
for (i=28; i<32; i++)
|
|
|
|
{
|
|
|
|
m68k_readmap_8[i] = WRAM;
|
|
|
|
m68k_readmap_16[i] = WRAM;
|
|
|
|
m68k_writemap_8[i] = WRAM;
|
|
|
|
m68k_writemap_16[i] = WRAM;
|
|
|
|
}
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
void gen_reset (unsigned int hard_reset)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2008-08-07 14:26:07 +02:00
|
|
|
if (hard_reset)
|
|
|
|
{
|
|
|
|
/* Clear RAM */
|
|
|
|
memset (work_ram, 0, sizeof (work_ram));
|
|
|
|
memset (zram, 0, sizeof (zram));
|
|
|
|
|
|
|
|
/* Reset ROM mapping */
|
|
|
|
if (config.bios_enabled == 3)
|
|
|
|
{
|
|
|
|
rom_readmap[0] = &bios_rom[0];
|
|
|
|
rom_size = 0x800;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rom_readmap[0] = &cart_rom[0];
|
|
|
|
rom_size = genromsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8 i;
|
|
|
|
for (i=1; i<8; i++) rom_readmap[i] = &cart_rom[i << 19];
|
|
|
|
}
|
|
|
|
|
2007-08-10 22:34:06 +02:00
|
|
|
gen_running = 1;
|
2008-08-07 14:26:07 +02:00
|
|
|
zreset = 0; /* Z80 is reset */
|
|
|
|
zbusreq = 0; /* Z80 has control of the Z bus */
|
|
|
|
zbusack = 1; /* Z80 is busy using the Z bus */
|
|
|
|
zbank = 0; /* Assume default bank is 000000-007FFF */
|
|
|
|
zirq = 0; /* No interrupts occuring */
|
|
|
|
resetline = -1;
|
|
|
|
|
|
|
|
/* Reset CPUs */
|
2007-08-10 22:34:06 +02:00
|
|
|
m68k_pulse_reset ();
|
2008-08-07 14:26:07 +02:00
|
|
|
z80_reset ();
|
|
|
|
_YM2612_Reset();
|
|
|
|
|
|
|
|
#ifdef NGC
|
|
|
|
/* register SOFTRESET */
|
|
|
|
SYS_SetResetCallback(set_softreset);
|
|
|
|
#endif
|
|
|
|
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void gen_shutdown (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------
|
|
|
|
Bus controller chip functions
|
|
|
|
-----------------------------------------------------------------------*/
|
2008-08-07 14:26:07 +02:00
|
|
|
unsigned int gen_busack_r (void)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2008-08-07 14:26:07 +02:00
|
|
|
return zbusack;
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
void gen_busreq_w (unsigned int state)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
|
|
|
int z80_cycles_to_run;
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
input_raz (); /* from Gens */
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
if (state)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
|
|
|
/* Bus Request */
|
2008-08-07 14:26:07 +02:00
|
|
|
if (!zbusreq && zreset)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
|
|
|
/* Z80 stopped */
|
|
|
|
/* z80 was ON during the last 68k cycles */
|
|
|
|
/* we execute the appropriate number of z80 cycles */
|
2008-08-07 14:26:07 +02:00
|
|
|
z80_cycles_to_run = line_z80 + ((count_m68k - line_m68k)*7)/15;
|
|
|
|
z80_run(z80_cycles_to_run);
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Bus released */
|
2008-08-07 14:26:07 +02:00
|
|
|
if (zbusreq && zreset)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
|
|
|
/* Z80 started */
|
|
|
|
/* z80 was OFF during the last 68k cycles */
|
|
|
|
/* we burn the appropriate number of z80 cycles */
|
2008-08-07 14:26:07 +02:00
|
|
|
z80_cycles_to_run = line_z80 + ((count_m68k - line_m68k)*7)/15;
|
|
|
|
count_z80 = z80_cycles_to_run;
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-07 14:26:07 +02:00
|
|
|
|
|
|
|
zbusreq = state;
|
2007-08-10 22:34:06 +02:00
|
|
|
zbusack = 1 ^ (zbusreq & zreset);
|
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
void gen_reset_w (unsigned int state)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2008-08-07 14:26:07 +02:00
|
|
|
int z80_cycles_to_run;
|
2007-08-10 22:34:06 +02:00
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
/* stop RESET process */
|
|
|
|
if (!zbusreq && !zreset)
|
|
|
|
{
|
|
|
|
/* Z80 started */
|
|
|
|
/* z80 was OFF during the last 68k cycles */
|
|
|
|
/* we burn the appropriate number of z80 cycles */
|
|
|
|
z80_cycles_to_run = line_z80 + ((count_m68k - line_m68k)*7)/15;
|
|
|
|
count_z80 = z80_cycles_to_run;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
2008-08-07 14:26:07 +02:00
|
|
|
/* start RESET process */
|
|
|
|
if (!zbusreq && zreset)
|
|
|
|
{
|
|
|
|
/* Z80 stopped */
|
|
|
|
/* z80 was ON during the last 68k cycles */
|
|
|
|
/* we execute the appropriate number of z80 cycles */
|
|
|
|
z80_cycles_to_run = line_z80 + ((count_m68k - line_m68k)*7)/15;
|
|
|
|
z80_run(z80_cycles_to_run);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset Z80 & YM2612 */
|
|
|
|
_YM2612_Reset();
|
|
|
|
z80_reset ();
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
2008-08-07 14:26:07 +02:00
|
|
|
|
|
|
|
zreset = state;
|
|
|
|
zbusack = 1 ^ (zbusreq & zreset);
|
2007-08-10 22:34:06 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 14:26:07 +02:00
|
|
|
void gen_bank_w (unsigned int state)
|
2007-08-10 22:34:06 +02:00
|
|
|
{
|
|
|
|
zbank = ((zbank >> 1) | ((state & 1) << 23)) & 0xFF8000;
|
|
|
|
}
|
|
|
|
|
|
|
|
int z80_irq_callback (int param)
|
|
|
|
{
|
|
|
|
zirq = 0;
|
|
|
|
z80_set_irq_line (0, CLEAR_LINE);
|
|
|
|
return 0xFF;
|
|
|
|
}
|