forked from macifom/macifom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNESAPUEmulator.mm
315 lines (240 loc) · 8.51 KB
/
NESAPUEmulator.mm
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* NESAPUEmulator.mm
*
* Copyright (c) 2010 Auston Stewart
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "NESAPUEmulator.h"
#import "NES6502Interpreter.h"
static int dmc_read_function( void* memoryReader, cpu_addr_t cpuAddress)
{
NESMemoryReader *memoryReadStructure = (NESMemoryReader *)memoryReader;
[memoryReadStructure->cpuInterpreter stealCycles:4];
return memoryReadStructure->memoryReadFunction(memoryReadStructure->cpuInterpreter,@selector(readByteFromCPUAddressSpace:),(uint16_t)cpuAddress);
}
static void HandleOutputBuffer (
void *aqData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer
) {
// UInt32 bytesToRead;
UInt32 bytesRead = 0;
UInt32 samplesRead;
UInt32 availableSamples;
NESAPUState *pAqData = (NESAPUState *)aqData;
// NSLog(@"In HandleOutputBuffer");
if (!pAqData->isRunning) {
bytesRead = pAqData->bufferByteSize;
bzero(inBuffer->mAudioData,pAqData->bufferByteSize);
// NSLog(@"NES APU is not running. Filling buffer with zeros.");
}
else {
availableSamples = pAqData->blipBuffer->samples_avail();
if (availableSamples < pAqData->numPacketsToRead) {
NSLog(@"Insufficient audio samples for buffering. Inserting silence.");
bytesRead = pAqData->bufferByteSize;
bzero(inBuffer->mAudioData,pAqData->bufferByteSize);
}
else {
samplesRead = pAqData->blipBuffer->read_samples((blip_sample_t*)inBuffer->mAudioData,pAqData->numPacketsToRead);
bytesRead = samplesRead * 2; // As each sample is 16-bits
}
}
inBuffer->mAudioDataByteSize = bytesRead;
AudioQueueEnqueueBuffer (
pAqData->queue,
inBuffer,
0,
NULL
);
}
@implementation NESAPUEmulator
- (void)initializeAudioPlaybackQueue
{
Float32 gain = 1.0;
int error;
// Create new output
error = AudioQueueNewOutput (
&(nesAPUState->dataFormat),
HandleOutputBuffer,
nesAPUState,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes,
0,
&(nesAPUState->queue)
);
NSLog(@"AudioQueueNewOutput: %d",error);
// Set buffer size
nesAPUState->numPacketsToRead = [(NSNumber *)[[NSUserDefaults standardUserDefaults] valueForKey:@"audioBufferLength"] unsignedIntValue]; // 44.1kHz at 60 fps = 735 (times 4 to reduce overhead)
nesAPUState->bufferByteSize = nesAPUState->numPacketsToRead * 2; // 735 samples times four, times 16-bits per sample
// Allocate those bufferes
for (int i = 0; i < NUM_BUFFERS; ++i) {
AudioQueueAllocateBuffer(
nesAPUState->queue,
nesAPUState->bufferByteSize,
&(nesAPUState->buffers[i])
);
NSLog(@"AudioQueueAllocateBuffer: %d",error);
}
AudioQueueSetParameter (
nesAPUState->queue,
kAudioQueueParam_Volume,
gain
);
}
- (id)init {
if ([super init]) {
time = 0;
nesAPU = new Nes_Apu();
blipBuffer = new Blip_Buffer();
blipBuffer->clock_rate( 1789773 ); // Should be 1789773 for NES
blargg_err_t error = blipBuffer->sample_rate(44100,600); // 600ms to accomodate up to eight times four frames of audio
if (error) NSLog(@"Error allocating blipBuffer.");
nesAPU->output(blipBuffer);
nesAPUState = (NESAPUState *)malloc(sizeof(NESAPUState));
nesAPUState->dataFormat.mSampleRate = 44100.0;
nesAPUState->dataFormat.mFormatID = kAudioFormatLinearPCM;
// Sort out endianness
if (NSHostByteOrder() == NS_BigEndian)
nesAPUState->dataFormat.mFormatFlags = kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
else
nesAPUState->dataFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
nesAPUState->dataFormat.mBytesPerPacket = 2;
nesAPUState->dataFormat.mFramesPerPacket = 1;
nesAPUState->dataFormat.mBytesPerFrame = 2;
nesAPUState->dataFormat.mChannelsPerFrame = 1;
nesAPUState->dataFormat.mBitsPerChannel = 16;
nesAPUState->isRunning = NO;
nesAPUState->blipBuffer = blipBuffer;
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObject:[NSNumber numberWithUnsignedInt:1470] forKey:@"audioBufferLength"]];
[self initializeAudioPlaybackQueue];
}
return self;
}
- (void)dealloc
{
// Kill the queue and its buffers
AudioQueueDispose (
nesAPUState->queue,
true
);
// FIXME: Free NES APU Resources
[super dealloc];
}
- (void)clearBuffer
{
blipBuffer->clear(true);
}
- (void)pause
{
nesAPUState->isRunning = NO;
AudioQueuePause(nesAPUState->queue); // FIXME: I think cacheing still occurs when paused, causing buffer problems when unpausing. I may need to stop the queue here instead.
}
- (void)resume
{
nesAPUState->isRunning = YES;
AudioQueueStart(nesAPUState->queue,NULL);
}
- (void)stopAPUPlayback
{
nesAPUState->isRunning = NO;
AudioQueueStop(nesAPUState->queue,true);
}
- (void)beginAPUPlayback
{
nesAPUState->isRunning = NO;
// Reset the APU and Buffer
_lastCPUCycle = 0;
nesAPU->reset(false,0);
blipBuffer->clear(true);
// Prime the playback buffer
for (int i = 0; i < NUM_BUFFERS; ++i) {
HandleOutputBuffer (
nesAPUState,
nesAPUState->queue,
nesAPUState->buffers[i]
);
}
AudioQueuePrime(nesAPUState->queue,0,NULL); // According to some docs, we should also call AudioQueuePrime
}
// Set function for APU to call when it needs to read memory (DMC samples)
-(void)setDMCReadObject:(NES6502Interpreter *)cpu {
NESMemoryReader *dmcUserData = (NESMemoryReader *)malloc(sizeof(NESMemoryReader));
dmcUserData->cpuInterpreter = cpu;
dmcUserData->memoryReadFunction = (uint8_t (*)(id, SEL, uint16_t))[cpu methodForSelector:@selector(readByteFromCPUAddressSpace:)];
nesAPU->dmc_reader(dmc_read_function,dmcUserData);
}
// Set output sample rate
- (BOOL)setOutputSampleRate:(long)rate {
// simpleApu.sample_rate(rate);
return YES;
}
// Write to register (0x4000-0x4017, except 0x4014 and 0x4016)
- (void)writeByte:(uint8_t)byte toAPUFromCPUAddress:(uint16_t)address onCycle:(uint_fast32_t)cycle {
nesAPU->write_register(cycle, address, byte);
}
// Read from status register at 0x4015
- (uint8_t)readAPUStatusOnCycle:(uint_fast32_t)cycle {
// This is done to allow multiple reads from APU status during debugging (otherwise Blargg's APU asserts) Fixes Issue #1
if (cycle > _lastCPUCycle) {
_apuStatus = nesAPU->read_status(cycle);
_lastCPUCycle = cycle;
}
return _apuStatus;
}
// End a 1/60 sound frame
- (double)endFrameOnCycle:(uint_fast32_t)cycle {
UInt32 availableSamples;
double timingCorrection = 0;
nesAPU->end_frame(cycle);
blipBuffer->end_frame(cycle);
_lastCPUCycle = 0;
nesAPUState->isRunning = YES;
availableSamples = nesAPUState->blipBuffer->samples_avail();
if (availableSamples < (nesAPUState->numPacketsToRead * 2)) {
timingCorrection = -0.005;
}
else if (availableSamples > (nesAPUState->numPacketsToRead * 4)) {
timingCorrection = 0.005;
// Try to catch run-away buffer overflow
if (availableSamples > (nesAPUState->numPacketsToRead * 6)) {
NSLog(@"Reducing samples in audio buffer to prevent overflow.");
blipBuffer->remove_samples(nesAPUState->numPacketsToRead);
}
}
return timingCorrection;
}
// Number of samples in buffer
- (long)numberOfBufferedSamples {
return blipBuffer->samples_avail();
}
- (int)pendingDMCReadsOnCycle:(uint_fast32_t)cycle {
return nesAPU->count_dmc_reads(cycle, NULL);
}
- (void)runAPUUntilCPUCycle:(uint_fast32_t)cycle {
nesAPU->run_until(cycle);
}
// Save/load snapshot of emulation state
- (void)saveSnapshot {
}
- (void)loadSnapshot {
}
@end