VirtualNes.Core核心低版本C#兼容

This commit is contained in:
sin365 2024-12-20 17:24:09 +08:00
parent 3e91952330
commit ad5d2cf35a
12 changed files with 132 additions and 90 deletions

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@ -249,7 +249,7 @@ namespace VirtualNes.Core
LoadDISK();
{
// Pad・ッ・鬣ケトレ、タ、ネウ<EFBFBD>レサッ・ソ・、・゚・<EFBFBD>ー、ャ゚W、、、ホ、ヌ、ウ、ウ、ヌ
// Padクラス内だと初期化タイミングが遅いのでここで
uint crc = rom.GetPROM_CRC();
if (
crc == 0xe792de94 // Best Play - Pro Yakyuu (New) (J)
@ -275,7 +275,7 @@ namespace VirtualNes.Core
LoadTurboFile();
// VS-Unisystem、ホ・ヌ・ユ・ゥ・<EFBFBD>ネヤOカィ
// VS-Unisystemのデフォルト設定
if (rom.IsVSUNISYSTEM())
{
uint crc = rom.GetPROM_CRC();
@ -286,13 +286,13 @@ namespace VirtualNes.Core
Reset();
// ・イゥ`・犹フモミ、ホ・ヌ・ユ・ゥ・<EFBDA9>ネ・ェ・ラ・キ・逾<EFBDA5><E980BE>Oカィ(ヤOカィ諾、ケ瓶、ヒハケ、ヲ樣)
// ゲーム固有のデフォルトオプションを設定(設定戻す時に使う為)
GameOption.defRenderMethod = (int)GetRenderMethod();
GameOption.defIRQtype = GetIrqType();
GameOption.defFrameIRQ = GetFrameIRQmode();
GameOption.defVideoMode = GetVideoMode();
// ヤOカィ、<EFBFBD>愰`・ノ、キ、ニヤOカィ、ケ、<EFBDB9>(・ィ・<EFBDA8>ネ・熙ャ殪、ア、<EFBDB1>ミ・ヌ・ユ・ゥ・<EFBDA9>ネ、ャネ<EFBDAC><EFBE88>)
// 設定をロードして設定する(エントリが無ければデフォルトが入る)
if (rom.GetMapperNo() != 20)
{
GameOption.Load(rom.GetPROM_CRC());
@ -331,12 +331,12 @@ namespace VirtualNes.Core
{
if (fp == null)
{
// xxx ・ユ・。・、・<EFBFBD><EFBFBD>_、ア、゙、サ、<EFBFBD>
// xxx ファイルを開けません
throw new Exception($"Can Not Open File [TurboFile.vtf]");
}
long size = fp.Length;
// ・ユ・。・、・<EFBFBD>オ・、・コネ。オテ
// ファイルサイズ取得
if (size > 32 * 1024)
{
size = 32 * 1024;
@ -354,7 +354,7 @@ namespace VirtualNes.Core
private void LoadDISK()
{
//todo : エナオ忞巐チネ。ヨァウヨ
//todo : 磁碟机读取支持
}
private void LoadSRAM()
@ -400,7 +400,7 @@ namespace VirtualNes.Core
{
int i;
// メモリクリア
// 儊儌儕僋儕傾
MemoryUtility.ZEROMEMORY(MMU.RAM, MMU.RAM.Length);
MemoryUtility.ZEROMEMORY(MMU.WRAM, MMU.WRAM.Length);
MemoryUtility.ZEROMEMORY(MMU.DRAM, MMU.DRAM.Length);
@ -420,11 +420,11 @@ namespace VirtualNes.Core
MMU.PROM = MMU.VROM = null;
// 0 除算防止対策
// 0 彍嶼杊巭懳嶔
MMU.PROM_8K_SIZE = MMU.PROM_16K_SIZE = MMU.PROM_32K_SIZE = 1;
MMU.VROM_1K_SIZE = MMU.VROM_2K_SIZE = MMU.VROM_4K_SIZE = MMU.VROM_8K_SIZE = 1;
// デフォルトバンク設定
// 僨僼僅儖僩僶儞僋愝掕
for (i = 0; i < 8; i++)
{
MMU.CPU_MEM_BANK[i] = null;
@ -432,11 +432,11 @@ namespace VirtualNes.Core
MMU.CPU_MEM_PAGE[i] = 0;
}
// 内臓RAM/WRAM
// 撪憻RAM/WRAM
MMU.SetPROM_Bank(0, MMU.RAM, MMU.BANKTYPE_RAM);
MMU.SetPROM_Bank(3, MMU.WRAM, MMU.BANKTYPE_RAM);
// ダミー
// 僟儈乕
MMU.SetPROM_Bank(1, MMU.XRAM, MMU.BANKTYPE_ROM);
MMU.SetPROM_Bank(2, MMU.XRAM, MMU.BANKTYPE_ROM);
@ -522,7 +522,7 @@ namespace VirtualNes.Core
}
}
}
ppu.ScanlineNext(); // これの位置でラスター系は画面が違う
ppu.ScanlineNext(); // 偙傟偺埵抲偱儔僗僞乕宯偼夋柺偑堘偆
if (RenderMethod == EnumRenderMethod.PRE_ALL_RENDER)
EmulationCPU(nescfg.ScanlineCycles);
@ -583,7 +583,7 @@ namespace VirtualNes.Core
{
pad.VSync();
// VBLANK期間
// VBLANK婜娫
if (scanline == nescfg.TotalScanlines - 1)
{
ppu.VBlankEnd();
@ -642,7 +642,7 @@ namespace VirtualNes.Core
if (scanline == 0)
{
// ダミースキャンライン
// 僟儈乕僗僉儍儞儔僀儞
// H-Draw (4fetches*32)
EmulationCPU(FETCH_CYCLES * 128);
ppu.FrameStart();
@ -655,7 +655,7 @@ namespace VirtualNes.Core
}
else if (scanline < 240)
{
// スクリーン描画(Scanline 1239)
// 僗僋儕乕儞昤夋(Scanline 1乣239)
if (bDraw)
{
ppu.Scanline(scanline, Supporter.Config.graphics.bAllSprite, Supporter.Config.graphics.bLeftClip);
@ -707,7 +707,7 @@ namespace VirtualNes.Core
}
else if (scanline == 240)
{
// ダミースキャンライン (Scanline 240)
// 僟儈乕僗僉儍儞儔僀儞 (Scanline 240)
mapper.VSync();
EmulationCPU(nescfg.HDrawCycles);
@ -720,7 +720,7 @@ namespace VirtualNes.Core
{
pad.VSync();
// VBLANK期間
// VBLANK婜娫
if (scanline == nescfg.TotalScanlines - 1)
{
ppu.VBlankEnd();
@ -864,7 +864,7 @@ namespace VirtualNes.Core
MMU.VROM_4K_SIZE = rom.GetVROM_SIZE() * 2;
MMU.VROM_8K_SIZE = rom.GetVROM_SIZE();
// デフォルトバンク
// 僨僼僅儖僩僶儞僋
if (MMU.VROM_8K_SIZE != 0)
{
MMU.SetVROM_8K_Bank(0);
@ -874,7 +874,7 @@ namespace VirtualNes.Core
MMU.SetCRAM_8K_Bank(0);
}
// ミラー
// 儈儔乕
if (rom.Is4SCREEN())
{
MMU.SetVRAM_Mirror(MMU.VRAM_MIRROR4);
@ -976,13 +976,13 @@ namespace VirtualNes.Core
reg.S = 0xFF;
reg.P = CPU.Z_FLAG | CPU.R_FLAG | CPU.I_FLAG;
// 安全対策を兼ねてあえてループに(1秒分)
// 埨慡懳嶔傪寭偹偰偁偊偰儖乕僾偵(1昩暘)
for (int i = 0; i < nescfg.TotalScanlines * 60; i++)
{
EmulationCPU(nescfg.ScanlineCycles);
cpu.GetContext(ref reg);
// 無限ループに入ったことを確認したら抜ける
// 柍尷儖乕僾偵擖偭偨偙偲傪妋擣偟偨傜敳偗傞
if (reg.PC == 0x4700)
{
break;
@ -993,7 +993,7 @@ namespace VirtualNes.Core
}
cpu.GetContext(ref reg);
// 無限ループに入っていたら再設定する
// 柍尷儖乕僾偵擖偭偰偄偨傜嵞愝掕偡傞
if (reg.PC == 0x4700)
{
reg.PC = 0x4720; // Play Address
@ -1009,7 +1009,7 @@ namespace VirtualNes.Core
else
{
cpu.GetContext(ref reg);
reg.PC = 0x4700; // 無限ループ
reg.PC = 0x4700; // 柍尷儖乕僾
reg.S = 0xFF;
EmulationCPU(nescfg.ScanlineCycles * nescfg.TotalScanlines);
@ -1186,7 +1186,7 @@ namespace VirtualNes.Core
if (m_BarcodeCycles > 1000)
{
m_BarcodeCycles = 0;
// 停止?
// 掆巭丠
if (m_BarcodeData[m_BarcodePtr] != 0xFF)
{
m_BarcodeOut = m_BarcodeData[m_BarcodePtr++];
@ -1227,7 +1227,7 @@ namespace VirtualNes.Core
return;
m_TapeCycles += (nescfg.CpuClock / 32000.0);
// m_TapeCycles += (nescfg.CpuClock / 22050.0); // 遅すぎてダメっぽい
// m_TapeCycles += (nescfg.CpuClock / 22050.0); // 抶偡偓偰僟儊偭傐偄
if (m_bTapePlay)
{
@ -1293,7 +1293,7 @@ namespace VirtualNes.Core
return MMU.CPU_MEM_BANK[addr >> 13][addr & 0x1FFF];
}
return 0x00; // Warning予防
return 0x00; // Warning梊杊
}
private byte ReadReg(ushort addr)
@ -1504,7 +1504,7 @@ namespace VirtualNes.Core
pad.Write(addr, data);
apu.Write(addr, data);
break;
// VirtuaNES固有ポート
// VirtuaNES屌桳億乕僩
case 0x18:
apu.Write(addr, data);
break;
@ -1596,7 +1596,7 @@ namespace VirtualNes.Core
ref state.reg.cpureg.FrameIRQ_type,
ref state.reg.cpureg.FrameIRQ,
ref state.reg.cpureg.FrameIRQ_occur);
state.reg.cpureg.FrameIRQ_cycles = cycles; // イホユユ、ャINT、ハ樣」ィ、ゥ
state.reg.cpureg.FrameIRQ_cycles = cycles; // 参照がINTな為
state.reg.cpureg.DMA_cycles = cpu.GetDmaCycles();
state.reg.cpureg.emul_cycles = emul_cycles;
@ -1625,7 +1625,7 @@ namespace VirtualNes.Core
MemoryUtility.memcpy(state.ram.SPPAL, MMU.SPPAL, state.ram.SPPAL.Length);
MemoryUtility.memcpy(state.ram.SPRAM, MMU.SPRAM, state.ram.SPRAM.Length);
// S-RAM STATE(ハケモテ/ホエハケモテ、ヒ騅、<E9A885>鬢コエ贇レ、ケ、<EFBDB9>ミ・サゥ`・ヨ、ケ、<EFBDB9>)
// S-RAM STATE(使用/未使用に関わらず存在すればセーブする)
if (rom.IsSAVERAM())
{
size = (uint)SAVERAM_SIZE;
@ -1649,9 +1649,9 @@ namespace VirtualNes.Core
uint size = 0;
// SAVE CPU MEMORY BANK DATA
// BANK0,1,2、マ・ミ・<EFBFBD>ッ・サゥ`・ヨ、ヒ騅4、ハ、キ
// VirtuaNES0.30、ォ、<EFBFBD>
// ・ミ・<EFBFBD>ッ」ウ、マSRAMハケモテ、ヒ騅、<EFBFBD>鬢コ・サゥ`・ヨ
// BANK0,1,2はバンクセーブに関係なし
// VirtuaNES0.30から
// バンクはSRAM使用に関わらずセーブ
for (int i = 3; i < 8; i++)
{
state.mmu.CPU_MEM_TYPE[i] = MMU.CPU_MEM_TYPE[i];
@ -1696,7 +1696,7 @@ namespace VirtualNes.Core
}
}
// WRITE VRAM MEMORY(ウ」、ヒ4Kキヨ、ケ、ル、ニ抱、ュ゙z、<EFBFBD>)
// WRITE VRAM MEMORY(常に4K分すべて書き込む)
state.VRAM = new byte[4 * 1024];
Array.Copy(MMU.VRAM, state.VRAM, state.VRAM.Length);
@ -1764,7 +1764,7 @@ namespace VirtualNes.Core
int DiskSize = 16 + 65500 * rom.GetDiskNo();
// マ珀`ハ<><EFBFBD>ォ・ヲ・<EFBDA6>
// 相違数をカウント
for (int i = 16; i < DiskSize; i++)
{
if (lpWrite[i] != 0)
@ -1874,9 +1874,9 @@ namespace VirtualNes.Core
//BANK STATE
{
// SAVE CPU MEMORY BANK DATA
// BANK0,1,2、マ・ミ・<EFBFBD>ッ・サゥ`・ヨ、ヒ騅4、ハ、キ
// VirtuaNES0.30、ォ、<EFBFBD>
// ・ミ・<EFBFBD>ッ」ウ、マSRAMハケモテ、ヒ騅、<EFBFBD>鬢コ・サゥ`・ヨ
// BANK0,1,2はバンクセーブに関係なし
// VirtuaNES0.30から
// バンクはSRAM使用に関わらずセーブ
for (byte i = 3; i < 8; i++)
{
MMU.CPU_MEM_TYPE[i] = state.mmu.CPU_MEM_TYPE[i];

View File

@ -2,7 +2,7 @@
{
public struct BLOCKHDR : IStateBufferObject
{
public bool Valid => !string.IsNullOrEmpty(ID);
public bool Valid => !string.IsNullOrEmpty(ID);
/// <summary> 总是8个字节 </summary>
public string ID;
public ushort Reserved;
@ -11,12 +11,12 @@
public uint GetSize()
public uint GetSize()
{
return (uint)(8 + sizeof(ushort) + sizeof(ushort) + sizeof(uint));
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
if (Valid)
{

View File

@ -8,12 +8,12 @@
public uint pad4bit;
public byte strobe;
public uint GetSize()
public uint GetSize()
{
return sizeof(uint) * 4 + sizeof(byte);
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(pad1bit);
buffer.Write(pad2bit);

View File

@ -4,12 +4,12 @@
{
public uint data;
public uint GetSize()
public uint GetSize()
{
return sizeof(uint);
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(data);
}

View File

@ -14,12 +14,12 @@
public uint GetSize()
public uint GetSize()
{
return (uint)(ID.Length + sizeof(ushort) + sizeof(uint) + sizeof(ushort) + sizeof(ushort));
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(ID);
buffer.Write(BlockVersion);

View File

@ -9,12 +9,12 @@
return new MMCSTAT() { mmcdata = new byte[256] };
}
public uint GetSize()
public uint GetSize()
{
return 256;
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(mmcdata);
}

View File

@ -22,12 +22,12 @@ namespace VirtualNes.Core
return res;
}
public uint GetSize()
public uint GetSize()
{
return (uint)(RAM.Length + BGPAL.Length + SPPAL.Length + SPRAM.Length);
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(RAM);
buffer.Write(BGPAL);

View File

@ -7,12 +7,12 @@
public uint GetSize()
public uint GetSize()
{
return cpureg.GetSize() + ppureg.GetSize();
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
cpureg.SaveState(buffer);
ppureg.SaveState(buffer);
@ -45,12 +45,12 @@
public long emul_cycles;
public long base_cycles;
public uint GetSize()
public uint GetSize()
{
return 32;
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(PC);
buffer.Write(A);
@ -102,12 +102,12 @@
public ushort loopy_v;
public ushort loopy_x;
public uint GetSize()
public uint GetSize()
{
return 12;
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(reg0);
buffer.Write(reg1);

View File

@ -9,12 +9,12 @@
return new SNDSTAT() { snddata = new byte[0x800] };
}
public uint GetSize()
public uint GetSize()
{
return (uint)snddata.Length;
}
public void SaveState(StateBuffer buffer)
public void SaveState(StateBuffer buffer)
{
buffer.Write(snddata);
}

View File

@ -36,7 +36,7 @@ namespace VirtualNes.Core
public BLOCKHDR exctrBLOCK;
public EXCTRSTAT exctr;
public byte[] ToBytes()
public byte[] ToBytes()
{
StateBuffer buffer = new StateBuffer();

View File

@ -2,7 +2,7 @@
namespace VirtualNes.Core
{
public struct ControllerState
public struct ControllerState : IEquatable<ControllerState>
{
public uint raw0;
public uint raw1;
@ -11,19 +11,55 @@ namespace VirtualNes.Core
public bool valid;
public ControllerState(
EnumButtonType player0_buttons,
EnumButtonType player1_buttons,
EnumButtonType player2_buttons,
EnumButtonType player3_buttons)
public ControllerState(EnumButtonType[] states)
{
raw0 = (uint)player0_buttons;
raw1 = (uint)player1_buttons;
raw2 = (uint)player2_buttons;
raw3 = (uint)player3_buttons;
raw0 = (uint)states[0];
raw1 = (uint)states[1];
raw2 = (uint)states[2];
raw3 = (uint)states[3];
valid = true;
}
public bool HasButton(int player, EnumButtonType button)
{
uint raw;
switch (player)
{
case 0: raw = raw0; break;
case 1: raw = raw1; break;
case 2: raw = raw2; break;
case 3: raw = raw3; break;
default:
raw = 0;
break;
}
return (raw & (uint)button) == (uint)button;
}
public override string ToString()
{
return $"{raw0}|{raw1}|{raw2}|{raw3}";
}
#region Impl_Equals
public bool Equals(ControllerState other)
{
return raw0 == other.raw0 && raw1 == other.raw1 && raw2 == other.raw2 && raw3 == other.raw3 && valid == other.valid;
}
public override bool Equals(object obj)
{
return obj is ControllerState other && Equals(other);
}
public override int GetHashCode()
{
//return HashCode.Combine(raw0, raw1, raw2, raw3, valid);
return raw0.GetHashCode()+ raw1.GetHashCode()+ raw2.GetHashCode()+ raw3.GetHashCode();
}
public static bool operator ==(ControllerState left, ControllerState right)
{
return
@ -37,24 +73,7 @@ namespace VirtualNes.Core
{
return !(left == right);
}
public override string ToString()
{
return $"{raw0}|{raw1}|{raw2}|{raw3}";
}
public bool HasButton(int player, EnumButtonType button)
{
uint raw = 0;
switch (player)
{
case 0: raw = raw0; break;
case 1: raw = raw1; break;
case 2: raw = raw2; break;
case 3: raw = raw3; break;
}
return (raw & (uint)button) == (uint)button;
}
#endregion
}
[Flags]

View File

@ -5,6 +5,7 @@ namespace VirtualNes.Core
public static class Supporter
{
private static ISupporterImpl s_support;
public static void Setup(ISupporterImpl supporter)
{
s_support = supporter;
@ -44,6 +45,7 @@ namespace VirtualNes.Core
{
s_support.SaveFile(fileData, directPath, fileName);
}
public static Stream OpenFile(string directPath, string fileName)
{
return s_support.OpenFile(directPath, fileName);
@ -64,6 +66,11 @@ namespace VirtualNes.Core
s_support.SampleInput(frameCount);
}
public static IControllerSetuper GetControllerSetuper()
{
return s_support.GetControllerSetuper();
}
public static EmulatorConfig Config => s_support.Config;
}
@ -82,5 +89,21 @@ namespace VirtualNes.Core
bool TryGetMapperNo(ROM rom, out int mapperNo);
ControllerState GetControllerState();
void SampleInput(uint frameCount);
IControllerSetuper GetControllerSetuper();
}
/// <summary>
/// 负责管理本地控制器与具体游戏之间的槽位分配
/// </summary>
public interface IControllerSetuper
{
/// <summary>
/// 设置本地手柄与游戏手柄槽位的映射
/// </summary>
void SetConnect(
uint? con0ToSlot = null,
uint? con1ToSlot = null,
uint? con2ToSlot = null,
uint? con3ToSlot = null);
}
}