Skip to content

Instantly share code, notes, and snippets.

@apage43
Created April 8, 2012 15:18
Show Gist options
  • Save apage43/2337915 to your computer and use it in GitHub Desktop.
Save apage43/2337915 to your computer and use it in GitHub Desktop.
#!/usr/bin/env coffee
fs = require 'fs'
program = require 'commander'
tpls = {
pop: {
hdr: ""
word: (word) ->
"""
SET POP, 0x#{word.toString(16)}\n
"""
end:"""
SET SP, 0
SET PC, 0\n
"""
}
dat: {
hdr: "DAT "
word: (word) -> "0x#{word.toString(16)}, "
end: "0\n"
}
}
program
.version('0.0.1')
.usage('[options] <image>')
.option('-E, --endian <fmt>', 'Word-endianness of input-image', 'little')
.option('-O, --outfmt <fmt>', 'Output munger', 'pop')
.parse(process.argv)
image = fs.readFileSync(program.args[0])
mtpl = tpls[program.outfmt]
words = image.length / 2
out = mtpl.hdr
if program.endian is 'little'
out += mtpl.word(image.readUInt16LE(i * 2)) for i in [0..words - 1]
else
out += mtpl.word(image.readUInt16BE(i * 2)) for i in [0..words - 1]
out += mtpl.end
console.log out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment