Skip to content

Commit

Permalink
Init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ls9512 committed Mar 29, 2021
1 parent db1fb60 commit 9d3f1ed
Show file tree
Hide file tree
Showing 92 changed files with 4,611 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Addinational File
LICENSE.meta
README.md.meta
README_CN.md.meta

/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Pp]rojectSettings/ProjectVersion.txt
/Assets/AssetStoreTools*

# Autogenerated VS/MD solution and project files
ExportedObj/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd

# Unity3D generated meta files
*.pidb.meta

# Unity3D Generated File On Crash Reports
sysinfo.txt

# Builds
*.apk
*.unitypackage
13 changes: 13 additions & 0 deletions Aya.UNes.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Aya.UNes",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Aya.UNes.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# UNes

本项目修改自 https://github.com/Xyene/Emulator.NES
8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Runtime/Addressable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Runtime.CompilerServices;

namespace Aya.UNes
{
public abstract class Addressable
{
public delegate uint ReadDelegate(uint address);

public delegate void WriteDelegate(uint address, byte val);

protected readonly Emulator _emulator;
protected readonly ReadDelegate[] _readMap;
protected readonly WriteDelegate[] _writeMap;
protected readonly uint _addressSize;

protected Addressable(Emulator emulator, uint addressSpace)
{
_emulator = emulator;
_addressSize = addressSpace;
_readMap = new ReadDelegate[addressSpace + 1];
_writeMap = new WriteDelegate[addressSpace + 1];
}

protected virtual void InitializeMemoryMap()
{
_readMap.Fill(address => 0);

// Some games write to addresses not mapped and expect to continue afterwards
_writeMap.Fill((address, val) => { });
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public uint ReadByte(uint address)
{
address &= _addressSize;
return _readMap[address](address);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteByte(uint address, uint val)
{
address &= _addressSize;
_writeMap[address](address, (byte)val);
}

public void MapReadHandler(uint start, uint end, CPU.ReadDelegate func)
{
for (uint i = start; i <= end; i++)
{
_readMap[i] = func;
}
}

public void MapWriteHandler(uint start, uint end, CPU.WriteDelegate func)
{
for (uint i = start; i <= end; i++)
{
_writeMap[i] = func;
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Addressable.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions Runtime/CPU.Core.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;

namespace Aya.UNes
{
sealed partial class CPU
{
public enum InterruptType
{
NMI,
IRQ,
RESET
}

private readonly uint[] _interruptHandlerOffsets = { 0xFFFA, 0xFFFE, 0xFFFC };
private readonly bool[] _interrupts = new bool[2];

public void Initialize()
{
A = 0;
X = 0;
Y = 0;
SP = 0xFD;
P = 0x24;

PC = ReadWord(_interruptHandlerOffsets[(int) InterruptType.RESET]);
}

public void Reset()
{
SP -= 3;
F.InterruptsDisabled = true;
}

public void TickFromPPU()
{
if (Cycle-- > 0) return;
ExecuteSingleInstruction();
}

public void ExecuteSingleInstruction()
{
for (var i = 0; i < _interrupts.Length; i++)
{
if (_interrupts[i])
{
PushWord(PC);
Push(P);
PC = ReadWord(_interruptHandlerOffsets[i]);
F.InterruptsDisabled = true;
_interrupts[i] = false;
return;
}
}

_currentInstruction = NextByte();

Cycle += _opCodeDefs[_currentInstruction].Cycles;

ResetInstructionAddressingMode();
// if (_numExecuted > 10000 && PC - 1 == 0xFF61)
// if(_emulator.Controller.debug || 0x6E00 <= PC && PC <= 0x6EEF)
// Console.WriteLine($"{(PC - 1).ToString("X4")} {_currentInstruction.ToString("X2")} {opcodeNames[_currentInstruction]}\t\t\tA:{A.ToString("X2")} X:{X.ToString("X2")} Y:{Y.ToString("X2")} P:{P.ToString("X2")} SP:{SP.ToString("X2")}");

var op = _opCodes[_currentInstruction];
if (op == null)
{
throw new ArgumentException(_currentInstruction.ToString("X2"));
}

op();
}

public void TriggerInterrupt(InterruptType type)
{
if (!F.InterruptsDisabled || type == InterruptType.NMI)
{
_interrupts[(int)type] = true;
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/CPU.Core.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions Runtime/CPU.IORegisters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Aya.UNes
{
sealed partial class CPU
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteIoRegister(uint reg, byte val)
{
switch (reg)
{
case 0x4014: // OAM DMA
_emulator.PPU.PerformDMA(val);
break;
case 0x4016:
_emulator.Controller.Strobe(val == 1);
break;
}

if (reg <= 0x401F)
{
return; // APU write
}

throw new NotImplementedException($"{reg:X4} = {val:X2}");
}

public uint ReadIORegister(uint reg)
{
switch (reg)
{
case 0x4016:
return (uint) _emulator.Controller.ReadState() & 0x1;
}
return 0x00;
//throw new NotImplementedException();
}
}
}
11 changes: 11 additions & 0 deletions Runtime/CPU.IORegisters.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d3f1ed

Please sign in to comment.