-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
337 lines (283 loc) · 9.57 KB
/
main.cpp
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
#include <stdio.h>
#include <ctype.h>
#include "buf.h"
#include "smc.h"
#include "tables.h"
#include "memmap.h"
#include "disasm.h"
//32768 bytes/32KB
#define SMC_BLOCK_SIZE 0x8000
static const unsigned header_offsets[] = {0x7fc0, 0xffc0, 0x40ffc0};
static size_t smc_off=0, header_off=0, rom_size;
static snes_rom_header rom_header;
static snes_rom_info rom_info = {(rom_type)0};
static void dump_rom_header(snes_rom_header &h);
static swc_header read_swc_header(stream_reader &smc)
{
swc_header swc;
swc.size = smc.read_u16le();
swc.mode_select = smc.read_u8();
swc.byte3 = smc.read_u8();
smc.skip(4);
swc.magic = smc.read_u16be();
swc.code = smc.read_u8();
swc.mode = swc.mode_select >> 7;
swc.multi= (swc.mode_select >> 6) & 1;
swc.sram = (swc.mode_select >> 5) & 1;
swc.dram = (swc.mode_select >> 4) & 1;
swc.sram_size = (swc.mode_select >> 2) & 3;
return swc;
}
static fig_header read_fig_header(stream_reader &smc)
{
fig_header fig;
fig.size = smc.read_u16le();
fig.multi = smc.read_u8();
fig.hirom = smc.read_u8();
fig.ex1 = smc.read_u8();
fig.ex2 = smc.read_u8();
return fig;
}
static const char *yes(bool y)
{
return y ? "yes" : "no";
}
static size_t smc_header(stream_reader &smc) {
size_t fs = smc.size(), extrasize = fs % SMC_BLOCK_SIZE;
fprintf(stderr, "-File size: %lu 32KB blocks, %lu 8KB pages\n", fs/0x8000, fs/8192);
if (extrasize == 0) {
fprintf(stderr, "-No copier header found (%lu bytes left over).\n", extrasize);
} else {
swc_header swc;
fig_header fig;
fprintf(stderr, "-Copier header found?\n\n");
swc = read_swc_header(smc);
smc.seek(0);
fig = read_fig_header(smc);
if (swc.magic == 0xAABB) {
if (swc.mode_select) {
printf("Front Fareast SWC/SMC header:\n");
printf("\t8KB pages: %d (%s)\n", swc.size, (swc.size == fs/8192) ? "correct" : "incorrect");
printf("\tMode select: %x\n", swc.mode_select);
printf("\tJump to $8000 instead of RST: %s\n", yes(swc.mode));
printf("\tMulti-image: %s\n", yes(swc.multi));
printf("\tSRAM map: %s\n", swc.sram ? "HiROM" : "LoROM");
printf("\tDRAM map: %s\n", swc.dram ? "HiROM" : "LoROM");
printf("\tSRAM size: %x (%d KBit)\n", swc.sram_size, swc.sram_size ? 1 << (swc.sram_size+3) : 0);
printf("\tCode: %d (%s)\n\n", swc.code, (swc.code == 4) ? "correct" : "incorrect");
} else {
printf("---Broken SWC/SMC header(?):\n");
printf("\t8KB pages: %d (%s)\n", swc.size, (swc.size == fs/8192) ? "correct" : "incorrect");
printf("\tBytes 2 and 3: %#x %#x\n", swc.mode_select, swc.byte3);
printf("\tCode: %d (%s)\n\n", swc.code, (swc.code == 4) ? "correct" : "incorrect");
}
} else if (fig.ex2 == 0x80 || fig.ex2 == 0x82 || fig.ex2 == 0x83 || fig.ex1 == 0xDD) {
printf("Pro Fighter header:\n");
printf("\t8KB pages: %d (%s)\n", fig.size, (fig.size == fs/8192) ? "correct" : "incorrect");
printf("\tMulti-image: %s\n", yes(fig.multi));
printf("\tROM type: %s\n", fig.hirom ? "HiROM" : "LoROM");
printf("\tComplicated code: %.2X%.2X\n", fig.ex1, fig.ex2);
} else printf("Magic %#x unknown...\n", swc.magic);
smc.seek(512);
}
return extrasize;
}
static snes_interrupt read_exception_table(stream_reader &smc)
{
snes_interrupt i;
i.cop = smc.read_u16le();
i.brk = smc.read_u16le();
i.abort = smc.read_u16le();
i.vbl = smc.read_u16le();
i.main = smc.read_u16le();
i.irq = smc.read_u16le();
return i;
}
static void read_rom_header_at(stream_reader &smc, size_t at, snes_rom_header *h)
{
char maker[2];
smc.seek(at - 16);
smc.read_bytes(maker, 2);
sscanf(maker, "%2hhx", &h->company1);
smc.read_bytes(h->game_code, 4);
smc.read_bytes(h->zero_fill, 7);
h->expansion_ram = smc.read_u8();
h->special_version = smc.read_u8();
h->subcartridge = smc.read_u8();
smc.read_bytes(h->rom_name, 21);
h->map_mode = smc.read_u8();
h->cart_type = smc.read_u8();
h->rom_size = smc.read_u8();
h->sram_size = smc.read_u8();
h->region = smc.read_u8();
h->company2 = smc.read_u8();
h->version = smc.read_u8();
h->ichecksum = smc.read_u16le();
h->checksum = smc.read_u16le();
smc.skip(4);
h->norm_exc = read_exception_table(smc);
smc.skip(4);
h->emu_exc = read_exception_table(smc);
}
static int rom_header_score(snes_rom_header &h, rom_type type)
{
int s = 0;
s += h.cart_type < 8;
s += h.rom_size < 16;
s += h.sram_size < 8;
s += h.region < 14;
s += h.version < 128;
s += (h.ichecksum + h.checksum) == 0xffff;
return s;
}
static snes_rom_header find_rom_header(stream_reader &smc, size_t *off)
{
snes_rom_header headers[3];
int scores[3] = {0};
for (size_t i = 0; i < sizeof(header_offsets) / sizeof(unsigned); i++) {
unsigned off = header_offsets[i] + smc_off;
if (rom_size < off) {
fprintf(stderr, "-The ROM is too small for a header at %#x\n", off);
scores[i] = -1;
} else {
fprintf(stderr, "-Checking for header at %#x\n", off);
read_rom_header_at(smc, off, &headers[i]);
scores[i] = rom_header_score(headers[i], (rom_type)i);
}
fprintf(stderr, "-Score: %d\n", scores[i]);
}
int hnum = 0;
for (int i = 0; i < 3; i++) if (scores[i] > scores[hnum]) hnum = i;
*off = header_offsets[hnum] + smc_off;
fprintf(stderr, "-Using header at %#x\n\n", (unsigned)*off);
rom_info.type = (rom_type)hnum;
return headers[hnum];
}
static void clean_puts(char *n, size_t s)
{
while (s--) {
char c = *n++;
putchar((c == ' ' || isgraph(c)) ? c : '.');
}
putchar('\n');
}
static void dump_rom_header(snes_rom_header &h)
{
if (h.company2 != 0x33) printf("This is a compact copied ROM header. (missing first 16 bytes)\n");
printf("This is a %s%s.\n\n", rom_info.interleaved ? "interleaved " : "", get_rom_type_name(rom_info));
printf("ROM header:\n");
if (h.company2 == 0x33) {
printf("Game code: ");
clean_puts(h.game_code, 4);
printf("\tExpansion RAM size: %d (%d KBit)\n"
"\tSpecial version: v%d\n"
"\tCartridge sub-id: %d\n",
h.expansion_ram, h.expansion_ram ? (1 << (h.expansion_ram + 3)) : 0, h.special_version, h.subcartridge);
}
printf("\tName: ");
clean_puts(h.rom_name, 21);
printf("\tMap mode: %#x\n"
"\tROM type: %#x\n"
"\tROM size: %d (%d MBit)\n"
"\tSRAM size: %d (%d KBit)\n"
"\tRegion: %d (%s)\n"
"\tCompany: %d (%s)\n"
"\tVersion: 1.%d\n"
"\tInverse checksum: %#x\n"
"\tChecksum: %#x %s\n\n",
h.map_mode , h.cart_type, h.rom_size, 1 << (h.rom_size-7),
h.sram_size, 1 << (h.sram_size + 3), h.region, get_rom_region_name(h), h.company2 == 0x33 ? h.company1 : h.company2, get_rom_company_name(h),
h.version, h.ichecksum, h.checksum, (h.ichecksum + h.checksum == 0xFFFF) ? "" : "(!)");
}
static void set_ram_type(uint8_t type, snes_rom_info &ri)
{
switch (type) {
case 2:
ri.battery = true;
case 1:
ri.ram = true;
}
}
static void find_game_chips(snes_rom_header &rh, snes_rom_info &ri)
{
//if (rh.company == 0x33) ri.type = ri.type ? BSCHiROM : BSCLoROM;
ri.hirom = rh.map_mode % 2;
if (ri.type != ri.hirom) ri.interleaved = true;
if (ri.type == LoROM && rom_size >= 0x401000) ri.type = ExLoROM;
if (rh.map_mode >= 0x30) ri.fastrom = true;
ri.map_mode = rh.map_mode & ~16;
if (rh.cart_type < 3) set_ram_type(rh.cart_type, ri);
else {
set_ram_type((rh.cart_type - 3) & 3, ri);
ri.coprocessor = (coprocessor_type)(rh.cart_type >> 4);
}
if (ri.coprocessor == DSP) ri.dsp_ver = 1; // wrong
ri.rom_mbit = 1 << (rh.rom_size-7);
ri.sram_kbit = 1 << (rh.sram_size+3);
ri.eram_kbit = 1 << (rh.expansion_ram+3);
}
static void dump_chip_list(snes_rom_info &ri)
{
const char *coproc_names[] = {"None", "DSP", "Super FX", "OBC1", "SA-1"};
printf("Hardware features:\n");
if (ri.coprocessor) printf("\tCoprocessor: %s\n", ri.coprocessor >= UnknownCPU ? "Unknown" : coproc_names[ri.coprocessor]);
if (ri.dsp_ver) printf("\tDSP version: %d\n", ri.dsp_ver);
printf("\tMemory map: Mode %x\n", ri.map_mode);
printf("\tCPU in fast mode: %s\n", yes(ri.fastrom));
printf("\tRAM: %s\n", yes(ri.ram));
printf("\tBattery: %s\n\n", yes(ri.battery));
}
static bool map_memory(snes_mapper &map, snes_rom_info &ri)
{
if (ri.map_mode == 0x20) {
fprintf(stderr, "-Mapping LoROM...\n");
map.map_rom(0x00, 0x7D, 0x8000, 0xFFFF, smc_off, rom_size - smc_off);
fprintf(stderr, "\n");
return 0;
}
fprintf(stderr, "-No mapping for mode %x\n", ri.map_mode);
return 1;
}
static void print_one_vector(const char *name, snes_mapper &map, uint16_t v)
{
printf("\t%s: %.2X ", name, v);
uint8_t *real = map.lookup(0, v);
if (real) printf("(%#x)\n", unsigned(real - map.base));
else printf("(unmapped)\n");
}
static void print_interrupt_table(snes_mapper &map, snes_rom_header &rh, bool emu)
{
snes_interrupt &i = emu ? rh.emu_exc : rh.norm_exc;
print_one_vector("COP", map, i.cop);
if (!emu) print_one_vector("BRK", map, i.brk);
print_one_vector("ABORT", map, i.abort);
print_one_vector("VBL/NMI", map, i.vbl);
if (emu) print_one_vector("MAIN/RESET", map, i.main);
print_one_vector(emu ? "IRQ/BRK" : "IRQ", map, i.irq);
}
static void print_vectors(snes_mapper &map, snes_rom_header &rh)
{
printf("Normal-mode exception handlers:\n");
print_interrupt_table(map, rh, 0);
printf("6502-mode exception handlers:\n");
print_interrupt_table(map, rh, 1);
}
int main (int argc, char * const argv[]) {
if (argc < 2) return 0;
mapped_file rf(argv[1]);
stream_reader smc(rf);
rom_size = smc.size();
smc_off = smc_header(smc);
rom_header = find_rom_header(smc, &header_off);
find_game_chips(rom_header, rom_info);
dump_rom_header(rom_header);
dump_chip_list(rom_info);
smc.seek(0);
snes_mapper map(smc.cur());
if (map_memory(map, rom_info) == 0) {
print_vectors(map, rom_header);
printf("\n");
disassemble(map, rom_header);
}
return 0;
}