-
Notifications
You must be signed in to change notification settings - Fork 1
/
Nes.cs
166 lines (145 loc) · 5.12 KB
/
Nes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Emu6502
{
public class Nes
{
//public const long PpuTicksPerSecond = 5369318;
public static Nes ActiveNes;
public Controller Controller1 = new Controller();
public Controller Controller2 = new Controller();
public Rom Rom { get; private set; }
public Chip6502 Cpu { get; private set; }
public Ppu Ppu { get; private set; }
public NesMemory Mem { get; private set; }
public Mapper Mapper { get; private set; }
//private int cpuCycles;
//private int ppuCycles;
//public int frameDivider = 0;
private int cpuDivider = 0;
public long TotalCpuCycles = 0;
public long TotalPpuCycles = 0;
private Stopwatch sw = new Stopwatch();
public float FPS;
private int frameCount = 0;
public Dictionary<string, int> EventCounters = new Dictionary<string, int>();
public void RecordEvent(string name)
{
if (EventCounters.ContainsKey(name))
EventCounters[name] += 1;
else
EventCounters[name] = 1;
}
public Nes(string romfile)
{
ActiveNes=this;
Rom = new Rom(romfile);
Mem = new NesMemory(this);
Cpu = new Chip6502(Mem);
Ppu = new Ppu(this);
switch (Rom.MapperNumber)
{
case 0:
Mapper = new NROM(this);
break;
case 1:
Mapper = new MMC1(this);
break;
case 2:
Mapper = new UxROM(this);
break;
default:
// TODO: Cleaner...
throw new Exception("Unknown mapper");
}
Reset();
}
public void Reset()
{
sw.Reset();
sw.Start();
//cpuCycles = 0;// ppuCycles = 0;
cpuDivider = 0;
TotalPpuCycles = 0;
TotalCpuCycles = 0;
frameCount = 0;
Mem.Reset();
Ppu.Reset();
Mapper.Reset();
Cpu.RAM = Mem.RAM;
Cpu.Reset();
sw.Start();
}
public int RunOneFrame()
{
/*if (Cpu.Paused)
return ppuCyclesToRun;*/
while (true)
{
if (Cpu.WaitCycles <= Ppu.WaitCycles)
{
Cpu.Run(Ppu.WaitCycles);
}
else
Cpu.WaitCycles -= Ppu.WaitCycles;
Ppu.FrameCycle += Ppu.WaitCycles;
Ppu.WaitCycles = 0;
Ppu.Tick();
if (Ppu.VsyncSignalToMainLoop)
{
Ppu.VsyncSignalToMainLoop = false;
++frameCount;
if (frameCount == 10)
{
float secs = sw.ElapsedTicks / (float)Stopwatch.Frequency;
FPS = frameCount / secs;
sw.Reset();
sw.Start();
frameCount = 0;
}
//render = true;
return 0;
}
#if false
//Console.WriteLine("ToRun: {0} CPU: {1} PPU: {2}", ppuCyclesToRun, Cpu.WaitCycles, Ppu.WaitCycles);
//int min = ppuCyclesToRun < Ppu.WaitCycles ? ppuCyclesToRun : Ppu.WaitCycles;
int min = Ppu.WaitCycles < Cpu.WaitCycles ? Ppu.WaitCycles : Cpu.WaitCycles;
//Console.WriteLine("Min={0}", min);
//TotalCpuCycles += min;
//TotalPpuCycles += min;
Cpu.WaitCycles -= min;
Ppu.WaitCycles -= min;
//ppuCyclesToRun -= min;
if (Cpu.WaitCycles == 0)
Cpu.Tick();
/*if (Ppu.WaitCycles < 0)
throw new InvalidOperationException("FUCK");*/
Ppu.FrameCycle += min;
if (Ppu.WaitCycles == 0)
{
Ppu.Tick();
if (Ppu.VsyncSignalToMainLoop)
{
Ppu.VsyncSignalToMainLoop = false;
++frameCount;
if (frameCount == 10)
{
float secs = sw.ElapsedTicks / (float)Stopwatch.Frequency;
FPS = frameCount / secs;
sw.Reset();
sw.Start();
frameCount = 0;
}
//render = true;
return 0;
}
}
#endif
}
//return ppuCyclesToRun;
}
}
}