png screenshot support
[drnoksnes] / snes9x.h
1 /*
2  * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  *
4  * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5  *                           Jerremy Koot (jkoot@snes9x.com)
6  *
7  * Super FX C emulator code 
8  * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
9  *                           Gary Henderson.
10  * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
11  *
12  * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13  * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14  * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
15  *
16  * DOS port code contains the works of other authors. See headers in
17  * individual files.
18  *
19  * Snes9x homepage: http://www.snes9x.com
20  *
21  * Permission to use, copy, modify and distribute Snes9x in both binary and
22  * source form, for non-commercial purposes, is hereby granted without fee,
23  * providing that this license information and copyright notice appear with
24  * all copies and any derived work.
25  *
26  * This software is provided 'as-is', without any express or implied
27  * warranty. In no event shall the authors be held liable for any damages
28  * arising from the use of this software.
29  *
30  * Snes9x is freeware for PERSONAL USE only. Commercial users should
31  * seek permission of the copyright holders first. Commercial use includes
32  * charging money for Snes9x or software derived from Snes9x.
33  *
34  * The copyright holders request that bug fixes and improvements to the code
35  * should be forwarded to them so everyone can benefit from the modifications
36  * in future versions.
37  *
38  * Super NES and Super Nintendo Entertainment System are trademarks of
39  * Nintendo Co., Limited and its subsidiary companies.
40  */
41 #ifndef _SNES9X_H_
42 #define _SNES9X_H_
43
44 #define VERSION "1.39"
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include "port.h"
49 #include "65c816.h"
50 #include "messages.h"
51
52 #if defined(USE_GLIDE) && !defined(GFX_MULTI_FORMAT)
53 #define GFX_MULTI_FORMAT
54 #endif
55
56 #define ROM_NAME_LEN 23
57
58 /* SNES screen width and height */
59 #define SNES_WIDTH              256
60 #define SNES_HEIGHT             224
61 #define SNES_HEIGHT_EXTENDED    239
62 #define IMAGE_WIDTH             (Settings.SupportHiRes ? SNES_WIDTH * 2 : SNES_WIDTH)
63 #define IMAGE_HEIGHT            (Settings.SupportHiRes ? SNES_HEIGHT_EXTENDED * 2 : SNES_HEIGHT_EXTENDED)
64
65 #define SNES_MAX_NTSC_VCOUNTER  262
66 #define SNES_MAX_PAL_VCOUNTER   312
67 #define SNES_HCOUNTER_MAX       342
68 #define SPC700_TO_65C816_RATIO  2
69 #define AUTO_FRAMERATE          200
70
71 #define PPU_IGNORE_FIXEDCOLCHANGES                      (1<<0)
72 #define PPU_IGNORE_WINDOW                                       (1<<1)
73 #define PPU_IGNORE_ADDSUB                                       (1<<2)
74 #define PPU_IGNORE_PALWRITE                                     (1<<3)
75 #define GFX_IGNORE_OBJ                                          (1<<4)
76 #define GFX_IGNORE_BG0                                          (1<<5)
77 #define GFX_IGNORE_BG1                                          (1<<6)
78 #define GFX_IGNORE_BG2                                          (1<<7)
79 #define GFX_IGNORE_BG3                                          (1<<8)
80
81 // NTSC master clock signal 21.47727MHz
82 // PPU: master clock / 4
83 // 1 / PPU clock * 342 -> 63.695us
84 // 63.695us / (1 / 3.579545MHz) -> 228 cycles per scanline
85 // From Earth Worm Jim: APU executes an average of 65.14285714 cycles per
86 // scanline giving an APU clock speed of 1.022731096MHz
87
88 // PAL master clock signal 21.28137MHz
89 // PPU: master clock / 4
90 // 1 / PPU clock * 342 -> 64.281us
91 // 64.281us / (1 / 3.546895MHz) -> 228 cycles per scanline.
92
93 //#define SNES_SCANLINE_TIME (63.695e-6)
94 //#define SNES_CLOCK_SPEED (3579545)
95
96 //#define SNES_CLOCK_LEN (1.0 / SNES_CLOCK_SPEED)
97
98 #ifdef VAR_CYCLES
99 //#define SNES_CYCLES_PER_SCANLINE ((uint32) ((SNES_SCANLINE_TIME / SNES_CLOCK_LEN) * 6 + 0.5))
100 #define SNES_CYCLES_PER_SCANLINE ((uint32)(228*6))
101 #else
102 #define SNES_CYCLES_PER_SCANLINE ((uint32) (SNES_SCANLINE_TIME / SNES_CLOCK_LEN + 0.5))
103 #endif
104
105 #define SNES_TR_MASK        (1 << 4)
106 #define SNES_TL_MASK        (1 << 5)
107 #define SNES_X_MASK         (1 << 6)
108 #define SNES_A_MASK         (1 << 7)
109 #define SNES_RIGHT_MASK     (1 << 8)
110 #define SNES_LEFT_MASK      (1 << 9)
111 #define SNES_DOWN_MASK      (1 << 10)
112 #define SNES_UP_MASK        (1 << 11)
113 #define SNES_START_MASK     (1 << 12)
114 #define SNES_SELECT_MASK    (1 << 13)
115 #define SNES_Y_MASK         (1 << 14)
116 #define SNES_B_MASK         (1 << 15)
117
118 enum {
119     SNES_MULTIPLAYER5,
120     SNES_JOYPAD,
121     SNES_MOUSE_SWAPPED,
122     SNES_MOUSE,
123     SNES_SUPERSCOPE,
124         SNES_JUSTIFIER,
125         SNES_JUSTIFIER_2,
126     SNES_MAX_CONTROLLER_OPTIONS
127 };
128
129 #define DEBUG_MODE_FLAG     (1 << 0)
130 #define TRACE_FLAG                      (1 << 1)
131 #define SINGLE_STEP_FLAG    (1 << 2)
132 #define BREAK_FLAG                      (1 << 3)
133 #define SCAN_KEYS_FLAG      (1 << 4)
134 #define SAVE_SNAPSHOT_FLAG  (1 << 5)
135 #define DELAYED_NMI_FLAG    (1 << 6)
136 #define NMI_FLAG                        (1 << 7)
137 #define PROCESS_SOUND_FLAG  (1 << 8)
138 #define FRAME_ADVANCE_FLAG  (1 << 9)
139 #define DELAYED_NMI_FLAG2   (1 << 10)
140 #define IRQ_PENDING_FLAG    (1 << 11)
141
142 #ifdef VAR_CYCLES
143 #define ONE_CYCLE 6
144 #define SLOW_ONE_CYCLE 8
145 #define TWO_CYCLES 12
146 #else
147 #define ONE_CYCLE 1
148 #define SLOW_ONE_CYCLE 1
149 #define TWO_CYCLES 2
150 #endif
151
152
153 #undef MEMMAP_BLOCK_SIZE
154 #define MEMMAP_BLOCK_SIZE (0x1000)
155 #undef MEMMAP_NUM_BLOCKS
156 #define MEMMAP_NUM_BLOCKS (0x1000000 / MEMMAP_BLOCK_SIZE)
157
158 struct SCPUState{
159     uint32  Flags;                                      //0
160     bool8   BranchSkip;                         //4
161     bool8   NMIActive;                          //5
162     bool8   IRQActive;                          //6
163     bool8   WaitingForInterrupt;        //7
164     struct SRegisters Regs;                     //8
165                 //uint8  PB;                            //8
166                 //uint8  DB;                            //9
167                 //pair   P;                                     //10
168                 //pair   A;                                     //12
169                 //pair   D;                                     //14
170                 //pair   X;                                     //16
171                 //pair   S;                                     //18
172                 //pair   Y;                                     //20
173             //uint16 PC;                                //22
174     uint8   *PC;                                        //24
175     int32   Cycles;                                     //28
176     uint8   *PCBase;                            //32
177     uint8   *PCAtOpcodeStart;           //36
178     uint8   *WaitAddress;                       //40
179     uint32  WaitCounter;                        //44
180     int32   NextEvent;                          //48
181     int32   V_Counter;                          //52
182     int32   MemSpeed;                           //56
183     int32   MemSpeedx2;                         //60
184         int32   FastROMSpeed;                   //64
185     uint32 AutoSaveTimer;               //68
186     uint32 NMITriggerPoint;             //72
187     uint32 NMICycleCount;                       //76
188     uint32 IRQCycleCount;                       //80
189
190     bool8   InDMA;                                      //84
191     uint8   WhichEvent;                         //85
192         bool8  SRAMModified;                    //86
193         bool8  BRKTriggered;                    //87
194         uint32  _ARM_asm_reserved_1;    //88  to stock current jmp table
195     bool8  TriedInterleavedMode2;       //92
196     bool8  _ARM_asm_padding1[3];        //93
197     
198     uint8*      Memory_Map;                             //96
199     uint8*      Memory_WriteMap;                //100
200     uint8*      Memory_MemorySpeed;             //104
201     uint8*      Memory_BlockIsRAM;              //108
202     uint8*      Memory_SRAM;                    //112
203     uint8*      Memory_BWRAM;                   //116
204     uint16      Memory_SRAMMask;                //120
205     bool8       APU_APUExecuting;               //122
206     bool8       _ARM_asm_padding2;              //123
207         int32   APU_Cycles;                             //124 notaz
208 };
209
210
211 #define HBLANK_START_EVENT 0
212 #define HBLANK_END_EVENT 1
213 #define HTIMER_BEFORE_EVENT 2
214 #define HTIMER_AFTER_EVENT 3
215 #define NO_EVENT 4
216
217 struct SSettings{
218     // CPU options
219     bool8  APUEnabled;
220     bool8  Shutdown;
221     uint8  SoundSkipMethod;
222     long   H_Max;
223     long   HBlankStart;
224     long   CyclesPercentage;
225     bool8  DisableIRQ;
226     bool8  Paused;
227     bool8  ForcedPause;
228     bool8  StopEmulation;
229
230     // Tracing options
231     bool8  TraceDMA;
232     bool8  TraceHDMA;
233     bool8  TraceVRAM;
234     bool8  TraceUnknownRegisters;
235     bool8  TraceDSP;
236
237     // Joystick options
238     bool8  SwapJoypads;
239     bool8  JoystickEnabled;
240
241     // ROM timing options (see also H_Max above)
242     bool8  ForcePAL;
243     bool8  ForceNTSC;
244     bool8  PAL;
245     uint32 FrameTimePAL;
246     uint32 FrameTimeNTSC;
247     uint32 FrameTime;
248     uint32 SkipFrames;
249
250     // ROM image options
251     bool8  ForceLoROM;
252     bool8  ForceHiROM;
253     bool8  ForceHeader;
254     bool8  ForceNoHeader;
255     bool8  ForceInterleaved;
256     bool8  ForceInterleaved2;
257     bool8  ForceNotInterleaved;
258
259     // Peripherial options
260     bool8  ForceSuperFX;
261     bool8  ForceNoSuperFX;
262     bool8  ForceDSP1;
263     bool8  ForceNoDSP1;
264     bool8  ForceSA1;
265     bool8  ForceNoSA1;
266     bool8  ForceC4;
267     bool8  ForceNoC4;
268     bool8  ForceSDD1;
269     bool8  ForceNoSDD1;
270     bool8  MultiPlayer5;
271     bool8  Mouse;
272     bool8  SuperScope;
273     bool8  SRTC;
274     uint32 ControllerOption;
275     
276     bool8  ShutdownMaster;
277     bool8  MultiPlayer5Master;
278     bool8  SuperScopeMaster;
279     bool8  MouseMaster;
280     bool8  SuperFX;
281     bool8  DSP1Master;
282     bool8  SA1;
283     bool8  C4;
284     bool8  SDD1;
285
286     // Sound options
287     uint32 SoundPlaybackRate;
288     bool8  TraceSoundDSP;
289     bool8  Stereo;
290     bool8  ReverseStereo;
291     bool8  SixteenBitSound;
292     int    SoundBufferSize;
293     int    SoundMixInterval;
294     bool8  SoundEnvelopeHeightReading;
295     bool8  DisableSoundEcho;
296     bool8  DisableSampleCaching;
297     bool8  DisableMasterVolume;
298     bool8  SoundSync;
299     bool8  InterpolatedSound;
300     bool8  ThreadSound;
301     bool8  Mute;
302 //    bool8  NextAPUEnabled;
303     uint8  AltSampleDecode;
304     bool8  FixFrequency;
305     
306     // Graphics options
307     bool8  Transparency;
308     bool8  SupportHiRes;
309     bool8  Mode7Interpolate;
310
311     // SNES graphics options
312     bool8  BGLayering;
313     bool8  DisableGraphicWindows;
314     bool8  ForceTransparency;
315     bool8  ForceNoTransparency;
316     bool8  DisableHDMA;
317     bool8  DisplayFrameRate;
318
319     // Others
320     bool8  NetPlay;
321     bool8  NetPlayServer;
322     char   ServerName [128];
323     int    Port;
324     bool8  GlideEnable;
325     bool8  OpenGLEnable;
326     int32  AutoSaveDelay; // Time in seconds before S-RAM auto-saved if modified.
327     bool8  ApplyCheats;
328     bool8  TurboMode;
329     uint32 TurboSkipFrames;
330     uint32 AutoMaxSkipFrames;
331         uint32 os9x_hack;
332     
333 // Fixes for individual games
334     uint32 StrikeGunnerOffsetHack;
335     bool8  ChuckRock;
336     bool8  StarfoxHack;
337     bool8  WinterGold;
338     bool8  Dezaemon;
339     bool8  WrestlemaniaArcade;
340     bool8  BS;  // Japanese Satellite System games.
341     bool8  DaffyDuck;
342     uint8  APURAMInitialValue;
343     bool8  SDD1Pack;
344     
345         // notaz
346         uint32 GfxLayerMask;
347
348 // Hacks
349         bool8   HacksEnabled;
350         bool8   HacksFilter;
351 };
352
353 struct SSNESGameFixes
354 {
355     uint8 NeedInit0x2137;
356     uint8 umiharakawaseFix;
357     uint8 alienVSpredetorFix;
358     uint8 APU_OutPorts_ReturnValueFix;
359     uint8 Old_Read0x4200;
360     uint8 _0x213E_ReturnValue;
361     uint8 TouhaidenControllerFix;
362     uint8 SoundEnvelopeHeightReading2;
363     uint8 SRAMInitialValue;
364 };
365
366 START_EXTERN_C
367 extern struct SSettings Settings;
368 extern struct SCPUState CPU;
369 extern struct SSNESGameFixes SNESGameFixes;
370 extern char String [513];
371
372 void S9xExit ();
373 void S9xMessage (int type, int number, const char *message);
374 void S9xLoadSDD1Data ();
375 END_EXTERN_C
376
377 START_EXTERN_C
378 enum {
379     PAUSE_NETPLAY_CONNECT = (1 << 0),
380     PAUSE_TOGGLE_FULL_SCREEN = (1 << 1),
381     PAUSE_EXIT = (1 << 2),
382     PAUSE_MENU = (1 << 3),
383     PAUSE_INACTIVE_WINDOW = (1 << 4),
384     PAUSE_WINDOW_ICONISED = (1 << 5),
385     PAUSE_RESTORE_GUI = (1 << 6),
386     PAUSE_FREEZE_FILE = (1 << 7)
387 };
388 void S9xSetPause (uint32 mask);
389 void S9xClearPause (uint32 mask);
390 END_EXTERN_C
391
392 START_EXTERN_C
393 enum FileTypes {
394         FILE_ROM = 0,
395         FILE_SRAM,
396         /** The default freeze filename (base.frz.gz) */
397         FILE_FREEZE,
398         FILE_CHT,
399         FILE_IPS,
400         FILE_SCREENSHOT,
401         FILE_SDD1_DAT
402 };
403 /** This routine allows to get path to files whose name depends on the basename
404  *  of the current ROM.
405  *  @param file see enum FileTypes.
406  *  @return wanted filepath. Do not free the returned string.
407  */
408 const char *S9xGetFilename(enum FileTypes file);
409 /** Returns the path to freeze file for the selected quick save slot.
410  *  @param slot slot number
411  *  @return wanted filepath. Do not free the returned string.
412  */
413 const char *S9xGetQuickSaveFilename(unsigned int slot);
414 END_EXTERN_C
415
416 #endif