-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cartridge.cs
165 lines (157 loc) · 5.47 KB
/
Cartridge.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
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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sth1edwv
{
public static class Cartridge
{
public class MemMapEntry
{
public uint offset;
public string label;
public MemMapEntry(uint off, string s)
{
offset = off;
label = s;
}
}
public static byte[] memory;
public static List<MemMapEntry> labels;
public static List<MemMapEntry> levels;
public static List<Level> level_list;
public static List<GameText> gameText;
public static void Load(string path)
{
memory = File.ReadAllBytes(path);
labels = ReadList(Properties.Resources.map);
levels = ReadList(Properties.Resources.levels);
ReadLevels();
ReadGameText();
}
public static void ReadLevels()
{
level_list = new List<Level>();
foreach (MemMapEntry e in levels)
level_list.Add(new Level(e.offset));
}
public static void ReadGameText()
{
gameText = new List<GameText>();
for (int i = 0; i < 6; i++)
{
MemoryStream m = new MemoryStream();
m.Write(memory, 0x122D + i * 0xF, 0xF);
gameText.Add(new GameText(m.ToArray(), i < 3));
}
for (int i = 0; i < 3; i++)
{
MemoryStream m = new MemoryStream();
m.Write(memory, 0x197E + i * 0x10, 0x10);
gameText.Add(new GameText(m.ToArray(), true));
}
}
public static List<MemMapEntry> ReadList(string s)
{
List<MemMapEntry> result = new List<MemMapEntry>();
using (StringReader sr = new StringReader(s))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(' ');
int len = parts[0].Length;
string offset = parts[0].Substring(1, len - 1);
string label = line.Substring(len + 1, line.Length - len - 1);
result.Add(new MemMapEntry(Convert.ToUInt32(offset, 16), label));
}
}
return result;
}
public static string ROMSizes(int size)
{
StringBuilder sb = new StringBuilder();
sb.Append(" (");
switch (size)
{
case 0xA:
sb.Append("8KB");
break;
case 0xB:
sb.Append("16KB");
break;
case 0xC:
sb.Append("32KB");
break;
case 0xD:
sb.Append("48KB");
break;
case 0xE:
sb.Append("64KB");
break;
case 0xF:
sb.Append("128KB");
break;
case 0x0:
sb.Append("256KB");
break;
case 0x1:
sb.Append("512KB");
break;
case 0x2:
sb.Append("1MB");
break;
}
sb.Append(")");
return sb.ToString();
}
public static string Regions(int region)
{
StringBuilder sb = new StringBuilder();
sb.Append(" (");
switch (region)
{
case 0x3:
sb.Append("SMS Japan");
break;
case 0x4:
sb.Append("SMS Export");
break;
case 0x5:
sb.Append("GG Japan");
break;
case 0x6:
sb.Append("GG Export");
break;
case 0x7:
sb.Append("GG International");
break;
}
sb.Append(")");
return sb.ToString();
}
public static string MakeSummary()
{
StringBuilder sb = new StringBuilder();
MemoryStream m = new MemoryStream(memory);
m.Seek(0x7FF0, 0);
byte[] header = new byte[16];
m.Read(header, 0, 16);
sb.AppendLine("Cartridge Header");
sb.Append("Magic : \"");
for (int i = 0; i < 8; i++)
sb.Append((char)header[i]);
sb.AppendLine("\"");
sb.AppendLine("Reserved : 0x" + header[8].ToString("X2") + header[9].ToString("X2"));
sb.AppendLine("Checksum : 0x" + header[10].ToString("X2") + header[11].ToString("X2"));
sb.AppendLine("Product code : " + (header[14] >> 4).ToString("X1") + header[13].ToString("X2") + header[12].ToString("X2"));
sb.AppendLine("Version : 0x" + (header[14] & 7).ToString("X1"));
sb.AppendLine("Region : 0x" + (header[15] >> 4).ToString("X1") + Regions(header[15] >> 4));
sb.AppendLine("ROM Size : 0x" + (header[15] & 7).ToString("X1") + ROMSizes(header[15] & 7));
sb.AppendLine("ROM Header : \"" + Encoding.UTF7.GetString(memory, 0x3B, 0x2A) + "\"");
return sb.ToString();
}
}
}