1 /***********************************************************************************
2   Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3 
4   (c) Copyright 1996 - 2002  Gary Henderson (gary.henderson@ntlworld.com),
5                              Jerremy Koot (jkoot@snes9x.com)
6 
7   (c) Copyright 2002 - 2004  Matthew Kendora
8 
9   (c) Copyright 2002 - 2005  Peter Bortas (peter@bortas.org)
10 
11   (c) Copyright 2004 - 2005  Joel Yliluoma (http://iki.fi/bisqwit/)
12 
13   (c) Copyright 2001 - 2006  John Weidman (jweidman@slip.net)
14 
15   (c) Copyright 2002 - 2006  funkyass (funkyass@spam.shaw.ca),
16                              Kris Bleakley (codeviolation@hotmail.com)
17 
18   (c) Copyright 2002 - 2010  Brad Jorsch (anomie@users.sourceforge.net),
19                              Nach (n-a-c-h@users.sourceforge.net),
20                              zones (kasumitokoduck@yahoo.com)
21 
22   (c) Copyright 2006 - 2007  nitsuja
23 
24   (c) Copyright 2009 - 2010  BearOso,
25                              OV2
26 
27 
28   BS-X C emulator code
29   (c) Copyright 2005 - 2006  Dreamer Nom,
30                              zones
31 
32   C4 x86 assembler and some C emulation code
33   (c) Copyright 2000 - 2003  _Demo_ (_demo_@zsnes.com),
34                              Nach,
35                              zsKnight (zsknight@zsnes.com)
36 
37   C4 C++ code
38   (c) Copyright 2003 - 2006  Brad Jorsch,
39                              Nach
40 
41   DSP-1 emulator code
42   (c) Copyright 1998 - 2006  _Demo_,
43                              Andreas Naive (andreasnaive@gmail.com),
44                              Gary Henderson,
45                              Ivar (ivar@snes9x.com),
46                              John Weidman,
47                              Kris Bleakley,
48                              Matthew Kendora,
49                              Nach,
50                              neviksti (neviksti@hotmail.com)
51 
52   DSP-2 emulator code
53   (c) Copyright 2003         John Weidman,
54                              Kris Bleakley,
55                              Lord Nightmare (lord_nightmare@users.sourceforge.net),
56                              Matthew Kendora,
57                              neviksti
58 
59   DSP-3 emulator code
60   (c) Copyright 2003 - 2006  John Weidman,
61                              Kris Bleakley,
62                              Lancer,
63                              z80 gaiden
64 
65   DSP-4 emulator code
66   (c) Copyright 2004 - 2006  Dreamer Nom,
67                              John Weidman,
68                              Kris Bleakley,
69                              Nach,
70                              z80 gaiden
71 
72   OBC1 emulator code
73   (c) Copyright 2001 - 2004  zsKnight,
74                              pagefault (pagefault@zsnes.com),
75                              Kris Bleakley
76                              Ported from x86 assembler to C by sanmaiwashi
77 
78   SPC7110 and RTC C++ emulator code used in 1.39-1.51
79   (c) Copyright 2002         Matthew Kendora with research by
80                              zsKnight,
81                              John Weidman,
82                              Dark Force
83 
84   SPC7110 and RTC C++ emulator code used in 1.52+
85   (c) Copyright 2009         byuu,
86                              neviksti
87 
88   S-DD1 C emulator code
89   (c) Copyright 2003         Brad Jorsch with research by
90                              Andreas Naive,
91                              John Weidman
92 
93   S-RTC C emulator code
94   (c) Copyright 2001 - 2006  byuu,
95                              John Weidman
96 
97   ST010 C++ emulator code
98   (c) Copyright 2003         Feather,
99                              John Weidman,
100                              Kris Bleakley,
101                              Matthew Kendora
102 
103   Super FX x86 assembler emulator code
104   (c) Copyright 1998 - 2003  _Demo_,
105                              pagefault,
106                              zsKnight
107 
108   Super FX C emulator code
109   (c) Copyright 1997 - 1999  Ivar,
110                              Gary Henderson,
111                              John Weidman
112 
113   Sound emulator code used in 1.5-1.51
114   (c) Copyright 1998 - 2003  Brad Martin
115   (c) Copyright 1998 - 2006  Charles Bilyue'
116 
117   Sound emulator code used in 1.52+
118   (c) Copyright 2004 - 2007  Shay Green (gblargg@gmail.com)
119 
120   SH assembler code partly based on x86 assembler code
121   (c) Copyright 2002 - 2004  Marcus Comstedt (marcus@mc.pp.se)
122 
123   2xSaI filter
124   (c) Copyright 1999 - 2001  Derek Liauw Kie Fa
125 
126   HQ2x, HQ3x, HQ4x filters
127   (c) Copyright 2003         Maxim Stepin (maxim@hiend3d.com)
128 
129   NTSC filter
130   (c) Copyright 2006 - 2007  Shay Green
131 
132   GTK+ GUI code
133   (c) Copyright 2004 - 2010  BearOso
134 
135   Win32 GUI code
136   (c) Copyright 2003 - 2006  blip,
137                              funkyass,
138                              Matthew Kendora,
139                              Nach,
140                              nitsuja
141   (c) Copyright 2009 - 2010  OV2
142 
143   Mac OS GUI code
144   (c) Copyright 1998 - 2001  John Stiles
145   (c) Copyright 2001 - 2010  zones
146 
147   (c) Copyright 2010 - 2016 Daniel De Matteis. (UNDER NO CIRCUMSTANCE
148   WILL COMMERCIAL RIGHTS EVER BE APPROPRIATED TO ANY PARTY)
149 
150   (c) Copyright 2020         Mahyar Koshkouei
151 
152   Specific ports contains the works of other authors. See headers in
153   individual files.
154 
155 
156   Snes9x homepage: http://www.snes9x.com/
157 
158   Permission to use, copy, modify and/or distribute Snes9x in both binary
159   and source form, for non-commercial purposes, is hereby granted without
160   fee, providing that this license information and copyright notice appear
161   with all copies and any derived work.
162 
163   This software is provided 'as-is', without any express or implied
164   warranty. In no event shall the authors be held liable for any damages
165   arising from the use of this software or it's derivatives.
166 
167   Snes9x is freeware for PERSONAL USE only. Commercial users should
168   seek permission of the copyright holders first. Commercial use includes,
169   but is not limited to, charging money for Snes9x or software derived from
170   Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
171   using Snes9x as a promotion for your commercial product.
172 
173   The copyright holders request that bug fixes and improvements to the code
174   should be forwarded to them so everyone can benefit from the modifications
175   in future versions.
176 
177   Super NES and Super Nintendo Entertainment System are trademarks of
178   Nintendo Co., Limited and its subsidiary companies.
179  ***********************************************************************************/
180 
181 
182 #ifndef _SNES9X_H_
183 #define _SNES9X_H_
184 
185 #ifndef VERSION
186 #define VERSION	"2010"
187 #endif
188 
189 #ifdef __cplusplus
190 extern "C" {
191 #endif
192 
193 #include "port.h"
194 #include "65c816.h"
195 #include "messages.h"
196 
197 #define ACCESSORY_AUTODETECTION_CONFIRM   0
198 #define ACCESSORY_AUTODETECTION_ENABLED   1
199 #define ACCESSORY_AUTODETECTION_NONE      2
200 
201 #define S9X_ACCURACY_LEVEL		3
202 
203 
204 #ifndef SNES_SUPPORT_MULTI_CART
205 #define SNES_SUPPORT_MULTI_CART		0
206 #endif
207 
208 #include <boolean.h>
209 #include <streams/memory_stream.h>
210 
211  /* Create some sort of abstraction for files
212   * using memory only since libretro API is memory oriented.
213   *
214   * Very hacky, but at least allows us to avoid hacking up
215   * the whole source or go through the file system every time
216   * we want to do something. */
217 
218 #define STREAM memstream_t *
219 #define READ_STREAM(p, l, s)     memstream_read(s, p, l)
220 #define WRITE_STREAM(p, l, s)    memstream_write(s, p, l)
221 #define GETS_STREAM(p, l, s)     memstream_gets(s, p, l)
222 #define GETC_STREAM(s)           memstream_getc(s)
223 #define OPEN_STREAM()            memstream_open(0)
224 #define FIND_STREAM(f)           memstream_pos(f)
225 #define REVERT_STREAM(f, o, s)   memstream_seek(f, o, s)
226 #define CLOSE_STREAM(s)          memstream_close(s)
227 
228 #define SNES_WIDTH		256
229 #define SNES_HEIGHT		224
230 #define SNES_HEIGHT_EXTENDED	239
231 
232 /* MAX_SNES_WIDTH = SNES_WIDTH * 2 */
233 #define MAX_SNES_WIDTH		512
234 
235 /* MAX_SNES_HEIGHT = SNES_HEIGHT_EXTENDED * 2 */
236 #define MAX_SNES_HEIGHT		478
237 
238 #define	NTSC_MASTER_CLOCK	21477272.0
239 #define	PAL_MASTER_CLOCK	21281370.0
240 
241 #define SNES_AUDIO_FREQ		32040.0
242 
243 #define SNES_MAX_NTSC_VCOUNTER	262
244 #define SNES_MAX_PAL_VCOUNTER	312
245 #define SNES_HCOUNTER_MAX	341
246 
247 #define ONE_CYCLE						(overclock_cycles ? one_c : 6)
248 #define SLOW_ONE_CYCLE				(overclock_cycles ? slow_one_c : 8)
249 #define TWO_CYCLES					(overclock_cycles ? two_c : 12)
250 #define	ONE_DOT_CYCLE		4
251 #define ONE_DOT_CYCLE_DIV_2	2
252 
253 #define SNES_CYCLES_PER_SCANLINE	1364
254 
255 #define SNES_WRAM_REFRESH_HC_v2		538
256 #define SNES_WRAM_REFRESH_HC_v2_MIN_ONE_DOT_CYCLE	534
257 #define SNES_WRAM_REFRESH_CYCLES	40
258 
259 #define SNES_HBLANK_START_HC		1096		/* H=274 */
260 #define	SNES_HDMA_START_HC		1106		/* FIXME: not true */
261 #define	SNES_HBLANK_END_HC		4		/* H=1 */
262 #define	SNES_HDMA_INIT_HC		20		/* FIXME: not true */
263 
264 /* SNES_RENDER_START_HC = 48 * ONE_DOT_CYCLE */
265 #define	SNES_RENDER_START_HC		192		/* FIXME: Snes9x renders a line at a time. */
266 
267 #define SNES_TR_MASK			16
268 #define SNES_TL_MASK			32
269 #define SNES_X_MASK			64
270 #define SNES_A_MASK			128
271 #define SNES_RIGHT_MASK			256
272 #define SNES_LEFT_MASK			512
273 #define SNES_DOWN_MASK			1024
274 #define SNES_UP_MASK			2048
275 #define SNES_START_MASK			4096
276 #define SNES_SELECT_MASK		8192
277 #define SNES_Y_MASK			16384
278 #define SNES_B_MASK			32768
279 
280 #define DEBUG_MODE_FLAG			1	/* debugger */
281 #define TRACE_FLAG			2	/* debugger */
282 #define SINGLE_STEP_FLAG		4	/* debugger */
283 #define BREAK_FLAG			8	/* debugger */
284 #define SCAN_KEYS_FLAG			16	/* CPU */
285 #define NMI_FLAG			128	/* CPU */
286 #define IRQ_FLAG			2048	/* CPU */
287 #define HALTED_FLAG			4096	/* APU */
288 
289 extern bool overclock_cycles;
290 extern int one_c, slow_one_c, two_c;
291 
292 struct SCPUState
293 {
294 	int32	Cycles;
295 	int32	PrevCycles;
296 	int32	V_Counter;
297 	uint32	Flags;
298 	uint8	*PCBase;
299 	bool8	IRQActive;
300 	int32	IRQPending;
301 	int32	MemSpeed;
302 	int32	MemSpeedx2;
303 	int32	FastROMSpeed;
304 	bool8	InDMA;
305 	bool8	InHDMA;
306 	bool8	InDMAorHDMA;
307 	bool8	InWRAMDMAorHDMA;
308 	uint8	HDMARanInDMA;
309 	int32	CurrentDMAorHDMAChannel;
310 	uint8	WhichEvent;
311 	int32	NextEvent;
312 	bool8	WaitingForInterrupt;
313 };
314 
315 #define HC_HBLANK_START_EVENT	1
316 #define HC_IRQ_1_3_EVENT	2
317 #define HC_HDMA_START_EVENT	3
318 #define	HC_IRQ_3_5_EVENT	4
319 #define HC_HCOUNTER_MAX_EVENT	5
320 #define HC_IRQ_5_7_EVENT	6
321 #define HC_HDMA_INIT_EVENT	7
322 #define HC_IRQ_7_9_EVENT	8
323 #define HC_RENDER_EVENT		9
324 #define HC_IRQ_9_A_EVENT	10
325 #define HC_WRAM_REFRESH_EVENT	11
326 #define HC_IRQ_A_1_EVENT	12
327 
328 struct STimings
329 {
330 	int32	H_Max_Master;
331 	int32	H_Max;
332 	int32	V_Max_Master;
333 	int32	V_Max;
334 	int32	HBlankStart;
335 	int32	HBlankEnd;
336 	int32	HDMAInit;
337 	int32	HDMAStart;
338 	int32	NMITriggerPos;
339 	int32	WRAMRefreshPos;
340 	int32	RenderPos;
341 	bool8	InterlaceField;
342 	int32	DMACPUSync;	/* The cycles to synchronize DMA and CPU. Snes9x cannot emulate correctly. */
343 	int32	NMIDMADelay;	/* The delay of NMI trigger after DMA transfers. Snes9x cannot emulate correctly. */
344 	int32	IRQPendCount;	/* This value is just a hack, because Snes9x cannot emulate any events in an opcode. */
345 	int32	APUSpeedup;
346 	bool8	APUAllowTimeOverflow;
347 };
348 
349 struct SSettings
350 {
351 	bool8	SuperFX;
352 	uint8	DSP;
353 	bool8	SA1;
354 	bool8	C4;
355 	bool8	SDD1;
356 	bool8	SPC7110;
357 	bool8	SPC7110RTC;
358 	bool8	OBC1;
359 	uint8	SETA;
360 	bool8	SRTC;
361 	bool8	BS;
362 	bool8	BSXItself;
363 	bool8	BSXBootup;
364 
365 	bool8	ForceLoROM;
366 	bool8	ForceHiROM;
367 	bool8	ForceHeader;
368 	bool8	ForceNoHeader;
369 	bool8	ForceInterleaved;
370 	bool8	ForceInterleaved2;
371 	bool8	ForceInterleaveGD24;
372 	bool8	ForceNotInterleaved;
373 	bool8	ForcePAL;
374 	bool8	ForceNTSC;
375 	bool8	ApplyCheats;
376 	bool8	NoPatch;
377 	bool8	PAL;
378 	uint32	FrameTimePAL;
379 	uint32	FrameTimeNTSC;
380 
381 	uint32  SpeedhackGameID;
382 
383 	uint32	SoundPlaybackRate;
384 	uint32	SoundInputRate;
385 	bool8	Mute;
386 
387 	bool8	Multi;
388 	char	CartAName[PATH_MAX + 1];
389 	char	CartBName[PATH_MAX + 1];
390 
391 	bool8	DisableGameSpecificHacks;
392 	bool8	BlockInvalidVRAMAccessMaster;
393 	bool8	BlockInvalidVRAMAccess;
394 	int32	HDMATimingHack;
395 
396 	uint32	Throttled;
397 	int32_t	Crosshair;
398 
399 	uint32	AccessoryType;
400 	int32		AccessoryAutoDetection;
401 	bool8		CurrentROMisMultitapCompatible;
402 	bool8		CurrentROMisMouseCompatible;
403 	bool8		CurrentROMisSuperScopeCompatible;
404 	bool8		CurrentROMisJustifierCompatible;
405    bool8    NormalControls;
406 	bool8		SupportHiRes;
407 	bool8		Transparency;
408    float    SuperFXSpeedPerLine;
409    bool8	FastSavestates;
410    bool8	HardDisableAudio;
411 };
412 
413 struct SSNESGameFixes
414 {
415 	uint8	SRAMInitialValue;
416 	uint8	Uniracers;
417 };
418 
419 void S9xMessage (S9xMessagePriority p, S9xMessageCategory c, const char *msg);
420 
421 extern struct SSettings			Settings;
422 extern struct SCPUState			CPU;
423 extern struct STimings			Timings;
424 extern struct SSNESGameFixes	SNESGameFixes;
425 
426 /* Use when writing to $4016. */
427 extern void S9xSetJoypadLatch (bool8 latch);
428 
429 /* Use when reading $4016/7 (JOYSER0 and JOYSER1). */
430 extern uint8 S9xReadJOYSERn (int n);
431 
432 extern bool8 pad_read;
433 
434 #ifdef __cplusplus
435 }
436 #endif
437 
438 #endif
439