-
Notifications
You must be signed in to change notification settings - Fork 11
/
rom_selector.h
225 lines (205 loc) · 5.55 KB
/
rom_selector.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef _22B2B909_1134_6471_AE6D_14EF3AF46BF0
#define _22B2B909_1134_6471_AE6D_14EF3AF46BF0
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "tar.h"
#include "builtinrom.h"
#include "InfoNES_Mapper.h"
inline bool checkNESMagic(const uint8_t *data)
{
bool ok = false;
int nIdx;
if (memcmp(data, "NES\x1a", 4) == 0)
{
uint8_t MapperNo = *(data + 6) >> 4;
for (nIdx = 0; MapperTable[nIdx].nMapperNo != -1; ++nIdx)
{
if (MapperTable[nIdx].nMapperNo == MapperNo)
{
ok = true;
break;
}
}
// if (!ok)
// {
// printf("checkNESMagic: skipped rom with invalid mapper #%d\n", MapperNo);
// }
}
return ok;
}
inline bool hasNVRAM(const uint8_t *data)
{
auto info1 = data[6];
return info1 & 2;
}
class ROMSelector
{
const uint8_t *singleROM_{};
const uint8_t *BuiltInRom_{};
int startBuiltIn_ = false;
int selectedIndex_ = 0;
int numberOfEntries = 0;
const uint8_t *tarAddress{};
public:
void init(uintptr_t addr, int startingRom)
{
auto *p = reinterpret_cast<const uint8_t *>(addr);
BuiltInRom_ = reinterpret_cast<const uint8_t *>(builtinrom);
if (checkNESMagic(p))
{
singleROM_ = p;
printf("Single ROM.\n");
return;
}
numberOfEntries = GetValidTAREntries(p, checkNESMagic);
printf("%zd compatible ROMs in flash.\n", numberOfEntries);
if (numberOfEntries > 0)
{
tarAddress = p;
for (int i = 0; i < numberOfEntries; i++)
{
TAREntry e = extractTAREntryatindex(i, p, checkNESMagic);
if (e.data)
{
printf(" %s: %p, %zd\n", e.filename.data(), e.data, e.size);
}
}
}
if (startingRom == -1 || numberOfEntries == 0)
{
startBuiltIn_ = true;
}
else
{
selectedIndex_ = startingRom;
}
}
const bool isBuiltInRomPlaying() const
{
return startBuiltIn_;
}
const uint8_t *getCurrentROM() const
{
if (startBuiltIn_ == false)
{
if (singleROM_)
{
return singleROM_;
}
if (numberOfEntries)
{
return extractTAREntryatindex(selectedIndex_, tarAddress, checkNESMagic).data;
}
}
return BuiltInRom_;
}
int getCurrentNVRAMSlot() const
{
auto currentROM = getCurrentROM();
if (!currentROM)
{
return -1;
}
if (startBuiltIn_)
{
if (hasNVRAM(BuiltInRom_))
{
printf("Built-in rom has NVRAM, returning slot 0\n");
return 0; // BuiltIn Ram has slot 0
}
printf("Built-in rom has no NVRAM, returning slot -1\n");
return -1;
}
if (!hasNVRAM(currentROM))
{
printf("Current rom has no NVRAM, returning slot -1\n");
return -1;
}
if (singleROM_)
{
printf("Custom rom (single rom) Rom has no NVRAM,returning slot -1\n");
return 1; // Singlerom has slot 1
}
// Has Game in Tar archie a SRAM slot?
int slot = 1;
int foundSlot = -1;
for (int i = 0; i <= selectedIndex_; ++i)
{
if (hasNVRAM(extractTAREntryatindex(i, tarAddress, checkNESMagic).data))
{
++slot;
if (i == selectedIndex_)
{
foundSlot = slot;
}
}
}
if (foundSlot == -1)
{
// ROm in archive has no SRAM
printf("Rom %d in tar archive has no NVRAM. Returning slot -1\n", selectedIndex_);
}
else
{
printf("Rom %d in tar archive has NVRAM. Returning slot %d\n", selectedIndex_, foundSlot);
}
return foundSlot;
}
void next()
{
startBuiltIn_ = false;
if (singleROM_ || numberOfEntries == 0)
{
return;
}
++selectedIndex_;
printf("Next: Selected Index %d\n", selectedIndex_);
if (selectedIndex_ == numberOfEntries)
{
selectedIndex_ = 0;
printf("Next: reset Index %d\n", selectedIndex_);
}
}
void prev()
{
startBuiltIn_ = false;
if (singleROM_ || numberOfEntries == 0)
{
return;
}
--selectedIndex_;
printf("Prev: Selected Index %d\n", selectedIndex_);
if (selectedIndex_ < 0)
{
selectedIndex_ = numberOfEntries - 1;
printf("Prev: reset Index %d\n", selectedIndex_);
}
}
uint8_t GetCurrentRomIndex() const
{
return (startBuiltIn_ ? -1 : selectedIndex_);
}
void selectcustomrom()
{
startBuiltIn_ = true;
}
const char *GetCurrentGameName()
{
if (startBuiltIn_)
return BUILTINROMNAME;
return (char *)extractTAREntryatindex(selectedIndex_, tarAddress, checkNESMagic).filename.data();
}
void setRomIndex(int index)
{
if (numberOfEntries > 0)
{
if (index >= 0 && index < numberOfEntries)
{
selectedIndex_ = index;
startBuiltIn_ = false;
}
}
}
};
#endif /* _22B2B909_1134_6471_AE6D_14EF3AF46BF0 */