DotN64/DotN64/Nintendo64.cs

111 lines
2.7 KiB
C#
Raw Permalink Normal View History

using System;
using System.Threading;
namespace DotN64
2017-08-28 08:53:48 +02:00
{
2017-09-03 09:33:27 +02:00
using CPU;
using Extensions;
2017-10-03 19:49:25 +02:00
using RCP;
2017-09-03 09:33:27 +02:00
public class Nintendo64
2017-08-28 08:53:48 +02:00
{
#region Properties
2017-08-29 15:40:18 +02:00
public VR4300 CPU { get; }
2017-08-28 08:53:48 +02:00
public RealityCoprocessor RCP { get; }
public RDRAM RAM { get; } = new RDRAM(new byte[0x00400000]); // The base system has 4 MB of RAM installed.
public PeripheralInterface PIF { get; }
private Cartridge cartridge;
public Cartridge Cartridge
{
get => cartridge;
set
{
if (Cartridge == value)
return;
cartridge = value;
CartridgeSwapped?.Invoke(this, value);
}
}
public IVideoOutput VideoOutput { get; set; }
public Switch Power { get; private set; }
public Diagnostics.Debugger Debugger { get; set; }
#endregion
#region Events
public event Action<Nintendo64, Cartridge> CartridgeSwapped;
2017-08-28 08:53:48 +02:00
#endregion
2017-08-29 15:40:18 +02:00
#region Constructors
public Nintendo64()
{
PIF = new PeripheralInterface(this);
RCP = new RealityCoprocessor(this);
CPU = new VR4300
2017-11-04 13:43:15 +01:00
{
DivMode = 0b01,
MasterClock = 62.5 * Math.Pow(10, 6),
ReadSysAD = RCP.MemoryMaps.ReadWord,
WriteSysAD = RCP.MemoryMaps.WriteWord
2017-11-04 13:43:15 +01:00
};
2017-08-29 15:40:18 +02:00
}
#endregion
2017-08-28 08:53:48 +02:00
#region Methods
2017-10-06 02:56:34 +02:00
public void PowerOn()
2017-08-28 08:53:48 +02:00
{
Power = Switch.On;
2017-11-04 12:14:51 +01:00
CPU.Reset();
PIF.Reset();
2017-11-16 08:33:04 +01:00
}
public void PowerOff() => Power = Switch.Off;
2017-11-16 08:33:04 +01:00
public void Run()
{
var cpuThread = new Thread(() =>
{
if (Debugger == null)
{
while (Power == Switch.On)
{
if (Debugger == null)
CPU.Cycle();
else
{
Debugger.Run(true);
Debugger = null;
}
}
}
else
{
Debugger.Run(true);
PowerOff();
}
})
{ Name = nameof(VR4300) };
cpuThread.Start();
if (VideoOutput != null)
{
while (Power == Switch.On && VideoOutput != null)
{
VideoOutput.Draw(new VideoFrame(RCP.VI), RCP.VI, RAM);
}
}
cpuThread.Join();
2017-08-28 08:53:48 +02:00
}
#endregion
}
}