AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesEmulator.cs

86 lines
2.3 KiB
C#
Raw Normal View History

using AxibugEmuOnline.Client.ClientCore;
2024-07-30 11:57:09 +08:00
using System;
using System.Xml.Linq;
2024-07-24 14:27:10 +08:00
using UnityEngine;
using VirtualNes.Core;
using VirtualNes.Core.Debug;
namespace AxibugEmuOnline.Client
{
public class NesEmulator : MonoBehaviour
{
2024-07-25 11:03:58 +08:00
private NES m_nesIns;
2024-07-30 11:57:09 +08:00
public VideoProvider VideoProvider;
2024-08-02 16:50:16 +08:00
public AudioProvider AudioProvider;
2024-07-30 11:57:09 +08:00
2024-07-24 14:27:10 +08:00
private void Start()
{
Application.targetFrameRate = 60;
2024-08-06 13:49:24 +08:00
StartGame("ff1.nes");
2024-07-24 14:27:10 +08:00
}
public void StartGame(string romName)
{
2024-07-25 11:03:58 +08:00
StopGame();
2024-07-24 14:27:10 +08:00
Supporter.Setup(new CoreSupporter());
Debuger.Setup(new CoreDebuger());
2024-07-30 11:57:09 +08:00
try
{
m_nesIns = new NES(romName);
}
catch (Exception ex)
{
m_nesIns = null;
Debug.LogError(ex);
}
2024-07-25 11:03:58 +08:00
}
public void StopGame()
{
m_nesIns?.Dispose();
m_nesIns = null;
}
private void Update()
{
2024-07-30 11:57:09 +08:00
if (m_nesIns != null)
{
m_nesIns.EmulateFrame(true);
2024-07-30 18:53:36 +08:00
2024-07-30 11:57:09 +08:00
var screenBuffer = m_nesIns.ppu.GetScreenPtr();
2024-07-30 18:53:36 +08:00
var lineColorMode = m_nesIns.ppu.GetLineColorMode();
VideoProvider.SetDrawData(screenBuffer, lineColorMode, 256, 240);
2024-08-02 16:50:16 +08:00
AudioProvider.ProcessSound(m_nesIns);
2024-07-30 11:57:09 +08:00
}
2024-07-24 14:27:10 +08:00
}
#if UNITY_EDITOR
[ContextMenu("IMPORT")]
public void TTTA()
{
var db = Resources.Load<RomDB>("NES/ROMDB");
db.Clear();
var dbFile = Resources.Load<TextAsset>("NES/nes20db");
var xml = XDocument.Parse(dbFile.text);
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}");
db.AddInfo(new RomDB.RomInfo { CRC = crc, Mapper = mapper });
}
UnityEditor.AssetDatabase.SaveAssets();
}
#endif
2024-07-24 14:27:10 +08:00
}
}