-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCartridge.java
More file actions
90 lines (77 loc) · 2.44 KB
/
Cartridge.java
File metadata and controls
90 lines (77 loc) · 2.44 KB
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
import java.awt.*;
import java.io.*;
public final class Cartridge {
private File romFile;
private FileInputStream fileStream;
private FileDialog openDialog;
private String fileName, fullPath;
public byte[] romData;
public int romSize, numPages;
private boolean romLoaded;
public Cartridge() {
romLoaded = false;
}
public void showOpenDialog(Frame parent) {
openDialog = new FileDialog(parent, "Abrir ROM", FileDialog.LOAD);
openDialog.setDirectory("./");
openDialog.setFile("*.sms;*.gg");
openDialog.show();
fileName = openDialog.getFile();
if(fileName != null && fileName.trim().length() != 0) {
fullPath = openDialog.getDirectory() + fileName;
try {
System.out.println("CARTRIDGE: Loading rom " + fileName.toUpperCase() + "... ");
romFile = new File(fullPath);
fileStream = new FileInputStream(romFile);
romSize = (int)romFile.length();
if(romSize % 4096 != 0) { // Header detected. Skip it.
System.out.print("CARTRIDGE: ROM Header Found. Skipping it... ");
romSize -= 512;
romData = new byte[romSize];
numPages = romSize / 16384;
try { fileStream.skip(512); System.out.println("OK"); }
catch(IOException e) { System.err.println("CRITICAL I/O ERROR: file error"); System.exit(1); }
}
else {
System.out.println("CARTRIDGE: ROM Header not found");
romData = new byte[romSize];
numPages = romSize / 16384;
}
try {
fileStream.read(romData);
romLoaded = true;
System.out.println("CARTRIDGE: Successfully loaded " + Integer.toString(romSize) + " bytes.");
}
catch(IOException e) { System.err.println("CRITICAL I/O ERROR: File cannot be loaded!"); System.exit(1); }
}
catch(FileNotFoundException e) {
fileName = null;
fullPath = null;
romLoaded = false;
System.err.println("CARTRIDGE ERROR: File not found.");
}
}
else {
romLoaded = false;
}
try { fileStream.close(); }
catch(Exception e) {}
fileStream = null;
openDialog = null;
}
public String getFileName() {
return fileName;
}
public String getFullPath() {
return fullPath;
}
public int getRomSize() {
return romSize;
}
public boolean isLoaded() {
return romLoaded;
}
public void unload() {
romLoaded = false;
}
}