Last active
February 13, 2024 07:49
-
-
Save taniarascia/6b0f7f9fbaafbf851c21b8d5cd82ccec to your computer and use it in GitHub Desktop.
RomBuffer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RomBuffer { | |
constructor(filename) { | |
// 16-bit big endian values | |
this.data = [] | |
let buffer = fs.readFileSync(filename) | |
if (buffer.length % 2 !== 0) throw new Error('Epic ROM Fail') | |
for (let i = 0; i < buffer.length; i += 2) { | |
this.data.push((buffer[i] << 8) | (buffer[i + 1] << 0)) | |
} | |
} | |
dump() { | |
let lines = [] | |
for (let i = 0; i < this.data.length; i += 8) { | |
let address = (i * 2).toString(16).padStart(8, '0') | |
let block = this.data.slice(i, i + 8) | |
let hexString = block.map(value => value.toString(16).padStart(4, '0')).join(' ') | |
lines.push(`${address} ${hexString}`) | |
} | |
return lines.join('\n') | |
} | |
} | |
let fs = require('fs') | |
let file = process.argv.slice(2)[0] | |
let romBuffer = new RomBuffer(file) | |
console.log(romBuffer.dump()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment