[NES] fix some interrupt-related bugs to pass more cpu tests, add reset handling, improve iNES header handling some more

This commit is contained in:
zeromus 2011-03-20 02:12:10 +00:00
parent 432dc26f14
commit f7bf5bdd17
20 changed files with 108 additions and 89 deletions

View File

@ -61,6 +61,7 @@ TriggerException(ExceptionType.BRK);
PendingCycles -= 5; TotalExecutedCycles += 5;
break;
case 0x08: // PHP
//FlagB = true; //why would it do this?? how weird
WriteMemory((ushort)(S-- + 0x100), P);
PendingCycles -= 3; TotalExecutedCycles += 3;
break;
@ -1204,8 +1205,10 @@ FlagT = true;// this seems wrong
P = (byte)((P & 0x7D) | TableNZ[value8]);
PendingCycles -= 7; TotalExecutedCycles += 7;
break;
default:
throw new Exception(String.Format("Unhandled opcode: {0:X2}", opcode));
default:
if(throw_unhandled)
throw new Exception(String.Format("Unhandled opcode: {0:X2}", opcode));
break;
}
}
}

View File

@ -26,6 +26,7 @@ namespace BizHawk.Emulation.CPUs.M6502
}*/
public bool debug;
public bool throw_unhandled;
public void Reset()
{
@ -39,11 +40,6 @@ namespace BizHawk.Emulation.CPUs.M6502
TotalExecutedCycles = 0;
}
public void ResetPC()
{
PC = ReadWord(0xFFFE);
}
public string State()
{
int notused;
@ -61,10 +57,10 @@ namespace BizHawk.Emulation.CPUs.M6502
return val;
}
private const ushort NMIVector = 0xFFFA;
private const ushort ResetVector = 0xFFFC;
private const ushort BRKVector = 0xFFFE;
private const ushort IRQVector = 0xFFFE;
public const ushort NMIVector = 0xFFFA;
public const ushort ResetVector = 0xFFFC;
public const ushort BRKVector = 0xFFFE;
public const ushort IRQVector = 0xFFFE;
enum ExceptionType
{
@ -73,13 +69,12 @@ namespace BizHawk.Emulation.CPUs.M6502
void TriggerException(ExceptionType type)
{
if (type == ExceptionType.BRK)
PC++;
WriteMemory((ushort)(S-- + 0x100), (byte)(PC >> 8));
WriteMemory((ushort)(S-- + 0x100), (byte)PC);
byte oldP = P;
FlagB = false;
FlagT = true;
FlagB = type == ExceptionType.BRK;
WriteMemory((ushort)(S-- + 0x100), P);
P = oldP;
FlagI = true;
switch (type)
{
@ -174,7 +169,7 @@ namespace BizHawk.Emulation.CPUs.M6502
}
/// <summary>Interrupt Disable Flag</summary>
private bool FlagI
public bool FlagI
{
get { return (P & 0x04) != 0; }
set { P = (byte)((P & ~0x04) | (value ? 0x04 : 0x00)); }
@ -227,7 +222,7 @@ namespace BizHawk.Emulation.CPUs.M6502
WriteMemory = null;
}
private ushort ReadWord(ushort address)
public ushort ReadWord(ushort address)
{
byte l = ReadMemory(address);
byte h = ReadMemory(++address);

View File

@ -12,7 +12,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
public interface INESBoard
{
void Create(NES nes);
bool Configure();
bool Configure(NES.EDetectionOrigin origin);
byte ReadPRG(int addr);
byte ReadPPU(int addr); byte PeekPPU(int addr);
byte ReadPRAM(int addr);
@ -38,7 +38,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
{
this.NES = nes;
}
public abstract bool Configure();
public abstract bool Configure(NES.EDetectionOrigin origin);
public CartInfo Cart { get { return NES.cart; } }
public NES NES { get; set; }
@ -118,6 +118,8 @@ namespace BizHawk.Emulation.Consoles.Nintendo
{
if (addr < 0x2000)
{
if (VRAM != null)
VRAM[addr] = value;
}
else
{
@ -131,7 +133,9 @@ namespace BizHawk.Emulation.Consoles.Nintendo
{
if (addr < 0x2000)
{
return VROM[addr];
if (VROM != null)
return VROM[addr];
else return VRAM[addr];
}
else
{
@ -222,7 +226,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
/// <summary>
/// finds a board class which can handle the provided cart
/// </summary>
static Type FindBoard(CartInfo cart)
static Type FindBoard(CartInfo cart, EDetectionOrigin origin)
{
NES nes = new NES();
nes.cart = cart;
@ -230,7 +234,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
{
INESBoard board = (INESBoard)Activator.CreateInstance(type);
board.Create(nes);
if (board.Configure())
if (board.Configure(origin))
return type;
}
return null;

View File

@ -16,7 +16,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
int prg;
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//configure
switch (Cart.board_type)

View File

@ -12,7 +12,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
int chr;
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//configure
switch (Cart.board_type)

View File

@ -21,7 +21,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
int chr;
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//configure
switch (Cart.board_type)

View File

@ -22,7 +22,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
int prg, chr;
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//configure
switch (Cart.board_type)

View File

@ -18,7 +18,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
int prg, chr;
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
switch (Cart.board_type)
{

View File

@ -11,14 +11,15 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
//(none)
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//configure
//configure.
//contrary to expectations, some NROM games may have WRAM if theyve been identified through iNES. lame.
switch (Cart.board_type)
{
case "HVC-NROM-256": //super mario bros.
case "NES-NROM-256": //10 yard fight
AssertPrg(32); AssertChr(8); AssertVram(0); AssertWram(0);
AssertPrg(32); AssertChr(8); AssertVram(0); AssertWram(0,8);
break;
case "HVC-RROM": //balloon fight
@ -27,12 +28,17 @@ namespace BizHawk.Emulation.Consoles.Nintendo
case "KONAMI-NROM-128":
case "NES-NROM-128":
case "NAMCOT-3301":
AssertPrg(16); AssertChr(8); AssertVram(0); AssertWram(0);
AssertPrg(16); AssertChr(8); AssertVram(0); AssertWram(0,8);
break;
case "NROM-HOMEBREW":
//whatever. who knows.
break;
default:
return false;
}
if (origin != NES.EDetectionOrigin.INES) AssertWram(0);
prg_byte_mask = (Cart.prg_size*1024) - 1;
SetMirrorType(Cart.pad_h, Cart.pad_v);

View File

@ -256,7 +256,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
}
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//analyze board type
switch (Cart.board_type)

View File

@ -23,7 +23,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//state
int prg;
public override bool Configure()
public override bool Configure(NES.EDetectionOrigin origin)
{
//configure
switch (Cart.board_type)

View File

@ -56,18 +56,31 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//in this emulator, reset takes place instantaneously
cpu.PC = (ushort)(ReadMemory(0xFFFC) | (ReadMemory(0xFFFD) << 8));
cpu.P = 0x34;
cpu.S = 0xFD;
//cpu.debug = true;
}
bool resetSignal;
public void FrameAdvance(bool render)
{
Controller.UpdateControls(Frame++);
if (resetSignal)
Controller.UnpressButton("Reset");
resetSignal = Controller["Reset"];
ppu.FrameAdvance();
}
protected void RunCpu(int ppu_cycles)
{
if (resetSignal)
{
cpu.PC = cpu.ReadWord(MOS6502.ResetVector);
apu.WriteReg(0x4015, 0);
cpu.FlagI = true;
}
int cycles = ppu_cycles;
if (ppu.PAL)
cycles *= 15;
@ -233,6 +246,10 @@ namespace BizHawk.Emulation.Consoles.Nintendo
public void WriteMemory(ushort addr, byte value)
{
if (addr >= 0x6000 && addr < 0x6fff)
{
int zzz = 9;
}
if (addr < 0x0800) ram[addr] = value;
else if (addr < 0x1000) ram[addr - 0x0800] = value;
else if (addr < 0x1800) ram[addr - 0x1000] = value;

View File

@ -13,7 +13,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
public partial class NES : IEmulator
{
static readonly bool USE_DATABASE = true;
static readonly bool USE_DATABASE = false;
//Game issues:
//3-D World Runner - UNROM - weird lines in gameplay (scanlines off?)
@ -188,6 +188,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
value <<= 1;
value |= nes.Controller.IsPressed(str) ? 1 : 0;
}
//Console.WriteLine("STROBE");
}
public override void Write(int value)
{
@ -373,7 +374,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
public string GameName { get { return game_name; } }
enum EDetectionOrigin
public enum EDetectionOrigin
{
None, BootGodDB, GameDB, INES
}
@ -447,27 +448,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//find a INESBoard to handle this
Type boardType = null;
bool iNES_tryAgain = false;
try
{
boardType = FindBoard(choice);
if (boardType == null)
iNES_tryAgain = true;
}
catch(Exception)
{
if (origin == EDetectionOrigin.INES)
iNES_tryAgain = true;
else throw;
}
if (iNES_tryAgain)
{
//try again with a different wram size.. because iNES sucks that way
choice.wram_size = 8;
Console.WriteLine("Trying classification again with iNES wram adjustment. new parameters:");
Console.WriteLine(choice);
boardType = FindBoard(choice);
}
boardType = FindBoard(choice,EDetectionOrigin.INES);
if (boardType == null)
throw new Exception("No class implements the necessary board type: " + choice.board_type);
@ -475,7 +456,7 @@ namespace BizHawk.Emulation.Consoles.Nintendo
cart = choice;
board.Create(this);
board.Configure();
board.Configure(origin);
//create the board's rom and vrom
board.ROM = new byte[choice.prg_size * 1024];

View File

@ -49,20 +49,24 @@ namespace BizHawk.Emulation.Consoles.Nintendo
//MAP PRG CHR WRAM VRAM BOARD
static string ClassifyTable = @"
0 16 8 0 0 NES-NROM-128; balloon fight, but its broken right now
0 32 8 0 0 NES-NROM-256; super mario bros
1 32 32 0 0 NES-SEROM; lolo
0 16 0 8 8 NROM-HOMEBREW; some of blargg's test (sprite tests)
0 16 8 8 0 NES-NROM-128; balloon fight, but its broken right now
0 32 8 8 0 NES-NROM-256; super mario bros
1 32 32 8 0 NES-SEROM; lolo
1 128 0 8 0 NES-SNROM; zelda
2 128 0 0 0 NES-UNROM; mega man
2 256 0 0 0 NES-UOROM; paperboy 2
3 32 32 0 0 NES-CNROM; adventure island
7 128 0 0 0 NES-ANROM; marble madness
7 256 0 0 8 NES-AOROM; battletoads
11 32 16 0 0 Discrete_74x377
11 16 32 0 0 Discrete_74x377
13 32 0 0 16 NES-CPROM; videomation
66 64 16 0 0 NES-MHROM; super mario bros / duck hunt
66 128 32 0 0 NES-GNROM; gumshoe
1 128 128 8 0 NES-SKROM; zelda 2
1 128 0 8 8 NES-SNROM; some of blargg's tests (apu)
1 256 0 8 8 NES-SNROM; some of blargg's test (cpu tests)
2 128 0 8 0 NES-UNROM; mega man
2 256 0 8 0 NES-UOROM; paperboy 2
3 32 32 8 0 NES-CNROM; adventure island
7 128 0 8 0 NES-ANROM; marble madness
7 256 0 8 8 NES-AOROM; battletoads
11 32 16 8 0 Discrete_74x377
11 16 32 8 0 Discrete_74x377
13 32 0 8 16 NES-CPROM; videomation
66 64 16 8 0 NES-MHROM; super mario bros / duck hunt
66 128 32 8 0 NES-GNROM; gumshoe
";
}
@ -73,7 +77,10 @@ static string ClassifyTable = @"
public byte VROM_size;
public byte ROM_type;
public byte ROM_type2;
public fixed byte reserve[8];
public byte wram_size;
public byte flags9, flags10;
public byte zero11, zero12, zero13, zero14, zero15;
public bool CheckID()
{
@ -123,9 +130,17 @@ static string ClassifyTable = @"
ret.chr_size = (short)(VROM_size * 8);
ret.wram_battery = (ROM_type & 2) != 0;
fixed (iNES_HEADER* self = &this) ret.wram_size = (short)(self->reserve[0] * 8);
//0 is supposed to mean 1 (for compatibility, as this is an extension to original iNES format)
//but we'll try using 8 later if it doesn't work with 0
if(wram_size != 0 || flags9 != 0 || flags10 != 0 || zero11 != 0 || zero12 != 0 || zero13 != 0 || zero14 != 0 || zero15 != 0)
{
Console.WriteLine("Looks like you have an iNES 2.0 header, or some other kind of weird garbage.");
Console.WriteLine("We haven't bothered to support iNES 2.0.");
Console.WriteLine("We might, if we can find anyone who uses it. Let us know.");
}
ret.wram_size = (short)(wram_size * 8);
//0 is supposed to mean 8KB (for compatibility, as this is an extension to original iNES format)
if (ret.wram_size == 0)
ret.wram_size = 8;
//iNES wants us to assume that no chr -> 8KB vram
if (ret.chr_size == 0) ret.vram_size = 8;

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{DD448B37-BA3F-4544-9754-5406E8094723}</ProjectGuid>
<OutputType>Exe</OutputType>
@ -399,6 +399,7 @@
<None Include="images\Refresh.bmp" />
<None Include="images\TruncateFromRW.png" />
<None Include="images\TruncateFromFile.png" />
<None Include="output\config.ini" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -181,6 +181,7 @@
//GameBoy Settings
public NESControllerTemplate GameBoyController = new NESControllerTemplate(true);
public string NESReset = "Tab";
public NESControllerTemplate[] NESController = new NESControllerTemplate[4];
}

View File

@ -309,6 +309,7 @@ namespace BizHawk.MultiClient
Global.PCEControls = pceControls;
var nesControls = new Controller(NES.NESController);
nesControls.BindMulti("Reset", Global.Config.NESReset);
for (int i = 0; i < 1 /*TODO*/; i++)
{
nesControls.BindMulti("Up", Global.Config.NESController[i].Up);

View File

@ -1,12 +0,0 @@
;blargg's tests
md5:FCDA199AE2DD1824F377AC3EC40E68CD blargg/sprite_hit_tests_2005.10.05/01.basics.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:8BE5342D1A1F5E63602B0B2AD4D3E957 blargg/sprite_hit_tests_2005.10.05/02.alignment.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:E7F1424401BA5C3484F10CB6F9E7328F blargg/sprite_hit_tests_2005.10.05/03.corners.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:64836B8BAA2D19AE0F41F8F00390478A blargg/sprite_hit_tests_2005.10.05/04.flip.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:99E18274B94CB11D0F67984D36B476BB blargg/sprite_hit_tests_2005.10.05/05.left_clip.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:9ADB96106B2D57D92A22CC67ACA43963 blargg/sprite_hit_tests_2005.10.05/06.right_edge.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:8D917EFB12AA4C6D08BE86C809A68F4A blargg/sprite_hit_tests_2005.10.05/07.screen_bottom.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:57DE8767DAB3F8A150EA5C63B374F825 blargg/sprite_hit_tests_2005.10.05/08.double_height.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:594E8568C4C621EC95A6390D559CA504 blargg/sprite_hit_tests_2005.10.05/09.timing_basics.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:7B42B8672F35068E96EB1D4D2CA1A33B blargg/sprite_hit_tests_2005.10.05/10.timing_order.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8
md5:55AD5FFC60CA9AEF60FEB8AEC900214E blargg/sprite_hit_tests_2005.10.05/11.edge_timing.nes NES board=NES-SNROM;PRG=16;VRAM=8;WRAM=8

View File

@ -247,6 +247,10 @@ namespace M6502
Set(0xDC, "NOP", AddrMode.IndirectX, 4);
Set(0xFC, "NOP", AddrMode.IndirectX, 4);
//undocumented opcodes
//RLA:
//Set(0x23, "RLA", AddrMode.IndirectX, 8);
// Bitwise OR with Accumulator
Set(0x09, "ORA", AddrMode.Immediate, 2);
Set(0x05, "ORA", AddrMode.ZeroPage , 2);
@ -384,8 +388,10 @@ namespace M6502
EmulateOpcode(w, i);
}
w.WriteLine(" default:");
w.WriteLine(" throw new Exception(String.Format(\"Unhandled opcode: {0:X2}\", opcode));");
w.WriteLine(" default:");
w.WriteLine(" if(throw_unhandled)");
w.WriteLine(" throw new Exception(String.Format(\"Unhandled opcode: {0:X2}\", opcode));");
w.WriteLine(" break;");
w.WriteLine(" }");
w.WriteLine(" }");
w.WriteLine(" }");

View File

@ -230,6 +230,7 @@ namespace M6502
private void PHP(OpcodeInfo op, TextWriter w)
{
w.WriteLine(Spaces + "FlagB = true; //why would it do this?? how weird");
w.WriteLine(Spaces + "WriteMemory((ushort)(S-- + 0x100), P);");
w.WriteLine(Spaces + "PendingCycles -= {0}; TotalExecutedCycles += {0};", op.Cycles);
}