-
Notifications
You must be signed in to change notification settings - Fork 0
/
snes_dumper.cs
111 lines (99 loc) · 3.37 KB
/
snes_dumper.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SnesKit
{
public enum BankTypeEnum { Lo, Hi };
public class RomDump
{
// Indica si la ROM tiene el smc header o no
bool SmcHeader;
// Indica la localización del header de SNES
int HeaderLocation;
// Array con los datos de la ROM
public byte[] Data;
// Los diferentes datos que obtenemos de la ROM
public string Name;
public byte Layout;
public byte CartridgeType;
public byte RomSize;
public byte RamSize;
public byte CountryCode;
public byte LicenseCode;
public byte VersionNumber;
ushort Checksum;
ushort ChecksumCompliment;
public BankTypeEnum BankType;
// Esta funcion permite el analisis de ROMS de SNES con extensiones SMC y SFC
public RomDump(byte[] rom)
{
this.Data = rom;
// Comprobamos si existe el header smc
if (this.Data.Length % 1024 == 512)
SmcHeader = true;
else if (this.Data.Length % 1024 == 0)
SmcHeader = false;
else
throw new Exception("Archivo de rom invalida.");
this.HeaderLocation = 0x81C0;
if (HeaderIsAt(0x07FC0)) // La Rom es LoROM
{
this.BankType = BankTypeEnum.Lo;
}
else if (HeaderIsAt(0x0FFC0))
{
this.BankType = BankTypeEnum.Hi;
}
// Leemos el Header
ReadHeader();
}
// Función para comprobar si el header esta en la dirección correcta
private bool HeaderIsAt(ushort addr)
{
this.HeaderLocation = addr;
return VerifyChecksum();
}
// Offset 0x07FC0 in a headerless LoROM image (LoROM rom sin smc header)
// Offset 0x0FFC0 in a headerless HiROM image (HiROM rom sin smc header)
// verifica el checksum
private bool VerifyChecksum()
{
// La rom tiene header smc
if (SmcHeader)
this.HeaderLocation += 512;
this.ChecksumCompliment = BitConverter.ToUInt16(this.Get(0x1C, 0x1D), 0);
this.Checksum = BitConverter.ToUInt16(this.Get(0x1E, 0x1F), 0);
ushort ver = (ushort)(this.Checksum ^ this.ChecksumCompliment);
return (ver == 0xFFFF);
}
private void ReadHeader()
{
this.Name = Encoding.ASCII.GetString(this.Get(0x00, 0x14)); // 21 chars
this.Layout = this.At(0x15);
this.CartridgeType = this.At(0x16);
this.RomSize = this.At(0x17);
this.RamSize = this.At(0x18);
this.CountryCode = this.At(0x19);
this.LicenseCode = this.At(0x1A);
this.VersionNumber = this.At(0x1B);
}
private string GetROmB()
{
return String.Format("{0}", this.RomSize);
}
private byte[] Get(int from, int to)
{
return this.Data.Skip(this.HeaderLocation + from).Take(to - from + 1).ToArray();
}
private byte At(int addr)
{
return this.Data[this.HeaderLocation + addr];
}
}
}