random bug fixes
[drnoksnes] / sdd1.cpp
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 #include "snes9x.h"
42 #include "memmap.h"
43 #include "ppu.h"
44 #include "sdd1.h"
45 #include "display.h"
46
47 #ifdef __linux
48 //#include <unistd.h>
49 #endif
50
51 void S9xSetSDD1MemoryMap (uint32 bank, uint32 value)
52 {
53     bank = 0xc00 + bank * 0x100;
54     value = value * 1024 * 1024;
55
56     int c;
57
58     for (c = 0; c < 0x100; c += 16)
59     {
60         uint8 *block = &Memory.ROM [value + (c << 12)];
61         int i;
62
63         for (i = c; i < c + 16; i++)
64             Memory.Map [i + bank] = block;
65     }
66 }
67
68 void S9xResetSDD1 ()
69 {
70     memset (&Memory.FillRAM [0x4800], 0, 4);
71     for (int i = 0; i < 4; i++)
72     {
73         Memory.FillRAM [0x4804 + i] = i;
74         S9xSetSDD1MemoryMap (i, i);
75     }
76 }
77
78 void S9xSDD1PostLoadState ()
79 {
80     for (int i = 0; i < 4; i++)
81         S9xSetSDD1MemoryMap (i, Memory.FillRAM [0x4804 + i]);
82 }
83
84 #ifndef _SNESPPC
85 static int S9xCompareSDD1LoggedDataEntries (const void *p1, const void *p2)
86 #else
87 static int _cdecl S9xCompareSDD1LoggedDataEntries (const void *p1, const void *p2)
88 #endif
89 {
90     uint8 *b1 = (uint8 *) p1;
91     uint8 *b2 = (uint8 *) p2;
92     uint32 a1 = (*b1 << 16) + (*(b1 + 1) << 8) + *(b1 + 2);
93     uint32 a2 = (*b2 << 16) + (*(b2 + 1) << 8) + *(b2 + 2);
94
95     return (a1 - a2);
96 }
97
98 void S9xSDD1SaveLoggedData ()
99 {
100     if (Memory.SDD1LoggedDataCount != Memory.SDD1LoggedDataCountPrev)
101     {
102         qsort (Memory.SDD1LoggedData, Memory.SDD1LoggedDataCount, 8,
103                S9xCompareSDD1LoggedDataEntries);
104
105         const char * sdd1_dat_file = S9xGetFilename(FILE_SDD1_DAT);
106         FILE *fs = fopen (sdd1_dat_file, "wb");
107
108         if (fs)
109         {
110             fwrite (Memory.SDD1LoggedData, 8,
111                     Memory.SDD1LoggedDataCount, fs);
112             fclose (fs);
113 #if defined(__linux)
114             chown (sdd1_dat_file, getuid (), getgid ());
115 #endif
116         }
117         Memory.SDD1LoggedDataCountPrev = Memory.SDD1LoggedDataCount;
118     }
119 }
120
121 void S9xSDD1LoadLoggedData ()
122 {
123     FILE *fs = fopen (S9xGetFilename(FILE_SDD1_DAT), "rb");
124
125     Memory.SDD1LoggedDataCount = Memory.SDD1LoggedDataCountPrev = 0;
126
127     if (fs)
128     {
129         int c = fread (Memory.SDD1LoggedData, 8, 
130                        MEMMAP_MAX_SDD1_LOGGED_ENTRIES, fs);
131
132         if (c != EOF)
133             Memory.SDD1LoggedDataCount = Memory.SDD1LoggedDataCountPrev = c;
134         fclose (fs);
135     }
136 }