Skip to content

Instantly share code, notes, and snippets.

@taniarascia
Last active February 13, 2024 07:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taniarascia/6b0f7f9fbaafbf851c21b8d5cd82ccec to your computer and use it in GitHub Desktop.
Save taniarascia/6b0f7f9fbaafbf851c21b8d5cd82ccec to your computer and use it in GitHub Desktop.
RomBuffer.js
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