2013-10-27 22:07:40 +00:00
|
|
|
|
using BizHawk.Common;
|
2014-07-03 17:23:03 +00:00
|
|
|
|
using BizHawk.Common.NumberExtensions;
|
2013-10-27 22:07:40 +00:00
|
|
|
|
|
2013-11-14 13:15:41 +00:00
|
|
|
|
namespace BizHawk.Emulation.Cores.Nintendo.NES
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
|
|
|
|
/*
|
2014-01-19 19:04:44 +00:00
|
|
|
|
I'm breaking FCG boards into 7 main types:
|
2011-09-18 02:49:09 +00:00
|
|
|
|
|
2014-01-19 19:04:44 +00:00
|
|
|
|
[1] FCG-1, FCG-2: regs at 6000:7fff.
|
|
|
|
|
FCG-3: regs at 8000:ffff. one of the following at 6000:7fff:
|
|
|
|
|
[2] nothing
|
|
|
|
|
[3] seeprom (1kbit)
|
|
|
|
|
[4] seeprom (2kbit)
|
|
|
|
|
[5] sram (8kbyte) (SEE SIZE NOTES BELOW)
|
|
|
|
|
[6] Datach Joint ROM System: daughterboard setup (DON'T KNOW MUCH ABOUT THIS)
|
|
|
|
|
[7] Non-existant zombie board: regs are at 6000:ffff and 2kbit seeprom is present
|
|
|
|
|
|
|
|
|
|
iNES #16 refers to [7], which ends up working correctly for most [1], [2], or [4] games.
|
|
|
|
|
iNES #153 refers to [5], theoretically.
|
|
|
|
|
iNES #157 refers to [6], theoretically.
|
|
|
|
|
iNES #159 refers to [3], theoretically.
|
|
|
|
|
|
2014-01-19 19:55:32 +00:00
|
|
|
|
We try to emulate everything but [6] here.
|
2014-01-19 19:04:44 +00:00
|
|
|
|
|
|
|
|
|
Size notes:
|
|
|
|
|
chr regs are 8 bit wide and swap 1K at a time, for a max size of 256K chr, always rom.
|
|
|
|
|
prg reg is 4 bit wide and swaps 16K at a time, for a max size of 256K prg.
|
|
|
|
|
[5] is a special case; it has 8K of vram and uses some of the chr banking lines to handle 512K of prgrom.
|
|
|
|
|
I have no idea what [6] does.
|
|
|
|
|
Every real instance of [1], [2], [3], [4] had 128K or 256K of each of chr and prg.
|
2011-09-18 02:49:09 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2013-08-25 01:08:17 +00:00
|
|
|
|
public sealed class BANDAI_FCG_1 : NES.NESBoardBase
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
|
|
|
|
//configuration
|
2011-09-18 19:29:53 +00:00
|
|
|
|
int prg_bank_mask_16k, chr_bank_mask_1k;
|
2011-09-26 03:26:47 +00:00
|
|
|
|
|
2014-01-19 19:55:32 +00:00
|
|
|
|
bool regs_prg_enable; // can the mapper regs be written to in 8000:ffff?
|
|
|
|
|
bool regs_wram_enable; // can the mapper regs be written to in 6000:7fff?
|
|
|
|
|
bool jump2 = false; // are we in special mode for the JUMP2 board?
|
2014-01-19 19:04:44 +00:00
|
|
|
|
|
2011-09-26 03:26:47 +00:00
|
|
|
|
//regenerable state
|
|
|
|
|
IntBuffer prg_banks_16k = new IntBuffer(2);
|
|
|
|
|
|
2011-09-18 16:14:55 +00:00
|
|
|
|
//state
|
2012-12-17 19:54:45 +00:00
|
|
|
|
int prg_reg_16k;
|
2011-09-18 19:29:53 +00:00
|
|
|
|
ByteBuffer regs = new ByteBuffer(8);
|
2012-10-31 16:29:26 +00:00
|
|
|
|
bool irq_enabled;
|
2011-09-18 02:49:09 +00:00
|
|
|
|
ushort irq_counter;
|
2012-12-17 19:54:45 +00:00
|
|
|
|
SEEPROM eprom;
|
2011-09-18 02:49:09 +00:00
|
|
|
|
|
2011-09-18 16:14:55 +00:00
|
|
|
|
public override void SyncState(Serializer ser)
|
|
|
|
|
{
|
|
|
|
|
base.SyncState(ser);
|
2011-09-26 03:26:47 +00:00
|
|
|
|
ser.Sync("prg_reg_16k", ref prg_reg_16k);
|
2011-09-20 01:07:24 +00:00
|
|
|
|
ser.Sync("regs", ref regs);
|
2011-09-18 16:14:55 +00:00
|
|
|
|
ser.Sync("irq_counter", ref irq_counter);
|
|
|
|
|
ser.Sync("irq_enabled", ref irq_enabled);
|
2012-12-17 19:54:45 +00:00
|
|
|
|
if (eprom != null)
|
|
|
|
|
eprom.SyncState(ser);
|
2012-06-25 06:32:54 +00:00
|
|
|
|
SyncPRG();
|
2011-09-18 16:14:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Dispose()
|
|
|
|
|
{
|
|
|
|
|
base.Dispose();
|
|
|
|
|
regs.Dispose();
|
2011-09-18 19:29:53 +00:00
|
|
|
|
prg_banks_16k.Dispose();
|
2011-09-18 16:14:55 +00:00
|
|
|
|
}
|
2011-09-18 02:49:09 +00:00
|
|
|
|
|
|
|
|
|
public override bool Configure(NES.EDetectionOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
switch (Cart.board_type)
|
|
|
|
|
{
|
2014-01-19 19:04:44 +00:00
|
|
|
|
// see notes above that explain some of this in more detail
|
|
|
|
|
|
|
|
|
|
case "BANDAI-FCG-1": // [1]
|
|
|
|
|
case "BANDAI-FCG-2": // [1]
|
|
|
|
|
case "IREM-FCG-1": // [1] (the extra glue logic is to connect the two chr roms, and doesn't affect emulation)
|
|
|
|
|
AssertPrg(128, 256); AssertChr(128, 256); AssertWram(0); AssertVram(0);
|
|
|
|
|
regs_prg_enable = false;
|
|
|
|
|
regs_wram_enable = true;
|
|
|
|
|
break;
|
|
|
|
|
case "BANDAI-LZ93D50": // [2]
|
|
|
|
|
AssertPrg(128, 256); AssertChr(128, 256); AssertWram(0); AssertVram(0);
|
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = false;
|
2011-09-18 02:49:09 +00:00
|
|
|
|
break;
|
2014-01-19 19:04:44 +00:00
|
|
|
|
case "BANDAI-LZ93D50+24C01": // [3]
|
2012-12-17 19:54:45 +00:00
|
|
|
|
AssertPrg(128, 256); AssertChr(128, 256); AssertWram(0); AssertVram(0);
|
|
|
|
|
eprom = new SEEPROM(false);
|
2014-01-19 19:04:44 +00:00
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = false;
|
2012-12-17 19:54:45 +00:00
|
|
|
|
break;
|
2014-01-19 19:04:44 +00:00
|
|
|
|
case "MAPPER159": // [3]
|
|
|
|
|
AssertPrg(128, 256); AssertChr(128, 256);
|
|
|
|
|
Cart.wram_size = 0;
|
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = false;
|
|
|
|
|
eprom = new SEEPROM(false);
|
|
|
|
|
break;
|
|
|
|
|
case "BANDAI-LZ93D50+24C02": // [4]
|
2011-09-25 17:16:26 +00:00
|
|
|
|
AssertPrg(128, 256); AssertChr(128, 256); AssertWram(0); AssertVram(0);
|
2012-12-17 19:54:45 +00:00
|
|
|
|
eprom = new SEEPROM(true);
|
2014-01-19 19:04:44 +00:00
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = false;
|
2011-09-25 17:16:26 +00:00
|
|
|
|
break;
|
2014-01-19 19:04:44 +00:00
|
|
|
|
case "MAPPER016": // [7]
|
2014-01-21 19:08:11 +00:00
|
|
|
|
if (Cart.prg_size > 256)
|
|
|
|
|
{
|
|
|
|
|
// you have two options:
|
|
|
|
|
// 1) assume prg > 256 => jump2 (aka mapper 153, type [5])
|
|
|
|
|
// this will break hypothetical prg oversize hacks
|
|
|
|
|
// 2) assume prg > 256 => oversize regular FCG
|
|
|
|
|
// this will break famicom 2 dumps without hash match,
|
|
|
|
|
// which are marked mapper016 usually
|
|
|
|
|
goto case "MAPPER153";
|
|
|
|
|
}
|
2014-01-19 19:04:44 +00:00
|
|
|
|
AssertPrg(128, 256); AssertChr(128, 256);
|
2012-12-17 19:54:45 +00:00
|
|
|
|
Cart.wram_size = 0;
|
2014-01-19 19:04:44 +00:00
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = true;
|
|
|
|
|
eprom = new SEEPROM(true);
|
2011-09-25 17:16:26 +00:00
|
|
|
|
break;
|
2014-01-19 19:55:32 +00:00
|
|
|
|
case "MAPPER153": // [5]
|
|
|
|
|
AssertPrg(512);
|
|
|
|
|
AssertChr(0);
|
|
|
|
|
Cart.vram_size = 8;
|
|
|
|
|
Cart.wram_size = 8;
|
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = false;
|
2014-01-21 19:08:11 +00:00
|
|
|
|
jump2 = true;
|
2014-01-19 19:55:32 +00:00
|
|
|
|
break;
|
|
|
|
|
case "BANDAI-JUMP2": // [5]
|
|
|
|
|
AssertPrg(512);
|
|
|
|
|
AssertChr(0);
|
|
|
|
|
AssertVram(8);
|
|
|
|
|
AssertWram(8);
|
|
|
|
|
regs_prg_enable = true;
|
|
|
|
|
regs_wram_enable = false;
|
|
|
|
|
jump2 = true;
|
|
|
|
|
break;
|
|
|
|
|
|
2011-09-18 02:49:09 +00:00
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-18 19:29:53 +00:00
|
|
|
|
prg_bank_mask_16k = (Cart.prg_size / 16) - 1;
|
2011-09-26 03:26:47 +00:00
|
|
|
|
chr_bank_mask_1k = Cart.chr_size - 1;
|
|
|
|
|
|
2011-09-18 02:49:09 +00:00
|
|
|
|
SetMirrorType(EMirrorType.Vertical);
|
2011-09-26 03:26:47 +00:00
|
|
|
|
|
|
|
|
|
prg_reg_16k = 0;
|
|
|
|
|
SyncPRG();
|
|
|
|
|
|
|
|
|
|
return true;
|
2011-09-18 02:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-18 16:14:55 +00:00
|
|
|
|
void SyncPRG()
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2011-09-26 03:26:47 +00:00
|
|
|
|
prg_banks_16k[0] = prg_reg_16k & prg_bank_mask_16k;
|
|
|
|
|
prg_banks_16k[1] = 0xFF & prg_bank_mask_16k;
|
2014-01-19 19:55:32 +00:00
|
|
|
|
if (jump2)
|
|
|
|
|
{
|
|
|
|
|
if (regs[0].Bit(0))
|
|
|
|
|
{
|
|
|
|
|
prg_banks_16k[0] |= 0x10;
|
|
|
|
|
prg_banks_16k[1] |= 0x10;
|
|
|
|
|
}
|
|
|
|
|
else // wouldn't need this, except we aren't &=15 on the prg bank addresses
|
|
|
|
|
{
|
|
|
|
|
prg_banks_16k[0] &= 0x0f;
|
|
|
|
|
prg_banks_16k[1] &= 0x0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-18 02:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-18 19:29:53 +00:00
|
|
|
|
void WriteReg(int reg, byte value)
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2011-09-26 03:26:47 +00:00
|
|
|
|
//Console.WriteLine("reg {0:X2} = {1:X2}", reg, value);
|
2011-09-18 19:29:53 +00:00
|
|
|
|
switch (reg)
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2011-09-18 19:29:53 +00:00
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
case 2:
|
|
|
|
|
case 3:
|
|
|
|
|
case 4:
|
|
|
|
|
case 5:
|
|
|
|
|
case 6:
|
|
|
|
|
case 7:
|
|
|
|
|
regs[reg] = value;
|
2014-01-19 19:55:32 +00:00
|
|
|
|
if (jump2) // in jump2, chr regs are rewired to swap prg
|
|
|
|
|
SyncPRG();
|
2011-09-18 02:49:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case 8:
|
2011-09-25 17:16:26 +00:00
|
|
|
|
//NES.LogLine("mapping PRG {0}", value);
|
2011-09-26 03:26:47 +00:00
|
|
|
|
prg_reg_16k = value;
|
2011-09-18 16:14:55 +00:00
|
|
|
|
SyncPRG();
|
2011-09-18 02:49:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case 9:
|
2011-09-18 16:14:55 +00:00
|
|
|
|
switch (value & 3)
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
|
|
|
|
case 0: SetMirrorType(NES.NESBoardBase.EMirrorType.Vertical); break;
|
|
|
|
|
case 1: SetMirrorType(NES.NESBoardBase.EMirrorType.Horizontal); break;
|
|
|
|
|
case 2: SetMirrorType(NES.NESBoardBase.EMirrorType.OneScreenA); break;
|
|
|
|
|
case 3: SetMirrorType(NES.NESBoardBase.EMirrorType.OneScreenB); break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 0xA:
|
|
|
|
|
irq_enabled = value.Bit(0);
|
2012-10-31 16:29:26 +00:00
|
|
|
|
// all write acknolwedge
|
|
|
|
|
IRQSignal = false;
|
2011-09-18 02:49:09 +00:00
|
|
|
|
break;
|
|
|
|
|
case 0xB:
|
|
|
|
|
irq_counter &= 0xFF00;
|
|
|
|
|
irq_counter |= value;
|
|
|
|
|
break;
|
|
|
|
|
case 0xC:
|
|
|
|
|
irq_counter &= 0x00FF;
|
|
|
|
|
irq_counter |= (ushort)(value << 8);
|
|
|
|
|
break;
|
|
|
|
|
case 0xD:
|
2012-12-17 19:54:45 +00:00
|
|
|
|
if (eprom != null)
|
|
|
|
|
eprom.WriteByte(value);
|
2011-09-18 02:49:09 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-18 19:29:53 +00:00
|
|
|
|
public override void WriteWRAM(int addr, byte value)
|
|
|
|
|
{
|
2011-09-25 17:16:26 +00:00
|
|
|
|
//NES.LogLine("writewram {0:X4} = {1:X2}", addr, value);
|
2014-01-19 19:04:44 +00:00
|
|
|
|
if (regs_wram_enable)
|
|
|
|
|
{
|
|
|
|
|
addr &= 0xF;
|
|
|
|
|
WriteReg(addr, value);
|
|
|
|
|
}
|
2014-01-19 19:55:32 +00:00
|
|
|
|
else if (jump2)
|
|
|
|
|
{
|
|
|
|
|
WRAM[addr] = value;
|
|
|
|
|
}
|
2011-09-18 19:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
public override void WritePRG(int addr, byte value)
|
|
|
|
|
{
|
2011-09-25 17:16:26 +00:00
|
|
|
|
//NES.LogLine("writeprg {0:X4} = {1:X2}", addr, value);
|
2014-01-19 19:04:44 +00:00
|
|
|
|
if (regs_prg_enable)
|
|
|
|
|
{
|
|
|
|
|
addr &= 0xF;
|
|
|
|
|
WriteReg(addr, value);
|
|
|
|
|
}
|
2011-09-18 19:29:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-31 16:29:26 +00:00
|
|
|
|
public override byte ReadWRAM(int addr)
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2012-10-31 16:29:26 +00:00
|
|
|
|
// reading any addr in 6000:7fff returns a single bit from the eeprom
|
2012-12-17 19:54:45 +00:00
|
|
|
|
// in bit 4.
|
2014-01-19 19:55:32 +00:00
|
|
|
|
if (!jump2)
|
|
|
|
|
{
|
|
|
|
|
byte ret = (byte)(NES.DB & 0xef);
|
|
|
|
|
if (eprom != null && eprom.ReadBit(NES.DB.Bit(4)))
|
|
|
|
|
ret |= 0x10;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return WRAM[addr];
|
|
|
|
|
}
|
2011-09-18 02:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-31 14:36:43 +00:00
|
|
|
|
public override void ClockCPU()
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2012-10-31 16:29:26 +00:00
|
|
|
|
if (irq_enabled)
|
2011-09-18 16:14:55 +00:00
|
|
|
|
{
|
2012-10-31 16:29:26 +00:00
|
|
|
|
irq_counter--;
|
|
|
|
|
if (irq_counter == 0x0000)
|
|
|
|
|
{
|
|
|
|
|
IRQSignal = true;
|
|
|
|
|
}
|
2011-09-18 16:14:55 +00:00
|
|
|
|
}
|
2011-09-18 02:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-09-18 16:14:55 +00:00
|
|
|
|
public override byte ReadPRG(int addr)
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2011-09-18 19:29:53 +00:00
|
|
|
|
int bank_16k = addr >> 14;
|
|
|
|
|
int ofs = addr & ((1 << 14) - 1);
|
|
|
|
|
bank_16k = prg_banks_16k[bank_16k];
|
|
|
|
|
addr = (bank_16k << 14) | ofs;
|
2011-09-18 16:14:55 +00:00
|
|
|
|
return ROM[addr];
|
2011-09-18 02:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-18 16:14:55 +00:00
|
|
|
|
int CalcPPUAddress(int addr)
|
2011-09-18 02:49:09 +00:00
|
|
|
|
{
|
2011-09-18 16:14:55 +00:00
|
|
|
|
int bank_1k = addr >> 10;
|
|
|
|
|
int ofs = addr & ((1 << 10) - 1);
|
|
|
|
|
bank_1k = regs[bank_1k];
|
|
|
|
|
bank_1k &= chr_bank_mask_1k;
|
|
|
|
|
return (bank_1k << 10) | ofs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override byte ReadPPU(int addr)
|
|
|
|
|
{
|
|
|
|
|
if (addr < 0x2000)
|
2014-01-19 19:55:32 +00:00
|
|
|
|
{
|
|
|
|
|
if (jump2)
|
|
|
|
|
return VRAM[addr];
|
|
|
|
|
else
|
|
|
|
|
return VROM[CalcPPUAddress(addr)];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return base.ReadPPU(addr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void WritePPU(int addr, byte value)
|
|
|
|
|
{
|
|
|
|
|
if (addr < 0x2000)
|
|
|
|
|
{
|
|
|
|
|
if (jump2)
|
|
|
|
|
VRAM[addr] = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
base.WritePPU(addr, value);
|
|
|
|
|
}
|
2011-09-18 16:14:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-17 19:54:45 +00:00
|
|
|
|
public override byte[] SaveRam
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (eprom != null)
|
|
|
|
|
return eprom.GetSaveRAM();
|
2014-01-19 19:55:32 +00:00
|
|
|
|
else if (jump2)
|
|
|
|
|
{
|
|
|
|
|
return WRAM;
|
|
|
|
|
}
|
2012-12-17 19:54:45 +00:00
|
|
|
|
else
|
2014-01-19 19:55:32 +00:00
|
|
|
|
{
|
2012-12-17 19:54:45 +00:00
|
|
|
|
return null;
|
2014-01-19 19:55:32 +00:00
|
|
|
|
}
|
2012-12-17 19:54:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-18 02:49:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|