-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathjavascript-node-local.js
More file actions
62 lines (51 loc) · 1.89 KB
/
javascript-node-local.js
File metadata and controls
62 lines (51 loc) · 1.89 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
/**
* Example script for querying a local `programs.json` file in NodeJS.
* Run with:
* $ node ./examples/javascript-node-local.js <path to ROM file>
*/
// Requirements for Node
const fs = require("fs");
const crypto = require("crypto");
// Import the database JSON files
const hashes = require("../database/sha1-hashes.json");
const programs = require("../database/programs.json");
// Returns a SHA1 hash of the given binary data
function sha1Hash(data) {
const shasum = crypto.createHash("sha1");
shasum.update(data);
return shasum.digest("hex");
}
// Check if parameter exists
if (process.argv.length != 3) {
console.error(
"Run with:\n$ node ./examples/javascript-node-local.js <path to ROM file>"
);
process.exit();
}
// Load the ROM file and calculate the SHA1 hash
const file = fs.readFileSync(process.argv[2]);
const hash = sha1Hash(file);
console.log(hash);
// Find the program and ROM metadata in the CHIP-8 database
if (!(hash in hashes)) {
console.error("That file is not in the CHIP-8 database!");
process.exit();
}
const programMetadata = programs[hashes[hash]];
const romMetadata = programMetadata.roms[hash];
console.log("Program metadata:", programMetadata);
console.log("ROM metadata:", romMetadata);
// Check to see if we can run this program in our interpreter by finding the
// intersection between what we support and what the ROM supports. The order of
// the ROM's platforms array defines its preference, so take the first match.
const platformsWeSupport = ["modernChip8", "superchip"];
const platformsTheRomSupports = romMetadata.platforms;
const chosenPlatform = platformsTheRomSupports.find((p) =>
platformsWeSupport.includes(p)
);
console.log("Running the interpreter with platform:", chosenPlatform);
if (!chosenPlatform) {
console.error("We don't support the requested platform(s) for this ROM");
process.exit();
}
// startInterpreter(file, chosenPlatform);