Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/xnes/snes9x/fxinst.h
28515 views
1
/***********************************************************************************
2
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3
4
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),
5
Jerremy Koot ([email protected])
6
7
(c) Copyright 2002 - 2004 Matthew Kendora
8
9
(c) Copyright 2002 - 2005 Peter Bortas ([email protected])
10
11
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
12
13
(c) Copyright 2001 - 2006 John Weidman ([email protected])
14
15
(c) Copyright 2002 - 2006 funkyass ([email protected]),
16
Kris Bleakley ([email protected])
17
18
(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),
19
Nach ([email protected]),
20
21
(c) Copyright 2002 - 2011 zones ([email protected])
22
23
(c) Copyright 2006 - 2007 nitsuja
24
25
(c) Copyright 2009 - 2011 BearOso,
26
OV2
27
28
29
BS-X C emulator code
30
(c) Copyright 2005 - 2006 Dreamer Nom,
31
zones
32
33
C4 x86 assembler and some C emulation code
34
(c) Copyright 2000 - 2003 _Demo_ ([email protected]),
35
Nach,
36
zsKnight ([email protected])
37
38
C4 C++ code
39
(c) Copyright 2003 - 2006 Brad Jorsch,
40
Nach
41
42
DSP-1 emulator code
43
(c) Copyright 1998 - 2006 _Demo_,
44
Andreas Naive ([email protected]),
45
Gary Henderson,
46
Ivar ([email protected]),
47
John Weidman,
48
Kris Bleakley,
49
Matthew Kendora,
50
Nach,
51
neviksti ([email protected])
52
53
DSP-2 emulator code
54
(c) Copyright 2003 John Weidman,
55
Kris Bleakley,
56
Lord Nightmare ([email protected]),
57
Matthew Kendora,
58
neviksti
59
60
DSP-3 emulator code
61
(c) Copyright 2003 - 2006 John Weidman,
62
Kris Bleakley,
63
Lancer,
64
z80 gaiden
65
66
DSP-4 emulator code
67
(c) Copyright 2004 - 2006 Dreamer Nom,
68
John Weidman,
69
Kris Bleakley,
70
Nach,
71
z80 gaiden
72
73
OBC1 emulator code
74
(c) Copyright 2001 - 2004 zsKnight,
75
pagefault ([email protected]),
76
Kris Bleakley
77
Ported from x86 assembler to C by sanmaiwashi
78
79
SPC7110 and RTC C++ emulator code used in 1.39-1.51
80
(c) Copyright 2002 Matthew Kendora with research by
81
zsKnight,
82
John Weidman,
83
Dark Force
84
85
SPC7110 and RTC C++ emulator code used in 1.52+
86
(c) Copyright 2009 byuu,
87
neviksti
88
89
S-DD1 C emulator code
90
(c) Copyright 2003 Brad Jorsch with research by
91
Andreas Naive,
92
John Weidman
93
94
S-RTC C emulator code
95
(c) Copyright 2001 - 2006 byuu,
96
John Weidman
97
98
ST010 C++ emulator code
99
(c) Copyright 2003 Feather,
100
John Weidman,
101
Kris Bleakley,
102
Matthew Kendora
103
104
Super FX x86 assembler emulator code
105
(c) Copyright 1998 - 2003 _Demo_,
106
pagefault,
107
zsKnight
108
109
Super FX C emulator code
110
(c) Copyright 1997 - 1999 Ivar,
111
Gary Henderson,
112
John Weidman
113
114
Sound emulator code used in 1.5-1.51
115
(c) Copyright 1998 - 2003 Brad Martin
116
(c) Copyright 1998 - 2006 Charles Bilyue'
117
118
Sound emulator code used in 1.52+
119
(c) Copyright 2004 - 2007 Shay Green ([email protected])
120
121
SH assembler code partly based on x86 assembler code
122
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
123
124
2xSaI filter
125
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
126
127
HQ2x, HQ3x, HQ4x filters
128
(c) Copyright 2003 Maxim Stepin ([email protected])
129
130
NTSC filter
131
(c) Copyright 2006 - 2007 Shay Green
132
133
GTK+ GUI code
134
(c) Copyright 2004 - 2011 BearOso
135
136
Win32 GUI code
137
(c) Copyright 2003 - 2006 blip,
138
funkyass,
139
Matthew Kendora,
140
Nach,
141
nitsuja
142
(c) Copyright 2009 - 2011 OV2
143
144
Mac OS GUI code
145
(c) Copyright 1998 - 2001 John Stiles
146
(c) Copyright 2001 - 2011 zones
147
148
149
Specific ports contains the works of other authors. See headers in
150
individual files.
151
152
153
Snes9x homepage: http://www.snes9x.com/
154
155
Permission to use, copy, modify and/or distribute Snes9x in both binary
156
and source form, for non-commercial purposes, is hereby granted without
157
fee, providing that this license information and copyright notice appear
158
with all copies and any derived work.
159
160
This software is provided 'as-is', without any express or implied
161
warranty. In no event shall the authors be held liable for any damages
162
arising from the use of this software or it's derivatives.
163
164
Snes9x is freeware for PERSONAL USE only. Commercial users should
165
seek permission of the copyright holders first. Commercial use includes,
166
but is not limited to, charging money for Snes9x or software derived from
167
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
168
using Snes9x as a promotion for your commercial product.
169
170
The copyright holders request that bug fixes and improvements to the code
171
should be forwarded to them so everyone can benefit from the modifications
172
in future versions.
173
174
Super NES and Super Nintendo Entertainment System are trademarks of
175
Nintendo Co., Limited and its subsidiary companies.
176
***********************************************************************************/
177
178
179
#ifndef _FXINST_H_
180
#define _FXINST_H_
181
182
/*
183
* FxChip(GSU) register space specification
184
* (Register address space 3000-32ff)
185
*
186
* The 16 generic 16 bit registers:
187
* (Some have a special function in special circumstances)
188
* 3000 - R0 default source/destination register
189
* 3002 - R1 pixel plot X position register
190
* 3004 - R2 pixel plot Y position register
191
* 3006 - R3
192
* 3008 - R4 lower 16 bit result of lmult
193
* 300a - R5
194
* 300c - R6 multiplier for fmult and lmult
195
* 300e - R7 fixed point texel X position for merge
196
* 3010 - R8 fixed point texel Y position for merge
197
* 3012 - R9
198
* 3014 - R10
199
* 3016 - R11 return address set by link
200
* 3018 - R12 loop counter
201
* 301a - R13 loop point address
202
* 301c - R14 rom address for getb, getbh, getbl, getbs
203
* 301e - R15 program counter
204
*
205
* 3020-302f - unused
206
*
207
* Other internal registers
208
* 3030 - SFR status flag register (16bit)
209
* 3032 - unused
210
* 3033 - BRAMR Backup RAM register (8bit)
211
* 3034 - PBR program bank register (8bit)
212
* 3035 - unused
213
* 3036 - ROMBR rom bank register (8bit)
214
* 3037 - CFGR control flags register (8bit)
215
* 3038 - SCBR screen base register (8bit)
216
* 3039 - CLSR clock speed register (8bit)
217
* 303a - SCMR screen mode register (8bit)
218
* 303b - VCR version code register (8bit) (read only)
219
* 303c - RAMBR ram bank register (8bit)
220
* 303d - unused
221
* 303e - CBR cache base register (16bit)
222
*
223
* 3040-30ff - unused
224
*
225
* 3100-32ff - CACHERAM 512 bytes of GSU cache memory
226
*
227
* SFR status flag register bits:
228
* 0 -
229
* 1 Z Zero flag
230
* 2 CY Carry flag
231
* 3 S Sign flag
232
* 4 OV Overflow flag
233
* 5 G Go flag (set to 1 when the GSU is running)
234
* 6 R Set to 1 when reading ROM using R14 address
235
* 7 -
236
* 8 ALT1 Mode set-up flag for the next instruction
237
* 9 ALT2 Mode set-up flag for the next instruction
238
* 10 IL Immediate lower 8-bit flag
239
* 11 IH Immediate higher 8-bit flag
240
* 12 B Set to 1 when the WITH instruction is executed
241
* 13 -
242
* 14 -
243
* 15 IRQ Set to 1 when GSU caused an interrupt
244
* Set to 0 when read by 658c16
245
*
246
* BRAMR = 0, BackupRAM is disabled
247
* BRAMR = 1, BackupRAM is enabled
248
*
249
* CFGR control flags register bits:
250
* 0 -
251
* 1 -
252
* 2 -
253
* 3 -
254
* 4 -
255
* 5 MS0 Multiplier speed, 0=standard, 1=high speed
256
* 6 -
257
* 7 IRQ Set to 1 when GSU interrupt request is masked
258
*
259
* CLSR clock speed register bits:
260
* 0 CLSR clock speed, 0 = 10.7Mhz, 1 = 21.4Mhz
261
*
262
* SCMR screen mode register bits:
263
* 0 MD0 color depth mode bit 0
264
* 1 MD1 color depth mode bit 1
265
* 2 HT0 screen height bit 1
266
* 3 RAN RAM access control
267
* 4 RON ROM access control
268
* 5 HT1 screen height bit 2
269
* 6 -
270
* 7 -
271
*
272
* RON = 0 SNES CPU has ROM access
273
* RON = 1 GSU has ROM access
274
*
275
* RAN = 0 SNES has game pak RAM access
276
* RAN = 1 GSU has game pak RAM access
277
*
278
* HT1 HT0 Screen height mode
279
* 0 0 128 pixels high
280
* 0 1 160 pixels high
281
* 1 0 192 pixels high
282
* 1 1 OBJ mode
283
*
284
* MD1 MD0 Color depth mode
285
* 0 0 4 color mode
286
* 0 1 16 color mode
287
* 1 0 not used
288
* 1 1 256 color mode
289
*
290
* CBR cache base register bits:
291
* 15-4 Specify base address for data to cache from ROM or RAM
292
* 3-0 Are 0 when address is read
293
*
294
* Write access to the program counter (301e) from
295
* the SNES-CPU will start the GSU, and it will not
296
* stop until it reaches a stop instruction.
297
*
298
*/
299
300
// Number of banks in GSU RAM
301
#define FX_RAM_BANKS 4
302
303
// Emulate proper R14 ROM access (slower, but safer)
304
#define FX_DO_ROMBUFFER
305
306
// Address checking (definately slow)
307
//#define FX_ADDRESS_CHECK
308
309
struct FxRegs_s
310
{
311
// FxChip registers
312
uint32 avReg[16]; // 16 Generic registers
313
uint32 vColorReg; // Internal color register
314
uint32 vPlotOptionReg; // Plot option register
315
uint32 vStatusReg; // Status register
316
uint32 vPrgBankReg; // Program bank index register
317
uint32 vRomBankReg; // Rom bank index register
318
uint32 vRamBankReg; // Ram bank index register
319
uint32 vCacheBaseReg; // Cache base address register
320
uint32 vCacheFlags; // Saying what parts of the cache was written to
321
uint32 vLastRamAdr; // Last RAM address accessed
322
uint32 *pvDreg; // Pointer to current destination register
323
uint32 *pvSreg; // Pointer to current source register
324
uint8 vRomBuffer; // Current byte read by R14
325
uint8 vPipe; // Instructionset pipe
326
uint32 vPipeAdr; // The address of where the pipe was read from
327
328
// Status register optimization stuff
329
uint32 vSign; // v & 0x8000
330
uint32 vZero; // v == 0
331
uint32 vCarry; // a value of 1 or 0
332
int32 vOverflow; // (v >= 0x8000 || v < -0x8000)
333
334
// Other emulator variables
335
int32 vErrorCode;
336
uint32 vIllegalAddress;
337
338
uint8 bBreakPoint;
339
uint32 vBreakPoint;
340
uint32 vStepPoint;
341
342
uint8 *pvRegisters; // 768 bytes located in the memory at address 0x3000
343
uint32 nRamBanks; // Number of 64kb-banks in FxRam (Don't confuse it with SNES-Ram!!!)
344
uint8 *pvRam; // Pointer to FxRam
345
uint32 nRomBanks; // Number of 32kb-banks in Cart-ROM
346
uint8 *pvRom; // Pointer to Cart-ROM
347
348
uint32 vMode; // Color depth/mode
349
uint32 vPrevMode; // Previous depth
350
uint8 *pvScreenBase;
351
uint8 *apvScreen[32]; // Pointer to each of the 32 screen colums
352
int32 x[32];
353
uint32 vScreenHeight; // 128, 160, 192 or 256 (could be overriden by cmode)
354
uint32 vScreenRealHeight; // 128, 160, 192 or 256
355
uint32 vPrevScreenHeight;
356
uint32 vScreenSize;
357
void (*pfPlot) (void);
358
void (*pfRpix) (void);
359
360
uint8 *pvRamBank; // Pointer to current RAM-bank
361
uint8 *pvRomBank; // Pointer to current ROM-bank
362
uint8 *pvPrgBank; // Pointer to current program ROM-bank
363
364
uint8 *apvRamBank[FX_RAM_BANKS]; // Ram bank table (max 256kb)
365
uint8 *apvRomBank[256]; // Rom bank table
366
367
uint8 bCacheActive;
368
uint8 *pvCache; // Pointer to the GSU cache
369
uint8 avCacheBackup[512]; // Backup of ROM when the cache has replaced it
370
uint32 vCounter;
371
uint32 vInstCount;
372
uint32 vSCBRDirty; // If SCBR is written, our cached screen pointers need updating
373
374
uint8 *avRegAddr; // To reference avReg in snapshot.cpp
375
};
376
377
extern struct FxRegs_s GSU;
378
379
// GSU registers
380
#define GSU_R0 0x000
381
#define GSU_R1 0x002
382
#define GSU_R2 0x004
383
#define GSU_R3 0x006
384
#define GSU_R4 0x008
385
#define GSU_R5 0x00a
386
#define GSU_R6 0x00c
387
#define GSU_R7 0x00e
388
#define GSU_R8 0x010
389
#define GSU_R9 0x012
390
#define GSU_R10 0x014
391
#define GSU_R11 0x016
392
#define GSU_R12 0x018
393
#define GSU_R13 0x01a
394
#define GSU_R14 0x01c
395
#define GSU_R15 0x01e
396
#define GSU_SFR 0x030
397
#define GSU_BRAMR 0x033
398
#define GSU_PBR 0x034
399
#define GSU_ROMBR 0x036
400
#define GSU_CFGR 0x037
401
#define GSU_SCBR 0x038
402
#define GSU_CLSR 0x039
403
#define GSU_SCMR 0x03a
404
#define GSU_VCR 0x03b
405
#define GSU_RAMBR 0x03c
406
#define GSU_CBR 0x03e
407
#define GSU_CACHERAM 0x100
408
409
// SFR flags
410
#define FLG_Z (1 << 1)
411
#define FLG_CY (1 << 2)
412
#define FLG_S (1 << 3)
413
#define FLG_OV (1 << 4)
414
#define FLG_G (1 << 5)
415
#define FLG_R (1 << 6)
416
#define FLG_ALT1 (1 << 8)
417
#define FLG_ALT2 (1 << 9)
418
#define FLG_IL (1 << 10)
419
#define FLG_IH (1 << 11)
420
#define FLG_B (1 << 12)
421
#define FLG_IRQ (1 << 15)
422
423
// Test flag
424
#define TF(a) (GSU.vStatusReg & FLG_##a)
425
#define CF(a) (GSU.vStatusReg &= ~FLG_##a)
426
#define SF(a) (GSU.vStatusReg |= FLG_##a)
427
428
// Test and set flag if condition, clear if not
429
#define TS(a, b) GSU.vStatusReg = ((GSU.vStatusReg & (~FLG_##a)) | ((!!(##b)) * FLG_##a))
430
431
// Testing ALT1 & ALT2 bits
432
#define ALT0 (!TF(ALT1) && !TF(ALT2))
433
#define ALT1 ( TF(ALT1) && !TF(ALT2))
434
#define ALT2 (!TF(ALT1) && TF(ALT2))
435
#define ALT3 ( TF(ALT1) && TF(ALT2))
436
437
// Sign extend from 8/16 bit to 32 bit
438
#define SEX8(a) ((int32) ((int8) (a)))
439
#define SEX16(a) ((int32) ((int16) (a)))
440
441
// Unsign extend from 8/16 bit to 32 bit
442
#define USEX8(a) ((uint32) ((uint8) (a)))
443
#define USEX16(a) ((uint32) ((uint16) (a)))
444
#define SUSEX16(a) ((int32) ((uint16) (a)))
445
446
// Set/Clr Sign and Zero flag
447
#define TSZ(num) TS(S, ((num) & 0x8000)); TS(Z, (!USEX16(num)))
448
449
// Clear flags
450
#define CLRFLAGS GSU.vStatusReg &= ~(FLG_ALT1 | FLG_ALT2 | FLG_B); GSU.pvDreg = GSU.pvSreg = &R0
451
452
// Read current RAM-Bank
453
#define RAM(adr) GSU.pvRamBank[USEX16(adr)]
454
455
// Read current ROM-Bank
456
#define ROM(idx) GSU.pvRomBank[USEX16(idx)]
457
458
// Access the current value in the pipe
459
#define PIPE GSU.vPipe
460
461
// Access data in the current program bank
462
#define PRGBANK(idx) GSU.pvPrgBank[USEX16(idx)]
463
464
// Update pipe from ROM
465
#if 0
466
#define FETCHPIPE { PIPE = PRGBANK(R15); GSU.vPipeAdr = (GSU.vPrgBankReg << 16) + R15; }
467
#else
468
#define FETCHPIPE { PIPE = PRGBANK(R15); }
469
#endif
470
471
// ABS
472
#define ABS(x) ((x) < 0 ? -(x) : (x))
473
474
// Access source register
475
#define SREG (*GSU.pvSreg)
476
477
// Access destination register
478
#define DREG (*GSU.pvDreg)
479
480
#ifndef FX_DO_ROMBUFFER
481
482
// Don't read R14
483
#define READR14
484
485
// Don't test and/or read R14
486
#define TESTR14
487
488
#else
489
490
// Read R14
491
#define READR14 GSU.vRomBuffer = ROM(R14)
492
493
// Test and/or read R14
494
#define TESTR14 if (GSU.pvDreg == &R14) READR14
495
496
#endif
497
498
// Access to registers
499
#define R0 GSU.avReg[0]
500
#define R1 GSU.avReg[1]
501
#define R2 GSU.avReg[2]
502
#define R3 GSU.avReg[3]
503
#define R4 GSU.avReg[4]
504
#define R5 GSU.avReg[5]
505
#define R6 GSU.avReg[6]
506
#define R7 GSU.avReg[7]
507
#define R8 GSU.avReg[8]
508
#define R9 GSU.avReg[9]
509
#define R10 GSU.avReg[10]
510
#define R11 GSU.avReg[11]
511
#define R12 GSU.avReg[12]
512
#define R13 GSU.avReg[13]
513
#define R14 GSU.avReg[14]
514
#define R15 GSU.avReg[15]
515
#define SFR GSU.vStatusReg
516
#define PBR GSU.vPrgBankReg
517
#define ROMBR GSU.vRomBankReg
518
#define RAMBR GSU.vRamBankReg
519
#define CBR GSU.vCacheBaseReg
520
#define SCBR USEX8(GSU.pvRegisters[GSU_SCBR])
521
#define SCMR USEX8(GSU.pvRegisters[GSU_SCMR])
522
#define COLR GSU.vColorReg
523
#define POR GSU.vPlotOptionReg
524
#define BRAMR USEX8(GSU.pvRegisters[GSU_BRAMR])
525
#define VCR USEX8(GSU.pvRegisters[GSU_VCR])
526
#define CFGR USEX8(GSU.pvRegisters[GSU_CFGR])
527
#define CLSR USEX8(GSU.pvRegisters[GSU_CLSR])
528
529
// Execute instruction from the pipe, and fetch next byte to the pipe
530
#define FX_STEP \
531
{ \
532
uint32 vOpcode = (uint32) PIPE; \
533
FETCHPIPE; \
534
(*fx_OpcodeTable[(GSU.vStatusReg & 0x300) | vOpcode])(); \
535
}
536
537
extern void (*fx_PlotTable[]) (void);
538
extern void (*fx_OpcodeTable[]) (void);
539
540
// Set this define if branches are relative to the instruction in the delay slot (I think they are)
541
#define BRANCH_DELAY_RELATIVE
542
543
#endif
544
545