Chipvuelto8/index.html
2024-09-06 12:43:07 +02:00

96 lines
2.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chip 8 emulator Test</title>
<style>
body {
background-color: #000;
color: #EEE;
}
#emulator-screen{
border: 1px solid pink;
}
</style>
<script src="./dist/revuelto8ts.js"></script>
</head>
<body>
<h1>Chip 8 emulator</h1>
<!-- Original screen is 64x32 -->
<canvas id="emulator-screen" width="512" height="256"></canvas>
<script>
const emulator = new Revuelto8ts.Chip8Emulator();
const emulatorCanvas = document.getElementById('emulator-screen');
let fullDirRoms = [
'./roms/1-chip8-logo.ch8',
'./roms/2-ibm-logo.ch8',
'./roms/3-corax+.ch8',
'./roms/4-flags.ch8',
'./roms/5-quirks.ch8',
'./roms/6-keypad.ch8',
'./roms/7-beep.ch8',
'./roms/8-scrolling.ch8',
'./roms/Maze[David Winter, 199x].ch8',
];
// Set user keys
emulator.setUserKey('1'.charCodeAt(0), 1); // 1
emulator.setUserKey('2'.charCodeAt(0), 2); // 2
emulator.setUserKey('3'.charCodeAt(0), 3); // 3
emulator.setUserKey('4'.charCodeAt(0), 12); // C
emulator.setUserKey('q'.charCodeAt(0), 4); // 4
emulator.setUserKey('w'.charCodeAt(0), 5); // 5
emulator.setUserKey('e'.charCodeAt(0), 6); // 6
emulator.setUserKey('r'.charCodeAt(0), 13); // D
emulator.setUserKey('a'.charCodeAt(0), 7); // 7
emulator.setUserKey('s'.charCodeAt(0), 8); // 8
emulator.setUserKey('d'.charCodeAt(0), 9); // 9
emulator.setUserKey('f'.charCodeAt(0), 14); // E
emulator.setUserKey('z'.charCodeAt(0), 10); // A
emulator.setUserKey('x'.charCodeAt(0), 0); // 0
emulator.setUserKey('c'.charCodeAt(0), 11); // B
emulator.setUserKey('v'.charCodeAt(0), 15); // F
document.addEventListener("keydown", function(evt) {
emulator.keyDownEvent(evt.key.charCodeAt(0));
});
document.addEventListener("keyup", function(evt) {
emulator.keyUpEvent(evt.key.charCodeAt(0));
});
// Set audio
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
let volume = 0.02; // Volume between [1, 0] // Set it low if you don't want to become deaf
emulator.startAudio(audioContext, volume);
// Load ROM
fetch(fullDirRoms[6])
.then(response => response.arrayBuffer())
.then(arrayBufferRom => {
let romData = new Uint8Array(arrayBufferRom);
emulator.loadRom(romData);
});
setInterval(function() {
if(emulator.isRomLoaded()) {
console.log(emulator.getCPUStatus());
emulator.emulateCycle();
emulator.drawToCanvas(emulatorCanvas, 8);
}
}, 60);
</script>
</body>
</html>