Skip to content

Instantly share code, notes, and snippets.

@taniarascia taniarascia/RomBuffer.js
Last active Jan 2, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.