Skip to content

Commit 9d3f1ed

Browse files
committed
Init repo
1 parent db1fb60 commit 9d3f1ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+4611
-0
lines changed

.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Addinational File
2+
LICENSE.meta
3+
README.md.meta
4+
README_CN.md.meta
5+
6+
/[Ll]ibrary/
7+
/[Tt]emp/
8+
/[Oo]bj/
9+
/[Bb]uild/
10+
/[Bb]uilds/
11+
/[Pp]rojectSettings/ProjectVersion.txt
12+
/Assets/AssetStoreTools*
13+
14+
# Autogenerated VS/MD solution and project files
15+
ExportedObj/
16+
*.csproj
17+
*.unityproj
18+
*.sln
19+
*.suo
20+
*.tmp
21+
*.user
22+
*.userprefs
23+
*.pidb
24+
*.booproj
25+
*.svd
26+
27+
# Unity3D generated meta files
28+
*.pidb.meta
29+
30+
# Unity3D Generated File On Crash Reports
31+
sysinfo.txt
32+
33+
# Builds
34+
*.apk
35+
*.unitypackage

Aya.UNes.asmdef

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Aya.UNes",
3+
"references": [],
4+
"includePlatforms": [],
5+
"excludePlatforms": [],
6+
"allowUnsafeCode": true,
7+
"overrideReferences": false,
8+
"precompiledReferences": [],
9+
"autoReferenced": true,
10+
"defineConstraints": [],
11+
"versionDefines": [],
12+
"noEngineReferences": false
13+
}

Aya.UNes.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# UNes
2+
3+
本项目修改自 https://github.com/Xyene/Emulator.NES

Runtime.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Addressable.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace Aya.UNes
4+
{
5+
public abstract class Addressable
6+
{
7+
public delegate uint ReadDelegate(uint address);
8+
9+
public delegate void WriteDelegate(uint address, byte val);
10+
11+
protected readonly Emulator _emulator;
12+
protected readonly ReadDelegate[] _readMap;
13+
protected readonly WriteDelegate[] _writeMap;
14+
protected readonly uint _addressSize;
15+
16+
protected Addressable(Emulator emulator, uint addressSpace)
17+
{
18+
_emulator = emulator;
19+
_addressSize = addressSpace;
20+
_readMap = new ReadDelegate[addressSpace + 1];
21+
_writeMap = new WriteDelegate[addressSpace + 1];
22+
}
23+
24+
protected virtual void InitializeMemoryMap()
25+
{
26+
_readMap.Fill(address => 0);
27+
28+
// Some games write to addresses not mapped and expect to continue afterwards
29+
_writeMap.Fill((address, val) => { });
30+
}
31+
32+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
33+
public uint ReadByte(uint address)
34+
{
35+
address &= _addressSize;
36+
return _readMap[address](address);
37+
}
38+
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public void WriteByte(uint address, uint val)
41+
{
42+
address &= _addressSize;
43+
_writeMap[address](address, (byte)val);
44+
}
45+
46+
public void MapReadHandler(uint start, uint end, CPU.ReadDelegate func)
47+
{
48+
for (uint i = start; i <= end; i++)
49+
{
50+
_readMap[i] = func;
51+
}
52+
}
53+
54+
public void MapWriteHandler(uint start, uint end, CPU.WriteDelegate func)
55+
{
56+
for (uint i = start; i <= end; i++)
57+
{
58+
_writeMap[i] = func;
59+
}
60+
}
61+
}
62+
}

Runtime/Addressable.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/CPU.Core.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
3+
namespace Aya.UNes
4+
{
5+
sealed partial class CPU
6+
{
7+
public enum InterruptType
8+
{
9+
NMI,
10+
IRQ,
11+
RESET
12+
}
13+
14+
private readonly uint[] _interruptHandlerOffsets = { 0xFFFA, 0xFFFE, 0xFFFC };
15+
private readonly bool[] _interrupts = new bool[2];
16+
17+
public void Initialize()
18+
{
19+
A = 0;
20+
X = 0;
21+
Y = 0;
22+
SP = 0xFD;
23+
P = 0x24;
24+
25+
PC = ReadWord(_interruptHandlerOffsets[(int) InterruptType.RESET]);
26+
}
27+
28+
public void Reset()
29+
{
30+
SP -= 3;
31+
F.InterruptsDisabled = true;
32+
}
33+
34+
public void TickFromPPU()
35+
{
36+
if (Cycle-- > 0) return;
37+
ExecuteSingleInstruction();
38+
}
39+
40+
public void ExecuteSingleInstruction()
41+
{
42+
for (var i = 0; i < _interrupts.Length; i++)
43+
{
44+
if (_interrupts[i])
45+
{
46+
PushWord(PC);
47+
Push(P);
48+
PC = ReadWord(_interruptHandlerOffsets[i]);
49+
F.InterruptsDisabled = true;
50+
_interrupts[i] = false;
51+
return;
52+
}
53+
}
54+
55+
_currentInstruction = NextByte();
56+
57+
Cycle += _opCodeDefs[_currentInstruction].Cycles;
58+
59+
ResetInstructionAddressingMode();
60+
// if (_numExecuted > 10000 && PC - 1 == 0xFF61)
61+
// if(_emulator.Controller.debug || 0x6E00 <= PC && PC <= 0x6EEF)
62+
// 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")}");
63+
64+
var op = _opCodes[_currentInstruction];
65+
if (op == null)
66+
{
67+
throw new ArgumentException(_currentInstruction.ToString("X2"));
68+
}
69+
70+
op();
71+
}
72+
73+
public void TriggerInterrupt(InterruptType type)
74+
{
75+
if (!F.InterruptsDisabled || type == InterruptType.NMI)
76+
{
77+
_interrupts[(int)type] = true;
78+
}
79+
}
80+
}
81+
}

Runtime/CPU.Core.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/CPU.IORegisters.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.CompilerServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Aya.UNes
9+
{
10+
sealed partial class CPU
11+
{
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13+
public void WriteIoRegister(uint reg, byte val)
14+
{
15+
switch (reg)
16+
{
17+
case 0x4014: // OAM DMA
18+
_emulator.PPU.PerformDMA(val);
19+
break;
20+
case 0x4016:
21+
_emulator.Controller.Strobe(val == 1);
22+
break;
23+
}
24+
25+
if (reg <= 0x401F)
26+
{
27+
return; // APU write
28+
}
29+
30+
throw new NotImplementedException($"{reg:X4} = {val:X2}");
31+
}
32+
33+
public uint ReadIORegister(uint reg)
34+
{
35+
switch (reg)
36+
{
37+
case 0x4016:
38+
return (uint) _emulator.Controller.ReadState() & 0x1;
39+
}
40+
return 0x00;
41+
//throw new NotImplementedException();
42+
}
43+
}
44+
}

Runtime/CPU.IORegisters.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)