-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemulator.js
More file actions
188 lines (152 loc) · 5.21 KB
/
emulator.js
File metadata and controls
188 lines (152 loc) · 5.21 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
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
class Emulator {
constructor() {
this.cpu = new CPU();
this.renderer = new Renderer(10);
this.keyboard = new Keyboard();
this.sound = new Sound();
this.cpu.renderer = this.renderer;
this.cpu.keyboard = this.keyboard;
this.cpu.sound = this.sound;
this.registersWidget = new RegistersWidget('registers-container');
this.memoryWidget = new MemoryWidget('memory-container');
this.stackWidget = new StackWidget('stack-container');
this.statusWidget = new StatusWidget('status-container');
this.fpsInterval = 1000 / 60;
this.then = Date.now();
this.running = false;
this.setupControls();
}
setupControls() {
document.getElementById('play-btn').addEventListener('click', () => this.start());
document.getElementById('pause-btn').addEventListener('click', () => this.pause());
document.getElementById('step-btn').addEventListener('click', () => this.step());
document.getElementById('reset-btn').addEventListener('click', () => this.reset());
document.getElementById('rom-select').addEventListener('change', (e) => this.loadROMFromSelect(e));
document.getElementById('rom-input').addEventListener('change', (e) => this.loadROMFromFile(e));
const speedSlider = document.getElementById('speed-slider');
const speedValue = document.getElementById('speed-value');
speedSlider.addEventListener('input', (e) => {
this.cpu.speed = parseInt(e.target.value);
speedValue.textContent = e.target.value;
});
const volumeSlider = document.getElementById('volume-slider');
const volumeValue = document.getElementById('volume-value');
volumeSlider.addEventListener('input', (e) => {
const volume = parseInt(e.target.value) / 100;
this.sound.setVolume(volume);
volumeValue.textContent = e.target.value;
});
const frequencySlider = document.getElementById('frequency-slider');
const frequencyValue = document.getElementById('frequency-value');
frequencySlider.addEventListener('input', (e) => {
this.sound.setFrequency(parseInt(e.target.value));
frequencyValue.textContent = e.target.value;
});
}
async loadROMFromSelect(event) {
const romName = event.target.value;
if (!romName) return;
try {
const response = await fetch(`roms/${romName}`);
if (!response.ok) {
throw new Error(`Failed to load ROM: ${response.statusText}`);
}
const buffer = await response.arrayBuffer();
const program = new Uint8Array(buffer);
if (program.length === 0) {
alert('Error: ROM file is empty');
return;
}
if (program.length > 4096 - 0x200) {
alert(`Error: ROM too large (${program.length} bytes). Maximum is ${4096 - 0x200} bytes.`);
return;
}
this.reset();
this.cpu.loadProgram(program);
this.updateWidgets();
console.log(`ROM loaded successfully: ${romName} (${program.length} bytes)`);
} catch (error) {
alert(`Error loading ROM: ${error.message}`);
console.error('ROM loading error:', error);
}
}
loadROMFromFile(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (e) => {
try {
const buffer = e.target.result;
const program = new Uint8Array(buffer);
if (program.length === 0) {
alert('Error: File is empty');
return;
}
if (program.length > 4096 - 0x200) {
alert(`Error: ROM too large (${program.length} bytes). Maximum is ${4096 - 0x200} bytes.`);
return;
}
this.reset();
this.cpu.loadProgram(program);
this.updateWidgets();
console.log(`ROM loaded successfully: ${file.name} (${program.length} bytes)`);
} catch (error) {
alert(`Error loading ROM: ${error.message}`);
console.error('ROM loading error:', error);
}
};
reader.onerror = () => {
alert('Error reading file');
console.error('File reader error:', reader.error);
};
reader.readAsArrayBuffer(file);
}
start() {
if (!this.running) {
this.running = true;
this.loop();
}
}
pause() {
this.running = false;
}
step() {
this.cpu.cycle();
this.renderer.render();
this.updateWidgets();
}
reset() {
this.running = false;
this.sound.stop();
this.cpu = new CPU();
this.cpu.renderer = this.renderer;
this.cpu.keyboard = this.keyboard;
this.cpu.sound = this.sound;
this.renderer.clear();
this.renderer.render();
this.updateWidgets();
}
loop() {
if (!this.running) return;
requestAnimationFrame(() => this.loop());
const now = Date.now();
const elapsed = now - this.then;
if (elapsed > this.fpsInterval) {
this.then = now - (elapsed % this.fpsInterval);
this.cpu.cycle();
this.renderer.render();
this.updateWidgets();
}
}
updateWidgets() {
this.registersWidget.update(this.cpu);
this.memoryWidget.update(this.cpu);
this.stackWidget.update(this.cpu);
this.statusWidget.update(this.cpu, this.keyboard);
}
}
let emulator;
window.addEventListener('DOMContentLoaded', () => {
emulator = new Emulator();
emulator.updateWidgets();
});