211 lines
5.4 KiB
C#
211 lines
5.4 KiB
C#
|
using AxibugEmuOnline.Client.ClientCore;
|
|||
|
using System;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.IO;
|
|||
|
using System.Xml.Linq;
|
|||
|
using UnityEngine;
|
|||
|
using VirtualNes.Core;
|
|||
|
using VirtualNes.Core.Debug;
|
|||
|
|
|||
|
namespace AxibugEmuOnline.Client
|
|||
|
{
|
|||
|
public class NesEmulator : MonoBehaviour, IEmuCore
|
|||
|
{
|
|||
|
public EnumPlatform Platform => EnumPlatform.NES;
|
|||
|
|
|||
|
//ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
public NES NesCore { get; private set; }
|
|||
|
|
|||
|
//<2F><>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Unity<74><79><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵIJ<DDB5><C4B2><EFBFBD>Ⱦ<EFBFBD><C8BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>
|
|||
|
public VideoProvider VideoProvider;
|
|||
|
//<2F><>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Unity<74><79><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>ݵIJ<DDB5><C4B2><EFBFBD><EFBFBD>ų<EFBFBD><C5B3><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>
|
|||
|
public AudioProvider AudioProvider;
|
|||
|
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ͣ
|
|||
|
private bool m_bPause;
|
|||
|
/// <summary> <20>Ƿ<EFBFBD><C7B7><EFBFBD>ͣ </summary>
|
|||
|
public bool IsPause => m_bPause;
|
|||
|
|
|||
|
private void Start()
|
|||
|
{
|
|||
|
//<2F>رմ<D8B1>ֱͬ<D6B1><CDAC>
|
|||
|
QualitySettings.vSyncCount = 0;
|
|||
|
//<2F><>Ϊ60֡
|
|||
|
Application.targetFrameRate = 60;
|
|||
|
VideoProvider.NesEmu = this;
|
|||
|
AudioProvider.NesEmu = this;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// ָ<><D6B8>ROM<4F><4D>ʼ<EFBFBD><CABC>Ϸ
|
|||
|
/// </summary>
|
|||
|
/// <param name="rom"></param>
|
|||
|
public void StartGame(RomFile rom)
|
|||
|
{
|
|||
|
StopGame();
|
|||
|
|
|||
|
Supporter.Setup(new CoreSupporter());
|
|||
|
Debuger.Setup(new CoreDebuger());
|
|||
|
|
|||
|
App.nesRomLib.AddRomFile(rom);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
NesCore = new NES(rom.FileName);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
NesCore = null;
|
|||
|
App.log.Error(ex.ToString());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// ֹͣ<CDA3><D6B9>Ϸ
|
|||
|
/// </summary>
|
|||
|
public void StopGame()
|
|||
|
{
|
|||
|
NesCore?.Dispose();
|
|||
|
NesCore = null;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Unity<74><79><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
private unsafe void Update()
|
|||
|
{
|
|||
|
if (m_bPause) return;
|
|||
|
|
|||
|
if (NesCore != null)
|
|||
|
{
|
|||
|
PushEmulatorFrame();
|
|||
|
if (InGameUI.Instance.IsNetPlay)
|
|||
|
FixEmulatorFrame();
|
|||
|
|
|||
|
var screenBuffer = NesCore.ppu.GetScreenPtr();
|
|||
|
VideoProvider.SetDrawData(screenBuffer);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
|
|||
|
private void FixEmulatorFrame()
|
|||
|
{
|
|||
|
var skipFrameCount = App.roomMgr.netReplay.GetSkipFrameCount();
|
|||
|
|
|||
|
if (skipFrameCount > 0) App.log.Debug($"SKIP FRAME : {skipFrameCount}");
|
|||
|
for (int i = 0; i < skipFrameCount; i++)
|
|||
|
{
|
|||
|
if (!PushEmulatorFrame()) break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
ControllerState lastState;
|
|||
|
//<2F>ƽ<EFBFBD>֡
|
|||
|
private bool PushEmulatorFrame()
|
|||
|
{
|
|||
|
Supporter.SampleInput(NesCore.FrameCount);
|
|||
|
var controlState = Supporter.GetControllerState();
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>δ<EFBFBD>յ<EFBFBD>Input<75><74><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD>ƽ<EFBFBD>
|
|||
|
if (!controlState.valid) return false;
|
|||
|
|
|||
|
#if UNITY_EDITOR
|
|||
|
if (controlState != lastState)
|
|||
|
{
|
|||
|
App.log.Info($"[LOCALDEBUG]{NesCore.FrameCount}-->{controlState}");
|
|||
|
}
|
|||
|
#endif
|
|||
|
|
|||
|
NesCore.pad.Sync(controlState);
|
|||
|
NesCore.EmulateFrame(true);
|
|||
|
|
|||
|
lastState = controlState;
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
public void Pause()
|
|||
|
{
|
|||
|
m_bPause = true;
|
|||
|
}
|
|||
|
|
|||
|
public void Resume()
|
|||
|
{
|
|||
|
m_bPause = false;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public void DoReset()
|
|||
|
{
|
|||
|
NesCore.Reset();
|
|||
|
}
|
|||
|
|
|||
|
public void SetupScheme()
|
|||
|
{
|
|||
|
CommandDispatcher.Instance.Current = CommandDispatcher.Instance.Gaming;
|
|||
|
}
|
|||
|
|
|||
|
public void LoadState(object state)
|
|||
|
{
|
|||
|
NesCore.LoadState((State)state);
|
|||
|
}
|
|||
|
|
|||
|
public object GetState()
|
|||
|
{
|
|||
|
return NesCore.GetState();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>浵
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public byte[] GetStateBytes()
|
|||
|
{
|
|||
|
return NesCore.GetState().ToBytes();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// <20><><EFBFBD>ؼ<EFBFBD>ʱ<EFBFBD>浵
|
|||
|
/// </summary>
|
|||
|
/// <param name="data"></param>
|
|||
|
public void LoadStateFromBytes(byte[] data)
|
|||
|
{
|
|||
|
State st = new State();
|
|||
|
st.FromByte(data);
|
|||
|
NesCore.LoadState(st);
|
|||
|
}
|
|||
|
|
|||
|
public uint Frame => NesCore.FrameCount;
|
|||
|
|
|||
|
#if UNITY_EDITOR
|
|||
|
/// <summary>
|
|||
|
/// <20>༭<EFBFBD><E0BCAD><EFBFBD><EFBFBD>
|
|||
|
/// </summary>
|
|||
|
[Conditional("UNITY_EDITOR")]
|
|||
|
[ContextMenu("ImportNesDB")]
|
|||
|
public void ImportNesDB()
|
|||
|
{
|
|||
|
var db = Resources.Load<RomDB>("NES/ROMDB");
|
|||
|
db.Clear();
|
|||
|
|
|||
|
var xmlStr = File.ReadAllText("nes20db.xml");
|
|||
|
var xml = XDocument.Parse(xmlStr);
|
|||
|
var games = xml.Element("nes20db").Elements("game");
|
|||
|
foreach (var game in games)
|
|||
|
{
|
|||
|
var crcStr = game.Element("rom").Attribute("crc32").Value;
|
|||
|
var crc = uint.Parse($"{crcStr}", System.Globalization.NumberStyles.HexNumber);
|
|||
|
|
|||
|
var mapper = int.Parse($"{game.Element("pcb").Attribute("mapper").Value}");
|
|||
|
|
|||
|
if (mapper > 255) continue;
|
|||
|
db.AddInfo(new RomDB.RomInfo { CRC = crc, Mapper = mapper });
|
|||
|
}
|
|||
|
|
|||
|
UnityEditor.EditorUtility.SetDirty(db);
|
|||
|
UnityEditor.AssetDatabase.SaveAssets();
|
|||
|
}
|
|||
|
|
|||
|
#endif
|
|||
|
}
|
|||
|
}
|