This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathROM.java
202 lines (179 loc) · 5.1 KB
/
ROM.java
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
/* GameGuha
* ROM Loader class
*/
import java.io.*;
public class ROM{
private int selectedBank = 0; // Selected memory bank -implement function-
static final int bankSize = 0x4000;
static final int ramSize = 0x2000;
public int rom[][];
public int ram[][];
// String constructor (usually for testing)
public ROM(String filename){
load(new File(filename));
}
// For use when we have a GUI ROM loading
public ROM(File file){
load(file);
}
// Loads the ROM into memory
private void load(File file){
int firstBank[] = new int[bankSize];
try{
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
for(int i = 0; i < 0x4000; i++)
{
firstBank[i] = buf.read();
}
int numBanks = getROMSize(firstBank);
rom = new int[numBanks][bankSize];
int numRAMBanks = getRAMSize();
if(numRAMBanks != 0)
ram = new int[numRAMBanks][];
for(int j = 1 ; j < numBanks; j++)
for(int i = 0; i < 0x4000; i++)
{
rom[j][i] = buf.read();
}
for(int i = 0; i < 0x4000; i++)
{
rom[0][i] = firstBank[i];
}
buf.close();
}
catch(Exception e){ e.printStackTrace(); }
}
public int[] getDefaultROM()
{
return rom[0];
}
public int[] getROM(int bank)
{
return rom[bank];
}
public int[] getRAM(int bank)
{
if(ram == null)
return null;
return ram[bank];
}
// Returns true if the ROM is a Color GB
public boolean isCGB(){
if(rom[0][0x0143]==0){
return false;
}
return true;
}
// Prints cartridge type, as specified in docs
public void printCartType(){
String out="";
switch(rom[0][0x0147]){
case 0x0: out="0 ROM ONLY"; break;
case 0x1: out="1 ROM+MBC1"; break;
case 0x2: out="2 ROM+MBC1+RAM"; break;
case 0x3: out="3 ROM+MBC1+RAM+BATT"; break;
case 0x5: out="5 ROM+MBC2"; break;
case 0x6: out="6 ROM+MBC2+BATTERY"; break;
case 0x8: out="8 ROM+RAM"; break;
case 0x9: out="9 ROM+RAM+BATTERY"; break;
case 0xB: out="B ROM+MMM01"; break;
case 0xC: out="C ROM+MMM01+SRAM"; break;
case 0xD: out="D ROM+MMM01+SRAM+BATT"; break;
case 0x12: out="12 ROM+MBC3+RAM"; break;
case 0x13: out="13 ROM+MBC3+RAM+BATT"; break;
case 0x19: out="19 ROM+MBC5"; break;
case 0x1A: out="1A ROM+MBC5+RAM"; break;
case 0x1B: out="1B ROM+MBC5+RAM+BATT"; break;
case 0x1C: out="1C ROM+MBC5+RUMBLE"; break;
case 0x1D: out="1D ROM+MBC5+RUMBLE+SRAM"; break;
case 0x1E: out="1E ROM+MBC5+RUMBLE+SRAM+BATT"; break;
case 0x1F: out="1F Pocket Camera"; break;
case 0xFD: out="FD Bandai TAMA5"; break;
case 0xFE: out="FE Hudson HuC-3"; break;
}
System.out.println("Cartridge type: "+out);
}
// Returns ROM size, as specified in docs
public int getROMSize(int bank[]){
String out="";
int numBanks=0;
switch(bank[0x148]){
case 0x0: out="32KB (2 banks)"; numBanks = 2; break;
case 0x1: out="64KB (4 bank)"; numBanks = 4; break;
case 0x2: out="128KB (8 banks)"; numBanks = 8; break;
case 0x3: out="256KB (16 banks)"; numBanks = 16; break;
case 0x4: out="512KB (32 banks)"; numBanks = 32; break;
case 0x5: out="1MB (64 banks)"; numBanks = 64; break;
case 0x6: out="2MB (128 banks)"; numBanks = 128; break;
case 0x7: out="4MB (256 banks)"; numBanks = 256; break;
case 0x52: out="1.1MB (72 banks)"; numBanks = 72; break;
case 0x53: out="1.2MB (80 banks)"; numBanks = 80; break;
case 0x54: out="1.5MB (96 banks)"; numBanks = 96; break;
}
System.out.println("ROM Size: "+out);
return numBanks;
}
public int getRAMSize(){
String out="";
int numBanks=0;
switch(rom[0][0x149]){
case 0x0: out="no RAM"; break;
case 0x1: out="2KB RAM"; numBanks=1; break;
case 0x2: out="8KB RAM"; numBanks=1; break;
case 0x3: out="32KB RAM (8x4)"; numBanks=4; break;
case 0x4: out="128KB RAM (8x16)"; numBanks=16; break;
}
System.out.println("RAM Size: "+out);
return numBanks;
}
// Verify checksum of ROM, prints result
// Returns true if checksum is valid, otherwise false
public boolean verifyChecksum(){
final int[] nintyBitmap =
{0xCE,0xED,0x66,0x66,0xCC,0x0D,0x00,0x0B,0x03,
0x73,0x00,0x83,0x00,0x0C,0x00,0x0D,0x00,0x08,
0x11,0x1F,0x88,0x89,0x00,0x0E,0xDC,0xCC,0x6E,
0xE6,0xDD,0xDD,0xD9,0x99,0xBB,0xBB,0x67,0x63,
0x6E,0x0E,0xEC,0xCC,0xDD,0xDC,0x99,0x9F,0xBB,
0xB9,0x33,0x3E};
int ptr=0x0104;
int x=0;
while (ptr <= 0x0133){
if(rom[0][ptr]!=nintyBitmap[ptr-0x0104])
{
System.out.println("Bitmap Invalid");
System.out.println("Header Checksum Invalid");
break;
}
ptr++;
}
if(ptr==0x0134)
{
//x=0:FOR i=0134h TO 014Ch:x=x-MEM[i]-1:NEXT
for(;ptr<=0x014C;ptr++)
{
x=x-rom[0][ptr]-1; //checksum algorithm
}
if((x&0xFF) == rom[0][ptr]){
System.out.println("Header Checksum Valid");
return true;
}
// else
System.out.println("Header Checksum Invalid");
}
return false;
}
// Prints game title
public void printTitle(){
System.out.printf("ROM Title: %s\n",getTitle());
}
public String getTitle(){
StringBuffer sb = new StringBuffer();
int ptr=0x0134;
while(ptr <0x0143 && rom[0][ptr]!=0){
sb.append((char)rom[0][ptr]);
ptr++;
}
return sb.toString();
}
}