Skip to content

Commit 7794c7e

Browse files
committed
fix: remove file extension restriction and add graceful error handling for rom loading
1 parent abd6be8 commit 7794c7e

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ <h1>CHIP-8 Emulator</h1>
2323
<div class="controls-group">
2424
<label for="rom-input" class="btn btn-primary">
2525
Load ROM
26-
<input type="file" id="rom-input" accept=".ch8,.rom" style="display: none;">
26+
<input type="file" id="rom-input" style="display: none;">
2727
</label>
2828
</div>
2929
<div class="controls-group">

js/emulator.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,38 @@ class Emulator {
4040
if (!file) return;
4141

4242
const reader = new FileReader();
43+
4344
reader.onload = (e) => {
44-
const buffer = e.target.result;
45-
const program = new Uint8Array(buffer);
46-
this.reset();
47-
this.cpu.loadProgram(program);
48-
this.updateWidgets();
45+
try {
46+
const buffer = e.target.result;
47+
const program = new Uint8Array(buffer);
48+
49+
if (program.length === 0) {
50+
alert('Error: File is empty');
51+
return;
52+
}
53+
54+
if (program.length > 4096 - 0x200) {
55+
alert(`Error: ROM too large (${program.length} bytes). Maximum is ${4096 - 0x200} bytes.`);
56+
return;
57+
}
58+
59+
this.reset();
60+
this.cpu.loadProgram(program);
61+
this.updateWidgets();
62+
63+
console.log(`ROM loaded successfully: ${file.name} (${program.length} bytes)`);
64+
} catch (error) {
65+
alert(`Error loading ROM: ${error.message}`);
66+
console.error('ROM loading error:', error);
67+
}
4968
};
69+
70+
reader.onerror = () => {
71+
alert('Error reading file');
72+
console.error('File reader error:', reader.error);
73+
};
74+
5075
reader.readAsArrayBuffer(file);
5176
}
5277

0 commit comments

Comments
 (0)