Skip to content

Commit 647231b

Browse files
committed
keeps program from freaking out when ROM file not found, added some command line arguments
1 parent d5ff913 commit 647231b

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

src/org/the429ers/gameboy/Cartridge.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ static Cartridge fromFile(String fileName) {
1111
try {
1212
new FileInputStream(file).read(rom);
1313
} catch (IOException e) {
14-
e.printStackTrace();
14+
System.out.println("File not found: " + fileName);
15+
return null;
1516
}
1617

1718
int cartridgeType = rom[0x0147] & 0xff;

src/org/the429ers/gameboy/GameBoy.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -242,25 +242,6 @@ public void switchRom(String newRom) {
242242

243243
}
244244

245-
public GameBoy() {
246-
super();
247-
BufferedImage img = new BufferedImage(160, 144, BufferedImage.TYPE_3BYTE_BGR);
248-
gbs = new GameBoyScreen(img);
249-
gbs.setDoubleBuffered(true);
250-
gbs.setPreferredSize(new Dimension(500, 500));
251-
gbs.setFocusable(true);
252-
this.add(gbs);
253-
this.setMenuBar(new MainMenuBar(this));
254-
this.pack();
255-
this.setTitle("OOPBoy");
256-
this.setVisible(true);
257-
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
258-
this.addWindowListener(listener);
259-
260-
quickSave = false;
261-
quickLoad = false;
262-
}
263-
264245
public GameBoy(String fileName) {
265246
super();
266247
this.romFileName = fileName;
@@ -449,6 +430,11 @@ public void pause() {
449430
}
450431

451432
public void start() {
433+
if(mmu.getROM() == null){
434+
System.out.println("Invalid cartridge");
435+
return;
436+
}
437+
452438
new Thread(() -> {
453439
paused = false;
454440

@@ -466,12 +452,20 @@ public void dispose() {
466452

467453
public static void main(String[] args) throws InterruptedException {
468454

469-
gb = new GameBoy(DEFAULT_ROM);
470-
471-
Scanner fin = new Scanner(System.in);
472-
if(args.length > 0 && args[0].equals("-d")) {
473-
System.out.print("First breakpoint (hex): ");
474-
gb.breakPoints.add(fin.nextInt(16));
455+
if(args.length > 0) {
456+
if(!args[0].equals("-d")) {
457+
gb = new GameBoy(args[0]);
458+
}else{
459+
gb = new GameBoy(DEFAULT_ROM);
460+
}
461+
462+
if(args[args.length-1].equals("-d")) {
463+
Scanner fin = new Scanner(System.in);
464+
System.out.print("First breakpoint (hex): ");
465+
gb.breakPoints.add(fin.nextInt(16));
466+
}
467+
}else{
468+
gb = new GameBoy(DEFAULT_ROM);
475469
}
476470

477471
gb.start();

src/org/the429ers/gameboy/MMU.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,18 @@ public IPPU getPPU() {
8585
return this.ppu;
8686
}
8787

88+
public Cartridge getROM() {
89+
return this.rom;
90+
}
91+
8892
public MMU() {
8993
//no need to copy boot rom since that is intercepted by readByte
9094
}
9195

9296
// Load rom from disk
9397
public MMU(String fileName) {
9498
this.rom = Cartridge.fromFile(fileName);
95-
this.isCGB = rom.isGBC();
99+
this.isCGB = rom != null && rom.isGBC();
96100
}
97101

98102
public void cleanUp() {
@@ -188,11 +192,11 @@ public int readByte(int location) {
188192
}
189193

190194
if(location < 0x8000){
191-
return rom.readByte(location);
195+
return rom == null? 0 : rom.readByte(location);
192196
}
193197

194198
if (location >= 0xA000 && location <= 0xBFFF) {
195-
return rom.readByte(location);
199+
return rom == null? 0 : rom.readByte(location);
196200
}
197201

198202
if (location >= 0x8000 && location <= 0x9FFF) {
@@ -271,12 +275,12 @@ public void writeByte(int location, int toWrite){
271275
}
272276

273277
if(location < 0x7fff){
274-
rom.writeByte(location, toWrite);
278+
if(rom != null) rom.writeByte(location, toWrite);
275279
return;
276280
}
277281

278282
if (location >= 0xA000 && location <= 0xBFFF) {
279-
rom.writeByte(location, toWrite);
283+
if(rom != null) rom.writeByte(location, toWrite);
280284
return;
281285
}
282286

0 commit comments

Comments
 (0)