bsx.cpp 35.6 KB
Newer Older
OV2's avatar
OV2 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/***********************************************************************************
  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.

  (c) Copyright 1996 - 2002  Gary Henderson (gary.henderson@ntlworld.com),
                             Jerremy Koot (jkoot@snes9x.com)

  (c) Copyright 2002 - 2004  Matthew Kendora

  (c) Copyright 2002 - 2005  Peter Bortas (peter@bortas.org)

  (c) Copyright 2004 - 2005  Joel Yliluoma (http://iki.fi/bisqwit/)

  (c) Copyright 2001 - 2006  John Weidman (jweidman@slip.net)

  (c) Copyright 2002 - 2006  funkyass (funkyass@spam.shaw.ca),
                             Kris Bleakley (codeviolation@hotmail.com)

  (c) Copyright 2002 - 2010  Brad Jorsch (anomie@users.sourceforge.net),
                             Nach (n-a-c-h@users.sourceforge.net),
OV2's avatar
OV2 committed
20
21

  (c) Copyright 2002 - 2011  zones (kasumitokoduck@yahoo.com)
OV2's avatar
OV2 committed
22
23
24

  (c) Copyright 2006 - 2007  nitsuja

kps501's avatar
kps501 committed
25
  (c) Copyright 2009 - 2018  BearOso,
OV2's avatar
OV2 committed
26
27
                             OV2

kps501's avatar
kps501 committed
28
29
30
  (c) Copyright 2017         qwertymodo

  (c) Copyright 2011 - 2017  Hans-Kristian Arntzen,
Brandon Wright's avatar
Brandon Wright committed
31
32
33
                             Daniel De Matteis
                             (Under no circumstances will commercial rights be given)

OV2's avatar
OV2 committed
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

  BS-X C emulator code
  (c) Copyright 2005 - 2006  Dreamer Nom,
                             zones

  C4 x86 assembler and some C emulation code
  (c) Copyright 2000 - 2003  _Demo_ (_demo_@zsnes.com),
                             Nach,
                             zsKnight (zsknight@zsnes.com)

  C4 C++ code
  (c) Copyright 2003 - 2006  Brad Jorsch,
                             Nach

  DSP-1 emulator code
  (c) Copyright 1998 - 2006  _Demo_,
                             Andreas Naive (andreasnaive@gmail.com),
                             Gary Henderson,
                             Ivar (ivar@snes9x.com),
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora,
                             Nach,
                             neviksti (neviksti@hotmail.com)

  DSP-2 emulator code
  (c) Copyright 2003         John Weidman,
                             Kris Bleakley,
                             Lord Nightmare (lord_nightmare@users.sourceforge.net),
                             Matthew Kendora,
                             neviksti

  DSP-3 emulator code
  (c) Copyright 2003 - 2006  John Weidman,
                             Kris Bleakley,
                             Lancer,
                             z80 gaiden

  DSP-4 emulator code
  (c) Copyright 2004 - 2006  Dreamer Nom,
                             John Weidman,
                             Kris Bleakley,
                             Nach,
                             z80 gaiden

  OBC1 emulator code
  (c) Copyright 2001 - 2004  zsKnight,
                             pagefault (pagefault@zsnes.com),
                             Kris Bleakley
                             Ported from x86 assembler to C by sanmaiwashi

  SPC7110 and RTC C++ emulator code used in 1.39-1.51
  (c) Copyright 2002         Matthew Kendora with research by
                             zsKnight,
                             John Weidman,
                             Dark Force

  SPC7110 and RTC C++ emulator code used in 1.52+
  (c) Copyright 2009         byuu,
                             neviksti

  S-DD1 C emulator code
  (c) Copyright 2003         Brad Jorsch with research by
                             Andreas Naive,
                             John Weidman

  S-RTC C emulator code
  (c) Copyright 2001 - 2006  byuu,
                             John Weidman

  ST010 C++ emulator code
  (c) Copyright 2003         Feather,
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora

  Super FX x86 assembler emulator code
  (c) Copyright 1998 - 2003  _Demo_,
                             pagefault,
                             zsKnight

  Super FX C emulator code
  (c) Copyright 1997 - 1999  Ivar,
                             Gary Henderson,
                             John Weidman

  Sound emulator code used in 1.5-1.51
  (c) Copyright 1998 - 2003  Brad Martin
  (c) Copyright 1998 - 2006  Charles Bilyue'

  Sound emulator code used in 1.52+
  (c) Copyright 2004 - 2007  Shay Green (gblargg@gmail.com)

kps501's avatar
kps501 committed
127
128
129
  S-SMP emulator code used in 1.54+
  (c) Copyright 2016         byuu

OV2's avatar
OV2 committed
130
131
132
133
134
135
136
137
138
139
140
141
142
  SH assembler code partly based on x86 assembler code
  (c) Copyright 2002 - 2004  Marcus Comstedt (marcus@mc.pp.se)

  2xSaI filter
  (c) Copyright 1999 - 2001  Derek Liauw Kie Fa

  HQ2x, HQ3x, HQ4x filters
  (c) Copyright 2003         Maxim Stepin (maxim@hiend3d.com)

  NTSC filter
  (c) Copyright 2006 - 2007  Shay Green

  GTK+ GUI code
kps501's avatar
kps501 committed
143
  (c) Copyright 2004 - 2018  BearOso
OV2's avatar
OV2 committed
144
145
146
147
148
149
150

  Win32 GUI code
  (c) Copyright 2003 - 2006  blip,
                             funkyass,
                             Matthew Kendora,
                             Nach,
                             nitsuja
kps501's avatar
kps501 committed
151
  (c) Copyright 2009 - 2018  OV2
OV2's avatar
OV2 committed
152
153
154

  Mac OS GUI code
  (c) Copyright 1998 - 2001  John Stiles
OV2's avatar
OV2 committed
155
  (c) Copyright 2001 - 2011  zones
OV2's avatar
OV2 committed
156

Brandon Wright's avatar
Brandon Wright committed
157
  Libretro port
kps501's avatar
kps501 committed
158
  (c) Copyright 2011 - 2017  Hans-Kristian Arntzen,
Brandon Wright's avatar
Brandon Wright committed
159
160
161
                             Daniel De Matteis
                             (Under no circumstances will commercial rights be given)

OV2's avatar
OV2 committed
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

  Specific ports contains the works of other authors. See headers in
  individual files.


  Snes9x homepage: http://www.snes9x.com/

  Permission to use, copy, modify and/or distribute Snes9x in both binary
  and source form, for non-commercial purposes, is hereby granted without
  fee, providing that this license information and copyright notice appear
  with all copies and any derived work.

  This software is provided 'as-is', without any express or implied
  warranty. In no event shall the authors be held liable for any damages
  arising from the use of this software or it's derivatives.

  Snes9x is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holders first. Commercial use includes,
  but is not limited to, charging money for Snes9x or software derived from
  Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
  using Snes9x as a promotion for your commercial product.

  The copyright holders request that bug fixes and improvements to the code
  should be forwarded to them so everyone can benefit from the modifications
  in future versions.

  Super NES and Super Nintendo Entertainment System are trademarks of
  Nintendo Co., Limited and its subsidiary companies.
 ***********************************************************************************/

// Dreamer Nom wrote:
// Large thanks to John Weidman for all his initial research
// Thanks to Seph3 for his modem notes


#include "snes9x.h"
#include "memmap.h"
#include "display.h"
kps501's avatar
kps501 committed
200
#include <math.h>
OV2's avatar
OV2 committed
201
202
203
204

//#define BSX_DEBUG

#define BIOS_SIZE	0x100000
kps501's avatar
kps501 committed
205
#define FLASH_SIZE	0x100000
OV2's avatar
OV2 committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#define PSRAM_SIZE	0x80000

#define Map			Memory.Map
#define BlockIsRAM	Memory.BlockIsRAM
#define BlockIsROM	Memory.BlockIsROM
#define RAM			Memory.RAM
#define SRAM		Memory.SRAM
#define PSRAM		Memory.BSRAM
#define BIOSROM		Memory.BIOSROM
#define MAP_BSX		Memory.MAP_BSX
#define MAP_CPU		Memory.MAP_CPU
#define MAP_PPU		Memory.MAP_PPU
#define MAP_NONE	Memory.MAP_NONE

#define BSXPPUBASE	0x2180

struct SBSX_RTC
{
kps501's avatar
kps501 committed
224
225
226
227
	int year;
	int month;
	int dayweek;
	int day;
OV2's avatar
OV2 committed
228
229
230
231
232
233
234
235
236
237
238
239
240
	int	hours;
	int	minutes;
	int	seconds;
	int	ticks;
};

static struct SBSX_RTC	BSX_RTC;

// flash card vendor information
static const uint8	flashcard[20] =
{
	0x4D, 0x00, 0x50, 0x00,	// vendor id
	0x00, 0x00,				// ?
kps501's avatar
kps501 committed
241
	0x1A, 0x00,				// 2MB Flash (1MB = 0x2A)
OV2's avatar
OV2 committed
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
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

static const uint8	init2192[32] =	// FIXME
{
	00, 00, 00, 00, 00,		// unknown
	01, 01, 00, 00, 00,
	00,						// seconds (?)
	00,						// minutes
	00,						// hours
	10, 10, 10, 10, 10,		// unknown
	10, 10, 10, 10, 10,		// dummy
	00, 00, 00, 00, 00, 00, 00, 00, 00
};

static bool8	FlashMode;
static uint32	FlashSize;
static uint8	*MapROM, *FlashROM;

static void BSX_Map_SNES (void);
static void BSX_Map_LoROM (void);
static void BSX_Map_HiROM (void);
static void BSX_Map_MMC (void);
static void BSX_Map_FlashIO (void);
static void BSX_Map_SRAM (void);
static void BSX_Map_PSRAM (void);
static void BSX_Map_BIOS (void);
static void BSX_Map_RAM (void);
static void BSX_Map (void);
static bool8 BSX_LoadBIOS (void);
static void map_psram_mirror_sub (uint32);
static int is_bsx (unsigned char *);


static void BSX_Map_SNES (void)
{
	// These maps will be partially overwritten

	int	c;

	// Banks 00->3F and 80->BF
	for (c = 0; c < 0x400; c += 16)
	{
		Map[c + 0] = Map[c + 0x800] = RAM;
		Map[c + 1] = Map[c + 0x801] = RAM;
		BlockIsRAM[c + 0] = BlockIsRAM[c + 0x800] = TRUE;
		BlockIsRAM[c + 1] = BlockIsRAM[c + 0x801] = TRUE;

		Map[c + 2] = Map[c + 0x802] = (uint8 *) MAP_PPU;
		Map[c + 3] = Map[c + 0x803] = (uint8 *) MAP_PPU;
		Map[c + 4] = Map[c + 0x804] = (uint8 *) MAP_CPU;
		Map[c + 5] = Map[c + 0x805] = (uint8 *) MAP_CPU;
		Map[c + 6] = Map[c + 0x806] = (uint8 *) MAP_NONE;
		Map[c + 7] = Map[c + 0x807] = (uint8 *) MAP_NONE;
	}
}

static void BSX_Map_LoROM (void)
{
	// These maps will be partially overwritten

	int	i, c;

	// Banks 00->3F and 80->BF
	for (c = 0; c < 0x400; c += 16)
	{
		for (i = c + 8; i < c + 16; i++)
		{
			Map[i] = Map[i + 0x800] = &MapROM[(c << 11) % FlashSize] - 0x8000;
			BlockIsRAM[i] = BlockIsRAM[i + 0x800] = BSX.write_enable;
			BlockIsROM[i] = BlockIsROM[i + 0x800] = !BSX.write_enable;
		}
	}

	// Banks 40->7F and C0->FF
	for (c = 0; c < 0x400; c += 16)
	{
		for (i = c; i < c + 8; i++)
			Map[i + 0x400] = Map[i + 0xC00] = &MapROM[(c << 11) % FlashSize];

		for (i = c + 8; i < c + 16; i++)
			Map[i + 0x400] = Map[i + 0xC00] = &MapROM[(c << 11) % FlashSize] - 0x8000;

		for (i = c; i < c + 16; i++)
		{
			BlockIsRAM[i + 0x400] = BlockIsRAM[i + 0xC00] = BSX.write_enable;
			BlockIsROM[i + 0x400] = BlockIsROM[i + 0xC00] = !BSX.write_enable;
		}
	}
}

static void BSX_Map_HiROM (void)
{
	// These maps will be partially overwritten

	int	i, c;

	// Banks 00->3F and 80->BF
	for (c = 0; c < 0x400; c += 16)
	{
		for (i = c + 8; i < c + 16; i++)
		{
			Map[i] = Map[i + 0x800] = &MapROM[(c << 12) % FlashSize];
			BlockIsRAM[i] = BlockIsRAM[i + 0x800] = BSX.write_enable;
			BlockIsROM[i] = BlockIsROM[i + 0x800] = !BSX.write_enable;
		}
	}

	// Banks 40->7F and C0->FF
	for (c = 0; c < 0x400; c += 16)
	{
		for (i = c; i < c + 16; i++)
		{
			Map[i + 0x400] = Map[i + 0xC00] = &MapROM[(c << 12) % FlashSize];
			BlockIsRAM[i + 0x400] = BlockIsRAM[i + 0xC00] = BSX.write_enable;
			BlockIsROM[i + 0x400] = BlockIsROM[i + 0xC00] = !BSX.write_enable;
		}
	}
}

static void BSX_Map_MMC (void)
{
	int	c;

	// Banks 01->0E:5000-5FFF
	for (c = 0x010; c < 0x0F0; c += 16)
	{
		Map[c + 5] = (uint8 *) MAP_BSX;
		BlockIsRAM[c + 5] = BlockIsROM[c + 5] = FALSE;
	}
}

static void BSX_Map_FlashIO (void)
{
kps501's avatar
kps501 committed
377
	int	i, c;
OV2's avatar
OV2 committed
378

kps501's avatar
kps501 committed
379
	if (BSX.prevMMC[0x0C])
OV2's avatar
OV2 committed
380
	{
kps501's avatar
kps501 committed
381
382
		// Banks 00->3F and 80->BF
		for (c = 0; c < 0x400; c += 16)
OV2's avatar
OV2 committed
383
		{
kps501's avatar
kps501 committed
384
385
386
387
388
389
			for (i = c + 8; i < c + 16; i++)
			{
				Map[i] = Map[i + 0x800] = (uint8 *)MAP_BSX;
				BlockIsRAM[i] = BlockIsRAM[i + 0x800] = TRUE;
				BlockIsROM[i] = BlockIsROM[i + 0x800] = FALSE;
			}
OV2's avatar
OV2 committed
390
		}
kps501's avatar
kps501 committed
391
392
393
394
395
396
397
398
399
400
401
402

		// Banks 40->7F and C0->FF
		for (c = 0; c < 0x400; c += 16)
		{
			for (i = c; i < c + 16; i++)
			{
				Map[i + 0x400] = Map[i + 0xC00] = (uint8 *)MAP_BSX;
				BlockIsRAM[i + 0x400] = BlockIsRAM[i + 0xC00] = TRUE;
				BlockIsROM[i + 0x400] = BlockIsROM[i + 0xC00] = FALSE;
			}
		}
	}	
OV2's avatar
OV2 committed
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
}

static void BSX_Map_SRAM (void)
{
	int	c;

	// Banks 10->17:5000-5FFF
	for (c = 0x100; c < 0x180; c += 16)
	{
		Map[c + 5] = (uint8 *) SRAM + ((c & 0x70) << 8) - 0x5000;
		BlockIsRAM[c + 5] = TRUE;
		BlockIsROM[c + 5] = FALSE;
	}
}

static void map_psram_mirror_sub (uint32 bank)
{
	int	i, c;

	bank <<= 4;

kps501's avatar
kps501 committed
424
	if (BSX.prevMMC[0x02])
OV2's avatar
OV2 committed
425
	{
kps501's avatar
kps501 committed
426
427
		//HiROM
		for (c = 0; c < 0x80; c += 16)
OV2's avatar
OV2 committed
428
		{
kps501's avatar
kps501 committed
429
			if ((bank & 0x7F0) >= 0x400)
OV2's avatar
OV2 committed
430
			{
kps501's avatar
kps501 committed
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
				for (i = c; i < c + 16; i++)
				{
					Map[i + bank] = &PSRAM[(c << 12) % PSRAM_SIZE];
					BlockIsRAM[i + bank] = TRUE;
					BlockIsROM[i + bank] = FALSE;
				}
			}
			else
			{
				for (i = c + 8; i < c + 16; i++)
				{
					Map[i + bank] = &PSRAM[(c << 12) % PSRAM_SIZE];
					BlockIsRAM[i + bank] = TRUE;
					BlockIsROM[i + bank] = FALSE;
				}
OV2's avatar
OV2 committed
446
447
448
449
450
			}
		}
	}
	else
	{
kps501's avatar
kps501 committed
451
		//LoROM
OV2's avatar
OV2 committed
452
453
		for (c = 0; c < 0x100; c += 16)
		{
kps501's avatar
kps501 committed
454
455
456
457
458
459
460
461
462
			if ((bank & 0x7F0) >= 0x400)
			{
				for (i = c; i < c + 8; i++)
				{
					Map[i + bank] = &PSRAM[(c << 11) % PSRAM_SIZE];
					BlockIsRAM[i + bank] = TRUE;
					BlockIsROM[i + bank] = FALSE;
				}
			}
OV2's avatar
OV2 committed
463
464
465

			for (i = c + 8; i < c + 16; i++)
			{
kps501's avatar
kps501 committed
466
				Map[i + bank] = &PSRAM[(c << 11) % PSRAM_SIZE] - 0x8000;
OV2's avatar
OV2 committed
467
468
469
470
471
472
473
				BlockIsRAM[i + bank] = TRUE;
				BlockIsROM[i + bank] = FALSE;
			}
		}
	}
}

kps501's avatar
kps501 committed
474
static void BSX_Map_PSRAM(void)
OV2's avatar
OV2 committed
475
476
477
{
	int	c;

kps501's avatar
kps501 committed
478
	if (!BSX.prevMMC[0x02])
OV2's avatar
OV2 committed
479
	{
kps501's avatar
kps501 committed
480
481
482
483
484
485
		//LoROM Mode
		if (!BSX.prevMMC[0x05] && !BSX.prevMMC[0x06])
		{
			//Map PSRAM to 00-0F/80-8F
			if (BSX.prevMMC[0x03])
				map_psram_mirror_sub(0x00);
OV2's avatar
OV2 committed
486

kps501's avatar
kps501 committed
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
			if (BSX.prevMMC[0x04])
				map_psram_mirror_sub(0x80);
		}
		else if (BSX.prevMMC[0x05] && !BSX.prevMMC[0x06])
		{
			//Map PSRAM to 20-2F/A0-AF
			if (BSX.prevMMC[0x03])
				map_psram_mirror_sub(0x20);

			if (BSX.prevMMC[0x04])
				map_psram_mirror_sub(0xA0);
		}
		else if (!BSX.prevMMC[0x05] && BSX.prevMMC[0x06])
		{
			//Map PSRAM to 40-4F/C0-CF
			if (BSX.prevMMC[0x03])
				map_psram_mirror_sub(0x40);

			if (BSX.prevMMC[0x04])
				map_psram_mirror_sub(0xC0);
		}
		else
		{
			//Map PSRAM to 60-6F/E0-EF
			if (BSX.prevMMC[0x03])
				map_psram_mirror_sub(0x60);

			if (BSX.prevMMC[0x04])
				map_psram_mirror_sub(0xE0);
		}

		//Map PSRAM to 70-7D/F0-FF
		if (BSX.prevMMC[0x03])
			map_psram_mirror_sub(0x70);

		if (BSX.prevMMC[0x04])
			map_psram_mirror_sub(0xF0);
OV2's avatar
OV2 committed
524
	}
kps501's avatar
kps501 committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
	else
	{
		//HiROM Mode
		if (!BSX.prevMMC[0x05] && !BSX.prevMMC[0x06])
		{
			//Map PSRAM to 00-07/40-47 / 80-87/C0-C7
			if (BSX.prevMMC[0x03])
			{
				map_psram_mirror_sub(0x00);
				map_psram_mirror_sub(0x40);
			}

			if (BSX.prevMMC[0x04])
			{
				map_psram_mirror_sub(0x80);
				map_psram_mirror_sub(0xC0);
			}
		}
		else if (BSX.prevMMC[0x05] && !BSX.prevMMC[0x06])
		{
			//Map PSRAM to 10-17/50-57 / 90-97-D0-D7
			if (BSX.prevMMC[0x03])
			{
				map_psram_mirror_sub(0x10);
				map_psram_mirror_sub(0x50);
			}

			if (BSX.prevMMC[0x04])
			{
				map_psram_mirror_sub(0x90);
				map_psram_mirror_sub(0xD0);
			}
		}
		else if (!BSX.prevMMC[0x05] && BSX.prevMMC[0x06])
		{
			//Map PSRAM to 20-27/60-67 / A0-A7/E0-E7
			if (BSX.prevMMC[0x03])
			{
				map_psram_mirror_sub(0x20);
				map_psram_mirror_sub(0x60);
			}
OV2's avatar
OV2 committed
566

kps501's avatar
kps501 committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
			if (BSX.prevMMC[0x04])
			{
				map_psram_mirror_sub(0xA0);
				map_psram_mirror_sub(0xE0);
			}
		}
		else
		{
			//Map PSRAM to 30-37/70-77 / B0-B7/F0-F7
			if (BSX.prevMMC[0x03])
			{
				map_psram_mirror_sub(0x30);
				map_psram_mirror_sub(0x70);
			}
OV2's avatar
OV2 committed
581

kps501's avatar
kps501 committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
			if (BSX.prevMMC[0x04])
			{
				map_psram_mirror_sub(0xB0);
				map_psram_mirror_sub(0xF0);
			}
		}

		if (BSX.prevMMC[0x03])
		{
			//Map PSRAM to 20->3F:6000-7FFF
			for (c = 0x200; c < 0x400; c += 16)
			{
				Map[c + 6] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
				Map[c + 7] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
				BlockIsRAM[c + 6] = TRUE;
				BlockIsRAM[c + 7] = TRUE;
				BlockIsROM[c + 6] = FALSE;
				BlockIsROM[c + 7] = FALSE;
			}
		}
OV2's avatar
OV2 committed
602

kps501's avatar
kps501 committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
		if (BSX.prevMMC[0x04])
		{
			//Map PSRAM to A0->BF:6000-7FFF
			for (c = 0xA00; c < 0xC00; c += 16)
			{
				Map[c + 6] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
				Map[c + 7] = &PSRAM[((c & 0x70) << 12) % PSRAM_SIZE];
				BlockIsRAM[c + 6] = TRUE;
				BlockIsRAM[c + 7] = TRUE;
				BlockIsROM[c + 6] = FALSE;
				BlockIsROM[c + 7] = FALSE;
			}
		}
	}
OV2's avatar
OV2 committed
617
618
619
620
621
622
623
}

static void BSX_Map_BIOS (void)
{
	int	i,c;

	// Banks 00->1F:8000-FFFF
kps501's avatar
kps501 committed
624
	if (BSX.prevMMC[0x07])
OV2's avatar
OV2 committed
625
626
627
628
629
630
631
632
633
634
635
636
637
	{
		for (c = 0; c < 0x200; c += 16)
		{
			for (i = c + 8; i < c + 16; i++)
			{
				Map[i] = &BIOSROM[(c << 11) % BIOS_SIZE] - 0x8000;
				BlockIsRAM[i] = FALSE;
				BlockIsROM[i] = TRUE;
			}
		}
	}

	// Banks 80->9F:8000-FFFF
kps501's avatar
kps501 committed
638
	if (BSX.prevMMC[0x08])
OV2's avatar
OV2 committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
	{
		for (c = 0; c < 0x200; c += 16)
		{
			for (i = c + 8; i < c + 16; i++)
			{
				Map[i + 0x800] = &BIOSROM[(c << 11) % BIOS_SIZE] - 0x8000;
				BlockIsRAM[i + 0x800] = FALSE;
				BlockIsROM[i + 0x800] = TRUE;
			}
		}
	}
}

static void BSX_Map_RAM (void)
{
	int	c;

	// Banks 7E->7F
	for (c = 0; c < 16; c++)
	{
		Map[c + 0x7E0] = RAM;
		Map[c + 0x7F0] = RAM + 0x10000;
		BlockIsRAM[c + 0x7E0] = TRUE;
		BlockIsRAM[c + 0x7F0] = TRUE;
		BlockIsROM[c + 0x7E0] = FALSE;
		BlockIsROM[c + 0x7F0] = FALSE;
	}
}

static void BSX_Map (void)
{
#ifdef BSX_DEBUG
	printf("BS: Remapping\n");
	for (int i = 0; i < 32; i++)
		printf("BS: MMC %02X: %d\n", i, BSX.MMC[i]);
#endif

	memcpy(BSX.prevMMC, BSX.MMC, sizeof(BSX.MMC));

kps501's avatar
kps501 committed
678
679
680
681
	MapROM = FlashROM;
	FlashSize = FLASH_SIZE;
	
	if (BSX.prevMMC[0x02])
OV2's avatar
OV2 committed
682
683
684
		BSX_Map_HiROM();
	else
		BSX_Map_LoROM();
kps501's avatar
kps501 committed
685
686
	
	BSX_Map_FlashIO();
OV2's avatar
OV2 committed
687
	BSX_Map_PSRAM();
kps501's avatar
kps501 committed
688
689

	BSX_Map_SNES();
OV2's avatar
OV2 committed
690
691
692
693
694
695
696
697
698
699
700
701
702
	BSX_Map_SRAM();
	BSX_Map_RAM();

	BSX_Map_BIOS();
	BSX_Map_MMC();

	// Monitor new register changes
	BSX.dirty  = FALSE;
	BSX.dirty2 = FALSE;

	Memory.map_WriteProtectROM();
}

kps501's avatar
kps501 committed
703
static uint8 BSX_Get_Bypass_FlashIO (uint32 offset)
OV2's avatar
OV2 committed
704
{
kps501's avatar
kps501 committed
705
706
707
708
709
	//For games other than BS-X
	FlashROM = Memory.ROM + Multi.cartOffsetB;

	if (BSX.prevMMC[0x02])
		return (FlashROM[offset & 0x0FFFFF]);
OV2's avatar
OV2 committed
710
	else
kps501's avatar
kps501 committed
711
		return (FlashROM[(offset & 0x1F0000) >> 1 | (offset & 0x7FFF)]);
OV2's avatar
OV2 committed
712
713
}

kps501's avatar
kps501 committed
714
static void BSX_Set_Bypass_FlashIO (uint32 offset, uint8 byte)
OV2's avatar
OV2 committed
715
{
kps501's avatar
kps501 committed
716
717
718
719
720
	//For games other than BS-X
	FlashROM = Memory.ROM + Multi.cartOffsetB;

	if (BSX.prevMMC[0x02])
		FlashROM[offset & 0x0FFFFF] = FlashROM[offset & 0x0FFFFF] & byte;
OV2's avatar
OV2 committed
721
	else
kps501's avatar
kps501 committed
722
		FlashROM[(offset & 0x1F0000) >> 1 | (offset & 0x7FFF)] = FlashROM[(offset & 0x1F0000) >> 1 | (offset & 0x7FFF)] & byte;
OV2's avatar
OV2 committed
723
724
725
726
727
728
729
730
731
}

uint8 S9xGetBSX (uint32 address)
{
	uint8	bank = (address >> 16) & 0xFF;
	uint16	offset = address & 0xFFFF;
	uint8	t = 0;

	// MMC
kps501's avatar
kps501 committed
732
	if ((bank >= 0x01 && bank <= 0x0E) && ((address & 0xF000) == 0x5000))
OV2's avatar
OV2 committed
733
734
		return (BSX.MMC[bank]);

kps501's avatar
kps501 committed
735
736
737
738
739
740
741
	// Flash Mapping

	// default: read-through mode
	t = BSX_Get_Bypass_FlashIO(address);

	// note: may be more registers, purposes unknown
	switch (offset)
OV2's avatar
OV2 committed
742
	{
kps501's avatar
kps501 committed
743
744
745
746
747
		case 0x0002:
		case 0x8002:
			if (BSX.flash_bsr)
				t = 0xC0; // Page Status Register
			break;
OV2's avatar
OV2 committed
748

kps501's avatar
kps501 committed
749
750
751
752
753
		case 0x0004:
		case 0x8004:
			if (BSX.flash_gsr)
				t = 0x82; // Global Status Register
			break;
OV2's avatar
OV2 committed
754

kps501's avatar
kps501 committed
755
756
757
758
		case 0x5555:
			if (BSX.flash_enable)
				t = 0x80; // ???
			break;
OV2's avatar
OV2 committed
759

kps501's avatar
kps501 committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
		case 0xFF00:
		case 0xFF02:
		case 0xFF04:
		case 0xFF06:
		case 0xFF08:
		case 0xFF0A:
		case 0xFF0C:
		case 0xFF0E:
		case 0xFF10:
		case 0xFF12:
			// return flash vendor information
			if (BSX.read_enable)
				t = flashcard[offset - 0xFF00];
			break;
	}

	if (BSX.flash_csr)
	{
		t = 0x80; // Compatible Status Register
		BSX.flash_csr = false;
OV2's avatar
OV2 committed
780
781
782
783
784
785
786
787
788
789
	}

	return (t);
}

void S9xSetBSX (uint8 byte, uint32 address)
{
	uint8	bank = (address >> 16) & 0xFF;

	// MMC
kps501's avatar
kps501 committed
790
	if ((bank >= 0x01 && bank <= 0x0E) && ((address & 0xF000) == 0x5000))
OV2's avatar
OV2 committed
791
	{
kps501's avatar
kps501 committed
792
793
		//Avoid updating the memory map when it is not needed
		if (bank == 0x0E && BSX.dirty)
OV2's avatar
OV2 committed
794
		{
kps501's avatar
kps501 committed
795
796
797
798
799
800
			BSX_Map();
			BSX.dirty = FALSE;
		}
		else if (bank != 0x0E && BSX.MMC[bank] != byte)
		{
			BSX.dirty = TRUE;
OV2's avatar
OV2 committed
801
		}
kps501's avatar
kps501 committed
802
803

		BSX.MMC[bank] = byte;
OV2's avatar
OV2 committed
804
805
806
	}

	// Flash IO
kps501's avatar
kps501 committed
807
808
809
	
	// Write to Flash
	if (BSX.write_enable)
OV2's avatar
OV2 committed
810
	{
kps501's avatar
kps501 committed
811
812
813
814
		BSX_Set_Bypass_FlashIO(address, byte);
		BSX.write_enable = false;
		return;
	}
OV2's avatar
OV2 committed
815

kps501's avatar
kps501 committed
816
817
818
819
820
	// Flash Command Handling
	
	//Memory Pack Type 1 & 3 & 4
	BSX.flash_command <<= 8;
	BSX.flash_command |= byte;
OV2's avatar
OV2 committed
821

kps501's avatar
kps501 committed
822
823
824
825
826
827
828
829
830
831
832
833
834
	switch (BSX.flash_command & 0xFF)
	{
		case 0x00:
		case 0xFF:
			//Reset to normal
			BSX.flash_enable = false;
			BSX.flash_bsr = false;
			BSX.flash_csr = false;
			BSX.flash_gsr = false;
			BSX.read_enable = false;
			BSX.write_enable = false;
			BSX.flash_cmd_done = true;
			break;
OV2's avatar
OV2 committed
835

kps501's avatar
kps501 committed
836
837
838
839
840
841
842
843
844
845
846
		case 0x10:
		case 0x40:
			//Write Byte
			BSX.flash_enable = false;
			BSX.flash_bsr = false;
			BSX.flash_csr = true;
			BSX.flash_gsr = false;
			BSX.read_enable = false;
			BSX.write_enable = true;
			BSX.flash_cmd_done = true;
			break;
OV2's avatar
OV2 committed
847

kps501's avatar
kps501 committed
848
849
850
851
852
853
854
855
		case 0x50:
			//Clear Status Register
			BSX.flash_enable = false;
			BSX.flash_bsr = false;
			BSX.flash_csr = false;
			BSX.flash_gsr = false;
			BSX.flash_cmd_done = true;
			break;
OV2's avatar
OV2 committed
856

kps501's avatar
kps501 committed
857
858
859
860
861
862
863
864
865
866
		case 0x70:
			//Read CSR
			BSX.flash_enable = false;
			BSX.flash_bsr = false;
			BSX.flash_csr = true;
			BSX.flash_gsr = false;
			BSX.read_enable = false;
			BSX.write_enable = false;
			BSX.flash_cmd_done = true;
			break;
OV2's avatar
OV2 committed
867

kps501's avatar
kps501 committed
868
869
870
871
872
873
874
875
876
877
		case 0x71:
			//Read Extended Status Registers (Page and Global)
			BSX.flash_enable = false;
			BSX.flash_bsr = true;
			BSX.flash_csr = false;
			BSX.flash_gsr = true;
			BSX.read_enable = false;
			BSX.write_enable = false;
			BSX.flash_cmd_done = true;
			break;
OV2's avatar
OV2 committed
878

kps501's avatar
kps501 committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
		case 0x75:
			//Show Page Buffer / Vendor Info
			BSX.flash_csr = false;
			BSX.read_enable = true;
			BSX.flash_cmd_done = true;
			break;

		case 0xD0:
			//DO COMMAND
			switch (BSX.flash_command & 0xFFFF)
			{
				case 0x20D0: //Block Erase
					uint32 x;
					for (x = 0; x < 0x10000; x++) {
						//BSX_Set_Bypass_FlashIO(((address & 0xFF0000) + x), 0xFF);
						if (BSX.MMC[0x02])
							FlashROM[(address & 0x0F0000) + x] = 0xFF;
						else
							FlashROM[((address & 0x1E0000) >> 1) + x] = 0xFF;
					}
					break;

				case 0xA7D0: //Chip Erase (ONLY IN TYPE 1 AND 4)
					if ((flashcard[6] & 0xF0) == 0x10 || (flashcard[6] & 0xF0) == 0x40)
					{
						uint32 x;
						for (x = 0; x < FLASH_SIZE; x++) {
							//BSX_Set_Bypass_FlashIO(x, 0xFF);
							FlashROM[x] = 0xFF;
						}
					}
					break;

				case 0x38D0: //Flashcart Reset
					break;
			}
			break;
	}
}

void S9xBSXSetStream1 (uint8 count)
{
	if (BSX.sat_stream1.is_open())
		BSX.sat_stream1.close(); //If Stream already opened for one file: Close it.

	char path[PATH_MAX + 1], name[PATH_MAX + 1];

	strcpy(path, S9xGetDirectory(SAT_DIR));
	strcat(path, SLASH_STR);

	snprintf(name, PATH_MAX + 1, "BSX%04X-%d.bin", (BSX.PPU[0x2188 - BSXPPUBASE] | (BSX.PPU[0x2189 - BSXPPUBASE] * 256)), count); //BSXHHHH-DDD.bin
	strcat(path, name);

	BSX.sat_stream1.clear();
	BSX.sat_stream1.open(path, std::ios::in | std::ios::binary);
	if (BSX.sat_stream1.good())
	{
		BSX.sat_stream1.seekg(0, BSX.sat_stream1.end);
		long str1size = BSX.sat_stream1.tellg();
		BSX.sat_stream1.seekg(0, BSX.sat_stream1.beg);
		float QueueSize = str1size / 22.;
		BSX.sat_stream1_queue = (uint16)(ceil(QueueSize));
		BSX.PPU[0x218D - BSXPPUBASE] = 0;
		BSX.sat_stream1_first = TRUE;
		BSX.sat_stream1_loaded = TRUE;
	}
	else
	{
		BSX.sat_stream1_loaded = FALSE;
	}
}

void S9xBSXSetStream2 (uint8 count)
{
	if (BSX.sat_stream2.is_open())
		BSX.sat_stream2.close(); //If Stream already opened for one file: Close it.

	char path[PATH_MAX + 1], name[PATH_MAX + 1];

	strcpy(path, S9xGetDirectory(SAT_DIR));
	strcat(path, SLASH_STR);

	snprintf(name, PATH_MAX + 1, "BSX%04X-%d.bin", (BSX.PPU[0x218E - BSXPPUBASE] | (BSX.PPU[0x218F - BSXPPUBASE] * 256)), count); //BSXHHHH-DDD.bin
	strcat(path, name);

	BSX.sat_stream2.clear();
	BSX.sat_stream2.open(path, std::ios::in | std::ios::binary);
	if (BSX.sat_stream2.good())
	{
		BSX.sat_stream2.seekg(0, BSX.sat_stream2.end);
		long str2size = BSX.sat_stream2.tellg();
		BSX.sat_stream2.seekg(0, BSX.sat_stream2.beg);
		float QueueSize = str2size / 22.;
		BSX.sat_stream2_queue = (uint16)(ceil(QueueSize));
		BSX.PPU[0x2193 - BSXPPUBASE] = 0;
		BSX.sat_stream2_first = TRUE;
		BSX.sat_stream2_loaded = TRUE;
	}
	else
	{
		BSX.sat_stream2_loaded = FALSE;
OV2's avatar
OV2 committed
980
981
982
	}
}

kps501's avatar
kps501 committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
uint8 S9xBSXGetRTC (void)
{
	//Get Time
	time_t		t;
	struct tm	*tmr;

	time(&t);
	tmr = localtime(&t);

	BSX.test2192[0] = 0x00;
	BSX.test2192[1] = 0x00;
	BSX.test2192[2] = 0x00;
	BSX.test2192[3] = 0x00;
	BSX.test2192[4] = 0x10;
	BSX.test2192[5] = 0x01;
	BSX.test2192[6] = 0x01;
	BSX.test2192[7] = 0x00;
	BSX.test2192[8] = 0x00;
	BSX.test2192[9] = 0x00;
	BSX.test2192[10] = BSX_RTC.seconds = tmr->tm_sec;
	BSX.test2192[11] = BSX_RTC.minutes = tmr->tm_min;
	BSX.test2192[12] = BSX_RTC.hours = tmr->tm_hour;
	BSX.test2192[13] = BSX_RTC.dayweek = (tmr->tm_wday) + 1;
	BSX.test2192[14] = BSX_RTC.day = tmr->tm_mday;
	BSX.test2192[15] = BSX_RTC.month = (tmr->tm_mon) + 1;
	BSX_RTC.year = tmr->tm_year + 1900;
	BSX.test2192[16] = (BSX_RTC.year) & 0xFF;
	BSX.test2192[17] = (BSX_RTC.year) >> 8;

	t = BSX.test2192[BSX.out_index++];

	if (BSX.out_index > 22)
		BSX.out_index = 0;

	return t;
}

OV2's avatar
OV2 committed
1020
1021
1022
1023
1024
1025
1026
uint8 S9xGetBSXPPU (uint16 address)
{
	uint8	t;

	// known read registers
	switch (address)
	{
kps501's avatar
kps501 committed
1027
1028
		//Stream 1
		// Logical Channel 1 + Data Structure (R/W)
OV2's avatar
OV2 committed
1029
1030
1031
1032
		case 0x2188:
			t = BSX.PPU[0x2188 - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1033
		// Logical Channel 2 (R/W) [6bit]
OV2's avatar
OV2 committed
1034
1035
1036
1037
		case 0x2189:
			t = BSX.PPU[0x2189 - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1038
		// Prefix Count (R)
OV2's avatar
OV2 committed
1039
		case 0x218A:
kps501's avatar
kps501 committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
			if (!BSX.sat_pf_latch1_enable || !BSX.sat_dt_latch1_enable)
			{
				t = 0;
				break;
			}

			if (BSX.PPU[0x2188 - BSXPPUBASE] == 0 && BSX.PPU[0x2189 - BSXPPUBASE] == 0)
			{
				t = 1;
				break;
			}

			if (BSX.sat_stream1_queue <= 0)
			{
				BSX.sat_stream1_count++;
				S9xBSXSetStream1(BSX.sat_stream1_count - 1);
			}

			if (!BSX.sat_stream1_loaded && (BSX.sat_stream1_count - 1) > 0)
			{
				BSX.sat_stream1_count = 1;
				S9xBSXSetStream1(BSX.sat_stream1_count - 1);
			}

			if (BSX.sat_stream1_loaded)
			{
				//Lock at 0x7F for bigger packets
				if (BSX.sat_stream1_queue >= 128)
					BSX.PPU[0x218A - BSXPPUBASE] = 0x7F;
				else
					BSX.PPU[0x218A - BSXPPUBASE] = BSX.sat_stream1_queue;
				t = BSX.PPU[0x218A - BSXPPUBASE];
			}
			else
				t = 0;
OV2's avatar
OV2 committed
1075
1076
			break;

kps501's avatar
kps501 committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
		// Prefix Latch (R/W)
		case 0x218B:
			if (BSX.sat_pf_latch1_enable)
			{
				if (BSX.PPU[0x2188 - BSXPPUBASE] == 0 && BSX.PPU[0x2189 - BSXPPUBASE] == 0)
				{
					BSX.PPU[0x218B - BSXPPUBASE] = 0x90;
				}

				if (BSX.sat_stream1_loaded)
				{
					uint8 temp = 0;
					if (BSX.sat_stream1_first)
					{
						// First packet
						temp |= 0x10;
						BSX.sat_stream1_first = FALSE;
					}

					BSX.sat_stream1_queue--;

					if (BSX.sat_stream1_queue == 0)
					{
						//Last packet
						temp |= 0x80;
					}

					BSX.PPU[0x218B - BSXPPUBASE] = temp;
				}

				BSX.PPU[0x218D - BSXPPUBASE] |= BSX.PPU[0x218B - BSXPPUBASE];
				t = BSX.PPU[0x218B - BSXPPUBASE];
			}
			else
			{
				t = 0;
			}
			break;

		// Data Latch (R/W)
OV2's avatar
OV2 committed
1117
		case 0x218C:
kps501's avatar
kps501 committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
			if (BSX.sat_dt_latch1_enable)
			{
				if (BSX.PPU[0x2188 - BSXPPUBASE] == 0 && BSX.PPU[0x2189 - BSXPPUBASE] == 0)
				{
					BSX.PPU[0x218C - BSXPPUBASE] = S9xBSXGetRTC();
				}
				else if (BSX.sat_stream1_loaded)
				{
					if (BSX.sat_stream1.eof())
						BSX.PPU[0x218C - BSXPPUBASE] = 0xFF;
					else
						BSX.PPU[0x218C - BSXPPUBASE] = BSX.sat_stream1.get();
				}
				t = BSX.PPU[0x218C - BSXPPUBASE];
			}
			else
			{
				t = 0;
			}
			break;

		// OR gate (R)
		case 0x218D:
			t = BSX.PPU[0x218D - BSXPPUBASE];
			BSX.PPU[0x218D - BSXPPUBASE] = 0;
OV2's avatar
OV2 committed
1143
1144
			break;

kps501's avatar
kps501 committed
1145
1146
		//Stream 2
		// Logical Channel 1 + Data Structure (R/W)
OV2's avatar
OV2 committed
1147
1148
1149
1150
		case 0x218E:
			t = BSX.PPU[0x218E - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1151
		// Logical Channel 2 (R/W) [6bit]
OV2's avatar
OV2 committed
1152
1153
1154
1155
		case 0x218F:
			t = BSX.PPU[0x218F - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1156
		// Prefix Count (R)
OV2's avatar
OV2 committed
1157
		case 0x2190:
kps501's avatar
kps501 committed
1158
1159
1160
1161
1162
			if (!BSX.sat_pf_latch2_enable || !BSX.sat_dt_latch2_enable)
			{
				t = 0;
				break;
			}
OV2's avatar
OV2 committed
1163

kps501's avatar
kps501 committed
1164
1165
1166
1167
1168
			if (BSX.PPU[0x218E - BSXPPUBASE] == 0 && BSX.PPU[0x218F - BSXPPUBASE] == 0)
			{
				t = 1;
				break;
			}
OV2's avatar
OV2 committed
1169

kps501's avatar
kps501 committed
1170
			if (BSX.sat_stream2_queue <= 0)
OV2's avatar
OV2 committed
1171
			{
kps501's avatar
kps501 committed
1172
1173
				BSX.sat_stream2_count++;
				S9xBSXSetStream2(BSX.sat_stream2_count - 1);
OV2's avatar
OV2 committed
1174
			}
kps501's avatar
kps501 committed
1175
1176

			if (!BSX.sat_stream2_loaded && (BSX.sat_stream2_count - 1) > 0)
OV2's avatar
OV2 committed
1177
			{
kps501's avatar
kps501 committed
1178
1179
				BSX.sat_stream2_count = 1;
				S9xBSXSetStream2(BSX.sat_stream2_count - 1);
OV2's avatar
OV2 committed
1180
			}
kps501's avatar
kps501 committed
1181
1182

			if (BSX.sat_stream2_loaded)
OV2's avatar
OV2 committed
1183
			{
kps501's avatar
kps501 committed
1184
1185
1186
1187
1188
				if (BSX.sat_stream2_queue >= 128)
					BSX.PPU[0x2190 - BSXPPUBASE] = 0x7F;
				else
					BSX.PPU[0x2190 - BSXPPUBASE] = BSX.sat_stream2_queue;
				t = BSX.PPU[0x2190 - BSXPPUBASE];
OV2's avatar
OV2 committed
1189
			}
kps501's avatar
kps501 committed
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
			else
				t = 0;
			break;

		// Prefix Latch (R/W)
		case 0x2191:
			if (BSX.sat_pf_latch2_enable)
			{
				if (BSX.PPU[0x218E - BSXPPUBASE] == 0 && BSX.PPU[0x218F - BSXPPUBASE] == 0)
				{
					BSX.PPU[0x2191 - BSXPPUBASE] = 0x90;
				}
OV2's avatar
OV2 committed
1202

kps501's avatar
kps501 committed
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
				if (BSX.sat_stream2_loaded)
				{
					uint8 temp = 0;
					if (BSX.sat_stream2_first)
					{
						// First packet
						temp |= 0x10;
						BSX.sat_stream2_first = FALSE;
					}

					BSX.sat_stream2_queue--;

					if (BSX.sat_stream2_queue == 0)
					{
						//Last packet
						temp |= 0x80;
					}

					BSX.PPU[0x2191 - BSXPPUBASE] = temp;
				}
OV2's avatar
OV2 committed
1223

kps501's avatar
kps501 committed
1224
1225
1226
1227
1228
1229
1230
				BSX.PPU[0x2193 - BSXPPUBASE] |= BSX.PPU[0x2191 - BSXPPUBASE];
				t = BSX.PPU[0x2191 - BSXPPUBASE];
			}
			else
			{
				t = 0;
			}
OV2's avatar
OV2 committed
1231
1232
			break;

kps501's avatar
kps501 committed
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
		// Data Latch (R/W)
		case 0x2192:
			if (BSX.sat_dt_latch2_enable)
			{
				if (BSX.PPU[0x218E - BSXPPUBASE] == 0 && BSX.PPU[0x218F - BSXPPUBASE] == 0)
				{
					BSX.PPU[0x2192 - BSXPPUBASE] = S9xBSXGetRTC();
				}
				else if (BSX.sat_stream2_loaded)
				{
					if (BSX.sat_stream2.eof())
						BSX.PPU[0x2192 - BSXPPUBASE] = 0xFF;
					else
						BSX.PPU[0x2192 - BSXPPUBASE] = BSX.sat_stream2.get();
				}
				t = BSX.PPU[0x2192 - BSXPPUBASE];
			}
			else
			{
				t = 0;
			}
			break;

		// OR gate (R)
OV2's avatar
OV2 committed
1257
		case 0x2193:
kps501's avatar
kps501 committed
1258
1259
			t = BSX.PPU[0x2193 - BSXPPUBASE];
			BSX.PPU[0x2193 - BSXPPUBASE] = 0;
OV2's avatar
OV2 committed
1260
1261
			break;

kps501's avatar
kps501 committed
1262
1263
		//Other
		// Satellaview LED / Stream Enable (R/W) [4bit]
OV2's avatar
OV2 committed
1264
1265
1266
1267
		case 0x2194:
			t = BSX.PPU[0x2194 - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1268
1269
1270
1271
1272
1273
		// Unknown
		case 0x2195:
			t = BSX.PPU[0x2195 - BSXPPUBASE];
			break;

		// Satellaview Status (R)
OV2's avatar
OV2 committed
1274
1275
1276
1277
		case 0x2196:
			t = BSX.PPU[0x2196 - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1278
		// Soundlink Settings (R/W)
OV2's avatar
OV2 committed
1279
1280
1281
1282
		case 0x2197:
			t = BSX.PPU[0x2197 - BSXPPUBASE];
			break;

kps501's avatar
kps501 committed
1283
1284
1285
1286
1287
1288
		// Serial I/O - Serial Number (R/W)
		case 0x2198:
			t = BSX.PPU[0x2198 - BSXPPUBASE];
			break;

		// Serial I/O - Unknown (R/W)
OV2's avatar
OV2 committed
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
		case 0x2199:
			t = BSX.PPU[0x2199 - BSXPPUBASE];
			break;

		default:
			t = OpenBus;
			break;
	}

	return (t);
}

void S9xSetBSXPPU (uint8 byte, uint16 address)
{
	// known write registers
	switch (address)
	{
kps501's avatar
kps501 committed
1306
1307
		//Stream 1
		// Logical Channel 1 + Data Structure (R/W)
OV2's avatar
OV2 committed
1308
		case 0x2188:
kps501's avatar
kps501 committed
1309
1310
1311
1312
			if (BSX.PPU[0x2188 - BSXPPUBASE] == byte)
			{
				BSX.sat_stream1_count = 0;
			}
OV2's avatar
OV2 committed
1313
1314
1315
			BSX.PPU[0x2188 - BSXPPUBASE] = byte;
			break;

kps501's avatar
kps501 committed
1316
		// Logical Channel 2 (R/W) [6bit]
OV2's avatar
OV2 committed
1317
		case 0x2189:
kps501's avatar
kps501 committed
1318
1319
1320
1321
1322
			if (BSX.PPU[0x2188 - BSXPPUBASE] == (byte & 0x3F))
			{
				BSX.sat_stream1_count = 0;
			}
			BSX.PPU[0x2189 - BSXPPUBASE] = byte & 0x3F;
OV2's avatar
OV2 committed
1323
1324
			break;

kps501's avatar
kps501 committed
1325
		// Prefix Latch (R/W)
OV2's avatar
OV2 committed
1326
		case 0x218B:
kps501's avatar
kps501 committed
1327
			BSX.sat_pf_latch1_enable = (byte != 0);
OV2's avatar
OV2 committed
1328
1329
			break;

kps501's avatar
kps501 committed
1330
		// Data Latch (R/W)
OV2's avatar
OV2 committed
1331
		case 0x218C:
kps501's avatar
kps501 committed
1332
1333
1334
1335
1336
			if (BSX.PPU[0x2188 - BSXPPUBASE] == 0 && BSX.PPU[0x2189 - BSXPPUBASE] == 0)
			{
				BSX.out_index = 0;
			}
			BSX.sat_dt_latch1_enable = (byte != 0);
OV2's avatar
OV2 committed
1337
1338
			break;

kps501's avatar
kps501 committed
1339
1340
		//Stream 2
		// Logical Channel 1 + Data Structure (R/W)
OV2's avatar
OV2 committed
1341
		case 0x218E:
kps501's avatar
kps501 committed
1342
1343
1344
1345
			if (BSX.PPU[0x218E - BSXPPUBASE] == byte)
			{
				BSX.sat_stream2_count = 0;
			}
OV2's avatar
OV2 committed
1346
1347
1348
			BSX.PPU[0x218E - BSXPPUBASE] = byte;
			break;

kps501's avatar
kps501 committed
1349
		// Logical Channel 2 (R/W) [6bit]
OV2's avatar
OV2 committed
1350
		case 0x218F:
kps501's avatar
kps501 committed
1351
1352
1353
1354
1355
			if (BSX.PPU[0x218F - BSXPPUBASE] == (byte & 0x3F))
			{
				BSX.sat_stream2_count = 0;
			}
			BSX.PPU[0x218F - BSXPPUBASE] = byte & 0x3F;
OV2's avatar
OV2 committed
1356
1357
			break;

kps501's avatar
kps501 committed
1358
		// Prefix Latch (R/W)
OV2's avatar
OV2 committed
1359
		case 0x2191:
kps501's avatar
kps501 committed
1360
			BSX.sat_pf_latch2_enable = (byte != 0);
OV2's avatar
OV2 committed
1361
1362
			break;

kps501's avatar
kps501 committed
1363
		// Data Latch (R/W)
OV2's avatar
OV2 committed
1364
		case 0x2192:
kps501's avatar
kps501 committed
1365
1366
1367
1368
1369
			if (BSX.PPU[0x218E - BSXPPUBASE] == 0 && BSX.PPU[0x218F - BSXPPUBASE] == 0)
			{
				BSX.out_index = 0;
			}
			BSX.sat_dt_latch2_enable = (byte != 0);
OV2's avatar
OV2 committed
1370
1371
			break;

kps501's avatar
kps501 committed
1372
1373
		//Other
		// Satellaview LED / Stream Enable (R/W) [4bit]
OV2's avatar
OV2 committed
1374
		case 0x2194:
kps501's avatar
kps501 committed
1375
			BSX.PPU[0x2194 - BSXPPUBASE] = byte & 0x0F;
OV2's avatar
OV2 committed
1376
1377
			break;

kps501's avatar
kps501 committed
1378
		// Soundlink Settings (R/W)
OV2's avatar
OV2 committed
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
		case 0x2197:
			BSX.PPU[0x2197 - BSXPPUBASE] = byte;
			break;
	}
}

uint8 * S9xGetBasePointerBSX (uint32 address)
{
	return (MapROM);
}

static bool8 BSX_LoadBIOS (void)
{
	FILE	*fp;
	char	path[PATH_MAX + 1], name[PATH_MAX + 1];
	bool8	r = FALSE;

	strcpy(path, S9xGetDirectory(BIOS_DIR));
	strcat(path, SLASH_STR);
	strcpy(name, path);
	strcat(name, "BS-X.bin");

	fp = fopen(name, "rb");
	if (!fp)
	{
		strcpy(name, path);
		strcat(name, "BS-X.bios");
		fp = fopen(name, "rb");
	}

	if (fp)
	{
		size_t	size;

		size = fread((void *) BIOSROM, 1, BIOS_SIZE, fp);
		fclose(fp);
		if (size == BIOS_SIZE)
			r = TRUE;
	}

#ifdef BSX_DEBUG
	if (r)
		printf("BS: BIOS found.\n");
	else
		printf("BS: BIOS not found!\n");
#endif

	return (r);
}

OV2's avatar
OV2 committed
1429
1430
1431
1432
1433
1434
1435
1436
static bool8 is_BSX_BIOS (const uint8 *data, uint32 size)
{
	if (size == BIOS_SIZE && strncmp((char *) (data + 0x7FC0), "Satellaview BS-X     ", 21) == 0)
		return (TRUE);
	else
		return (FALSE);
}

OV2's avatar
OV2 committed
1437
1438
1439
1440
void S9xInitBSX (void)
{
	Settings.BS = FALSE;

kps501's avatar
kps501 committed
1441
	if (is_BSX_BIOS(Memory.ROM,Memory.CalculatedSize))
OV2's avatar
OV2 committed
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
	{
		// BS-X itself

		Settings.BS = TRUE;
		Settings.BSXItself = TRUE;

		Memory.LoROM = TRUE;
		Memory.HiROM = FALSE;

		memmove(BIOSROM, Memory.ROM, BIOS_SIZE);

		FlashMode = FALSE;
		FlashSize = FLASH_SIZE;

		BSX.bootup = TRUE;
	}
	else
	{
		Settings.BSXItself = FALSE;

		int	r1, r2;

		r1 = (is_bsx(Memory.ROM + 0x7FC0) == 1);
		r2 = (is_bsx(Memory.ROM + 0xFFC0) == 1);
		Settings.BS = (r1 | r2) ? TRUE : FALSE;

		if (Settings.BS)
		{
			// BS games

			Memory.LoROM = r1 ? TRUE : FALSE;
			Memory.HiROM = r2 ? TRUE : FALSE;

			uint8	*header = r1 ? Memory.ROM + 0x7FC0 : Memory.ROM + 0xFFC0;

			FlashMode = (header[0x18] & 0xEF) == 0x20 ? FALSE : TRUE;
kps501's avatar
kps501 committed
1478
1479
1480
1481
1482
1483
1484
			FlashSize = FLASH_SIZE;

			// Fix Block Allocation Flags
			// (for games that don't have it setup properly,
			// for exemple when taken seperately from the upper memory of the Memory Pack,
			// else the game will error out on BS-X)
			for (; (((header[0x10] & 1) == 0) && header[0x10] != 0); (header[0x10] >>= 1));
OV2's avatar
OV2 committed
1485
1486
1487
1488
1489
1490
1491
1492
1493

#ifdef BSX_DEBUG
			for (int i = 0; i <= 0x1F; i++)
				printf("BS: ROM Header %02X: %02X\n", i, header[i]);
			printf("BS: FlashMode: %d, FlashSize: %x\n", FlashMode, FlashSize);
#endif

			BSX.bootup = Settings.BSXBootup;

OV2's avatar
OV2 committed
1494
			if (!BSX_LoadBIOS() && !is_BSX_BIOS(BIOSROM,BIOS_SIZE))
OV2's avatar
OV2 committed
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
			{
				BSX.bootup = FALSE;
				memset(BIOSROM, 0, BIOS_SIZE);
			}
		}
	}

	if (Settings.BS)
	{
		MapROM = NULL;
		FlashROM = Memory.ROM;
kps501's avatar
kps501 committed
1506
		/*
OV2's avatar
OV2 committed
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
		time_t		t;
		struct tm	*tmr;

		time(&t);
		tmr = localtime(&t);

		BSX_RTC.ticks = 0;
		memcpy(BSX.test2192, init2192, sizeof(init2192));
		BSX.test2192[10] = BSX_RTC.seconds = tmr->tm_sec;
		BSX.test2192[11] = BSX_RTC.minutes = tmr->tm_min;
		BSX.test2192[12] = BSX_RTC.hours   = tmr->tm_hour;
#ifdef BSX_DEBUG
		printf("BS: Current Time: %02d:%02d:%02d\n",  BSX_RTC.hours, BSX_RTC.minutes, BSX_RTC.seconds);
#endif
kps501's avatar
kps501 committed
1521
		*/
OV2's avatar
OV2 committed
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
		SNESGameFixes.SRAMInitialValue = 0x00;
	}
}

void S9xResetBSX (void)
{
	if (Settings.BSXItself)
		memset(Memory.ROM, 0, FLASH_SIZE);

	memset(BSX.PPU, 0, sizeof(BSX.PPU));
	memset(BSX.MMC, 0, sizeof(BSX.MMC));
	memset(BSX.prevMMC, 0, sizeof(BSX.prevMMC));

	BSX.dirty         = FALSE;
	BSX.dirty2        = FALSE;
	BSX.flash_enable  = FALSE;
	BSX.write_enable  = FALSE;
	BSX.read_enable   = FALSE;
	BSX.flash_command = 0;
	BSX.old_write     = 0;
	BSX.new_write     = 0;

	BSX.out_index = 0;
	memset(BSX.output, 0, sizeof(BSX.output));

	// starting from the bios
kps501's avatar
kps501 committed
1548
1549
	BSX.MMC[0x02] = BSX.MMC[0x03] = BSX.MMC[0x05] = BSX.MMC[0x06] = 0x80;
	BSX.MMC[0x09] = BSX.MMC[0x0B] = 0x80;
OV2's avatar
OV2 committed
1550

kps501's avatar
kps501 committed
1551
1552
	BSX.MMC[0x07] = BSX.MMC[0x08] = 0x80;
	BSX.MMC[0x0E] = 0x80;
OV2's avatar
OV2 committed
1553

kps501's avatar
kps501 committed
1554
1555
1556
	// default register values
	BSX.PPU[0x2196 - BSXPPUBASE] = 0x10;
	BSX.PPU[0x2197 - BSXPPUBASE] = 0x80;
OV2's avatar
OV2 committed
1557

kps501's avatar
kps501 committed
1558
1559
1560
	// stream reset
	BSX.sat_pf_latch1_enable = BSX.sat_dt_latch1_enable = FALSE;
	BSX.sat_pf_latch2_enable = BSX.sat_dt_latch2_enable = FALSE;
OV2's avatar
OV2 committed
1561

kps501's avatar
kps501 committed
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
	BSX.sat_stream1_loaded = BSX.sat_stream2_loaded = FALSE;
	BSX.sat_stream1_first = BSX.sat_stream2_first = FALSE;
	BSX.sat_stream1_count = BSX.sat_stream2_count = 0;

	if (BSX.sat_stream1.is_open())
		BSX.sat_stream1.close();

	if (BSX.sat_stream2.is_open())
		BSX.sat_stream2.close();

	if (Settings.BS)
		BSX_Map();
OV2's avatar
OV2 committed
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
}

void S9xBSXPostLoadState (void)
{
	uint8	temp[16];
	bool8	pd1, pd2;

	pd1 = BSX.dirty;
	pd2 = BSX.dirty2;
	memcpy(temp, BSX.MMC, sizeof(BSX.MMC));

	memcpy(BSX.MMC, BSX.prevMMC, sizeof(BSX.MMC));
	BSX_Map();

	memcpy(BSX.MMC, temp, sizeof(BSX.MMC));
	BSX.dirty  = pd1;
	BSX.dirty2 = pd2;
}

static bool valid_normal_bank (unsigned char bankbyte)
{
	switch (bankbyte)
	{
		case 32: case 33: case 48: case 49:
			return (true);
			break;
	}

	return (false);
}

static int is_bsx (unsigned char *p)
{
	if ((p[26] == 0x33 || p[26] == 0xFF) && (!p[21] || (p[21] & 131) == 128) && valid_normal_bank(p[24]))
	{
		unsigned char	m = p[22];

		if (!m && !p[23])
			return (2);

		if ((m == 0xFF && p[23] == 0xFF) || (!(m & 0xF) && ((m >> 4) - 1 < 12)))
			return (1);
	}

	return (0);
}