-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCNesGme.cpp
287 lines (244 loc) · 6.92 KB
/
CNesGme.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "CNesGme.h"
#include "CNesGmeAudioDataProvider.h"
#include "CNesCalculations.h"
#include "DNesConsts.h"
#include "CFile.h"
namespace PurrFX
{
const nullptr_t NO_ERROR = nullptr;
CNesGme::CNesGme():
m_oAudioConverter(16, 2)
{
}
CNesGme::~CNesGme()
{
if (m_pEmu != nullptr)
delete m_pEmu;
}
bool CNesGme::prepareEmulator()
{
gme_err_t sError = nullptr;
// Determine file type
gme_type_t pFileType;
sError = gme_identify_file("file.nsf", &pFileType);
if (sError != NO_ERROR || pFileType == nullptr || strcmp(pFileType->system, "Nintendo NES") != 0)
return false;
// Create emulator
Music_Emu* pEmu = pFileType->new_emu();
if (pEmu == nullptr)
return false;
if (m_pEmu != nullptr)
delete m_pEmu;
m_pEmu = pEmu;
// Setup emulator audio
sError = m_pEmu->set_sample_rate( audioFormat().sampleRate() );
if (sError != NO_ERROR)
return false;
// Setup logging
Nsf_Emu* pNsfEmu = static_cast<Nsf_Emu*>(m_pEmu);
Nes_Cpu* pNesCpu = pNsfEmu->cpu_();
pNesCpu->gme_integrator = this;
pNsfEmu->apu_()->gme_integrator = this;
// Other
pEmu->ignore_silence(true);
return true;
}
bool CNesGme::open(const pathstring& i_sFileName)
{
if (!prepareEmulator())
return false;
static const size_t nFileSizeLimit = 128*1024; // 128KB should be enough
bool bSuccess = false;
CFile oFile(i_sFileName, CFile::Read);
if (oFile.isOpened())
{
size_t nFileSize = oFile.size();
if (nFileSize > 0 &&
nFileSize <= nFileSizeLimit)
{
uint8_t* pData = new uint8_t[nFileSize];
oFile.read(pData, nFileSize);
bSuccess = open(pData, nFileSize);
delete[] pData;
}
}
return bSuccess;
}
bool CNesGme::open(const uint8_t* i_pData, size_t i_nSize)
{
if (!prepareEmulator())
return false;
gme_err_t sError = m_pEmu->load_mem(i_pData, i_nSize);
bool bSuccess = (sError == NO_ERROR);
if (!bSuccess)
{
delete m_pEmu;
m_pEmu = nullptr;
}
else
m_oAudioConverter.reset();
return bSuccess;
}
bool CNesGme::setTrack(int i_nTrack)
{
if (m_pEmu == nullptr)
return false;
if (i_nTrack < 0 ||
i_nTrack >= m_pEmu->track_count())
return false;
gme_err_t sError = m_pEmu->start_track(i_nTrack);
bool bSuccess = (sError == NO_ERROR);
if (bSuccess)
m_oAudioConverter.reset();
return bSuccess;
}
int CNesGme::trackCount() const
{
if (m_pEmu == nullptr)
return 0;
return m_pEmu->track_count();
}
bool CNesGme::render(char* o_pData, size_t i_nDataSize)
{
if (m_pEmu == nullptr)
return false;
CNesGmeAudioDataProvider oProvider(m_pEmu);
return m_oAudioConverter.getData(
o_pData, i_nDataSize,
audioFormat().bitDepth(),
audioFormat().channels(),
&oProvider);
}
void CNesGme::onGmeEventCpuInstruction(uint16_t i_nAddress, uint8_t i_nOpcode, uint8_t i_nArgByte1, uint8_t i_nArgByte2)
{
if (!nesEventHandlingEnabled())
return;
CNesEventCpuInstruction oEvent(i_nAddress, i_nOpcode, i_nArgByte1, i_nArgByte2);
handleNesEvent(oEvent);
}
void CNesGme::onGmeEventCodeLabel(char i_cLabelName)
{
if (!nesEventHandlingEnabled())
return;
ECodeLabelType eType = ECodeLabelType::Undefined;
switch (i_cLabelName)
{
case 'i':
eType = ECodeLabelType::InitAddress;
break;
case 'p':
eType = ECodeLabelType::PlayAddress;
break;
}
assert(eType != ECodeLabelType::Undefined);
CNesEventCodeLabel oEvent(eType);
handleNesEvent(oEvent);
}
void CNesGme::onGmeEventFrameStart(int i_nFrame)
{
if (nesEventHandlingEnabled())
{
CNesEventFrameStart oEvent(i_nFrame);
handleNesEvent(oEvent);
}
if (usesFrameDataConsumer())
frameDataConsumer()->startFrame();
}
void CNesGme::onGmeEventFrameEnd()
{
if (nesEventHandlingEnabled())
{
CNesEventFrameEnd oEvent;
handleNesEvent(oEvent);
}
if (usesFrameDataConsumer())
frameDataConsumer()->endFrame();
}
void CNesGme::onGmeEventApuRegisterWrite(uint16_t i_nRegister, uint8_t i_nValue)
{
if (nesEventHandlingEnabled())
{
CNesEventApuRegisterWrite oEvent(i_nRegister, i_nValue);
handleNesEvent(oEvent);
}
if (usesFrameDataConsumer())
{
ERegister eRegister = ERegister::Unknown;
switch(i_nRegister)
{
/////////
// APU //
/////////
// Pulse 1
case 0x4000: eRegister = ERegister::Apu4000; break;
case 0x4001: eRegister = ERegister::Apu4001; break;
case 0x4002: eRegister = ERegister::Apu4002; break;
case 0x4003: eRegister = ERegister::Apu4003; break;
// Pulse 2
case 0x4004: eRegister = ERegister::Apu4004; break;
case 0x4005: eRegister = ERegister::Apu4005; break;
case 0x4006: eRegister = ERegister::Apu4006; break;
case 0x4007: eRegister = ERegister::Apu4007; break;
// Triangle
case 0x4008: eRegister = ERegister::Apu4008; break;
case 0x400A: eRegister = ERegister::Apu400A; break;
case 0x400B: eRegister = ERegister::Apu400B; break;
// Noise
case 0x400C: eRegister = ERegister::Apu400C; break;
case 0x400E: eRegister = ERegister::Apu400E; break;
case 0x400F: eRegister = ERegister::Apu400F; break;
// DMC
case 0x4010: eRegister = ERegister::Apu4010; break;
case 0x4011: eRegister = ERegister::Apu4011; break;
case 0x4012: eRegister = ERegister::Apu4012; break;
case 0x4013: eRegister = ERegister::Apu4013; break;
// Status
case 0x4015: eRegister = ERegister::Apu4015; break;
default:
assert(false && "Unknown register");
}
if (eRegister != ERegister::Unknown)
frameDataConsumer()->setRegister(eRegister, i_nValue);
}
}
void CNesGme::onGmeDpcmSampleStarted(uint8_t i_nAddress, uint8_t i_nLength)
{
if (!usesDpcmDataConsumer())
return;
dpcmDataConsumer()->start(i_nAddress, i_nLength);
uint16_t nSampleBytes = CNesCalculations::dpcmSampleLengthUnpack(i_nLength);
uint16_t nSampleAddress = CNesCalculations::dpcmSampleAddressUnpack(i_nAddress);
Nsf_Emu* pNsfEmu = static_cast<Nsf_Emu*>(m_pEmu);
for (uint16_t i = 0; i < nSampleBytes; i++)
dpcmDataConsumer()->addByte( pNsfEmu->cpu_read_(nSampleAddress+i) );
dpcmDataConsumer()->end();
}
bool CNesGme::gmeGetFrameCode(std::vector<uint8_t>& o_rCode)
{
auto* pDataSource = frameDataProducer();
if (pDataSource == nullptr)
return false;
CFrameData oData;
if (pDataSource->available())
oData = pDataSource->get();
CFrameDataProcessor oDataProcessor(oData);
oDataProcessor.generateAssemblyCode(o_rCode);
pDataSource->next();
return true;
}
bool CNesGme::gmeActivateDpcmSample(uint8_t i_nAddress, uint8_t i_nLength)
{
if (!usesDpcmDataProvider())
return false;
const CDpcmSample* pSample = dpcmDataProvider()->getSample();
if (pSample == nullptr)
pSample = dpcmDataProvider()->getSample(i_nAddress, i_nLength);
if (pSample == nullptr)
return false;
Nsf_Emu* pNsfEmu = static_cast<Nsf_Emu*>(m_pEmu);
Nes_Cpu* pNesCpu = pNsfEmu->cpu_();
for (uint16_t nByteIndex = 0; nByteIndex < pSample->size(); nByteIndex++)
pNesCpu->memory_write(NesConsts::dpcmSampleAddressMin+nByteIndex, pSample->get(nByteIndex));
return true;
}
}