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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
|
// Author: skogaby
// Lots of examples were followed using other emulators found on github
#include <SDL/SDL.h>
#include <libgen.h>
#include <sys/time.h>
#include <sys/types.h>
#include "main.h"
#include "snes9x.h"
#include "soundux.h"
#include "memmap.h"
#include "apu.h"
#include "apu_blargg.h"
#include "cheats.h"
#include "display.h"
#include "gfx.h"
#include "cpuexec.h"
#include "spc7110.h"
#include "srtc.h"
#include "sa1.h"
#include "scaler.h"
#include "shared.h"
#include "menu.h"
#include "video_blit.h"
#include "input.h"
#include "sound_output.h"
char GameName_emu[512];
bool overclock_cycles = false;
bool reduce_sprite_flicker = true;
int one_c, slow_one_c, two_c;
static int32_t samples_to_play = 0;
static int32_t samples_per_frame = 0;
static int32_t samplerate = (((SNES_CLOCK_SPEED * 6) / (32 * ONE_APU_CYCLE)));
#ifdef USE_BLARGG_APU
static void S9xAudioCallback()
{
size_t avail;
/* Just pick a big buffer. We won't use it all. */
static int16_t audio_buf[0x20000];
S9xFinalizeSamples();
avail = S9xGetSampleCount();
S9xMixSamples(audio_buf, avail);
Audio_Write(audio_buf, avail >> 1);
}
#endif
size_t retro_serialize_size(void)
{
return sizeof(CPU) + sizeof(ICPU) + sizeof(PPU) + sizeof(DMA) +
0x10000 + 0x20000 + 0x20000 + 0x8000 +
#ifndef USE_BLARGG_APU
sizeof(APU) + sizeof(IAPU) + 0x10000 +
#else
SPC_SAVE_STATE_BLOCK_SIZE +
#endif
sizeof(SA1) + sizeof(s7r) + sizeof(rtc_f9);
}
void RTC_Save(char* path, uint_fast8_t state)
{
FILE* savefp;
/* If RTC is disabled then don't bother saving any RTC info */
if (!Settings.SRTC) return;
if (state == 1)
{
savefp = fopen(path, "rb");
if (savefp)
{
if (Settings.SPC7110RTC)
{
fread(&rtc_f9, sizeof(uint8_t), sizeof(rtc_f9), savefp);
fclose(savefp);
S9xUpdateRTC();
}
else
{
fread(&rtc, sizeof(uint8_t), sizeof(rtc), savefp);
fclose(savefp);
S9xSRTCPostLoadState();
}
}
}
else
{
savefp = fopen(path, "wb");
if (savefp)
{
if (Settings.SPC7110RTC)
{
fwrite(&rtc_f9, sizeof(uint8_t), sizeof(rtc_f9), savefp);
fclose(savefp);
S9xUpdateRTC();
}
else
{
S9xSRTCPreSaveState();
fwrite(&rtc, sizeof(uint8_t), sizeof(rtc), savefp);
fclose(savefp);
}
}
}
}
void SRAM_Save(char* path, uint_fast8_t state)
{
FILE* savefp;
/* If SRAM is not used then don't bother saving any SRAM file */
if (Memory.SRAMMask == 0) return;
if (state == 1)
{
savefp = fopen(path, "rb");
if (savefp)
{
fread(Memory.SRAM, sizeof(uint8_t), Memory.SRAMMask, savefp);
fclose(savefp);
}
}
else
{
savefp = fopen(path, "wb");
if (savefp)
{
fwrite(Memory.SRAM, sizeof(uint8_t), Memory.SRAMMask, savefp);
fclose(savefp);
}
}
}
void SaveState(char* path, uint_fast8_t state)
{
#ifndef USE_BLARGG_APU
uint8_t* IAPU_RAM_current = IAPU.RAM;
uintptr_t IAPU_RAM_offset;
#endif
char* buffer;
FILE* savefp;
#ifndef USE_BLARGG_APU
buffer = malloc(sizeof(APU) + sizeof(IAPU) + 0x10000);
#else
buffer = malloc(SPC_SAVE_STATE_BLOCK_SIZE);
#endif
if (state == 1)
{
savefp = fopen(path, "rb");
if (savefp)
{
S9xReset();
fread(&CPU, sizeof(uint8_t), sizeof(CPU), savefp);
fread(&ICPU, sizeof(uint8_t), sizeof(ICPU), savefp);
fread(&PPU, sizeof(uint8_t), sizeof(PPU), savefp);
fread(&DMA, sizeof(uint8_t), sizeof(DMA), savefp);
fread(Memory.VRAM, sizeof(uint8_t), 0x10000, savefp);
fread(Memory.RAM, sizeof(uint8_t), 0x20000, savefp);
fread(Memory.SRAM, sizeof(uint8_t), 0x20000, savefp);
fread(Memory.FillRAM, sizeof(uint8_t), 0x8000, savefp);
#ifndef USE_BLARGG_APU
fread(&APU, sizeof(uint8_t), sizeof(APU), savefp);
fread(&IAPU, sizeof(uint8_t), sizeof(IAPU), savefp);
IAPU_RAM_offset = IAPU_RAM_current - IAPU.RAM;
IAPU.PC += IAPU_RAM_offset;
IAPU.DirectPage += IAPU_RAM_offset;
IAPU.WaitAddress1 += IAPU_RAM_offset;
IAPU.WaitAddress2 += IAPU_RAM_offset;
IAPU.RAM = IAPU_RAM_current;
fread(IAPU.RAM, sizeof(uint8_t), 0x10000, savefp);
#else
S9xAPULoadState(buffer);
fread(buffer, sizeof(uint8_t), SPC_SAVE_STATE_BLOCK_SIZE, savefp);
#endif
SA1.Registers.PC = SA1.PC - SA1.PCBase;
S9xSA1PackStatus();
fread(&SA1, sizeof(uint8_t), sizeof(SA1), savefp);
fread(&s7r, sizeof(uint8_t), sizeof(s7r), savefp);
fread(&rtc_f9, sizeof(uint8_t), sizeof(rtc_f9), savefp);
fclose(savefp);
S9xFixSA1AfterSnapshotLoad();
FixROMSpeed();
IPPU.ColorsChanged = true;
IPPU.OBJChanged = true;
CPU.InDMA = false;
S9xFixColourBrightness();
S9xSA1UnpackStatus();
#ifndef USE_BLARGG_APU
S9xAPUUnpackStatus();
S9xFixSoundAfterSnapshotLoad();
#endif
ICPU.ShiftedPB = ICPU.Registers.PB << 16;
ICPU.ShiftedDB = ICPU.Registers.DB << 16;
S9xSetPCBase(ICPU.ShiftedPB + ICPU.Registers.PC);
S9xUnpackStatus();
S9xFixCycles();
S9xReschedule();
}
}
else
{
savefp = fopen(path, "wb");
if (savefp)
{
#ifdef LAGFIX
S9xPackStatus();
#ifndef USE_BLARGG_APU
S9xAPUPackStatus();
#endif
#endif
S9xUpdateRTC();
S9xSRTCPreSaveState();
fwrite(&CPU, sizeof(uint8_t), sizeof(CPU), savefp);
fwrite(&ICPU, sizeof(uint8_t), sizeof(ICPU), savefp);
fwrite(&PPU, sizeof(uint8_t), sizeof(PPU), savefp);
fwrite(&DMA, sizeof(uint8_t), sizeof(DMA), savefp);
fwrite(Memory.VRAM, sizeof(uint8_t), 0x10000, savefp);
fwrite(Memory.RAM, sizeof(uint8_t), 0x20000, savefp);
fwrite(Memory.SRAM, sizeof(uint8_t), 0x20000, savefp);
fwrite(Memory.FillRAM, sizeof(uint8_t), 0x8000, savefp);
#ifndef USE_BLARGG_APU
fwrite(&APU, sizeof(uint8_t), sizeof(APU), savefp);
fwrite(&IAPU, sizeof(uint8_t), sizeof(IAPU), savefp);
fwrite(IAPU.RAM, sizeof(uint8_t), 0x10000, savefp);
#else
S9xAPUSaveState(buffer);
fwrite(buffer, sizeof(uint8_t), SPC_SAVE_STATE_BLOCK_SIZE, savefp);
#endif
SA1.Registers.PC = SA1.PC - SA1.PCBase;
S9xSA1PackStatus();
fwrite(&SA1, sizeof(uint8_t), sizeof(SA1), savefp);
fwrite(&s7r, sizeof(uint8_t), sizeof(s7r), savefp);
fwrite(&rtc_f9, sizeof(uint8_t), sizeof(rtc_f9), savefp);
fclose(savefp);
}
}
if (buffer) free(buffer);
}
#ifdef FRAMESKIP
static uint32_t Timer_Read(void)
{
/* Timing. */
struct timeval tval;
gettimeofday(&tval, 0);
return (((tval.tv_sec*1000000) + (tval.tv_usec)));
}
static long lastTick = 0, newTick;
static uint32_t SkipCnt = 0, video_frames = 0, FPS = 60, FrameSkip;
static const uint32_t TblSkip[4][4] = {
{0, 0, 0, 0},
{0, 0, 0, 1},
{0, 0, 1, 1},
{0, 1, 1, 1}
};
#endif
void Emulation_Run (void)
{
#ifndef USE_BLARGG_APU
static int16_t audio_buf[2048];
#endif
#ifdef FRAMESKIP
SkipCnt++;
if (SkipCnt > 3) SkipCnt = 0;
if (TblSkip[FrameSkip][SkipCnt]) IPPU.RenderThisFrame = false;
else IPPU.RenderThisFrame = true;
#else
IPPU.RenderThisFrame = true;
#endif
#ifdef USE_BLARGG_APU
S9xSetSoundMute(false);
#endif
Settings.HardDisableAudio = false;
S9xMainLoop();
#ifndef USE_BLARGG_APU
samples_to_play += samples_per_frame;
if (samples_to_play > 512)
{
S9xMixSamples(audio_buf, samples_to_play * 2);
Audio_Write(audio_buf, samples_to_play);
samples_to_play = 0;
}
#else
S9xAudioCallback();
#endif
#ifdef FRAMESKIP
if (IPPU.RenderThisFrame)
{
#endif
Update_Video_Ingame();
#ifdef FRAMESKIP
IPPU.RenderThisFrame = false;
}
else
{
IPPU.RenderThisFrame = true;
}
#endif
#ifdef FRAMESKIP
video_frames++;
newTick = Timer_Read();
if ( (newTick) - (lastTick) > 1000000)
{
FPS = video_frames;
video_frames = 0;
lastTick = newTick;
if (FPS >= 60)
{
FrameSkip = 0;
}
else
{
FrameSkip = 60 / FPS;
if (FrameSkip > 3) FrameSkip = 3;
}
}
#endif
}
bool Load_Game_Memory(char* game_path)
{
uint64_t fps;
CPU.Flags = 0;
if (!LoadROM(game_path))
return false;
Settings.FrameTime = (Settings.PAL ? Settings.FrameTimePAL : Settings.FrameTimeNTSC);
if (!Settings.PAL)
fps = (SNES_CLOCK_SPEED * 6.0 / (SNES_CYCLES_PER_SCANLINE * SNES_MAX_NTSC_VCOUNTER));
else
fps = (SNES_CLOCK_SPEED * 6.0 / (SNES_CYCLES_PER_SCANLINE * SNES_MAX_PAL_VCOUNTER));
samplerate = SOUND_OUTPUT_FREQUENCY;
Settings.SoundPlaybackRate = samplerate;
#ifndef USE_BLARGG_APU
samples_per_frame = samplerate / fps;
S9xSetPlaybackRate(Settings.SoundPlaybackRate);
#endif
return true;
}
void init_sfc_setting(void)
{
memset(&Settings, 0, sizeof(Settings));
Settings.JoystickEnabled = false;
Settings.SoundPlaybackRate = samplerate;
Settings.CyclesPercentage = 100;
Settings.DisableSoundEcho = false;
Settings.InterpolatedSound = true;
Settings.APUEnabled = true;
Settings.H_Max = SNES_CYCLES_PER_SCANLINE;
Settings.FrameTimePAL = 20000;
Settings.FrameTimeNTSC = 16667;
Settings.DisableMasterVolume = false;
Settings.Mouse = true;
Settings.SuperScope = true;
Settings.MultiPlayer5 = true;
Settings.ControllerOption = SNES_JOYPAD;
#ifdef USE_BLARGG_APU
Settings.SoundSync = true;
#endif
Settings.ApplyCheats = false;
Settings.HBlankStart = (256 * Settings.H_Max) / SNES_HCOUNTER_MAX;
}
void Init_SFC(void)
{
init_sfc_setting();
S9xInitMemory();
S9xInitAPU();
S9xInitDisplay();
S9xInitGFX();
#ifdef USE_BLARGG_APU
S9xInitSound(1000, 0); /* just give it a 1 second buffer */
S9xSetSamplesAvailableCallback(S9xAudioCallback);
#else
S9xInitSound();
#endif
CPU.SaveStateVersion = 0;
}
void Deinit_SFC(void)
{
if (Settings.SPC7110)
Del7110Gfx();
S9xDeinitGFX();
S9xDeinitDisplay();
S9xDeinitAPU();
S9xDeinitMemory();
}
/* Main entrypoint of the emulator */
int main(int argc, char* argv[])
{
int isloaded;
printf("Starting Snes9x2005\n");
if (argc < 2)
{
printf("Specify a ROM to load in memory\n");
return 0;
}
snprintf(GameName_emu, sizeof(GameName_emu), "%s", basename(argv[1]));
Init_SFC();
Init_Video();
Audio_Init();
overclock_cycles = false;
one_c = 4;
slow_one_c = 5;
two_c = 6;
reduce_sprite_flicker = false;
isloaded = Load_Game_Memory(argv[1]);
if (!isloaded)
{
printf("Could not load ROM in memory\n");
return 0;
}
Init_Configuration();
// get the game ready
while (!exit_snes)
{
switch(emulator_state)
{
case 0:
Emulation_Run();
break;
case 1:
Menu();
break;
}
}
Deinit_SFC();
Audio_Close();
Video_Close();
return 0;
}
|